Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Initial revision
[palacios.git] / palacios / src / geekos / crc32.c
1 /* Copyright abandoned; this code is in the public domain. */
2 /* Provided to GNUnet by peter@horizon.com */
3
4 /*
5  * Adapted for GeekOS by David Hovemeyer
6  * Code downloaded from OVM (http://www.ovmj.org)
7  */
8
9 #include <geekos/crc32.h>
10 #include <geekos/kassert.h>
11 #include <geekos/serial.h>
12
13 #define POLYNOMIAL (ulong_t)0xedb88320
14 static ulong_t crc_table[256];
15
16 /*
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.
20  */
21 void Init_CRC32(void) {
22   unsigned int i, j;
23   ulong_t h = 1;
24   PrintBoth("Initializing CRC32\n");
25   crc_table[0] = 0;
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;
31   }
32 }
33
34 /*
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.
43  */
44 ulong_t crc32(ulong_t crc, char const *buf, size_t len) {
45   KASSERT(crc_table[255] != 0);
46   crc ^= 0xffffffff;
47   while (len--)
48     crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
49   return crc ^ 0xffffffff;
50 }
51
52 /* end of crc32.c */