jdk/src/share/native/java/util/zip/zlib-1.2.3/zcrc32.c
changeset 11303 5f48992867e6
parent 11302 a6305295d4d9
parent 11239 885050364691
child 11304 5d3d2bd1dfd1
equal deleted inserted replaced
11302:a6305295d4d9 11303:5f48992867e6
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Oracle designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Oracle in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21  * or visit www.oracle.com if you need additional information or have any
       
    22  * questions.
       
    23  */
       
    24 
       
    25 /* crc32.c -- compute the CRC-32 of a data stream
       
    26  * Copyright (C) 1995-2005 Mark Adler
       
    27  * For conditions of distribution and use, see copyright notice in zlib.h
       
    28  *
       
    29  * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
       
    30  * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
       
    31  * tables for updating the shift register in one step with three exclusive-ors
       
    32  * instead of four steps with four exclusive-ors.  This results in about a
       
    33  * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
       
    34  */
       
    35 
       
    36 /* @(#) $Id$ */
       
    37 
       
    38 /*
       
    39   Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
       
    40   protection on the static variables used to control the first-use generation
       
    41   of the crc tables.  Therefore, if you #define DYNAMIC_CRC_TABLE, you should
       
    42   first call get_crc_table() to initialize the tables before allowing more than
       
    43   one thread to use crc32().
       
    44  */
       
    45 
       
    46 #ifdef MAKECRCH
       
    47 #  include <stdio.h>
       
    48 #  ifndef DYNAMIC_CRC_TABLE
       
    49 #    define DYNAMIC_CRC_TABLE
       
    50 #  endif /* !DYNAMIC_CRC_TABLE */
       
    51 #endif /* MAKECRCH */
       
    52 
       
    53 #include "zutil.h"      /* for STDC and FAR definitions */
       
    54 
       
    55 #define local static
       
    56 
       
    57 /* Find a four-byte integer type for crc32_little() and crc32_big(). */
       
    58 #ifndef NOBYFOUR
       
    59 #  ifdef STDC           /* need ANSI C limits.h to determine sizes */
       
    60 #    include <limits.h>
       
    61 #    define BYFOUR
       
    62 #    if (UINT_MAX == 0xffffffffUL)
       
    63        typedef unsigned int u4;
       
    64 #    else
       
    65 #      if (ULONG_MAX == 0xffffffffUL)
       
    66          typedef unsigned long u4;
       
    67 #      else
       
    68 #        if (USHRT_MAX == 0xffffffffUL)
       
    69            typedef unsigned short u4;
       
    70 #        else
       
    71 #          undef BYFOUR     /* can't find a four-byte integer type! */
       
    72 #        endif
       
    73 #      endif
       
    74 #    endif
       
    75 #  endif /* STDC */
       
    76 #endif /* !NOBYFOUR */
       
    77 
       
    78 /* Definitions for doing the crc four data bytes at a time. */
       
    79 #ifdef BYFOUR
       
    80 #  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
       
    81                 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
       
    82    local unsigned long crc32_little OF((unsigned long,
       
    83                         const unsigned char FAR *, unsigned));
       
    84    local unsigned long crc32_big OF((unsigned long,
       
    85                         const unsigned char FAR *, unsigned));
       
    86 #  define TBLS 8
       
    87 #else
       
    88 #  define TBLS 1
       
    89 #endif /* BYFOUR */
       
    90 
       
    91 /* Local functions for crc concatenation */
       
    92 local unsigned long gf2_matrix_times OF((unsigned long *mat,
       
    93                                          unsigned long vec));
       
    94 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
       
    95 
       
    96 #ifdef DYNAMIC_CRC_TABLE
       
    97 
       
    98 local volatile int crc_table_empty = 1;
       
    99 local unsigned long FAR crc_table[TBLS][256];
       
   100 local void make_crc_table OF((void));
       
   101 #ifdef MAKECRCH
       
   102    local void write_table OF((FILE *, const unsigned long FAR *));
       
   103 #endif /* MAKECRCH */
       
   104 /*
       
   105   Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
       
   106   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.
       
   107 
       
   108   Polynomials over GF(2) are represented in binary, one bit per coefficient,
       
   109   with the lowest powers in the most significant bit.  Then adding polynomials
       
   110   is just exclusive-or, and multiplying a polynomial by x is a right shift by
       
   111   one.  If we call the above polynomial p, and represent a byte as the
       
   112   polynomial q, also with the lowest power in the most significant bit (so the
       
   113   byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
       
   114   where a mod b means the remainder after dividing a by b.
       
   115 
       
   116   This calculation is done using the shift-register method of multiplying and
       
   117   taking the remainder.  The register is initialized to zero, and for each
       
   118   incoming bit, x^32 is added mod p to the register if the bit is a one (where
       
   119   x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
       
   120   x (which is shifting right by one and adding x^32 mod p if the bit shifted
       
   121   out is a one).  We start with the highest power (least significant bit) of
       
   122   q and repeat for all eight bits of q.
       
   123 
       
   124   The first table is simply the CRC of all possible eight bit values.  This is
       
   125   all the information needed to generate CRCs on data a byte at a time for all
       
   126   combinations of CRC register values and incoming bytes.  The remaining tables
       
   127   allow for word-at-a-time CRC calculation for both big-endian and little-
       
   128   endian machines, where a word is four bytes.
       
   129 */
       
   130 local void make_crc_table()
       
   131 {
       
   132     unsigned long c;
       
   133     int n, k;
       
   134     unsigned long poly;                 /* polynomial exclusive-or pattern */
       
   135     /* terms of polynomial defining this crc (except x^32): */
       
   136     static volatile int first = 1;      /* flag to limit concurrent making */
       
   137     static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
       
   138 
       
   139     /* See if another task is already doing this (not thread-safe, but better
       
   140        than nothing -- significantly reduces duration of vulnerability in
       
   141        case the advice about DYNAMIC_CRC_TABLE is ignored) */
       
   142     if (first) {
       
   143         first = 0;
       
   144 
       
   145         /* make exclusive-or pattern from polynomial (0xedb88320UL) */
       
   146         poly = 0UL;
       
   147         for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
       
   148             poly |= 1UL << (31 - p[n]);
       
   149 
       
   150         /* generate a crc for every 8-bit value */
       
   151         for (n = 0; n < 256; n++) {
       
   152             c = (unsigned long)n;
       
   153             for (k = 0; k < 8; k++)
       
   154                 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
       
   155             crc_table[0][n] = c;
       
   156         }
       
   157 
       
   158 #ifdef BYFOUR
       
   159         /* generate crc for each value followed by one, two, and three zeros,
       
   160            and then the byte reversal of those as well as the first table */
       
   161         for (n = 0; n < 256; n++) {
       
   162             c = crc_table[0][n];
       
   163             crc_table[4][n] = REV(c);
       
   164             for (k = 1; k < 4; k++) {
       
   165                 c = crc_table[0][c & 0xff] ^ (c >> 8);
       
   166                 crc_table[k][n] = c;
       
   167                 crc_table[k + 4][n] = REV(c);
       
   168             }
       
   169         }
       
   170 #endif /* BYFOUR */
       
   171 
       
   172         crc_table_empty = 0;
       
   173     }
       
   174     else {      /* not first */
       
   175         /* wait for the other guy to finish (not efficient, but rare) */
       
   176         while (crc_table_empty)
       
   177             ;
       
   178     }
       
   179 
       
   180 #ifdef MAKECRCH
       
   181     /* write out CRC tables to crc32.h */
       
   182     {
       
   183         FILE *out;
       
   184 
       
   185         out = fopen("crc32.h", "w");
       
   186         if (out == NULL) return;
       
   187         fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
       
   188         fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
       
   189         fprintf(out, "local const unsigned long FAR ");
       
   190         fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
       
   191         write_table(out, crc_table[0]);
       
   192 #  ifdef BYFOUR
       
   193         fprintf(out, "#ifdef BYFOUR\n");
       
   194         for (k = 1; k < 8; k++) {
       
   195             fprintf(out, "  },\n  {\n");
       
   196             write_table(out, crc_table[k]);
       
   197         }
       
   198         fprintf(out, "#endif\n");
       
   199 #  endif /* BYFOUR */
       
   200         fprintf(out, "  }\n};\n");
       
   201         fclose(out);
       
   202     }
       
   203 #endif /* MAKECRCH */
       
   204 }
       
   205 
       
   206 #ifdef MAKECRCH
       
   207 local void write_table(out, table)
       
   208     FILE *out;
       
   209     const unsigned long FAR *table;
       
   210 {
       
   211     int n;
       
   212 
       
   213     for (n = 0; n < 256; n++)
       
   214         fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
       
   215                 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
       
   216 }
       
   217 #endif /* MAKECRCH */
       
   218 
       
   219 #else /* !DYNAMIC_CRC_TABLE */
       
   220 /* ========================================================================
       
   221  * Tables of CRC-32s of all single-byte values, made by make_crc_table().
       
   222  */
       
   223 #include "crc32.h"
       
   224 #endif /* DYNAMIC_CRC_TABLE */
       
   225 
       
   226 /* =========================================================================
       
   227  * This function can be used by asm versions of crc32()
       
   228  */
       
   229 const unsigned long FAR * ZEXPORT get_crc_table()
       
   230 {
       
   231 #ifdef DYNAMIC_CRC_TABLE
       
   232     if (crc_table_empty)
       
   233         make_crc_table();
       
   234 #endif /* DYNAMIC_CRC_TABLE */
       
   235     return (const unsigned long FAR *)crc_table;
       
   236 }
       
   237 
       
   238 /* ========================================================================= */
       
   239 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
       
   240 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
       
   241 
       
   242 /* ========================================================================= */
       
   243 uLong ZEXPORT crc32(crc, buf, len)
       
   244     uLong crc;
       
   245     const unsigned char FAR *buf;
       
   246     unsigned len;
       
   247 {
       
   248     if (buf == Z_NULL) return 0UL;
       
   249 
       
   250 #ifdef DYNAMIC_CRC_TABLE
       
   251     if (crc_table_empty)
       
   252         make_crc_table();
       
   253 #endif /* DYNAMIC_CRC_TABLE */
       
   254 
       
   255 #ifdef BYFOUR
       
   256     if (sizeof(void *) == sizeof(ptrdiff_t)) {
       
   257         u4 endian;
       
   258 
       
   259         endian = 1;
       
   260         if (*((unsigned char *)(&endian)))
       
   261             return (uLong)crc32_little(crc, buf, len);
       
   262         else
       
   263             return (uLong)crc32_big(crc, buf, len);
       
   264     }
       
   265 #endif /* BYFOUR */
       
   266     crc = crc ^ 0xffffffffUL;
       
   267     while (len >= 8) {
       
   268         DO8;
       
   269         len -= 8;
       
   270     }
       
   271     if (len) do {
       
   272         DO1;
       
   273     } while (--len);
       
   274     return crc ^ 0xffffffffUL;
       
   275 }
       
   276 
       
   277 #ifdef BYFOUR
       
   278 
       
   279 /* ========================================================================= */
       
   280 #define DOLIT4 c ^= *buf4++; \
       
   281         c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
       
   282             crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
       
   283 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
       
   284 
       
   285 /* ========================================================================= */
       
   286 local unsigned long crc32_little(crc, buf, len)
       
   287     unsigned long crc;
       
   288     const unsigned char FAR *buf;
       
   289     unsigned len;
       
   290 {
       
   291     register u4 c;
       
   292     register const u4 FAR *buf4;
       
   293 
       
   294     c = (u4)crc;
       
   295     c = ~c;
       
   296     while (len && ((ptrdiff_t)buf & 3)) {
       
   297         c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
       
   298         len--;
       
   299     }
       
   300 
       
   301     buf4 = (const u4 FAR *)(const void FAR *)buf;
       
   302     while (len >= 32) {
       
   303         DOLIT32;
       
   304         len -= 32;
       
   305     }
       
   306     while (len >= 4) {
       
   307         DOLIT4;
       
   308         len -= 4;
       
   309     }
       
   310     buf = (const unsigned char FAR *)buf4;
       
   311 
       
   312     if (len) do {
       
   313         c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
       
   314     } while (--len);
       
   315     c = ~c;
       
   316     return (unsigned long)c;
       
   317 }
       
   318 
       
   319 /* ========================================================================= */
       
   320 #define DOBIG4 c ^= *++buf4; \
       
   321         c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
       
   322             crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
       
   323 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
       
   324 
       
   325 /* ========================================================================= */
       
   326 local unsigned long crc32_big(crc, buf, len)
       
   327     unsigned long crc;
       
   328     const unsigned char FAR *buf;
       
   329     unsigned len;
       
   330 {
       
   331     register u4 c;
       
   332     register const u4 FAR *buf4;
       
   333 
       
   334     c = REV((u4)crc);
       
   335     c = ~c;
       
   336     while (len && ((ptrdiff_t)buf & 3)) {
       
   337         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
       
   338         len--;
       
   339     }
       
   340 
       
   341     buf4 = (const u4 FAR *)(const void FAR *)buf;
       
   342     buf4--;
       
   343     while (len >= 32) {
       
   344         DOBIG32;
       
   345         len -= 32;
       
   346     }
       
   347     while (len >= 4) {
       
   348         DOBIG4;
       
   349         len -= 4;
       
   350     }
       
   351     buf4++;
       
   352     buf = (const unsigned char FAR *)buf4;
       
   353 
       
   354     if (len) do {
       
   355         c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
       
   356     } while (--len);
       
   357     c = ~c;
       
   358     return (unsigned long)(REV(c));
       
   359 }
       
   360 
       
   361 #endif /* BYFOUR */
       
   362 
       
   363 #define GF2_DIM 32      /* dimension of GF(2) vectors (length of CRC) */
       
   364 
       
   365 /* ========================================================================= */
       
   366 local unsigned long gf2_matrix_times(mat, vec)
       
   367     unsigned long *mat;
       
   368     unsigned long vec;
       
   369 {
       
   370     unsigned long sum;
       
   371 
       
   372     sum = 0;
       
   373     while (vec) {
       
   374         if (vec & 1)
       
   375             sum ^= *mat;
       
   376         vec >>= 1;
       
   377         mat++;
       
   378     }
       
   379     return sum;
       
   380 }
       
   381 
       
   382 /* ========================================================================= */
       
   383 local void gf2_matrix_square(square, mat)
       
   384     unsigned long *square;
       
   385     unsigned long *mat;
       
   386 {
       
   387     int n;
       
   388 
       
   389     for (n = 0; n < GF2_DIM; n++)
       
   390         square[n] = gf2_matrix_times(mat, mat[n]);
       
   391 }
       
   392 
       
   393 /* ========================================================================= */
       
   394 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
       
   395     uLong crc1;
       
   396     uLong crc2;
       
   397     z_off_t len2;
       
   398 {
       
   399     int n;
       
   400     unsigned long row;
       
   401     unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
       
   402     unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
       
   403 
       
   404     /* degenerate case */
       
   405     if (len2 == 0)
       
   406         return crc1;
       
   407 
       
   408     /* put operator for one zero bit in odd */
       
   409     odd[0] = 0xedb88320UL;           /* CRC-32 polynomial */
       
   410     row = 1;
       
   411     for (n = 1; n < GF2_DIM; n++) {
       
   412         odd[n] = row;
       
   413         row <<= 1;
       
   414     }
       
   415 
       
   416     /* put operator for two zero bits in even */
       
   417     gf2_matrix_square(even, odd);
       
   418 
       
   419     /* put operator for four zero bits in odd */
       
   420     gf2_matrix_square(odd, even);
       
   421 
       
   422     /* apply len2 zeros to crc1 (first square will put the operator for one
       
   423        zero byte, eight zero bits, in even) */
       
   424     do {
       
   425         /* apply zeros operator for this bit of len2 */
       
   426         gf2_matrix_square(even, odd);
       
   427         if (len2 & 1)
       
   428             crc1 = gf2_matrix_times(even, crc1);
       
   429         len2 >>= 1;
       
   430 
       
   431         /* if no more bits set, then done */
       
   432         if (len2 == 0)
       
   433             break;
       
   434 
       
   435         /* another iteration of the loop with odd and even swapped */
       
   436         gf2_matrix_square(odd, even);
       
   437         if (len2 & 1)
       
   438             crc1 = gf2_matrix_times(odd, crc1);
       
   439         len2 >>= 1;
       
   440 
       
   441         /* if no more bits set, then done */
       
   442     } while (len2 != 0);
       
   443 
       
   444     /* return combined crc */
       
   445     crc1 ^= crc2;
       
   446     return crc1;
       
   447 }