Category : Utilities for DOS and Windows Machines
Archive   : CRCDOS3.ZIP
Filename : CRCDOS.C

 
Output of file : CRCDOS.C contained in archive : CRCDOS3.ZIP

/*********************************************************************/
/* Module name: crcdos.c */
/* Date: 11 Jan 88 */
/* Environment: Turbo C 1.0 */
/* Author: R. E. Faith */
/* Notice: Public Domain: The following conditions apply to use: */
/* 1) No fee shall be charged for distribution. */
/* 2) Modifications may be made, but authorship information */
/* for all contributing authors shall be retained. */
/* 3) This code may not be included as part of a commercial */
/* package. */
/* This program is provided AS IS without any warranty, expressed or */
/* implied, including, but not limited to, fitness for a particular */
/* purpose. */
/*********************************************************************/
static char *__ATH__ = "@(#)Public domain (P) 1988, R. E. Faith";
static char *__VER__ = "@(#)crcdos: version 1.0, 11 Jan 88";

#include
#include
#include
#include
#include
#include

#define BUF_SIZE 0x2800
static char buffer[BUF_SIZE];
#define DO_16 1 /* otherwise, do crc ccitt style */
static int Build = 0, Compare = 0, Silent = 0;

static void usage( void )
{
fprintf( stderr, "%s\n", &__VER__[4] );
fprintf( stderr, "%s\n\n", &__ATH__[4] );
fprintf( stderr,
"usage: crcdos [-s] [-b crc_file list_file] [-c crc_file]\n" );
fprintf( stderr, "-s: be silent when comparing files\n" );
fprintf( stderr, "-b: build a good crc file\n" );
fprintf( stderr, "-c: compare current crc's to file\n" );
exit( 1 );
}

static WORD get_crc( char *filename )
{
register FILE *str;
register int size;
register WORD crc = 0;

if ( (str = fopen( filename, "rb" )) == NULL ) {
fprintf( stderr, "crcdos: cannot open %s for read",
fntounix( filename ) );
perror( "" );
return 0;
}

while ( (size = fread( buffer, 1, BUF_SIZE, str )) != 0 ) {
#ifdef DO_16
crc = get_crc_16( crc, buffer, size );
#else
crc = get_crc_ccitt( crc, buffer, size );
#endif
}

fclose( str );

return crc;
}

static void build( char *target, char *infile )
{
char path[MAXPATH];
register FILE *str, *instr;
register WORD crc;

if ( (instr = fopen( infile, "r" )) == NULL) {
fprintf( stderr, "crcdos: cannot open %s for read",
fntounix( infile ) );
perror( "" );
exit( 1 );
}

if ( (str = fopen( target, "w" )) == NULL ) {
fprintf( stderr, "crcdos: cannot open %s for write",
fntounix( target ) );
perror( "" );
exit( 1 );
}

while (fgets( path, MAXPATH, instr )) {
fntomsdos( path );
crc = get_crc( path );
printf( "%s: crc = %04x\n", path, crc );
fprintf( str, "%s %04x\n", path, crc );
}

fclose( str );
fclose( instr );
}

static void compare( char *filename )
{
char path[MAXPATH];
register FILE *str;
register WORD newcrc;
WORD oldcrc;

if ( (str = fopen( filename, "r" )) == NULL ) {
fprintf( stderr, "crcdos: cannot open %s for write",
fntounix( filename ) );
perror( "" );
exit( 1 );
}

while (fscanf( str, "%s %x", path, &oldcrc ) != EOF) {
fntomsdos( path );
newcrc = get_crc( path );
if (!Silent)
printf( "%s: oldcrc = %04x, newcrc = %04x\n",
path, oldcrc, newcrc );
if (newcrc != oldcrc) {
printf( "\n*** WARNING: %s has been altered! *******\n",
path );
printf( "*** The crc of %s was %04x and now is %04x\n",
path, oldcrc, newcrc );
printf( "*** If there is no reason for this change, a virus could have\n");
printf( "*** infected the file! Consider replacing this file with a\n" );
printf( "*** sterile backup copy. Please realize that some files are\n" );
printf( "*** designed to be self-modifying: this is only a warning.\n\n" );
}
}

fclose( str );

}

main( int argc, char **argv )
{
int ch;
char target[MAXPATH], infile[MAXPATH];

if (_osmajor < 2) {
fprintf( stderr, "Requires DOS 2.x or greater!\n" );
exit( 1 );
}

harderr( chandler );
ctrlbrk( c_break );

opterr = 0; /* Don't let getopt() report errors */
while ((ch = getopt( argc, argv, "sb:c:" )) != EOF)
switch (ch) {
case 's': Silent = !Silent; break;
case 'b':
Build = 1;
strcpy( target, optarg );
fntomsdos( target );
break;
case 'c':
Compare = 1;
strcpy( target, optarg );
fntomsdos( target );
break;
default:
usage();
}

if (Build && Compare) {
usage();
exit( 0 );
}

if (Build) {
if (optind >= argc)
usage();
strcpy( infile, argv[optind] );
fntomsdos( infile );
printf( "Building a list of valid crc's from the list in %s\n",
infile );
build( target, infile );
printf( "The crc list has been stored in %s\n", target );
printf( "To check the crc's, use:\n" );
printf( " crcdos -c %s or\n", target );
printf( " crcdos -sc %s (for silent checking)\n",
target );
exit( 0 );
}

if (Compare) {
if (!Silent)
printf( "Comparing crc's with the list in %s\n",
target );
compare( target );
exit( 0 );
}

usage();
}