Pack.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities
  5. {
  6. internal sealed class Pack
  7. {
  8. private Pack()
  9. {
  10. }
  11. internal static void UInt16_To_BE(ushort n, byte[] bs)
  12. {
  13. bs[0] = (byte)(n >> 8);
  14. bs[1] = (byte)(n);
  15. }
  16. internal static void UInt16_To_BE(ushort n, byte[] bs, int off)
  17. {
  18. bs[off] = (byte)(n >> 8);
  19. bs[off + 1] = (byte)(n);
  20. }
  21. internal static ushort BE_To_UInt16(byte[] bs)
  22. {
  23. uint n = (uint)bs[0] << 8
  24. | (uint)bs[1];
  25. return (ushort)n;
  26. }
  27. internal static ushort BE_To_UInt16(byte[] bs, int off)
  28. {
  29. uint n = (uint)bs[off] << 8
  30. | (uint)bs[off + 1];
  31. return (ushort)n;
  32. }
  33. internal static byte[] UInt32_To_BE(uint n)
  34. {
  35. byte[] bs = new byte[4];
  36. UInt32_To_BE(n, bs, 0);
  37. return bs;
  38. }
  39. internal static void UInt32_To_BE(uint n, byte[] bs)
  40. {
  41. bs[0] = (byte)(n >> 24);
  42. bs[1] = (byte)(n >> 16);
  43. bs[2] = (byte)(n >> 8);
  44. bs[3] = (byte)(n);
  45. }
  46. internal static void UInt32_To_BE(uint n, byte[] bs, int off)
  47. {
  48. bs[off] = (byte)(n >> 24);
  49. bs[off + 1] = (byte)(n >> 16);
  50. bs[off + 2] = (byte)(n >> 8);
  51. bs[off + 3] = (byte)(n);
  52. }
  53. internal static byte[] UInt32_To_BE(uint[] ns)
  54. {
  55. byte[] bs = new byte[4 * ns.Length];
  56. UInt32_To_BE(ns, bs, 0);
  57. return bs;
  58. }
  59. internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
  60. {
  61. for (int i = 0; i < ns.Length; ++i)
  62. {
  63. UInt32_To_BE(ns[i], bs, off);
  64. off += 4;
  65. }
  66. }
  67. internal static uint BE_To_UInt32(byte[] bs)
  68. {
  69. return (uint)bs[0] << 24
  70. | (uint)bs[1] << 16
  71. | (uint)bs[2] << 8
  72. | (uint)bs[3];
  73. }
  74. internal static uint BE_To_UInt32(byte[] bs, int off)
  75. {
  76. return (uint)bs[off] << 24
  77. | (uint)bs[off + 1] << 16
  78. | (uint)bs[off + 2] << 8
  79. | (uint)bs[off + 3];
  80. }
  81. internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns)
  82. {
  83. for (int i = 0; i < ns.Length; ++i)
  84. {
  85. ns[i] = BE_To_UInt32(bs, off);
  86. off += 4;
  87. }
  88. }
  89. internal static byte[] UInt64_To_BE(ulong n)
  90. {
  91. byte[] bs = new byte[8];
  92. UInt64_To_BE(n, bs, 0);
  93. return bs;
  94. }
  95. internal static void UInt64_To_BE(ulong n, byte[] bs)
  96. {
  97. UInt32_To_BE((uint)(n >> 32), bs);
  98. UInt32_To_BE((uint)(n), bs, 4);
  99. }
  100. internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
  101. {
  102. UInt32_To_BE((uint)(n >> 32), bs, off);
  103. UInt32_To_BE((uint)(n), bs, off + 4);
  104. }
  105. internal static byte[] UInt64_To_BE(ulong[] ns)
  106. {
  107. byte[] bs = new byte[8 * ns.Length];
  108. UInt64_To_BE(ns, bs, 0);
  109. return bs;
  110. }
  111. internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off)
  112. {
  113. for (int i = 0; i < ns.Length; ++i)
  114. {
  115. UInt64_To_BE(ns[i], bs, off);
  116. off += 8;
  117. }
  118. }
  119. internal static ulong BE_To_UInt64(byte[] bs)
  120. {
  121. uint hi = BE_To_UInt32(bs);
  122. uint lo = BE_To_UInt32(bs, 4);
  123. return ((ulong)hi << 32) | (ulong)lo;
  124. }
  125. internal static ulong BE_To_UInt64(byte[] bs, int off)
  126. {
  127. uint hi = BE_To_UInt32(bs, off);
  128. uint lo = BE_To_UInt32(bs, off + 4);
  129. return ((ulong)hi << 32) | (ulong)lo;
  130. }
  131. internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns)
  132. {
  133. for (int i = 0; i < ns.Length; ++i)
  134. {
  135. ns[i] = BE_To_UInt64(bs, off);
  136. off += 8;
  137. }
  138. }
  139. internal static void UInt16_To_LE(ushort n, byte[] bs)
  140. {
  141. bs[0] = (byte)(n);
  142. bs[1] = (byte)(n >> 8);
  143. }
  144. internal static void UInt16_To_LE(ushort n, byte[] bs, int off)
  145. {
  146. bs[off] = (byte)(n);
  147. bs[off + 1] = (byte)(n >> 8);
  148. }
  149. internal static ushort LE_To_UInt16(byte[] bs)
  150. {
  151. uint n = (uint)bs[0]
  152. | (uint)bs[1] << 8;
  153. return (ushort)n;
  154. }
  155. internal static ushort LE_To_UInt16(byte[] bs, int off)
  156. {
  157. uint n = (uint)bs[off]
  158. | (uint)bs[off + 1] << 8;
  159. return (ushort)n;
  160. }
  161. internal static byte[] UInt32_To_LE(uint n)
  162. {
  163. byte[] bs = new byte[4];
  164. UInt32_To_LE(n, bs, 0);
  165. return bs;
  166. }
  167. internal static void UInt32_To_LE(uint n, byte[] bs)
  168. {
  169. bs[0] = (byte)(n);
  170. bs[1] = (byte)(n >> 8);
  171. bs[2] = (byte)(n >> 16);
  172. bs[3] = (byte)(n >> 24);
  173. }
  174. internal static void UInt32_To_LE(uint n, byte[] bs, int off)
  175. {
  176. bs[off] = (byte)(n);
  177. bs[off + 1] = (byte)(n >> 8);
  178. bs[off + 2] = (byte)(n >> 16);
  179. bs[off + 3] = (byte)(n >> 24);
  180. }
  181. internal static byte[] UInt32_To_LE(uint[] ns)
  182. {
  183. byte[] bs = new byte[4 * ns.Length];
  184. UInt32_To_LE(ns, bs, 0);
  185. return bs;
  186. }
  187. internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off)
  188. {
  189. for (int i = 0; i < ns.Length; ++i)
  190. {
  191. UInt32_To_LE(ns[i], bs, off);
  192. off += 4;
  193. }
  194. }
  195. internal static uint LE_To_UInt32(byte[] bs)
  196. {
  197. return (uint)bs[0]
  198. | (uint)bs[1] << 8
  199. | (uint)bs[2] << 16
  200. | (uint)bs[3] << 24;
  201. }
  202. internal static uint LE_To_UInt32(byte[] bs, int off)
  203. {
  204. return (uint)bs[off]
  205. | (uint)bs[off + 1] << 8
  206. | (uint)bs[off + 2] << 16
  207. | (uint)bs[off + 3] << 24;
  208. }
  209. internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns)
  210. {
  211. for (int i = 0; i < ns.Length; ++i)
  212. {
  213. ns[i] = LE_To_UInt32(bs, off);
  214. off += 4;
  215. }
  216. }
  217. internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count)
  218. {
  219. for (int i = 0; i < count; ++i)
  220. {
  221. ns[nOff + i] = LE_To_UInt32(bs, bOff);
  222. bOff += 4;
  223. }
  224. }
  225. internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
  226. {
  227. uint[] ns = new uint[count];
  228. for (int i = 0; i < ns.Length; ++i)
  229. {
  230. ns[i] = LE_To_UInt32(bs, off);
  231. off += 4;
  232. }
  233. return ns;
  234. }
  235. internal static byte[] UInt64_To_LE(ulong n)
  236. {
  237. byte[] bs = new byte[8];
  238. UInt64_To_LE(n, bs, 0);
  239. return bs;
  240. }
  241. internal static void UInt64_To_LE(ulong n, byte[] bs)
  242. {
  243. UInt32_To_LE((uint)(n), bs);
  244. UInt32_To_LE((uint)(n >> 32), bs, 4);
  245. }
  246. internal static void UInt64_To_LE(ulong n, byte[] bs, int off)
  247. {
  248. UInt32_To_LE((uint)(n), bs, off);
  249. UInt32_To_LE((uint)(n >> 32), bs, off + 4);
  250. }
  251. internal static byte[] UInt64_To_LE(ulong[] ns)
  252. {
  253. byte[] bs = new byte[8 * ns.Length];
  254. UInt64_To_LE(ns, bs, 0);
  255. return bs;
  256. }
  257. internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off)
  258. {
  259. for (int i = 0; i < ns.Length; ++i)
  260. {
  261. UInt64_To_LE(ns[i], bs, off);
  262. off += 8;
  263. }
  264. }
  265. internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  266. {
  267. for (int i = 0; i < nsLen; ++i)
  268. {
  269. UInt64_To_LE(ns[nsOff + i], bs, bsOff);
  270. bsOff += 8;
  271. }
  272. }
  273. internal static ulong LE_To_UInt64(byte[] bs)
  274. {
  275. uint lo = LE_To_UInt32(bs);
  276. uint hi = LE_To_UInt32(bs, 4);
  277. return ((ulong)hi << 32) | (ulong)lo;
  278. }
  279. internal static ulong LE_To_UInt64(byte[] bs, int off)
  280. {
  281. uint lo = LE_To_UInt32(bs, off);
  282. uint hi = LE_To_UInt32(bs, off + 4);
  283. return ((ulong)hi << 32) | (ulong)lo;
  284. }
  285. internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns)
  286. {
  287. for (int i = 0; i < ns.Length; ++i)
  288. {
  289. ns[i] = LE_To_UInt64(bs, off);
  290. off += 8;
  291. }
  292. }
  293. internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
  294. {
  295. for (int i = 0; i < nsLen; ++i)
  296. {
  297. ns[nsOff + i] = LE_To_UInt64(bs, bsOff);
  298. bsOff += 8;
  299. }
  300. }
  301. }
  302. }
  303. #pragma warning restore
  304. #endif