The sample code provided is in the C programming language. (See also ISO 3309 and ITU-T V.42 for a formal specification.)
/* table of crc's of all 8-bit messages */
unsigned long crc_table[256];
/* Flag: has the table been computed? Initially false. */
int crc_table_computed = 0;
/* make the table for a fast crc */
void make_crc_table(void)
{
unsigned long c;
int n, k;
for (n = 0; n < 256; n++) {
c = (unsigned long) n;
for (k = 0; k < 8; k++)
c = (c & 1) ? (0xedb88320L ^ (c >> 1)) : (c >> 1);
crc_table[n] = c;
}
crc_table_computed = 1;
}
/* update a running crc with the bytes buf[0..len-1]--the crc should be
initialized to all 1's, and the transmitted value is the 1's complement
of the final running crc (see the crc() routine below)). */
unsigned long update_crc(unsigned long crc, unsigned char *buf, int len)
{
unsigned long c = crc;
unsigned char *p = buf;
int n = len;
if (!crc_table_computed)
make_crc_table();
if (n > 0) do {
c = crc_table[(c ^ (*p++)) & 0xff] ^ (c >> 8);
} while (--n);
return c;
}
/* return the crc of the bytes buf[0..len-1] */
unsigned long crc(unsigned char *buf, int len)
{
if (!crc_table_computed)
make_crc_table();
return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
}