jaroslav@640: /* CRC32.java - Computes CRC32 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@609: jaroslav@609: package java.util.zip; jaroslav@609: 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 CRC32 algorithm is taken from RFC 1952. jaroslav@640: * Status: Believed complete and correct. jaroslav@640: */ jaroslav@640: jaroslav@609: /** jaroslav@640: * Computes CRC32 data checksum of a data stream. jaroslav@640: * The actual CRC32 algorithm is described in RFC 1952 jaroslav@640: * (GZIP file format specification version 4.3). jaroslav@640: * Can be used to get the CRC32 over a stream if used with checked input/output jaroslav@640: * streams. jaroslav@609: * jaroslav@640: * @see InflaterInputStream jaroslav@640: * @see DeflaterOutputStream jaroslav@640: * jaroslav@640: * @author Per Bothner jaroslav@640: * @date April 1, 1999. jaroslav@609: */ jaroslav@640: public class CRC32 implements Checksum jaroslav@640: { jaroslav@640: /** The crc data checksum so far. */ jaroslav@640: private int crc = 0; jaroslav@609: jaroslav@640: /** The fast CRC table. Computed once when the CRC32 class is loaded. */ jaroslav@640: private static int[] crc_table = make_crc_table(); jaroslav@609: jaroslav@640: /** Make the table for a fast CRC. */ jaroslav@640: private static int[] make_crc_table () jaroslav@640: { jaroslav@640: int[] crc_table = new int[256]; jaroslav@640: for (int n = 0; n < 256; n++) jaroslav@640: { jaroslav@640: int c = n; jaroslav@640: for (int k = 8; --k >= 0; ) jaroslav@640: { jaroslav@640: if ((c & 1) != 0) jaroslav@640: c = 0xedb88320 ^ (c >>> 1); jaroslav@640: else jaroslav@640: c = c >>> 1; jaroslav@640: } jaroslav@640: crc_table[n] = c; jaroslav@640: } jaroslav@640: return crc_table; jaroslav@640: } jaroslav@609: jaroslav@640: /** jaroslav@640: * Returns the CRC32 data checksum computed so far. jaroslav@640: */ jaroslav@640: public long getValue () jaroslav@640: { jaroslav@640: return (long) crc & 0xffffffffL; jaroslav@640: } jaroslav@609: jaroslav@640: /** jaroslav@640: * Resets the CRC32 data checksum as if no update was ever called. jaroslav@640: */ jaroslav@640: public void reset () { crc = 0; } jaroslav@609: jaroslav@640: /** jaroslav@640: * Updates the checksum with the int bval. jaroslav@640: * jaroslav@640: * @param bval (the byte is taken as the lower 8 bits of bval) jaroslav@640: */ jaroslav@609: jaroslav@640: public void update (int bval) jaroslav@640: { jaroslav@640: int c = ~crc; jaroslav@640: c = crc_table[(c ^ bval) & 0xff] ^ (c >>> 8); jaroslav@640: crc = ~c; jaroslav@640: } jaroslav@609: jaroslav@640: /** jaroslav@640: * Adds the byte array to the data checksum. jaroslav@640: * jaroslav@640: * @param buf the buffer which contains the data jaroslav@640: * @param off the offset in the buffer where the data starts jaroslav@640: * @param len the length of the data jaroslav@640: */ jaroslav@640: public void update (byte[] buf, int off, int len) jaroslav@640: { jaroslav@640: int c = ~crc; jaroslav@640: while (--len >= 0) jaroslav@640: c = crc_table[(c ^ buf[off++]) & 0xff] ^ (c >>> 8); jaroslav@640: crc = ~c; jaroslav@640: } jaroslav@609: jaroslav@640: /** jaroslav@640: * Adds the complete byte array to the data checksum. jaroslav@640: */ jaroslav@640: public void update (byte[] buf) { update(buf, 0, buf.length); } jaroslav@609: }