modules/zlib/src/crc32.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial