Arrays.cs 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities
  7. {
  8. /// <summary> General array utilities.</summary>
  9. public static class Arrays
  10. {
  11. public static readonly byte[] EmptyBytes = new byte[0];
  12. public static readonly int[] EmptyInts = new int[0];
  13. public static bool AreAllZeroes(byte[] buf, int off, int len)
  14. {
  15. uint bits = 0;
  16. for (int i = 0; i < len; ++i)
  17. {
  18. bits |= buf[off + i];
  19. }
  20. return bits == 0;
  21. }
  22. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  23. public static bool AreAllZeroes(ReadOnlySpan<byte> buf)
  24. {
  25. uint bits = 0;
  26. for (int i = 0; i < buf.Length; ++i)
  27. {
  28. bits |= buf[i];
  29. }
  30. return bits == 0;
  31. }
  32. #endif
  33. public static bool AreEqual(
  34. bool[] a,
  35. bool[] b)
  36. {
  37. if (a == b)
  38. return true;
  39. if (a == null || b == null)
  40. return false;
  41. return HaveSameContents(a, b);
  42. }
  43. public static bool AreEqual(
  44. char[] a,
  45. char[] b)
  46. {
  47. if (a == b)
  48. return true;
  49. if (a == null || b == null)
  50. return false;
  51. return HaveSameContents(a, b);
  52. }
  53. /// <summary>
  54. /// Are two arrays equal.
  55. /// </summary>
  56. /// <param name="a">Left side.</param>
  57. /// <param name="b">Right side.</param>
  58. /// <returns>True if equal.</returns>
  59. public static bool AreEqual(byte[] a, byte[] b)
  60. {
  61. if (a == b)
  62. return true;
  63. if (a == null || b == null)
  64. return false;
  65. return HaveSameContents(a, b);
  66. }
  67. public static bool AreEqual(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)
  68. {
  69. int aLength = aToIndex - aFromIndex;
  70. int bLength = bToIndex - bFromIndex;
  71. if (aLength != bLength)
  72. return false;
  73. for (int i = 0; i < aLength; ++i)
  74. {
  75. if (a[aFromIndex + i] != b[bFromIndex + i])
  76. return false;
  77. }
  78. return true;
  79. }
  80. /// <summary>
  81. /// A constant time equals comparison - does not terminate early if
  82. /// test will fail.
  83. /// </summary>
  84. /// <param name="a">first array</param>
  85. /// <param name="b">second array</param>
  86. /// <returns>true if arrays equal, false otherwise.</returns>
  87. public static bool ConstantTimeAreEqual(byte[] a, byte[] b)
  88. {
  89. if (null == a || null == b)
  90. return false;
  91. if (a == b)
  92. return true;
  93. int len = System.Math.Min(a.Length, b.Length);
  94. int nonEqual = a.Length ^ b.Length;
  95. for (int i = 0; i < len; ++i)
  96. {
  97. nonEqual |= (a[i] ^ b[i]);
  98. }
  99. for (int i = len; i < b.Length; ++i)
  100. {
  101. nonEqual |= (b[i] ^ ~b[i]);
  102. }
  103. return 0 == nonEqual;
  104. }
  105. public static bool ConstantTimeAreEqual(int len, byte[] a, int aOff, byte[] b, int bOff)
  106. {
  107. if (null == a)
  108. throw new ArgumentNullException("a");
  109. if (null == b)
  110. throw new ArgumentNullException("b");
  111. if (len < 0)
  112. throw new ArgumentException("cannot be negative", "len");
  113. if (aOff > (a.Length - len))
  114. throw new IndexOutOfRangeException("'aOff' value invalid for specified length");
  115. if (bOff > (b.Length - len))
  116. throw new IndexOutOfRangeException("'bOff' value invalid for specified length");
  117. int d = 0;
  118. for (int i = 0; i < len; ++i)
  119. {
  120. d |= a[aOff + i] ^ b[bOff + i];
  121. }
  122. return 0 == d;
  123. }
  124. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  125. public static bool ConstantTimeAreEqual(Span<byte> a, Span<byte> b)
  126. {
  127. if (a.Length != b.Length)
  128. throw new ArgumentException("Spans to compare must have equal length");
  129. int d = 0;
  130. for (int i = 0, count = a.Length; i < count; ++i)
  131. {
  132. d |= a[i] ^ b[i];
  133. }
  134. return 0 == d;
  135. }
  136. #endif
  137. public static bool AreEqual(
  138. int[] a,
  139. int[] b)
  140. {
  141. if (a == b)
  142. return true;
  143. if (a == null || b == null)
  144. return false;
  145. return HaveSameContents(a, b);
  146. }
  147. [CLSCompliant(false)]
  148. public static bool AreEqual(uint[] a, uint[] b)
  149. {
  150. if (a == b)
  151. return true;
  152. if (a == null || b == null)
  153. return false;
  154. return HaveSameContents(a, b);
  155. }
  156. public static bool AreEqual(long[] a, long[] b)
  157. {
  158. if (a == b)
  159. return true;
  160. if (a == null || b == null)
  161. return false;
  162. return HaveSameContents(a, b);
  163. }
  164. [CLSCompliant(false)]
  165. public static bool AreEqual(ulong[] a, ulong[] b)
  166. {
  167. if (a == b)
  168. return true;
  169. if (a == null || b == null)
  170. return false;
  171. return HaveSameContents(a, b);
  172. }
  173. private static bool HaveSameContents(
  174. bool[] a,
  175. bool[] b)
  176. {
  177. int i = a.Length;
  178. if (i != b.Length)
  179. return false;
  180. while (i != 0)
  181. {
  182. --i;
  183. if (a[i] != b[i])
  184. return false;
  185. }
  186. return true;
  187. }
  188. private static bool HaveSameContents(
  189. char[] a,
  190. char[] b)
  191. {
  192. int i = a.Length;
  193. if (i != b.Length)
  194. return false;
  195. while (i != 0)
  196. {
  197. --i;
  198. if (a[i] != b[i])
  199. return false;
  200. }
  201. return true;
  202. }
  203. private static bool HaveSameContents(
  204. byte[] a,
  205. byte[] b)
  206. {
  207. int i = a.Length;
  208. if (i != b.Length)
  209. return false;
  210. while (i != 0)
  211. {
  212. --i;
  213. if (a[i] != b[i])
  214. return false;
  215. }
  216. return true;
  217. }
  218. private static bool HaveSameContents(
  219. int[] a,
  220. int[] b)
  221. {
  222. int i = a.Length;
  223. if (i != b.Length)
  224. return false;
  225. while (i != 0)
  226. {
  227. --i;
  228. if (a[i] != b[i])
  229. return false;
  230. }
  231. return true;
  232. }
  233. private static bool HaveSameContents(uint[] a, uint[] b)
  234. {
  235. int i = a.Length;
  236. if (i != b.Length)
  237. return false;
  238. while (i != 0)
  239. {
  240. --i;
  241. if (a[i] != b[i])
  242. return false;
  243. }
  244. return true;
  245. }
  246. private static bool HaveSameContents(long[] a, long[] b)
  247. {
  248. int i = a.Length;
  249. if (i != b.Length)
  250. return false;
  251. while (i != 0)
  252. {
  253. --i;
  254. if (a[i] != b[i])
  255. return false;
  256. }
  257. return true;
  258. }
  259. private static bool HaveSameContents(ulong[] a, ulong[] b)
  260. {
  261. int i = a.Length;
  262. if (i != b.Length)
  263. return false;
  264. while (i != 0)
  265. {
  266. --i;
  267. if (a[i] != b[i])
  268. return false;
  269. }
  270. return true;
  271. }
  272. public static string ToString(
  273. object[] a)
  274. {
  275. StringBuilder sb = new StringBuilder("[");
  276. if (a.Length > 0)
  277. {
  278. sb.Append(a[0]);
  279. for (int index = 1; index < a.Length; ++index)
  280. {
  281. sb.Append(", ").Append(a[index]);
  282. }
  283. }
  284. sb.Append(']');
  285. return sb.ToString();
  286. }
  287. public static int GetHashCode(byte[] data)
  288. {
  289. if (data == null)
  290. {
  291. return 0;
  292. }
  293. int i = data.Length;
  294. int hc = i + 1;
  295. while (--i >= 0)
  296. {
  297. hc *= 257;
  298. hc ^= data[i];
  299. }
  300. return hc;
  301. }
  302. public static int GetHashCode(byte[] data, int off, int len)
  303. {
  304. if (data == null)
  305. {
  306. return 0;
  307. }
  308. int i = len;
  309. int hc = i + 1;
  310. while (--i >= 0)
  311. {
  312. hc *= 257;
  313. hc ^= data[off + i];
  314. }
  315. return hc;
  316. }
  317. public static int GetHashCode(int[] data)
  318. {
  319. if (data == null)
  320. return 0;
  321. int i = data.Length;
  322. int hc = i + 1;
  323. while (--i >= 0)
  324. {
  325. hc *= 257;
  326. hc ^= data[i];
  327. }
  328. return hc;
  329. }
  330. [CLSCompliant(false)]
  331. public static int GetHashCode(ushort[] data)
  332. {
  333. if (data == null)
  334. return 0;
  335. int i = data.Length;
  336. int hc = i + 1;
  337. while (--i >= 0)
  338. {
  339. hc *= 257;
  340. hc ^= data[i];
  341. }
  342. return hc;
  343. }
  344. public static int GetHashCode(int[] data, int off, int len)
  345. {
  346. if (data == null)
  347. return 0;
  348. int i = len;
  349. int hc = i + 1;
  350. while (--i >= 0)
  351. {
  352. hc *= 257;
  353. hc ^= data[off + i];
  354. }
  355. return hc;
  356. }
  357. [CLSCompliant(false)]
  358. public static int GetHashCode(uint[] data)
  359. {
  360. if (data == null)
  361. return 0;
  362. int i = data.Length;
  363. int hc = i + 1;
  364. while (--i >= 0)
  365. {
  366. hc *= 257;
  367. hc ^= (int)data[i];
  368. }
  369. return hc;
  370. }
  371. [CLSCompliant(false)]
  372. public static int GetHashCode(uint[] data, int off, int len)
  373. {
  374. if (data == null)
  375. return 0;
  376. int i = len;
  377. int hc = i + 1;
  378. while (--i >= 0)
  379. {
  380. hc *= 257;
  381. hc ^= (int)data[off + i];
  382. }
  383. return hc;
  384. }
  385. [CLSCompliant(false)]
  386. public static int GetHashCode(ulong[] data)
  387. {
  388. if (data == null)
  389. return 0;
  390. int i = data.Length;
  391. int hc = i + 1;
  392. while (--i >= 0)
  393. {
  394. ulong di = data[i];
  395. hc *= 257;
  396. hc ^= (int)di;
  397. hc *= 257;
  398. hc ^= (int)(di >> 32);
  399. }
  400. return hc;
  401. }
  402. [CLSCompliant(false)]
  403. public static int GetHashCode(ulong[] data, int off, int len)
  404. {
  405. if (data == null)
  406. return 0;
  407. int i = len;
  408. int hc = i + 1;
  409. while (--i >= 0)
  410. {
  411. ulong di = data[off + i];
  412. hc *= 257;
  413. hc ^= (int)di;
  414. hc *= 257;
  415. hc ^= (int)(di >> 32);
  416. }
  417. return hc;
  418. }
  419. public static bool[] Clone(bool[] data)
  420. {
  421. return data == null ? null : (bool[])data.Clone();
  422. }
  423. public static byte[] Clone(byte[] data)
  424. {
  425. return data == null ? null : (byte[])data.Clone();
  426. }
  427. public static short[] Clone(short[] data)
  428. {
  429. return data == null ? null : (short[])data.Clone();
  430. }
  431. [CLSCompliant(false)]
  432. public static ushort[] Clone(ushort[] data)
  433. {
  434. return data == null ? null : (ushort[])data.Clone();
  435. }
  436. public static int[] Clone(int[] data)
  437. {
  438. return data == null ? null : (int[])data.Clone();
  439. }
  440. [CLSCompliant(false)]
  441. public static uint[] Clone(uint[] data)
  442. {
  443. return data == null ? null : (uint[])data.Clone();
  444. }
  445. public static long[] Clone(long[] data)
  446. {
  447. return data == null ? null : (long[])data.Clone();
  448. }
  449. [CLSCompliant(false)]
  450. public static ulong[] Clone(ulong[] data)
  451. {
  452. return data == null ? null : (ulong[])data.Clone();
  453. }
  454. public static byte[] Clone(byte[] data, byte[] existing)
  455. {
  456. if (data == null)
  457. return null;
  458. if (existing == null || existing.Length != data.Length)
  459. return Clone(data);
  460. Array.Copy(data, 0, existing, 0, existing.Length);
  461. return existing;
  462. }
  463. [CLSCompliant(false)]
  464. public static ulong[] Clone(ulong[] data, ulong[] existing)
  465. {
  466. if (data == null)
  467. return null;
  468. if (existing == null || existing.Length != data.Length)
  469. return Clone(data);
  470. Array.Copy(data, 0, existing, 0, existing.Length);
  471. return existing;
  472. }
  473. public static bool Contains(byte[] a, byte n)
  474. {
  475. for (int i = 0; i < a.Length; ++i)
  476. {
  477. if (a[i] == n)
  478. return true;
  479. }
  480. return false;
  481. }
  482. public static bool Contains(short[] a, short n)
  483. {
  484. for (int i = 0; i < a.Length; ++i)
  485. {
  486. if (a[i] == n)
  487. return true;
  488. }
  489. return false;
  490. }
  491. public static bool Contains(int[] a, int n)
  492. {
  493. for (int i = 0; i < a.Length; ++i)
  494. {
  495. if (a[i] == n)
  496. return true;
  497. }
  498. return false;
  499. }
  500. public static void Fill(
  501. byte[] buf,
  502. byte b)
  503. {
  504. int i = buf.Length;
  505. while (i > 0)
  506. {
  507. buf[--i] = b;
  508. }
  509. }
  510. [CLSCompliant(false)]
  511. public static void Fill(
  512. ulong[] buf,
  513. ulong b)
  514. {
  515. int i = buf.Length;
  516. while (i > 0)
  517. {
  518. buf[--i] = b;
  519. }
  520. }
  521. public static void Fill(byte[] buf, int from, int to, byte b)
  522. {
  523. for (int i = from; i < to; ++i)
  524. {
  525. buf[i] = b;
  526. }
  527. }
  528. public static void Fill<T>(T[] ts, T t)
  529. {
  530. for (int i = 0; i < ts.Length; ++i)
  531. {
  532. ts[i] = t;
  533. }
  534. }
  535. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  536. public static void Fill<T>(Span<T> ts, T t)
  537. {
  538. ts.Fill(t);
  539. }
  540. #endif
  541. public static byte[] CopyOf(byte[] data, int newLength)
  542. {
  543. byte[] tmp = new byte[newLength];
  544. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  545. return tmp;
  546. }
  547. public static char[] CopyOf(char[] data, int newLength)
  548. {
  549. char[] tmp = new char[newLength];
  550. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  551. return tmp;
  552. }
  553. public static int[] CopyOf(int[] data, int newLength)
  554. {
  555. int[] tmp = new int[newLength];
  556. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  557. return tmp;
  558. }
  559. [CLSCompliant(false)]
  560. public static uint[] CopyOf(uint[] data, int newLength)
  561. {
  562. uint[] tmp = new uint[newLength];
  563. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  564. return tmp;
  565. }
  566. public static long[] CopyOf(long[] data, int newLength)
  567. {
  568. long[] tmp = new long[newLength];
  569. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  570. return tmp;
  571. }
  572. public static BigInteger[] CopyOf(BigInteger[] data, int newLength)
  573. {
  574. BigInteger[] tmp = new BigInteger[newLength];
  575. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  576. return tmp;
  577. }
  578. /**
  579. * Make a copy of a range of bytes from the passed in data array. The range can
  580. * extend beyond the end of the input array, in which case the return array will
  581. * be padded with zeroes.
  582. *
  583. * @param data the array from which the data is to be copied.
  584. * @param from the start index at which the copying should take place.
  585. * @param to the final index of the range (exclusive).
  586. *
  587. * @return a new byte array containing the range given.
  588. */
  589. public static byte[] CopyOfRange(byte[] data, int from, int to)
  590. {
  591. int newLength = GetLength(from, to);
  592. byte[] tmp = new byte[newLength];
  593. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  594. return tmp;
  595. }
  596. public static int[] CopyOfRange(int[] data, int from, int to)
  597. {
  598. int newLength = GetLength(from, to);
  599. int[] tmp = new int[newLength];
  600. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  601. return tmp;
  602. }
  603. public static long[] CopyOfRange(long[] data, int from, int to)
  604. {
  605. int newLength = GetLength(from, to);
  606. long[] tmp = new long[newLength];
  607. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  608. return tmp;
  609. }
  610. public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to)
  611. {
  612. int newLength = GetLength(from, to);
  613. BigInteger[] tmp = new BigInteger[newLength];
  614. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  615. return tmp;
  616. }
  617. private static int GetLength(int from, int to)
  618. {
  619. int newLength = to - from;
  620. if (newLength < 0)
  621. throw new ArgumentException(from + " > " + to);
  622. return newLength;
  623. }
  624. public static byte[] Append(byte[] a, byte b)
  625. {
  626. if (a == null)
  627. return new byte[] { b };
  628. int length = a.Length;
  629. byte[] result = new byte[length + 1];
  630. Array.Copy(a, 0, result, 0, length);
  631. result[length] = b;
  632. return result;
  633. }
  634. public static short[] Append(short[] a, short b)
  635. {
  636. if (a == null)
  637. return new short[] { b };
  638. int length = a.Length;
  639. short[] result = new short[length + 1];
  640. Array.Copy(a, 0, result, 0, length);
  641. result[length] = b;
  642. return result;
  643. }
  644. public static int[] Append(int[] a, int b)
  645. {
  646. if (a == null)
  647. return new int[] { b };
  648. int length = a.Length;
  649. int[] result = new int[length + 1];
  650. Array.Copy(a, 0, result, 0, length);
  651. result[length] = b;
  652. return result;
  653. }
  654. public static byte[] Concatenate(byte[] a, byte[] b)
  655. {
  656. if (a == null)
  657. return Clone(b);
  658. if (b == null)
  659. return Clone(a);
  660. byte[] rv = new byte[a.Length + b.Length];
  661. Array.Copy(a, 0, rv, 0, a.Length);
  662. Array.Copy(b, 0, rv, a.Length, b.Length);
  663. return rv;
  664. }
  665. [CLSCompliant(false)]
  666. public static ushort[] Concatenate(ushort[] a, ushort[] b)
  667. {
  668. if (a == null)
  669. return Clone(b);
  670. if (b == null)
  671. return Clone(a);
  672. ushort[] rv = new ushort[a.Length + b.Length];
  673. Array.Copy(a, 0, rv, 0, a.Length);
  674. Array.Copy(b, 0, rv, a.Length, b.Length);
  675. return rv;
  676. }
  677. public static byte[] ConcatenateAll(params byte[][] vs)
  678. {
  679. byte[][] nonNull = new byte[vs.Length][];
  680. int count = 0;
  681. int totalLength = 0;
  682. for (int i = 0; i < vs.Length; ++i)
  683. {
  684. byte[] v = vs[i];
  685. if (v != null)
  686. {
  687. nonNull[count++] = v;
  688. totalLength += v.Length;
  689. }
  690. }
  691. byte[] result = new byte[totalLength];
  692. int pos = 0;
  693. for (int j = 0; j < count; ++j)
  694. {
  695. byte[] v = nonNull[j];
  696. Array.Copy(v, 0, result, pos, v.Length);
  697. pos += v.Length;
  698. }
  699. return result;
  700. }
  701. public static int[] Concatenate(int[] a, int[] b)
  702. {
  703. if (a == null)
  704. return Clone(b);
  705. if (b == null)
  706. return Clone(a);
  707. int[] rv = new int[a.Length + b.Length];
  708. Array.Copy(a, 0, rv, 0, a.Length);
  709. Array.Copy(b, 0, rv, a.Length, b.Length);
  710. return rv;
  711. }
  712. [CLSCompliant(false)]
  713. public static uint[] Concatenate(uint[] a, uint[] b)
  714. {
  715. if (a == null)
  716. return Clone(b);
  717. if (b == null)
  718. return Clone(a);
  719. uint[] rv = new uint[a.Length + b.Length];
  720. Array.Copy(a, 0, rv, 0, a.Length);
  721. Array.Copy(b, 0, rv, a.Length, b.Length);
  722. return rv;
  723. }
  724. public static byte[] Prepend(byte[] a, byte b)
  725. {
  726. if (a == null)
  727. return new byte[] { b };
  728. int length = a.Length;
  729. byte[] result = new byte[length + 1];
  730. Array.Copy(a, 0, result, 1, length);
  731. result[0] = b;
  732. return result;
  733. }
  734. public static short[] Prepend(short[] a, short b)
  735. {
  736. if (a == null)
  737. return new short[] { b };
  738. int length = a.Length;
  739. short[] result = new short[length + 1];
  740. Array.Copy(a, 0, result, 1, length);
  741. result[0] = b;
  742. return result;
  743. }
  744. public static int[] Prepend(int[] a, int b)
  745. {
  746. if (a == null)
  747. return new int[] { b };
  748. int length = a.Length;
  749. int[] result = new int[length + 1];
  750. Array.Copy(a, 0, result, 1, length);
  751. result[0] = b;
  752. return result;
  753. }
  754. public static T[] Prepend<T>(T[] a, T b)
  755. {
  756. if (a == null)
  757. return new T[1]{ b };
  758. T[] result = new T[1 + a.Length];
  759. result[0] = b;
  760. a.CopyTo(result, 1);
  761. return result;
  762. }
  763. public static byte[] Reverse(byte[] a)
  764. {
  765. if (a == null)
  766. return null;
  767. int p1 = 0, p2 = a.Length;
  768. byte[] result = new byte[p2];
  769. while (--p2 >= 0)
  770. {
  771. result[p2] = a[p1++];
  772. }
  773. return result;
  774. }
  775. public static int[] Reverse(int[] a)
  776. {
  777. if (a == null)
  778. return null;
  779. int p1 = 0, p2 = a.Length;
  780. int[] result = new int[p2];
  781. while (--p2 >= 0)
  782. {
  783. result[p2] = a[p1++];
  784. }
  785. return result;
  786. }
  787. public static T[] ReverseInPlace<T>(T[] array)
  788. {
  789. if (null == array)
  790. return null;
  791. Array.Reverse(array);
  792. return array;
  793. }
  794. public static void Clear(byte[] data)
  795. {
  796. if (null != data)
  797. {
  798. Array.Clear(data, 0, data.Length);
  799. }
  800. }
  801. public static void Clear(int[] data)
  802. {
  803. if (null != data)
  804. {
  805. Array.Clear(data, 0, data.Length);
  806. }
  807. }
  808. public static bool IsNullOrContainsNull(object[] array)
  809. {
  810. if (null == array)
  811. return true;
  812. int count = array.Length;
  813. for (int i = 0; i < count; ++i)
  814. {
  815. if (null == array[i])
  816. return true;
  817. }
  818. return false;
  819. }
  820. public static bool IsNullOrEmpty(byte[] array)
  821. {
  822. return null == array || array.Length < 1;
  823. }
  824. public static bool IsNullOrEmpty(object[] array)
  825. {
  826. return null == array || array.Length < 1;
  827. }
  828. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  829. public static byte[] Concatenate(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
  830. {
  831. byte[] rv = new byte[a.Length + b.Length];
  832. a.CopyTo(rv);
  833. b.CopyTo(rv.AsSpan(a.Length));
  834. return rv;
  835. }
  836. public static byte[] Concatenate(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, ReadOnlySpan<byte> c)
  837. {
  838. byte[] rv = new byte[a.Length + b.Length + c.Length];
  839. a.CopyTo(rv);
  840. b.CopyTo(rv.AsSpan(a.Length));
  841. c.CopyTo(rv.AsSpan(a.Length + b.Length));
  842. return rv;
  843. }
  844. public static byte[] Concatenate(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, ReadOnlySpan<byte> c,
  845. ReadOnlySpan<byte> d)
  846. {
  847. byte[] rv = new byte[a.Length + b.Length + c.Length + d.Length];
  848. a.CopyTo(rv);
  849. b.CopyTo(rv.AsSpan(a.Length));
  850. c.CopyTo(rv.AsSpan(a.Length + b.Length));
  851. d.CopyTo(rv.AsSpan(a.Length + b.Length + c.Length));
  852. return rv;
  853. }
  854. #endif
  855. }
  856. }
  857. #pragma warning restore
  858. #endif