rt/emul/mini/src/main/java/java/util/zip/CRC32.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 640 emul/mini/src/main/java/java/util/zip/CRC32.java@693745d01b55
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@640
     1
/* CRC32.java - Computes CRC32 data checksum of a data stream
jaroslav@640
     2
   Copyright (C) 1999. 2000, 2001 Free Software Foundation, Inc.
jaroslav@640
     3
jaroslav@640
     4
This file is part of GNU Classpath.
jaroslav@640
     5
jaroslav@640
     6
GNU Classpath is free software; you can redistribute it and/or modify
jaroslav@640
     7
it under the terms of the GNU General Public License as published by
jaroslav@640
     8
the Free Software Foundation; either version 2, or (at your option)
jaroslav@640
     9
any later version.
jaroslav@640
    10
jaroslav@640
    11
GNU Classpath is distributed in the hope that it will be useful, but
jaroslav@640
    12
WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@640
    13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
jaroslav@640
    14
General Public License for more details.
jaroslav@640
    15
jaroslav@640
    16
You should have received a copy of the GNU General Public License
jaroslav@640
    17
along with GNU Classpath; see the file COPYING.  If not, write to the
jaroslav@640
    18
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
jaroslav@640
    19
02111-1307 USA.
jaroslav@640
    20
jaroslav@640
    21
Linking this library statically or dynamically with other modules is
jaroslav@640
    22
making a combined work based on this library.  Thus, the terms and
jaroslav@640
    23
conditions of the GNU General Public License cover the whole
jaroslav@640
    24
combination.
jaroslav@640
    25
jaroslav@640
    26
As a special exception, the copyright holders of this library give you
jaroslav@640
    27
permission to link this library with independent modules to produce an
jaroslav@640
    28
executable, regardless of the license terms of these independent
jaroslav@640
    29
modules, and to copy and distribute the resulting executable under
jaroslav@640
    30
terms of your choice, provided that you also meet, for each linked
jaroslav@640
    31
independent module, the terms and conditions of the license of that
jaroslav@640
    32
module.  An independent module is a module which is not derived from
jaroslav@640
    33
or based on this library.  If you modify this library, you may extend
jaroslav@640
    34
this exception to your version of the library, but you are not
jaroslav@640
    35
obligated to do so.  If you do not wish to do so, delete this
jaroslav@640
    36
exception statement from your version. */
jaroslav@609
    37
jaroslav@609
    38
package java.util.zip;
jaroslav@609
    39
jaroslav@640
    40
/*
jaroslav@640
    41
 * Written using on-line Java Platform 1.2 API Specification, as well
jaroslav@640
    42
 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
jaroslav@640
    43
 * The actual CRC32 algorithm is taken from RFC 1952.
jaroslav@640
    44
 * Status:  Believed complete and correct.
jaroslav@640
    45
 */
jaroslav@640
    46
jaroslav@609
    47
/**
jaroslav@640
    48
 * Computes CRC32 data checksum of a data stream.
jaroslav@640
    49
 * The actual CRC32 algorithm is described in RFC 1952
jaroslav@640
    50
 * (GZIP file format specification version 4.3).
jaroslav@640
    51
 * Can be used to get the CRC32 over a stream if used with checked input/output
jaroslav@640
    52
 * streams.
jaroslav@609
    53
 *
jaroslav@640
    54
 * @see InflaterInputStream
jaroslav@640
    55
 * @see DeflaterOutputStream
jaroslav@640
    56
 *
jaroslav@640
    57
 * @author Per Bothner
jaroslav@640
    58
 * @date April 1, 1999.
jaroslav@609
    59
 */
jaroslav@640
    60
public class CRC32 implements Checksum
jaroslav@640
    61
{
jaroslav@640
    62
  /** The crc data checksum so far. */
jaroslav@640
    63
  private int crc = 0;
jaroslav@609
    64
jaroslav@640
    65
  /** The fast CRC table. Computed once when the CRC32 class is loaded. */
jaroslav@640
    66
  private static int[] crc_table = make_crc_table();
jaroslav@609
    67
jaroslav@640
    68
  /** Make the table for a fast CRC. */
jaroslav@640
    69
  private static int[] make_crc_table ()
jaroslav@640
    70
  {
jaroslav@640
    71
    int[] crc_table = new int[256];
jaroslav@640
    72
    for (int n = 0; n < 256; n++)
jaroslav@640
    73
      {
jaroslav@640
    74
	int c = n;
jaroslav@640
    75
	for (int k = 8;  --k >= 0; )
jaroslav@640
    76
	  {
jaroslav@640
    77
	    if ((c & 1) != 0)
jaroslav@640
    78
	      c = 0xedb88320 ^ (c >>> 1);
jaroslav@640
    79
	    else
jaroslav@640
    80
	      c = c >>> 1;
jaroslav@640
    81
	  }
jaroslav@640
    82
	crc_table[n] = c;
jaroslav@640
    83
      }
jaroslav@640
    84
    return crc_table;
jaroslav@640
    85
  }
jaroslav@609
    86
jaroslav@640
    87
  /**
jaroslav@640
    88
   * Returns the CRC32 data checksum computed so far.
jaroslav@640
    89
   */
jaroslav@640
    90
  public long getValue ()
jaroslav@640
    91
  {
jaroslav@640
    92
    return (long) crc & 0xffffffffL;
jaroslav@640
    93
  }
jaroslav@609
    94
jaroslav@640
    95
  /**
jaroslav@640
    96
   * Resets the CRC32 data checksum as if no update was ever called.
jaroslav@640
    97
   */
jaroslav@640
    98
  public void reset () { crc = 0; }
jaroslav@609
    99
jaroslav@640
   100
  /**
jaroslav@640
   101
   * Updates the checksum with the int bval. 
jaroslav@640
   102
   *
jaroslav@640
   103
   * @param bval (the byte is taken as the lower 8 bits of bval)
jaroslav@640
   104
   */
jaroslav@609
   105
jaroslav@640
   106
  public void update (int bval)
jaroslav@640
   107
  {
jaroslav@640
   108
    int c = ~crc;
jaroslav@640
   109
    c = crc_table[(c ^ bval) & 0xff] ^ (c >>> 8);
jaroslav@640
   110
    crc = ~c;
jaroslav@640
   111
  }
jaroslav@609
   112
jaroslav@640
   113
  /**
jaroslav@640
   114
   * Adds the byte array to the data checksum.
jaroslav@640
   115
   *
jaroslav@640
   116
   * @param buf the buffer which contains the data
jaroslav@640
   117
   * @param off the offset in the buffer where the data starts
jaroslav@640
   118
   * @param len the length of the data
jaroslav@640
   119
   */
jaroslav@640
   120
  public void update (byte[] buf, int off, int len)
jaroslav@640
   121
  {
jaroslav@640
   122
    int c = ~crc;
jaroslav@640
   123
    while (--len >= 0)
jaroslav@640
   124
      c = crc_table[(c ^ buf[off++]) & 0xff] ^ (c >>> 8);
jaroslav@640
   125
    crc = ~c;
jaroslav@640
   126
  }
jaroslav@609
   127
jaroslav@640
   128
  /**
jaroslav@640
   129
   * Adds the complete byte array to the data checksum.
jaroslav@640
   130
   */
jaroslav@640
   131
  public void update (byte[] buf) { update(buf, 0, buf.length); }
jaroslav@609
   132
}