Path: blob/master/src/java.base/share/native/libzip/zlib/zcrc32.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* crc32.c -- compute the CRC-32 of a data stream25* Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*28* Thanks to Rodney Brown <[email protected]> for his contribution of faster29* CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing30* tables for updating the shift register in one step with three exclusive-ors31* instead of four steps with four exclusive-ors. This results in about a32* factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.33*/3435/* @(#) $Id$ */3637/*38Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore39protection on the static variables used to control the first-use generation40of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should41first call get_crc_table() to initialize the tables before allowing more than42one thread to use crc32().4344DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.45*/4647#ifdef MAKECRCH48# include <stdio.h>49# ifndef DYNAMIC_CRC_TABLE50# define DYNAMIC_CRC_TABLE51# endif /* !DYNAMIC_CRC_TABLE */52#endif /* MAKECRCH */5354#include "zutil.h" /* for STDC and FAR definitions */5556/* Definitions for doing the crc four data bytes at a time. */57#if !defined(NOBYFOUR) && defined(Z_U4)58# define BYFOUR59#endif60#ifdef BYFOUR61local unsigned long crc32_little OF((unsigned long,62const unsigned char FAR *, z_size_t));63local unsigned long crc32_big OF((unsigned long,64const unsigned char FAR *, z_size_t));65# define TBLS 866#else67# define TBLS 168#endif /* BYFOUR */6970/* Local functions for crc concatenation */71local unsigned long gf2_matrix_times OF((unsigned long *mat,72unsigned long vec));73local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));74local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));757677#ifdef DYNAMIC_CRC_TABLE7879local volatile int crc_table_empty = 1;80local z_crc_t FAR crc_table[TBLS][256];81local void make_crc_table OF((void));82#ifdef MAKECRCH83local void write_table OF((FILE *, const z_crc_t FAR *));84#endif /* MAKECRCH */85/*86Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:87x^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.8889Polynomials over GF(2) are represented in binary, one bit per coefficient,90with the lowest powers in the most significant bit. Then adding polynomials91is just exclusive-or, and multiplying a polynomial by x is a right shift by92one. If we call the above polynomial p, and represent a byte as the93polynomial q, also with the lowest power in the most significant bit (so the94byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,95where a mod b means the remainder after dividing a by b.9697This calculation is done using the shift-register method of multiplying and98taking the remainder. The register is initialized to zero, and for each99incoming bit, x^32 is added mod p to the register if the bit is a one (where100x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by101x (which is shifting right by one and adding x^32 mod p if the bit shifted102out is a one). We start with the highest power (least significant bit) of103q and repeat for all eight bits of q.104105The first table is simply the CRC of all possible eight bit values. This is106all the information needed to generate CRCs on data a byte at a time for all107combinations of CRC register values and incoming bytes. The remaining tables108allow for word-at-a-time CRC calculation for both big-endian and little-109endian machines, where a word is four bytes.110*/111local void make_crc_table()112{113z_crc_t c;114int n, k;115z_crc_t poly; /* polynomial exclusive-or pattern */116/* terms of polynomial defining this crc (except x^32): */117static volatile int first = 1; /* flag to limit concurrent making */118static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};119120/* See if another task is already doing this (not thread-safe, but better121than nothing -- significantly reduces duration of vulnerability in122case the advice about DYNAMIC_CRC_TABLE is ignored) */123if (first) {124first = 0;125126/* make exclusive-or pattern from polynomial (0xedb88320UL) */127poly = 0;128for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)129poly |= (z_crc_t)1 << (31 - p[n]);130131/* generate a crc for every 8-bit value */132for (n = 0; n < 256; n++) {133c = (z_crc_t)n;134for (k = 0; k < 8; k++)135c = c & 1 ? poly ^ (c >> 1) : c >> 1;136crc_table[0][n] = c;137}138139#ifdef BYFOUR140/* generate crc for each value followed by one, two, and three zeros,141and then the byte reversal of those as well as the first table */142for (n = 0; n < 256; n++) {143c = crc_table[0][n];144crc_table[4][n] = ZSWAP32(c);145for (k = 1; k < 4; k++) {146c = crc_table[0][c & 0xff] ^ (c >> 8);147crc_table[k][n] = c;148crc_table[k + 4][n] = ZSWAP32(c);149}150}151#endif /* BYFOUR */152153crc_table_empty = 0;154}155else { /* not first */156/* wait for the other guy to finish (not efficient, but rare) */157while (crc_table_empty)158;159}160161#ifdef MAKECRCH162/* write out CRC tables to crc32.h */163{164FILE *out;165166out = fopen("crc32.h", "w");167if (out == NULL) return;168fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");169fprintf(out, " * Generated automatically by crc32.c\n */\n\n");170fprintf(out, "local const z_crc_t FAR ");171fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");172write_table(out, crc_table[0]);173# ifdef BYFOUR174fprintf(out, "#ifdef BYFOUR\n");175for (k = 1; k < 8; k++) {176fprintf(out, " },\n {\n");177write_table(out, crc_table[k]);178}179fprintf(out, "#endif\n");180# endif /* BYFOUR */181fprintf(out, " }\n};\n");182fclose(out);183}184#endif /* MAKECRCH */185}186187#ifdef MAKECRCH188local void write_table(out, table)189FILE *out;190const z_crc_t FAR *table;191{192int n;193194for (n = 0; n < 256; n++)195fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",196(unsigned long)(table[n]),197n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));198}199#endif /* MAKECRCH */200201#else /* !DYNAMIC_CRC_TABLE */202/* ========================================================================203* Tables of CRC-32s of all single-byte values, made by make_crc_table().204*/205#include "crc32.h"206#endif /* DYNAMIC_CRC_TABLE */207208/* =========================================================================209* This function can be used by asm versions of crc32()210*/211const z_crc_t FAR * ZEXPORT get_crc_table()212{213#ifdef DYNAMIC_CRC_TABLE214if (crc_table_empty)215make_crc_table();216#endif /* DYNAMIC_CRC_TABLE */217return (const z_crc_t FAR *)crc_table;218}219220/* ========================================================================= */221#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)222#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1223224/* ========================================================================= */225uLong ZEXPORT crc32_z(crc, buf, len)226uLong crc;227const unsigned char FAR *buf;228z_size_t len;229{230if (buf == Z_NULL) return 0UL;231232#ifdef DYNAMIC_CRC_TABLE233if (crc_table_empty)234make_crc_table();235#endif /* DYNAMIC_CRC_TABLE */236237#ifdef BYFOUR238if (sizeof(void *) == sizeof(ptrdiff_t)) {239z_crc_t endian;240241endian = 1;242if (*((unsigned char *)(&endian)))243return (uLong)crc32_little(crc, buf, len);244else245return (uLong)crc32_big(crc, buf, len);246}247#endif /* BYFOUR */248crc = crc ^ 0xffffffffUL;249while (len >= 8) {250DO8;251len -= 8;252}253if (len) do {254DO1;255} while (--len);256return crc ^ 0xffffffffUL;257}258259/* ========================================================================= */260uLong ZEXPORT crc32(crc, buf, len)261uLong crc;262const unsigned char FAR *buf;263uInt len;264{265return crc32_z(crc, buf, len);266}267268#ifdef BYFOUR269270/*271This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit272integer pointer type. This violates the strict aliasing rule, where a273compiler can assume, for optimization purposes, that two pointers to274fundamentally different types won't ever point to the same memory. This can275manifest as a problem only if one of the pointers is written to. This code276only reads from those pointers. So long as this code remains isolated in277this compilation unit, there won't be a problem. For this reason, this code278should not be copied and pasted into a compilation unit in which other code279writes to the buffer that is passed to these routines.280*/281282/* ========================================================================= */283#define DOLIT4 c ^= *buf4++; \284c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \285crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]286#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4287288/* ========================================================================= */289local unsigned long crc32_little(crc, buf, len)290unsigned long crc;291const unsigned char FAR *buf;292z_size_t len;293{294register z_crc_t c;295register const z_crc_t FAR *buf4;296297c = (z_crc_t)crc;298c = ~c;299while (len && ((ptrdiff_t)buf & 3)) {300c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);301len--;302}303304buf4 = (const z_crc_t FAR *)(const void FAR *)buf;305while (len >= 32) {306DOLIT32;307len -= 32;308}309while (len >= 4) {310DOLIT4;311len -= 4;312}313buf = (const unsigned char FAR *)buf4;314315if (len) do {316c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);317} while (--len);318c = ~c;319return (unsigned long)c;320}321322/* ========================================================================= */323#define DOBIG4 c ^= *buf4++; \324c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \325crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]326#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4327328/* ========================================================================= */329local unsigned long crc32_big(crc, buf, len)330unsigned long crc;331const unsigned char FAR *buf;332z_size_t len;333{334register z_crc_t c;335register const z_crc_t FAR *buf4;336337c = ZSWAP32((z_crc_t)crc);338c = ~c;339while (len && ((ptrdiff_t)buf & 3)) {340c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);341len--;342}343344buf4 = (const z_crc_t FAR *)(const void FAR *)buf;345while (len >= 32) {346DOBIG32;347len -= 32;348}349while (len >= 4) {350DOBIG4;351len -= 4;352}353buf = (const unsigned char FAR *)buf4;354355if (len) do {356c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);357} while (--len);358c = ~c;359return (unsigned long)(ZSWAP32(c));360}361362#endif /* BYFOUR */363364#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */365366/* ========================================================================= */367local unsigned long gf2_matrix_times(mat, vec)368unsigned long *mat;369unsigned long vec;370{371unsigned long sum;372373sum = 0;374while (vec) {375if (vec & 1)376sum ^= *mat;377vec >>= 1;378mat++;379}380return sum;381}382383/* ========================================================================= */384local void gf2_matrix_square(square, mat)385unsigned long *square;386unsigned long *mat;387{388int n;389390for (n = 0; n < GF2_DIM; n++)391square[n] = gf2_matrix_times(mat, mat[n]);392}393394/* ========================================================================= */395local uLong crc32_combine_(crc1, crc2, len2)396uLong crc1;397uLong crc2;398z_off64_t len2;399{400int n;401unsigned long row;402unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */403unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */404405/* degenerate case (also disallow negative lengths) */406if (len2 <= 0)407return crc1;408409/* put operator for one zero bit in odd */410odd[0] = 0xedb88320UL; /* CRC-32 polynomial */411row = 1;412for (n = 1; n < GF2_DIM; n++) {413odd[n] = row;414row <<= 1;415}416417/* put operator for two zero bits in even */418gf2_matrix_square(even, odd);419420/* put operator for four zero bits in odd */421gf2_matrix_square(odd, even);422423/* apply len2 zeros to crc1 (first square will put the operator for one424zero byte, eight zero bits, in even) */425do {426/* apply zeros operator for this bit of len2 */427gf2_matrix_square(even, odd);428if (len2 & 1)429crc1 = gf2_matrix_times(even, crc1);430len2 >>= 1;431432/* if no more bits set, then done */433if (len2 == 0)434break;435436/* another iteration of the loop with odd and even swapped */437gf2_matrix_square(odd, even);438if (len2 & 1)439crc1 = gf2_matrix_times(odd, crc1);440len2 >>= 1;441442/* if no more bits set, then done */443} while (len2 != 0);444445/* return combined crc */446crc1 ^= crc2;447return crc1;448}449450/* ========================================================================= */451uLong ZEXPORT crc32_combine(crc1, crc2, len2)452uLong crc1;453uLong crc2;454z_off_t len2;455{456return crc32_combine_(crc1, crc2, len2);457}458459uLong ZEXPORT crc32_combine64(crc1, crc2, len2)460uLong crc1;461uLong crc2;462z_off64_t len2;463{464return crc32_combine_(crc1, crc2, len2);465}466467468