26 # ifndef DYNAMIC_CRC_TABLE 27 # define DYNAMIC_CRC_TABLE 34 #if !defined(NOBYFOUR) && defined(Z_U4) 38 local
unsigned long crc32_little OF((
unsigned long,
39 const unsigned char FAR *, z_size_t));
40 local
unsigned long crc32_big OF((
unsigned long,
41 const unsigned char FAR *, z_size_t));
48 local unsigned long gf2_matrix_times
OF((
unsigned long *mat,
50 local void gf2_matrix_square
OF((
unsigned long *square,
unsigned long *mat));
54 #ifdef DYNAMIC_CRC_TABLE 56 local
volatile int crc_table_empty = 1;
57 local z_crc_t FAR crc_table[TBLS][256];
58 local
void make_crc_table OF((
void));
60 local
void write_table OF((FILE *,
const z_crc_t FAR *));
88 local
void make_crc_table()
94 static volatile int first = 1;
95 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
105 for (n = 0; n < (
int)(
sizeof(p)/
sizeof(
unsigned char)); n++)
106 poly |= (z_crc_t)1 << (31 - p[n]);
109 for (n = 0; n < 256; n++) {
111 for (k = 0; k < 8; k++)
112 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
119 for (n = 0; n < 256; n++) {
121 crc_table[4][n] = ZSWAP32(c);
122 for (k = 1; k < 4; k++) {
123 c = crc_table[0][c & 0xff] ^ (c >> 8);
125 crc_table[k + 4][n] = ZSWAP32(c);
134 while (crc_table_empty)
143 out = fopen(
"crc32.h",
"w");
144 if (out == NULL)
return;
145 fprintf(out,
"/* crc32.h -- tables for rapid CRC calculation\n");
146 fprintf(out,
" * Generated automatically by crc32.c\n */\n\n");
147 fprintf(out,
"local const z_crc_t FAR ");
148 fprintf(out,
"crc_table[TBLS][256] =\n{\n {\n");
149 write_table(out, crc_table[0]);
151 fprintf(out,
"#ifdef BYFOUR\n");
152 for (k = 1; k < 8; k++) {
153 fprintf(out,
" },\n {\n");
154 write_table(out, crc_table[k]);
156 fprintf(out,
"#endif\n");
158 fprintf(out,
" }\n};\n");
165 local
void write_table(out, table)
167 const z_crc_t FAR *table;
171 for (n = 0; n < 256; n++)
172 fprintf(out,
"%s0x%08lxUL%s", n % 5 ?
"" :
" ",
173 (
unsigned long)(table[n]),
174 n == 255 ?
"\n" : (n % 5 == 4 ?
",\n" :
", "));
190 #ifdef DYNAMIC_CRC_TABLE 198 #define DO1 crc = crc_table[0
][((int)crc ^ (*buf++)) & 0xff
] ^ (crc >> 8
) 204 const unsigned char FAR *buf;
207 if (buf ==
Z_NULL)
return 0UL;
209 #ifdef DYNAMIC_CRC_TABLE 215 if (
sizeof(
void *) ==
sizeof(ptrdiff_t)) {
219 if (*((
unsigned char *)(&endian)))
220 return crc32_little(crc, buf, len);
222 return crc32_big(crc, buf, len);
225 crc = crc ^ 0xffffffffUL;
233 return crc ^ 0xffffffffUL;
239 const unsigned char FAR *buf;
242 return crc32_z(crc, buf, len);
260 #define DOLIT4 c ^= *buf4++; 261 c = crc_table[3
][c & 0xff
] ^ crc_table[2
][(c >> 8
) & 0xff
] ^ 262 crc_table[1
][(c >> 16
) & 0xff
] ^ crc_table[0
][c >> 24
] 263 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 266 local
unsigned long crc32_little(crc, buf, len)
268 const unsigned char FAR *buf;
272 register const z_crc_t FAR *buf4;
276 while (len && ((ptrdiff_t)buf & 3)) {
277 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
281 buf4 = (
const z_crc_t FAR *)(
const void FAR *)buf;
290 buf = (
const unsigned char FAR *)buf4;
293 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
296 return (
unsigned long)c;
300 #define DOBIG4 c ^= *buf4++; 301 c = crc_table[4
][c & 0xff
] ^ crc_table[5
][(c >> 8
) & 0xff
] ^ 302 crc_table[6
][(c >> 16
) & 0xff
] ^ crc_table[7
][c >> 24
] 303 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 306 local
unsigned long crc32_big(crc, buf, len)
308 const unsigned char FAR *buf;
312 register const z_crc_t FAR *buf4;
314 c = ZSWAP32((z_crc_t)crc);
316 while (len && ((ptrdiff_t)buf & 3)) {
317 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
321 buf4 = (
const z_crc_t FAR *)(
const void FAR *)buf;
330 buf = (
const unsigned char FAR *)buf4;
333 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
336 return (
unsigned long)(ZSWAP32(c));
362 unsigned long *square;
368 square[n] = gf2_matrix_times(mat, mat[n]);
387 odd[0] = 0xedb88320UL;
389 for (n = 1; n <
GF2_DIM; n++) {
395 gf2_matrix_square(even, odd);
398 gf2_matrix_square(odd, even);
404 gf2_matrix_square(even, odd);
406 crc1 = gf2_matrix_times(even, crc1);
414 gf2_matrix_square(odd, even);
416 crc1 = gf2_matrix_times(odd, crc1);
433 return crc32_combine_(crc1, crc2, len2);
441 return crc32_combine_(crc1, crc2, len2);
local void gf2_matrix_square(unsigned long *square, unsigned long *mat)
local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2)
local const z_crc_t FAR crc_table[TBLS][256]
const z_crc_t FAR *ZEXPORT get_crc_table()
local unsigned long gf2_matrix_times(unsigned long *mat, unsigned long vec)
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, z_size_t len)
uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2)