Arrays.cs 17 KB

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