24 # ifndef DYNAMIC_CRC_TABLE
25 # define DYNAMIC_CRC_TABLE
38 # if (UINT_MAX == 0xffffffffUL)
39 typedef unsigned int u4;
41 # if (ULONG_MAX == 0xffffffffUL)
42 typedef unsigned long u4;
44 # if (USHRT_MAX == 0xffffffffUL)
45 typedef unsigned short u4;
56 # define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
57 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58 local
unsigned long crc32_little OF((
unsigned long,
59 const unsigned char FAR *,
unsigned));
60 local
unsigned long crc32_big OF((
unsigned long,
61 const unsigned char FAR *,
unsigned));
68 local
unsigned long gf2_matrix_times OF((
unsigned long *mat,
70 local
void gf2_matrix_square OF((
unsigned long *square,
unsigned long *mat));
72 #ifdef DYNAMIC_CRC_TABLE
74 local
volatile int crc_table_empty = 1;
75 local
unsigned long FAR crc_table[TBLS][256];
76 local
void make_crc_table OF((
void));
78 local
void write_table OF((FILE *,
const unsigned long FAR *));
106 local
void make_crc_table()
112 static volatile int first = 1;
113 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
123 for (n = 0; n <
sizeof(p)/
sizeof(
unsigned char); n++)
124 poly |= 1UL << (31 - p[n]);
127 for (n = 0; n < 256; n++) {
128 c = (
unsigned long)n;
129 for (k = 0; k < 8; k++)
130 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
137 for (n = 0; n < 256; n++) {
139 crc_table[4][
n] = REV(c);
140 for (k = 1; k < 4; k++) {
141 c = crc_table[0][c & 0xff] ^ (c >> 8);
143 crc_table[k + 4][
n] = REV(c);
152 while (crc_table_empty)
161 out = fopen(
"crc32.h",
"w");
162 if (out == NULL)
return;
163 fprintf(out,
"/* crc32.h -- tables for rapid CRC calculation\n");
164 fprintf(out,
" * Generated automatically by crc32.c\n */\n\n");
165 fprintf(out,
"local const unsigned long FAR ");
166 fprintf(out,
"crc_table[TBLS][256] =\n{\n {\n");
167 write_table(out, crc_table[0]);
169 fprintf(out,
"#ifdef BYFOUR\n");
170 for (k = 1; k < 8; k++) {
171 fprintf(out,
" },\n {\n");
172 write_table(out, crc_table[k]);
174 fprintf(out,
"#endif\n");
176 fprintf(out,
" }\n};\n");
183 local
void write_table(out,
table)
185 const
unsigned long FAR *
table;
189 for (n = 0; n < 256; n++)
190 fprintf(out,
"%s0x%08lxUL%s", n % 5 ?
"" :
" ", table[n],
191 n == 255 ?
"\n" : (n % 5 == 4 ?
",\n" :
", "));
205 const unsigned long FAR * ZEXPORT get_crc_table()
207 #ifdef DYNAMIC_CRC_TABLE
211 return (
const unsigned long FAR *)crc_table;
215 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
216 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
219 unsigned long ZEXPORT crc32(crc,
buf, len)
221 const
unsigned char FAR *
buf;
224 if (buf == Z_NULL)
return 0UL;
226 #ifdef DYNAMIC_CRC_TABLE
232 if (
sizeof(
void *) ==
sizeof(ptrdiff_t)) {
236 if (*((
unsigned char *)(&endian)))
237 return crc32_little(crc, buf, len);
239 return crc32_big(crc, buf, len);
242 crc = crc ^ 0xffffffffUL;
250 return crc ^ 0xffffffffUL;
256 #define DOLIT4 c ^= *buf4++; \
257 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
258 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
259 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
262 local
unsigned long crc32_little(crc, buf, len)
264 const
unsigned char FAR *buf;
268 register const u4 FAR *buf4;
272 while (len && ((ptrdiff_t)buf & 3)) {
273 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
277 buf4 = (
const u4 FAR *)(
const void FAR *)
buf;
286 buf = (
const unsigned char FAR *)buf4;
289 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
292 return (
unsigned long)c;
296 #define DOBIG4 c ^= *++buf4; \
297 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
298 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
299 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
302 local
unsigned long crc32_big(crc, buf, len)
304 const
unsigned char FAR *buf;
308 register const u4 FAR *buf4;
312 while (len && ((ptrdiff_t)buf & 3)) {
313 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
317 buf4 = (
const u4 FAR *)(
const void FAR *)
buf;
328 buf = (
const unsigned char FAR *)buf4;
331 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
334 return (
unsigned long)(REV(c));
342 local
unsigned long gf2_matrix_times(mat, vec)
359 local
void gf2_matrix_square(square, mat)
360 unsigned
long *square;
365 for (n = 0; n < GF2_DIM; n++)
366 square[n] = gf2_matrix_times(mat, mat[n]);
370 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
377 unsigned long even[GF2_DIM];
378 unsigned long odd[GF2_DIM];
385 odd[0] = 0xedb88320L;
387 for (n = 1; n < GF2_DIM; n++) {
393 gf2_matrix_square(even, odd);
396 gf2_matrix_square(odd, even);
402 gf2_matrix_square(even, odd);
404 crc1 = gf2_matrix_times(even, crc1);
412 gf2_matrix_square(odd, even);
414 crc1 = gf2_matrix_times(odd, crc1);