xref: /inferno-os/os/boot/libflate/crc.c (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1 #include "lib9.h"
2 #include <flate.h>
3 
4 ulong*
mkcrctab(ulong poly)5 mkcrctab(ulong poly)
6 {
7 	ulong *crctab;
8 	ulong crc;
9 	int i, j;
10 
11 	crctab = malloc(256 * sizeof(ulong));
12 	if(crctab == nil)
13 		return nil;
14 
15 	for(i = 0; i < 256; i++){
16 		crc = i;
17 		for(j = 0; j < 8; j++){
18 			if(crc & 1)
19 				crc = (crc >> 1) ^ poly;
20 			else
21 				crc >>= 1;
22 		}
23 		crctab[i] = crc;
24 	}
25 	return crctab;
26 }
27 
28 ulong
blockcrc(ulong * crctab,ulong crc,void * vbuf,int n)29 blockcrc(ulong *crctab, ulong crc, void *vbuf, int n)
30 {
31 	uchar *buf, *ebuf;
32 
33 	crc ^= 0xffffffff;
34 	buf = vbuf;
35 	ebuf = buf + n;
36 	while(buf < ebuf)
37 		crc = crctab[(crc & 0xff) ^ *buf++] ^ (crc >> 8);
38 	return crc ^ 0xffffffff;
39 }
40