diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libflate/crc.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libflate/crc.c')
-rwxr-xr-x | sys/src/libflate/crc.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/sys/src/libflate/crc.c b/sys/src/libflate/crc.c new file mode 100755 index 000000000..4da78f6cc --- /dev/null +++ b/sys/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; +} |