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