• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* crc32.c -- compute the CRC-32 of a data stream
2  * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 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 "deflate.h"
32 #include "cpu_features.h"
33 #include "zutil.h"      /* for STDC and FAR definitions */
34 
35 #if defined(CRC32_SIMD_SSE42_PCLMUL) || defined(CRC32_ARMV8_CRC32)
36 #include "crc32_simd.h"
37 #endif
38 
39 /* Definitions for doing the crc four data bytes at a time. */
40 #if !defined(NOBYFOUR) && defined(Z_U4)
41 #  define BYFOUR
42 #endif
43 #ifdef BYFOUR
44    local unsigned long crc32_little OF((unsigned long,
45                         const unsigned char FAR *, z_size_t));
46    local unsigned long crc32_big OF((unsigned long,
47                         const unsigned char FAR *, z_size_t));
48 #  define TBLS 8
49 #else
50 #  define TBLS 1
51 #endif /* BYFOUR */
52 
53 /* Local functions for crc concatenation */
54 local unsigned long gf2_matrix_times OF((unsigned long *mat,
55                                          unsigned long vec));
56 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
57 local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
58 
59 
60 #ifdef DYNAMIC_CRC_TABLE
61 
62 local volatile int crc_table_empty = 1;
63 local z_crc_t FAR crc_table[TBLS][256];
64 local void make_crc_table OF((void));
65 #ifdef MAKECRCH
66    local void write_table OF((FILE *, const z_crc_t FAR *));
67 #endif /* MAKECRCH */
68 /*
69   Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
70   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.
71 
72   Polynomials over GF(2) are represented in binary, one bit per coefficient,
73   with the lowest powers in the most significant bit.  Then adding polynomials
74   is just exclusive-or, and multiplying a polynomial by x is a right shift by
75   one.  If we call the above polynomial p, and represent a byte as the
76   polynomial q, also with the lowest power in the most significant bit (so the
77   byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
78   where a mod b means the remainder after dividing a by b.
79 
80   This calculation is done using the shift-register method of multiplying and
81   taking the remainder.  The register is initialized to zero, and for each
82   incoming bit, x^32 is added mod p to the register if the bit is a one (where
83   x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
84   x (which is shifting right by one and adding x^32 mod p if the bit shifted
85   out is a one).  We start with the highest power (least significant bit) of
86   q and repeat for all eight bits of q.
87 
88   The first table is simply the CRC of all possible eight bit values.  This is
89   all the information needed to generate CRCs on data a byte at a time for all
90   combinations of CRC register values and incoming bytes.  The remaining tables
91   allow for word-at-a-time CRC calculation for both big-endian and little-
92   endian machines, where a word is four bytes.
93 */
make_crc_table()94 local void make_crc_table()
95 {
96     z_crc_t c;
97     int n, k;
98     z_crc_t poly;                       /* polynomial exclusive-or pattern */
99     /* terms of polynomial defining this crc (except x^32): */
100     static volatile int first = 1;      /* flag to limit concurrent making */
101     static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
102 
103     /* See if another task is already doing this (not thread-safe, but better
104        than nothing -- significantly reduces duration of vulnerability in
105        case the advice about DYNAMIC_CRC_TABLE is ignored) */
106     if (first) {
107         first = 0;
108 
109         /* make exclusive-or pattern from polynomial (0xedb88320UL) */
110         poly = 0;
111         for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
112             poly |= (z_crc_t)1 << (31 - p[n]);
113 
114         /* generate a crc for every 8-bit value */
115         for (n = 0; n < 256; n++) {
116             c = (z_crc_t)n;
117             for (k = 0; k < 8; k++)
118                 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
119             crc_table[0][n] = c;
120         }
121 
122 #ifdef BYFOUR
123         /* generate crc for each value followed by one, two, and three zeros,
124            and then the byte reversal of those as well as the first table */
125         for (n = 0; n < 256; n++) {
126             c = crc_table[0][n];
127             crc_table[4][n] = ZSWAP32(c);
128             for (k = 1; k < 4; k++) {
129                 c = crc_table[0][c & 0xff] ^ (c >> 8);
130                 crc_table[k][n] = c;
131                 crc_table[k + 4][n] = ZSWAP32(c);
132             }
133         }
134 #endif /* BYFOUR */
135 
136         crc_table_empty = 0;
137     }
138     else {      /* not first */
139         /* wait for the other guy to finish (not efficient, but rare) */
140         while (crc_table_empty)
141             ;
142     }
143 
144 #ifdef MAKECRCH
145     /* write out CRC tables to crc32.h */
146     {
147         FILE *out;
148 
149         out = fopen("crc32.h", "w");
150         if (out == NULL) return;
151         fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
152         fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
153         fprintf(out, "local const z_crc_t FAR ");
154         fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
155         write_table(out, crc_table[0]);
156 #  ifdef BYFOUR
157         fprintf(out, "#ifdef BYFOUR\n");
158         for (k = 1; k < 8; k++) {
159             fprintf(out, "  },\n  {\n");
160             write_table(out, crc_table[k]);
161         }
162         fprintf(out, "#endif\n");
163 #  endif /* BYFOUR */
164         fprintf(out, "  }\n};\n");
165         fclose(out);
166     }
167 #endif /* MAKECRCH */
168 }
169 
170 #ifdef MAKECRCH
write_table(out,table)171 local void write_table(out, table)
172     FILE *out;
173     const z_crc_t FAR *table;
174 {
175     int n;
176 
177     for (n = 0; n < 256; n++)
178         fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ",
179                 (unsigned long)(table[n]),
180                 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
181 }
182 #endif /* MAKECRCH */
183 
184 #else /* !DYNAMIC_CRC_TABLE */
185 /* ========================================================================
186  * Tables of CRC-32s of all single-byte values, made by make_crc_table().
187  */
188 #include "crc32.h"
189 #endif /* DYNAMIC_CRC_TABLE */
190 
191 /* =========================================================================
192  * This function can be used by asm versions of crc32()
193  */
get_crc_table()194 const z_crc_t FAR * ZEXPORT get_crc_table()
195 {
196 #ifdef DYNAMIC_CRC_TABLE
197     if (crc_table_empty)
198         make_crc_table();
199 #endif /* DYNAMIC_CRC_TABLE */
200     return (const z_crc_t FAR *)crc_table;
201 }
202 
203 /* ========================================================================= */
204 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
205 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
206 
207 /* ========================================================================= */
crc32_z(crc,buf,len)208 unsigned long ZEXPORT crc32_z(crc, buf, len)
209     unsigned long crc;
210     const unsigned char FAR *buf;
211     z_size_t len;
212 {
213     /*
214      * zlib convention is to call crc32(0, NULL, 0); before making
215      * calls to crc32(). So this is a good, early (and infrequent)
216      * place to cache CPU features if needed for those later, more
217      * interesting crc32() calls.
218      */
219 #if defined(CRC32_SIMD_SSE42_PCLMUL)
220     /*
221      * Use x86 sse4.2+pclmul SIMD to compute the crc32. Since this
222      * routine can be freely used, check CPU features here.
223      */
224     if (buf == Z_NULL) {
225         if (!len) /* Assume user is calling crc32(0, NULL, 0); */
226             cpu_check_features();
227         return 0UL;
228     }
229 
230     if (x86_cpu_enable_simd && len >= Z_CRC32_SSE42_MINIMUM_LENGTH) {
231         /* crc32 16-byte chunks */
232         z_size_t chunk_size = len & ~Z_CRC32_SSE42_CHUNKSIZE_MASK;
233         crc = ~crc32_sse42_simd_(buf, chunk_size, ~(uint32_t)crc);
234         /* check remaining data */
235         len -= chunk_size;
236         if (!len)
237             return crc;
238         /* Fall into the default crc32 for the remaining data. */
239         buf += chunk_size;
240     }
241 #else
242     if (buf == Z_NULL) {
243         return 0UL;
244     }
245 #endif /* CRC32_SIMD_SSE42_PCLMUL */
246 
247 #ifdef DYNAMIC_CRC_TABLE
248     if (crc_table_empty)
249         make_crc_table();
250 #endif /* DYNAMIC_CRC_TABLE */
251 
252 #ifdef BYFOUR
253     if (sizeof(void *) == sizeof(ptrdiff_t)) {
254         z_crc_t endian;
255 
256         endian = 1;
257         if (*((unsigned char *)(&endian)))
258             return crc32_little(crc, buf, len);
259         else
260             return crc32_big(crc, buf, len);
261     }
262 #endif /* BYFOUR */
263     crc = crc ^ 0xffffffffUL;
264     while (len >= 8) {
265         DO8;
266         len -= 8;
267     }
268     if (len) do {
269         DO1;
270     } while (--len);
271     return crc ^ 0xffffffffUL;
272 }
273 
274 /* ========================================================================= */
crc32(crc,buf,len)275 unsigned long ZEXPORT crc32(crc, buf, len)
276     unsigned long crc;
277     const unsigned char FAR *buf;
278     uInt len;
279 {
280 #if defined(CRC32_ARMV8_CRC32)
281     /* We got to verify ARM CPU features, so exploit the common usage pattern
282      * of calling this function with Z_NULL for an initial valid crc value.
283      * This allows to cache the result of the feature check and avoid extraneous
284      * function calls.
285      * TODO: try to move this to crc32_z if we don't loose performance on ARM.
286      */
287     if (buf == Z_NULL) {
288         if (!len) /* Assume user is calling crc32(0, NULL, 0); */
289             cpu_check_features();
290         return 0UL;
291     }
292 
293     if (arm_cpu_enable_crc32)
294         return armv8_crc32_little(crc, buf, len);
295 #endif
296     return crc32_z(crc, buf, len);
297 }
298 
299 #ifdef BYFOUR
300 
301 /*
302    This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
303    integer pointer type. This violates the strict aliasing rule, where a
304    compiler can assume, for optimization purposes, that two pointers to
305    fundamentally different types won't ever point to the same memory. This can
306    manifest as a problem only if one of the pointers is written to. This code
307    only reads from those pointers. So long as this code remains isolated in
308    this compilation unit, there won't be a problem. For this reason, this code
309    should not be copied and pasted into a compilation unit in which other code
310    writes to the buffer that is passed to these routines.
311  */
312 
313 /* ========================================================================= */
314 #define DOLIT4 c ^= *buf4++; \
315         c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
316             crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
317 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
318 
319 /* ========================================================================= */
crc32_little(crc,buf,len)320 local unsigned long crc32_little(crc, buf, len)
321     unsigned long crc;
322     const unsigned char FAR *buf;
323     z_size_t len;
324 {
325     register z_crc_t c;
326     register const z_crc_t FAR *buf4;
327 
328     c = (z_crc_t)crc;
329     c = ~c;
330     while (len && ((ptrdiff_t)buf & 3)) {
331         c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
332         len--;
333     }
334 
335     buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
336     while (len >= 32) {
337         DOLIT32;
338         len -= 32;
339     }
340     while (len >= 4) {
341         DOLIT4;
342         len -= 4;
343     }
344     buf = (const unsigned char FAR *)buf4;
345 
346     if (len) do {
347         c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
348     } while (--len);
349     c = ~c;
350     return (unsigned long)c;
351 }
352 
353 /* ========================================================================= */
354 #define DOBIG4 c ^= *buf4++; \
355         c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
356             crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
357 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
358 
359 /* ========================================================================= */
crc32_big(crc,buf,len)360 local unsigned long crc32_big(crc, buf, len)
361     unsigned long crc;
362     const unsigned char FAR *buf;
363     z_size_t len;
364 {
365     register z_crc_t c;
366     register const z_crc_t FAR *buf4;
367 
368     c = ZSWAP32((z_crc_t)crc);
369     c = ~c;
370     while (len && ((ptrdiff_t)buf & 3)) {
371         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
372         len--;
373     }
374 
375     buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
376     while (len >= 32) {
377         DOBIG32;
378         len -= 32;
379     }
380     while (len >= 4) {
381         DOBIG4;
382         len -= 4;
383     }
384     buf = (const unsigned char FAR *)buf4;
385 
386     if (len) do {
387         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
388     } while (--len);
389     c = ~c;
390     return (unsigned long)(ZSWAP32(c));
391 }
392 
393 #endif /* BYFOUR */
394 
395 #define GF2_DIM 32      /* dimension of GF(2) vectors (length of CRC) */
396 
397 /* ========================================================================= */
gf2_matrix_times(mat,vec)398 local unsigned long gf2_matrix_times(mat, vec)
399     unsigned long *mat;
400     unsigned long vec;
401 {
402     unsigned long sum;
403 
404     sum = 0;
405     while (vec) {
406         if (vec & 1)
407             sum ^= *mat;
408         vec >>= 1;
409         mat++;
410     }
411     return sum;
412 }
413 
414 /* ========================================================================= */
gf2_matrix_square(square,mat)415 local void gf2_matrix_square(square, mat)
416     unsigned long *square;
417     unsigned long *mat;
418 {
419     int n;
420 
421     for (n = 0; n < GF2_DIM; n++)
422         square[n] = gf2_matrix_times(mat, mat[n]);
423 }
424 
425 /* ========================================================================= */
crc32_combine_(crc1,crc2,len2)426 local uLong crc32_combine_(crc1, crc2, len2)
427     uLong crc1;
428     uLong crc2;
429     z_off64_t len2;
430 {
431     int n;
432     unsigned long row;
433     unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
434     unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
435 
436     /* degenerate case (also disallow negative lengths) */
437     if (len2 <= 0)
438         return crc1;
439 
440     /* put operator for one zero bit in odd */
441     odd[0] = 0xedb88320UL;          /* CRC-32 polynomial */
442     row = 1;
443     for (n = 1; n < GF2_DIM; n++) {
444         odd[n] = row;
445         row <<= 1;
446     }
447 
448     /* put operator for two zero bits in even */
449     gf2_matrix_square(even, odd);
450 
451     /* put operator for four zero bits in odd */
452     gf2_matrix_square(odd, even);
453 
454     /* apply len2 zeros to crc1 (first square will put the operator for one
455        zero byte, eight zero bits, in even) */
456     do {
457         /* apply zeros operator for this bit of len2 */
458         gf2_matrix_square(even, odd);
459         if (len2 & 1)
460             crc1 = gf2_matrix_times(even, crc1);
461         len2 >>= 1;
462 
463         /* if no more bits set, then done */
464         if (len2 == 0)
465             break;
466 
467         /* another iteration of the loop with odd and even swapped */
468         gf2_matrix_square(odd, even);
469         if (len2 & 1)
470             crc1 = gf2_matrix_times(odd, crc1);
471         len2 >>= 1;
472 
473         /* if no more bits set, then done */
474     } while (len2 != 0);
475 
476     /* return combined crc */
477     crc1 ^= crc2;
478     return crc1;
479 }
480 
481 /* ========================================================================= */
crc32_combine(crc1,crc2,len2)482 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
483     uLong crc1;
484     uLong crc2;
485     z_off_t len2;
486 {
487     return crc32_combine_(crc1, crc2, len2);
488 }
489 
crc32_combine64(crc1,crc2,len2)490 uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
491     uLong crc1;
492     uLong crc2;
493     z_off64_t len2;
494 {
495     return crc32_combine_(crc1, crc2, len2);
496 }
497 
crc_reset(deflate_state * const s)498 ZLIB_INTERNAL void crc_reset(deflate_state *const s)
499 {
500 #ifdef ADLER32_SIMD_SSSE3
501     if (x86_cpu_enable_simd) {
502         crc_fold_init(s);
503         return;
504     }
505 #endif
506     s->strm->adler = crc32(0L, Z_NULL, 0);
507 }
508 
crc_finalize(deflate_state * const s)509 ZLIB_INTERNAL void crc_finalize(deflate_state *const s)
510 {
511 #ifdef ADLER32_SIMD_SSSE3
512     if (x86_cpu_enable_simd)
513         s->strm->adler = crc_fold_512to32(s);
514 #endif
515 }
516 
copy_with_crc(z_streamp strm,Bytef * dst,long size)517 ZLIB_INTERNAL void copy_with_crc(z_streamp strm, Bytef *dst, long size)
518 {
519 #ifdef ADLER32_SIMD_SSSE3
520     if (x86_cpu_enable_simd) {
521         crc_fold_copy(strm->state, dst, strm->next_in, size);
522         return;
523     }
524 #endif
525     zmemcpy(dst, strm->next_in, size);
526     strm->adler = crc32(strm->adler, dst, size);
527 }
528