annotate src/zlib-1.2.8/crc32.c @ 156:1bf23f5aebc4

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