Adler32.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. namespace ICSharpCode.SharpZipLib.Checksum
  3. {
  4. /// <summary>
  5. /// Computes Adler32 checksum for a stream of data. An Adler32
  6. /// checksum is not as reliable as a CRC32 checksum, but a lot faster to
  7. /// compute.
  8. ///
  9. /// The specification for Adler32 may be found in RFC 1950.
  10. /// ZLIB Compressed Data Format Specification version 3.3)
  11. ///
  12. ///
  13. /// From that document:
  14. ///
  15. /// "ADLER32 (Adler-32 checksum)
  16. /// This contains a checksum value of the uncompressed data
  17. /// (excluding any dictionary data) computed according to Adler-32
  18. /// algorithm. This algorithm is a 32-bit extension and improvement
  19. /// of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
  20. /// standard.
  21. ///
  22. /// Adler-32 is composed of two sums accumulated per byte: s1 is
  23. /// the sum of all bytes, s2 is the sum of all s1 values. Both sums
  24. /// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
  25. /// Adler-32 checksum is stored as s2*65536 + s1 in most-
  26. /// significant-byte first (network) order."
  27. ///
  28. /// "8.2. The Adler-32 algorithm
  29. ///
  30. /// The Adler-32 algorithm is much faster than the CRC32 algorithm yet
  31. /// still provides an extremely low probability of undetected errors.
  32. ///
  33. /// The modulo on unsigned long accumulators can be delayed for 5552
  34. /// bytes, so the modulo operation time is negligible. If the bytes
  35. /// are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
  36. /// and order sensitive, unlike the first sum, which is just a
  37. /// checksum. That 65521 is prime is important to avoid a possible
  38. /// large class of two-byte errors that leave the check unchanged.
  39. /// (The Fletcher checksum uses 255, which is not prime and which also
  40. /// makes the Fletcher check insensitive to single byte changes 0 -
  41. /// 255.)
  42. ///
  43. /// The sum s1 is initialized to 1 instead of zero to make the length
  44. /// of the sequence part of s2, so that the length does not have to be
  45. /// checked separately. (Any sequence of zeroes has a Fletcher
  46. /// checksum of zero.)"
  47. /// </summary>
  48. /// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream"/>
  49. /// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream"/>
  50. public sealed class Adler32 : IChecksum
  51. {
  52. #region Instance Fields
  53. /// <summary>
  54. /// largest prime smaller than 65536
  55. /// </summary>
  56. readonly static uint BASE = 65521;
  57. /// <summary>
  58. /// The CRC data checksum so far.
  59. /// </summary>
  60. uint checkValue;
  61. #endregion
  62. /// <summary>
  63. /// Initialise a default instance of <see cref="Adler32"></see>
  64. /// </summary>
  65. public Adler32()
  66. {
  67. Reset();
  68. }
  69. /// <summary>
  70. /// Resets the Adler32 data checksum as if no update was ever called.
  71. /// </summary>
  72. public void Reset()
  73. {
  74. checkValue = 1;
  75. }
  76. /// <summary>
  77. /// Returns the Adler32 data checksum computed so far.
  78. /// </summary>
  79. public long Value {
  80. get {
  81. return checkValue;
  82. }
  83. }
  84. /// <summary>
  85. /// Updates the checksum with the byte b.
  86. /// </summary>
  87. /// <param name="bval">
  88. /// The data value to add. The high byte of the int is ignored.
  89. /// </param>
  90. public void Update(int bval)
  91. {
  92. // We could make a length 1 byte array and call update again, but I
  93. // would rather not have that overhead
  94. uint s1 = checkValue & 0xFFFF;
  95. uint s2 = checkValue >> 16;
  96. s1 = (s1 + ((uint)bval & 0xFF)) % BASE;
  97. s2 = (s1 + s2) % BASE;
  98. checkValue = (s2 << 16) + s1;
  99. }
  100. /// <summary>
  101. /// Updates the Adler32 data checksum with the bytes taken from
  102. /// a block of data.
  103. /// </summary>
  104. /// <param name="buffer">Contains the data to update the checksum with.</param>
  105. public void Update(byte[] buffer)
  106. {
  107. if (buffer == null) {
  108. throw new ArgumentNullException("nameof(buffer)");
  109. }
  110. Update(buffer, 0, buffer.Length);
  111. }
  112. /// <summary>
  113. /// Update Adler32 data checksum based on a portion of a block of data
  114. /// </summary>
  115. /// <param name = "buffer">Contains the data to update the CRC with.</param>
  116. /// <param name = "offset">The offset into the buffer where the data starts</param>
  117. /// <param name = "count">The number of data bytes to update the CRC with.</param>
  118. public void Update(byte[] buffer, int offset, int count)
  119. {
  120. if (buffer == null) {
  121. throw new ArgumentNullException("nameof(buffer)");
  122. }
  123. if (offset < 0) {
  124. throw new ArgumentOutOfRangeException("nameof(offset)", "cannot be less than zero");
  125. }
  126. if (offset >= buffer.Length) {
  127. throw new ArgumentOutOfRangeException("nameof(offset)", "not a valid index into buffer");
  128. }
  129. if (count < 0) {
  130. throw new ArgumentOutOfRangeException("nameof(count)", "cannot be less than zero");
  131. }
  132. if (offset + count > buffer.Length) {
  133. throw new ArgumentOutOfRangeException("nameof(count)", "exceeds buffer size");
  134. }
  135. //(By Per Bothner)
  136. uint s1 = checkValue & 0xFFFF;
  137. uint s2 = checkValue >> 16;
  138. while (count > 0) {
  139. // We can defer the modulo operation:
  140. // s1 maximally grows from 65521 to 65521 + 255 * 3800
  141. // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
  142. int n = 3800;
  143. if (n > count) {
  144. n = count;
  145. }
  146. count -= n;
  147. while (--n >= 0) {
  148. s1 = s1 + (uint)(buffer[offset++] & 0xff);
  149. s2 = s2 + s1;
  150. }
  151. s1 %= BASE;
  152. s2 %= BASE;
  153. }
  154. checkValue = (s2 << 16) | s1;
  155. }
  156. }
  157. }