]> Joshua Wise's Git repositories - netwatch.git/blobdiff - lib/crc32.c
Add crc32.
[netwatch.git] / lib / crc32.c
diff --git a/lib/crc32.c b/lib/crc32.c
new file mode 100644 (file)
index 0000000..fd7bd49
--- /dev/null
@@ -0,0 +1,34 @@
+#include <stdint.h>
+
+/* code from http://www.faqs.org/faqs/compression-faq/part1/section-26.html,
+ * presumed public domain */
+
+uint32_t crc32_table[256];
+
+uint32_t crc32(uint8_t *buf, int len)
+{
+       uint8_t *p;
+       uint32_t crc;
+
+       crc = 0xffffffff;       /* preload shift register, per CRC-32 spec */
+       for (p = buf; len > 0; ++p, --len)
+               crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *p];
+       return ~crc;        /* transmit complement, per CRC-32 spec */
+}
+
+/*
+ * Build auxiliary table for parallel byte-at-a-time CRC-32.
+ */
+#define CRC32_POLY 0x04c11db7     /* AUTODIN II, Ethernet, & FDDI */
+
+void crc32_init()
+{
+       int i, j;
+       uint32_t c;
+
+       for (i = 0; i < 256; ++i) {
+               for (c = i << 24, j = 8; j > 0; --j)
+                       c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
+               crc32_table[i] = c;
+       }
+}
This page took 0.022483 seconds and 4 git commands to generate.