crc32.c revision 58a77c7622bb738b4b84816838dd870b40409a54
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden/* crc32.c -- compute the CRC-32 of a data stream
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * For conditions of distribution and use, see copyright notice in zlib.h
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * tables for updating the shift register in one step with three exclusive-ors
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * instead of four steps with four exclusive-ors. This results in about a
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden/* @(#) $Id$ */
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden protection on the static variables used to control the first-use generation
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington first call get_crc_table() to initialize the tables before allowing more than
2dd75eff92ef66e22cca286b6f4fe5a9c929af9dPhill Cunnington one thread to use crc32().
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington# endif /* !DYNAMIC_CRC_TABLE */
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington#endif /* MAKECRCH */
c49a898cb851f8fef7d7a6a9501c66b8f03ae05cPhill Cunnington#include "zutil.h" /* for STDC and FAR definitions */
c49a898cb851f8fef7d7a6a9501c66b8f03ae05cPhill Cunnington/* Definitions for doing the crc four data bytes at a time. */
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington local unsigned long crc32_little OF((unsigned long,
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington const unsigned char FAR *, unsigned));
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington local unsigned long crc32_big OF((unsigned long,
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington const unsigned char FAR *, unsigned));
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington#endif /* BYFOUR */
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington/* Local functions for crc concatenation */
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunningtonlocal unsigned long gf2_matrix_times OF((unsigned long *mat,
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunnington unsigned long vec));
c49a898cb851f8fef7d7a6a9501c66b8f03ae05cPhill Cunningtonlocal void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
91a599b60b62e70c63f6f698aaac20b41ba12941Phill Cunningtonlocal uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest local void write_table OF((FILE *, const z_crc_t FAR *));
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest#endif /* MAKECRCH */
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest 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.
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest Polynomials over GF(2) are represented in binary, one bit per coefficient,
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest with the lowest powers in the most significant bit. Then adding polynomials
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest is just exclusive-or, and multiplying a polynomial by x is a right shift by
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest one. If we call the above polynomial p, and represent a byte as the
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest polynomial q, also with the lowest power in the most significant bit (so the
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest where a mod b means the remainder after dividing a by b.
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest This calculation is done using the shift-register method of multiplying and
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest taking the remainder. The register is initialized to zero, and for each
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest incoming bit, x^32 is added mod p to the register if the bit is a one (where
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
124697010df6ca68fe3ecd0c6f5a77bf59777354Andrew Forrest x (which is shifting right by one and adding x^32 mod p if the bit shifted
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden out is a one). We start with the highest power (least significant bit) of
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden q and repeat for all eight bits of q.
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden The first table is simply the CRC of all possible eight bit values. This is
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden all the information needed to generate CRCs on data a byte at a time for all
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden combinations of CRC register values and incoming bytes. The remaining tables
2a60e63ce06684b7e9a6a74a07ccffe339a6bbb4Neil Madden allow for word-at-a-time CRC calculation for both big-endian and little-
c49a898cb851f8fef7d7a6a9501c66b8f03ae05cPhill Cunnington endian machines, where a word is four bytes.
z_crc_t c;
if (first) {
first = 0;
poly = 0;
c = (z_crc_t)n;
crc_table[0][n] = c;
#ifdef BYFOUR
c = crc_table[0][n];
crc_table[k][n] = c;
crc_table_empty = 0;
while (crc_table_empty)
#ifdef MAKECRCH
/* write out CRC tables to crc32.h */
# ifdef BYFOUR
#ifdef MAKECRCH
(unsigned long)(table[n]),
#include "crc32.h"
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
unsigned long crc;
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
#ifdef BYFOUR
if (sizeof(void *) == sizeof(ptrdiff_t)) {
if (*((unsigned char *)(&endian)))
DO8;
if (len) do {
DO1;
} while (--len);
#ifdef BYFOUR
unsigned long crc;
unsigned len;
register z_crc_t c;
len--;
if (len) do {
} while (--len);
unsigned long crc;
unsigned len;
register z_crc_t c;
len--;
buf4--;
buf4++;
if (len) do {
} while (--len);
return (unsigned long)(ZSWAP32(c));
unsigned long *mat;
unsigned long vec;
unsigned long sum;
sum = 0;
while (vec) {
mat++;
return sum;
unsigned long *square;
unsigned long *mat;
for (n = 0; n < GF2_DIM; n++)
unsigned long row;
if (len2 <= 0)
return crc1;
if (len2 == 0)
} while (len2 != 0);
return crc1;