001 /* Adler32.java - Computes Adler32 data checksum of a data stream
002 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package java.util.zip;
039
040 /*
041 * Written using on-line Java Platform 1.2 API Specification, as well
042 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
043 * The actual Adler32 algorithm is taken from RFC 1950.
044 * Status: Believed complete and correct.
045 */
046
047 /**
048 * Computes Adler32 checksum for a stream of data. An Adler32
049 * checksum is not as reliable as a CRC32 checksum, but a lot faster to
050 * compute.
051 *<p>
052 * The specification for Adler32 may be found in RFC 1950.
053 * (ZLIB Compressed Data Format Specification version 3.3)
054 *<p>
055 *<p>
056 * From that document:
057 *<p>
058 * "ADLER32 (Adler-32 checksum)
059 * This contains a checksum value of the uncompressed data
060 * (excluding any dictionary data) computed according to Adler-32
061 * algorithm. This algorithm is a 32-bit extension and improvement
062 * of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
063 * standard.
064 *<p>
065 * Adler-32 is composed of two sums accumulated per byte: s1 is
066 * the sum of all bytes, s2 is the sum of all s1 values. Both sums
067 * are done modulo 65521. s1 is initialized to 1, s2 to zero. The
068 * Adler-32 checksum is stored as s2*65536 + s1 in most-
069 * significant-byte first (network) order."
070 *<p>
071 * "8.2. The Adler-32 algorithm
072 *<p>
073 * The Adler-32 algorithm is much faster than the CRC32 algorithm yet
074 * still provides an extremely low probability of undetected errors.
075 *<p>
076 * The modulo on unsigned long accumulators can be delayed for 5552
077 * bytes, so the modulo operation time is negligible. If the bytes
078 * are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
079 * and order sensitive, unlike the first sum, which is just a
080 * checksum. That 65521 is prime is important to avoid a possible
081 * large class of two-byte errors that leave the check unchanged.
082 * (The Fletcher checksum uses 255, which is not prime and which also
083 * makes the Fletcher check insensitive to single byte changes 0 <->
084 * 255.)
085 *<p>
086 * The sum s1 is initialized to 1 instead of zero to make the length
087 * of the sequence part of s2, so that the length does not have to be
088 * checked separately. (Any sequence of zeroes has a Fletcher
089 * checksum of zero.)"
090 *
091 * @author John Leuner, Per Bothner
092 * @since JDK 1.1
093 *
094 * @see InflaterInputStream
095 * @see DeflaterOutputStream
096 */
097 public class Adler32 implements Checksum
098 {
099
100 /** largest prime smaller than 65536 */
101 private static final int BASE = 65521;
102
103 private int checksum; //we do all in int.
104
105 //Note that java doesn't have unsigned integers,
106 //so we have to be careful with what arithmetic
107 //we do. We return the checksum as a long to
108 //avoid sign confusion.
109
110 /**
111 * Creates a new instance of the <code>Adler32</code> class.
112 * The checksum starts off with a value of 1.
113 */
114 public Adler32 ()
115 {
116 reset();
117 }
118
119 /**
120 * Resets the Adler32 checksum to the initial value.
121 */
122 public void reset ()
123 {
124 checksum = 1; //Initialize to 1
125 }
126
127 /**
128 * Updates the checksum with the byte b.
129 *
130 * @param bval the data value to add. The high byte of the int is ignored.
131 */
132 public void update (int bval)
133 {
134 //We could make a length 1 byte array and call update again, but I
135 //would rather not have that overhead
136 int s1 = checksum & 0xffff;
137 int s2 = checksum >>> 16;
138
139 s1 = (s1 + (bval & 0xFF)) % BASE;
140 s2 = (s1 + s2) % BASE;
141
142 checksum = (s2 << 16) + s1;
143 }
144
145 /**
146 * Updates the checksum with the bytes taken from the array.
147 *
148 * @param buffer an array of bytes
149 */
150 public void update (byte[] buffer)
151 {
152 update(buffer, 0, buffer.length);
153 }
154
155 /**
156 * Updates the checksum with the bytes taken from the array.
157 *
158 * @param buf an array of bytes
159 * @param off the start of the data used for this update
160 * @param len the number of bytes to use for this update
161 */
162 public void update (byte[] buf, int off, int len)
163 {
164 //(By Per Bothner)
165 int s1 = checksum & 0xffff;
166 int s2 = checksum >>> 16;
167
168 while (len > 0)
169 {
170 // We can defer the modulo operation:
171 // s1 maximally grows from 65521 to 65521 + 255 * 3800
172 // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
173 int n = 3800;
174 if (n > len)
175 n = len;
176 len -= n;
177 while (--n >= 0)
178 {
179 s1 = s1 + (buf[off++] & 0xFF);
180 s2 = s2 + s1;
181 }
182 s1 %= BASE;
183 s2 %= BASE;
184 }
185
186 /*Old implementation, borrowed from somewhere:
187 int n;
188
189 while (len-- > 0) {
190
191 s1 = (s1 + (bs[offset++] & 0xff)) % BASE;
192 s2 = (s2 + s1) % BASE;
193 }*/
194
195 checksum = (s2 << 16) | s1;
196 }
197
198 /**
199 * Returns the Adler32 data checksum computed so far.
200 */
201 public long getValue()
202 {
203 return (long) checksum & 0xffffffffL;
204 }
205 }