Blame zlib-src/crc32.c

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