home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncryption.Cab / F112991_Utility.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-28  |  5.0 KB  |  180 lines

  1. /* Xceed Encryption Library - RSASign Sample Application
  2.  * Copyright (c) 2001 Xceed Software Inc.
  3.  *
  4.  * [Utility.h]
  5.  *
  6.  * This utility header files contains declarations of useful
  7.  * functions.
  8.  *
  9.  * This file is part of the Xceed Encryption Library sample 
  10.  * applications. The source code in this file is only intended as 
  11.  * a supplement to Xceed Encryption Library's documentation, 
  12.  * and is provided "as is", without warranty of any kind, either 
  13.  * expressed or implied. 
  14.  */
  15.  
  16. #include "stdafx.h"
  17. #include "utility.h"
  18.  
  19. //--------------------------------------------------------------------------
  20. // Convert the byte array to an hexadecimal representation and save the 
  21. // latter in the specified file
  22. //--------------------------------------------------------------------------
  23. bool WriteHexValueToFile( LPCSTR szFileName, 
  24.                           BYTE* pcBinaryValue, 
  25.                           short nBinaryValueSize )
  26. {
  27.   bool bSuccess = true;
  28.  
  29.   HANDLE hOutFile = NULL;
  30.  
  31.   hOutFile = CreateFile( szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
  32.  
  33.   if( hOutFile == INVALID_HANDLE_VALUE )
  34.   {
  35.     printf( "Error creating file %s\n", szFileName );
  36.     DisplayLastError();
  37.     return false;
  38.   }
  39.  
  40.   char* szHexValue = NULL;
  41.   DWORD dwBytesWritten = 0;
  42.   
  43.   BinaryToHex( pcBinaryValue, nBinaryValueSize, &szHexValue );
  44.   
  45.   if( !WriteFile(hOutFile, szHexValue, lstrlen(szHexValue), &dwBytesWritten, NULL) )
  46.   {
  47.     printf( "Error writing to file %s\n", szFileName );
  48.     DisplayLastError();
  49.     bSuccess = false;
  50.   }
  51.  
  52.   CloseHandle( hOutFile );
  53.   delete [] szHexValue;
  54.  
  55.   return bSuccess;
  56. }
  57.  
  58. //--------------------------------------------------------------------------
  59. // Read the hexadecimal string in the specified file, convert it to a
  60. // byte array which is returned.
  61. //--------------------------------------------------------------------------
  62. bool ReadHexValueFromFile( LPCSTR szFileName, 
  63.                            BYTE** ppcBinaryValue, 
  64.                            short* pnBinaryValueSize )
  65. {
  66.   bool bSuccess = true;
  67.  
  68.   HANDLE hInFile = NULL;
  69.  
  70.   hInFile = CreateFile( szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
  71.  
  72.   if( hInFile == INVALID_HANDLE_VALUE )
  73.   {
  74.     printf( "Error opening file %s\n", szFileName );
  75.     DisplayLastError();
  76.     return false;
  77.   }
  78.  
  79.   DWORD dwFileSize = GetFileSize( hInFile, NULL );
  80.  
  81.   if( dwFileSize == 0xFFFFFFFF )
  82.   {
  83.     printf( "Error reading file size of %s\n", szFileName );
  84.     DisplayLastError();
  85.     bSuccess = false;
  86.   }
  87.   else
  88.   {
  89.     char* szHexValue = new char[ dwFileSize + 1 ];
  90.     DWORD dwBytesRead = 0;
  91.  
  92.     if( ReadFile( hInFile, szHexValue, dwFileSize, &dwBytesRead, NULL) && dwBytesRead == dwFileSize )
  93.     {
  94.       szHexValue[ dwFileSize ] = '\0';
  95.       HexToBinary( szHexValue, ppcBinaryValue, pnBinaryValueSize );
  96.     }
  97.     else
  98.     {
  99.       printf( "Error reading file size of %s\n", szFileName );
  100.       DisplayLastError();
  101.       bSuccess = false;
  102.     }
  103.  
  104.     delete [] szHexValue;
  105.   }
  106.   
  107.   CloseHandle( hInFile );
  108.  
  109.   return bSuccess;
  110. }
  111.  
  112. //------------------------------------------------------------------------------------
  113. // Convert an hexadecimal string to a byte array
  114. //------------------------------------------------------------------------------------
  115. void HexToBinary( LPCSTR szHexValue, 
  116.                   BYTE** ppcBinaryValue, 
  117.                   short* pnBinaryValueSize )
  118. {
  119.   long lHexLen = lstrlen( szHexValue );
  120.  
  121.   *ppcBinaryValue = NULL;
  122.   *pnBinaryValueSize = 0;
  123.  
  124.   if( lHexLen > 0 )
  125.   {
  126.     *ppcBinaryValue = new BYTE[ lHexLen / 2 + 1 ];
  127.     long I = 0;
  128.  
  129.     while( I < lHexLen )
  130.     {
  131.       int nTemp;
  132.  
  133.       sscanf( szHexValue + I, "%2x", &nTemp );
  134.       (*ppcBinaryValue)[ *pnBinaryValueSize ] = nTemp;
  135.       (*pnBinaryValueSize)++;
  136.       I += 2;
  137.     }
  138.   }
  139. }
  140.  
  141. //------------------------------------------------------------------------------------
  142. // Convert a byte array to an hexadecimal string.
  143. //------------------------------------------------------------------------------------
  144. void BinaryToHex( BYTE* pcBinaryValue,
  145.                   short nBinaryValueSize,
  146.                   char** pszHexValue )
  147. {
  148.   if( nBinaryValueSize < 0 )
  149.     return;
  150.  
  151.   *pszHexValue = new char[ nBinaryValueSize * 2 + 1 ];
  152.   (*pszHexValue)[0] = '\0';
  153.  
  154.   for( long I = 0; I < nBinaryValueSize; I++ )
  155.   {
  156.     wsprintf( (*pszHexValue) + (I * 2), "%02X", pcBinaryValue[I] );
  157.   }
  158. }
  159.  
  160. //--------------------------------------------------------------------------
  161. // Display a textual representation of the last system error that occured.
  162. //--------------------------------------------------------------------------
  163. void DisplayLastError()
  164. {
  165.   LPVOID lpMsgBuf;
  166.  
  167.   FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  168.                  NULL, GetLastError(), 0, // Default language
  169.                  (LPTSTR) &lpMsgBuf, 0, NULL );
  170.   printf( (char*)lpMsgBuf );
  171.   printf( "\n" );
  172.  
  173.   LocalFree( lpMsgBuf );
  174. }
  175.  
  176.  
  177. //
  178. // END_OF_FILE
  179. //
  180.