zlib121/crc32.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 07 Dec 2003 05:29:20 +0000
changeset 602 691c1eadb8b7
permissions -rw-r--r--
Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/* crc32.c -- compute the CRC-32 of a data stream
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * Copyright (C) 1995-2003 Mark Adler
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * tables for updating the shift register in one step with three exclusive-ors
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * instead of four steps with four exclusive-ors.  This results about a factor
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 * of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
/* @(#) $Id: crc32.c,v 1.1 2003/12/07 05:29:20 icculus Exp $ */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#ifdef MAKECRCH
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#  include <stdio.h>
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#  ifndef DYNAMIC_CRC_TABLE
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#    define DYNAMIC_CRC_TABLE
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#  endif /* !DYNAMIC_CRC_TABLE */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#endif /* MAKECRCH */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
#include "zutil.h"      /* for STDC and FAR definitions */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#define local static
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
/* Find a four-byte integer type for crc32_little() and crc32_big(). */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
#ifndef NOBYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#  ifdef STDC           /* need ANSI C limits.h to determine sizes */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#    include <limits.h>
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#    define BYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#    if (UINT_MAX == 0xffffffffUL)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
       typedef unsigned int u4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#    else
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#      if (ULONG_MAX == 0xffffffffUL)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
         typedef unsigned long u4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#      else
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#        if (USHRT_MAX == 0xffffffffUL)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
           typedef unsigned short u4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#        else
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#          undef BYFOUR     /* can't find a four-byte integer type! */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#        endif
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#      endif
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
#    endif
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#  endif /* STDC */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#endif /* !NOBYFOUR */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
/* Definitions for doing the crc four data bytes at a time. */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#ifdef BYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
                (((w)&0xff00)<<8)+(((w)&0xff)<<24))
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
   local unsigned long crc32_little OF((unsigned long,
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
                        const unsigned char FAR *, unsigned));
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
   local unsigned long crc32_big OF((unsigned long,
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
                        const unsigned char FAR *, unsigned));
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
#  define TBLS 8
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
#else
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
#  define TBLS 1
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
#endif /* BYFOUR */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
#ifdef DYNAMIC_CRC_TABLE
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
local int crc_table_empty = 1;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
local unsigned long FAR crc_table[TBLS][256];
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
local void make_crc_table OF((void));
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
#ifdef MAKECRCH
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
   local void write_table OF((FILE *, const unsigned long FAR *));
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
#endif /* MAKECRCH */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
/*
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
  Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
  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.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
  Polynomials over GF(2) are represented in binary, one bit per coefficient,
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
  with the lowest powers in the most significant bit.  Then adding polynomials
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
  is just exclusive-or, and multiplying a polynomial by x is a right shift by
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
  one.  If we call the above polynomial p, and represent a byte as the
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
  polynomial q, also with the lowest power in the most significant bit (so the
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
  byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
  where a mod b means the remainder after dividing a by b.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
  This calculation is done using the shift-register method of multiplying and
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
  taking the remainder.  The register is initialized to zero, and for each
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
  incoming bit, x^32 is added mod p to the register if the bit is a one (where
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
  x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
  x (which is shifting right by one and adding x^32 mod p if the bit shifted
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
  out is a one).  We start with the highest power (least significant bit) of
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
  q and repeat for all eight bits of q.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
  The first table is simply the CRC of all possible eight bit values.  This is
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
  all the information needed to generate CRCs on data a byte at a time for all
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
  combinations of CRC register values and incoming bytes.  The remaining tables
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
  allow for word-at-a-time CRC calculation for both big-endian and little-
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
  endian machines, where a word is four bytes.
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
*/
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
local void make_crc_table()
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    unsigned long c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    int n, k;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    unsigned long poly;            /* polynomial exclusive-or pattern */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    /* terms of polynomial defining this crc (except x^32): */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    /* make exclusive-or pattern from polynomial (0xedb88320UL) */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    poly = 0UL;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
        poly |= 1UL << (31 - p[n]);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    /* generate a crc for every 8-bit value */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
    for (n = 0; n < 256; n++) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        c = (unsigned long)n;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
        for (k = 0; k < 8; k++)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
            c = c & 1 ? poly ^ (c >> 1) : c >> 1;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        crc_table[0][n] = c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
#ifdef BYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    /* generate crc for each value followed by one, two, and three zeros, and
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
       then the byte reversal of those as well as the first table */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    for (n = 0; n < 256; n++) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        c = crc_table[0][n];
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
        crc_table[4][n] = REV(c);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
        for (k = 1; k < 4; k++) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
            c = crc_table[0][c & 0xff] ^ (c >> 8);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
            crc_table[k][n] = c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
            crc_table[k + 4][n] = REV(c);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
        }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
