comparison zlib/crc32.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents ae3a4bfb450b
children
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* crc32.c -- compute the CRC-32 of a data stream 1 /* crc32.c -- compute the CRC-32 of a data stream
2 * Copyright (C) 1995-2005 Mark Adler 2 * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 * 4 *
5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster 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 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 7 * tables for updating the shift register in one step with three exclusive-ors
15 Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore 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 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 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 18 first call get_crc_table() to initialize the tables before allowing more than
19 one thread to use crc32(). 19 one thread to use crc32().
20
21 DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
20 */ 22 */
21 23
22 #ifdef MAKECRCH 24 #ifdef MAKECRCH
23 # include <stdio.h> 25 # include <stdio.h>
24 # ifndef DYNAMIC_CRC_TABLE 26 # ifndef DYNAMIC_CRC_TABLE
26 # endif /* !DYNAMIC_CRC_TABLE */ 28 # endif /* !DYNAMIC_CRC_TABLE */
27 #endif /* MAKECRCH */ 29 #endif /* MAKECRCH */
28 30
29 #include "zutil.h" /* for STDC and FAR definitions */ 31 #include "zutil.h" /* for STDC and FAR definitions */
30 32
31 #define local static
32
33 /* Find a four-byte integer type for crc32_little() and crc32_big(). */
34 #ifndef NOBYFOUR
35 # ifdef STDC /* need ANSI C limits.h to determine sizes */
36 # include <limits.h>
37 # define BYFOUR
38 # if (UINT_MAX == 0xffffffffUL)
39 typedef unsigned int u4;
40 # else
41 # if (ULONG_MAX == 0xffffffffUL)
42 typedef unsigned long u4;
43 # else
44 # if (USHRT_MAX == 0xffffffffUL)
45 typedef unsigned short u4;
46 # else
47 # undef BYFOUR /* can't find a four-byte integer type! */
48 # endif
49 # endif
50 # endif
51 # endif /* STDC */
52 #endif /* !NOBYFOUR */
53
54 /* Definitions for doing the crc four data bytes at a time. */ 33 /* Definitions for doing the crc four data bytes at a time. */
34 #if !defined(NOBYFOUR) && defined(Z_U4)
35 # define BYFOUR
36 #endif
55 #ifdef BYFOUR 37 #ifdef BYFOUR
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, 38 local unsigned long crc32_little OF((unsigned long,
59 const unsigned char FAR *, unsigned)); 39 const unsigned char FAR *, z_size_t));
60 local unsigned long crc32_big OF((unsigned long, 40 local unsigned long crc32_big OF((unsigned long,
61 const unsigned char FAR *, unsigned)); 41 const unsigned char FAR *, z_size_t));
62 # define TBLS 8 42 # define TBLS 8
63 #else 43 #else
64 # define TBLS 1 44 # define TBLS 1
65 #endif /* BYFOUR */ 45 #endif /* BYFOUR */
66 46
67 /* Local functions for crc concatenation */ 47 /* Local functions for crc concatenation */
68 local unsigned long gf2_matrix_times OF((unsigned long *mat, 48 local unsigned long gf2_matrix_times OF((unsigned long *mat,
69 unsigned long vec)); 49 unsigned long vec));
70 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); 50 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
51 local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
52
71 53
72 #ifdef DYNAMIC_CRC_TABLE 54 #ifdef DYNAMIC_CRC_TABLE
73 55
74 local volatile int crc_table_empty = 1; 56 local volatile int crc_table_empty = 1;
75 local unsigned long FAR crc_table[TBLS][256]; 57 local z_crc_t FAR crc_table[TBLS][256];
76 local void make_crc_table OF((void)); 58 local void make_crc_table OF((void));
77 #ifdef MAKECRCH 59 #ifdef MAKECRCH
78 local void write_table OF((FILE *, const unsigned long FAR *)); 60 local void write_table OF((FILE *, const z_crc_t FAR *));
79 #endif /* MAKECRCH */ 61 #endif /* MAKECRCH */
80 /* 62 /*
81 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 63 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
82 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. 64 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.
83 65
103 allow for word-at-a-time CRC calculation for both big-endian and little- 85 allow for word-at-a-time CRC calculation for both big-endian and little-
104 endian machines, where a word is four bytes. 86 endian machines, where a word is four bytes.
105 */ 87 */
106 local void make_crc_table() 88 local void make_crc_table()
107 { 89 {
108 unsigned long c; 90 z_crc_t c;
109 int n, k; 91 int n, k;
110 unsigned long poly; /* polynomial exclusive-or pattern */ 92 z_crc_t poly; /* polynomial exclusive-or pattern */
111 /* terms of polynomial defining this crc (except x^32): */ 93 /* terms of polynomial defining this crc (except x^32): */
112 static volatile int first = 1; /* flag to limit concurrent making */ 94 static volatile int first = 1; /* flag to limit concurrent making */
113 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 95 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
114 96
115 /* See if another task is already doing this (not thread-safe, but better 97 /* See if another task is already doing this (not thread-safe, but better
117 case the advice about DYNAMIC_CRC_TABLE is ignored) */ 99 case the advice about DYNAMIC_CRC_TABLE is ignored) */
118 if (first) { 100 if (first) {
119 first = 0; 101 first = 0;
120 102
121 /* make exclusive-or pattern from polynomial (0xedb88320UL) */ 103 /* make exclusive-or pattern from polynomial (0xedb88320UL) */
122 poly = 0UL; 104 poly = 0;
123 for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) 105 for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
124 poly |= 1UL << (31 - p[n]); 106 poly |= (z_crc_t)1 << (31 - p[n]);
125 107
126 /* generate a crc for every 8-bit value */ 108 /* generate a crc for every 8-bit value */
127 for (n = 0; n < 256; n++) { 109 for (n = 0; n < 256; n++) {
128 c = (unsigned long)n; 110 c = (z_crc_t)n;
129 for (k = 0; k < 8; k++) 111 for (k = 0; k < 8; k++)
130 c = c & 1 ? poly ^ (c >> 1) : c >> 1; 112 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
131 crc_table[0][n] = c; 113 crc_table[0][n] = c;
132 } 114 }
133 115
134 #ifdef BYFOUR 116 #ifdef BYFOUR
135 /* generate crc for each value followed by one, two, and three zeros, 117 /* generate crc for each value followed by one, two, and three zeros,
136 and then the byte reversal of those as well as the first table */ 118 and then the byte reversal of those as well as the first table */
137 for (n = 0; n < 256; n++) { 119 for (n = 0; n < 256; n++) {
138 c = crc_table[0][n]; 120 c = crc_table[0][n];
139 crc_table[4][n] = REV(c); 121 crc_table[4][n] = ZSWAP32(c);
140 for (k = 1; k < 4; k++) { 122 for (k = 1; k < 4; k++) {
141 c = crc_table[0][c & 0xff] ^ (c >> 8); 123 c = crc_table[0][c & 0xff] ^ (c >> 8);
142 crc_table[k][n] = c; 124 crc_table[k][n] = c;
143 crc_table[k + 4][n] = REV(c); 125 crc_table[k + 4][n] = ZSWAP32(c);
144 } 126 }
145 } 127 }
146 #endif /* BYFOUR */ 128 #endif /* BYFOUR */
147 129
148 crc_table_empty = 0; 130 crc_table_empty = 0;
160 142
161 out = fopen("crc32.h", "w"); 143 out = fopen("crc32.h", "w");
162 if (out == NULL) return; 144 if (out == NULL) return;
163 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); 145 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
164 fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); 146 fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
165 fprintf(out, "local const unsigned long FAR "); 147 fprintf(out, "local const z_crc_t FAR ");
166 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); 148 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
167 write_table(out, crc_table[0]); 149 write_table(out, crc_table[0]);
168 # ifdef BYFOUR 150 # ifdef BYFOUR
169 fprintf(out, "#ifdef BYFOUR\n"); 151 fprintf(out, "#ifdef BYFOUR\n");
170 for (k = 1; k < 8; k++) { 152 for (k = 1; k < 8; k++) {
180 } 162 }
181 163
182 #ifdef MAKECRCH 164 #ifdef MAKECRCH
183 local void write_table(out, table) 165 local void write_table(out, table)
184 FILE *out; 166 FILE *out;
185 const unsigned long FAR *table; 167 const z_crc_t FAR *table;
186 { 168 {
187 int n; 169 int n;
188 170
189 for (n = 0; n < 256; n++) 171 for (n = 0; n < 256; n++)
190 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], 172 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
173 (unsigned long)(table[n]),
191 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); 174 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
192 } 175 }
193 #endif /* MAKECRCH */ 176 #endif /* MAKECRCH */
194 177
195 #else /* !DYNAMIC_CRC_TABLE */ 178 #else /* !DYNAMIC_CRC_TABLE */
200 #endif /* DYNAMIC_CRC_TABLE */ 183 #endif /* DYNAMIC_CRC_TABLE */
201 184
202 /* ========================================================================= 185 /* =========================================================================
203 * This function can be used by asm versions of crc32() 186 * This function can be used by asm versions of crc32()
204 */ 187 */
205 const unsigned long FAR * ZEXPORT get_crc_table() 188 const z_crc_t FAR * ZEXPORT get_crc_table()
206 { 189 {
207 #ifdef DYNAMIC_CRC_TABLE 190 #ifdef DYNAMIC_CRC_TABLE
208 if (crc_table_empty) 191 if (crc_table_empty)
209 make_crc_table(); 192 make_crc_table();
210 #endif /* DYNAMIC_CRC_TABLE */ 193 #endif /* DYNAMIC_CRC_TABLE */
211 return (const unsigned long FAR *)crc_table; 194 return (const z_crc_t FAR *)crc_table;
212 } 195 }
213 196
214 /* ========================================================================= */ 197 /* ========================================================================= */
215 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) 198 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
216 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 199 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
217 200
218 /* ========================================================================= */ 201 /* ========================================================================= */
219 unsigned long ZEXPORT crc32(crc, buf, len) 202 unsigned long ZEXPORT crc32_z(crc, buf, len)
220 unsigned long crc; 203 unsigned long crc;
221 const unsigned char FAR *buf; 204 const unsigned char FAR *buf;
222 unsigned len; 205 z_size_t len;
223 { 206 {
224 if (buf == Z_NULL) return 0UL; 207 if (buf == Z_NULL) return 0UL;
225 208
226 #ifdef DYNAMIC_CRC_TABLE 209 #ifdef DYNAMIC_CRC_TABLE
227 if (crc_table_empty) 210 if (crc_table_empty)
228 make_crc_table(); 211 make_crc_table();
229 #endif /* DYNAMIC_CRC_TABLE */ 212 #endif /* DYNAMIC_CRC_TABLE */
230 213
231 #ifdef BYFOUR 214 #ifdef BYFOUR
232 if (sizeof(void *) == sizeof(ptrdiff_t)) { 215 if (sizeof(void *) == sizeof(ptrdiff_t)) {
233 u4 endian; 216 z_crc_t endian;
234 217
235 endian = 1; 218 endian = 1;
236 if (*((unsigned char *)(&endian))) 219 if (*((unsigned char *)(&endian)))
237 return crc32_little(crc, buf, len); 220 return crc32_little(crc, buf, len);
238 else 221 else
248 DO1; 231 DO1;
249 } while (--len); 232 } while (--len);
250 return crc ^ 0xffffffffUL; 233 return crc ^ 0xffffffffUL;
251 } 234 }
252 235
236 /* ========================================================================= */
237 unsigned long ZEXPORT crc32(crc, buf, len)
238 unsigned long crc;
239 const unsigned char FAR *buf;
240 uInt len;
241 {
242 return crc32_z(crc, buf, len);
243 }
244
253 #ifdef BYFOUR 245 #ifdef BYFOUR
246
247 /*
248 This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
249 integer pointer type. This violates the strict aliasing rule, where a
250 compiler can assume, for optimization purposes, that two pointers to
251 fundamentally different types won't ever point to the same memory. This can
252 manifest as a problem only if one of the pointers is written to. This code
253 only reads from those pointers. So long as this code remains isolated in
254 this compilation unit, there won't be a problem. For this reason, this code
255 should not be copied and pasted into a compilation unit in which other code
256 writes to the buffer that is passed to these routines.
257 */
254 258
255 /* ========================================================================= */ 259 /* ========================================================================= */
256 #define DOLIT4 c ^= *buf4++; \ 260 #define DOLIT4 c ^= *buf4++; \
257 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ 261 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
258 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] 262 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
260 264
261 /* ========================================================================= */ 265 /* ========================================================================= */
262 local unsigned long crc32_little(crc, buf, len) 266 local unsigned long crc32_little(crc, buf, len)
263 unsigned long crc; 267 unsigned long crc;
264 const unsigned char FAR *buf; 268 const unsigned char FAR *buf;
265 unsigned len; 269 z_size_t len;
266 { 270 {
267 register u4 c; 271 register z_crc_t c;
268 register const u4 FAR *buf4; 272 register const z_crc_t FAR *buf4;
269 273
270 c = (u4)crc; 274 c = (z_crc_t)crc;
271 c = ~c; 275 c = ~c;
272 while (len && ((ptrdiff_t)buf & 3)) { 276 while (len && ((ptrdiff_t)buf & 3)) {
273 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 277 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
274 len--; 278 len--;
275 } 279 }
276 280
277 buf4 = (const u4 FAR *)(const void FAR *)buf; 281 buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
278 while (len >= 32) { 282 while (len >= 32) {
279 DOLIT32; 283 DOLIT32;
280 len -= 32; 284 len -= 32;
281 } 285 }
282 while (len >= 4) { 286 while (len >= 4) {
291 c = ~c; 295 c = ~c;
292 return (unsigned long)c; 296 return (unsigned long)c;
293 } 297 }
294 298
295 /* ========================================================================= */ 299 /* ========================================================================= */
296 #define DOBIG4 c ^= *++buf4; \ 300 #define DOBIG4 c ^= *buf4++; \
297 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ 301 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
298 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] 302 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
299 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 303 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
300 304
301 /* ========================================================================= */ 305 /* ========================================================================= */
302 local unsigned long crc32_big(crc, buf, len) 306 local unsigned long crc32_big(crc, buf, len)
303 unsigned long crc; 307 unsigned long crc;
304 const unsigned char FAR *buf; 308 const unsigned char FAR *buf;
305 unsigned len; 309 z_size_t len;
306 { 310 {
307 register u4 c; 311 register z_crc_t c;
308 register const u4 FAR *buf4; 312 register const z_crc_t FAR *buf4;
309 313
310 c = REV((u4)crc); 314 c = ZSWAP32((z_crc_t)crc);
311 c = ~c; 315 c = ~c;
312 while (len && ((ptrdiff_t)buf & 3)) { 316 while (len && ((ptrdiff_t)buf & 3)) {
313 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 317 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
314 len--; 318 len--;
315 } 319 }
316 320
317 buf4 = (const u4 FAR *)(const void FAR *)buf; 321 buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
318 buf4--;
319 while (len >= 32) { 322 while (len >= 32) {
320 DOBIG32; 323 DOBIG32;
321 len -= 32; 324 len -= 32;
322 } 325 }
323 while (len >= 4) { 326 while (len >= 4) {
324 DOBIG4; 327 DOBIG4;
325 len -= 4; 328 len -= 4;
326 } 329 }
327 buf4++;
328 buf = (const unsigned char FAR *)buf4; 330 buf = (const unsigned char FAR *)buf4;
329 331
330 if (len) do { 332 if (len) do {
331 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 333 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
332 } while (--len); 334 } while (--len);
333 c = ~c; 335 c = ~c;
334 return (unsigned long)(REV(c)); 336 return (unsigned long)(ZSWAP32(c));
335 } 337 }
336 338
337 #endif /* BYFOUR */ 339 #endif /* BYFOUR */
338 340
339 #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ 341 #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
365 for (n = 0; n < GF2_DIM; n++) 367 for (n = 0; n < GF2_DIM; n++)
366 square[n] = gf2_matrix_times(mat, mat[n]); 368 square[n] = gf2_matrix_times(mat, mat[n]);
367 } 369 }
368 370
369 /* ========================================================================= */ 371 /* ========================================================================= */
370 uLong ZEXPORT crc32_combine(crc1, crc2, len2) 372 local uLong crc32_combine_(crc1, crc2, len2)
371 uLong crc1; 373 uLong crc1;
372 uLong crc2; 374 uLong crc2;
373 z_off_t len2; 375 z_off64_t len2;
374 { 376 {
375 int n; 377 int n;
376 unsigned long row; 378 unsigned long row;
377 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ 379 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
378 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ 380 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
379 381
380 /* degenerate case */ 382 /* degenerate case (also disallow negative lengths) */
381 if (len2 == 0) 383 if (len2 <= 0)
382 return crc1; 384 return crc1;
383 385
384 /* put operator for one zero bit in odd */ 386 /* put operator for one zero bit in odd */
385 odd[0] = 0xedb88320L; /* CRC-32 polynomial */ 387 odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
386 row = 1; 388 row = 1;
387 for (n = 1; n < GF2_DIM; n++) { 389 for (n = 1; n < GF2_DIM; n++) {
388 odd[n] = row; 390 odd[n] = row;
389 row <<= 1; 391 row <<= 1;
390 } 392 }
419 421
420 /* return combined crc */ 422 /* return combined crc */
421 crc1 ^= crc2; 423 crc1 ^= crc2;
422 return crc1; 424 return crc1;
423 } 425 }
426
427 /* ========================================================================= */
428 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
429 uLong crc1;
430 uLong crc2;
431 z_off_t len2;
432 {
433 return crc32_combine_(crc1, crc2, len2);
434 }
435
436 uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
437 uLong crc1;
438 uLong crc2;
439 z_off64_t len2;
440 {
441 return crc32_combine_(crc1, crc2, len2);
442 }