Pack.cs 10.0 KB

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