1 /* Copyright abandoned; this code is in the public domain. */
2 /* Provided to GNUnet by peter@horizon.com */
5 * Adapted for GeekOS by David Hovemeyer
6 * Code downloaded from OVM (http://www.ovmj.org)
9 #include <geekos/crc32.h>
10 #include <geekos/kassert.h>
11 #include <geekos/serial.h>
13 #define POLYNOMIAL (ulong_t)0xedb88320
14 static ulong_t crc_table[256];
17 * This routine writes each crc_table entry exactly once,
18 * with the correct final value. Thus, it is safe to call
19 * even on a table that someone else is using concurrently.
21 void Init_CRC32(void) {
24 PrintBoth("Initializing CRC32\n");
26 for (i = 128; i; i >>= 1) {
27 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
28 /* h is now crc_table[i] */
29 for (j = 0; j < 256; j += 2*i)
30 crc_table[i+j] = crc_table[j] ^ h;
35 * This computes the standard preset and inverted CRC, as used
36 * by most networking standards. Start by passing in an initial
37 * chaining value of 0, and then pass in the return value from the
38 * previous crc32() call. The final return value is the CRC.
39 * Note that this is a little-endian CRC, which is best used with
40 * data transmitted lsbit-first, and it should, itself, be appended
41 * to data in little-endian byte and bit order to preserve the
42 * property of detecting all burst errors of length 32 bits or less.
44 ulong_t crc32(ulong_t crc, char const *buf, size_t len) {
45 KASSERT(crc_table[255] != 0);
48 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
49 return crc ^ 0xffffffff;