< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Utilities.Arrays
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\util\Arrays.cs
Line coverage
2%
Covered lines: 10
Uncovered lines: 473
Coverable lines: 483
Total lines: 718
Line coverage: 2%
Branch coverage
1%
Covered branches: 3
Total branches: 176
Branch coverage: 1.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\util\Arrays.cs

#LineLine coverage
 1using System;
 2using System.Text;
 3
 4using Renci.SshNet.Security.Org.BouncyCastle.Math;
 5
 6namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities
 7{
 8    /// <summary> General array utilities.</summary>
 9    internal abstract class Arrays
 10    {
 011        public static readonly byte[] EmptyBytes = new byte[0];
 012        public static readonly int[] EmptyInts = new int[0];
 13
 14        public static bool AreAllZeroes(byte[] buf, int off, int len)
 015        {
 016            uint bits = 0;
 017            for (int i = 0; i < len; ++i)
 018            {
 019                bits |= buf[off + i];
 020            }
 021            return bits == 0;
 022        }
 23
 24        public static bool AreEqual(
 25            bool[]  a,
 26            bool[]  b)
 027        {
 028            if (a == b)
 029                return true;
 30
 031            if (a == null || b == null)
 032                return false;
 33
 034            return HaveSameContents(a, b);
 035        }
 36
 37        public static bool AreEqual(
 38            char[] a,
 39            char[] b)
 040        {
 041            if (a == b)
 042                return true;
 43
 044            if (a == null || b == null)
 045                return false;
 46
 047            return HaveSameContents(a, b);
 048        }
 49
 50        /// <summary>
 51        /// Are two arrays equal.
 52        /// </summary>
 53        /// <param name="a">Left side.</param>
 54        /// <param name="b">Right side.</param>
 55        /// <returns>True if equal.</returns>
 56        public static bool AreEqual(
 57            byte[]  a,
 58            byte[]  b)
 059        {
 060            if (a == b)
 061                return true;
 62
 063            if (a == null || b == null)
 064                return false;
 65
 066            return HaveSameContents(a, b);
 067        }
 68
 69        [Obsolete("Use 'AreEqual' method instead")]
 70        public static bool AreSame(
 71            byte[]  a,
 72            byte[]  b)
 073        {
 074            return AreEqual(a, b);
 075        }
 76
 77        /// <summary>
 78        /// A constant time equals comparison - does not terminate early if
 79        /// test will fail.
 80        /// </summary>
 81        /// <param name="a">first array</param>
 82        /// <param name="b">second array</param>
 83        /// <returns>true if arrays equal, false otherwise.</returns>
 84        public static bool ConstantTimeAreEqual(
 85            byte[]  a,
 86            byte[]  b)
 087        {
 088            int i = a.Length;
 089            if (i != b.Length)
 090                return false;
 091            int cmp = 0;
 092            while (i != 0)
 093            {
 094                --i;
 095                cmp |= (a[i] ^ b[i]);
 096            }
 097            return cmp == 0;
 098        }
 99
 100        public static bool AreEqual(
 101            int[]  a,
 102            int[]  b)
 0103        {
 0104            if (a == b)
 0105                return true;
 106
 0107            if (a == null || b == null)
 0108                return false;
 109
 0110            return HaveSameContents(a, b);
 0111        }
 112
 113        public static bool AreEqual(uint[] a, uint[] b)
 0114        {
 0115            if (a == b)
 0116                return true;
 117
 0118            if (a == null || b == null)
 0119                return false;
 120
 0121            return HaveSameContents(a, b);
 0122        }
 123
 124        private static bool HaveSameContents(
 125            bool[] a,
 126            bool[] b)
 0127        {
 0128            int i = a.Length;
 0129            if (i != b.Length)
 0130                return false;
 0131            while (i != 0)
 0132            {
 0133                --i;
 0134                if (a[i] != b[i])
 0135                    return false;
 0136            }
 0137            return true;
 0138        }
 139
 140        private static bool HaveSameContents(
 141            char[] a,
 142            char[] b)
 0143        {
 0144            int i = a.Length;
 0145            if (i != b.Length)
 0146                return false;
 0147            while (i != 0)
 0148            {
 0149                --i;
 0150                if (a[i] != b[i])
 0151                    return false;
 0152            }
 0153            return true;
 0154        }
 155
 156        private static bool HaveSameContents(
 157            byte[]  a,
 158            byte[]  b)
 0159        {
 0160            int i = a.Length;
 0161            if (i != b.Length)
 0162                return false;
 0163            while (i != 0)
 0164            {
 0165                --i;
 0166                if (a[i] != b[i])
 0167                    return false;
 0168            }
 0169            return true;
 0170        }
 171
 172        private static bool HaveSameContents(
 173            int[]  a,
 174            int[]  b)
 0175        {
 0176            int i = a.Length;
 0177            if (i != b.Length)
 0178                return false;
 0179            while (i != 0)
 0180            {
 0181                --i;
 0182                if (a[i] != b[i])
 0183                    return false;
 0184            }
 0185            return true;
 0186        }
 187
 188        private static bool HaveSameContents(uint[] a, uint[] b)
 0189        {
 0190            int i = a.Length;
 0191            if (i != b.Length)
 0192                return false;
 0193            while (i != 0)
 0194            {
 0195                --i;
 0196                if (a[i] != b[i])
 0197                    return false;
 0198            }
 0199            return true;
 0200        }
 201
 202        public static string ToString(
 203            object[] a)
 0204        {
 0205            StringBuilder sb = new StringBuilder('[');
 0206            if (a.Length > 0)
 0207            {
 0208                sb.Append(a[0]);
 0209                for (int index = 1; index < a.Length; ++index)
 0210                {
 0211                    sb.Append(", ").Append(a[index]);
 0212                }
 0213            }
 0214            sb.Append(']');
 0215            return sb.ToString();
 0216        }
 217
 218        public static int GetHashCode(byte[] data)
 0219        {
 0220            if (data == null)
 0221            {
 0222                return 0;
 223            }
 224
 0225            int i = data.Length;
 0226            int hc = i + 1;
 227
 0228            while (--i >= 0)
 0229            {
 0230                hc *= 257;
 0231                hc ^= data[i];
 0232            }
 233
 0234            return hc;
 0235        }
 236
 237        public static int GetHashCode(byte[] data, int off, int len)
 0238        {
 0239            if (data == null)
 0240            {
 0241                return 0;
 242            }
 243
 0244            int i = len;
 0245            int hc = i + 1;
 246
 0247            while (--i >= 0)
 0248            {
 0249                hc *= 257;
 0250                hc ^= data[off + i];
 0251            }
 252
 0253            return hc;
 0254        }
 255
 256        public static int GetHashCode(int[] data)
 0257        {
 0258            if (data == null)
 0259                return 0;
 260
 0261            int i = data.Length;
 0262            int hc = i + 1;
 263
 0264            while (--i >= 0)
 0265            {
 0266                hc *= 257;
 0267                hc ^= data[i];
 0268            }
 269
 0270            return hc;
 0271        }
 272
 273        public static int GetHashCode(int[] data, int off, int len)
 0274        {
 0275            if (data == null)
 0276                return 0;
 277
 0278            int i = len;
 0279            int hc = i + 1;
 280
 0281            while (--i >= 0)
 0282            {
 0283                hc *= 257;
 0284                hc ^= data[off + i];
 0285            }
 286
 0287            return hc;
 0288        }
 289
 290        public static int GetHashCode(uint[] data)
 0291        {
 0292            if (data == null)
 0293                return 0;
 294
 0295            int i = data.Length;
 0296            int hc = i + 1;
 297
 0298            while (--i >= 0)
 0299            {
 0300                hc *= 257;
 0301                hc ^= (int)data[i];
 0302            }
 303
 0304            return hc;
 0305        }
 306
 307        public static int GetHashCode(uint[] data, int off, int len)
 0308        {
 0309            if (data == null)
 0310                return 0;
 311
 0312            int i = len;
 0313            int hc = i + 1;
 314
 0315            while (--i >= 0)
 0316            {
 0317                hc *= 257;
 0318                hc ^= (int)data[off + i];
 0319            }
 320
 0321            return hc;
 0322        }
 323
 324        public static int GetHashCode(ulong[] data)
 0325        {
 0326            if (data == null)
 0327                return 0;
 328
 0329            int i = data.Length;
 0330            int hc = i + 1;
 331
 0332            while (--i >= 0)
 0333            {
 0334                ulong di = data[i];
 0335                hc *= 257;
 0336                hc ^= (int)di;
 0337                hc *= 257;
 0338                hc ^= (int)(di >> 32);
 0339            }
 340
 0341            return hc;
 0342        }
 343
 344        public static int GetHashCode(ulong[] data, int off, int len)
 0345        {
 0346            if (data == null)
 0347                return 0;
 348
 0349            int i = len;
 0350            int hc = i + 1;
 351
 0352            while (--i >= 0)
 0353            {
 0354                ulong di = data[off + i];
 0355                hc *= 257;
 0356                hc ^= (int)di;
 0357                hc *= 257;
 0358                hc ^= (int)(di >> 32);
 0359            }
 360
 0361            return hc;
 0362        }
 363
 364        public static byte[] Clone(
 365            byte[] data)
 12366        {
 12367            return data == null ? null : (byte[])data.Clone();
 12368        }
 369
 370        public static byte[] Clone(
 371            byte[] data,
 372            byte[] existing)
 0373        {
 0374            if (data == null)
 0375            {
 0376                return null;
 377            }
 0378            if ((existing == null) || (existing.Length != data.Length))
 0379            {
 0380                return Clone(data);
 381            }
 0382            Array.Copy(data, 0, existing, 0, existing.Length);
 0383            return existing;
 0384        }
 385
 386        public static int[] Clone(
 387            int[] data)
 0388        {
 0389            return data == null ? null : (int[])data.Clone();
 0390        }
 391
 392        internal static uint[] Clone(uint[] data)
 0393        {
 0394            return data == null ? null : (uint[])data.Clone();
 0395        }
 396
 397        public static long[] Clone(long[] data)
 0398        {
 0399            return data == null ? null : (long[])data.Clone();
 0400        }
 401
 402        public static ulong[] Clone(
 403            ulong[] data)
 0404        {
 0405            return data == null ? null : (ulong[]) data.Clone();
 0406        }
 407
 408        public static ulong[] Clone(
 409            ulong[] data,
 410            ulong[] existing)
 0411        {
 0412            if (data == null)
 0413            {
 0414                return null;
 415            }
 0416            if ((existing == null) || (existing.Length != data.Length))
 0417            {
 0418                return Clone(data);
 419            }
 0420            Array.Copy(data, 0, existing, 0, existing.Length);
 0421            return existing;
 0422        }
 423
 424        public static bool Contains(byte[] a, byte n)
 0425        {
 0426            for (int i = 0; i < a.Length; ++i)
 0427            {
 0428                if (a[i] == n)
 0429                    return true;
 0430            }
 0431            return false;
 0432        }
 433
 434        public static bool Contains(short[] a, short n)
 0435        {
 0436            for (int i = 0; i < a.Length; ++i)
 0437            {
 0438                if (a[i] == n)
 0439                    return true;
 0440            }
 0441            return false;
 0442        }
 443
 444        public static bool Contains(int[] a, int n)
 0445        {
 0446            for (int i = 0; i < a.Length; ++i)
 0447            {
 0448                if (a[i] == n)
 0449                    return true;
 0450            }
 0451            return false;
 0452        }
 453
 454        public static void Fill(
 455            byte[]  buf,
 456            byte  b)
 1457        {
 1458            int i = buf.Length;
 129459            while (i > 0)
 128460            {
 128461                buf[--i] = b;
 128462            }
 1463        }
 464
 465        public static void Fill(byte[] buf, int from, int to, byte b)
 0466        {
 0467            for (int i = from; i < to; ++i)
 0468            {
 0469                buf[i] = b;
 0470            }
 0471        }
 472
 473        public static byte[] CopyOf(byte[] data, int newLength)
 0474        {
 0475            byte[] tmp = new byte[newLength];
 0476            Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
 0477            return tmp;
 0478        }
 479
 480        public static char[] CopyOf(char[] data, int newLength)
 0481        {
 0482            char[] tmp = new char[newLength];
 0483            Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
 0484            return tmp;
 0485        }
 486
 487        public static int[] CopyOf(int[] data, int newLength)
 0488        {
 0489            int[] tmp = new int[newLength];
 0490            Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
 0491            return tmp;
 0492        }
 493
 494        public static long[] CopyOf(long[] data, int newLength)
 0495        {
 0496            long[] tmp = new long[newLength];
 0497            Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
 0498            return tmp;
 0499        }
 500
 501        public static BigInteger[] CopyOf(BigInteger[] data, int newLength)
 0502        {
 0503            BigInteger[] tmp = new BigInteger[newLength];
 0504            Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length));
 0505            return tmp;
 0506        }
 507
 508        /**
 509         * Make a copy of a range of bytes from the passed in data array. The range can
 510         * extend beyond the end of the input array, in which case the return array will
 511         * be padded with zeroes.
 512         *
 513         * @param data the array from which the data is to be copied.
 514         * @param from the start index at which the copying should take place.
 515         * @param to the final index of the range (exclusive).
 516         *
 517         * @return a new byte array containing the range given.
 518         */
 519        public static byte[] CopyOfRange(byte[] data, int from, int to)
 0520        {
 0521            int newLength = GetLength(from, to);
 0522            byte[] tmp = new byte[newLength];
 0523            Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
 0524            return tmp;
 0525        }
 526
 527        public static int[] CopyOfRange(int[] data, int from, int to)
 0528        {
 0529            int newLength = GetLength(from, to);
 0530            int[] tmp = new int[newLength];
 0531            Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
 0532            return tmp;
 0533        }
 534
 535        public static long[] CopyOfRange(long[] data, int from, int to)
 0536        {
 0537            int newLength = GetLength(from, to);
 0538            long[] tmp = new long[newLength];
 0539            Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
 0540            return tmp;
 0541        }
 542
 543        public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to)
 0544        {
 0545            int newLength = GetLength(from, to);
 0546            BigInteger[] tmp = new BigInteger[newLength];
 0547            Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from));
 0548            return tmp;
 0549        }
 550
 551        private static int GetLength(int from, int to)
 0552        {
 0553            int newLength = to - from;
 0554            if (newLength < 0)
 0555                throw new ArgumentException(from + " > " + to);
 0556            return newLength;
 0557        }
 558
 559        public static byte[] Append(byte[] a, byte b)
 0560        {
 0561            if (a == null)
 0562                return new byte[] { b };
 563
 0564            int length = a.Length;
 0565            byte[] result = new byte[length + 1];
 0566            Array.Copy(a, 0, result, 0, length);
 0567            result[length] = b;
 0568            return result;
 0569        }
 570
 571        public static short[] Append(short[] a, short b)
 0572        {
 0573            if (a == null)
 0574                return new short[] { b };
 575
 0576            int length = a.Length;
 0577            short[] result = new short[length + 1];
 0578            Array.Copy(a, 0, result, 0, length);
 0579            result[length] = b;
 0580            return result;
 0581        }
 582
 583        public static int[] Append(int[] a, int b)
 0584        {
 0585            if (a == null)
 0586                return new int[] { b };
 587
 0588            int length = a.Length;
 0589            int[] result = new int[length + 1];
 0590            Array.Copy(a, 0, result, 0, length);
 0591            result[length] = b;
 0592            return result;
 0593        }
 594
 595        public static byte[] Concatenate(byte[] a, byte[] b)
 0596        {
 0597            if (a == null)
 0598                return Clone(b);
 0599            if (b == null)
 0600                return Clone(a);
 601
 0602            byte[] rv = new byte[a.Length + b.Length];
 0603            Array.Copy(a, 0, rv, 0, a.Length);
 0604            Array.Copy(b, 0, rv, a.Length, b.Length);
 0605            return rv;
 0606        }
 607
 608        public static byte[] ConcatenateAll(params byte[][] vs)
 0609        {
 0610            byte[][] nonNull = new byte[vs.Length][];
 0611            int count = 0;
 0612            int totalLength = 0;
 613
 0614            for (int i = 0; i < vs.Length; ++i)
 0615            {
 0616                byte[] v = vs[i];
 0617                if (v != null)
 0618                {
 0619                    nonNull[count++] = v;
 0620                    totalLength += v.Length;
 0621                }
 0622            }
 623
 0624            byte[] result = new byte[totalLength];
 0625            int pos = 0;
 626
 0627            for (int j = 0; j < count; ++j)
 0628            {
 0629                byte[] v = nonNull[j];
 0630                Array.Copy(v, 0, result, pos, v.Length);
 0631                pos += v.Length;
 0632            }
 633
 0634            return result;
 0635        }
 636
 637        public static int[] Concatenate(int[] a, int[] b)
 0638        {
 0639            if (a == null)
 0640                return Clone(b);
 0641            if (b == null)
 0642                return Clone(a);
 643
 0644            int[] rv = new int[a.Length + b.Length];
 0645            Array.Copy(a, 0, rv, 0, a.Length);
 0646            Array.Copy(b, 0, rv, a.Length, b.Length);
 0647            return rv;
 0648        }
 649
 650        public static byte[] Prepend(byte[] a, byte b)
 0651        {
 0652            if (a == null)
 0653                return new byte[] { b };
 654
 0655            int length = a.Length;
 0656            byte[] result = new byte[length + 1];
 0657            Array.Copy(a, 0, result, 1, length);
 0658            result[0] = b;
 0659            return result;
 0660        }
 661
 662        public static short[] Prepend(short[] a, short b)
 0663        {
 0664            if (a == null)
 0665                return new short[] { b };
 666
 0667            int length = a.Length;
 0668            short[] result = new short[length + 1];
 0669            Array.Copy(a, 0, result, 1, length);
 0670            result[0] = b;
 0671            return result;
 0672        }
 673
 674        public static int[] Prepend(int[] a, int b)
 0675        {
 0676            if (a == null)
 0677                return new int[] { b };
 678
 0679            int length = a.Length;
 0680            int[] result = new int[length + 1];
 0681            Array.Copy(a, 0, result, 1, length);
 0682            result[0] = b;
 0683            return result;
 0684        }
 685
 686        public static byte[] Reverse(byte[] a)
 0687        {
 0688            if (a == null)
 0689                return null;
 690
 0691            int p1 = 0, p2 = a.Length;
 0692            byte[] result = new byte[p2];
 693
 0694            while (--p2 >= 0)
 0695            {
 0696                result[p2] = a[p1++];
 0697            }
 698
 0699            return result;
 0700        }
 701
 702        public static int[] Reverse(int[] a)
 0703        {
 0704            if (a == null)
 0705                return null;
 706
 0707            int p1 = 0, p2 = a.Length;
 0708            int[] result = new int[p2];
 709
 0710            while (--p2 >= 0)
 0711            {
 0712                result[p2] = a[p1++];
 0713            }
 714
 0715            return result;
 0716        }
 717    }
 718}

