#include <stdio.h>
#include <string.h>
#include <windows.h>
/* All function indices in FastCRC.dll can be found in:*/
/* FastCRC Library Help (FastCRC Library Run-Time Dynamic Linking page) */
#define CRC32_INIT_INDEX 101
#define CRC32_UPDATE_INDEX 102
#define CRC32_FINAL_INDEX 104
#define CRC32_CALCULATE_INDEX 105
#define FCRC_CONVERTTOHEX32_INDEX 51
/* This constant can be found in FastCRC.h */
#define FCRC_MAXHEXSIZE 9
/* Declare pointer types for each function you need to use */
typedef void ( _stdcall *PINIT )( unsigned long* );
typedef void ( _stdcall *PUPDATE )( unsigned long*, const void*, unsigned int );
typedef unsigned long ( _stdcall *PFINAL )( unsigned long* );
typedef unsigned long ( _stdcall *PCALCULATE )( const void*, unsigned int );
typedef void ( _stdcall *PCONVERTTOHEX32 )( char*, unsigned long, int );
int main()
{
PINIT pInit;
PUPDATE pUpdate;
PFINAL pFinal;
PCALCULATE pCalculate;
PCONVERTTOHEX32 pConvertToHex32;
char buff[ 256 ];
unsigned long checksum;
char checksumhex[ FCRC_MAXHEXSIZE ]; /*0 terminated*/
unsigned long crcvar;
/*****Load FastCRC.dll*************/
HMODULE hModule = LoadLibrary( "FastCRC.dll" );
if( !hModule )
{
printf( "Could not load FastCRC.dll library" );
return 1;
}
/*****Get all function addresses*****/
pInit = (PINIT)GetProcAddress( hModule, (LPCSTR)CRC32_INIT_INDEX );
pUpdate = (PUPDATE)GetProcAddress( hModule, (LPCSTR)CRC32_UPDATE_INDEX );
pFinal = (PFINAL)GetProcAddress( hModule, (LPCSTR)CRC32_FINAL_INDEX );
pCalculate = (PCALCULATE)GetProcAddress( hModule, (LPCSTR)CRC32_CALCULATE_INDEX );
pConvertToHex32 = (PCONVERTTOHEX32)GetProcAddress( hModule, (LPCSTR)FCRC_CONVERTTOHEX32_INDEX );
do
{
/*****Get the string from the user****************************************/
printf( "\nEnter a string: " );
gets( buff );
printf( "\nDigest for \"%s\":", buff );
/*****Calculate the checksum using Calculate******************************/
printf( "\nCalculated using Calculate: " );
checksum = ( *pCalculate )( buff, strlen( buff ) );
( *pConvertToHex32 )( checksumhex, checksum, 0 );
printf( "%s", checksumhex );
/*****Initialize the context before calling Update, Final, or FinalHex****/
( *pInit )( &crcvar );
/*****Calculate the checksum using Update and Final***********************/
printf( "\nCalculated using Update and Final: " );
( *pUpdate )( &crcvar, buff, strlen( buff ) );
checksum = ( *pFinal )( &crcvar ); /* Final reinitializes the crcvar for the next use */
( *pConvertToHex32 )( checksumhex, checksum, 1 );
printf( "%s", checksumhex );
/*****Continue?***********************************************************/
printf( "\nContinue (Y/N)?" );
gets( buff );
}while( *buff == 'y' || *buff == 'Y' );
FreeLibrary( hModule );
return 0;
}
|