summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/asn1/Asn1Set.cs90
-rw-r--r--crypto/src/asn1/BerSet.cs4
-rw-r--r--crypto/src/asn1/DLSet.cs4
-rw-r--r--crypto/src/asn1/DerSet.cs20
-rw-r--r--crypto/src/asn1/LazyDLSet.cs18
5 files changed, 72 insertions, 64 deletions
diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs
index 2b3810e43..222a6c727 100644
--- a/crypto/src/asn1/Asn1Set.cs
+++ b/crypto/src/asn1/Asn1Set.cs
@@ -55,7 +55,7 @@ namespace Org.BouncyCastle.Asn1
                 }
             }
 
-            throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj");
+            throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
         }
 
         /**
@@ -78,22 +78,22 @@ namespace Org.BouncyCastle.Asn1
         }
 
         // NOTE: Only non-readonly to support LazyDLSet
-        internal Asn1Encodable[] elements;
-        internal bool isSorted;
+        internal Asn1Encodable[] m_elements;
+        internal Asn1Encodable[] m_sortedElements;
 
         protected internal Asn1Set()
         {
-            this.elements = Asn1EncodableVector.EmptyElements;
-            this.isSorted = true;
+            m_elements = Asn1EncodableVector.EmptyElements;
+            m_sortedElements = m_elements;
         }
 
         protected internal Asn1Set(Asn1Encodable element)
         {
             if (null == element)
-                throw new ArgumentNullException("element");
+                throw new ArgumentNullException(nameof(element));
 
-            this.elements = new Asn1Encodable[]{ element };
-            this.isSorted = true;
+            m_elements = new Asn1Encodable[]{ element };
+            m_sortedElements = m_elements;
         }
 
         protected internal Asn1Set(Asn1Encodable[] elements, bool doSort)
@@ -101,39 +101,41 @@ namespace Org.BouncyCastle.Asn1
             if (Arrays.IsNullOrContainsNull(elements))
                 throw new NullReferenceException("'elements' cannot be null, or contain null");
 
-            Asn1Encodable[] tmp = Asn1EncodableVector.CloneElements(elements);
-            if (doSort && tmp.Length >= 2)
+            elements = Asn1EncodableVector.CloneElements(elements);
+
+            if (doSort && elements.Length > 1)
             {
-                tmp = Sort(tmp);
+                Sort(elements);
             }
 
-            this.elements = tmp;
-            this.isSorted = doSort || tmp.Length < 2;
+            m_elements = elements;
+            m_sortedElements = doSort || elements.Length <= 1 ? elements : null;
         }
 
         protected internal Asn1Set(Asn1EncodableVector elementVector, bool doSort)
         {
             if (null == elementVector)
-                throw new ArgumentNullException("elementVector");
+                throw new ArgumentNullException(nameof(elementVector));
 
-            Asn1Encodable[] tmp;
-            if (doSort && elementVector.Count >= 2)
+            Asn1Encodable[] elements;
+            if (doSort && elementVector.Count > 1)
             {
-                tmp = Sort(elementVector.CopyElements());
+                elements = elementVector.CopyElements();
+                Sort(elements);
             }
             else
             {
-                tmp = elementVector.TakeElements();
+                elements = elementVector.TakeElements();
             }
 
-            this.elements = tmp;
-            this.isSorted = doSort || tmp.Length < 2;
+            m_elements = elements;
+            m_sortedElements = doSort || elementVector.Count <= 1 ? elements : null;
         }
 
         protected internal Asn1Set(bool isSorted, Asn1Encodable[] elements)
         {
-            this.elements = elements;
-            this.isSorted = isSorted || elements.Length < 2;
+            m_elements = elements;
+            m_sortedElements = isSorted || elements.Length <= 1 ? elements : null;
         }
 
         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
@@ -143,7 +145,7 @@ namespace Org.BouncyCastle.Asn1
 
         public virtual IEnumerator<Asn1Encodable> GetEnumerator()
         {
-            IEnumerable<Asn1Encodable> e = elements;
+            IEnumerable<Asn1Encodable> e = m_elements;
             return e.GetEnumerator();
         }
 
@@ -155,12 +157,12 @@ namespace Org.BouncyCastle.Asn1
          */
         public virtual Asn1Encodable this[int index]
         {
-            get { return elements[index]; }
+            get { return m_elements[index]; }
         }
 
         public virtual int Count
         {
-            get { return elements.Length; }
+            get { return m_elements.Length; }
         }
 
         public virtual T[] MapElements<T>(Func<Asn1Encodable, T> func)
