1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/mini/src/main/java/java/util/zip/CRC32.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,132 @@
1.4 +/* CRC32.java - Computes CRC32 data checksum of a data stream
1.5 + Copyright (C) 1999. 2000, 2001 Free Software Foundation, Inc.
1.6 +
1.7 +This file is part of GNU Classpath.
1.8 +
1.9 +GNU Classpath is free software; you can redistribute it and/or modify
1.10 +it under the terms of the GNU General Public License as published by
1.11 +the Free Software Foundation; either version 2, or (at your option)
1.12 +any later version.
1.13 +
1.14 +GNU Classpath is distributed in the hope that it will be useful, but
1.15 +WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.17 +General Public License for more details.
1.18 +
1.19 +You should have received a copy of the GNU General Public License
1.20 +along with GNU Classpath; see the file COPYING. If not, write to the
1.21 +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1.22 +02111-1307 USA.
1.23 +
1.24 +Linking this library statically or dynamically with other modules is
1.25 +making a combined work based on this library. Thus, the terms and
1.26 +conditions of the GNU General Public License cover the whole
1.27 +combination.
1.28 +
1.29 +As a special exception, the copyright holders of this library give you
1.30 +permission to link this library with independent modules to produce an
1.31 +executable, regardless of the license terms of these independent
1.32 +modules, and to copy and distribute the resulting executable under
1.33 +terms of your choice, provided that you also meet, for each linked
1.34 +independent module, the terms and conditions of the license of that
1.35 +module. An independent module is a module which is not derived from
1.36 +or based on this library. If you modify this library, you may extend
1.37 +this exception to your version of the library, but you are not
1.38 +obligated to do so. If you do not wish to do so, delete this
1.39 +exception statement from your version. */
1.40 +
1.41 +package java.util.zip;
1.42 +
1.43 +/*
1.44 + * Written using on-line Java Platform 1.2 API Specification, as well
1.45 + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
1.46 + * The actual CRC32 algorithm is taken from RFC 1952.
1.47 + * Status: Believed complete and correct.
1.48 + */
1.49 +
1.50 +/**
1.51 + * Computes CRC32 data checksum of a data stream.
1.52 + * The actual CRC32 algorithm is described in RFC 1952
1.53 + * (GZIP file format specification version 4.3).
1.54 + * Can be used to get the CRC32 over a stream if used with checked input/output
1.55 + * streams.
1.56 + *
1.57 + * @see InflaterInputStream
1.58 + * @see DeflaterOutputStream
1.59 + *
1.60 + * @author Per Bothner
1.61 + * @date April 1, 1999.
1.62 + */
1.63 +public class CRC32 implements Checksum
1.64 +{
1.65 + /** The crc data checksum so far. */
1.66 + private int crc = 0;
1.67 +
1.68 + /** The fast CRC table. Computed once when the CRC32 class is loaded. */
1.69 + private static int[] crc_table = make_crc_table();
1.70 +
1.71 + /** Make the table for a fast CRC. */
1.72 + private static int[] make_crc_table ()
1.73 + {
1.74 + int[] crc_table = new int[256];
1.75 + for (int n = 0; n < 256; n++)
1.76 + {
1.77 + int c = n;
1.78 + for (int k = 8; --k >= 0; )
1.79 + {
1.80 + if ((c & 1) != 0)
1.81 + c = 0xedb88320 ^ (c >>> 1);
1.82 + else
1.83 + c = c >>> 1;
1.84 + }
1.85 + crc_table[n] = c;
1.86 + }
1.87 + return crc_table;
1.88 + }
1.89 +
1.90 + /**
1.91 + * Returns the CRC32 data checksum computed so far.
1.92 + */
1.93 + public long getValue ()
1.94 + {
1.95 + return (long) crc & 0xffffffffL;
1.96 + }
1.97 +
1.98 + /**
1.99 + * Resets the CRC32 data checksum as if no update was ever called.
1.100 + */
1.101 + public void reset () { crc = 0; }
1.102 +
1.103 + /**
1.104 + * Updates the checksum with the int bval.
1.105 + *
1.106 + * @param bval (the byte is taken as the lower 8 bits of bval)
1.107 + */
1.108 +
1.109 + public void update (int bval)
1.110 + {
1.111 + int c = ~crc;
1.112 + c = crc_table[(c ^ bval) & 0xff] ^ (c >>> 8);
1.113 + crc = ~c;
1.114 + }
1.115 +
1.116 + /**
1.117 + * Adds the byte array to the data checksum.
1.118 + *
1.119 + * @param buf the buffer which contains the data
1.120 + * @param off the offset in the buffer where the data starts
1.121 + * @param len the length of the data
1.122 + */
1.123 + public void update (byte[] buf, int off, int len)
1.124 + {
1.125 + int c = ~crc;
1.126 + while (--len >= 0)
1.127 + c = crc_table[(c ^ buf[off++]) & 0xff] ^ (c >>> 8);
1.128 + crc = ~c;
1.129 + }
1.130 +
1.131 + /**
1.132 + * Adds the complete byte array to the data checksum.
1.133 + */
1.134 + public void update (byte[] buf) { update(buf, 0, buf.length); }
1.135 +}