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