CRC32Algorithm.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Security.Cryptography;
  3. namespace YooAsset
  4. {
  5. internal class SafeProxy
  6. {
  7. private const uint Poly = 0xedb88320u;
  8. private readonly uint[] _table = new uint[16 * 256];
  9. internal SafeProxy()
  10. {
  11. Init(Poly);
  12. }
  13. public void Init(uint poly)
  14. {
  15. var table = _table;
  16. for (uint i = 0; i < 256; i++)
  17. {
  18. uint res = i;
  19. for (int t = 0; t < 16; t++)
  20. {
  21. for (int k = 0; k < 8; k++) res = (res & 1) == 1 ? poly ^ (res >> 1) : (res >> 1);
  22. table[(t * 256) + i] = res;
  23. }
  24. }
  25. }
  26. public uint Append(uint crc, byte[] input, int offset, int length)
  27. {
  28. uint crcLocal = uint.MaxValue ^ crc;
  29. uint[] table = _table;
  30. while (length >= 16)
  31. {
  32. var a = table[(3 * 256) + input[offset + 12]]
  33. ^ table[(2 * 256) + input[offset + 13]]
  34. ^ table[(1 * 256) + input[offset + 14]]
  35. ^ table[(0 * 256) + input[offset + 15]];
  36. var b = table[(7 * 256) + input[offset + 8]]
  37. ^ table[(6 * 256) + input[offset + 9]]
  38. ^ table[(5 * 256) + input[offset + 10]]
  39. ^ table[(4 * 256) + input[offset + 11]];
  40. var c = table[(11 * 256) + input[offset + 4]]
  41. ^ table[(10 * 256) + input[offset + 5]]
  42. ^ table[(9 * 256) + input[offset + 6]]
  43. ^ table[(8 * 256) + input[offset + 7]];
  44. var d = table[(15 * 256) + ((byte)crcLocal ^ input[offset])]
  45. ^ table[(14 * 256) + ((byte)(crcLocal >> 8) ^ input[offset + 1])]
  46. ^ table[(13 * 256) + ((byte)(crcLocal >> 16) ^ input[offset + 2])]
  47. ^ table[(12 * 256) + ((crcLocal >> 24) ^ input[offset + 3])];
  48. crcLocal = d ^ c ^ b ^ a;
  49. offset += 16;
  50. length -= 16;
  51. }
  52. while (--length >= 0)
  53. crcLocal = table[(byte)(crcLocal ^ input[offset++])] ^ crcLocal >> 8;
  54. return crcLocal ^ uint.MaxValue;
  55. }
  56. }
  57. /// <summary>
  58. /// This is .NET safe implementation of Crc32 algorithm.
  59. /// Implementation of CRC-32.
  60. /// This class supports several convenient static methods returning the CRC as UInt32.
  61. /// </summary>
  62. internal class CRC32Algorithm : HashAlgorithm
  63. {
  64. private uint _currentCrc;
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="CRC32Algorithm"/> class.
  67. /// </summary>
  68. public CRC32Algorithm()
  69. {
  70. #if !NETCORE13
  71. HashSizeValue = 32;
  72. #endif
  73. }
  74. /// <summary>
  75. /// Resets internal state of the algorithm. Used internally.
  76. /// </summary>
  77. public override void Initialize()
  78. {
  79. _currentCrc = 0;
  80. }
  81. /// <summary>
  82. /// Appends CRC-32 from given buffer
  83. /// </summary>
  84. protected override void HashCore(byte[] input, int offset, int length)
  85. {
  86. _currentCrc = AppendInternal(_currentCrc, input, offset, length);
  87. }
  88. /// <summary>
  89. /// Computes CRC-32 from <see cref="HashCore"/>
  90. /// </summary>
  91. protected override byte[] HashFinal()
  92. {
  93. if(BitConverter.IsLittleEndian)
  94. return new[] { (byte)_currentCrc, (byte)(_currentCrc >> 8), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 24) };
  95. else
  96. return new[] { (byte)(_currentCrc >> 24), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 8), (byte)_currentCrc };
  97. }
  98. /// <summary>
  99. /// Computes CRC-32 from multiple buffers.
  100. /// Call this method multiple times to chain multiple buffers.
  101. /// </summary>
  102. /// <param name="initial">
  103. /// Initial CRC value for the algorithm. It is zero for the first buffer.
  104. /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method.
  105. /// </param>
  106. /// <param name="input">Input buffer with data to be checksummed.</param>
  107. /// <param name="offset">Offset of the input data within the buffer.</param>
  108. /// <param name="length">Length of the input data in the buffer.</param>
  109. /// <returns>Accumulated CRC-32 of all buffers processed so far.</returns>
  110. public static uint Append(uint initial, byte[] input, int offset, int length)
  111. {
  112. if (input == null)
  113. throw new ArgumentNullException("input");
  114. if (offset < 0 || length < 0 || offset + length > input.Length)
  115. throw new ArgumentOutOfRangeException("length");
  116. return AppendInternal(initial, input, offset, length);
  117. }
  118. /// <summary>
  119. /// Computes CRC-32 from multiple buffers.
  120. /// Call this method multiple times to chain multiple buffers.
  121. /// </summary>
  122. /// <param name="initial">
  123. /// Initial CRC value for the algorithm. It is zero for the first buffer.
  124. /// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method.
  125. /// </param>
  126. /// <param name="input">Input buffer containing data to be checksummed.</param>
  127. /// <returns>Accumulated CRC-32 of all buffers processed so far.</returns>
  128. public static uint Append(uint initial, byte[] input)
  129. {
  130. if (input == null)
  131. throw new ArgumentNullException();
  132. return AppendInternal(initial, input, 0, input.Length);
  133. }
  134. /// <summary>
  135. /// Computes CRC-32 from input buffer.
  136. /// </summary>
  137. /// <param name="input">Input buffer with data to be checksummed.</param>
  138. /// <param name="offset">Offset of the input data within the buffer.</param>
  139. /// <param name="length">Length of the input data in the buffer.</param>
  140. /// <returns>CRC-32 of the data in the buffer.</returns>
  141. public static uint Compute(byte[] input, int offset, int length)
  142. {
  143. return Append(0, input, offset, length);
  144. }
  145. /// <summary>
  146. /// Computes CRC-32 from input buffer.
  147. /// </summary>
  148. /// <param name="input">Input buffer containing data to be checksummed.</param>
  149. /// <returns>CRC-32 of the buffer.</returns>
  150. public static uint Compute(byte[] input)
  151. {
  152. return Append(0, input);
  153. }
  154. /// <summary>
  155. /// Computes CRC-32 from input buffer and writes it after end of data (buffer should have 4 bytes reserved space for it). Can be used in conjunction with <see cref="IsValidWithCrcAtEnd(byte[],int,int)"/>
  156. /// </summary>
  157. /// <param name="input">Input buffer with data to be checksummed.</param>
  158. /// <param name="offset">Offset of the input data within the buffer.</param>
  159. /// <param name="length">Length of the input data in the buffer.</param>
  160. /// <returns>CRC-32 of the data in the buffer.</returns>
  161. public static uint ComputeAndWriteToEnd(byte[] input, int offset, int length)
  162. {
  163. if (length + 4 > input.Length)
  164. throw new ArgumentOutOfRangeException("length", "Length of data should be less than array length - 4 bytes of CRC data");
  165. var crc = Append(0, input, offset, length);
  166. var r = offset + length;
  167. input[r] = (byte)crc;
  168. input[r + 1] = (byte)(crc >> 8);
  169. input[r + 2] = (byte)(crc >> 16);
  170. input[r + 3] = (byte)(crc >> 24);
  171. return crc;
  172. }
  173. /// <summary>
  174. /// Computes CRC-32 from input buffer - 4 bytes and writes it as last 4 bytes of buffer. Can be used in conjunction with <see cref="IsValidWithCrcAtEnd(byte[])"/>
  175. /// </summary>
  176. /// <param name="input">Input buffer with data to be checksummed.</param>
  177. /// <returns>CRC-32 of the data in the buffer.</returns>
  178. public static uint ComputeAndWriteToEnd(byte[] input)
  179. {
  180. if (input.Length < 4)
  181. throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least");
  182. return ComputeAndWriteToEnd(input, 0, input.Length - 4);
  183. }
  184. /// <summary>
  185. /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with <see cref="ComputeAndWriteToEnd(byte[],int,int)"/>
  186. /// </summary>
  187. /// <param name="input">Input buffer with data to be checksummed.</param>
  188. /// <param name="offset">Offset of the input data within the buffer.</param>
  189. /// <param name="lengthWithCrc">Length of the input data in the buffer with CRC-32 bytes.</param>
  190. /// <returns>Is checksum valid.</returns>
  191. public static bool IsValidWithCrcAtEnd(byte[] input, int offset, int lengthWithCrc)
  192. {
  193. return Append(0, input, offset, lengthWithCrc) == 0x2144DF1C;
  194. }
  195. /// <summary>
  196. /// Validates correctness of CRC-32 data in source buffer with assumption that CRC-32 data located at end of buffer in reverse bytes order. Can be used in conjunction with <see cref="ComputeAndWriteToEnd(byte[],int,int)"/>
  197. /// </summary>
  198. /// <param name="input">Input buffer with data to be checksummed.</param>
  199. /// <returns>Is checksum valid.</returns>
  200. public static bool IsValidWithCrcAtEnd(byte[] input)
  201. {
  202. if (input.Length < 4)
  203. throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least");
  204. return Append(0, input, 0, input.Length) == 0x2144DF1C;
  205. }
  206. private static readonly SafeProxy _proxy = new SafeProxy();
  207. private static uint AppendInternal(uint initial, byte[] input, int offset, int length)
  208. {
  209. if (length > 0)
  210. {
  211. return _proxy.Append(initial, input, offset, length);
  212. }
  213. else
  214. return initial;
  215. }
  216. }
  217. }