/* +++Date last modified: 05-Jul-1997 */
/*
* Calculate, intelligently, the CRC of a dataset incrementally given a
* buffer full at a time.
* Initialize crc to 0 for XMODEM, -1 for CCITT.
*
* Usage:
* newcrc = updcrc( oldcrc, bufadr, buflen )
* size_t oldcrc, buflen;
* char *bufadr;
*
* Compile with -DTEST to generate program that prints CRC of stdin to stdout.
* Compile with -DMAKETAB to print values for crctab to stdout
*/
#include "crc.h"
/* the CRC polynomial. This is used by XMODEM (almost CCITT).
* If you change P, you must change crctab[]'s initial value to what is
* printed by initcrctab()
*/
#define P 0x1021
/* number of bits in CRC: don't change it. */
#define W 16
/* this the number of bits per char: don't change it. */
#define B 8
static WORD crctab[1<>(W-B)) ^ *cp++];
return( crc );
}
#ifdef MAKETAB
main()
{
initcrctab();
}
initcrctab()
{
register b, v, i;
for( b = 0; b <= (1<= 0; )
v = v&0x8000 ? (v<<1)^P : v<<1;
crctab[b] = v;
printf( "0x%04x,", v & 0xFFFF );
if( (b&7) == 7 )
printf("\n" );
else printf(" ");
}
}
#endif
#ifdef TEST
#include
#if defined(MSDOS) || defined(__MSDOS__)
#include "unistd.h"
#else
#include
#endif
#include
#define MAXBUF 4096
main(int argc, char **argv)
{
int fd = 0;
int nr;
char buf[MAXBUF];
WORD crc;
if( argc > 1 )
{
if( (fd = open( argv[1], O_RDONLY )) < 0 )
{
perror( argv[1] );
exit( -1 );
}
}
crc = 0;
while( (nr = read( fd, buf, MAXBUF )) > 0 )
crc = updcrc( crc, (BYTE *)buf, nr );
printf( "%04x\n", crc );
if( nr != 0 )
perror( "reading" );
return 0;
}
#endif /* TEST */