Utility.Verifier.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. namespace GameFramework
  10. {
  11. public static partial class Utility
  12. {
  13. /// <summary>
  14. /// 校验相关的实用函数。
  15. /// </summary>
  16. public static partial class Verifier
  17. {
  18. private const int CachedBytesLength = 0x1000;
  19. private static readonly byte[] s_CachedBytes = new byte[CachedBytesLength];
  20. private static readonly Crc32 s_Algorithm = new Crc32();
  21. /// <summary>
  22. /// 计算二进制流的 CRC32。
  23. /// </summary>
  24. /// <param name="bytes">指定的二进制流。</param>
  25. /// <returns>计算后的 CRC32。</returns>
  26. public static int GetCrc32(byte[] bytes)
  27. {
  28. if (bytes == null)
  29. {
  30. throw new GameFrameworkException("Bytes is invalid.");
  31. }
  32. return GetCrc32(bytes, 0, bytes.Length);
  33. }
  34. /// <summary>
  35. /// 计算二进制流的 CRC32。
  36. /// </summary>
  37. /// <param name="bytes">指定的二进制流。</param>
  38. /// <param name="offset">二进制流的偏移。</param>
  39. /// <param name="length">二进制流的长度。</param>
  40. /// <returns>计算后的 CRC32。</returns>
  41. public static int GetCrc32(byte[] bytes, int offset, int length)
  42. {
  43. if (bytes == null)
  44. {
  45. throw new GameFrameworkException("Bytes is invalid.");
  46. }
  47. if (offset < 0 || length < 0 || offset + length > bytes.Length)
  48. {
  49. throw new GameFrameworkException("Offset or length is invalid.");
  50. }
  51. s_Algorithm.HashCore(bytes, offset, length);
  52. int result = (int)s_Algorithm.HashFinal();
  53. s_Algorithm.Initialize();
  54. return result;
  55. }
  56. /// <summary>
  57. /// 计算二进制流的 CRC32。
  58. /// </summary>
  59. /// <param name="stream">指定的二进制流。</param>
  60. /// <returns>计算后的 CRC32。</returns>
  61. public static int GetCrc32(Stream stream)
  62. {
  63. if (stream == null)
  64. {
  65. throw new GameFrameworkException("Stream is invalid.");
  66. }
  67. while (true)
  68. {
  69. int bytesRead = stream.Read(s_CachedBytes, 0, CachedBytesLength);
  70. if (bytesRead > 0)
  71. {
  72. s_Algorithm.HashCore(s_CachedBytes, 0, bytesRead);
  73. }
  74. else
  75. {
  76. break;
  77. }
  78. }
  79. int result = (int)s_Algorithm.HashFinal();
  80. s_Algorithm.Initialize();
  81. Array.Clear(s_CachedBytes, 0, CachedBytesLength);
  82. return result;
  83. }
  84. /// <summary>
  85. /// 获取 CRC32 数值的二进制数组。
  86. /// </summary>
  87. /// <param name="crc32">CRC32 数值。</param>
  88. /// <returns>CRC32 数值的二进制数组。</returns>
  89. public static byte[] GetCrc32Bytes(int crc32)
  90. {
  91. return new byte[] { (byte)((crc32 >> 24) & 0xff), (byte)((crc32 >> 16) & 0xff), (byte)((crc32 >> 8) & 0xff), (byte)(crc32 & 0xff) };
  92. }
  93. /// <summary>
  94. /// 获取 CRC32 数值的二进制数组。
  95. /// </summary>
  96. /// <param name="crc32">CRC32 数值。</param>
  97. /// <param name="bytes">要存放结果的数组。</param>
  98. public static void GetCrc32Bytes(int crc32, byte[] bytes)
  99. {
  100. GetCrc32Bytes(crc32, bytes, 0);
  101. }
  102. /// <summary>
  103. /// 获取 CRC32 数值的二进制数组。
  104. /// </summary>
  105. /// <param name="crc32">CRC32 数值。</param>
  106. /// <param name="bytes">要存放结果的数组。</param>
  107. /// <param name="offset">CRC32 数值的二进制数组在结果数组内的起始位置。</param>
  108. public static void GetCrc32Bytes(int crc32, byte[] bytes, int offset)
  109. {
  110. if (bytes == null)
  111. {
  112. throw new GameFrameworkException("Result is invalid.");
  113. }
  114. if (offset < 0 || offset + 4 > bytes.Length)
  115. {
  116. throw new GameFrameworkException("Offset or length is invalid.");
  117. }
  118. bytes[offset] = (byte)((crc32 >> 24) & 0xff);
  119. bytes[offset + 1] = (byte)((crc32 >> 16) & 0xff);
  120. bytes[offset + 2] = (byte)((crc32 >> 8) & 0xff);
  121. bytes[offset + 3] = (byte)(crc32 & 0xff);
  122. }
  123. internal static int GetCrc32(Stream stream, byte[] code, int length)
  124. {
  125. if (stream == null)
  126. {
  127. throw new GameFrameworkException("Stream is invalid.");
  128. }
  129. if (code == null)
  130. {
  131. throw new GameFrameworkException("Code is invalid.");
  132. }
  133. int codeLength = code.Length;
  134. if (codeLength <= 0)
  135. {
  136. throw new GameFrameworkException("Code length is invalid.");
  137. }
  138. int bytesLength = (int)stream.Length;
  139. if (length < 0 || length > bytesLength)
  140. {
  141. length = bytesLength;
  142. }
  143. int codeIndex = 0;
  144. while (true)
  145. {
  146. int bytesRead = stream.Read(s_CachedBytes, 0, CachedBytesLength);
  147. if (bytesRead > 0)
  148. {
  149. if (length > 0)
  150. {
  151. for (int i = 0; i < bytesRead && i < length; i++)
  152. {
  153. s_CachedBytes[i] ^= code[codeIndex++];
  154. codeIndex %= codeLength;
  155. }
  156. length -= bytesRead;
  157. }
  158. s_Algorithm.HashCore(s_CachedBytes, 0, bytesRead);
  159. }
  160. else
  161. {
  162. break;
  163. }
  164. }
  165. int result = (int)s_Algorithm.HashFinal();
  166. s_Algorithm.Initialize();
  167. Array.Clear(s_CachedBytes, 0, CachedBytesLength);
  168. return result;
  169. }
  170. }
  171. }
  172. }