Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
executable file
·
125 lines (103 loc) · 2.23 KB

File metadata and controls

executable file
·
125 lines (103 loc) · 2.23 KB
 
Jul 24, 2011
Jul 24, 2011
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "utl_crc32.h"
int utl_crc32Init(CRC32_CTX *crcContext)
{
int i,j;
CrcUint32 c;
/* Sanity check context pointer */
if (crcContext==NULL) {
return(-1);
}
/*
* Build auxiliary table for parallel byte-at-a-time CRC-32
*/
#ifdef ORIGINAL_METHOD
for (i = 0; i < 256; ++i) {
for (c = i << 24, j = 8; j > 0; --j) {
c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
}
crcContext->crc32_table[i] = c;
}
#else
for (i=0; i<256; i++) {
c = i;
for (j=8; j>0; j--) {
if (c & 1) {
c = (c >> 1) ^ CRC32_POLY;
} else {
c >>= 1;
}
}
crcContext->crc32_table[i] = c;
}
#endif
return(0);
}
/* Complete CRC32 calculation on a memory block */
int utl_crc32Calc(CRC32_CTX * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
{
if (utl_crc32CalcStart(crcContext,crc32)) {
return(-1);
}
if (utl_crc32CalcBuffer(crcContext, inBuf, inLen, crc32)) {
return(-1);
}
if (utl_crc32CalcEnd(crcContext, crc32)) {
return(-1);
}
return(0);
}
/* Start crc calculation */
int utl_crc32CalcStart(CRC32_CTX * crcContext, CrcUint32 *crc32)
{
/* Sanity check pointers */
if (crcContext==NULL) {
*crc32=0;
return(-1);
}
/*
* Preload shift register, per CRC-32 spec
*/
*crc32 = 0xffffffff;
return(0);
}
/* Finish crc calculation */
int utl_crc32CalcEnd(CRC32_CTX * crcContext, CrcUint32 *crc32)
{
/*
* Return complement, per CRC-32 spec
*/
*crc32 = (~(*crc32));
return(0);
}
/* Include memory block in crc */
int utl_crc32CalcBuffer(CRC32_CTX * crcContext, CrcUint8 *inBuf, CrcUint32 inLen, CrcUint32 *crc32)
{
CrcUint8 *p;
register CrcUint32 crc;
/* Sanity check pointers */
if (crcContext==NULL) {
*crc32=0;
return(-1);
}
/*
* Calculate CRC from data
*/
crc = *crc32;
for (p = inBuf; inLen > 0; ++p, --inLen) {
#ifdef ORIGINAL_METHOD
crc = (crc << 8) ^ crcContext->crc32_table[(crc >> 24) ^ *p];
#else
crc = ((crc >> 8) & 0x00FFFFFF) ^ crcContext->crc32_table[ (crc ^ *p) & 0xFF ];
#endif
}
*crc32 = crc;
return(0);
}
int utl_crc32Done(CRC32_CTX * crcContext)
{
/* Sanity check context pointer */
if (crcContext==NULL) {
return(-1);
}
return(0);
}