Arrays.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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 abstract 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. public static bool AreEqual(
  23. bool[] a,
  24. bool[] b)
  25. {
  26. if (a == b)
  27. return true;
  28. if (a == null || b == null)
  29. return false;
  30. return HaveSameContents(a, b);
  31. }
  32. public static bool AreEqual(
  33. char[] a,
  34. char[] b)
  35. {
  36. if (a == b)
  37. return true;
  38. if (a == null || b == null)
  39. return false;
  40. return HaveSameContents(a, b);
  41. }
  42. /// <summary>
  43. /// Are two arrays equal.
  44. /// </summary>
  45. /// <param name="a">Left side.</param>
  46. /// <param name="b">Right side.</param>
  47. /// <returns>True if equal.</returns>
  48. public static bool AreEqual(
  49. byte[] a,
  50. byte[] b)
  51. {
  52. if (a == b)
  53. return true;
  54. if (a == null || b == null)
  55. return false;
  56. return HaveSameContents(a, b);
  57. }
  58. [Obsolete("Use 'AreEqual' method instead")]
  59. public static bool AreSame(
  60. byte[] a,
  61. byte[] b)
  62. {
  63. return AreEqual(a, b);
  64. }
  65. /// <summary>
  66. /// A constant time equals comparison - does not terminate early if
  67. /// test will fail.
  68. /// </summary>
  69. /// <param name="a">first array</param>
  70. /// <param name="b">second array</param>
  71. /// <returns>true if arrays equal, false otherwise.</returns>
  72. public static bool ConstantTimeAreEqual(
  73. byte[] a,
  74. byte[] b)
  75. {
  76. int i = a.Length;
  77. if (i != b.Length)
  78. return false;
  79. int cmp = 0;
  80. while (i != 0)
  81. {
  82. --i;
  83. cmp |= (a[i] ^ b[i]);
  84. }
  85. return cmp == 0;
  86. }
  87. public static bool AreEqual(
  88. int[] a,
  89. int[] b)
  90. {
  91. if (a == b)
  92. return true;
  93. if (a == null || b == null)
  94. return false;
  95. return HaveSameContents(a, b);
  96. }
  97. [CLSCompliantAttribute(false)]
  98. public static bool AreEqual(uint[] a, uint[] b)
  99. {
  100. if (a == b)
  101. return true;
  102. if (a == null || b == null)
  103. return false;
  104. return HaveSameContents(a, b);
  105. }
  106. private static bool HaveSameContents(
  107. bool[] a,
  108. bool[] b)
  109. {
  110. int i = a.Length;
  111. if (i != b.Length)
  112. return false;
  113. while (i != 0)
  114. {
  115. --i;
  116. if (a[i] != b[i])
  117. return false;
  118. }
  119. return true;
  120. }
  121. private static bool HaveSameContents(
  122. char[] a,
  123. char[] b)
  124. {
  125. int i = a.Length;
  126. if (i != b.Length)
  127. return false;
  128. while (i != 0)
  129. {
  130. --i;
  131. if (a[i] != b[i])
  132. return false;
  133. }
  134. return true;
  135. }
  136. private static bool HaveSameContents(
  137. byte[] a,
  138. byte[] b)
  139. {
  140. int i = a.Length;
  141. if (i != b.Length)
  142. return false;
  143. while (i != 0)
  144. {
  145. --i;
  146. if (a[i] != b[i])
  147. return false;
  148. }
  149. return true;
  150. }
  151. private static bool HaveSameContents(
  152. int[] a,
  153. int[] b)
  154. {
  155. int i = a.Length;
  156. if (i != b.Length)
  157. return false;
  158. while (i != 0)
  159. {
  160. --i;
  161. if (a[i] != b[i])
  162. return false;
  163. }
  164. return true;
  165. }
  166. private static bool HaveSameContents(uint[] a, uint[] b)
  167. {
  168. int i = a.Length;
  169. if (i != b.Length)
  170. return false;
  171. while (i != 0)
  172. {
  173. --i;
  174. if (a[i] != b[i])
  175. return false;
  176. }
  177. return true;
  178. }
  179. public static string ToString(
  180. object[] a)
  181. {
  182. StringBuilder sb = new StringBuilder('[');
  183. if (a.Length > 0)
  184. {
  185. sb.Append(a[0]);
  186. for (int index = 1; index < a.Length; ++index)
  187. {
  188. sb.Append(", ").Append(a[index]);
  189. }
  190. }
  191. sb.Append(']');
  192. return sb.ToString();
  193. }
  194. public static int GetHashCode(byte[] data)
  195. {
  196. if (data == null)
  197. {
  198. return 0;
  199. }
  200. int i = data.Length;
  201. int hc = i + 1;
  202. while (--i >= 0)
  203. {
  204. hc *= 257;
  205. hc ^= data[i];
  206. }
  207. return hc;
  208. }
  209. public static int GetHashCode(byte[] data, int off, int len)
  210. {
  211. if (data == null)
  212. {
  213. return 0;
  214. }
  215. int i = len;
  216. int hc = i + 1;
  217. while (--i >= 0)
  218. {
  219. hc *= 257;
  220. hc ^= data[off + i];
  221. }
  222. return hc;
  223. }
  224. public static int GetHashCode(int[] data)
  225. {
  226. if (data == null)
  227. return 0;
  228. int i = data.Length;
  229. int hc = i + 1;
  230. while (--i >= 0)
  231. {
  232. hc *= 257;
  233. hc ^= data[i];
  234. }
  235. return hc;
  236. }
  237. public static int GetHashCode(int[] data, int off, int len)
  238. {
  239. if (data == null)
  240. return 0;
  241. int i = len;
  242. int hc = i + 1;
  243. while (--i >= 0)
  244. {
  245. hc *= 257;
  246. hc ^= data[off + i];
  247. }
  248. return hc;
  249. }
  250. [CLSCompliantAttribute(false)]
  251. public static int GetHashCode(uint[] data)
  252. {
  253. if (data == null)
  254. return 0;
  255. int i = data.Length;
  256. int hc = i + 1;
  257. while (--i >= 0)
  258. {
  259. hc *= 257;
  260. hc ^= (int)data[i];
  261. }
  262. return hc;
  263. }
  264. [CLSCompliantAttribute(false)]
  265. public static int GetHashCode(uint[] data, int off, int len)
  266. {
  267. if (data == null)
  268. return 0;
  269. int i = len;
  270. int hc = i + 1;
  271. while (--i >= 0)
  272. {
  273. hc *= 257;
  274. hc ^= (int)data[off + i];
  275. }
  276. return hc;
  277. }
  278. [CLSCompliantAttribute(false)]
  279. public static int GetHashCode(ulong[] data)
  280. {
  281. if (data == null)
  282. return 0;
  283. int i = data.Length;
  284. int hc = i + 1;
  285. while (--i >= 0)
  286. {
  287. ulong di = data[i];
  288. hc *= 257;
  289. hc ^= (int)di;
  290. hc *= 257;
  291. hc ^= (int)(di >> 32);
  292. }
  293. return hc;
  294. }
  295. [CLSCompliantAttribute(false)]
  296. public static int GetHashCode(ulong[] data, int off, int len)
  297. {
  298. if (data == null)
  299. return 0;
  300. int i = len;
  301. int hc = i + 1;
  302. while (--i >= 0)
  303. {
  304. ulong di = data[off + i];
  305. hc *= 257;
  306. hc ^= (int)di;
  307. hc *= 257;
  308. hc ^= (int)(di >> 32);
  309. }
  310. return hc;
  311. }
  312. public static byte[] Clone(
  313. byte[] data)
  314. {
  315. return data == null ? null : (byte[])data.Clone();
  316. }
  317. public static byte[] Clone(
  318. byte[] data,
  319. byte[] existing)
  320. {
  321. if (data == null)
  322. {
  323. return null;
  324. }
  325. if ((existing == null) || (existing.Length != data.Length))
  326. {
  327. return Clone(data);
  328. }
  329. Array.Copy(data, 0, existing, 0, existing.Length);
  330. return existing;
  331. }
  332. public static int[] Clone(
  333. int[] data)
  334. {
  335. return data == null ? null : (int[])data.Clone();
  336. }
  337. internal static uint[] Clone(uint[] data)
  338. {
  339. return data == null ? null : (uint[])data.Clone();
  340. }
  341. public static long[] Clone(long[] data)
  342. {
  343. return data == null ? null : (long[])data.Clone();
  344. }
  345. [CLSCompliantAttribute(false)]
  346. public static ulong[] Clone(
  347. ulong[] data)
  348. {
  349. return data == null ? null : (ulong[]) data.Clone();
  350. }
  351. [CLSCompliantAttribute(false)]
  352. public static ulong[] Clone(
  353. ulong[] data,
  354. ulong[] existing)
  355. {
  356. if (data == null)
  357. {
  358. return null;
  359. }
  360. if ((existing == null) || (existing.Length != data.Length))
  361. {
  362. return Clone(data);
  363. }
  364. Array.Copy(data, 0, existing, 0, existing.Length);
  365. return existing;
  366. }
  367. public static bool Contains(byte[] a, byte n)
  368. {
  369. for (int i = 0; i < a.Length; ++i)
  370. {
  371. if (a[i] == n)
  372. return true;
  373. }
  374. return false;
  375. }
  376. public static bool Contains(short[] a, short n)
  377. {
  378. for (int i = 0; i < a.Length; ++i)
  379. {
  380. if (a[i] == n)
  381. return true;
  382. }
  383. return false;
  384. }
  385. public static bool Contains(int[] a, int n)
  386. {
  387. for (int i = 0; i < a.Length; ++i)
  388. {
  389. if (a[i] == n)
  390. return true;
  391. }
  392. return false;
  393. }
  394. public static void Fill(
  395. byte[] buf,
  396. byte b)
  397. {
  398. int i = buf.Length;
  399. while (i > 0)
  400. {
  401. buf[--i] = b;
  402. }
  403. }
  404. public static void Fill(byte[] buf, int from, int to, byte b)
  405. {
  406. for (int i = from; i < to; ++i)
  407. {
  408. buf[i] = b;
  409. }
  410. }
  411. public static byte[] CopyOf(byte[] data, int newLength)
  412. {
  413. byte[] tmp = new byte[newLength];
  414. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  415. return tmp;
  416. }
  417. public static char[] CopyOf(char[] data, int newLength)
  418. {
  419. char[] tmp = new char[newLength];
  420. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  421. return tmp;
  422. }
  423. public static int[] CopyOf(int[] data, int newLength)
  424. {
  425. int[] tmp = new int[newLength];
  426. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  427. return tmp;
  428. }
  429. public static long[] CopyOf(long[] data, int newLength)
  430. {
  431. long[] tmp = new long[newLength];
  432. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  433. return tmp;
  434. }
  435. public static BigInteger[] CopyOf(BigInteger[] data, int newLength)
  436. {
  437. BigInteger[] tmp = new BigInteger[newLength];
  438. Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
  439. return tmp;
  440. }
  441. /**
  442. * Make a copy of a range of bytes from the passed in data array. The range can
  443. * extend beyond the end of the input array, in which case the return array will
  444. * be padded with zeroes.
  445. *
  446. * @param data the array from which the data is to be copied.
  447. * @param from the start index at which the copying should take place.
  448. * @param to the final index of the range (exclusive).
  449. *
  450. * @return a new byte array containing the range given.
  451. */
  452. public static byte[] CopyOfRange(byte[] data, int from, int to)
  453. {
  454. int newLength = GetLength(from, to);
  455. byte[] tmp = new byte[newLength];
  456. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  457. return tmp;
  458. }
  459. public static int[] CopyOfRange(int[] data, int from, int to)
  460. {
  461. int newLength = GetLength(from, to);
  462. int[] tmp = new int[newLength];
  463. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  464. return tmp;
  465. }
  466. public static long[] CopyOfRange(long[] data, int from, int to)
  467. {
  468. int newLength = GetLength(from, to);
  469. long[] tmp = new long[newLength];
  470. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  471. return tmp;
  472. }
  473. public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to)
  474. {
  475. int newLength = GetLength(from, to);
  476. BigInteger[] tmp = new BigInteger[newLength];
  477. Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
  478. return tmp;
  479. }
  480. private static int GetLength(int from, int to)
  481. {
  482. int newLength = to - from;
  483. if (newLength < 0)
  484. throw new ArgumentException(from + " > " + to);
  485. return newLength;
  486. }
  487. public static byte[] Append(byte[] a, byte b)
  488. {
  489. if (a == null)
  490. return new byte[] { b };
  491. int length = a.Length;
  492. byte[] result = new byte[length + 1];
  493. Array.Copy(a, 0, result, 0, length);
  494. result[length] = b;
  495. return result;
  496. }
  497. public static short[] Append(short[] a, short b)
  498. {
  499. if (a == null)
  500. return new short[] { b };
  501. int length = a.Length;
  502. short[] result = new short[length + 1];
  503. Array.Copy(a, 0, result, 0, length);
  504. result[length] = b;
  505. return result;
  506. }
  507. public static int[] Append(int[] a, int b)
  508. {
  509. if (a == null)
  510. return new int[] { b };
  511. int length = a.Length;
  512. int[] result = new int[length + 1];
  513. Array.Copy(a, 0, result, 0, length);
  514. result[length] = b;
  515. return result;
  516. }
  517. public static byte[] Concatenate(byte[] a, byte[] b)
  518. {
  519. if (a == null)
  520. return Clone(b);
  521. if (b == null)
  522. return Clone(a);
  523. byte[] rv = new byte[a.Length + b.Length];
  524. Array.Copy(a, 0, rv, 0, a.Length);
  525. Array.Copy(b, 0, rv, a.Length, b.Length);
  526. return rv;
  527. }
  528. public static byte[] ConcatenateAll(params byte[][] vs)
  529. {
  530. byte[][] nonNull = new byte[vs.Length][];
  531. int count = 0;
  532. int totalLength = 0;
  533. for (int i = 0; i < vs.Length; ++i)
  534. {
  535. byte[] v = vs[i];
  536. if (v != null)
  537. {
  538. nonNull[count++] = v;
  539. totalLength += v.Length;
  540. }
  541. }
  542. byte[] result = new byte[totalLength];
  543. int pos = 0;
  544. for (int j = 0; j < count; ++j)
  545. {
  546. byte[] v = nonNull[j];
  547. Array.Copy(v, 0, result, pos, v.Length);
  548. pos += v.Length;
  549. }
  550. return result;
  551. }
  552. public static int[] Concatenate(int[] a, int[] b)
  553. {
  554. if (a == null)
  555. return Clone(b);
  556. if (b == null)
  557. return Clone(a);
  558. int[] rv = new int[a.Length + b.Length];
  559. Array.Copy(a, 0, rv, 0, a.Length);
  560. Array.Copy(b, 0, rv, a.Length, b.Length);
  561. return rv;
  562. }
  563. public static byte[] Prepend(byte[] a, byte b)
  564. {
  565. if (a == null)
  566. return new byte[] { b };
  567. int length = a.Length;
  568. byte[] result = new byte[length + 1];
  569. Array.Copy(a, 0, result, 1, length);
  570. result[0] = b;
  571. return result;
  572. }
  573. public static short[] Prepend(short[] a, short b)
  574. {
  575. if (a == null)
  576. return new short[] { b };
  577. int length = a.Length;
  578. short[] result = new short[length + 1];
  579. Array.Copy(a, 0, result, 1, length);
  580. result[0] = b;
  581. return result;
  582. }
  583. public static int[] Prepend(int[] a, int b)
  584. {
  585. if (a == null)
  586. return new int[] { b };
  587. int length = a.Length;
  588. int[] result = new int[length + 1];
  589. Array.Copy(a, 0, result, 1, length);
  590. result[0] = b;
  591. return result;
  592. }
  593. public static byte[] Reverse(byte[] a)
  594. {
  595. if (a == null)
  596. return null;
  597. int p1 = 0, p2 = a.Length;
  598. byte[] result = new byte[p2];
  599. while (--p2 >= 0)
  600. {
  601. result[p2] = a[p1++];
  602. }
  603. return result;
  604. }
  605. public static int[] Reverse(int[] a)
  606. {
  607. if (a == null)
  608. return null;
  609. int p1 = 0, p2 = a.Length;
  610. int[] result = new int[p2];
  611. while (--p2 >= 0)
  612. {
  613. result[p2] = a[p1++];
  614. }
  615. return result;
  616. }
  617. }
  618. }
  619. #pragma warning restore
  620. #endif