X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=blobdiff_plain;f=geekos%2Fsrc%2Fgeekos%2Fcrc32.c;fp=geekos%2Fsrc%2Fgeekos%2Fcrc32.c;h=a349badc2ceb90ce4363010350fe15524f5c2d8c;hp=0000000000000000000000000000000000000000;hb=ddc16b0737cf58f7aa90a69c6652cdf4090aec51;hpb=626595465a2c6987606a6bc697df65130ad8c2d3 diff --git a/geekos/src/geekos/crc32.c b/geekos/src/geekos/crc32.c new file mode 100644 index 0000000..a349bad --- /dev/null +++ b/geekos/src/geekos/crc32.c @@ -0,0 +1,53 @@ +/* Copyright abandoned; this code is in the public domain. */ +/* Provided to GNUnet by peter@horizon.com */ + +/* + * Adapted for GeekOS by David Hovemeyer + * Code downloaded from OVM (http://www.ovmj.org) + */ + +#include +#include +#include +#include + +#define POLYNOMIAL (ulong_t)0xedb88320 +static ulong_t crc_table[256]; + +/* + * This routine writes each crc_table entry exactly once, + * with the correct final value. Thus, it is safe to call + * even on a table that someone else is using concurrently. + */ +void Init_CRC32(void) { + unsigned int i, j; + ulong_t h = 1; + PrintBoth("Initializing CRC32\n"); + crc_table[0] = 0; + for (i = 128; i; i >>= 1) { + h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); + /* h is now crc_table[i] */ + for (j = 0; j < 256; j += 2*i) + crc_table[i+j] = crc_table[j] ^ h; + } +} + +/* + * This computes the standard preset and inverted CRC, as used + * by most networking standards. Start by passing in an initial + * chaining value of 0, and then pass in the return value from the + * previous crc32() call. The final return value is the CRC. + * Note that this is a little-endian CRC, which is best used with + * data transmitted lsbit-first, and it should, itself, be appended + * to data in little-endian byte and bit order to preserve the + * property of detecting all burst errors of length 32 bits or less. + */ +ulong_t crc32(ulong_t crc, char const *buf, size_t len) { + KASSERT(crc_table[255] != 0); + crc ^= 0xffffffff; + while (len--) + crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; + return crc ^ 0xffffffff; +} + +/* end of crc32.c */