home *** CD-ROM | disk | FTP | other *** search
- /* Xceed Encryption Library - RSASign Sample Application
- * Copyright (c) 2001 Xceed Software Inc.
- *
- * [Utility.h]
- *
- * This utility header files contains declarations of useful
- * functions.
- *
- * This file is part of the Xceed Encryption Library sample
- * applications. The source code in this file is only intended as
- * a supplement to Xceed Encryption Library's documentation,
- * and is provided "as is", without warranty of any kind, either
- * expressed or implied.
- */
-
- #include "stdafx.h"
- #include "utility.h"
-
- //--------------------------------------------------------------------------
- // Convert the byte array to an hexadecimal representation and save the
- // latter in the specified file
- //--------------------------------------------------------------------------
- bool WriteHexValueToFile( LPCSTR szFileName,
- BYTE* pcBinaryValue,
- short nBinaryValueSize )
- {
- bool bSuccess = true;
-
- HANDLE hOutFile = NULL;
-
- hOutFile = CreateFile( szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
-
- if( hOutFile == INVALID_HANDLE_VALUE )
- {
- printf( "Error creating file %s\n", szFileName );
- DisplayLastError();
- return false;
- }
-
- char* szHexValue = NULL;
- DWORD dwBytesWritten = 0;
-
- BinaryToHex( pcBinaryValue, nBinaryValueSize, &szHexValue );
-
- if( !WriteFile(hOutFile, szHexValue, lstrlen(szHexValue), &dwBytesWritten, NULL) )
- {
- printf( "Error writing to file %s\n", szFileName );
- DisplayLastError();
- bSuccess = false;
- }
-
- CloseHandle( hOutFile );
- delete [] szHexValue;
-
- return bSuccess;
- }
-
- //--------------------------------------------------------------------------
- // Read the hexadecimal string in the specified file, convert it to a
- // byte array which is returned.
- //--------------------------------------------------------------------------
- bool ReadHexValueFromFile( LPCSTR szFileName,
- BYTE** ppcBinaryValue,
- short* pnBinaryValueSize )
- {
- bool bSuccess = true;
-
- HANDLE hInFile = NULL;
-
- hInFile = CreateFile( szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
-
- if( hInFile == INVALID_HANDLE_VALUE )
- {
- printf( "Error opening file %s\n", szFileName );
- DisplayLastError();
- return false;
- }
-
- DWORD dwFileSize = GetFileSize( hInFile, NULL );
-
- if( dwFileSize == 0xFFFFFFFF )
- {
- printf( "Error reading file size of %s\n", szFileName );
- DisplayLastError();
- bSuccess = false;
- }
- else
- {
- char* szHexValue = new char[ dwFileSize + 1 ];
- DWORD dwBytesRead = 0;
-
- if( ReadFile( hInFile, szHexValue, dwFileSize, &dwBytesRead, NULL) && dwBytesRead == dwFileSize )
- {
- szHexValue[ dwFileSize ] = '\0';
- HexToBinary( szHexValue, ppcBinaryValue, pnBinaryValueSize );
- }
- else
- {
- printf( "Error reading file size of %s\n", szFileName );
- DisplayLastError();
- bSuccess = false;
- }
-
- delete [] szHexValue;
- }
-
- CloseHandle( hInFile );
-
- return bSuccess;
- }
-
- //------------------------------------------------------------------------------------
- // Convert an hexadecimal string to a byte array
- //------------------------------------------------------------------------------------
- void HexToBinary( LPCSTR szHexValue,
- BYTE** ppcBinaryValue,
- short* pnBinaryValueSize )
- {
- long lHexLen = lstrlen( szHexValue );
-
- *ppcBinaryValue = NULL;
- *pnBinaryValueSize = 0;
-
- if( lHexLen > 0 )
- {
- *ppcBinaryValue = new BYTE[ lHexLen / 2 + 1 ];
- long I = 0;
-
- while( I < lHexLen )
- {
- int nTemp;
-
- sscanf( szHexValue + I, "%2x", &nTemp );
- (*ppcBinaryValue)[ *pnBinaryValueSize ] = nTemp;
- (*pnBinaryValueSize)++;
- I += 2;
- }
- }
- }
-
- //------------------------------------------------------------------------------------
- // Convert a byte array to an hexadecimal string.
- //------------------------------------------------------------------------------------
- void BinaryToHex( BYTE* pcBinaryValue,
- short nBinaryValueSize,
- char** pszHexValue )
- {
- if( nBinaryValueSize < 0 )
- return;
-
- *pszHexValue = new char[ nBinaryValueSize * 2 + 1 ];
- (*pszHexValue)[0] = '\0';
-
- for( long I = 0; I < nBinaryValueSize; I++ )
- {
- wsprintf( (*pszHexValue) + (I * 2), "%02X", pcBinaryValue[I] );
- }
- }
-
- //--------------------------------------------------------------------------
- // Display a textual representation of the last system error that occured.
- //--------------------------------------------------------------------------
- void DisplayLastError()
- {
- LPVOID lpMsgBuf;
-
- FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, GetLastError(), 0, // Default language
- (LPTSTR) &lpMsgBuf, 0, NULL );
- printf( (char*)lpMsgBuf );
- printf( "\n" );
-
- LocalFree( lpMsgBuf );
- }
-
-
- //
- // END_OF_FILE
- //
-