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