summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1Sequence.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2019-08-01 20:30:22 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2019-08-01 20:30:22 +0700
commitf06dc7c2f2e578c77bdb53ff981c578c3fe017eb (patch)
treeda2e616d2554028d0314681abacc5632aa523627 /crypto/src/asn1/Asn1Sequence.cs
parentDon't create a SecureRandom unnecessarily (diff)
downloadBouncyCastle.NET-ed25519-f06dc7c2f2e578c77bdb53ff981c578c3fe017eb.tar.xz
Rewrite Asn1Sequence/Set to store elements as Asn1Encodable[]
Diffstat (limited to 'crypto/src/asn1/Asn1Sequence.cs')
-rw-r--r--crypto/src/asn1/Asn1Sequence.cs112
1 files changed, 48 insertions, 64 deletions
diff --git a/crypto/src/asn1/Asn1Sequence.cs b/crypto/src/asn1/Asn1Sequence.cs
index 849f5e308..854c81590 100644
--- a/crypto/src/asn1/Asn1Sequence.cs
+++ b/crypto/src/asn1/Asn1Sequence.cs
@@ -10,7 +10,8 @@ namespace Org.BouncyCastle.Asn1
     public abstract class Asn1Sequence
         : Asn1Object, IEnumerable
     {
-        private readonly IList seq;
+        // NOTE: Only non-readonly to support LazyDerSequence
+        internal Asn1Encodable[] elements;
 
         /**
          * return an Asn1Sequence from the given object.
@@ -106,21 +107,38 @@ namespace Org.BouncyCastle.Asn1
             throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
         }
 
-        protected internal Asn1Sequence(
-            int capacity)
+        protected internal Asn1Sequence()
         {
-            seq = Platform.CreateArrayList(capacity);
+            this.elements = Asn1EncodableVector.EmptyElements;
         }
 
-        public virtual IEnumerator GetEnumerator()
+        protected internal Asn1Sequence(Asn1Encodable element)
+        {
+            if (null == element)
+                throw new ArgumentNullException("element");
+
+            this.elements = new Asn1Encodable[]{ element };
+        }
+
+        protected internal Asn1Sequence(params Asn1Encodable[] elements)
         {
-            return seq.GetEnumerator();
+            if (Arrays.IsNullOrContainsNull(elements))
+                throw new NullReferenceException("'elements' cannot be null, or contain null");
+
+            this.elements = Asn1EncodableVector.CloneElements(elements);
         }
 
-        [Obsolete("Use GetEnumerator() instead")]
-        public IEnumerator GetObjects()
+        protected internal Asn1Sequence(Asn1EncodableVector elementVector)
         {
-            return GetEnumerator();
+            if (null == elementVector)
+                throw new ArgumentNullException("elementVector");
+
+            this.elements = elementVector.TakeElements();
+        }
+
+        public virtual IEnumerator GetEnumerator()
+        {
+            return elements.GetEnumerator();
         }
 
         private class Asn1SequenceParserImpl
@@ -176,93 +194,59 @@ namespace Org.BouncyCastle.Asn1
          */
         public virtual Asn1Encodable this[int index]
         {
-            get { return (Asn1Encodable) seq[index]; }
+            get { return elements[index]; }
         }
 
-        [Obsolete("Use 'object[index]' syntax instead")]
-        public Asn1Encodable GetObjectAt(
-            int index)
-        {
-             return this[index];
-        }
-
-        [Obsolete("Use 'Count' property instead")]
-        public int Size
+        public virtual int Count
         {
-            get { return Count; }
+            get { return elements.Length; }
         }
 
-        public virtual int Count
+        public virtual Asn1Encodable[] ToArray()
         {
-            get { return seq.Count; }
+            return Asn1EncodableVector.CloneElements(elements);
         }
 
         protected override int Asn1GetHashCode()
         {
-            int hc = Count;
+            //return Arrays.GetHashCode(elements);
+            int i = elements.Length;
+            int hc = i + 1;
 
-            foreach (object o in this)
+            while (--i >= 0)
             {
-                hc *= 17;
-                if (o == null)
-                {
-                    hc ^= DerNull.Instance.GetHashCode();
-                }
-                else
-                {
-                    hc ^= o.GetHashCode();
-                }
+                hc *= 257;
+                hc ^= elements[i].ToAsn1Object().CallAsn1GetHashCode();
             }
 
             return hc;
         }
 
-        protected override bool Asn1Equals(
-            Asn1Object asn1Object)
+        protected override bool Asn1Equals(Asn1Object asn1Object)
         {
-            Asn1Sequence other = asn1Object as Asn1Sequence;
-
-            if (other == null)
+            Asn1Sequence that = asn1Object as Asn1Sequence;
+            if (null == that)
                 return false;
 
-            if (Count != other.Count)
+            int count = this.Count;
+            if (that.Count != count)
                 return false;
 
-            IEnumerator s1 = GetEnumerator();
-            IEnumerator s2 = other.GetEnumerator();
-
-            while (s1.MoveNext() && s2.MoveNext())
+            for (int i = 0; i < count; ++i)
             {
-                Asn1Object o1 = GetCurrent(s1).ToAsn1Object();
-                Asn1Object o2 = GetCurrent(s2).ToAsn1Object();
+                Asn1Object o1 = this.elements[i].ToAsn1Object();
+                Asn1Object o2 = that.elements[i].ToAsn1Object();
 
-                if (!o1.Equals(o2))
+                if (o1 != o2 && !o1.CallAsn1Equals(o2))
                     return false;
             }
 
             return true;
         }
 
-        private Asn1Encodable GetCurrent(IEnumerator e)
-        {
-            Asn1Encodable encObj = (Asn1Encodable)e.Current;
-
-            // unfortunately null was allowed as a substitute for DER null
-            if (encObj == null)
-                return DerNull.Instance;
-
-            return encObj;
-        }
-
-        protected internal void AddObject(
-            Asn1Encodable obj)
-        {
-            seq.Add(obj);
-        }
-
         public override string ToString()
         {
-            return CollectionUtilities.ToString(seq);
+            return CollectionUtilities.ToString(elements);
         }
     }
 }