123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using System.Text;
- using Org.BouncyCastle.Math;
- namespace Org.BouncyCastle.Utilities
- {
- /// <summary> General array utilities.</summary>
- public abstract class Arrays
- {
- public static bool AreEqual(
- bool[] a,
- bool[] b)
- {
- if (a == b)
- return true;
- if (a == null || b == null)
- return false;
- return HaveSameContents(a, b);
- }
- public static bool AreEqual(
- char[] a,
- char[] b)
- {
- if (a == b)
- return true;
- if (a == null || b == null)
- return false;
- return HaveSameContents(a, b);
- }
- /// <summary>
- /// Are two arrays equal.
- /// </summary>
- /// <param name="a">Left side.</param>
- /// <param name="b">Right side.</param>
- /// <returns>True if equal.</returns>
- public static bool AreEqual(
- byte[] a,
- byte[] b)
- {
- if (a == b)
- return true;
- if (a == null || b == null)
- return false;
- return HaveSameContents(a, b);
- }
- [Obsolete("Use 'AreEqual' method instead")]
- public static bool AreSame(
- byte[] a,
- byte[] b)
- {
- return AreEqual(a, b);
- }
- /// <summary>
- /// A constant time equals comparison - does not terminate early if
- /// test will fail.
- /// </summary>
- /// <param name="a">first array</param>
- /// <param name="b">second array</param>
- /// <returns>true if arrays equal, false otherwise.</returns>
- public static bool ConstantTimeAreEqual(
- byte[] a,
- byte[] b)
- {
- int i = a.Length;
- if (i != b.Length)
- return false;
- int cmp = 0;
- while (i != 0)
- {
- --i;
- cmp |= (a[i] ^ b[i]);
- }
- return cmp == 0;
- }
- public static bool AreEqual(
- int[] a,
- int[] b)
- {
- if (a == b)
- return true;
- if (a == null || b == null)
- return false;
- return HaveSameContents(a, b);
- }
- public static bool AreEqual(uint[] a, uint[] b)
- {
- if (a == b)
- return true;
- if (a == null || b == null)
- return false;
- return HaveSameContents(a, b);
- }
- private static bool HaveSameContents(
- bool[] a,
- bool[] b)
- {
- int i = a.Length;
- if (i != b.Length)
- return false;
- while (i != 0)
- {
- --i;
- if (a[i] != b[i])
- return false;
- }
- return true;
- }
- private static bool HaveSameContents(
- char[] a,
- char[] b)
- {
- int i = a.Length;
- if (i != b.Length)
- return false;
- while (i != 0)
- {
- --i;
- if (a[i] != b[i])
- return false;
- }
- return true;
- }
- private static bool HaveSameContents(
- byte[] a,
- byte[] b)
- {
- int i = a.Length;
- if (i != b.Length)
- return false;
- while (i != 0)
- {
- --i;
- if (a[i] != b[i])
- return false;
- }
- return true;
- }
- private static bool HaveSameContents(
- int[] a,
- int[] b)
- {
- int i = a.Length;
- if (i != b.Length)
- return false;
- while (i != 0)
- {
- --i;
- if (a[i] != b[i])
- return false;
- }
- return true;
- }
- private static bool HaveSameContents(uint[] a, uint[] b)
- {
- int i = a.Length;
- if (i != b.Length)
- return false;
- while (i != 0)
- {
- --i;
- if (a[i] != b[i])
- return false;
- }
- return true;
- }
- public static string ToString(
- object[] a)
- {
- StringBuilder sb = new StringBuilder('[');
- if (a.Length > 0)
- {
- sb.Append(a[0]);
- for (int index = 1; index < a.Length; ++index)
- {
- sb.Append(", ").Append(a[index]);
- }
- }
- sb.Append(']');
- return sb.ToString();
- }
- public static int GetHashCode(byte[] data)
- {
- if (data == null)
- {
- return 0;
- }
- int i = data.Length;
- int hc = i + 1;
- while (--i >= 0)
- {
- hc *= 257;
- hc ^= data[i];
- }
- return hc;
- }
- public static int GetHashCode(byte[] data, int off, int len)
- {
- if (data == null)
- {
- return 0;
- }
- int i = len;
- int hc = i + 1;
- while (--i >= 0)
- {
- hc *= 257;
- hc ^= data[off + i];
- }
- return hc;
- }
- public static int GetHashCode(int[] data)
- {
- if (data == null)
- return 0;
- int i = data.Length;
- int hc = i + 1;
- while (--i >= 0)
- {
- hc *= 257;
- hc ^= data[i];
- }
- return hc;
- }
- public static int GetHashCode(int[] data, int off, int len)
- {
- if (data == null)
- return 0;
- int i = len;
- int hc = i + 1;
- while (--i >= 0)
- {
- hc *= 257;
- hc ^= data[off + i];
- }
- return hc;
- }
- public static int GetHashCode(uint[] data)
- {
- if (data == null)
- return 0;
- int i = data.Length;
- int hc = i + 1;
- while (--i >= 0)
- {
- hc *= 257;
- hc ^= (int)data[i];
- }
- return hc;
- }
- public static int GetHashCode(uint[] data, int off, int len)
- {
- if (data == null)
- return 0;
- int i = len;
- int hc = i + 1;
- while (--i >= 0)
- {
- hc *= 257;
- hc ^= (int)data[off + i];
- }
- return hc;
- }
- public static int GetHashCode(ulong[] data)
- {
- if (data == null)
- return 0;
- int i = data.Length;
- int hc = i + 1;
- while (--i >= 0)
- {
- ulong di = data[i];
- hc *= 257;
- hc ^= (int)di;
- hc *= 257;
- hc ^= (int)(di >> 32);
- }
- return hc;
- }
- public static int GetHashCode(ulong[] data, int off, int len)
- {
- if (data == null)
- return 0;
- int i = len;
- int hc = i + 1;
- while (--i >= 0)
- {
- ulong di = data[off + i];
- hc *= 257;
- hc ^= (int)di;
- hc *= 257;
- hc ^= (int)(di >> 32);
- }
- return hc;
- }
- public static byte[] Clone(
- byte[] data)
- {
- return data == null ? null : (byte[])data.Clone();
- }
- public static byte[] Clone(
- byte[] data,
- byte[] existing)
- {
- if (data == null)
- {
- return null;
- }
- if ((existing == null) || (existing.Length != data.Length))
- {
- return Clone(data);
- }
- Array.Copy(data, 0, existing, 0, existing.Length);
- return existing;
- }
- public static int[] Clone(
- int[] data)
- {
- return data == null ? null : (int[])data.Clone();
- }
- internal static uint[] Clone(uint[] data)
- {
- return data == null ? null : (uint[])data.Clone();
- }
- public static long[] Clone(long[] data)
- {
- return data == null ? null : (long[])data.Clone();
- }
- public static ulong[] Clone(
- ulong[] data)
- {
- return data == null ? null : (ulong[]) data.Clone();
- }
- public static ulong[] Clone(
- ulong[] data,
- ulong[] existing)
- {
- if (data == null)
- {
- return null;
- }
- if ((existing == null) || (existing.Length != data.Length))
- {
- return Clone(data);
- }
- Array.Copy(data, 0, existing, 0, existing.Length);
- return existing;
- }
- public static bool Contains(byte[] a, byte n)
- {
- for (int i = 0; i < a.Length; ++i)
- {
- if (a[i] == n)
- return true;
- }
- return false;
- }
- public static bool Contains(short[] a, short n)
- {
- for (int i = 0; i < a.Length; ++i)
- {
- if (a[i] == n)
- return true;
- }
- return false;
- }
- public static bool Contains(int[] a, int n)
- {
- for (int i = 0; i < a.Length; ++i)
- {
- if (a[i] == n)
- return true;
- }
- return false;
- }
- public static void Fill(
- byte[] buf,
- byte b)
- {
- int i = buf.Length;
- while (i > 0)
- {
- buf[--i] = b;
- }
- }
- public static byte[] CopyOf(byte[] data, int newLength)
- {
- byte[] tmp = new byte[newLength];
- Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
- return tmp;
- }
- public static char[] CopyOf(char[] data, int newLength)
- {
- char[] tmp = new char[newLength];
- Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
- return tmp;
- }
- public static int[] CopyOf(int[] data, int newLength)
- {
- int[] tmp = new int[newLength];
- Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
- return tmp;
- }
- public static long[] CopyOf(long[] data, int newLength)
- {
- long[] tmp = new long[newLength];
- Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
- return tmp;
- }
- public static BigInteger[] CopyOf(BigInteger[] data, int newLength)
- {
- BigInteger[] tmp = new BigInteger[newLength];
- Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
- return tmp;
- }
- /**
- * Make a copy of a range of bytes from the passed in data array. The range can
- * extend beyond the end of the input array, in which case the return array will
- * be padded with zeroes.
- *
- * @param data the array from which the data is to be copied.
- * @param from the start index at which the copying should take place.
- * @param to the final index of the range (exclusive).
- *
- * @return a new byte array containing the range given.
- */
- public static byte[] CopyOfRange(byte[] data, int from, int to)
- {
- int newLength = GetLength(from, to);
- byte[] tmp = new byte[newLength];
- Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
- return tmp;
- }
- public static int[] CopyOfRange(int[] data, int from, int to)
- {
- int newLength = GetLength(from, to);
- int[] tmp = new int[newLength];
- Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
- return tmp;
- }
- public static long[] CopyOfRange(long[] data, int from, int to)
- {
- int newLength = GetLength(from, to);
- long[] tmp = new long[newLength];
- Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
- return tmp;
- }
- public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to)
- {
- int newLength = GetLength(from, to);
- BigInteger[] tmp = new BigInteger[newLength];
- Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
- return tmp;
- }
- private static int GetLength(int from, int to)
- {
- int newLength = to - from;
- if (newLength < 0)
- throw new ArgumentException(from + " > " + to);
- return newLength;
- }
- public static byte[] Append(byte[] a, byte b)
- {
- if (a == null)
- return new byte[] { b };
- int length = a.Length;
- byte[] result = new byte[length + 1];
- Array.Copy(a, 0, result, 0, length);
- result[length] = b;
- return result;
- }
- public static short[] Append(short[] a, short b)
- {
- if (a == null)
- return new short[] { b };
- int length = a.Length;
- short[] result = new short[length + 1];
- Array.Copy(a, 0, result, 0, length);
- result[length] = b;
- return result;
- }
- public static int[] Append(int[] a, int b)
- {
- if (a == null)
- return new int[] { b };
- int length = a.Length;
- int[] result = new int[length + 1];
- Array.Copy(a, 0, result, 0, length);
- result[length] = b;
- return result;
- }
- public static byte[] Concatenate(byte[] a, byte[] b)
- {
- if (a == null)
- return Clone(b);
- if (b == null)
- return Clone(a);
- byte[] rv = new byte[a.Length + b.Length];
- Array.Copy(a, 0, rv, 0, a.Length);
- Array.Copy(b, 0, rv, a.Length, b.Length);
- return rv;
- }
- public static byte[] ConcatenateAll(params byte[][] vs)
- {
- byte[][] nonNull = new byte[vs.Length][];
- int count = 0;
- int totalLength = 0;
- for (int i = 0; i < vs.Length; ++i)
- {
- byte[] v = vs[i];
- if (v != null)
- {
- nonNull[count++] = v;
- totalLength += v.Length;
- }
- }
- byte[] result = new byte[totalLength];
- int pos = 0;
- for (int j = 0; j < count; ++j)
- {
- byte[] v = nonNull[j];
- Array.Copy(v, 0, result, pos, v.Length);
- pos += v.Length;
- }
- return result;
- }
- public static int[] Concatenate(int[] a, int[] b)
- {
- if (a == null)
- return Clone(b);
- if (b == null)
- return Clone(a);
- int[] rv = new int[a.Length + b.Length];
- Array.Copy(a, 0, rv, 0, a.Length);
- Array.Copy(b, 0, rv, a.Length, b.Length);
- return rv;
- }
- public static byte[] Prepend(byte[] a, byte b)
- {
- if (a == null)
- return new byte[] { b };
- int length = a.Length;
- byte[] result = new byte[length + 1];
- Array.Copy(a, 0, result, 1, length);
- result[0] = b;
- return result;
- }
- public static short[] Prepend(short[] a, short b)
- {
- if (a == null)
- return new short[] { b };
- int length = a.Length;
- short[] result = new short[length + 1];
- Array.Copy(a, 0, result, 1, length);
- result[0] = b;
- return result;
- }
- public static int[] Prepend(int[] a, int b)
- {
- if (a == null)
- return new int[] { b };
- int length = a.Length;
- int[] result = new int[length + 1];
- Array.Copy(a, 0, result, 1, length);
- result[0] = b;
- return result;
- }
- public static byte[] Reverse(byte[] a)
- {
- if (a == null)
- return null;
- int p1 = 0, p2 = a.Length;
- byte[] result = new byte[p2];
- while (--p2 >= 0)
- {
- result[p2] = a[p1++];
- }
- return result;
- }
- public static int[] Reverse(int[] a)
- {
- if (a == null)
- return null;
- int p1 = 0, p2 = a.Length;
- int[] result = new int[p2];
- while (--p2 >= 0)
- {
- result[p2] = a[p1++];
- }
- return result;
- }
- }
- }
- #endif
|