@@ -170,14 +172,14 @@ namespace Org.BouncyCastle.Asn1
             T[] result = new T[count];
             for (int i = 0; i < count; ++i)
             {
-                result[i] = func(elements[i]);
+                result[i] = func(m_elements[i]);
             }
             return result;
         }
 
         public virtual Asn1Encodable[] ToArray()
         {
-            return Asn1EncodableVector.CloneElements(elements);
+            return Asn1EncodableVector.CloneElements(m_elements);
         }
 
         private class Asn1SetParserImpl
@@ -234,7 +236,7 @@ namespace Org.BouncyCastle.Asn1
             while (--i >= 0)
             {
                 hc *= 257;
-                hc ^= elements[i].ToAsn1Object().CallAsn1GetHashCode();
+                hc ^= m_elements[i].ToAsn1Object().CallAsn1GetHashCode();
             }
 
             return hc;
@@ -242,8 +244,7 @@ namespace Org.BouncyCastle.Asn1
 
         protected override bool Asn1Equals(Asn1Object asn1Object)
         {
-            Asn1Set that = asn1Object as Asn1Set;
-            if (null == that)
+            if (!(asn1Object is Asn1Set that))
                 return false;
 
             // NOTE: Call Count here (on both) to 'force' a LazyDerSet
@@ -253,8 +254,8 @@ namespace Org.BouncyCastle.Asn1
 
             for (int i = 0; i < count; ++i)
             {
-                Asn1Object o1 = this.elements[i].ToAsn1Object();
-                Asn1Object o2 = that.elements[i].ToAsn1Object();
+                Asn1Object o1 = this.m_elements[i].ToAsn1Object();
+                Asn1Object o2 = that.m_elements[i].ToAsn1Object();
 
                 if (!o1.Equals(o2))
                     return false;
@@ -265,27 +266,32 @@ namespace Org.BouncyCastle.Asn1
 
         public override string ToString()
         {
-            return CollectionUtilities.ToString(elements);
+            return CollectionUtilities.ToString(m_elements);
         }
 
-        internal static Asn1Encodable[] Sort(Asn1Encodable[] elements)
+        internal static void Sort(Asn1Encodable[] elements)
         {
             int count = elements.Length;
-            if (count < 2)
-                return elements;
-
-            byte[][] keys = new byte[count][];
-            for (int i = 0; i < count; ++i)
+            if (count > 1)
             {
-                keys[i] = elements[i].GetEncoded(Der);
+                byte[][] keys = new byte[count][];
+                for (int i = 0; i < count; ++i)
+                {
+                    keys[i] = elements[i].GetEncoded(Der);
+                }
+                Array.Sort(keys, elements, DerComparer.Instance);
             }
-            Array.Sort(keys, elements, new DerComparer());
-            return elements;
         }
 
         private class DerComparer
             : IComparer<byte[]>
         {
+            internal static DerComparer Instance = new DerComparer();
+
+            private DerComparer()
+            {
+            }
+
             public int Compare(byte[] a, byte[] b)
             {
                 Debug.Assert(a.Length >= 2 && b.Length >= 2);
diff --git a/crypto/src/asn1/BerSet.cs b/crypto/src/asn1/BerSet.cs
index 0a273b3f3..d75bfa46e 100644
--- a/crypto/src/asn1/BerSet.cs
+++ b/crypto/src/asn1/BerSet.cs
@@ -54,7 +54,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncoding(encoding);
 
             return new ConstructedILEncoding(Asn1Tags.Universal, Asn1Tags.Set,
-                Asn1OutputStream.GetContentsEncodings(encoding, elements));
+                Asn1OutputStream.GetContentsEncodings(encoding, m_elements));
         }
 
         internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
@@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncodingImplicit(encoding, tagClass, tagNo);
 
             return new ConstructedILEncoding(tagClass, tagNo,
-                Asn1OutputStream.GetContentsEncodings(encoding, elements));
+                Asn1OutputStream.GetContentsEncodings(encoding, m_elements));
         }
     }
 }
