PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Compression/Checksums/Adler32.cs

#
C# | 221 lines | 103 code | 25 blank | 93 comment | 6 complexity | 053790727166bf2ad9dca8fbbb3bdaf8 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. namespace Delta.Utilities.Compression.Checksums
  3. {
  4. #region Summary
  5. /// <summary>
  6. /// Computes Adler32 checksum for a stream of data. An Adler32
  7. /// checksum is not as reliable as a CRC32 checksum, but a lot faster to
  8. /// compute.
  9. /// </summary>
  10. /// <remarks>
  11. /// The specification for Adler32 may be found in RFC 1950.
  12. /// ZLIB Compressed Data Format Specification version 3.3)
  13. ///
  14. /// From that document:
  15. ///
  16. /// "ADLER32 (Adler-32 checksum)
  17. /// This contains a checksum value of the uncompressed data
  18. /// (excluding any dictionary data) computed according to Adler-32
  19. /// algorithm. This algorithm is a 32-bit extension and improvement
  20. /// of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
  21. /// standard.
  22. ///
  23. /// Adler-32 is composed of two sums accumulated per byte: s1 is
  24. /// the sum of all bytes, s2 is the sum of all s1 values. Both sums
  25. /// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
  26. /// Adler-32 checksum is stored as s2*65536 + s1 in most-
  27. /// significant-byte first (network) order."
  28. ///
  29. /// "8.2. The Adler-32 algorithm
  30. ///
  31. /// The Adler-32 algorithm is much faster than the CRC32 algorithm yet
  32. /// still provides an extremely low probability of undetected errors.
  33. ///
  34. /// The modulo on unsigned long accumulators can be delayed for 5552
  35. /// bytes, so the modulo operation time is negligible. If the bytes
  36. /// are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
  37. /// and order sensitive, unlike the first sum, which is just a
  38. /// checksum. That 65521 is prime is important to avoid a possible
  39. /// large class of two-byte errors that leave the check unchanged.
  40. /// (The Fletcher checksum uses 255, which is not prime and which also
  41. /// makes the Fletcher check insensitive to single byte changes 0 -
  42. /// 255.)
  43. ///
  44. /// The sum s1 is initialized to 1 instead of zero to make the length
  45. /// of the sequence part of s2, so that the length does not have to be
  46. /// checked separately. (Any sequence of zeroes has a Fletcher
  47. /// checksum of zero.)"
  48. /// </remarks>
  49. /// <see cref="Delta.Utilities.Compression.Streams.InflaterInputStream"/>
  50. /// <see cref="Delta.Utilities.Compression.Streams.DeflaterOutputStream"/>
  51. #endregion
  52. public sealed class Adler32
  53. {
  54. #region Constants
  55. /// <summary>
  56. /// largest prime smaller than 65536
  57. /// </summary>
  58. private const uint Base = 65521;
  59. #endregion
  60. #region Value (Public)
  61. /// <summary>
  62. /// Returns the Adler32 data checksum computed so far.
  63. /// </summary>
  64. public long Value
  65. {
  66. get
  67. {
  68. return checksum;
  69. } // get
  70. }
  71. #endregion
  72. #region Private
  73. #region checksum (Private)
  74. /// <summary>
  75. /// Checksum
  76. /// </summary>
  77. private uint checksum;
  78. #endregion
  79. #endregion
  80. #region Constructors
  81. /// <summary>
  82. /// Creates a new instance of the <code>Adler32</code> class.
  83. /// The checksum starts off with a value of 1.
  84. /// </summary>
  85. public Adler32()
  86. {
  87. Reset();
  88. }
  89. #endregion
  90. #region Reset (Public)
  91. /// <summary>
  92. /// Resets the Adler32 checksum to the initial value.
  93. /// </summary>
  94. public void Reset()
  95. {
  96. checksum = 1; //Initialize to 1
  97. }
  98. #endregion
  99. #region Update (Public)
  100. /// <summary>
  101. /// Updates the checksum with the byte b.
  102. /// </summary>
  103. /// <param name="value">Data value to add.
  104. /// The high byte of the int is ignored.</param>
  105. public void Update(int value)
  106. {
  107. // We could make a length 1 byte array and call update again,
  108. // but I would rather not have that overhead.
  109. uint s1 = checksum & 0xFFFF;
  110. uint s2 = checksum >> 16;
  111. s1 = (s1 + ((uint)value & 0xFF)) % Base;
  112. s2 = (s1 + s2) % Base;
  113. checksum = (s2 << 16) + s1;
  114. }
  115. // Update(value)
  116. /// <summary>
  117. /// Updates the checksum with the bytes taken from the array.
  118. /// </summary>
  119. /// <param name="buffer">An array of bytes</param>
  120. public void Update(byte[] buffer)
  121. {
  122. Update(buffer, 0, buffer.Length);
  123. }
  124. // Update(buffer)
  125. /// <summary>
  126. /// Updates the checksum with the bytes taken from the array.
  127. /// </summary>
  128. /// <param name="buffer">An array of bytes</param>
  129. /// <param name="offset">Start of the data used for this update</param>
  130. /// <param name="count">Number of bytes to use for this update</param>
  131. public void Update(byte[] buffer, int offset, int count)
  132. {
  133. if (buffer == null)
  134. {
  135. throw new ArgumentNullException("buffer");
  136. } // if (buf)
  137. if (offset < 0 ||
  138. count < 0 ||
  139. offset + count > buffer.Length)
  140. {
  141. throw new ArgumentOutOfRangeException();
  142. } // if (off)
  143. //(By Per Bothner)
  144. uint s1 = checksum & 0xFFFF;
  145. uint s2 = checksum >> 16;
  146. while (count > 0)
  147. {
  148. // We can defer the modulo operation:
  149. // s1 maximally grows from 65521 to 65521 + 255 * 3800
  150. // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
  151. int n = 3800;
  152. if (n > count)
  153. {
  154. n = count;
  155. } // if (n)
  156. count -= n;
  157. while (--n >= 0)
  158. {
  159. s1 = s1 + (uint)(buffer[offset++] & 0xFF);
  160. s2 = s2 + s1;
  161. } // while (--n)
  162. s1 %= Base;
  163. s2 %= Base;
  164. } // while (count)
  165. checksum = (s2 << 16) | s1;
  166. }
  167. #endregion
  168. /// <summary>
  169. /// Adler 32 tests
  170. /// </summary>
  171. public class Adler32Tests
  172. {
  173. #region Helpers
  174. #region TestAdler32
  175. /// <summary>
  176. /// Test adler 32
  177. /// </summary>
  178. public void TestAdler32()
  179. {
  180. Adler32 testAdler32 = new Adler32();
  181. // Test clearing (sets value to 1 here)
  182. testAdler32.Reset();
  183. Assert.Equal(1, testAdler32.Value);
  184. // Throw in a byte array
  185. byte[] someData = new byte[]
  186. {
  187. 59, 49, 193, 98, 4, 199, 254, 9, 42
  188. };
  189. testAdler32.Update(someData);
  190. Assert.Equal(295502732, testAdler32.Value);
  191. }
  192. #endregion
  193. #endregion
  194. }
  195. }
  196. }