annotate src/zlib-1.2.8/crc32.c @ 43:5ea0608b923f

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