#include <stdio.h>
#include <QuickHash.h>
#define BUFF_SIZE 1024
int CalculateFileChecksum( const char* filename, char* checksumhex )
{
FILE* file;
unsigned char buff[ BUFF_SIZE ];
unsigned char context[ SLC_ADLER32_CONTEXTSIZE ];
file = fopen( filename, "rb" );
if( file == NULL )
{
printf( "\nCould not open file: %s", filename );
return 0;
}
/*****Initialize the context before calling Update, Final, or FinalHex****/
SL_ADLER32_Init( context );
/*****Calculate the checksum by calling Update for each block of the file***/
while( !feof( file ) )
{
unsigned int nCount = fread( buff, sizeof( char ), BUFF_SIZE, file );
if( ferror( file ) )
{
printf( "\nAn error occurred when accessing the file: %s", filename );
fclose( file );
return 0;
}
SL_ADLER32_Update( context, buff, nCount );
}
/*****Do final changes and get the checksum in hex format*******************/
SL_ADLER32_FinalHex( context, checksumhex, 0 ); /* SL_ADLER32_FinalHex reinitializes the context */
fclose( file );
return 1;
}
int main()
{
char buff[ 256 ];
char checksumhex[ SLC_ADLER32_HEXDIGESTSIZE ]; /*0 terminated*/
do
{
/*****Get the file name from the user*************************************/
printf( "\nEnter a file name:\n" );
gets( buff );
/*****Calculate the checksum************************************************/
if( CalculateFileChecksum( buff, checksumhex ) != 0 )
printf( "\nChecksum: %s", checksumhex );
/*****Continue?***********************************************************/
printf( "\nContinue (Y/N)?" );
gets( buff );
} while ( *buff == 'y' || *buff == 'Y' );
return 0;
}
|