32-Bit CRC Functions




The CRC32 (Cyclic Redundancy Check) functions are used to calculate a sophisticated checksum based on the algebra of polynomials. The Cyclic Redundancy Check, is a way to detect bit errors that occur during data storage or transmission. The CRC-32 algorithm operates on a block of data as a single large numerical value. The algorithm divides this large value by the CRC-32 polynomial or generator polynomial, leaving the remainder 32-bit, which is the checksum.

The CRC32 functions use a CRC table to calculate a checksum value for a single byte, a raw pattern of bytes, or a file. The CRC table is an array of all possible single-byte values needed to generate a 32-bit CRC for data one a byte at a time for all combinations of incoming bytes. It is based on the Autodin/Ethernet/ADCCP polynomial of 0x4C11DB7:

0000 0100 1100 0001 0001 1101 1011 0111 (binary) or a poly of x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7 +x^5+x^4+x^2+x^1+x^0

In this implementation the coefficient of x^0 is stored in the MSB of the 32-bit word and the coefficient of x^31 is stored in the LSB. Thus 0x4C11DB7 becomes 0xEDB88320:

1110 1101 1011 1000 1000 0011 0010 0000 (binary)


unsigned long calcCRC32(const char *buf, unsigned len) - Calculates a 32-bit CRC for a raw pattern of bytes. Returns a 32-bit checksum.

unsigned long calcCRC32(unsigned char c, unsigned long CRC) - Calculates a 32-bit CRC table value for a single byte. NOTE: The initial CRC value must be set to 0xffffffffL and the final 32-bit value that must be XOR'ed with 0xffffffffL to obtain the checksum.

unsigned long calcCRC32(fstream &infile) - Calculates a 32-bit CRC for a file. Assumes the stream is already open. Returns a 32-bit checksum.

End Of Document