24 # ifndef DYNAMIC_CRC_TABLE
25 # define DYNAMIC_CRC_TABLE
47 # if (UINT_MAX == 0xffffffffUL)
48 typedef unsigned int u4;
50 # if (ULONG_MAX == 0xffffffffUL)
51 typedef unsigned long u4;
53 # if (USHRT_MAX == 0xffffffffUL)
54 typedef unsigned short u4;
65 # define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
66 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
67 local unsigned long crc32_little
OF((
unsigned long,
68 const unsigned char FAR *,
unsigned));
69 local unsigned long crc32_big
OF((
unsigned long,
70 const unsigned char FAR *,
unsigned));
83 #ifdef DYNAMIC_CRC_TABLE
85 local volatile int crc_table_empty = 1;
87 local void make_crc_table
OF((
void));
89 local void write_table
OF((FILE *,
const unsigned long FAR *));
117 local void make_crc_table()
123 static volatile int first = 1;
124 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
134 for (n = 0; n <
sizeof(
p)/
sizeof(
unsigned char); n++)
135 poly |= 1UL << (31 - p[n]);
138 for (n = 0; n < 256; n++) {
139 c = (
unsigned long)n;
140 for (k = 0; k < 8; k++)
141 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
148 for (n = 0; n < 256; n++) {
150 crc_table[4][n] = REV(c);
151 for (k = 1; k < 4; k++) {
152 c = crc_table[0][c & 0xff] ^ (c >> 8);
154 crc_table[k + 4][n] = REV(c);
163 while (crc_table_empty)
172 out = fopen(
"crc32.h",
"w");
173 if (out == NULL)
return;
174 fprintf(out,
"/* crc32.h -- tables for rapid CRC calculation\n");
175 fprintf(out,
" * Generated automatically by crc32.c\n */\n\n");
176 fprintf(out,
"local const unsigned long FAR ");
177 fprintf(out,
"crc_table[TBLS][256] =\n{\n {\n");
178 write_table(out, crc_table[0]);
180 fprintf(out,
"#ifdef BYFOUR\n");
181 for (k = 1; k < 8; k++) {
182 fprintf(out,
" },\n {\n");
183 write_table(out, crc_table[k]);
185 fprintf(out,
"#endif\n");
187 fprintf(out,
" }\n};\n");
194 local void write_table(FILE *out,
const unsigned long FAR *table)
198 for (n = 0; n < 256; n++)
199 fprintf(out,
"%s0x%08lxUL%s", n % 5 ?
"" :
" ", table[n],
200 n == 255 ?
"\n" : (n % 5 == 4 ?
",\n" :
", "));
218 #ifdef DYNAMIC_CRC_TABLE
226 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
227 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
232 if (buf ==
Z_NULL)
return 0UL;
234 #ifdef DYNAMIC_CRC_TABLE
240 if (
sizeof(
void *) ==
sizeof(ptrdiff_t)) {
244 if (*((
unsigned char *)(&endian)))
245 return crc32_little(crc, buf, len);
247 return crc32_big(crc, buf, len);
250 crc = crc ^ 0xffffffffUL;
258 return crc ^ 0xffffffffUL;
264 #define DOLIT4 c ^= *buf4++; \
265 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
266 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
267 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
270 local unsigned long crc32_little(
unsigned long crc,
const unsigned char FAR *buf,
unsigned len)
273 register const u4
FAR *buf4;
277 while (len && ((ptrdiff_t)buf & 3)) {
278 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
282 buf4 = (
const u4
FAR *)(
const void FAR *)buf;
291 buf = (
const unsigned char FAR *)buf4;
294 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
297 return (
unsigned long)c;
301 #define DOBIG4 c ^= *++buf4; \
302 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
303 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
304 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
307 local unsigned long crc32_big(
unsigned long crc,
const unsigned char FAR *buf,
unsigned len)
310 register const u4
FAR *buf4;
314 while (len && ((ptrdiff_t)buf & 3)) {
315 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
319 buf4 = (
const u4
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)(REV(c));
380 odd[0] = 0xedb88320UL;
382 for (n = 1; n <
GF2_DIM; n++) {
local void gf2_matrix_square(unsigned long *square, unsigned long *mat)
local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2)
ABC_NAMESPACE_IMPL_END ABC_NAMESPACE_IMPL_START const unsigned long FAR *ZEXPORT get_crc_table()
local unsigned long gf2_matrix_times(unsigned long *mat, unsigned long vec)
uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2)
#define ABC_NAMESPACE_IMPL_END
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
ABC_NAMESPACE_HEADER_START local const unsigned long FAR crc_table[TBLS][256]
uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2)
#define ABC_NAMESPACE_IMPL_START
local unsigned long gf2_matrix_times OF((unsigned long *mat, unsigned long vec))