aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/lzp/crc.c
blob: 6b00c8e1b2e0eb9e585fab695227acde15f73e51 (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdint.h>
#include "lzp.h"

void initTable16(uint16_t *table) {

	int i, j;
    uint16_t crc, c;

    for (i=0; i<256; i++) {

        crc = 0;
        c   = (uint16_t) i;

        for (j=0; j<8; j++) {

            if ( (crc ^ c) & 0x0001 )
				crc = ( crc >> 1 ) ^ 0xA001;
            else
				crc =   crc >> 1;

            c = c >> 1;
        }

        table[i] = crc;
    }

}

void initTable32(uint32_t *table) {

	int i,j;
	uint32_t crcVal;

	for(i=0; i<256; i++) {

		crcVal = i;

		for(j=0; j<8; j++) {

			if (crcVal&0x00000001L)
				crcVal = (crcVal>>1)^0xEDB88320L;
			else
				crcVal = crcVal>>1;

		}

		table[i] = crcVal;

	}

}

uint16_t lzCRC16(const void* buff, int bytes, uint16_t crc) {

	int i;
	uint16_t tmp, short_c;
	uint16_t crcTable[256];

	initTable16(crcTable);

	for(i=0; i<bytes; i++) {

		short_c = 0x00ff & (uint16_t)((const uint8_t *)buff)[i];

		tmp =  crc       ^ short_c;
		crc = (crc >> 8) ^ crcTable[tmp&0xff];

	}

    return(crc);

}

uint32_t lzCRC32(const void* buff, int bytes, uint32_t crc) {

	int	i;
	const uint8_t	*byteBuff = (const uint8_t *)buff;
	uint32_t		byte;
	uint32_t		crcTable[256];

    initTable32(crcTable);

	for(i=0; i<bytes; i++) {

		byte = 0x000000ffL&(uint32_t)byteBuff[i];
		crc = (crc>>8)^crcTable[(crc^byte)&0xff];

	}

	return(crc^0xFFFFFFFF);

}