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));
67#ifdef DYNAMIC_CRC_TABLE
69local volatile int crc_table_empty = 1;
71local void make_crc_table
OF((
void));
73 local void write_table
OF((FILE *,
const unsigned long FAR *));
102local void make_crc_table()
108 static volatile int first = 1;
109 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
119 for (n = 0;
n <
sizeof(p)/
sizeof(
unsigned char);
n++)
120 poly |= 1UL << (31 - p[n]);
123 for (n = 0;
n < 256;
n++) {
124 c = (
unsigned long)n;
125 for (k = 0; k < 8; k++)
126 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
133 for (n = 0;
n < 256;
n++) {
136 for (k = 1; k < 4; k++) {
148 while (crc_table_empty)
157 out = fopen(
"crc32.h",
"w");
158 if (out == NULL)
return;
159 fprintf(out,
"/* crc32.h -- tables for rapid CRC calculation\n");
160 fprintf(out,
" * Generated automatically by crc32.c\n */\n\n");
161 fprintf(out,
"local const unsigned long FAR ");
162 fprintf(out,
"crc_table[TBLS][256] =\n{\n {\n");
165 fprintf(out,
"#ifdef BYFOUR\n");
166 for (k = 1; k < 8; k++) {
167 fprintf(out,
" },\n {\n");
170 fprintf(out,
"#endif\n");
172 fprintf(out,
" }\n};\n");
179local void write_table(FILE *out,
const unsigned long FAR *table)
183 for (n = 0;
n < 256;
n++)
184 fprintf(out,
"%s0x%08lxUL%s", n % 5 ?
"" :
" ", table[n],
185 n == 255 ?
"\n" : (n % 5 == 4 ?
",\n" :
", "));
201#ifdef DYNAMIC_CRC_TABLE
209#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
210#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
213unsigned long ZEXPORT crc32(
unsigned long crc,
const unsigned char FAR *buf,
unsigned len)
215 if (buf ==
Z_NULL)
return 0UL;
217#ifdef DYNAMIC_CRC_TABLE
223 if (
sizeof(
void *) ==
sizeof(ptrdiff_t)) {
227 if (*((
unsigned char *)(&endian)))
228 return crc32_little(crc, buf, len);
230 return crc32_big(crc, buf, len);
233 crc = crc ^ 0xffffffffUL;
241 return crc ^ 0xffffffffUL;
247#define DOLIT4 c ^= *buf4++; \
248 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
249 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
250#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
253local unsigned long crc32_little(
unsigned long crc,
const unsigned char FAR *buf,
unsigned len)
256 register const u4
FAR *buf4;
260 while (len && ((ptrdiff_t)buf & 3)) {
261 c =
crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
265 buf4 = (
const u4
FAR *)buf;
274 buf = (
const unsigned char FAR *)buf4;
277 c =
crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
280 return (
unsigned long)c;
284#define DOBIG4 c ^= *++buf4; \
285 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
286 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
287#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
290local unsigned long crc32_big(
unsigned long crc,
const unsigned char FAR *buf,
unsigned len)
293 register const u4
FAR *buf4;
297 while (len && ((ptrdiff_t)buf & 3)) {
298 c =
crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
302 buf4 = (
const u4
FAR *)buf;
313 buf = (
const unsigned char FAR *)buf4;
316 c =
crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
319 return (
unsigned long)(REV(c));
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, unsigned len)
const unsigned long FAR *ZEXPORT get_crc_table()
const unsigned long FAR crc_table[TBLS][256]