CRC32.java 4.0 KB

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