cannam@128: /* crc32.c -- compute the CRC-32 of a data stream cannam@128: * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler cannam@128: * For conditions of distribution and use, see copyright notice in zlib.h cannam@128: * cannam@128: * Thanks to Rodney Brown for his contribution of faster cannam@128: * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing cannam@128: * tables for updating the shift register in one step with three exclusive-ors cannam@128: * instead of four steps with four exclusive-ors. This results in about a cannam@128: * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. cannam@128: */ cannam@128: cannam@128: /* @(#) $Id$ */ cannam@128: cannam@128: /* cannam@128: Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore cannam@128: protection on the static variables used to control the first-use generation cannam@128: of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should cannam@128: first call get_crc_table() to initialize the tables before allowing more than cannam@128: one thread to use crc32(). cannam@128: cannam@128: DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. cannam@128: */ cannam@128: cannam@128: #ifdef MAKECRCH cannam@128: # include cannam@128: # ifndef DYNAMIC_CRC_TABLE cannam@128: # define DYNAMIC_CRC_TABLE cannam@128: # endif /* !DYNAMIC_CRC_TABLE */ cannam@128: #endif /* MAKECRCH */ cannam@128: cannam@128: #include "zutil.h" /* for STDC and FAR definitions */ cannam@128: cannam@128: #define local static cannam@128: cannam@128: /* Definitions for doing the crc four data bytes at a time. */ cannam@128: #if !defined(NOBYFOUR) && defined(Z_U4) cannam@128: # define BYFOUR cannam@128: #endif cannam@128: #ifdef BYFOUR cannam@128: local unsigned long crc32_little OF((unsigned long, cannam@128: const unsigned char FAR *, unsigned)); cannam@128: local unsigned long crc32_big OF((unsigned long, cannam@128: const unsigned char FAR *, unsigned)); cannam@128: # define TBLS 8 cannam@128: #else cannam@128: # define TBLS 1 cannam@128: #endif /* BYFOUR */ cannam@128: cannam@128: /* Local functions for crc concatenation */ cannam@128: local unsigned long gf2_matrix_times OF((unsigned long *mat, cannam@128: unsigned long vec)); cannam@128: local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); cannam@128: local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); cannam@128: cannam@128: cannam@128: #ifdef DYNAMIC_CRC_TABLE cannam@128: cannam@128: local volatile int crc_table_empty = 1; cannam@128: local z_crc_t FAR crc_table[TBLS][256]; cannam@128: local void make_crc_table OF((void)); cannam@128: #ifdef MAKECRCH cannam@128: local void write_table OF((FILE *, const z_crc_t FAR *)); cannam@128: #endif /* MAKECRCH */ cannam@128: /* cannam@128: Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: cannam@128: 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: cannam@128: Polynomials over GF(2) are represented in binary, one bit per coefficient, cannam@128: with the lowest powers in the most significant bit. Then adding polynomials cannam@128: is just exclusive-or, and multiplying a polynomial by x is a right shift by cannam@128: one. If we call the above polynomial p, and represent a byte as the cannam@128: polynomial q, also with the lowest power in the most significant bit (so the cannam@128: byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, cannam@128: where a mod b means the remainder after dividing a by b. cannam@128: cannam@128: This calculation is done using the shift-register method of multiplying and cannam@128: taking the remainder. The register is initialized to zero, and for each cannam@128: incoming bit, x^32 is added mod p to the register if the bit is a one (where cannam@128: x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by cannam@128: x (which is shifting right by one and adding x^32 mod p if the bit shifted cannam@128: out is a one). We start with the highest power (least significant bit) of cannam@128: q and repeat for all eight bits of q. cannam@128: cannam@128: The first table is simply the CRC of all possible eight bit values. This is cannam@128: all the information needed to generate CRCs on data a byte at a time for all cannam@128: combinations of CRC register values and incoming bytes. The remaining tables cannam@128: allow for word-at-a-time CRC calculation for both big-endian and little- cannam@128: endian machines, where a word is four bytes. cannam@128: */ cannam@128: local void make_crc_table() cannam@128: { cannam@128: z_crc_t c; cannam@128: int n, k; cannam@128: z_crc_t poly; /* polynomial exclusive-or pattern */ cannam@128: /* terms of polynomial defining this crc (except x^32): */ cannam@128: static volatile int first = 1; /* flag to limit concurrent making */ cannam@128: static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; cannam@128: cannam@128: /* See if another task is already doing this (not thread-safe, but better cannam@128: than nothing -- significantly reduces duration of vulnerability in cannam@128: case the advice about DYNAMIC_CRC_TABLE is ignored) */ cannam@128: if (first) { cannam@128: first = 0; cannam@128: cannam@128: /* make exclusive-or pattern from polynomial (0xedb88320UL) */ cannam@128: poly = 0; cannam@128: for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) cannam@128: poly |= (z_crc_t)1 << (31 - p[n]); cannam@128: cannam@128: /* generate a crc for every 8-bit value */ cannam@128: for (n = 0; n < 256; n++) { cannam@128: c = (z_crc_t)n; cannam@128: for (k = 0; k < 8; k++) cannam@128: c = c & 1 ? poly ^ (c >> 1) : c >> 1; cannam@128: crc_table[0][n] = c; cannam@128: } cannam@128: cannam@128: #ifdef BYFOUR cannam@128: /* generate crc for each value followed by one, two, and three zeros, cannam@128: and then the byte reversal of those as well as the first table */ cannam@128: for (n = 0; n < 256; n++) { cannam@128: c = crc_table[0][n]; cannam@128: crc_table[4][n] = ZSWAP32(c); cannam@128: for (k = 1; k < 4; k++) { cannam@128: c = crc_table[0][c & 0xff] ^ (c >> 8); cannam@128: crc_table[k][n] = c; cannam@128: crc_table[k + 4][n] = ZSWAP32(c); cannam@128: } cannam@128: } cannam@128: #endif /* BYFOUR */ cannam@128: cannam@128: crc_table_empty = 0; cannam@128: } cannam@128: else { /* not first */ cannam@128: /* wait for the other guy to finish (not efficient, but rare) */ cannam@128: while (crc_table_empty) cannam@128: ; cannam@128: } cannam@128: cannam@128: #ifdef MAKECRCH cannam@128: /* write out CRC tables to crc32.h */ cannam@128: { cannam@128: FILE *out; cannam@128: cannam@128: out = fopen("crc32.h", "w"); cannam@128: if (out == NULL) return; cannam@128: fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); cannam@128: fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); cannam@128: fprintf(out, "local const z_crc_t FAR "); cannam@128: fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); cannam@128: write_table(out, crc_table[0]); cannam@128: # ifdef BYFOUR cannam@128: fprintf(out, "#ifdef BYFOUR\n"); cannam@128: for (k = 1; k < 8; k++) { cannam@128: fprintf(out, " },\n {\n"); cannam@128: write_table(out, crc_table[k]); cannam@128: } cannam@128: fprintf(out, "#endif\n"); cannam@128: # endif /* BYFOUR */ cannam@128: fprintf(out, " }\n};\n"); cannam@128: fclose(out); cannam@128: } cannam@128: #endif /* MAKECRCH */ cannam@128: } cannam@128: cannam@128: #ifdef MAKECRCH cannam@128: local void write_table(out, table) cannam@128: FILE *out; cannam@128: const z_crc_t FAR *table; cannam@128: { cannam@128: int n; cannam@128: cannam@128: for (n = 0; n < 256; n++) cannam@128: fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", cannam@128: (unsigned long)(table[n]), cannam@128: n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); cannam@128: } cannam@128: #endif /* MAKECRCH */ cannam@128: cannam@128: #else /* !DYNAMIC_CRC_TABLE */ cannam@128: /* ======================================================================== cannam@128: * Tables of CRC-32s of all single-byte values, made by make_crc_table(). cannam@128: */ cannam@128: #include "crc32.h" cannam@128: #endif /* DYNAMIC_CRC_TABLE */ cannam@128: cannam@128: /* ========================================================================= cannam@128: * This function can be used by asm versions of crc32() cannam@128: */ cannam@128: const z_crc_t FAR * ZEXPORT get_crc_table() cannam@128: { cannam@128: #ifdef DYNAMIC_CRC_TABLE cannam@128: if (crc_table_empty) cannam@128: make_crc_table(); cannam@128: #endif /* DYNAMIC_CRC_TABLE */ cannam@128: return (const z_crc_t FAR *)crc_table; cannam@128: } cannam@128: cannam@128: /* ========================================================================= */ cannam@128: #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) cannam@128: #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 cannam@128: cannam@128: /* ========================================================================= */ cannam@128: unsigned long ZEXPORT crc32(crc, buf, len) cannam@128: unsigned long crc; cannam@128: const unsigned char FAR *buf; cannam@128: uInt len; cannam@128: { cannam@128: if (buf == Z_NULL) return 0UL; cannam@128: cannam@128: #ifdef DYNAMIC_CRC_TABLE cannam@128: if (crc_table_empty) cannam@128: make_crc_table(); cannam@128: #endif /* DYNAMIC_CRC_TABLE */ cannam@128: cannam@128: #ifdef BYFOUR cannam@128: if (sizeof(void *) == sizeof(ptrdiff_t)) { cannam@128: z_crc_t endian; cannam@128: cannam@128: endian = 1; cannam@128: if (*((unsigned char *)(&endian))) cannam@128: return crc32_little(crc, buf, len); cannam@128: else cannam@128: return crc32_big(crc, buf, len); cannam@128: } cannam@128: #endif /* BYFOUR */ cannam@128: crc = crc ^ 0xffffffffUL; cannam@128: while (len >= 8) { cannam@128: DO8; cannam@128: len -= 8; cannam@128: } cannam@128: if (len) do { cannam@128: DO1; cannam@128: } while (--len); cannam@128: return crc ^ 0xffffffffUL; cannam@128: } cannam@128: cannam@128: #ifdef BYFOUR cannam@128: cannam@128: /* ========================================================================= */ cannam@128: #define DOLIT4 c ^= *buf4++; \ cannam@128: c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ cannam@128: crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] cannam@128: #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 cannam@128: cannam@128: /* ========================================================================= */ cannam@128: local unsigned long crc32_little(crc, buf, len) cannam@128: unsigned long crc; cannam@128: const unsigned char FAR *buf; cannam@128: unsigned len; cannam@128: { cannam@128: register z_crc_t c; cannam@128: register const z_crc_t FAR *buf4; cannam@128: cannam@128: c = (z_crc_t)crc; cannam@128: c = ~c; cannam@128: while (len && ((ptrdiff_t)buf & 3)) { cannam@128: c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); cannam@128: len--; cannam@128: } cannam@128: cannam@128: buf4 = (const z_crc_t FAR *)(const void FAR *)buf; cannam@128: while (len >= 32) { cannam@128: DOLIT32; cannam@128: len -= 32; cannam@128: } cannam@128: while (len >= 4) { cannam@128: DOLIT4; cannam@128: len -= 4; cannam@128: } cannam@128: buf = (const unsigned char FAR *)buf4; cannam@128: cannam@128: if (len) do { cannam@128: c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); cannam@128: } while (--len); cannam@128: c = ~c; cannam@128: return (unsigned long)c; cannam@128: } cannam@128: cannam@128: /* ========================================================================= */ cannam@128: #define DOBIG4 c ^= *++buf4; \ cannam@128: c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ cannam@128: crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] cannam@128: #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 cannam@128: cannam@128: /* ========================================================================= */ cannam@128: local unsigned long crc32_big(crc, buf, len) cannam@128: unsigned long crc; cannam@128: const unsigned char FAR *buf; cannam@128: unsigned len; cannam@128: { cannam@128: register z_crc_t c; cannam@128: register const z_crc_t FAR *buf4; cannam@128: cannam@128: c = ZSWAP32((z_crc_t)crc); cannam@128: c = ~c; cannam@128: while (len && ((ptrdiff_t)buf & 3)) { cannam@128: c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); cannam@128: len--; cannam@128: } cannam@128: cannam@128: buf4 = (const z_crc_t FAR *)(const void FAR *)buf; cannam@128: buf4--; cannam@128: while (len >= 32) { cannam@128: DOBIG32; cannam@128: len -= 32; cannam@128: } cannam@128: while (len >= 4) { cannam@128: DOBIG4; cannam@128: len -= 4; cannam@128: } cannam@128: buf4++; cannam@128: buf = (const unsigned char FAR *)buf4; cannam@128: cannam@128: if (len) do { cannam@128: c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); cannam@128: } while (--len); cannam@128: c = ~c; cannam@128: return (unsigned long)(ZSWAP32(c)); cannam@128: } cannam@128: cannam@128: #endif /* BYFOUR */ cannam@128: cannam@128: #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ cannam@128: cannam@128: /* ========================================================================= */ cannam@128: local unsigned long gf2_matrix_times(mat, vec) cannam@128: unsigned long *mat; cannam@128: unsigned long vec; cannam@128: { cannam@128: unsigned long sum; cannam@128: cannam@128: sum = 0; cannam@128: while (vec) { cannam@128: if (vec & 1) cannam@128: sum ^= *mat; cannam@128: vec >>= 1; cannam@128: mat++; cannam@128: } cannam@128: return sum; cannam@128: } cannam@128: cannam@128: /* ========================================================================= */ cannam@128: local void gf2_matrix_square(square, mat) cannam@128: unsigned long *square; cannam@128: unsigned long *mat; cannam@128: { cannam@128: int n; cannam@128: cannam@128: for (n = 0; n < GF2_DIM; n++) cannam@128: square[n] = gf2_matrix_times(mat, mat[n]); cannam@128: } cannam@128: cannam@128: /* ========================================================================= */ cannam@128: local uLong crc32_combine_(crc1, crc2, len2) cannam@128: uLong crc1; cannam@128: uLong crc2; cannam@128: z_off64_t len2; cannam@128: { cannam@128: int n; cannam@128: unsigned long row; cannam@128: unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ cannam@128: unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ cannam@128: cannam@128: /* degenerate case (also disallow negative lengths) */ cannam@128: if (len2 <= 0) cannam@128: return crc1; cannam@128: cannam@128: /* put operator for one zero bit in odd */ cannam@128: odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ cannam@128: row = 1; cannam@128: for (n = 1; n < GF2_DIM; n++) { cannam@128: odd[n] = row; cannam@128: row <<= 1; cannam@128: } cannam@128: cannam@128: /* put operator for two zero bits in even */ cannam@128: gf2_matrix_square(even, odd); cannam@128: cannam@128: /* put operator for four zero bits in odd */ cannam@128: gf2_matrix_square(odd, even); cannam@128: cannam@128: /* apply len2 zeros to crc1 (first square will put the operator for one cannam@128: zero byte, eight zero bits, in even) */ cannam@128: do { cannam@128: /* apply zeros operator for this bit of len2 */ cannam@128: gf2_matrix_square(even, odd); cannam@128: if (len2 & 1) cannam@128: crc1 = gf2_matrix_times(even, crc1); cannam@128: len2 >>= 1; cannam@128: cannam@128: /* if no more bits set, then done */ cannam@128: if (len2 == 0) cannam@128: break; cannam@128: cannam@128: /* another iteration of the loop with odd and even swapped */ cannam@128: gf2_matrix_square(odd, even); cannam@128: if (len2 & 1) cannam@128: crc1 = gf2_matrix_times(odd, crc1); cannam@128: len2 >>= 1; cannam@128: cannam@128: /* if no more bits set, then done */ cannam@128: } while (len2 != 0); cannam@128: cannam@128: /* return combined crc */ cannam@128: crc1 ^= crc2; cannam@128: return crc1; cannam@128: } cannam@128: cannam@128: /* ========================================================================= */ cannam@128: uLong ZEXPORT crc32_combine(crc1, crc2, len2) cannam@128: uLong crc1; cannam@128: uLong crc2; cannam@128: z_off_t len2; cannam@128: { cannam@128: return crc32_combine_(crc1, crc2, len2); cannam@128: } cannam@128: cannam@128: uLong ZEXPORT crc32_combine64(crc1, crc2, len2) cannam@128: uLong crc1; cannam@128: uLong crc2; cannam@128: z_off64_t len2; cannam@128: { cannam@128: return crc32_combine_(crc1, crc2, len2); cannam@128: }