summary refs log tree commit diff
path: root/crypto/src/asn1/LazyDLEnumerator.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-08 20:14:47 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-08 20:14:47 +0700
commit2f23f0acb5dba40ed9fb7e9d340c0dfaf3db035b (patch)
tree99ea424d4c8718f8db82a945d082c5f0f9e49bc8 /crypto/src/asn1/LazyDLEnumerator.cs
parentASN.1 refactoring (diff)
downloadBouncyCastle.NET-ed25519-2f23f0acb5dba40ed9fb7e9d340c0dfaf3db035b.tar.xz
Lazy ASN.1 enumeration and refactoring
Diffstat (limited to 'crypto/src/asn1/LazyDLEnumerator.cs')
-rw-r--r--crypto/src/asn1/LazyDLEnumerator.cs56
1 files changed, 56 insertions, 0 deletions
diff --git a/crypto/src/asn1/LazyDLEnumerator.cs b/crypto/src/asn1/LazyDLEnumerator.cs
new file mode 100644
index 000000000..efe383e3d
--- /dev/null
+++ b/crypto/src/asn1/LazyDLEnumerator.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections;
+using System.IO;
+
+namespace Org.BouncyCastle.Asn1
+{
+    internal class LazyDLEnumerator
+        : IEnumerator
+    {
+        private readonly byte[] m_contents;
+
+        private Asn1InputStream m_input;
+        private Asn1Object m_current;
+
+        internal LazyDLEnumerator(byte[] contents)
+        {
+            this.m_contents = contents;
+
+            Reset();
+        }
+
+        public object Current
+        {
+            get
+            {
+                if (null == m_current)
+                    throw new InvalidOperationException();
+
+                return m_current;
+            }
+        }
+
+        public bool MoveNext()
+        {
+            return null != (this.m_current = ReadObject());
+        }
+
+        public void Reset()
+        {
+            this.m_input = new LazyAsn1InputStream(m_contents);
+            this.m_current = null;
+        }
+
+        private Asn1Object ReadObject()
+        {
+            try
+            {
+                return m_input.ReadObject();
+            }
+            catch (IOException e)
+            {
+                throw new Asn1ParsingException("malformed ASN.1: " + e.Message, e);
+            }
+        }
+    }
+}