Arrays.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. namespace BestHTTP.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