annotate src/zlib-1.2.7/crc32.c @ 97:efb4b8187266

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