summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-26 22:25:10 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-26 22:25:10 +0700
commit3535fc1c1b65880796f215a880ddab8cda70f516 (patch)
treead9587bc64d658aca4e22d0aa3f88a4bf4dcf994
parentReplace IX509Store API with new store/selector API (diff)
downloadBouncyCastle.NET-ed25519-3535fc1c1b65880796f215a880ddab8cda70f516.tar.xz
Implement generic IEnumerable in ASN.1 classes
-rw-r--r--crypto/src/asn1/ASN1Generator.cs1
-rw-r--r--crypto/src/asn1/Asn1EncodableVector.cs15
-rw-r--r--crypto/src/asn1/Asn1Sequence.cs13
-rw-r--r--crypto/src/asn1/Asn1Set.cs39
-rw-r--r--crypto/src/asn1/BerOctetString.cs78
-rw-r--r--crypto/src/asn1/LazyDLEnumerator.cs14
-rw-r--r--crypto/src/asn1/LazyDLSequence.cs4
-rw-r--r--crypto/src/asn1/LazyDLSet.cs4
8 files changed, 49 insertions, 119 deletions
diff --git a/crypto/src/asn1/ASN1Generator.cs b/crypto/src/asn1/ASN1Generator.cs
index d064b803a..cd2d06e72 100644
--- a/crypto/src/asn1/ASN1Generator.cs
+++ b/crypto/src/asn1/ASN1Generator.cs
@@ -1,4 +1,3 @@
-using System.Collections;
 using System.IO;
 
 namespace Org.BouncyCastle.Asn1