#endif /* BYFOUR */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
  crc_table_empty = 0;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
#ifdef MAKECRCH
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    /* write out CRC tables to crc32.h */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
        FILE *out;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
        out = fopen("crc32.h", "w");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
        if (out == NULL) return;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
        fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
        fprintf(out, "local const unsigned long FAR ");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
        fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
        write_table(out, crc_table[0]);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
#  ifdef BYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        fprintf(out, "#ifdef BYFOUR\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
        for (k = 1; k < 8; k++) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
            fprintf(out, "  },\n  {\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
            write_table(out, crc_table[k]);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
        fprintf(out, "#endif\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
#  endif /* BYFOUR */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
        fprintf(out, "  }\n};\n");
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
        fclose(out);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
#endif /* MAKECRCH */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
}
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
#ifdef MAKECRCH
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
local void write_table(out, table)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    FILE *out;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    const unsigned long FAR *table;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    int n;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    for (n = 0; n < 256; n++)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
                n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
}
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
#endif /* MAKECRCH */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
#else /* !DYNAMIC_CRC_TABLE */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
/* ========================================================================
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
 * Tables of CRC-32s of all single-byte values, made by make_crc_table().
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
 */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
#include "crc32.h"
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
#endif /* DYNAMIC_CRC_TABLE */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
/* =========================================================================
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
 * This function can be used by asm versions of crc32()
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
 */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
const unsigned long FAR * ZEXPORT get_crc_table()
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
#ifdef DYNAMIC_CRC_TABLE
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
  if (crc_table_empty) make_crc_table();
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
#endif /* DYNAMIC_CRC_TABLE */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
  return (const unsigned long FAR *)crc_table;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
}
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
/* ========================================================================= */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
/* ========================================================================= */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
unsigned long ZEXPORT crc32(crc, buf, len)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    unsigned long crc;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    const unsigned char FAR *buf;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    unsigned len;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    if (buf == Z_NULL) return 0UL;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
#ifdef DYNAMIC_CRC_TABLE
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    if (crc_table_empty)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        make_crc_table();
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
#endif /* DYNAMIC_CRC_TABLE */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
#ifdef BYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    if (sizeof(void *) == sizeof(ptrdiff_t)) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
        u4 endian;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
        endian = 1;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
        if (*((unsigned char *)(&endian)))
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
            return crc32_little(crc, buf, len);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
        else
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
            return crc32_big(crc, buf, len);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
#endif /* BYFOUR */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    crc = crc ^ 0xffffffffUL;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    while (len >= 8) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        DO8;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
        len -= 8;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
    if (len) do {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
        DO1;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    } while (--len);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    return crc ^ 0xffffffffUL;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
}
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
#ifdef BYFOUR
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
/* ========================================================================= */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
#define DOLIT4 c ^= *buf4++; \
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
        c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
            crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
/* ========================================================================= */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
local unsigned long crc32_little(crc, buf, len)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    unsigned long crc;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    const unsigned char FAR *buf;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    unsigned len;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    register u4 c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    register const u4 FAR *buf4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    c = (u4)crc;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    c = ~c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    while (len && ((ptrdiff_t)buf & 3)) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        len--;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    buf4 = (const u4 FAR *)buf;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    while (len >= 32) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        DOLIT32;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
        len -= 32;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    while (len >= 4) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
        DOLIT4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
        len -= 4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    buf = (const unsigned char FAR *)buf4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    if (len) do {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    } while (--len);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    c = ~c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    return (unsigned long)c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
}
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
/* ========================================================================= */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
#define DOBIG4 c ^= *++buf4; \
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
            crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
/* ========================================================================= */
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
local unsigned long crc32_big(crc, buf, len)
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    unsigned long crc;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    const unsigned char FAR *buf;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    unsigned len;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
{
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    register u4 c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    register const u4 FAR *buf4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    c = REV((u4)crc);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    c = ~c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    while (len && ((ptrdiff_t)buf & 3)) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        len--;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    buf4 = (const u4 FAR *)buf;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    buf4--;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    while (len >= 32) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
        DOBIG32;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
        len -= 32;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    while (len >= 4) {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        DOBIG4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        len -= 4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    }
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    buf4++;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    buf = (const unsigned char FAR *)buf4;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    if (len) do {
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    } while (--len);
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    c = ~c;
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    return (unsigned long)(REV(c));
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
}
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
691c1eadb8b7 Upgraded internal zlib to 1.2.1 (thanks, Adam!)
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
#endif /* BYFOUR */