CRC32.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // CRC32.cs
  2. // ------------------------------------------------------------------
  3. //
  4. // Copyright (c) 2011 Dino Chiesa.
  5. // All rights reserved.
  6. //
  7. // This code module is part of DotNetZip, a zipfile class library.
  8. //
  9. // ------------------------------------------------------------------
  10. //
  11. // This code is licensed under the Microsoft Public License.
  12. // See the file License.txt for the license details.
  13. // More info on: http://dotnetzip.codeplex.com
  14. //
  15. // ------------------------------------------------------------------
  16. //
  17. // Last Saved: <2011-August-02 18:25:54>
  18. //
  19. // ------------------------------------------------------------------
  20. //
  21. // This module defines the CRC32 class, which can do the CRC32 algorithm, using
  22. // arbitrary starting polynomials, and bit reversal. The bit reversal is what
  23. // distinguishes this CRC-32 used in BZip2 from the CRC-32 that is used in PKZIP
  24. // files, or GZIP files. This class does both.
  25. //
  26. // ------------------------------------------------------------------
  27. using System;
  28. using Interop = System.Runtime.InteropServices;
  29. namespace BestHTTP.Decompression.Crc
  30. {
  31. /// <summary>
  32. /// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
  33. /// can set the polynomial and enable or disable bit
  34. /// reversal. This can be used for GZIP, BZip2, or ZIP.
  35. /// </summary>
  36. /// <remarks>
  37. /// This type is used internally by DotNetZip; it is generally not used
  38. /// directly by applications wishing to create, read, or manipulate zip
  39. /// archive files.
  40. /// </remarks>
  41. internal class CRC32
  42. {
  43. /// <summary>
  44. /// Indicates the total number of bytes applied to the CRC.
  45. /// </summary>
  46. public Int64 TotalBytesRead
  47. {
  48. get
  49. {
  50. return _TotalBytesRead;
  51. }
  52. }
  53. /// <summary>
  54. /// Indicates the current CRC for all blocks slurped in.
  55. /// </summary>
  56. public Int32 Crc32Result
  57. {
  58. get
  59. {
  60. return unchecked((Int32)(~_register));
  61. }
  62. }
  63. /// <summary>
  64. /// Returns the CRC32 for the specified stream.
  65. /// </summary>
  66. /// <param name="input">The stream over which to calculate the CRC32</param>
  67. /// <returns>the CRC32 calculation</returns>
  68. public Int32 GetCrc32(System.IO.Stream input)
  69. {
  70. return GetCrc32AndCopy(input, null);
  71. }
  72. /// <summary>
  73. /// Returns the CRC32 for the specified stream, and writes the input into the
  74. /// output stream.
  75. /// </summary>
  76. /// <param name="input">The stream over which to calculate the CRC32</param>
  77. /// <param name="output">The stream into which to deflate the input</param>
  78. /// <returns>the CRC32 calculation</returns>
  79. public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
  80. {
  81. if (input == null)
  82. throw new Exception("The input stream must not be null.");
  83. unchecked
  84. {
  85. byte[] buffer = new byte[BUFFER_SIZE];
  86. int readSize = BUFFER_SIZE;
  87. _TotalBytesRead = 0;
  88. int count = input.Read(buffer, 0, readSize);
  89. if (output != null) output.Write(buffer, 0, count);
  90. _TotalBytesRead += count;
  91. while (count > 0)
  92. {
  93. SlurpBlock(buffer, 0, count);
  94. count = input.Read(buffer, 0, readSize);
  95. if (output != null) output.Write(buffer, 0, count);
  96. _TotalBytesRead += count;
  97. }
  98. return (Int32)(~_register);
  99. }
  100. }
  101. /// <summary>
  102. /// Get the CRC32 for the given (word,byte) combo. This is a
  103. /// computation defined by PKzip for PKZIP 2.0 (weak) encryption.
  104. /// </summary>
  105. /// <param name="W">The word to start with.</param>
  106. /// <param name="B">The byte to combine it with.</param>
  107. /// <returns>The CRC-ized result.</returns>
  108. public Int32 ComputeCrc32(Int32 W, byte B)
  109. {
  110. return _InternalComputeCrc32((UInt32)W, B);
  111. }
  112. internal Int32 _InternalComputeCrc32(UInt32 W, byte B)
  113. {
  114. return (Int32)(crc32Table[(W ^ B) & 0xFF] ^ (W >> 8));
  115. }
  116. /// <summary>
  117. /// Update the value for the running CRC32 using the given block of bytes.
  118. /// This is useful when using the CRC32() class in a Stream.
  119. /// </summary>
  120. /// <param name="block">block of bytes to slurp</param>
  121. /// <param name="offset">starting point in the block</param>
  122. /// <param name="count">how many bytes within the block to slurp</param>
  123. public void SlurpBlock(byte[] block, int offset, int count)
  124. {
  125. if (block == null)
  126. throw new Exception("The data buffer must not be null.");
  127. // bzip algorithm
  128. for (int i = 0; i < count; i++)
  129. {
  130. int x = offset + i;
  131. byte b = block[x];
  132. if (this.reverseBits)
  133. {
  134. UInt32 temp = (_register >> 24) ^ b;
  135. _register = (_register << 8) ^ crc32Table[temp];
  136. }
  137. else
  138. {
  139. UInt32 temp = (_register & 0x000000FF) ^ b;
  140. _register = (_register >> 8) ^ crc32Table[temp];
  141. }
  142. }
  143. _TotalBytesRead += count;
  144. }
  145. /// <summary>
  146. /// Process one byte in the CRC.
  147. /// </summary>
  148. /// <param name = "b">the byte to include into the CRC . </param>
  149. public void UpdateCRC(byte b)
  150. {
  151. if (this.reverseBits)
  152. {
  153. UInt32 temp = (_register >> 24) ^ b;
  154. _register = (_register << 8) ^ crc32Table[temp];
  155. }
  156. else
  157. {
  158. UInt32 temp = (_register & 0x000000FF) ^ b;
  159. _register = (_register >> 8) ^ crc32Table[temp];
  160. }
  161. }
  162. /// <summary>
  163. /// Process a run of N identical bytes into the CRC.
  164. /// </summary>
  165. /// <remarks>
  166. /// <para>
  167. /// This method serves as an optimization for updating the CRC when a
  168. /// run of identical bytes is found. Rather than passing in a buffer of
  169. /// length n, containing all identical bytes b, this method accepts the
  170. /// byte value and the length of the (virtual) buffer - the length of
  171. /// the run.
  172. /// </para>
  173. /// </remarks>
  174. /// <param name = "b">the byte to include into the CRC. </param>
  175. /// <param name = "n">the number of times that byte should be repeated. </param>
  176. public void UpdateCRC(byte b, int n)
  177. {
  178. while (n-- > 0)
  179. {
  180. if (this.reverseBits)
  181. {
  182. uint temp = (_register >> 24) ^ b;
  183. _register = (_register << 8) ^ crc32Table[(temp >= 0)
  184. ? temp
  185. : (temp + 256)];
  186. }
  187. else
  188. {
  189. UInt32 temp = (_register & 0x000000FF) ^ b;
  190. _register = (_register >> 8) ^ crc32Table[(temp >= 0)
  191. ? temp
  192. : (temp + 256)];
  193. }
  194. }
  195. }
  196. private static uint ReverseBits(uint data)
  197. {
  198. unchecked
  199. {
  200. uint ret = data;
  201. ret = (ret & 0x55555555) << 1 | (ret >> 1) & 0x55555555;
  202. ret = (ret & 0x33333333) << 2 | (ret >> 2) & 0x33333333;
  203. ret = (ret & 0x0F0F0F0F) << 4 | (ret >> 4) & 0x0F0F0F0F;
  204. ret = (ret << 24) | ((ret & 0xFF00) << 8) | ((ret >> 8) & 0xFF00) | (ret >> 24);
  205. return ret;
  206. }
  207. }
  208. private static byte ReverseBits(byte data)
  209. {
  210. unchecked
  211. {
  212. uint u = (uint)data * 0x00020202;
  213. uint m = 0x01044010;
  214. uint s = u & m;
  215. uint t = (u << 2) & (m << 1);
  216. return (byte)((0x01001001 * (s + t)) >> 24);
  217. }
  218. }
  219. private void GenerateLookupTable()
  220. {
  221. crc32Table = new UInt32[256];
  222. unchecked
  223. {
  224. UInt32 dwCrc;
  225. byte i = 0;
  226. do
  227. {
  228. dwCrc = i;
  229. for (byte j = 8; j > 0; j--)
  230. {
  231. if ((dwCrc & 1) == 1)
  232. {
  233. dwCrc = (dwCrc >> 1) ^ dwPolynomial;
  234. }
  235. else
  236. {
  237. dwCrc >>= 1;
  238. }
  239. }
  240. if (reverseBits)
  241. {
  242. crc32Table[ReverseBits(i)] = ReverseBits(dwCrc);
  243. }
  244. else
  245. {
  246. crc32Table[i] = dwCrc;
  247. }
  248. i++;
  249. } while (i!=0);
  250. }
  251. #if VERBOSE
  252. Console.WriteLine();
  253. Console.WriteLine("private static readonly UInt32[] crc32Table = {");
  254. for (int i = 0; i < crc32Table.Length; i+=4)
  255. {
  256. Console.Write(" ");
  257. for (int j=0; j < 4; j++)
  258. {
  259. Console.Write(" 0x{0:X8}U,", crc32Table[i+j]);
  260. }
  261. Console.WriteLine();
  262. }
  263. Console.WriteLine("};");
  264. Console.WriteLine();
  265. #endif
  266. }
  267. private uint gf2_matrix_times(uint[] matrix, uint vec)
  268. {
  269. uint sum = 0;
  270. int i=0;
  271. while (vec != 0)
  272. {
  273. if ((vec & 0x01)== 0x01)
  274. sum ^= matrix[i];
  275. vec >>= 1;
  276. i++;
  277. }
  278. return sum;
  279. }
  280. private void gf2_matrix_square(uint[] square, uint[] mat)
  281. {
  282. for (int i = 0; i < 32; i++)
  283. square[i] = gf2_matrix_times(mat, mat[i]);
  284. }
  285. /// <summary>
  286. /// Combines the given CRC32 value with the current running total.
  287. /// </summary>
  288. /// <remarks>
  289. /// This is useful when using a divide-and-conquer approach to
  290. /// calculating a CRC. Multiple threads can each calculate a
  291. /// CRC32 on a segment of the data, and then combine the
  292. /// individual CRC32 values at the end.
  293. /// </remarks>
  294. /// <param name="crc">the crc value to be combined with this one</param>
  295. /// <param name="length">the length of data the CRC value was calculated on</param>
  296. public void Combine(int crc, int length)
  297. {
  298. uint[] even = new uint[32]; // even-power-of-two zeros operator
  299. uint[] odd = new uint[32]; // odd-power-of-two zeros operator
  300. if (length == 0)
  301. return;
  302. uint crc1= ~_register;
  303. uint crc2= (uint) crc;
  304. // put operator for one zero bit in odd
  305. odd[0] = this.dwPolynomial; // the CRC-32 polynomial
  306. uint row = 1;
  307. for (int i = 1; i < 32; i++)
  308. {
  309. odd[i] = row;
  310. row <<= 1;
  311. }
  312. // put operator for two zero bits in even
  313. gf2_matrix_square(even, odd);
  314. // put operator for four zero bits in odd
  315. gf2_matrix_square(odd, even);
  316. uint len2 = (uint) length;
  317. // apply len2 zeros to crc1 (first square will put the operator for one
  318. // zero byte, eight zero bits, in even)
  319. do {
  320. // apply zeros operator for this bit of len2
  321. gf2_matrix_square(even, odd);
  322. if ((len2 & 1)== 1)
  323. crc1 = gf2_matrix_times(even, crc1);
  324. len2 >>= 1;
  325. if (len2 == 0)
  326. break;
  327. // another iteration of the loop with odd and even swapped
  328. gf2_matrix_square(odd, even);
  329. if ((len2 & 1)==1)
  330. crc1 = gf2_matrix_times(odd, crc1);
  331. len2 >>= 1;
  332. } while (len2 != 0);
  333. crc1 ^= crc2;
  334. _register= ~crc1;
  335. //return (int) crc1;
  336. return;
  337. }
  338. /// <summary>
  339. /// Create an instance of the CRC32 class using the default settings: no
  340. /// bit reversal, and a polynomial of 0xEDB88320.
  341. /// </summary>
  342. public CRC32() : this(false)
  343. {
  344. }
  345. /// <summary>
  346. /// Create an instance of the CRC32 class, specifying whether to reverse
  347. /// data bits or not.
  348. /// </summary>
  349. /// <param name='reverseBits'>
  350. /// specify true if the instance should reverse data bits.
  351. /// </param>
  352. /// <remarks>
  353. /// <para>
  354. /// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
  355. /// want a CRC32 with compatibility with BZip2, you should pass true
  356. /// here. In the CRC-32 used by GZIP and PKZIP, the bits are not
  357. /// reversed; Therefore if you want a CRC32 with compatibility with
  358. /// those, you should pass false.
  359. /// </para>
  360. /// </remarks>
  361. public CRC32(bool reverseBits) :
  362. this( unchecked((int)0xEDB88320), reverseBits)
  363. {
  364. }
  365. /// <summary>
  366. /// Create an instance of the CRC32 class, specifying the polynomial and
  367. /// whether to reverse data bits or not.
  368. /// </summary>
  369. /// <param name='polynomial'>
  370. /// The polynomial to use for the CRC, expressed in the reversed (LSB)
  371. /// format: the highest ordered bit in the polynomial value is the
  372. /// coefficient of the 0th power; the second-highest order bit is the
  373. /// coefficient of the 1 power, and so on. Expressed this way, the
  374. /// polynomial for the CRC-32C used in IEEE 802.3, is 0xEDB88320.
  375. /// </param>
  376. /// <param name='reverseBits'>
  377. /// specify true if the instance should reverse data bits.
  378. /// </param>
  379. ///
  380. /// <remarks>
  381. /// <para>
  382. /// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
  383. /// want a CRC32 with compatibility with BZip2, you should pass true
  384. /// here for the <c>reverseBits</c> parameter. In the CRC-32 used by
  385. /// GZIP and PKZIP, the bits are not reversed; Therefore if you want a
  386. /// CRC32 with compatibility with those, you should pass false for the
  387. /// <c>reverseBits</c> parameter.
  388. /// </para>
  389. /// </remarks>
  390. public CRC32(int polynomial, bool reverseBits)
  391. {
  392. this.reverseBits = reverseBits;
  393. this.dwPolynomial = (uint) polynomial;
  394. this.GenerateLookupTable();
  395. }
  396. /// <summary>
  397. /// Reset the CRC-32 class - clear the CRC "remainder register."
  398. /// </summary>
  399. /// <remarks>
  400. /// <para>
  401. /// Use this when employing a single instance of this class to compute
  402. /// multiple, distinct CRCs on multiple, distinct data blocks.
  403. /// </para>
  404. /// </remarks>
  405. public void Reset()
  406. {
  407. _register = 0xFFFFFFFFU;
  408. }
  409. // private member vars
  410. private UInt32 dwPolynomial;
  411. private Int64 _TotalBytesRead;
  412. private bool reverseBits;
  413. private UInt32[] crc32Table;
  414. private const int BUFFER_SIZE = 8192;
  415. private UInt32 _register = 0xFFFFFFFFU;
  416. }
  417. }