Adler32.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. private static readonly uint BASE = 65521;
  57. /// <summary>
  58. /// The CRC data checksum so far.
  59. /// </summary>
  60. private uint checkValue;
  61. #endregion Instance Fields
  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. {
  81. get
  82. {
  83. return checkValue;
  84. }
  85. }
  86. /// <summary>
  87. /// Updates the checksum with the byte b.
  88. /// </summary>
  89. /// <param name="bval">
  90. /// The data value to add. The high byte of the int is ignored.
  91. /// </param>
  92. public void Update(int bval)
  93. {
  94. // We could make a length 1 byte array and call update again, but I
  95. // would rather not have that overhead
  96. uint s1 = checkValue & 0xFFFF;
  97. uint s2 = checkValue >> 16;
  98. s1 = (s1 + ((uint)bval & 0xFF)) % BASE;
  99. s2 = (s1 + s2) % BASE;
  100. checkValue = (s2 << 16) + s1;
  101. }
  102. /// <summary>
  103. /// Updates the Adler32 data checksum with the bytes taken from
  104. /// a block of data.
  105. /// </summary>
  106. /// <param name="buffer">Contains the data to update the checksum with.</param>
  107. public void Update(byte[] buffer)
  108. {
  109. if (buffer == null)
  110. {
  111. throw new ArgumentNullException(nameof(buffer));
  112. }
  113. Update(new ArraySegment<byte>(buffer, 0, buffer.Length));
  114. }
  115. /// <summary>
  116. /// Update Adler32 data checksum based on a portion of a block of data
  117. /// </summary>
  118. /// <param name = "segment">
  119. /// The chunk of data to add
  120. /// </param>
  121. public void Update(ArraySegment<byte> segment)
  122. {
  123. //(By Per Bothner)
  124. uint s1 = checkValue & 0xFFFF;
  125. uint s2 = checkValue >> 16;
  126. var count = segment.Count;
  127. var offset = segment.Offset;
  128. while (count > 0)
  129. {
  130. // We can defer the modulo operation:
  131. // s1 maximally grows from 65521 to 65521 + 255 * 3800
  132. // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
  133. int n = 3800;
  134. if (n > count)
  135. {
  136. n = count;
  137. }
  138. count -= n;
  139. while (--n >= 0)
  140. {
  141. s1 = s1 + (uint)(segment.Array[offset++] & 0xff);
  142. s2 = s2 + s1;
  143. }
  144. s1 %= BASE;
  145. s2 %= BASE;
  146. }
  147. checkValue = (s2 << 16) | s1;
  148. }
  149. }
  150. }