jaroslav@694: /* -*-mode:java; c-basic-offset:2; -*- */ jaroslav@694: /* jaroslav@694: Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved. jaroslav@694: jaroslav@694: Redistribution and use in source and binary forms, with or without jaroslav@694: modification, are permitted provided that the following conditions are met: jaroslav@694: jaroslav@694: 1. Redistributions of source code must retain the above copyright notice, jaroslav@694: this list of conditions and the following disclaimer. jaroslav@694: jaroslav@694: 2. Redistributions in binary form must reproduce the above copyright jaroslav@694: notice, this list of conditions and the following disclaimer in jaroslav@694: the documentation and/or other materials provided with the distribution. jaroslav@694: jaroslav@694: 3. The names of the authors may not be used to endorse or promote products jaroslav@694: derived from this software without specific prior written permission. jaroslav@694: jaroslav@694: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, jaroslav@694: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jaroslav@694: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, jaroslav@694: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, jaroslav@694: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT jaroslav@694: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, jaroslav@694: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF jaroslav@694: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING jaroslav@694: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, jaroslav@694: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jaroslav@694: */ jaroslav@694: /* jaroslav@694: * This program is based on zlib-1.1.3, so all credit should go authors jaroslav@694: * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu) jaroslav@694: * and contributors of zlib. jaroslav@694: */ jaroslav@694: jaroslav@694: package org.apidesign.bck2brwsr.emul.zip; jaroslav@694: jaroslav@694: final class Adler32 implements Checksum { jaroslav@694: jaroslav@694: // largest prime smaller than 65536 jaroslav@694: static final private int BASE=65521; jaroslav@694: // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 jaroslav@694: static final private int NMAX=5552; jaroslav@694: jaroslav@694: private long s1=1L; jaroslav@694: private long s2=0L; jaroslav@694: jaroslav@694: public void reset(long init){ jaroslav@694: s1=init&0xffff; jaroslav@694: s2=(init>>16)&0xffff; jaroslav@694: } jaroslav@694: jaroslav@694: public void reset(){ jaroslav@694: s1=1L; jaroslav@694: s2=0L; jaroslav@694: } jaroslav@694: jaroslav@694: public long getValue(){ jaroslav@694: return ((s2<<16)|s1); jaroslav@694: } jaroslav@694: jaroslav@694: public void update(byte[] buf, int index, int len){ jaroslav@694: jaroslav@694: if(len==1){ jaroslav@694: s1+=buf[index++]&0xff; s2+=s1; jaroslav@694: s1%=BASE; jaroslav@694: s2%=BASE; jaroslav@694: return; jaroslav@694: } jaroslav@694: jaroslav@694: int len1 = len/NMAX; jaroslav@694: int len2 = len%NMAX; jaroslav@694: while(len1-->0) { jaroslav@694: int k=NMAX; jaroslav@694: len-=k; jaroslav@694: while(k-->0){ jaroslav@694: s1+=buf[index++]&0xff; s2+=s1; jaroslav@694: } jaroslav@694: s1%=BASE; jaroslav@694: s2%=BASE; jaroslav@694: } jaroslav@694: jaroslav@694: int k=len2; jaroslav@694: len-=k; jaroslav@694: while(k-->0){ jaroslav@694: s1+=buf[index++]&0xff; s2+=s1; jaroslav@694: } jaroslav@694: s1%=BASE; jaroslav@694: s2%=BASE; jaroslav@694: } jaroslav@694: jaroslav@694: public Adler32 copy(){ jaroslav@694: Adler32 foo = new Adler32(); jaroslav@694: foo.s1 = this.s1; jaroslav@694: foo.s2 = this.s2; jaroslav@694: return foo; jaroslav@694: } jaroslav@694: jaroslav@694: // The following logic has come from zlib.1.2. jaroslav@694: static long combine(long adler1, long adler2, long len2){ jaroslav@694: long BASEL = (long)BASE; jaroslav@694: long sum1; jaroslav@694: long sum2; jaroslav@694: long rem; // unsigned int jaroslav@694: jaroslav@694: rem = len2 % BASEL; jaroslav@694: sum1 = adler1 & 0xffffL; jaroslav@694: sum2 = rem * sum1; jaroslav@694: sum2 %= BASEL; // MOD(sum2); jaroslav@694: sum1 += (adler2 & 0xffffL) + BASEL - 1; jaroslav@694: sum2 += ((adler1 >> 16) & 0xffffL) + ((adler2 >> 16) & 0xffffL) + BASEL - rem; jaroslav@694: if (sum1 >= BASEL) sum1 -= BASEL; jaroslav@694: if (sum1 >= BASEL) sum1 -= BASEL; jaroslav@694: if (sum2 >= (BASEL << 1)) sum2 -= (BASEL << 1); jaroslav@694: if (sum2 >= BASEL) sum2 -= BASEL; jaroslav@694: return sum1 | (sum2 << 16); jaroslav@694: } jaroslav@694: jaroslav@694: /* jaroslav@694: private java.util.zip.Adler32 adler=new java.util.zip.Adler32(); jaroslav@694: public void update(byte[] buf, int index, int len){ jaroslav@694: if(buf==null) {adler.reset();} jaroslav@694: else{adler.update(buf, index, len);} jaroslav@694: } jaroslav@694: public void reset(){ jaroslav@694: adler.reset(); jaroslav@694: } jaroslav@694: public void reset(long init){ jaroslav@694: if(init==1L){ jaroslav@694: adler.reset(); jaroslav@694: } jaroslav@694: else{ jaroslav@694: System.err.println("unsupported operation"); jaroslav@694: } jaroslav@694: } jaroslav@694: public long getValue(){ jaroslav@694: return adler.getValue(); jaroslav@694: } jaroslav@694: */ jaroslav@694: }