aboutsummaryrefslogtreecommitdiff
path: root/src/libflate/crc.c
blob: 8085c23e8189ef82d9bc90b2d7f51845f2798422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <u.h>
#include <libc.h>
#include <flate.h>

uint32*
mkcrctab(uint32 poly)
{
	uint32 *crctab;
	uint32 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;
}

uint32
blockcrc(uint32 *crctab, uint32 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;
}