Pack.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  5. using System.Buffers.Binary;
  6. #endif
  7. using System.Diagnostics;
  8. using System.Runtime.CompilerServices;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities
  10. {
  11. internal static class Pack
  12. {
  13. internal static void UInt16_To_BE(ushort n, byte[] bs)
  14. {
  15. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  16. BinaryPrimitives.WriteUInt16BigEndian(bs, n);
  17. #else
  18. bs[0] = (byte)(n >> 8);
  19. bs[1] = (byte)n;
  20. #endif
  21. }
  22. internal static void UInt16_To_BE(ushort n, byte[] bs, int off)
  23. {
  24. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  25. BinaryPrimitives.WriteUInt16BigEndian(bs.AsSpan(off), n);
  26. #else
  27. bs[off] = (byte)(n >> 8);
  28. bs[off + 1] = (byte)n;
  29. #endif
  30. }
  31. internal static void UInt16_To_BE(ushort[] ns, byte[] bs, int off)
  32. {
  33. for (int i = 0; i < ns.Length; ++i)
  34. {
  35. UInt16_To_BE(ns[i], bs, off);
  36. off += 2;
  37. }
  38. }
  39. internal static void UInt16_To_BE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  40. {
  41. for (int i = 0; i < nsLen; ++i)
  42. {
  43. UInt16_To_BE(ns[nsOff + i], bs, bsOff);
  44. bsOff += 2;
  45. }
  46. }
  47. internal static byte[] UInt16_To_BE(ushort n)
  48. {
  49. byte[] bs = new byte[2];
  50. UInt16_To_BE(n, bs, 0);
  51. return bs;
  52. }
  53. internal static byte[] UInt16_To_BE(ushort[] ns)
  54. {
  55. return UInt16_To_BE(ns, 0, ns.Length);
  56. }
  57. internal static byte[] UInt16_To_BE(ushort[] ns, int nsOff, int nsLen)
  58. {
  59. byte[] bs = new byte[2 * nsLen];
  60. UInt16_To_BE(ns, nsOff, nsLen, bs, 0);
  61. return bs;
  62. }
  63. internal static ushort BE_To_UInt16(byte[] bs, int off)
  64. {
  65. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  66. return BinaryPrimitives.ReadUInt16BigEndian(bs.AsSpan(off));
  67. #else
  68. uint n = (uint)bs[off] << 8
  69. | bs[off + 1];
  70. return (ushort)n;
  71. #endif
  72. }
  73. internal static void BE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff)
  74. {
  75. ns[nsOff] = BE_To_UInt16(bs, bsOff);
  76. }
  77. internal static ushort[] BE_To_UInt16(byte[] bs)
  78. {
  79. return BE_To_UInt16(bs, 0, bs.Length);
  80. }
  81. internal static ushort[] BE_To_UInt16(byte[] bs, int off, int len)
  82. {
  83. if ((len & 1) != 0)
  84. throw new ArgumentException("must be a multiple of 2", "len");
  85. ushort[] ns = new ushort[len / 2];
  86. for (int i = 0; i < len; i += 2)
  87. {
  88. BE_To_UInt16(bs, off + i, ns, i >> 1);
  89. }
  90. return ns;
  91. }
  92. internal static void UInt32_To_BE(uint n, byte[] bs)
  93. {
  94. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  95. BinaryPrimitives.WriteUInt32BigEndian(bs, n);
  96. #else
  97. bs[0] = (byte)(n >> 24);
  98. bs[1] = (byte)(n >> 16);
  99. bs[2] = (byte)(n >> 8);
  100. bs[3] = (byte)n;
  101. #endif
  102. }
  103. internal static void UInt32_To_BE(uint n, byte[] bs, int off)
  104. {
  105. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  106. BinaryPrimitives.WriteUInt32BigEndian(bs.AsSpan(off), n);
  107. #else
  108. bs[off] = (byte)(n >> 24);
  109. bs[off + 1] = (byte)(n >> 16);
  110. bs[off + 2] = (byte)(n >> 8);
  111. bs[off + 3] = (byte)n;
  112. #endif
  113. }
  114. internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
  115. {
  116. for (int i = 0; i < ns.Length; ++i)
  117. {
  118. UInt32_To_BE(ns[i], bs, off);
  119. off += 4;
  120. }
  121. }
  122. internal static void UInt32_To_BE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  123. {
  124. for (int i = 0; i < nsLen; ++i)
  125. {
  126. UInt32_To_BE(ns[nsOff + i], bs, bsOff);
  127. bsOff += 4;
  128. }
  129. }
  130. internal static byte[] UInt32_To_BE(uint n)
  131. {
  132. byte[] bs = new byte[4];
  133. UInt32_To_BE(n, bs, 0);
  134. return bs;
  135. }
  136. internal static byte[] UInt32_To_BE(uint[] ns)
  137. {
  138. byte[] bs = new byte[4 * ns.Length];
  139. UInt32_To_BE(ns, bs, 0);
  140. return bs;
  141. }
  142. internal static uint BE_To_UInt32(byte[] bs)
  143. {
  144. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  145. return BinaryPrimitives.ReadUInt32BigEndian(bs);
  146. #else
  147. return (uint)bs[0] << 24
  148. | (uint)bs[1] << 16
  149. | (uint)bs[2] << 8
  150. | bs[3];
  151. #endif
  152. }
  153. internal static uint BE_To_UInt32(byte[] bs, int off)
  154. {
  155. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  156. return BinaryPrimitives.ReadUInt32BigEndian(bs.AsSpan(off));
  157. #else
  158. return (uint)bs[off] << 24
  159. | (uint)bs[off + 1] << 16
  160. | (uint)bs[off + 2] << 8
  161. | bs[off + 3];
  162. #endif
  163. }
  164. internal static uint BE_To_UInt32_Partial(byte[] bs, int off, int len)
  165. {
  166. Debug.Assert(1 <= len && len <= 4);
  167. uint result = bs[off];
  168. for (int i = 1; i < len; ++i)
  169. {
  170. result <<= 8;
  171. result |= bs[off + i];
  172. }
  173. return result;
  174. }
  175. internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns)
  176. {
  177. for (int i = 0; i < ns.Length; ++i)
  178. {
  179. ns[i] = BE_To_UInt32(bs, off);
  180. off += 4;
  181. }
  182. }
  183. internal static void BE_To_UInt32(byte[] bs, int bsOff, uint[] ns, int nsOff, int nsLen)
  184. {
  185. for (int i = 0; i < nsLen; ++i)
  186. {
  187. ns[nsOff + i] = BE_To_UInt32(bs, bsOff);
  188. bsOff += 4;
  189. }
  190. }
  191. internal static byte[] UInt64_To_BE(ulong n)
  192. {
  193. byte[] bs = new byte[8];
  194. UInt64_To_BE(n, bs, 0);
  195. return bs;
  196. }
  197. internal static void UInt64_To_BE(ulong n, byte[] bs)
  198. {
  199. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  200. BinaryPrimitives.WriteUInt64BigEndian(bs, n);
  201. #else
  202. UInt32_To_BE((uint)(n >> 32), bs);
  203. UInt32_To_BE((uint)n, bs, 4);
  204. #endif
  205. }
  206. internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
  207. {
  208. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  209. BinaryPrimitives.WriteUInt64BigEndian(bs.AsSpan(off), n);
  210. #else
  211. UInt32_To_BE((uint)(n >> 32), bs, off);
  212. UInt32_To_BE((uint)n, bs, off + 4);
  213. #endif
  214. }
  215. internal static byte[] UInt64_To_BE(ulong[] ns)
  216. {
  217. byte[] bs = new byte[8 * ns.Length];
  218. UInt64_To_BE(ns, bs, 0);
  219. return bs;
  220. }
  221. internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off)
  222. {
  223. for (int i = 0; i < ns.Length; ++i)
  224. {
  225. UInt64_To_BE(ns[i], bs, off);
  226. off += 8;
  227. }
  228. }
  229. internal static void UInt64_To_BE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  230. {
  231. for (int i = 0; i < nsLen; ++i)
  232. {
  233. UInt64_To_BE(ns[nsOff + i], bs, bsOff);
  234. bsOff += 8;
  235. }
  236. }
  237. internal static ulong BE_To_UInt64(byte[] bs)
  238. {
  239. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  240. return BinaryPrimitives.ReadUInt64BigEndian(bs);
  241. #else
  242. uint hi = BE_To_UInt32(bs);
  243. uint lo = BE_To_UInt32(bs, 4);
  244. return ((ulong)hi << 32) | (ulong)lo;
  245. #endif
  246. }
  247. internal static ulong BE_To_UInt64(byte[] bs, int off)
  248. {
  249. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  250. return BinaryPrimitives.ReadUInt64BigEndian(bs.AsSpan(off));
  251. #else
  252. uint hi = BE_To_UInt32(bs, off);
  253. uint lo = BE_To_UInt32(bs, off + 4);
  254. return ((ulong)hi << 32) | (ulong)lo;
  255. #endif
  256. }
  257. internal static ulong BE_To_UInt64_Partial(byte[] bs, int off, int len)
  258. {
  259. Debug.Assert(1 <= len && len <= 8);
  260. ulong result = bs[off];
  261. for (int i = 1; i < len; ++i)
  262. {
  263. result <<= 8;
  264. result |= bs[off + i];
  265. }
  266. return result;
  267. }
  268. internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns)
  269. {
  270. for (int i = 0; i < ns.Length; ++i)
  271. {
  272. ns[i] = BE_To_UInt64(bs, off);
  273. off += 8;
  274. }
  275. }
  276. internal static void BE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
  277. {
  278. for (int i = 0; i < nsLen; ++i)
  279. {
  280. ns[nsOff + i] = BE_To_UInt64(bs, bsOff);
  281. bsOff += 8;
  282. }
  283. }
  284. internal static void UInt16_To_LE(ushort n, byte[] bs)
  285. {
  286. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  287. BinaryPrimitives.WriteUInt16LittleEndian(bs, n);
  288. #else
  289. bs[0] = (byte)n;
  290. bs[1] = (byte)(n >> 8);
  291. #endif
  292. }
  293. internal static void UInt16_To_LE(ushort n, byte[] bs, int off)
  294. {
  295. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  296. BinaryPrimitives.WriteUInt16LittleEndian(bs.AsSpan(off), n);
  297. #else
  298. bs[off] = (byte)n;
  299. bs[off + 1] = (byte)(n >> 8);
  300. #endif
  301. }
  302. internal static byte[] UInt16_To_LE(ushort n)
  303. {
  304. byte[] bs = new byte[2];
  305. UInt16_To_LE(n, bs, 0);
  306. return bs;
  307. }
  308. internal static ushort LE_To_UInt16(byte[] bs)
  309. {
  310. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  311. return BinaryPrimitives.ReadUInt16LittleEndian(bs);
  312. #else
  313. uint n = bs[0]
  314. | (uint)bs[1] << 8;
  315. return (ushort)n;
  316. #endif
  317. }
  318. internal static ushort LE_To_UInt16(byte[] bs, int off)
  319. {
  320. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  321. return BinaryPrimitives.ReadUInt16LittleEndian(bs.AsSpan(off));
  322. #else
  323. uint n = bs[off]
  324. | (uint)bs[off + 1] << 8;
  325. return (ushort)n;
  326. #endif
  327. }
  328. internal static byte[] UInt32_To_LE(uint n)
  329. {
  330. byte[] bs = new byte[4];
  331. UInt32_To_LE(n, bs, 0);
  332. return bs;
  333. }
  334. internal static void UInt32_To_LE(uint n, byte[] bs)
  335. {
  336. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  337. BinaryPrimitives.WriteUInt32LittleEndian(bs, n);
  338. #else
  339. bs[0] = (byte)n;
  340. bs[1] = (byte)(n >> 8);
  341. bs[2] = (byte)(n >> 16);
  342. bs[3] = (byte)(n >> 24);
  343. #endif
  344. }
  345. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  346. internal static void UInt32_To_LE(uint n, byte[] bs, int off)
  347. {
  348. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  349. BinaryPrimitives.WriteUInt32LittleEndian(bs.AsSpan(off), n);
  350. #else
  351. bs[off] = (byte)n;
  352. bs[off + 1] = (byte)(n >> 8);
  353. bs[off + 2] = (byte)(n >> 16);
  354. bs[off + 3] = (byte)(n >> 24);
  355. #endif
  356. }
  357. internal static byte[] UInt32_To_LE(uint[] ns)
  358. {
  359. byte[] bs = new byte[4 * ns.Length];
  360. UInt32_To_LE(ns, bs, 0);
  361. return bs;
  362. }
  363. internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off)
  364. {
  365. for (int i = 0; i < ns.Length; ++i)
  366. {
  367. UInt32_To_LE(ns[i], bs, off);
  368. off += 4;
  369. }
  370. }
  371. internal static void UInt32_To_LE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  372. {
  373. for (int i = 0; i < nsLen; ++i)
  374. {
  375. UInt32_To_LE(ns[nsOff + i], bs, bsOff);
  376. bsOff += 4;
  377. }
  378. }
  379. internal static uint LE_To_UInt24(byte[] bs, int off)
  380. {
  381. return bs[off]
  382. | (uint)bs[off + 1] << 8
  383. | (uint)bs[off + 2] << 16;
  384. }
  385. internal static uint LE_To_UInt32(byte[] bs)
  386. {
  387. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  388. return BinaryPrimitives.ReadUInt32LittleEndian(bs);
  389. #else
  390. return bs[0]
  391. | (uint)bs[1] << 8
  392. | (uint)bs[2] << 16
  393. | (uint)bs[3] << 24;
  394. #endif
  395. }
  396. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  397. internal static uint LE_To_UInt32(byte[] bs, int off)
  398. {
  399. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  400. return BinaryPrimitives.ReadUInt32LittleEndian(bs.AsSpan(off));
  401. #else
  402. return bs[off]
  403. | (uint)bs[off + 1] << 8
  404. | (uint)bs[off + 2] << 16
  405. | (uint)bs[off + 3] << 24;
  406. #endif
  407. }
  408. internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns)
  409. {
  410. for (int i = 0; i < ns.Length; ++i)
  411. {
  412. ns[i] = LE_To_UInt32(bs, off);
  413. off += 4;
  414. }
  415. }
  416. internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count)
  417. {
  418. for (int i = 0; i < count; ++i)
  419. {
  420. ns[nOff + i] = LE_To_UInt32(bs, bOff);
  421. bOff += 4;
  422. }
  423. }
  424. internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
  425. {
  426. uint[] ns = new uint[count];
  427. for (int i = 0; i < ns.Length; ++i)
  428. {
  429. ns[i] = LE_To_UInt32(bs, off);
  430. off += 4;
  431. }
  432. return ns;
  433. }
  434. internal static byte[] UInt64_To_LE(ulong n)
  435. {
  436. byte[] bs = new byte[8];
  437. UInt64_To_LE(n, bs, 0);
  438. return bs;
  439. }
  440. internal static void UInt64_To_LE(ulong n, byte[] bs)
  441. {
  442. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  443. BinaryPrimitives.WriteUInt64LittleEndian(bs, n);
  444. #else
  445. UInt32_To_LE((uint)n, bs);
  446. UInt32_To_LE((uint)(n >> 32), bs, 4);
  447. #endif
  448. }
  449. internal static void UInt64_To_LE(ulong n, byte[] bs, int off)
  450. {
  451. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  452. BinaryPrimitives.WriteUInt64LittleEndian(bs.AsSpan(off), n);
  453. #else
  454. UInt32_To_LE((uint)n, bs, off);
  455. UInt32_To_LE((uint)(n >> 32), bs, off + 4);
  456. #endif
  457. }
  458. internal static byte[] UInt64_To_LE(ulong[] ns)
  459. {
  460. byte[] bs = new byte[8 * ns.Length];
  461. UInt64_To_LE(ns, bs, 0);
  462. return bs;
  463. }
  464. internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off)
  465. {
  466. for (int i = 0; i < ns.Length; ++i)
  467. {
  468. UInt64_To_LE(ns[i], bs, off);
  469. off += 8;
  470. }
  471. }
  472. internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
  473. {
  474. for (int i = 0; i < nsLen; ++i)
  475. {
  476. UInt64_To_LE(ns[nsOff + i], bs, bsOff);
  477. bsOff += 8;
  478. }
  479. }
  480. internal static ulong LE_To_UInt64(byte[] bs)
  481. {
  482. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  483. return BinaryPrimitives.ReadUInt64LittleEndian(bs);
  484. #else
  485. uint lo = LE_To_UInt32(bs);
  486. uint hi = LE_To_UInt32(bs, 4);
  487. return ((ulong)hi << 32) | (ulong)lo;
  488. #endif
  489. }
  490. internal static ulong LE_To_UInt64(byte[] bs, int off)
  491. {
  492. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  493. return BinaryPrimitives.ReadUInt64LittleEndian(bs.AsSpan(off));
  494. #else
  495. uint lo = LE_To_UInt32(bs, off);
  496. uint hi = LE_To_UInt32(bs, off + 4);
  497. return ((ulong)hi << 32) | (ulong)lo;
  498. #endif
  499. }
  500. internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns)
  501. {
  502. for (int i = 0; i < ns.Length; ++i)
  503. {
  504. ns[i] = LE_To_UInt64(bs, off);
  505. off += 8;
  506. }
  507. }
  508. internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
  509. {
  510. for (int i = 0; i < nsLen; ++i)
  511. {
  512. ns[nsOff + i] = LE_To_UInt64(bs, bsOff);
  513. bsOff += 8;
  514. }
  515. }
  516. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  517. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  518. internal static uint BE_To_UInt32(ReadOnlySpan<byte> bs)
  519. {
  520. return BinaryPrimitives.ReadUInt32BigEndian(bs);
  521. }
  522. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  523. internal static void BE_To_UInt32(ReadOnlySpan<byte> bs, Span<uint> ns)
  524. {
  525. for (int i = 0; i < ns.Length; ++i)
  526. {
  527. ns[i] = BE_To_UInt32(bs);
  528. bs = bs[4..];
  529. }
  530. }
  531. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  532. internal static uint BE_To_UInt32_Partial(ReadOnlySpan<byte> bs)
  533. {
  534. int len = bs.Length;
  535. Debug.Assert(1 <= len && len <= 4);
  536. uint result = bs[0];
  537. for (int i = 1; i < len; ++i)
  538. {
  539. result <<= 8;
  540. result |= bs[i];
  541. }
  542. return result;
  543. }
  544. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  545. internal static ulong BE_To_UInt64(ReadOnlySpan<byte> bs)
  546. {
  547. return BinaryPrimitives.ReadUInt64BigEndian(bs);
  548. }
  549. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  550. internal static void BE_To_UInt64(ReadOnlySpan<byte> bs, Span<ulong> ns)
  551. {
  552. for (int i = 0; i < ns.Length; ++i)
  553. {
  554. ns[i] = BE_To_UInt64(bs);
  555. bs = bs[8..];
  556. }
  557. }
  558. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  559. internal static ulong BE_To_UInt64_Partial(ReadOnlySpan<byte> bs)
  560. {
  561. int len = bs.Length;
  562. Debug.Assert(1 <= len && len <= 8);
  563. ulong result = bs[0];
  564. for (int i = 1; i < len; ++i)
  565. {
  566. result <<= 8;
  567. result |= bs[i];
  568. }
  569. return result;
  570. }
  571. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  572. internal static ushort LE_To_UInt16(ReadOnlySpan<byte> bs)
  573. {
  574. return BinaryPrimitives.ReadUInt16LittleEndian(bs);
  575. }
  576. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  577. internal static uint LE_To_UInt32(ReadOnlySpan<byte> bs)
  578. {
  579. return BinaryPrimitives.ReadUInt32LittleEndian(bs);
  580. }
  581. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  582. internal static void LE_To_UInt32(ReadOnlySpan<byte> bs, Span<uint> ns)
  583. {
  584. for (int i = 0; i < ns.Length; ++i)
  585. {
  586. ns[i] = LE_To_UInt32(bs);
  587. bs = bs[4..];
  588. }
  589. }
  590. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  591. internal static ulong LE_To_UInt64(ReadOnlySpan<byte> bs)
  592. {
  593. return BinaryPrimitives.ReadUInt64LittleEndian(bs);
  594. }
  595. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  596. internal static void LE_To_UInt64(ReadOnlySpan<byte> bs, Span<ulong> ns)
  597. {
  598. for (int i = 0; i < ns.Length; ++i)
  599. {
  600. ns[i] = LE_To_UInt64(bs);
  601. bs = bs[8..];
  602. }
  603. }
  604. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  605. internal static void UInt16_To_BE(ushort n, Span<byte> bs)
  606. {
  607. BinaryPrimitives.WriteUInt16BigEndian(bs, n);
  608. }
  609. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  610. internal static void UInt16_To_LE(ushort n, Span<byte> bs)
  611. {
  612. BinaryPrimitives.WriteUInt16LittleEndian(bs, n);
  613. }
  614. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  615. internal static void UInt32_To_BE(uint n, Span<byte> bs)
  616. {
  617. BinaryPrimitives.WriteUInt32BigEndian(bs, n);
  618. }
  619. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  620. internal static void UInt32_To_BE(ReadOnlySpan<uint> ns, Span<byte> bs)
  621. {
  622. for (int i = 0; i < ns.Length; ++i)
  623. {
  624. UInt32_To_BE(ns[i], bs);
  625. bs = bs[4..];
  626. }
  627. }
  628. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  629. internal static void UInt32_To_LE(uint n, Span<byte> bs)
  630. {
  631. BinaryPrimitives.WriteUInt32LittleEndian(bs, n);
  632. }
  633. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  634. internal static void UInt32_To_LE(ReadOnlySpan<uint> ns, Span<byte> bs)
  635. {
  636. for (int i = 0; i < ns.Length; ++i)
  637. {
  638. UInt32_To_LE(ns[i], bs);
  639. bs = bs[4..];
  640. }
  641. }
  642. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  643. internal static void UInt64_To_BE(ulong n, Span<byte> bs)
  644. {
  645. BinaryPrimitives.WriteUInt64BigEndian(bs, n);
  646. }
  647. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  648. internal static void UInt64_To_BE(ReadOnlySpan<ulong> ns, Span<byte> bs)
  649. {
  650. for (int i = 0; i < ns.Length; ++i)
  651. {
  652. UInt64_To_BE(ns[i], bs);
  653. bs = bs[8..];
  654. }
  655. }
  656. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  657. internal static void UInt64_To_LE(ulong n, Span<byte> bs)
  658. {
  659. BinaryPrimitives.WriteUInt64LittleEndian(bs, n);
  660. }
  661. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  662. internal static void UInt64_To_LE(ReadOnlySpan<ulong> ns, Span<byte> bs)
  663. {
  664. for (int i = 0; i < ns.Length; ++i)
  665. {
  666. UInt64_To_LE(ns[i], bs);
  667. bs = bs[8..];
  668. }
  669. }
  670. #endif
  671. }
  672. }
  673. #pragma warning restore
  674. #endif