]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | /* crc32.c -- compute the CRC-32 of a data stream\r |
2 | * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler\r | |
3 | * For conditions of distribution and use, see copyright notice in zlib.h\r | |
4 | *\r | |
5 | * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster\r | |
6 | * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing\r | |
7 | * tables for updating the shift register in one step with three exclusive-ors\r | |
8 | * instead of four steps with four exclusive-ors. This results in about a\r | |
9 | * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.\r | |
10 | */\r | |
11 | \r | |
12 | /* @(#) $Id$ */\r | |
13 | \r | |
14 | /*\r | |
15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore\r | |
16 | protection on the static variables used to control the first-use generation\r | |
17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should\r | |
18 | first call get_crc_table() to initialize the tables before allowing more than\r | |
19 | one thread to use crc32().\r | |
20 | \r | |
21 | DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.\r | |
22 | */\r | |
23 | \r | |
24 | #ifdef MAKECRCH\r | |
25 | # include <stdio.h>\r | |
26 | # ifndef DYNAMIC_CRC_TABLE\r | |
27 | # define DYNAMIC_CRC_TABLE\r | |
28 | # endif /* !DYNAMIC_CRC_TABLE */\r | |
29 | #endif /* MAKECRCH */\r | |
30 | \r | |
31 | #include "zutil.h" /* for STDC and FAR definitions */\r | |
32 | \r | |
33 | #define local static\r | |
34 | \r | |
35 | /* Definitions for doing the crc four data bytes at a time. */\r | |
36 | #if !defined(NOBYFOUR) && defined(Z_U4)\r | |
37 | # define BYFOUR\r | |
38 | #endif\r | |
39 | #ifdef BYFOUR\r | |
40 | local unsigned long crc32_little OF((unsigned long,\r | |
41 | const unsigned char FAR *, unsigned));\r | |
42 | local unsigned long crc32_big OF((unsigned long,\r | |
43 | const unsigned char FAR *, unsigned));\r | |
44 | # define TBLS 8\r | |
45 | #else\r | |
46 | # define TBLS 1\r | |
47 | #endif /* BYFOUR */\r | |
48 | \r | |
49 | /* Local functions for crc concatenation */\r | |
50 | local unsigned long gf2_matrix_times OF((unsigned long *mat,\r | |
51 | unsigned long vec));\r | |
52 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));\r | |
53 | local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));\r | |
54 | \r | |
55 | \r | |
56 | #ifdef DYNAMIC_CRC_TABLE\r | |
57 | \r | |
58 | local volatile int crc_table_empty = 1;\r | |
59 | local z_crc_t FAR crc_table[TBLS][256];\r | |
60 | local void make_crc_table OF((void));\r | |
61 | #ifdef MAKECRCH\r | |
62 | local void write_table OF((FILE *, const z_crc_t FAR *));\r | |
63 | #endif /* MAKECRCH */\r | |
64 | /*\r | |
65 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:\r | |
66 | x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.\r | |
67 | \r | |
68 | Polynomials over GF(2) are represented in binary, one bit per coefficient,\r | |
69 | with the lowest powers in the most significant bit. Then adding polynomials\r | |
70 | is just exclusive-or, and multiplying a polynomial by x is a right shift by\r | |
71 | one. If we call the above polynomial p, and represent a byte as the\r | |
72 | polynomial q, also with the lowest power in the most significant bit (so the\r | |
73 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,\r | |
74 | where a mod b means the remainder after dividing a by b.\r | |
75 | \r | |
76 | This calculation is done using the shift-register method of multiplying and\r | |
77 | taking the remainder. The register is initialized to zero, and for each\r | |
78 | incoming bit, x^32 is added mod p to the register if the bit is a one (where\r | |
79 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by\r | |
80 | x (which is shifting right by one and adding x^32 mod p if the bit shifted\r | |
81 | out is a one). We start with the highest power (least significant bit) of\r | |
82 | q and repeat for all eight bits of q.\r | |
83 | \r | |
84 | The first table is simply the CRC of all possible eight bit values. This is\r | |
85 | all the information needed to generate CRCs on data a byte at a time for all\r | |
86 | combinations of CRC register values and incoming bytes. The remaining tables\r | |
87 | allow for word-at-a-time CRC calculation for both big-endian and little-\r | |
88 | endian machines, where a word is four bytes.\r | |
89 | */\r | |
90 | local void make_crc_table()\r | |
91 | {\r | |
92 | z_crc_t c;\r | |
93 | int n, k;\r | |
94 | z_crc_t poly; /* polynomial exclusive-or pattern */\r | |
95 | /* terms of polynomial defining this crc (except x^32): */\r | |
96 | static volatile int first = 1; /* flag to limit concurrent making */\r | |
97 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};\r | |
98 | \r | |
99 | /* See if another task is already doing this (not thread-safe, but better\r | |
100 | than nothing -- significantly reduces duration of vulnerability in\r | |
101 | case the advice about DYNAMIC_CRC_TABLE is ignored) */\r | |
102 | if (first) {\r | |
103 | first = 0;\r | |
104 | \r | |
105 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */\r | |
106 | poly = 0;\r | |
107 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)\r | |
108 | poly |= (z_crc_t)1 << (31 - p[n]);\r | |
109 | \r | |
110 | /* generate a crc for every 8-bit value */\r | |
111 | for (n = 0; n < 256; n++) {\r | |
112 | c = (z_crc_t)n;\r | |
113 | for (k = 0; k < 8; k++)\r | |
114 | c = c & 1 ? poly ^ (c >> 1) : c >> 1;\r | |
115 | crc_table[0][n] = c;\r | |
116 | }\r | |
117 | \r | |
118 | #ifdef BYFOUR\r | |
119 | /* generate crc for each value followed by one, two, and three zeros,\r | |
120 | and then the byte reversal of those as well as the first table */\r | |
121 | for (n = 0; n < 256; n++) {\r | |
122 | c = crc_table[0][n];\r | |
123 | crc_table[4][n] = ZSWAP32(c);\r | |
124 | for (k = 1; k < 4; k++) {\r | |
125 | c = crc_table[0][c & 0xff] ^ (c >> 8);\r | |
126 | crc_table[k][n] = c;\r | |
127 | crc_table[k + 4][n] = ZSWAP32(c);\r | |
128 | }\r | |
129 | }\r | |
130 | #endif /* BYFOUR */\r | |
131 | \r | |
132 | crc_table_empty = 0;\r | |
133 | }\r | |
134 | else { /* not first */\r | |
135 | /* wait for the other guy to finish (not efficient, but rare) */\r | |
136 | while (crc_table_empty)\r | |
137 | ;\r | |
138 | }\r | |
139 | \r | |
140 | #ifdef MAKECRCH\r | |
141 | /* write out CRC tables to crc32.h */\r | |
142 | {\r | |
143 | FILE *out;\r | |
144 | \r | |
145 | out = fopen("crc32.h", "w");\r | |
146 | if (out == NULL) return;\r | |
147 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");\r | |
148 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n");\r | |
149 | fprintf(out, "local const z_crc_t FAR ");\r | |
150 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");\r | |
151 | write_table(out, crc_table[0]);\r | |
152 | # ifdef BYFOUR\r | |
153 | fprintf(out, "#ifdef BYFOUR\n");\r | |
154 | for (k = 1; k < 8; k++) {\r | |
155 | fprintf(out, " },\n {\n");\r | |
156 | write_table(out, crc_table[k]);\r | |
157 | }\r | |
158 | fprintf(out, "#endif\n");\r | |
159 | # endif /* BYFOUR */\r | |
160 | fprintf(out, " }\n};\n");\r | |
161 | fclose(out);\r | |
162 | }\r | |
163 | #endif /* MAKECRCH */\r | |
164 | }\r | |
165 | \r | |
166 | #ifdef MAKECRCH\r | |
167 | local void write_table(out, table)\r | |
168 | FILE *out;\r | |
169 | const z_crc_t FAR *table;\r | |
170 | {\r | |
171 | int n;\r | |
172 | \r | |
173 | for (n = 0; n < 256; n++)\r | |
174 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",\r | |
175 | (unsigned long)(table[n]),\r | |
176 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));\r | |
177 | }\r | |
178 | #endif /* MAKECRCH */\r | |
179 | \r | |
180 | #else /* !DYNAMIC_CRC_TABLE */\r | |
181 | /* ========================================================================\r | |
182 | * Tables of CRC-32s of all single-byte values, made by make_crc_table().\r | |
183 | */\r | |
184 | #include "crc32.h"\r | |
185 | #endif /* DYNAMIC_CRC_TABLE */\r | |
186 | \r | |
187 | /* =========================================================================\r | |
188 | * This function can be used by asm versions of crc32()\r | |
189 | */\r | |
190 | const z_crc_t FAR * ZEXPORT get_crc_table()\r | |
191 | {\r | |
192 | #ifdef DYNAMIC_CRC_TABLE\r | |
193 | if (crc_table_empty)\r | |
194 | make_crc_table();\r | |
195 | #endif /* DYNAMIC_CRC_TABLE */\r | |
196 | return (const z_crc_t FAR *)crc_table;\r | |
197 | }\r | |
198 | \r | |
199 | /* ========================================================================= */\r | |
200 | #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)\r | |
201 | #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1\r | |
202 | \r | |
203 | /* ========================================================================= */\r | |
204 | unsigned long ZEXPORT crc32(crc, buf, len)\r | |
205 | unsigned long crc;\r | |
206 | const unsigned char FAR *buf;\r | |
207 | uInt len;\r | |
208 | {\r | |
209 | if (buf == Z_NULL) return 0UL;\r | |
210 | \r | |
211 | #ifdef DYNAMIC_CRC_TABLE\r | |
212 | if (crc_table_empty)\r | |
213 | make_crc_table();\r | |
214 | #endif /* DYNAMIC_CRC_TABLE */\r | |
215 | \r | |
216 | #ifdef BYFOUR\r | |
217 | if (sizeof(void *) == sizeof(ptrdiff_t)) {\r | |
218 | z_crc_t endian;\r | |
219 | \r | |
220 | endian = 1;\r | |
221 | if (*((unsigned char *)(&endian)))\r | |
222 | return crc32_little(crc, buf, len);\r | |
223 | else\r | |
224 | return crc32_big(crc, buf, len);\r | |
225 | }\r | |
226 | #endif /* BYFOUR */\r | |
227 | crc = crc ^ 0xffffffffUL;\r | |
228 | while (len >= 8) {\r | |
229 | DO8;\r | |
230 | len -= 8;\r | |
231 | }\r | |
232 | if (len) do {\r | |
233 | DO1;\r | |
234 | } while (--len);\r | |
235 | return crc ^ 0xffffffffUL;\r | |
236 | }\r | |
237 | \r | |
238 | #ifdef BYFOUR\r | |
239 | \r | |
240 | /* ========================================================================= */\r | |
241 | #define DOLIT4 c ^= *buf4++; \\r | |
242 | c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \\r | |
243 | crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]\r | |
244 | #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4\r | |
245 | \r | |
246 | /* ========================================================================= */\r | |
247 | local unsigned long crc32_little(crc, buf, len)\r | |
248 | unsigned long crc;\r | |
249 | const unsigned char FAR *buf;\r | |
250 | unsigned len;\r | |
251 | {\r | |
252 | register z_crc_t c;\r | |
253 | register const z_crc_t FAR *buf4;\r | |
254 | \r | |
255 | c = (z_crc_t)crc;\r | |
256 | c = ~c;\r | |
257 | while (len && ((ptrdiff_t)buf & 3)) {\r | |
258 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);\r | |
259 | len--;\r | |
260 | }\r | |
261 | \r | |
262 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf;\r | |
263 | while (len >= 32) {\r | |
264 | DOLIT32;\r | |
265 | len -= 32;\r | |
266 | }\r | |
267 | while (len >= 4) {\r | |
268 | DOLIT4;\r | |
269 | len -= 4;\r | |
270 | }\r | |
271 | buf = (const unsigned char FAR *)buf4;\r | |
272 | \r | |
273 | if (len) do {\r | |
274 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);\r | |
275 | } while (--len);\r | |
276 | c = ~c;\r | |
277 | return (unsigned long)c;\r | |
278 | }\r | |
279 | \r | |
280 | /* ========================================================================= */\r | |
281 | #define DOBIG4 c ^= *++buf4; \\r | |
282 | c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \\r | |
283 | crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]\r | |
284 | #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4\r | |
285 | \r | |
286 | /* ========================================================================= */\r | |
287 | local unsigned long crc32_big(crc, buf, len)\r | |
288 | unsigned long crc;\r | |
289 | const unsigned char FAR *buf;\r | |
290 | unsigned len;\r | |
291 | {\r | |
292 | register z_crc_t c;\r | |
293 | register const z_crc_t FAR *buf4;\r | |
294 | \r | |
295 | c = ZSWAP32((z_crc_t)crc);\r | |
296 | c = ~c;\r | |
297 | while (len && ((ptrdiff_t)buf & 3)) {\r | |
298 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);\r | |
299 | len--;\r | |
300 | }\r | |
301 | \r | |
302 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf;\r | |
303 | buf4--;\r | |
304 | while (len >= 32) {\r | |
305 | DOBIG32;\r | |
306 | len -= 32;\r | |
307 | }\r | |
308 | while (len >= 4) {\r | |
309 | DOBIG4;\r | |
310 | len -= 4;\r | |
311 | }\r | |
312 | buf4++;\r | |
313 | buf = (const unsigned char FAR *)buf4;\r | |
314 | \r | |
315 | if (len) do {\r | |
316 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);\r | |
317 | } while (--len);\r | |
318 | c = ~c;\r | |
319 | return (unsigned long)(ZSWAP32(c));\r | |
320 | }\r | |
321 | \r | |
322 | #endif /* BYFOUR */\r | |
323 | \r | |
324 | #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */\r | |
325 | \r | |
326 | /* ========================================================================= */\r | |
327 | local unsigned long gf2_matrix_times(mat, vec)\r | |
328 | unsigned long *mat;\r | |
329 | unsigned long vec;\r | |
330 | {\r | |
331 | unsigned long sum;\r | |
332 | \r | |
333 | sum = 0;\r | |
334 | while (vec) {\r | |
335 | if (vec & 1)\r | |
336 | sum ^= *mat;\r | |
337 | vec >>= 1;\r | |
338 | mat++;\r | |
339 | }\r | |
340 | return sum;\r | |
341 | }\r | |
342 | \r | |
343 | /* ========================================================================= */\r | |
344 | local void gf2_matrix_square(square, mat)\r | |
345 | unsigned long *square;\r | |
346 | unsigned long *mat;\r | |
347 | {\r | |
348 | int n;\r | |
349 | \r | |
350 | for (n = 0; n < GF2_DIM; n++)\r | |
351 | square[n] = gf2_matrix_times(mat, mat[n]);\r | |
352 | }\r | |
353 | \r | |
354 | /* ========================================================================= */\r | |
355 | local uLong crc32_combine_(crc1, crc2, len2)\r | |
356 | uLong crc1;\r | |
357 | uLong crc2;\r | |
358 | z_off64_t len2;\r | |
359 | {\r | |
360 | int n;\r | |
361 | unsigned long row;\r | |
362 | unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */\r | |
363 | unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */\r | |
364 | \r | |
365 | /* degenerate case (also disallow negative lengths) */\r | |
366 | if (len2 <= 0)\r | |
367 | return crc1;\r | |
368 | \r | |
369 | /* put operator for one zero bit in odd */\r | |
370 | odd[0] = 0xedb88320UL; /* CRC-32 polynomial */\r | |
371 | row = 1;\r | |
372 | for (n = 1; n < GF2_DIM; n++) {\r | |
373 | odd[n] = row;\r | |
374 | row <<= 1;\r | |
375 | }\r | |
376 | \r | |
377 | /* put operator for two zero bits in even */\r | |
378 | gf2_matrix_square(even, odd);\r | |
379 | \r | |
380 | /* put operator for four zero bits in odd */\r | |
381 | gf2_matrix_square(odd, even);\r | |
382 | \r | |
383 | /* apply len2 zeros to crc1 (first square will put the operator for one\r | |
384 | zero byte, eight zero bits, in even) */\r | |
385 | do {\r | |
386 | /* apply zeros operator for this bit of len2 */\r | |
387 | gf2_matrix_square(even, odd);\r | |
388 | if (len2 & 1)\r | |
389 | crc1 = gf2_matrix_times(even, crc1);\r | |
390 | len2 >>= 1;\r | |
391 | \r | |
392 | /* if no more bits set, then done */\r | |
393 | if (len2 == 0)\r | |
394 | break;\r | |
395 | \r | |
396 | /* another iteration of the loop with odd and even swapped */\r | |
397 | gf2_matrix_square(odd, even);\r | |
398 | if (len2 & 1)\r | |
399 | crc1 = gf2_matrix_times(odd, crc1);\r | |
400 | len2 >>= 1;\r | |
401 | \r | |
402 | /* if no more bits set, then done */\r | |
403 | } while (len2 != 0);\r | |
404 | \r | |
405 | /* return combined crc */\r | |
406 | crc1 ^= crc2;\r | |
407 | return crc1;\r | |
408 | }\r | |
409 | \r | |
410 | /* ========================================================================= */\r | |
411 | uLong ZEXPORT crc32_combine(crc1, crc2, len2)\r | |
412 | uLong crc1;\r | |
413 | uLong crc2;\r | |
414 | z_off_t len2;\r | |
415 | {\r | |
416 | return crc32_combine_(crc1, crc2, len2);\r | |
417 | }\r | |
418 | \r | |
419 | uLong ZEXPORT crc32_combine64(crc1, crc2, len2)\r | |
420 | uLong crc1;\r | |
421 | uLong crc2;\r | |
422 | z_off64_t len2;\r | |
423 | {\r | |
424 | return crc32_combine_(crc1, crc2, len2);\r | |
425 | }\r |