Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/Adler32.java
8650 views
/* -*-mode:java; c-basic-offset:2; -*- */1/*2Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28/*29* This program is based on zlib-1.1.3, so all credit should go authors30* Jean-loup Gailly([email protected]) and Mark Adler([email protected])31* and contributors of zlib.32*/3334package com.jcraft.jzlib;3536final public class Adler32 implements Checksum {3738// largest prime smaller than 6553639static final private int BASE=65521;40// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-141static final private int NMAX=5552;4243private long s1=1L;44private long s2=0L;4546public void reset(long init){47s1=init&0xffff;48s2=(init>>16)&0xffff;49}5051public void reset(){52s1=1L;53s2=0L;54}5556public long getValue(){57return ((s2<<16)|s1);58}5960public void update(byte[] buf, int index, int len){6162if(len==1){63s1+=buf[index++]&0xff; s2+=s1;64s1%=BASE;65s2%=BASE;66return;67}6869int len1 = len/NMAX;70int len2 = len%NMAX;71while(len1-->0) {72int k=NMAX;73len-=k;74while(k-->0){75s1+=buf[index++]&0xff; s2+=s1;76}77s1%=BASE;78s2%=BASE;79}8081int k=len2;82len-=k;83while(k-->0){84s1+=buf[index++]&0xff; s2+=s1;85}86s1%=BASE;87s2%=BASE;88}8990public Adler32 copy(){91Adler32 foo = new Adler32();92foo.s1 = this.s1;93foo.s2 = this.s2;94return foo;95}9697// The following logic has come from zlib.1.2.98static long combine(long adler1, long adler2, long len2){99long BASEL = (long)BASE;100long sum1;101long sum2;102long rem; // unsigned int103104rem = len2 % BASEL;105sum1 = adler1 & 0xffffL;106sum2 = rem * sum1;107sum2 %= BASEL; // MOD(sum2);108sum1 += (adler2 & 0xffffL) + BASEL - 1;109sum2 += ((adler1 >> 16) & 0xffffL) + ((adler2 >> 16) & 0xffffL) + BASEL - rem;110if (sum1 >= BASEL) sum1 -= BASEL;111if (sum1 >= BASEL) sum1 -= BASEL;112if (sum2 >= (BASEL << 1)) sum2 -= (BASEL << 1);113if (sum2 >= BASEL) sum2 -= BASEL;114return sum1 | (sum2 << 16);115}116117}118119120