diff --git a/crypto/src/asn1/Asn1EncodableVector.cs b/crypto/src/asn1/Asn1EncodableVector.cs
index 987aa5298..7560968b6 100644
--- a/crypto/src/asn1/Asn1EncodableVector.cs
+++ b/crypto/src/asn1/Asn1EncodableVector.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections;
+using System.Collections.Generic;
 
 namespace Org.BouncyCastle.Asn1
 {
@@ -7,7 +8,7 @@ namespace Org.BouncyCastle.Asn1
      * Mutable class for building ASN.1 constructed objects such as SETs or SEQUENCEs.
      */
     public class Asn1EncodableVector
-        : IEnumerable
+        : IEnumerable<Asn1Encodable>
     {
         internal static readonly Asn1Encodable[] EmptyElements = new Asn1Encodable[0];
 
@@ -17,7 +18,7 @@ namespace Org.BouncyCastle.Asn1
         private int elementCount;
         private bool copyOnWrite;
 
-        public static Asn1EncodableVector FromEnumerable(IEnumerable e)
+        public static Asn1EncodableVector FromEnumerable(IEnumerable<Asn1Encodable> e)
         {
             Asn1EncodableVector v = new Asn1EncodableVector();
             foreach (Asn1Encodable obj in e)
@@ -140,9 +141,15 @@ namespace Org.BouncyCastle.Asn1
             get { return elementCount; }
         }
 
-        public IEnumerator GetEnumerator()
+        IEnumerator IEnumerable.GetEnumerator()
         {
-            return CopyElements().GetEnumerator();
+            return GetEnumerator();
+        }
+
+        public IEnumerator<Asn1Encodable> GetEnumerator()
+        {
+            IEnumerable<Asn1Encodable> e = CopyElements();
+            return e.GetEnumerator();
         }
 
         internal Asn1Encodable[] CopyElements()
diff --git a/crypto/src/asn1/Asn1Sequence.cs b/crypto/src/asn1/Asn1Sequence.cs
index d7f84d3e4..fea5a6984 100644
--- a/crypto/src/asn1/Asn1Sequence.cs
+++ b/crypto/src/asn1/Asn1Sequence.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
@@ -8,7 +9,7 @@ using Org.BouncyCastle.Utilities.Collections;
 namespace Org.BouncyCastle.Asn1
 {
     public abstract class Asn1Sequence
-        : Asn1Object, IEnumerable
+        : Asn1Object, IEnumerable<Asn1Encodable>
     {
         internal class Meta : Asn1UniversalType
         {
@@ -112,9 +113,15 @@ namespace Org.BouncyCastle.Asn1
             this.elements = elementVector.TakeElements();
         }
 
-        public virtual IEnumerator GetEnumerator()
+        IEnumerator IEnumerable.GetEnumerator()
         {
-            return elements.GetEnumerator();
+            return GetEnumerator();
+        }
+
+        public virtual IEnumerator<Asn1Encodable> GetEnumerator()
+        {
+            IEnumerable<Asn1Encodable> e = elements;
+            return e.GetEnumerator();
         }
 
         private class Asn1SequenceParserImpl
diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs
index f3b94121b..f9bfd21c3 100644
--- a/crypto/src/asn1/Asn1Set.cs
+++ b/crypto/src/asn1/Asn1Set.cs
@@ -1,20 +1,16 @@
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 
-#if PORTABLE
-using System.Collections.Generic;
-using System.Linq;
-#endif
-
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Collections;
 
 namespace Org.BouncyCastle.Asn1
 {
     public abstract class Asn1Set
-        : Asn1Object, IEnumerable
+        : Asn1Object, IEnumerable<Asn1Encodable>
     {
         internal class Meta : Asn1UniversalType
         {
@@ -140,9 +136,15 @@ namespace Org.BouncyCastle.Asn1
             this.isSorted = isSorted || elements.Length < 2;
         }
 
-        public virtual IEnumerator GetEnumerator()
+        IEnumerator IEnumerable.GetEnumerator()
         {
-            return elements.GetEnumerator();
+            return GetEnumerator();
+        }
+
+        public virtual IEnumerator<Asn1Encodable> GetEnumerator()
+        {
+            IEnumerable<Asn1Encodable> e = elements;
+            return e.GetEnumerator();
         }
 
         /**
@@ -260,14 +262,6 @@ namespace Org.BouncyCastle.Asn1
             if (count < 2)
                 return elements;
 
-#if PORTABLE
-            return 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
             byte[][] keys = new byte[count][];
             for (int i = 0; i < count; ++i)
             {
@@ -275,24 +269,13 @@ namespace Org.BouncyCastle.Asn1
             }
             Array.Sort(keys, elements, new DerComparer());
             return elements;
-#endif
         }
 
-#if PORTABLE
         private class DerComparer
             : IComparer<byte[]>
         {
-            public int Compare(byte[] x, byte[] y)
-            {
-                byte[] a = x, b = y;
-#else
-        private class DerComparer
-            : IComparer
-        {
-            public int Compare(object x, object y)
+            public int Compare(byte[] a, byte[] b)
             {
-                byte[] a = (byte[])x, b = (byte[])y;
-#endif
                 Debug.Assert(a.Length >= 2 && b.Length >= 2);
 
                 /*
diff --git a/crypto/src/asn1/BerOctetString.cs b/crypto/src/asn1/BerOctetString.cs
index 2ee508b34..5350b8416 100644
--- a/crypto/src/asn1/BerOctetString.cs
+++ b/crypto/src/asn1/BerOctetString.cs
@@ -1,13 +1,10 @@
 using System;
-using System.Collections;
 using System.Diagnostics;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1
 {
     public class BerOctetString
-        : DerOctetString, IEnumerable
+        : DerOctetString
     {
         private const int DefaultSegmentLimit = 1000;
 
@@ -54,19 +51,6 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        private static Asn1OctetString[] ToOctetStringArray(IEnumerable e)
-        {
-            IList list = Platform.CreateArrayList(e);
-
-            int count = list.Count;
-            Asn1OctetString[] v = new Asn1OctetString[count];
-            for (int i = 0; i < count; ++i)
-            {
-                v[i] = GetInstance(list[i]);
-            }
-            return v;
-        }
-
         private readonly int segmentLimit;
         private readonly Asn1OctetString[] elements;
 
@@ -97,17 +81,6 @@ namespace Org.BouncyCastle.Asn1
             this.segmentLimit = segmentLimit;
         }
 
-        /**
-         * return the DER octets that make up this string.
-         */
-		public IEnumerator GetEnumerator()
-		{
-			if (elements == null)
-                return new ChunkEnumerator(contents, segmentLimit);
-
-			return elements.GetEnumerator();
-		}
-
         internal override IAsn1Encoding GetEncoding(int encoding)
         {
             if (Asn1OutputStream.EncodingBer != encoding)
@@ -131,54 +104,5 @@ namespace Org.BouncyCastle.Asn1
             return new ConstructedILEncoding(tagClass, tagNo,
                 Asn1OutputStream.GetContentsEncodings(encoding, elements));
         }
-
-        private class ChunkEnumerator
-            : IEnumerator
-        {
-            private readonly byte[] octets;
-            private readonly int segmentLimit;
-
-            private DerOctetString currentSegment = null;
-            private int nextSegmentPos = 0;
-
-            internal ChunkEnumerator(byte[] octets, int segmentLimit)
-            {
-                this.octets = octets;
-                this.segmentLimit = segmentLimit;
-            }
-
-            public object Current
-            {
-                get
-                {
-                    if (null == currentSegment)
-                        throw new InvalidOperationException();
-
-                    return currentSegment;
-                }
-            }
-
-            public bool MoveNext()
-            {
-                if (nextSegmentPos >= octets.Length)
-                {
-                    this.currentSegment = null;
-                    return false;
-                }
-
-                int length = System.Math.Min(octets.Length - nextSegmentPos, segmentLimit);
-                byte[] segment = new byte[length];
-                Array.Copy(octets, nextSegmentPos, segment, 0, length);
-                this.currentSegment = new DerOctetString(segment);
-                this.nextSegmentPos += length;
-                return true;
-            }
-
-            public void Reset()
-            {
-                this.currentSegment = null;
-                this.nextSegmentPos = 0;
-            }
-        }
     }
 }
diff --git a/crypto/src/asn1/LazyDLEnumerator.cs b/crypto/src/asn1/LazyDLEnumerator.cs
index efe383e3d..5e1be8228 100644
--- a/crypto/src/asn1/LazyDLEnumerator.cs
+++ b/crypto/src/asn1/LazyDLEnumerator.cs
@@ -1,11 +1,12 @@
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 namespace Org.BouncyCastle.Asn1
 {
     internal class LazyDLEnumerator
-        : IEnumerator
+        : IEnumerator<Asn1Encodable>
     {
         private readonly byte[] m_contents;
 
@@ -19,7 +20,12 @@ namespace Org.BouncyCastle.Asn1
             Reset();
         }
 
-        public object Current
+        object IEnumerator.Current
+        {
+            get { return Current; }
+        }
+
+        public Asn1Encodable Current
         {
             get
             {
@@ -30,6 +36,10 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
+        public virtual void Dispose()
+        {
+        }
+
         public bool MoveNext()
         {
             return null != (this.m_current = ReadObject());
diff --git a/crypto/src/asn1/LazyDLSequence.cs b/crypto/src/asn1/LazyDLSequence.cs
index 96b9bbca1..d45706bb3 100644
--- a/crypto/src/asn1/LazyDLSequence.cs
+++ b/crypto/src/asn1/LazyDLSequence.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 namespace Org.BouncyCastle.Asn1
@@ -28,7 +28,7 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        public override IEnumerator GetEnumerator()
+        public override IEnumerator<Asn1Encodable> GetEnumerator()
         {
             byte[] encoded = GetContents();
             if (null != encoded)
diff --git a/crypto/src/asn1/LazyDLSet.cs b/crypto/src/asn1/LazyDLSet.cs
index 551200513..1b8b3ef40 100644
--- a/crypto/src/asn1/LazyDLSet.cs
+++ b/crypto/src/asn1/LazyDLSet.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 namespace Org.BouncyCastle.Asn1
@@ -28,7 +28,7 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        public override IEnumerator GetEnumerator()
+        public override IEnumerator<Asn1Encodable> GetEnumerator()
         {
             byte[] encoded = GetContents();
             if (null != encoded)