rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/CRC32.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 694 emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/CRC32.java@0d277415ed02
permissions -rw-r--r--
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; -*- */
     2 /*
     3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
     4 
     5 Redistribution and use in source and binary forms, with or without
     6 modification, are permitted provided that the following conditions are met:
     7 
     8   1. Redistributions of source code must retain the above copyright notice,
     9      this list of conditions and the following disclaimer.
    10 
    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.
    14 
    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.
    17 
    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.
    28  */
    29 /*
    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.
    33  */
    34 
    35 package org.apidesign.bck2brwsr.emul.zip;
    36 
    37 import org.apidesign.bck2brwsr.emul.lang.System;
    38 
    39 final class CRC32 implements Checksum {
    40 
    41   /*
    42    *  The following logic has come from RFC1952.
    43    */
    44   private int v = 0;
    45   private static int[] crc_table = null;
    46   static {
    47     crc_table = new int[256];
    48     for (int n = 0; n < 256; n++) {
    49       int c = n;
    50       for (int k = 8;  --k >= 0; ) {
    51         if ((c & 1) != 0)
    52 	  c = 0xedb88320 ^ (c >>> 1);
    53         else
    54           c = c >>> 1;
    55       }
    56       crc_table[n] = c;
    57     }
    58   }
    59 
    60   public void update (byte[] buf, int index, int len) {
    61     int c = ~v;
    62     while (--len >= 0)
    63       c = crc_table[(c^buf[index++])&0xff]^(c >>> 8);
    64     v = ~c;
    65   }
    66 
    67   public void reset(){
    68     v = 0;
    69   }
    70 
    71   public void reset(long vv){
    72     v = (int)(vv&0xffffffffL);
    73   }
    74 
    75   public long getValue(){
    76     return (long)(v&0xffffffffL);
    77   }
    78 
    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){
    82     long row;
    83     long[] even = new long[GF2_DIM];
    84     long[] odd = new long[GF2_DIM];
    85 
    86     // degenerate case (also disallow negative lengths)
    87     if (len2 <= 0)
    88       return crc1;
    89 
    90     // put operator for one zero bit in odd
    91     odd[0] = 0xedb88320L;          // CRC-32 polynomial
    92     row = 1;
    93     for (int n = 1; n < GF2_DIM; n++) {
    94         odd[n] = row;
    95         row <<= 1;
    96     }
    97 
    98     // put operator for two zero bits in even
    99     gf2_matrix_square(even, odd);
   100 
   101     // put operator for four zero bits in odd
   102     gf2_matrix_square(odd, even);
   103 
   104     // apply len2 zeros to crc1 (first square will put the operator for one
   105     // zero byte, eight zero bits, in even)
   106     do {
   107       // apply zeros operator for this bit of len2
   108       gf2_matrix_square(even, odd);
   109       if ((len2 & 1)!=0)
   110         crc1 = gf2_matrix_times(even, crc1);
   111       len2 >>= 1;
   112 
   113       // if no more bits set, then done
   114       if (len2 == 0)
   115         break;
   116 
   117       // another iteration of the loop with odd and even swapped
   118       gf2_matrix_square(odd, even);
   119       if ((len2 & 1)!=0)
   120         crc1 = gf2_matrix_times(odd, crc1);
   121       len2 >>= 1;
   122 
   123       // if no more bits set, then done
   124     } while (len2 != 0);
   125 
   126     /* return combined crc */
   127     crc1 ^= crc2;
   128     return crc1;
   129   }
   130 
   131   private static long gf2_matrix_times(long[] mat, long vec){
   132     long sum = 0;
   133     int index = 0;
   134     while (vec!=0) {
   135       if ((vec & 1)!=0)
   136         sum ^= mat[index];
   137       vec >>= 1;
   138       index++;
   139     }
   140     return sum;
   141   }
   142 
   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]);
   146   }
   147 
   148   /*
   149   private java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
   150 
   151   public void update(byte[] buf, int index, int len){
   152     if(buf==null) {crc32.reset();}
   153     else{crc32.update(buf, index, len);}
   154   }
   155   public void reset(){
   156     crc32.reset();
   157   }
   158   public void reset(long init){
   159     if(init==0L){
   160       crc32.reset();
   161     }
   162     else{
   163       System.err.println("unsupported operation");
   164     }
   165   }
   166   public long getValue(){
   167     return crc32.getValue();
   168   }
   169 */
   170   public CRC32 copy(){
   171     CRC32 foo = new CRC32();
   172     foo.v = this.v;
   173     return foo;
   174   }
   175 
   176   public static int[] getCRC32Table(){
   177     int[] tmp = new int[crc_table.length];
   178     System.arraycopy(crc_table, 0, tmp, 0, tmp.length);
   179     return tmp;
   180   }
   181 }