emul/mini/src/main/java/java/util/zip/CRC32.java
branchemul
changeset 640 693745d01b55
parent 611 9839e9a75bcf
     1.1 --- a/emul/mini/src/main/java/java/util/zip/CRC32.java	Wed Jan 30 14:03:49 2013 +0100
     1.2 +++ b/emul/mini/src/main/java/java/util/zip/CRC32.java	Fri Feb 01 18:02:16 2013 +0100
     1.3 @@ -1,113 +1,132 @@
     1.4 -/*
     1.5 - * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.  Oracle designates this
    1.11 - * particular file as subject to the "Classpath" exception as provided
    1.12 - * by Oracle in the LICENSE file that accompanied this code.
    1.13 - *
    1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 - * version 2 for more details (a copy is included in the LICENSE file that
    1.18 - * accompanied this code).
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License version
    1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 - *
    1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 - * or visit www.oracle.com if you need additional information or have any
    1.26 - * questions.
    1.27 - */
    1.28 +/* CRC32.java - Computes CRC32 data checksum of a data stream
    1.29 +   Copyright (C) 1999. 2000, 2001 Free Software Foundation, Inc.
    1.30 +
    1.31 +This file is part of GNU Classpath.
    1.32 +
    1.33 +GNU Classpath is free software; you can redistribute it and/or modify
    1.34 +it under the terms of the GNU General Public License as published by
    1.35 +the Free Software Foundation; either version 2, or (at your option)
    1.36 +any later version.
    1.37 +
    1.38 +GNU Classpath is distributed in the hope that it will be useful, but
    1.39 +WITHOUT ANY WARRANTY; without even the implied warranty of
    1.40 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.41 +General Public License for more details.
    1.42 +
    1.43 +You should have received a copy of the GNU General Public License
    1.44 +along with GNU Classpath; see the file COPYING.  If not, write to the
    1.45 +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    1.46 +02111-1307 USA.
    1.47 +
    1.48 +Linking this library statically or dynamically with other modules is
    1.49 +making a combined work based on this library.  Thus, the terms and
    1.50 +conditions of the GNU General Public License cover the whole
    1.51 +combination.
    1.52 +
    1.53 +As a special exception, the copyright holders of this library give you
    1.54 +permission to link this library with independent modules to produce an
    1.55 +executable, regardless of the license terms of these independent
    1.56 +modules, and to copy and distribute the resulting executable under
    1.57 +terms of your choice, provided that you also meet, for each linked
    1.58 +independent module, the terms and conditions of the license of that
    1.59 +module.  An independent module is a module which is not derived from
    1.60 +or based on this library.  If you modify this library, you may extend
    1.61 +this exception to your version of the library, but you are not
    1.62 +obligated to do so.  If you do not wish to do so, delete this
    1.63 +exception statement from your version. */
    1.64  
    1.65  package java.util.zip;
    1.66  
    1.67 +/*
    1.68 + * Written using on-line Java Platform 1.2 API Specification, as well
    1.69 + * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
    1.70 + * The actual CRC32 algorithm is taken from RFC 1952.
    1.71 + * Status:  Believed complete and correct.
    1.72 + */
    1.73 +
    1.74  /**
    1.75 - * A class that can be used to compute the CRC-32 of a data stream.
    1.76 + * Computes CRC32 data checksum of a data stream.
    1.77 + * The actual CRC32 algorithm is described in RFC 1952
    1.78 + * (GZIP file format specification version 4.3).
    1.79 + * Can be used to get the CRC32 over a stream if used with checked input/output
    1.80 + * streams.
    1.81   *
    1.82 - * @see         Checksum
    1.83 - * @author      David Connelly
    1.84 + * @see InflaterInputStream
    1.85 + * @see DeflaterOutputStream
    1.86 + *
    1.87 + * @author Per Bothner
    1.88 + * @date April 1, 1999.
    1.89   */
    1.90 -public
    1.91 -class CRC32 implements Checksum {
    1.92 -    private int crc = 0xFFFFFFFF;
    1.93 +public class CRC32 implements Checksum
    1.94 +{
    1.95 +  /** The crc data checksum so far. */
    1.96 +  private int crc = 0;
    1.97  
    1.98 -    /**
    1.99 -     * Creates a new CRC32 object.
   1.100 -     */
   1.101 -    public CRC32() {
   1.102 -    }
   1.103 +  /** The fast CRC table. Computed once when the CRC32 class is loaded. */
   1.104 +  private static int[] crc_table = make_crc_table();
   1.105  
   1.106 +  /** Make the table for a fast CRC. */
   1.107 +  private static int[] make_crc_table ()
   1.108 +  {
   1.109 +    int[] crc_table = new int[256];
   1.110 +    for (int n = 0; n < 256; n++)
   1.111 +      {
   1.112 +	int c = n;
   1.113 +	for (int k = 8;  --k >= 0; )
   1.114 +	  {
   1.115 +	    if ((c & 1) != 0)
   1.116 +	      c = 0xedb88320 ^ (c >>> 1);
   1.117 +	    else
   1.118 +	      c = c >>> 1;
   1.119 +	  }
   1.120 +	crc_table[n] = c;
   1.121 +      }
   1.122 +    return crc_table;
   1.123 +  }
   1.124  
   1.125 -    /**
   1.126 -     * Updates the CRC-32 checksum with the specified byte (the low
   1.127 -     * eight bits of the argument b).
   1.128 -     *
   1.129 -     * @param b the byte to update the checksum with
   1.130 -     */
   1.131 -    public void update(int b) {
   1.132 -        byte[] arr = { (byte)b };
   1.133 -        update(arr);
   1.134 -    }
   1.135 +  /**
   1.136 +   * Returns the CRC32 data checksum computed so far.
   1.137 +   */
   1.138 +  public long getValue ()
   1.139 +  {
   1.140 +    return (long) crc & 0xffffffffL;
   1.141 +  }
   1.142  
   1.143 -    /**
   1.144 -     * Updates the CRC-32 checksum with the specified array of bytes.
   1.145 -     */
   1.146 -    public void update(byte[] b, int off, int len) {
   1.147 -        if (b == null) {
   1.148 -            throw new NullPointerException();
   1.149 -        }
   1.150 -        if (off < 0 || len < 0 || off > b.length - len) {
   1.151 -            throw new ArrayIndexOutOfBoundsException();
   1.152 -        }
   1.153 -        crc = updateBytes(crc, b, off, len);
   1.154 -    }
   1.155 +  /**
   1.156 +   * Resets the CRC32 data checksum as if no update was ever called.
   1.157 +   */
   1.158 +  public void reset () { crc = 0; }
   1.159  
   1.160 -    /**
   1.161 -     * Updates the CRC-32 checksum with the specified array of bytes.
   1.162 -     *
   1.163 -     * @param b the array of bytes to update the checksum with
   1.164 -     */
   1.165 -    public void update(byte[] b) {
   1.166 -        crc = updateBytes(crc, b, 0, b.length);
   1.167 -    }
   1.168 +  /**
   1.169 +   * Updates the checksum with the int bval. 
   1.170 +   *
   1.171 +   * @param bval (the byte is taken as the lower 8 bits of bval)
   1.172 +   */
   1.173  
   1.174 -    /**
   1.175 -     * Resets CRC-32 to initial value.
   1.176 -     */
   1.177 -    public void reset() {
   1.178 -        crc = 0;
   1.179 -    }
   1.180 +  public void update (int bval)
   1.181 +  {
   1.182 +    int c = ~crc;
   1.183 +    c = crc_table[(c ^ bval) & 0xff] ^ (c >>> 8);
   1.184 +    crc = ~c;
   1.185 +  }
   1.186  
   1.187 -    /**
   1.188 -     * Returns CRC-32 value.
   1.189 -     */
   1.190 -    public long getValue() {
   1.191 -        return (long)crc & 0xffffffffL;
   1.192 -    }
   1.193 +  /**
   1.194 +   * Adds the byte array to the data checksum.
   1.195 +   *
   1.196 +   * @param buf the buffer which contains the data
   1.197 +   * @param off the offset in the buffer where the data starts
   1.198 +   * @param len the length of the data
   1.199 +   */
   1.200 +  public void update (byte[] buf, int off, int len)
   1.201 +  {
   1.202 +    int c = ~crc;
   1.203 +    while (--len >= 0)
   1.204 +      c = crc_table[(c ^ buf[off++]) & 0xff] ^ (c >>> 8);
   1.205 +    crc = ~c;
   1.206 +  }
   1.207  
   1.208 -    // XXX: taken from 
   1.209 -    // http://introcs.cs.princeton.edu/java/51data/CRC32.java.html
   1.210 -    private static int updateBytes(int crc, byte[] arr, int off, int len) {
   1.211 -        int poly = 0xEDB88320;   // reverse polynomial
   1.212 -
   1.213 -        while (len-- > 0) {
   1.214 -            byte b = arr[off++];
   1.215 -            int temp = (crc ^ b) & 0xff;
   1.216 -
   1.217 -            // read 8 bits one at a time
   1.218 -            for (int i = 0; i < 8; i++) {
   1.219 -                if ((temp & 1) == 1) {
   1.220 -                    temp = (temp >>> 1) ^ poly;
   1.221 -                } else {
   1.222 -                    temp = (temp >>> 1);
   1.223 -                }
   1.224 -            }
   1.225 -            crc = (crc >>> 8) ^ temp;
   1.226 -        }
   1.227 -        return crc ^ 0xffffffff;
   1.228 -    }
   1.229 +  /**
   1.230 +   * Adds the complete byte array to the data checksum.
   1.231 +   */
   1.232 +  public void update (byte[] buf) { update(buf, 0, buf.length); }
   1.233  }