rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/Adler32.java
changeset 772 d382dacfd73f
parent 694 0d277415ed02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/Adler32.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* -*-mode:java; c-basic-offset:2; -*- */
     1.5 +/*
     1.6 +Copyright (c) 2000-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 +final class Adler32 implements Checksum {
    1.41 +
    1.42 +  // largest prime smaller than 65536
    1.43 +  static final private int BASE=65521; 
    1.44 +  // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
    1.45 +  static final private int NMAX=5552;
    1.46 +
    1.47 +  private long s1=1L;
    1.48 +  private long s2=0L;
    1.49 +
    1.50 +  public void reset(long init){
    1.51 +    s1=init&0xffff;
    1.52 +    s2=(init>>16)&0xffff;
    1.53 +  }
    1.54 +
    1.55 +  public void reset(){
    1.56 +    s1=1L;
    1.57 +    s2=0L;
    1.58 +  }
    1.59 +
    1.60 +  public long getValue(){
    1.61 +    return ((s2<<16)|s1);
    1.62 +  }
    1.63 +
    1.64 +  public void update(byte[] buf, int index, int len){
    1.65 +
    1.66 +    if(len==1){
    1.67 +      s1+=buf[index++]&0xff; s2+=s1;
    1.68 +      s1%=BASE;
    1.69 +      s2%=BASE;
    1.70 +      return;
    1.71 +    }
    1.72 +
    1.73 +    int len1 = len/NMAX;
    1.74 +    int len2 = len%NMAX;
    1.75 +    while(len1-->0) {
    1.76 +      int k=NMAX;
    1.77 +      len-=k;
    1.78 +      while(k-->0){
    1.79 +	s1+=buf[index++]&0xff; s2+=s1;
    1.80 +      }
    1.81 +      s1%=BASE;
    1.82 +      s2%=BASE;
    1.83 +    }
    1.84 +
    1.85 +    int k=len2;
    1.86 +    len-=k;
    1.87 +    while(k-->0){
    1.88 +      s1+=buf[index++]&0xff; s2+=s1;
    1.89 +    }
    1.90 +    s1%=BASE;
    1.91 +    s2%=BASE;
    1.92 +  }
    1.93 +
    1.94 +  public Adler32 copy(){
    1.95 +    Adler32 foo = new Adler32();
    1.96 +    foo.s1 = this.s1;
    1.97 +    foo.s2 = this.s2;
    1.98 +    return foo;
    1.99 +  }
   1.100 +
   1.101 +  // The following logic has come from zlib.1.2.
   1.102 +  static long combine(long adler1, long adler2, long len2){
   1.103 +    long BASEL = (long)BASE;
   1.104 +    long sum1;
   1.105 +    long sum2;
   1.106 +    long rem;  // unsigned int
   1.107 +
   1.108 +    rem = len2 % BASEL;
   1.109 +    sum1 = adler1 & 0xffffL;
   1.110 +    sum2 = rem * sum1;
   1.111 +    sum2 %= BASEL; // MOD(sum2);
   1.112 +    sum1 += (adler2 & 0xffffL) + BASEL - 1;
   1.113 +    sum2 += ((adler1 >> 16) & 0xffffL) + ((adler2 >> 16) & 0xffffL) + BASEL - rem;
   1.114 +    if (sum1 >= BASEL) sum1 -= BASEL;
   1.115 +    if (sum1 >= BASEL) sum1 -= BASEL;
   1.116 +    if (sum2 >= (BASEL << 1)) sum2 -= (BASEL << 1);
   1.117 +    if (sum2 >= BASEL) sum2 -= BASEL;
   1.118 +    return sum1 | (sum2 << 16);
   1.119 +  }
   1.120 +
   1.121 +/*
   1.122 +  private java.util.zip.Adler32 adler=new java.util.zip.Adler32();
   1.123 +  public void update(byte[] buf, int index, int len){
   1.124 +    if(buf==null) {adler.reset();}
   1.125 +    else{adler.update(buf, index, len);}
   1.126 +  }
   1.127 +  public void reset(){
   1.128 +    adler.reset();
   1.129 +  }
   1.130 +  public void reset(long init){
   1.131 +    if(init==1L){
   1.132 +      adler.reset();
   1.133 +    }
   1.134 +    else{
   1.135 +      System.err.println("unsupported operation");
   1.136 +    }
   1.137 +  }
   1.138 +  public long getValue(){
   1.139 +    return adler.getValue();
   1.140 +  }
   1.141 +*/
   1.142 +}