crc32.c revision 180208
175752Smsmith/* crc32.c -- compute the CRC-32 of a data stream
275752Smsmith * Copyright (C) 1995-2005 Mark Adler
375752Smsmith * For conditions of distribution and use, see copyright notice in zlib.h
475752Smsmith *
575752Smsmith * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
675752Smsmith * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
775752Smsmith * tables for updating the shift register in one step with three exclusive-ors
875752Smsmith * instead of four steps with four exclusive-ors.  This results in about a
975752Smsmith * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
1075752Smsmith */
1175752Smsmith
1275752Smsmith/* @(#) $Id$ */
1375752Smsmith
1475752Smsmith/*
1575752Smsmith  Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
1675752Smsmith  protection on the static variables used to control the first-use generation
1775752Smsmith  of the crc tables.  Therefore, if you #define DYNAMIC_CRC_TABLE, you should
1875752Smsmith  first call get_crc_table() to initialize the tables before allowing more than
1975752Smsmith  one thread to use crc32().
2075752Smsmith */
2175752Smsmith
2275752Smsmith#ifdef MAKECRCH
2375752Smsmith#  include <stdio.h>
2475752Smsmith#  ifndef DYNAMIC_CRC_TABLE
2575752Smsmith#    define DYNAMIC_CRC_TABLE
2675752Smsmith#  endif /* !DYNAMIC_CRC_TABLE */
2775752Smsmith#endif /* MAKECRCH */
2875752Smsmith
2975752Smsmith#include "zutil.h"      /* for STDC and FAR definitions */
3075752Smsmith
3175752Smsmith#define local static
32124128Scharnier
33124128Scharnier/* Find a four-byte integer type for crc32_little() and crc32_big(). */
34124128Scharnier#ifndef NOBYFOUR
3575752Smsmith#  ifdef STDC           /* need ANSI C limits.h to determine sizes */
3675752Smsmith#    include <limits.h>
3775752Smsmith#    define BYFOUR
38162799Sru#    if (UINT_MAX == 0xffffffffUL)
3975752Smsmith       typedef unsigned int u4;
4075752Smsmith#    else
4175752Smsmith#      if (ULONG_MAX == 0xffffffffUL)
4275752Smsmith         typedef unsigned long u4;
43103663Simp#      else
4475752Smsmith#        if (USHRT_MAX == 0xffffffffUL)
4587553Smikeh           typedef unsigned short u4;
4687553Smikeh#        else
4787553Smikeh#          undef BYFOUR     /* can't find a four-byte integer type! */
4887553Smikeh#        endif
4987553Smikeh#      endif
5087553Smikeh#    endif
5187553Smikeh#  endif /* STDC */
5275752Smsmith#endif /* !NOBYFOUR */
5375752Smsmith
5475752Smsmith/* Definitions for doing the crc four data bytes at a time. */
5575752Smsmith#ifdef BYFOUR
5675752Smsmith#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
5775752Smsmith                (((w)&0xff00)<<8)+(((w)&0xff)<<24))
5875752Smsmith   local unsigned long crc32_little OF((unsigned long,
5975752Smsmith                        const unsigned char FAR *, unsigned));
6075752Smsmith   local unsigned long crc32_big OF((unsigned long,
6175752Smsmith                        const unsigned char FAR *, unsigned));
6275752Smsmith#  define TBLS 8
6375752Smsmith#else
6475752Smsmith#  define TBLS 1
6575752Smsmith#endif /* BYFOUR */
6675752Smsmith
6775752Smsmith/* Local functions for crc concatenation */
68173057Sjhblocal unsigned long gf2_matrix_times OF((unsigned long *mat,
6975752Smsmith                                         unsigned long vec));
7075752Smsmithlocal void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
7175752Smsmith
7275752Smsmith#ifdef DYNAMIC_CRC_TABLE
7375752Smsmith
7475752Smsmithlocal volatile int crc_table_empty = 1;
7575752Smsmithlocal unsigned long FAR crc_table[TBLS][256];
7675752Smsmithlocal void make_crc_table OF((void));
7775752Smsmith#ifdef MAKECRCH
7875752Smsmith   local void write_table OF((FILE *, const unsigned long FAR *));
7975752Smsmith#endif /* MAKECRCH */
8075752Smsmith/*
8175752Smsmith  Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
8275752Smsmith  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.
8375752Smsmith
8475752Smsmith  Polynomials over GF(2) are represented in binary, one bit per coefficient,
8575752Smsmith  with the lowest powers in the most significant bit.  Then adding polynomials
8675752Smsmith  is just exclusive-or, and multiplying a polynomial by x is a right shift by
8775752Smsmith  one.  If we call the above polynomial p, and represent a byte as the
8875752Smsmith  polynomial q, also with the lowest power in the most significant bit (so the
8975752Smsmith  byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
9075752Smsmith  where a mod b means the remainder after dividing a by b.
9175752Smsmith
9275752Smsmith  This calculation is done using the shift-register method of multiplying and
9375752Smsmith  taking the remainder.  The register is initialized to zero, and for each
9475752Smsmith  incoming bit, x^32 is added mod p to the register if the bit is a one (where
9575752Smsmith  x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
9675752Smsmith  x (which is shifting right by one and adding x^32 mod p if the bit shifted
9775752Smsmith  out is a one).  We start with the highest power (least significant bit) of
9875752Smsmith  q and repeat for all eight bits of q.
9975752Smsmith
10075752Smsmith  The first table is simply the CRC of all possible eight bit values.  This is
10175752Smsmith  all the information needed to generate CRCs on data a byte at a time for all
10275752Smsmith  combinations of CRC register values and incoming bytes.  The remaining tables
10375752Smsmith  allow for word-at-a-time CRC calculation for both big-endian and little-
10475752Smsmith  endian machines, where a word is four bytes.
10575752Smsmith*/
10675752Smsmithlocal void make_crc_table()
10775752Smsmith{
10875752Smsmith    unsigned long c;
10975752Smsmith    int n, k;
11075752Smsmith    unsigned long poly;                 /* polynomial exclusive-or pattern */
11175752Smsmith    /* terms of polynomial defining this crc (except x^32): */
11275752Smsmith    static volatile int first = 1;      /* flag to limit concurrent making */
11375752Smsmith    static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
11475752Smsmith
11575752Smsmith    /* See if another task is already doing this (not thread-safe, but better
11675752Smsmith       than nothing -- significantly reduces duration of vulnerability in
11775752Smsmith       case the advice about DYNAMIC_CRC_TABLE is ignored) */
11875752Smsmith    if (first) {
11975752Smsmith        first = 0;
12075752Smsmith
12175752Smsmith        /* make exclusive-or pattern from polynomial (0xedb88320UL) */
12275752Smsmith        poly = 0UL;
12375752Smsmith        for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
12475752Smsmith            poly |= 1UL << (31 - p[n]);
12575752Smsmith
12675752Smsmith        /* generate a crc for every 8-bit value */
12775752Smsmith        for (n = 0; n < 256; n++) {
12875752Smsmith            c = (unsigned long)n;
12975752Smsmith            for (k = 0; k < 8; k++)
13075752Smsmith                c = c & 1 ? poly ^ (c >> 1) : c >> 1;
13175752Smsmith            crc_table[0][n] = c;
13275752Smsmith        }
13375752Smsmith
13475752Smsmith#ifdef BYFOUR
13575752Smsmith        /* generate crc for each value followed by one, two, and three zeros,
13675752Smsmith           and then the byte reversal of those as well as the first table */
13775752Smsmith        for (n = 0; n < 256; n++) {
13875752Smsmith            c = crc_table[0][n];
13975752Smsmith            crc_table[4][n] = REV(c);
140199291Sattilio            for (k = 1; k < 4; k++) {
14187553Smikeh                c = crc_table[0][c & 0xff] ^ (c >> 8);
14275752Smsmith                crc_table[k][n] = c;
14375752Smsmith                crc_table[k + 4][n] = REV(c);
144111046Simp            }
145111046Simp        }
146111046Simp#endif /* BYFOUR */
147111046Simp
148111046Simp        crc_table_empty = 0;
149111046Simp    }
15075752Smsmith    else {      /* not first */
15175752Smsmith        /* wait for the other guy to finish (not efficient, but rare) */
15275752Smsmith        while (crc_table_empty)
15375752Smsmith            ;
15475752Smsmith    }
15575752Smsmith
15675752Smsmith#ifdef MAKECRCH
15775752Smsmith    /* write out CRC tables to crc32.h */
15875752Smsmith    {
15987553Smikeh        FILE *out;
16075752Smsmith
16175752Smsmith        out = fopen("crc32.h", "w");
16275752Smsmith        if (out == NULL) return;
16375752Smsmith        fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
16475752Smsmith        fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
16575752Smsmith        fprintf(out, "local const unsigned long FAR ");
16687553Smikeh        fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
16775752Smsmith        write_table(out, crc_table[0]);
16875752Smsmith#  ifdef BYFOUR
16975752Smsmith        fprintf(out, "#ifdef BYFOUR\n");
17075752Smsmith        for (k = 1; k < 8; k++) {
17175752Smsmith            fprintf(out, "  },\n  {\n");
17275752Smsmith            write_table(out, crc_table[k]);
17375752Smsmith        }
17475752Smsmith        fprintf(out, "#endif\n");
17575752Smsmith#  endif /* BYFOUR */
17675752Smsmith        fprintf(out, "  }\n};\n");
17775752Smsmith        fclose(out);
17875752Smsmith    }
17975752Smsmith#endif /* MAKECRCH */
18075752Smsmith}
18175752Smsmith
18275752Smsmith#ifdef MAKECRCH
18375752Smsmithlocal void write_table(out, table)
18475752Smsmith    FILE *out;
18575752Smsmith    const unsigned long FAR *table;
18687553Smikeh{
18775752Smsmith    int n;
18875752Smsmith
18975752Smsmith    for (n = 0; n < 256; n++)
19075752Smsmith        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
19175752Smsmith                n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
19275752Smsmith}
19375752Smsmith#endif /* MAKECRCH */
19475752Smsmith
19575752Smsmith#else /* !DYNAMIC_CRC_TABLE */
19675752Smsmith/* ========================================================================
19775752Smsmith * Tables of CRC-32s of all single-byte values, made by make_crc_table().
19875752Smsmith */
19975752Smsmith#include "crc32.h"
200103663Simp#endif /* DYNAMIC_CRC_TABLE */
20175752Smsmith
20275752Smsmith/* =========================================================================
20375752Smsmith * This function can be used by asm versions of crc32()
20475752Smsmith */
20575752Smsmithconst unsigned long FAR * ZEXPORT get_crc_table()
20675752Smsmith{
20775752Smsmith#ifdef DYNAMIC_CRC_TABLE
208103663Simp    if (crc_table_empty)
209103663Simp        make_crc_table();
210103663Simp#endif /* DYNAMIC_CRC_TABLE */
21175752Smsmith    return (const unsigned long FAR *)crc_table;
212162799Sru}
213162799Sru
214162799Sru/* ========================================================================= */
215162799Sru#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
21675752Smsmith#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
21775752Smsmith
21875752Smsmith/* ========================================================================= */
21975752Smsmithunsigned long ZEXPORT crc32(crc, buf, len)
22075752Smsmith    unsigned long crc;
22175752Smsmith    const unsigned char FAR *buf;
22275752Smsmith    unsigned len;
22375752Smsmith{
22475752Smsmith    if (buf == Z_NULL) return 0UL;
22575752Smsmith
22675752Smsmith#ifdef DYNAMIC_CRC_TABLE
22775752Smsmith    if (crc_table_empty)
22875752Smsmith        make_crc_table();
22975752Smsmith#endif /* DYNAMIC_CRC_TABLE */
23075752Smsmith
23175752Smsmith#ifdef BYFOUR
23275752Smsmith    if (sizeof(void *) == sizeof(ptrdiff_t)) {
23375752Smsmith        u4 endian;
234
235        endian = 1;
236        if (*((unsigned char *)(&endian)))
237            return crc32_little(crc, buf, len);
238        else
239            return crc32_big(crc, buf, len);
240    }
241#endif /* BYFOUR */
242    crc = crc ^ 0xffffffffUL;
243    while (len >= 8) {
244        DO8;
245        len -= 8;
246    }
247    if (len) do {
248        DO1;
249    } while (--len);
250    return crc ^ 0xffffffffUL;
251}
252
253#ifdef BYFOUR
254
255/* ========================================================================= */
256#define DOLIT4 c ^= *buf4++; \
257        c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
258            crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
259#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
260
261/* ========================================================================= */
262local unsigned long crc32_little(crc, buf, len)
263    unsigned long crc;
264    const unsigned char FAR *buf;
265    unsigned len;
266{
267    register u4 c;
268    register const u4 FAR *buf4;
269
270    c = (u4)crc;
271    c = ~c;
272    while (len && ((ptrdiff_t)buf & 3)) {
273        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
274        len--;
275    }
276
277    buf4 = (const u4 FAR *)(const void FAR *)buf;
278    while (len >= 32) {
279        DOLIT32;
280        len -= 32;
281    }
282    while (len >= 4) {
283        DOLIT4;
284        len -= 4;
285    }
286    buf = (const unsigned char FAR *)buf4;
287
288    if (len) do {
289        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
290    } while (--len);
291    c = ~c;
292    return (unsigned long)c;
293}
294
295/* ========================================================================= */
296#define DOBIG4 c ^= *++buf4; \
297        c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
298            crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
299#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
300
301/* ========================================================================= */
302local unsigned long crc32_big(crc, buf, len)
303    unsigned long crc;
304    const unsigned char FAR *buf;
305    unsigned len;
306{
307    register u4 c;
308    register const u4 FAR *buf4;
309
310    c = REV((u4)crc);
311    c = ~c;
312    while (len && ((ptrdiff_t)buf & 3)) {
313        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
314        len--;
315    }
316
317    buf4 = (const u4 FAR *)(const void FAR *)buf;
318    buf4--;
319    while (len >= 32) {
320        DOBIG32;
321        len -= 32;
322    }
323    while (len >= 4) {
324        DOBIG4;
325        len -= 4;
326    }
327    buf4++;
328    buf = (const unsigned char FAR *)buf4;
329
330    if (len) do {
331        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
332    } while (--len);
333    c = ~c;
334    return (unsigned long)(REV(c));
335}
336
337#endif /* BYFOUR */
338
339#define GF2_DIM 32      /* dimension of GF(2) vectors (length of CRC) */
340
341/* ========================================================================= */
342local unsigned long gf2_matrix_times(mat, vec)
343    unsigned long *mat;
344    unsigned long vec;
345{
346    unsigned long sum;
347
348    sum = 0;
349    while (vec) {
350        if (vec & 1)
351            sum ^= *mat;
352        vec >>= 1;
353        mat++;
354    }
355    return sum;
356}
357
358/* ========================================================================= */
359local void gf2_matrix_square(square, mat)
360    unsigned long *square;
361    unsigned long *mat;
362{
363    int n;
364
365    for (n = 0; n < GF2_DIM; n++)
366        square[n] = gf2_matrix_times(mat, mat[n]);
367}
368
369/* ========================================================================= */
370uLong ZEXPORT crc32_combine(crc1, crc2, len2)
371    uLong crc1;
372    uLong crc2;
373    z_off_t len2;
374{
375    int n;
376    unsigned long row;
377    unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
378    unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
379
380    /* degenerate case */
381    if (len2 == 0)
382        return crc1;
383
384    /* put operator for one zero bit in odd */
385    odd[0] = 0xedb88320L;           /* CRC-32 polynomial */
386    row = 1;
387    for (n = 1; n < GF2_DIM; n++) {
388        odd[n] = row;
389        row <<= 1;
390    }
391
392    /* put operator for two zero bits in even */
393    gf2_matrix_square(even, odd);
394
395    /* put operator for four zero bits in odd */
396    gf2_matrix_square(odd, even);
397
398    /* apply len2 zeros to crc1 (first square will put the operator for one
399       zero byte, eight zero bits, in even) */
400    do {
401        /* apply zeros operator for this bit of len2 */
402        gf2_matrix_square(even, odd);
403        if (len2 & 1)
404            crc1 = gf2_matrix_times(even, crc1);
405        len2 >>= 1;
406
407        /* if no more bits set, then done */
408        if (len2 == 0)
409            break;
410
411        /* another iteration of the loop with odd and even swapped */
412        gf2_matrix_square(odd, even);
413        if (len2 & 1)
414            crc1 = gf2_matrix_times(odd, crc1);
415        len2 >>= 1;
416
417        /* if no more bits set, then done */
418    } while (len2 != 0);
419
420    /* return combined crc */
421    crc1 ^= crc2;
422    return crc1;
423}
424