Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/CRC32.java
8650 views
/* -*-mode:java; c-basic-offset:2; -*- */1/*2Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28/*29* This program is based on zlib-1.1.3, so all credit should go authors30* Jean-loup Gailly([email protected]) and Mark Adler([email protected])31* and contributors of zlib.32*/3334package com.jcraft.jzlib;3536final public class CRC32 implements Checksum {3738/*39* The following logic has come from RFC1952.40*/41private int v = 0;42private static int[] crc_table = null;43static {44crc_table = new int[256];45for (int n = 0; n < 256; n++) {46int c = n;47for (int k = 8; --k >= 0; ) {48if ((c & 1) != 0)49c = 0xedb88320 ^ (c >>> 1);50else51c = c >>> 1;52}53crc_table[n] = c;54}55}5657public void update (byte[] buf, int index, int len) {58int c = ~v;59while (--len >= 0)60c = crc_table[(c^buf[index++])&0xff]^(c >>> 8);61v = ~c;62}6364public void reset(){65v = 0;66}6768public void reset(long vv){69v = (int)(vv&0xffffffffL);70}7172public long getValue(){73return (long)(v&0xffffffffL);74}7576// The following logic has come from zlib.1.2.77private static final int GF2_DIM = 32;78static long combine(long crc1, long crc2, long len2){79long row;80long[] even = new long[GF2_DIM];81long[] odd = new long[GF2_DIM];8283// degenerate case (also disallow negative lengths)84if (len2 <= 0)85return crc1;8687// put operator for one zero bit in odd88odd[0] = 0xedb88320L; // CRC-32 polynomial89row = 1;90for (int n = 1; n < GF2_DIM; n++) {91odd[n] = row;92row <<= 1;93}9495// put operator for two zero bits in even96gf2_matrix_square(even, odd);9798// put operator for four zero bits in odd99gf2_matrix_square(odd, even);100101// apply len2 zeros to crc1 (first square will put the operator for one102// zero byte, eight zero bits, in even)103do {104// apply zeros operator for this bit of len2105gf2_matrix_square(even, odd);106if ((len2 & 1)!=0)107crc1 = gf2_matrix_times(even, crc1);108len2 >>= 1;109110// if no more bits set, then done111if (len2 == 0)112break;113114// another iteration of the loop with odd and even swapped115gf2_matrix_square(odd, even);116if ((len2 & 1)!=0)117crc1 = gf2_matrix_times(odd, crc1);118len2 >>= 1;119120// if no more bits set, then done121} while (len2 != 0);122123/* return combined crc */124crc1 ^= crc2;125return crc1;126}127128private static long gf2_matrix_times(long[] mat, long vec){129long sum = 0;130int index = 0;131while (vec!=0) {132if ((vec & 1)!=0)133sum ^= mat[index];134vec >>= 1;135index++;136}137return sum;138}139140static final void gf2_matrix_square(long[] square, long[] mat) {141for (int n = 0; n < GF2_DIM; n++)142square[n] = gf2_matrix_times(mat, mat[n]);143}144145public CRC32 copy(){146CRC32 foo = new CRC32();147foo.v = this.v;148return foo;149}150151public static int[] getCRC32Table(){152int[] tmp = new int[crc_table.length];153System.arraycopy(crc_table, 0, tmp, 0, tmp.length);154return tmp;155}156}157158159