Methods/Properties

.cctor()
AreAllZeroes(System.Byte[],System.Int32,System.Int32)
AreEqual(System.Boolean[],System.Boolean[])
AreEqual(System.Char[],System.Char[])
AreEqual(System.Byte[],System.Byte[])
AreSame(System.Byte[],System.Byte[])
ConstantTimeAreEqual(System.Byte[],System.Byte[])
AreEqual(System.Int32[],System.Int32[])
AreEqual(System.UInt32[],System.UInt32[])
HaveSameContents(System.Boolean[],System.Boolean[])
HaveSameContents(System.Char[],System.Char[])
HaveSameContents(System.Byte[],System.Byte[])
HaveSameContents(System.Int32[],System.Int32[])
HaveSameContents(System.UInt32[],System.UInt32[])
ToString(System.Object[])
GetHashCode(System.Byte[])
GetHashCode(System.Byte[],System.Int32,System.Int32)
GetHashCode(System.Int32[])
GetHashCode(System.Int32[],System.Int32,System.Int32)
GetHashCode(System.UInt32[])
GetHashCode(System.UInt32[],System.Int32,System.Int32)
GetHashCode(System.UInt64[])
GetHashCode(System.UInt64[],System.Int32,System.Int32)
Clone(System.Byte[])
Clone(System.Byte[],System.Byte[])
Clone(System.Int32[])
Clone(System.UInt32[])
Clone(System.Int64[])
Clone(System.UInt64[])
Clone(System.UInt64[],System.UInt64[])
Contains(System.Byte[],System.Byte)
Contains(System.Int16[],System.Int16)
Contains(System.Int32[],System.Int32)
Fill(System.Byte[],System.Byte)
Fill(System.Byte[],System.Int32,System.Int32,System.Byte)
CopyOf(System.Byte[],System.Int32)
CopyOf(System.Char[],System.Int32)
CopyOf(System.Int32[],System.Int32)
CopyOf(System.Int64[],System.Int32)
CopyOf(Renci.SshNet.Security.Org.BouncyCastle.Math.BigInteger[],System.Int32)
CopyOfRange(System.Byte[],System.Int32,System.Int32)
CopyOfRange(System.Int32[],System.Int32,System.Int32)
CopyOfRange(System.Int64[],System.Int32,System.Int32)
CopyOfRange(Renci.SshNet.Security.Org.BouncyCastle.Math.BigInteger[],System.Int32,System.Int32)
GetLength(System.Int32,System.Int32)
Append(System.Byte[],System.Byte)
Append(System.Int16[],System.Int16)
Append(System.Int32[],System.Int32)
Concatenate(System.Byte[],System.Byte[])
ConcatenateAll(System.Byte[][])
Concatenate(System.Int32[],System.Int32[])
Prepend(System.Byte[],System.Byte)
Prepend(System.Int16[],System.Int16)
Prepend(System.Int32[],System.Int32)
Reverse(System.Byte[])
Reverse(System.Int32[])