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