Pack.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.NullChecks, false)]
  7. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.ArrayBoundsChecks, false)]
  8. [BestHTTP.PlatformSupport.IL2CPP.Il2CppSetOption(BestHTTP.PlatformSupport.IL2CPP.Option.DivideByZeroChecks, false)]
  9. internal static class Pack
  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. // for (int i = 0; i < ns.Length; ++i)
  24. // {
  25. // UInt16_To_BE(ns[i], bs, off);
  26. // off += 2;
  27. // }
  28. //}
  29. internal static void UInt16_To_BE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  30. {
  31. for (int i = 0; i < nsLen; ++i)
  32. {
  33. UInt16_To_BE(ns[nsOff + i], bs, bsOff);
  34. bsOff += 2;
  35. }
  36. }
  37. internal static byte[] UInt16_To_BE(ushort n)
  38. {
  39. byte[] bs = new byte[2];
  40. UInt16_To_BE(n, bs, 0);
  41. return bs;
  42. }
  43. internal static byte[] UInt16_To_BE(ushort[] ns)
  44. {
  45. return UInt16_To_BE(ns, 0, ns.Length);
  46. }
  47. internal static byte[] UInt16_To_BE(ushort[] ns, int nsOff, int nsLen)
  48. {
  49. byte[] bs = new byte[2 * nsLen];
  50. UInt16_To_BE(ns, nsOff, nsLen, bs, 0);
  51. return bs;
  52. }
  53. internal static ushort BE_To_UInt16(byte[] bs, int off)
  54. {
  55. uint n = (uint)bs[off] << 8
  56. | (uint)bs[off + 1];
  57. return (ushort)n;
  58. }
  59. internal static void BE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff)
  60. {
  61. ns[nsOff] = BE_To_UInt16(bs, bsOff);
  62. }
  63. internal static ushort[] BE_To_UInt16(byte[] bs)
  64. {
  65. return BE_To_UInt16(bs, 0, bs.Length);
  66. }
  67. internal static ushort[] BE_To_UInt16(byte[] bs, int off, int len)
  68. {
  69. if ((len & 1) != 0)
  70. throw new ArgumentException("must be a multiple of 2", "len");
  71. ushort[] ns = new ushort[len / 2];
  72. for (int i = 0; i < len; i += 2)
  73. {
  74. BE_To_UInt16(bs, off + i, ns, i >> 1);
  75. }
  76. return ns;
  77. }
  78. internal static void UInt32_To_BE(uint n, byte[] bs)
  79. {
  80. bs[0] = (byte)(n >> 24);
  81. bs[1] = (byte)(n >> 16);
  82. bs[2] = (byte)(n >> 8);
  83. bs[3] = (byte)(n);
  84. }
  85. internal static void UInt32_To_BE(uint n, byte[] bs, int off)
  86. {
  87. bs[off] = (byte)(n >> 24);
  88. bs[off + 1] = (byte)(n >> 16);
  89. bs[off + 2] = (byte)(n >> 8);
  90. bs[off + 3] = (byte)(n);
  91. }
  92. internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
  93. {
  94. for (int i = 0; i < ns.Length; ++i)
  95. {
  96. UInt32_To_BE(ns[i], bs, off);
  97. off += 4;
  98. }
  99. }
  100. internal static void UInt32_To_BE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  101. {
  102. for (int i = 0; i < nsLen; ++i)
  103. {
  104. UInt32_To_BE(ns[nsOff + i], bs, bsOff);
  105. bsOff += 4;
  106. }
  107. }
  108. internal static byte[] UInt32_To_BE(uint n)
  109. {
  110. byte[] bs = new byte[4];
  111. UInt32_To_BE(n, bs, 0);
  112. return bs;
  113. }
  114. internal static byte[] UInt32_To_BE(uint[] ns)
  115. {
  116. byte[] bs = new byte[4 * ns.Length];
  117. UInt32_To_BE(ns, bs, 0);
  118. return bs;
  119. }
  120. internal static uint BE_To_UInt32(byte[] bs)
  121. {
  122. return (uint)bs[0] << 24
  123. | (uint)bs[1] << 16
  124. | (uint)bs[2] << 8
  125. | (uint)bs[3];
  126. }
  127. internal static uint BE_To_UInt32(byte[] bs, int off)
  128. {
  129. return (uint)bs[off] << 24
  130. | (uint)bs[off + 1] << 16
  131. | (uint)bs[off + 2] << 8
  132. | (uint)bs[off + 3];
  133. }
  134. internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns)
  135. {
  136. for (int i = 0; i < ns.Length; ++i)
  137. {
  138. ns[i] = BE_To_UInt32(bs, off);
  139. off += 4;
  140. }
  141. }
  142. internal static void BE_To_UInt32(byte[] bs, int bsOff, uint[] ns, int nsOff, int nsLen)
  143. {
  144. for (int i = 0; i < nsLen; ++i)
  145. {
  146. ns[nsOff + i] = BE_To_UInt32(bs, bsOff);
  147. bsOff += 4;
  148. }
  149. }
  150. internal static byte[] UInt64_To_BE(ulong n)
  151. {
  152. byte[] bs = new byte[8];
  153. UInt64_To_BE(n, bs, 0);
  154. return bs;
  155. }
  156. internal static void UInt64_To_BE(ulong n, byte[] bs)
  157. {
  158. UInt32_To_BE((uint)(n >> 32), bs);
  159. UInt32_To_BE((uint)(n), bs, 4);
  160. }
  161. internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
  162. {
  163. UInt32_To_BE((uint)(n >> 32), bs, off);
  164. UInt32_To_BE((uint)(n), bs, off + 4);
  165. }
  166. internal static byte[] UInt64_To_BE(ulong[] ns)
  167. {
  168. byte[] bs = new byte[8 * ns.Length];
  169. UInt64_To_BE(ns, bs, 0);
  170. return bs;
  171. }
  172. internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off)
  173. {
  174. for (int i = 0; i < ns.Length; ++i)
  175. {
  176. UInt64_To_BE(ns[i], bs, off);
  177. off += 8;
  178. }
  179. }
  180. internal static void UInt64_To_BE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  181. {
  182. for (int i = 0; i < nsLen; ++i)
  183. {
  184. UInt64_To_BE(ns[nsOff + i], bs, bsOff);
  185. bsOff += 8;
  186. }
  187. }
  188. internal static ulong BE_To_UInt64(byte[] bs, int off)
  189. {
  190. uint hi = BE_To_UInt32(bs, off);
  191. uint lo = BE_To_UInt32(bs, off + 4);
  192. return ((ulong)hi << 32) | (ulong)lo;
  193. }
  194. internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns)
  195. {
  196. for (int i = 0; i < ns.Length; ++i)
  197. {
  198. ns[i] = BE_To_UInt64(bs, off);
  199. off += 8;
  200. }
  201. }
  202. internal static void BE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
  203. {
  204. for (int i = 0; i < nsLen; ++i)
  205. {
  206. ns[nsOff + i] = BE_To_UInt64(bs, bsOff);
  207. bsOff += 8;
  208. }
  209. }
  210. internal static void UInt16_To_LE(ushort n, byte[] bs)
  211. {
  212. bs[0] = (byte)(n);
  213. bs[1] = (byte)(n >> 8);
  214. }
  215. internal static void UInt16_To_LE(ushort n, byte[] bs, int off)
  216. {
  217. bs[off] = (byte)(n);
  218. bs[off + 1] = (byte)(n >> 8);
  219. }
  220. internal static ushort LE_To_UInt16(byte[] bs)
  221. {
  222. uint n = (uint)bs[0]
  223. | (uint)bs[1] << 8;
  224. return (ushort)n;
  225. }
  226. internal static ushort LE_To_UInt16(byte[] bs, int off)
  227. {
  228. uint n = (uint)bs[off]
  229. | (uint)bs[off + 1] << 8;
  230. return (ushort)n;
  231. }
  232. internal static byte[] UInt32_To_LE(uint n)
  233. {
  234. byte[] bs = new byte[4];
  235. UInt32_To_LE(n, bs, 0);
  236. return bs;
  237. }
  238. internal static void UInt32_To_LE(uint n, byte[] bs)
  239. {
  240. bs[0] = (byte)(n);
  241. bs[1] = (byte)(n >> 8);
  242. bs[2] = (byte)(n >> 16);
  243. bs[3] = (byte)(n >> 24);
  244. }
  245. internal static void UInt32_To_LE(uint n, byte[] bs, int off)
  246. {
  247. bs[off] = (byte)(n);
  248. bs[off + 1] = (byte)(n >> 8);
  249. bs[off + 2] = (byte)(n >> 16);
  250. bs[off + 3] = (byte)(n >> 24);
  251. }
  252. internal static byte[] UInt32_To_LE(uint[] ns)
  253. {
  254. byte[] bs = new byte[4 * ns.Length];
  255. UInt32_To_LE(ns, bs, 0);
  256. return bs;
  257. }
  258. internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off)
  259. {
  260. for (int i = 0; i < ns.Length; ++i)
  261. {
  262. UInt32_To_LE(ns[i], bs, off);
  263. off += 4;
  264. }
  265. }
  266. internal static void UInt32_To_LE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  267. {
  268. for (int i = 0; i < nsLen; ++i)
  269. {
  270. UInt32_To_LE(ns[nsOff + i], bs, bsOff);
  271. bsOff += 4;
  272. }
  273. }
  274. internal static uint LE_To_UInt32(byte[] bs)
  275. {
  276. return (uint)bs[0]
  277. | (uint)bs[1] << 8
  278. | (uint)bs[2] << 16
  279. | (uint)bs[3] << 24;
  280. }
  281. internal static uint LE_To_UInt32(byte[] bs, int off)
  282. {
  283. return (uint)bs[off]
  284. | (uint)bs[off + 1] << 8
  285. | (uint)bs[off + 2] << 16
  286. | (uint)bs[off + 3] << 24;
  287. }
  288. internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns)
  289. {
  290. for (int i = 0; i < ns.Length; ++i)
  291. {
  292. ns[i] = LE_To_UInt32(bs, off);
  293. off += 4;
  294. }
  295. }
  296. internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count)
  297. {
  298. for (int i = 0; i < count; ++i)
  299. {
  300. ns[nOff + i] = LE_To_UInt32(bs, bOff);
  301. bOff += 4;
  302. }
  303. }
  304. internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
  305. {
  306. uint[] ns = new uint[count];
  307. for (int i = 0; i < ns.Length; ++i)
  308. {
  309. ns[i] = LE_To_UInt32(bs, off);
  310. off += 4;
  311. }
  312. return ns;
  313. }
  314. internal static byte[] UInt64_To_LE(ulong n)
  315. {
  316. byte[] bs = new byte[8];
  317. UInt64_To_LE(n, bs, 0);
  318. return bs;
  319. }
  320. internal static void UInt64_To_LE(ulong n, byte[] bs)
  321. {
  322. UInt32_To_LE((uint)(n), bs);
  323. UInt32_To_LE((uint)(n >> 32), bs, 4);
  324. }
  325. internal static void UInt64_To_LE(ulong n, byte[] bs, int off)
  326. {
  327. UInt32_To_LE((uint)(n), bs, off);
  328. UInt32_To_LE((uint)(n >> 32), bs, off + 4);
  329. }
  330. internal static byte[] UInt64_To_LE(ulong[] ns)
  331. {
  332. byte[] bs = new byte[8 * ns.Length];
  333. UInt64_To_LE(ns, bs, 0);
  334. return bs;
  335. }
  336. internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off)
  337. {
  338. for (int i = 0; i < ns.Length; ++i)
  339. {
  340. UInt64_To_LE(ns[i], bs, off);
  341. off += 8;
  342. }
  343. }
  344. internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  345. {
  346. for (int i = 0; i < nsLen; ++i)
  347. {
  348. UInt64_To_LE(ns[nsOff + i], bs, bsOff);
  349. bsOff += 8;
  350. }
  351. }
  352. internal static ulong LE_To_UInt64(byte[] bs)
  353. {
  354. uint lo = LE_To_UInt32(bs);
  355. uint hi = LE_To_UInt32(bs, 4);
  356. return ((ulong)hi << 32) | (ulong)lo;
  357. }
  358. internal static ulong LE_To_UInt64(byte[] bs, int off)
  359. {
  360. uint lo = LE_To_UInt32(bs, off);
  361. uint hi = LE_To_UInt32(bs, off + 4);
  362. return ((ulong)hi << 32) | (ulong)lo;
  363. }
  364. internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns)
  365. {
  366. for (int i = 0; i < ns.Length; ++i)
  367. {
  368. ns[i] = LE_To_UInt64(bs, off);
  369. off += 8;
  370. }
  371. }
  372. internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
  373. {
  374. for (int i = 0; i < nsLen; ++i)
  375. {
  376. ns[nsOff + i] = LE_To_UInt64(bs, bsOff);
  377. bsOff += 8;
  378. }
  379. }
  380. }
  381. }
  382. #pragma warning restore
  383. #endif