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