Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
1 /* -*-mode:java; c-basic-offset:2; -*- */
3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This program is based on zlib-1.1.3, so all credit should go authors
31 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
32 * and contributors of zlib.
35 package org.apidesign.bck2brwsr.emul.zip;
37 import org.apidesign.bck2brwsr.emul.lang.System;
39 final class CRC32 implements Checksum {
42 * The following logic has come from RFC1952.
45 private static int[] crc_table = null;
47 crc_table = new int[256];
48 for (int n = 0; n < 256; n++) {
50 for (int k = 8; --k >= 0; ) {
52 c = 0xedb88320 ^ (c >>> 1);
60 public void update (byte[] buf, int index, int len) {
63 c = crc_table[(c^buf[index++])&0xff]^(c >>> 8);
71 public void reset(long vv){
72 v = (int)(vv&0xffffffffL);
75 public long getValue(){
76 return (long)(v&0xffffffffL);
79 // The following logic has come from zlib.1.2.
80 private static final int GF2_DIM = 32;
81 static long combine(long crc1, long crc2, long len2){
83 long[] even = new long[GF2_DIM];
84 long[] odd = new long[GF2_DIM];
86 // degenerate case (also disallow negative lengths)
90 // put operator for one zero bit in odd
91 odd[0] = 0xedb88320L; // CRC-32 polynomial
93 for (int n = 1; n < GF2_DIM; n++) {
98 // put operator for two zero bits in even
99 gf2_matrix_square(even, odd);
101 // put operator for four zero bits in odd
102 gf2_matrix_square(odd, even);
104 // apply len2 zeros to crc1 (first square will put the operator for one
105 // zero byte, eight zero bits, in even)
107 // apply zeros operator for this bit of len2
108 gf2_matrix_square(even, odd);
110 crc1 = gf2_matrix_times(even, crc1);
113 // if no more bits set, then done
117 // another iteration of the loop with odd and even swapped
118 gf2_matrix_square(odd, even);
120 crc1 = gf2_matrix_times(odd, crc1);
123 // if no more bits set, then done
126 /* return combined crc */
131 private static long gf2_matrix_times(long[] mat, long vec){
143 static final void gf2_matrix_square(long[] square, long[] mat) {
144 for (int n = 0; n < GF2_DIM; n++)
145 square[n] = gf2_matrix_times(mat, mat[n]);
149 private java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
151 public void update(byte[] buf, int index, int len){
152 if(buf==null) {crc32.reset();}
153 else{crc32.update(buf, index, len);}
158 public void reset(long init){
163 System.err.println("unsupported operation");
166 public long getValue(){
167 return crc32.getValue();
171 CRC32 foo = new CRC32();
176 public static int[] getCRC32Table(){
177 int[] tmp = new int[crc_table.length];
178 System.arraycopy(crc_table, 0, tmp, 0, tmp.length);