1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/CRC32.java Fri Mar 22 16:59:47 2013 +0100
1.3 @@ -0,0 +1,181 @@
1.4 +/* -*-mode:java; c-basic-offset:2; -*- */
1.5 +/*
1.6 +Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
1.7 +
1.8 +Redistribution and use in source and binary forms, with or without
1.9 +modification, are permitted provided that the following conditions are met:
1.10 +
1.11 + 1. Redistributions of source code must retain the above copyright notice,
1.12 + this list of conditions and the following disclaimer.
1.13 +
1.14 + 2. Redistributions in binary form must reproduce the above copyright
1.15 + notice, this list of conditions and the following disclaimer in
1.16 + the documentation and/or other materials provided with the distribution.
1.17 +
1.18 + 3. The names of the authors may not be used to endorse or promote products
1.19 + derived from this software without specific prior written permission.
1.20 +
1.21 +THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
1.22 +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1.23 +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
1.24 +INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
1.25 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1.26 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
1.27 +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1.28 +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1.29 +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1.30 +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.31 + */
1.32 +/*
1.33 + * This program is based on zlib-1.1.3, so all credit should go authors
1.34 + * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
1.35 + * and contributors of zlib.
1.36 + */
1.37 +
1.38 +package org.apidesign.bck2brwsr.emul.zip;
1.39 +
1.40 +import org.apidesign.bck2brwsr.emul.lang.System;
1.41 +
1.42 +final class CRC32 implements Checksum {
1.43 +
1.44 + /*
1.45 + * The following logic has come from RFC1952.
1.46 + */
1.47 + private int v = 0;
1.48 + private static int[] crc_table = null;
1.49 + static {
1.50 + crc_table = new int[256];
1.51 + for (int n = 0; n < 256; n++) {
1.52 + int c = n;
1.53 + for (int k = 8; --k >= 0; ) {
1.54 + if ((c & 1) != 0)
1.55 + c = 0xedb88320 ^ (c >>> 1);
1.56 + else
1.57 + c = c >>> 1;
1.58 + }
1.59 + crc_table[n] = c;
1.60 + }
1.61 + }
1.62 +
1.63 + public void update (byte[] buf, int index, int len) {
1.64 + int c = ~v;
1.65 + while (--len >= 0)
1.66 + c = crc_table[(c^buf[index++])&0xff]^(c >>> 8);
1.67 + v = ~c;
1.68 + }
1.69 +
1.70 + public void reset(){
1.71 + v = 0;
1.72 + }
1.73 +
1.74 + public void reset(long vv){
1.75 + v = (int)(vv&0xffffffffL);
1.76 + }
1.77 +
1.78 + public long getValue(){
1.79 + return (long)(v&0xffffffffL);
1.80 + }
1.81 +
1.82 + // The following logic has come from zlib.1.2.
1.83 + private static final int GF2_DIM = 32;
1.84 + static long combine(long crc1, long crc2, long len2){
1.85 + long row;
1.86 + long[] even = new long[GF2_DIM];
1.87 + long[] odd = new long[GF2_DIM];
1.88 +
1.89 + // degenerate case (also disallow negative lengths)
1.90 + if (len2 <= 0)
1.91 + return crc1;
1.92 +
1.93 + // put operator for one zero bit in odd
1.94 + odd[0] = 0xedb88320L; // CRC-32 polynomial
1.95 + row = 1;
1.96 + for (int n = 1; n < GF2_DIM; n++) {
1.97 + odd[n] = row;
1.98 + row <<= 1;
1.99 + }
1.100 +
1.101 + // put operator for two zero bits in even
1.102 + gf2_matrix_square(even, odd);
1.103 +
1.104 + // put operator for four zero bits in odd
1.105 + gf2_matrix_square(odd, even);
1.106 +
1.107 + // apply len2 zeros to crc1 (first square will put the operator for one
1.108 + // zero byte, eight zero bits, in even)
1.109 + do {
1.110 + // apply zeros operator for this bit of len2
1.111 + gf2_matrix_square(even, odd);
1.112 + if ((len2 & 1)!=0)
1.113 + crc1 = gf2_matrix_times(even, crc1);
1.114 + len2 >>= 1;
1.115 +
1.116 + // if no more bits set, then done
1.117 + if (len2 == 0)
1.118 + break;
1.119 +
1.120 + // another iteration of the loop with odd and even swapped
1.121 + gf2_matrix_square(odd, even);
1.122 + if ((len2 & 1)!=0)
1.123 + crc1 = gf2_matrix_times(odd, crc1);
1.124 + len2 >>= 1;
1.125 +
1.126 + // if no more bits set, then done
1.127 + } while (len2 != 0);
1.128 +
1.129 + /* return combined crc */
1.130 + crc1 ^= crc2;
1.131 + return crc1;
1.132 + }
1.133 +
1.134 + private static long gf2_matrix_times(long[] mat, long vec){
1.135 + long sum = 0;
1.136 + int index = 0;
1.137 + while (vec!=0) {
1.138 + if ((vec & 1)!=0)
1.139 + sum ^= mat[index];
1.140 + vec >>= 1;
1.141 + index++;
1.142 + }
1.143 + return sum;
1.144 + }
1.145 +
1.146 + static final void gf2_matrix_square(long[] square, long[] mat) {
1.147 + for (int n = 0; n < GF2_DIM; n++)
1.148 + square[n] = gf2_matrix_times(mat, mat[n]);
1.149 + }
1.150 +
1.151 + /*
1.152 + private java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
1.153 +
1.154 + public void update(byte[] buf, int index, int len){
1.155 + if(buf==null) {crc32.reset();}
1.156 + else{crc32.update(buf, index, len);}
1.157 + }
1.158 + public void reset(){
1.159 + crc32.reset();
1.160 + }
1.161 + public void reset(long init){
1.162 + if(init==0L){
1.163 + crc32.reset();
1.164 + }
1.165 + else{
1.166 + System.err.println("unsupported operation");
1.167 + }
1.168 + }
1.169 + public long getValue(){
1.170 + return crc32.getValue();
1.171 + }
1.172 +*/
1.173 + public CRC32 copy(){
1.174 + CRC32 foo = new CRC32();
1.175 + foo.v = this.v;
1.176 + return foo;
1.177 + }
1.178 +
1.179 + public static int[] getCRC32Table(){
1.180 + int[] tmp = new int[crc_table.length];
1.181 + System.arraycopy(crc_table, 0, tmp, 0, tmp.length);
1.182 + return tmp;
1.183 + }
1.184 +}