Adler32.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Adler32.java - Computes Adler32 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 Adler32 algorithm is taken from RFC 1950.
  36. * Status: Believed complete and correct.
  37. */
  38. /**
  39. * Computes Adler32 checksum for a stream of data. An Adler32
  40. * checksum is not as reliable as a CRC32 checksum, but a lot faster to
  41. * compute.
  42. *<p>
  43. * The specification for Adler32 may be found in RFC 1950.
  44. * (ZLIB Compressed Data Format Specification version 3.3)
  45. *<p>
  46. *<p>
  47. * From that document:
  48. *<p>
  49. * "ADLER32 (Adler-32 checksum)
  50. * This contains a checksum value of the uncompressed data
  51. * (excluding any dictionary data) computed according to Adler-32
  52. * algorithm. This algorithm is a 32-bit extension and improvement
  53. * of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
  54. * standard.
  55. *<p>
  56. * Adler-32 is composed of two sums accumulated per byte: s1 is
  57. * the sum of all bytes, s2 is the sum of all s1 values. Both sums
  58. * are done modulo 65521. s1 is initialized to 1, s2 to zero. The
  59. * Adler-32 checksum is stored as s2*65536 + s1 in most-
  60. * significant-byte first (network) order."
  61. *<p>
  62. * "8.2. The Adler-32 algorithm
  63. *<p>
  64. * The Adler-32 algorithm is much faster than the CRC32 algorithm yet
  65. * still provides an extremely low probability of undetected errors.
  66. *<p>
  67. * The modulo on unsigned long accumulators can be delayed for 5552
  68. * bytes, so the modulo operation time is negligible. If the bytes
  69. * are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
  70. * and order sensitive, unlike the first sum, which is just a
  71. * checksum. That 65521 is prime is important to avoid a possible
  72. * large class of two-byte errors that leave the check unchanged.
  73. * (The Fletcher checksum uses 255, which is not prime and which also
  74. * makes the Fletcher check insensitive to single byte changes 0 <->
  75. * 255.)
  76. *<p>
  77. * The sum s1 is initialized to 1 instead of zero to make the length
  78. * of the sequence part of s2, so that the length does not have to be
  79. * checked separately. (Any sequence of zeroes has a Fletcher
  80. * checksum of zero.)"
  81. *
  82. * @author John Leuner, Per Bothner
  83. * @since JDK 1.1
  84. *
  85. * @see InflaterInputStream
  86. * @see DeflaterOutputStream
  87. */
  88. public class Adler32 implements Checksum
  89. {
  90. /** largest prime smaller than 65536 */
  91. private static final int BASE = 65521;
  92. private int checksum; //we do all in int.
  93. //Note that java doesn't have unsigned integers,
  94. //so we have to be careful with what arithmetic
  95. //we do. We return the checksum as a long to
  96. //avoid sign confusion.
  97. /**
  98. * Creates a new instance of the <code>Adler32</code> class.
  99. * The checksum starts off with a value of 1.
  100. */
  101. public Adler32 ()
  102. {
  103. reset();
  104. }
  105. /**
  106. * Resets the Adler32 checksum to the initial value.
  107. */
  108. public void reset ()
  109. {
  110. checksum = 1; //Initialize to 1
  111. }
  112. /**
  113. * Updates the checksum with the byte b.
  114. *
  115. * @param bval the data value to add. The high byte of the int is ignored.
  116. */
  117. public void update (int bval)
  118. {
  119. //We could make a length 1 byte array and call update again, but I
  120. //would rather not have that overhead
  121. int s1 = checksum & 0xffff;
  122. int s2 = checksum >>> 16;
  123. s1 = (s1 + (bval & 0xFF)) % BASE;
  124. s2 = (s1 + s2) % BASE;
  125. checksum = (s2 << 16) + s1;
  126. }
  127. /**
  128. * Updates the checksum with the bytes taken from the array.
  129. *
  130. * @param buffer an array of bytes
  131. */
  132. public void update (byte[] buffer)
  133. {
  134. update(buffer, 0, buffer.length);
  135. }
  136. /**
  137. * Updates the checksum with the bytes taken from the array.
  138. *
  139. * @param buf an array of bytes
  140. * @param off the start of the data used for this update
  141. * @param len the number of bytes to use for this update
  142. */
  143. public void update (byte[] buf, int off, int len)
  144. {
  145. //(By Per Bothner)
  146. int s1 = checksum & 0xffff;
  147. int s2 = checksum >>> 16;
  148. while (len > 0)
  149. {
  150. // We can defer the modulo operation:
  151. // s1 maximally grows from 65521 to 65521 + 255 * 3800
  152. // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
  153. int n = 3800;
  154. if (n > len)
  155. n = len;
  156. len -= n;
  157. while (--n >= 0)
  158. {
  159. s1 = s1 + (buf[off++] & 0xFF);
  160. s2 = s2 + s1;
  161. }
  162. s1 %= BASE;
  163. s2 %= BASE;
  164. }
  165. /*Old implementation, borrowed from somewhere:
  166. int n;
  167. while (len-- > 0) {
  168. s1 = (s1 + (bs[offset++] & 0xff)) % BASE;
  169. s2 = (s2 + s1) % BASE;
  170. }*/
  171. checksum = (s2 << 16) | s1;
  172. }
  173. /**
  174. * Returns the Adler32 data checksum computed so far.
  175. */
  176. public long getValue()
  177. {
  178. return (long) checksum & 0xffffffffL;
  179. }
  180. }