comparison src/com/jcraft/jzlib/Adler32.java @ 357:46c2115ae1c8

update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
author Carl Byington <carl@five-ten-sg.com>
date Fri, 01 Aug 2014 13:34:58 -0700
parents 0ce5cc452d02
children
comparison
equal deleted inserted replaced
356:5e91b559b5fe 357:46c2115ae1c8
1 /* -*-mode:java; c-basic-offset:2; -*- */ 1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /* 2 /*
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. 3 Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met: 6 modification, are permitted provided that the following conditions are met:
7 7
8 1. Redistributions of source code must retain the above copyright notice, 8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
10 10
11 2. Redistributions in binary form must reproduce the above copyright 11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in 12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution. 13 the documentation and/or other materials provided with the distribution.
14 14
15 3. The names of the authors may not be used to endorse or promote products 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. 16 derived from this software without specific prior written permission.
17 17
32 * and contributors of zlib. 32 * and contributors of zlib.
33 */ 33 */
34 34
35 package com.jcraft.jzlib; 35 package com.jcraft.jzlib;
36 36
37 final class Adler32 { 37 final public class Adler32 implements Checksum {
38 38
39 // largest prime smaller than 65536 39 // largest prime smaller than 65536
40 static final private int BASE = 65521; 40 static final private int BASE=65521;
41 // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 41 // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
42 static final private int NMAX = 5552; 42 static final private int NMAX=5552;
43 43
44 long adler32(long adler, byte[] buf, int index, int len) { 44 private long s1=1L;
45 if (buf == null) { return 1L; } 45 private long s2=0L;
46 46
47 long s1 = adler & 0xffff; 47 public void reset(long init){
48 long s2 = (adler >> 16) & 0xffff; 48 s1=init&0xffff;
49 int k; 49 s2=(init>>16)&0xffff;
50 }
50 51
51 while (len > 0) { 52 public void reset(){
52 k = len < NMAX ? len : NMAX; 53 s1=1L;
53 len -= k; 54 s2=0L;
55 }
54 56
55 while (k >= 16) { 57 public long getValue(){
56 s1 += buf[index++] & 0xff; s2 += s1; 58 return ((s2<<16)|s1);
57 s1 += buf[index++] & 0xff; s2 += s1; 59 }
58 s1 += buf[index++] & 0xff; s2 += s1;
59 s1 += buf[index++] & 0xff; s2 += s1;
60 s1 += buf[index++] & 0xff; s2 += s1;
61 s1 += buf[index++] & 0xff; s2 += s1;
62 s1 += buf[index++] & 0xff; s2 += s1;
63 s1 += buf[index++] & 0xff; s2 += s1;
64 s1 += buf[index++] & 0xff; s2 += s1;
65 s1 += buf[index++] & 0xff; s2 += s1;
66 s1 += buf[index++] & 0xff; s2 += s1;
67 s1 += buf[index++] & 0xff; s2 += s1;
68 s1 += buf[index++] & 0xff; s2 += s1;
69 s1 += buf[index++] & 0xff; s2 += s1;
70 s1 += buf[index++] & 0xff; s2 += s1;
71 s1 += buf[index++] & 0xff; s2 += s1;
72 k -= 16;
73 }
74 60
75 if (k != 0) { 61 public void update(byte[] buf, int index, int len){
76 do {
77 s1 += buf[index++] & 0xff; s2 += s1;
78 }
79 while (--k != 0);
80 }
81 62
82 s1 %= BASE; 63 if(len==1){
83 s2 %= BASE; 64 s1+=buf[index++]&0xff; s2+=s1;
84 } 65 s1%=BASE;
85 66 s2%=BASE;
86 return (s2 << 16) | s1; 67 return;
87 } 68 }
88 69
89 /* 70 int len1 = len/NMAX;
90 private java.util.zip.Adler32 adler=new java.util.zip.Adler32(); 71 int len2 = len%NMAX;
91 long adler32(long value, byte[] buf, int index, int len){ 72 while(len1-->0) {
92 if(value==1) {adler.reset();} 73 int k=NMAX;
93 if(buf==null) {adler.reset();} 74 len-=k;
94 else{adler.update(buf, index, len);} 75 while(k-->0){
95 return adler.getValue(); 76 s1+=buf[index++]&0xff; s2+=s1;
77 }
78 s1%=BASE;
79 s2%=BASE;
96 } 80 }
97 */ 81
82 int k=len2;
83 len-=k;
84 while(k-->0){
85 s1+=buf[index++]&0xff; s2+=s1;
86 }
87 s1%=BASE;
88 s2%=BASE;
89 }
90
91 public Adler32 copy(){
92 Adler32 foo = new Adler32();
93 foo.s1 = this.s1;
94 foo.s2 = this.s2;
95 return foo;
96 }
97
98 // The following logic has come from zlib.1.2.
99 static long combine(long adler1, long adler2, long len2){
100 long BASEL = (long)BASE;
101 long sum1;
102 long sum2;
103 long rem; // unsigned int
104
105 rem = len2 % BASEL;
106 sum1 = adler1 & 0xffffL;
107 sum2 = rem * sum1;
108 sum2 %= BASEL; // MOD(sum2);
109 sum1 += (adler2 & 0xffffL) + BASEL - 1;
110 sum2 += ((adler1 >> 16) & 0xffffL) + ((adler2 >> 16) & 0xffffL) + BASEL - rem;
111 if (sum1 >= BASEL) sum1 -= BASEL;
112 if (sum1 >= BASEL) sum1 -= BASEL;
113 if (sum2 >= (BASEL << 1)) sum2 -= (BASEL << 1);
114 if (sum2 >= BASEL) sum2 -= BASEL;
115 return sum1 | (sum2 << 16);
116 }
117
118 /*
119 private java.util.zip.Adler32 adler=new java.util.zip.Adler32();
120 public void update(byte[] buf, int index, int len){
121 if(buf==null) {adler.reset();}
122 else{adler.update(buf, index, len);}
123 }
124 public void reset(){
125 adler.reset();
126 }
127 public void reset(long init){
128 if(init==1L){
129 adler.reset();
130 }
131 else{
132 System.err.println("unsupported operation");
133 }
134 }
135 public long getValue(){
136 return adler.getValue();
137 }
138 */
98 } 139 }