summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1Set.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/Asn1Set.cs')
-rw-r--r--crypto/src/asn1/Asn1Set.cs156
1 files changed, 58 insertions, 98 deletions
diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs
index 7fa072c0d..07605f5e1 100644
--- a/crypto/src/asn1/Asn1Set.cs
+++ b/crypto/src/asn1/Asn1Set.cs
@@ -16,7 +16,8 @@ namespace Org.BouncyCastle.Asn1
     abstract public class Asn1Set
         : Asn1Object, IEnumerable
     {
-        private readonly IList _set;
+        // NOTE: Only non-readonly to support LazyDerSequence
+        internal Asn1Encodable[] elements;
 
         /**
          * return an ASN1Set from the given object.
@@ -125,21 +126,38 @@ namespace Org.BouncyCastle.Asn1
             throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
         }
 
-        protected internal Asn1Set(
-            int capacity)
+        protected internal Asn1Set()
         {
-            _set = Platform.CreateArrayList(capacity);
+            this.elements = Asn1EncodableVector.EmptyElements;
         }
 
-        public virtual IEnumerator GetEnumerator()
+        protected internal Asn1Set(Asn1Encodable element)
+        {
+            if (null == element)
+                throw new ArgumentNullException("element");
+
+            this.elements = new Asn1Encodable[]{ element };
+        }
+
+        protected internal Asn1Set(params Asn1Encodable[] elements)
         {
-            return _set.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 Asn1Set(Asn1EncodableVector elementVector)
+        {
+            if (null == elementVector)
+                throw new ArgumentNullException("elementVector");
+
+            this.elements = elementVector.TakeElements();
+        }
+
+        public virtual IEnumerator GetEnumerator()
         {
-            return GetEnumerator();
+            return elements.GetEnumerator();
         }
 
         /**
@@ -150,35 +168,17 @@ namespace Org.BouncyCastle.Asn1
          */
         public virtual Asn1Encodable this[int index]
         {
-            get { return (Asn1Encodable) _set[index]; }
-        }
-
-        [Obsolete("Use 'object[index]' syntax instead")]
-        public Asn1Encodable GetObjectAt(
-            int index)
-        {
-             return this[index];
-        }
-
-        [Obsolete("Use 'Count' property instead")]
-        public int Size
-        {
-            get { return Count; }
+            get { return elements[index]; }
         }
 
         public virtual int Count
         {
-            get { return _set.Count; }
+            get { return elements.Length; }
         }
 
         public virtual Asn1Encodable[] ToArray()
         {
-            Asn1Encodable[] values = new Asn1Encodable[this.Count];
-            for (int i = 0; i < this.Count; ++i)
-            {
-                values[i] = this[i];
-            }
-            return values;
+            return Asn1EncodableVector.CloneElements(elements);
         }
 
         private class Asn1SetParserImpl
@@ -227,107 +227,67 @@ namespace Org.BouncyCastle.Asn1
 
         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)
         {
-            Asn1Set other = asn1Object as Asn1Set;
-
-            if (other == null)
+            Asn1Set that = asn1Object as Asn1Set;
+            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 Sort()
         {
-            if (_set.Count < 2)
+            if (elements.Length < 2)
                 return;
 
 #if PORTABLE
-            var sorted = _set.Cast<Asn1Encodable>()
-                             .Select(a => new { Item = a, Key = a.GetEncoded(Asn1Encodable.Der) })
-                             .OrderBy(t => t.Key, new DerComparer())
-                             .Select(t => t.Item)
-                             .ToList();
-
-            for (int i = 0; i < _set.Count; ++i)
-            {
-                _set[i] = sorted[i];
-            }
+            this.elements = elements
+                .Cast<Asn1Encodable>()
+                .Select(a => new { Item = a, Key = a.GetEncoded(Asn1Encodable.Der) })
+                .OrderBy(t => t.Key, new DerComparer())
+                .Select(t => t.Item)
+                .ToArray();
 #else
-            Asn1Encodable[] items = new Asn1Encodable[_set.Count];
-            byte[][] keys = new byte[_set.Count][];
-
-            for (int i = 0; i < _set.Count; ++i)
-            {
-                Asn1Encodable item = (Asn1Encodable)_set[i];
-                items[i] = item;
-                keys[i] = item.GetEncoded(Asn1Encodable.Der);
-            }
-
-            Array.Sort(keys, items, new DerComparer());
-
-            for (int i = 0; i < _set.Count; ++i)
+            int count = elements.Length;
+            byte[][] keys = new byte[count][];
+            for (int i = 0; i < count; ++i)
             {
-                _set[i] = items[i];
+                keys[i] = elements[i].GetEncoded(Asn1Encodable.Der);
             }
+            Array.Sort(keys, elements, new DerComparer());
 #endif
         }
 
-        protected internal void AddObject(Asn1Encodable obj)
-        {
-            _set.Add(obj);
-        }
-
         public override string ToString()
         {
-            return CollectionUtilities.ToString(_set);
+            return CollectionUtilities.ToString(elements);
         }
 
 #if PORTABLE