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