diff --git a/crypto/src/asn1/DLSet.cs b/crypto/src/asn1/DLSet.cs
index ba55be826..4f44718cb 100644
--- a/crypto/src/asn1/DLSet.cs
+++ b/crypto/src/asn1/DLSet.cs
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncoding(encoding);
 
             return new ConstructedDLEncoding(Asn1Tags.Universal, Asn1Tags.Set,
-                Asn1OutputStream.GetContentsEncodings(encoding, elements));
+                Asn1OutputStream.GetContentsEncodings(encoding, m_elements));
         }
 
         internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
@@ -61,7 +61,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncodingImplicit(encoding, tagClass, tagNo);
 
             return new ConstructedDLEncoding(tagClass, tagNo,
-                Asn1OutputStream.GetContentsEncodings(encoding, elements));
+                Asn1OutputStream.GetContentsEncodings(encoding, m_elements));
         }
     }
 }
diff --git a/crypto/src/asn1/DerSet.cs b/crypto/src/asn1/DerSet.cs
index 3e52094c3..db4c7b1a2 100644
--- a/crypto/src/asn1/DerSet.cs
+++ b/crypto/src/asn1/DerSet.cs
@@ -73,17 +73,19 @@ namespace Org.BouncyCastle.Asn1
 
         private Asn1Encodable[] GetSortedElements()
         {
-            if (isSorted)
-                return elements;
-
-            int count = elements.Length;
-            Asn1Object[] asn1Objects = new Asn1Object[count];
-            for (int i = 0; i < count; ++i)
-            {
-                asn1Objects[i] = elements[i].ToAsn1Object();
+			if (m_sortedElements == null)
+			{
+                int count = m_elements.Length;
+                Asn1Object[] asn1Objects = new Asn1Object[count];
+                for (int i = 0; i < count; ++i)
+                {
+                    asn1Objects[i] = m_elements[i].ToAsn1Object();
+                }
+				Sort(asn1Objects);
+                m_sortedElements = asn1Objects;
             }
 
-            return Sort(asn1Objects);
+			return m_sortedElements;
         }
     }
 }
diff --git a/crypto/src/asn1/LazyDLSet.cs b/crypto/src/asn1/LazyDLSet.cs
index 1b8b3ef40..7386e72ba 100644
--- a/crypto/src/asn1/LazyDLSet.cs
+++ b/crypto/src/asn1/LazyDLSet.cs
@@ -7,15 +7,15 @@ namespace Org.BouncyCastle.Asn1
     internal class LazyDLSet
         : DLSet
     {
-        private byte[] encoded;
+        private byte[] m_encoded;
 
         internal LazyDLSet(byte[] encoded)
             : base()
         {
             if (null == encoded)
-                throw new ArgumentNullException("encoded");
+                throw new ArgumentNullException(nameof(encoded));
 
-            this.encoded = encoded;
+            m_encoded = encoded;
         }
 
         public override Asn1Encodable this[int index]
@@ -97,16 +97,16 @@ namespace Org.BouncyCastle.Asn1
         {
             lock (this)
             {
-                if (null != encoded)
+                if (null != m_encoded)
                 {
-                    Asn1InputStream input = new LazyAsn1InputStream(encoded);
+                    Asn1InputStream input = new LazyAsn1InputStream(m_encoded);
                     try
                     {
                         Asn1EncodableVector v = input.ReadVector();
 
-                        this.elements = v.TakeElements();
-                        this.isSorted = elements.Length < 2;
-                        this.encoded = null;
+                        m_elements = v.TakeElements();
+                        m_sortedElements = m_elements.Length <= 1 ? m_elements : null;
+                        m_encoded = null;
                     }
                     catch (IOException e)
                     {
@@ -118,7 +118,7 @@ namespace Org.BouncyCastle.Asn1
 
         private byte[] GetContents()
         {
-            lock (this) return encoded;
+            lock (this) return m_encoded;
         }
     }
 }