jaroslav@640: /* Adler32.java - Computes Adler32 data checksum of a data stream jaroslav@640: Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. jaroslav@640: jaroslav@640: This file is part of GNU Classpath. jaroslav@640: jaroslav@640: GNU Classpath is free software; you can redistribute it and/or modify jaroslav@640: it under the terms of the GNU General Public License as published by jaroslav@640: the Free Software Foundation; either version 2, or (at your option) jaroslav@640: any later version. jaroslav@640: jaroslav@640: GNU Classpath is distributed in the hope that it will be useful, but jaroslav@640: WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@640: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU jaroslav@640: General Public License for more details. jaroslav@640: jaroslav@640: You should have received a copy of the GNU General Public License jaroslav@640: along with GNU Classpath; see the file COPYING. If not, write to the jaroslav@640: Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA jaroslav@640: 02111-1307 USA. jaroslav@640: jaroslav@640: Linking this library statically or dynamically with other modules is jaroslav@640: making a combined work based on this library. Thus, the terms and jaroslav@640: conditions of the GNU General Public License cover the whole jaroslav@640: combination. jaroslav@640: jaroslav@640: As a special exception, the copyright holders of this library give you jaroslav@640: permission to link this library with independent modules to produce an jaroslav@640: executable, regardless of the license terms of these independent jaroslav@640: modules, and to copy and distribute the resulting executable under jaroslav@640: terms of your choice, provided that you also meet, for each linked jaroslav@640: independent module, the terms and conditions of the license of that jaroslav@640: module. An independent module is a module which is not derived from jaroslav@640: or based on this library. If you modify this library, you may extend jaroslav@640: this exception to your version of the library, but you are not jaroslav@640: obligated to do so. If you do not wish to do so, delete this jaroslav@640: exception statement from your version. */ jaroslav@640: jaroslav@640: package java.util.zip; jaroslav@640: jaroslav@640: /* jaroslav@640: * Written using on-line Java Platform 1.2 API Specification, as well jaroslav@640: * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). jaroslav@640: * The actual Adler32 algorithm is taken from RFC 1950. jaroslav@640: * Status: Believed complete and correct. jaroslav@640: */ jaroslav@640: jaroslav@640: /** jaroslav@640: * Computes Adler32 checksum for a stream of data. An Adler32 jaroslav@640: * checksum is not as reliable as a CRC32 checksum, but a lot faster to jaroslav@640: * compute. jaroslav@640: *
jaroslav@640: * The specification for Adler32 may be found in RFC 1950. jaroslav@640: * (ZLIB Compressed Data Format Specification version 3.3) jaroslav@640: *
jaroslav@640: *
jaroslav@640: * From that document: jaroslav@640: *
jaroslav@640: * "ADLER32 (Adler-32 checksum) jaroslav@640: * This contains a checksum value of the uncompressed data jaroslav@640: * (excluding any dictionary data) computed according to Adler-32 jaroslav@640: * algorithm. This algorithm is a 32-bit extension and improvement jaroslav@640: * of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073 jaroslav@640: * standard. jaroslav@640: *
jaroslav@640: * Adler-32 is composed of two sums accumulated per byte: s1 is jaroslav@640: * the sum of all bytes, s2 is the sum of all s1 values. Both sums jaroslav@640: * are done modulo 65521. s1 is initialized to 1, s2 to zero. The jaroslav@640: * Adler-32 checksum is stored as s2*65536 + s1 in most- jaroslav@640: * significant-byte first (network) order." jaroslav@640: *
jaroslav@640: * "8.2. The Adler-32 algorithm jaroslav@640: *
jaroslav@640: * The Adler-32 algorithm is much faster than the CRC32 algorithm yet jaroslav@640: * still provides an extremely low probability of undetected errors. jaroslav@640: *
jaroslav@640: * The modulo on unsigned long accumulators can be delayed for 5552 jaroslav@640: * bytes, so the modulo operation time is negligible. If the bytes jaroslav@640: * are a, b, c, the second sum is 3a + 2b + c + 3, and so is position jaroslav@640: * and order sensitive, unlike the first sum, which is just a jaroslav@640: * checksum. That 65521 is prime is important to avoid a possible jaroslav@640: * large class of two-byte errors that leave the check unchanged. jaroslav@640: * (The Fletcher checksum uses 255, which is not prime and which also jaroslav@640: * makes the Fletcher check insensitive to single byte changes 0 <-> jaroslav@640: * 255.) jaroslav@640: *
jaroslav@640: * The sum s1 is initialized to 1 instead of zero to make the length
jaroslav@640: * of the sequence part of s2, so that the length does not have to be
jaroslav@640: * checked separately. (Any sequence of zeroes has a Fletcher
jaroslav@640: * checksum of zero.)"
jaroslav@640: *
jaroslav@640: * @author John Leuner, Per Bothner
jaroslav@640: * @since JDK 1.1
jaroslav@640: *
jaroslav@640: * @see InflaterInputStream
jaroslav@640: * @see DeflaterOutputStream
jaroslav@640: */
jaroslav@640: public class Adler32 implements Checksum
jaroslav@640: {
jaroslav@640:
jaroslav@640: /** largest prime smaller than 65536 */
jaroslav@640: private static final int BASE = 65521;
jaroslav@640:
jaroslav@640: private int checksum; //we do all in int.
jaroslav@640:
jaroslav@640: //Note that java doesn't have unsigned integers,
jaroslav@640: //so we have to be careful with what arithmetic
jaroslav@640: //we do. We return the checksum as a long to
jaroslav@640: //avoid sign confusion.
jaroslav@640:
jaroslav@640: /**
jaroslav@640: * Creates a new instance of the Adler32
class.
jaroslav@640: * The checksum starts off with a value of 1.
jaroslav@640: */
jaroslav@640: public Adler32 ()
jaroslav@640: {
jaroslav@640: reset();
jaroslav@640: }
jaroslav@640:
jaroslav@640: /**
jaroslav@640: * Resets the Adler32 checksum to the initial value.
jaroslav@640: */
jaroslav@640: public void reset ()
jaroslav@640: {
jaroslav@640: checksum = 1; //Initialize to 1
jaroslav@640: }
jaroslav@640:
jaroslav@640: /**
jaroslav@640: * Updates the checksum with the byte b.
jaroslav@640: *
jaroslav@640: * @param bval the data value to add. The high byte of the int is ignored.
jaroslav@640: */
jaroslav@640: public void update (int bval)
jaroslav@640: {
jaroslav@640: //We could make a length 1 byte array and call update again, but I
jaroslav@640: //would rather not have that overhead
jaroslav@640: int s1 = checksum & 0xffff;
jaroslav@640: int s2 = checksum >>> 16;
jaroslav@640:
jaroslav@640: s1 = (s1 + (bval & 0xFF)) % BASE;
jaroslav@640: s2 = (s1 + s2) % BASE;
jaroslav@640:
jaroslav@640: checksum = (s2 << 16) + s1;
jaroslav@640: }
jaroslav@640:
jaroslav@640: /**
jaroslav@640: * Updates the checksum with the bytes taken from the array.
jaroslav@640: *
jaroslav@640: * @param buffer an array of bytes
jaroslav@640: */
jaroslav@640: public void update (byte[] buffer)
jaroslav@640: {
jaroslav@640: update(buffer, 0, buffer.length);
jaroslav@640: }
jaroslav@640:
jaroslav@640: /**
jaroslav@640: * Updates the checksum with the bytes taken from the array.
jaroslav@640: *
jaroslav@640: * @param buf an array of bytes
jaroslav@640: * @param off the start of the data used for this update
jaroslav@640: * @param len the number of bytes to use for this update
jaroslav@640: */
jaroslav@640: public void update (byte[] buf, int off, int len)
jaroslav@640: {
jaroslav@640: //(By Per Bothner)
jaroslav@640: int s1 = checksum & 0xffff;
jaroslav@640: int s2 = checksum >>> 16;
jaroslav@640:
jaroslav@640: while (len > 0)
jaroslav@640: {
jaroslav@640: // We can defer the modulo operation:
jaroslav@640: // s1 maximally grows from 65521 to 65521 + 255 * 3800
jaroslav@640: // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
jaroslav@640: int n = 3800;
jaroslav@640: if (n > len)
jaroslav@640: n = len;
jaroslav@640: len -= n;
jaroslav@640: while (--n >= 0)
jaroslav@640: {
jaroslav@640: s1 = s1 + (buf[off++] & 0xFF);
jaroslav@640: s2 = s2 + s1;
jaroslav@640: }
jaroslav@640: s1 %= BASE;
jaroslav@640: s2 %= BASE;
jaroslav@640: }
jaroslav@640:
jaroslav@640: /*Old implementation, borrowed from somewhere:
jaroslav@640: int n;
jaroslav@640:
jaroslav@640: while (len-- > 0) {
jaroslav@640:
jaroslav@640: s1 = (s1 + (bs[offset++] & 0xff)) % BASE;
jaroslav@640: s2 = (s2 + s1) % BASE;
jaroslav@640: }*/
jaroslav@640:
jaroslav@640: checksum = (s2 << 16) | s1;
jaroslav@640: }
jaroslav@640:
jaroslav@640: /**
jaroslav@640: * Returns the Adler32 data checksum computed so far.
jaroslav@640: */
jaroslav@640: public long getValue()
jaroslav@640: {
jaroslav@640: return (long) checksum & 0xffffffffL;
jaroslav@640: }
jaroslav@640: }