summaryrefslogtreecommitdiffstats
path: root/src/libflate/crc.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2003-11-23 18:19:18 +0000
committerrsc <devnull@localhost>2003-11-23 18:19:18 +0000
commitb6afd33e2f23953f00c6fac6b5d45946a9113654 (patch)
tree300478ec8ac939d1b7aa0c1b987367a49a4e47ce /src/libflate/crc.c
parent8a708fb239f4272ac7e4f16f437093c56b2cab39 (diff)
downloadplan9port-b6afd33e2f23953f00c6fac6b5d45946a9113654.tar.gz
plan9port-b6afd33e2f23953f00c6fac6b5d45946a9113654.zip
add libflate
Diffstat (limited to 'src/libflate/crc.c')
-rw-r--r--src/libflate/crc.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libflate/crc.c b/src/libflate/crc.c
new file mode 100644
index 00000000..4da78f6c
--- /dev/null
+++ b/src/libflate/crc.c
@@ -0,0 +1,40 @@
+#include <u.h>
+#include <libc.h>
+#include <flate.h>
+
+ulong*
+mkcrctab(ulong poly)
+{
+ ulong *crctab;
+ ulong crc;
+ int i, j;
+
+ crctab = malloc(256 * sizeof(ulong));
+ if(crctab == nil)
+ return nil;
+
+ for(i = 0; i < 256; i++){
+ crc = i;
+ for(j = 0; j < 8; j++){
+ if(crc & 1)
+ crc = (crc >> 1) ^ poly;
+ else
+ crc >>= 1;
+ }
+ crctab[i] = crc;
+ }
+ return crctab;
+}
+
+ulong
+blockcrc(ulong *crctab, ulong crc, void *vbuf, int n)
+{
+ uchar *buf, *ebuf;
+
+ crc ^= 0xffffffff;
+ buf = vbuf;
+ ebuf = buf + n;
+ while(buf < ebuf)
+ crc = crctab[(crc & 0xff) ^ *buf++] ^ (crc >> 8);
+ return crc ^ 0xffffffff;
+}