summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-21 13:53:34 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-21 13:53:34 +0700
commitfd62c077b82454d94ee2419992c886ed821777e3 (patch)
tree2ecd30ac0d0894fd3981be7232aa6237ba65afb7
parentASN.1: Tagged object parser improvements (diff)
downloadBouncyCastle.NET-ed25519-fd62c077b82454d94ee2419992c886ed821777e3.tar.xz
Use Seek when available
-rw-r--r--crypto/src/util/io/PushbackStream.cs27
-rw-r--r--crypto/src/x509/X509AttrCertParser.cs29
-rw-r--r--crypto/src/x509/X509CertPairParser.cs21
-rw-r--r--crypto/src/x509/X509CertificateParser.cs29
-rw-r--r--crypto/src/x509/X509CrlParser.cs31
5 files changed, 82 insertions, 55 deletions
diff --git a/crypto/src/util/io/PushbackStream.cs b/crypto/src/util/io/PushbackStream.cs
index 954694259..d51e195ca 100644
--- a/crypto/src/util/io/PushbackStream.cs
+++ b/crypto/src/util/io/PushbackStream.cs
@@ -1,27 +1,24 @@
 using System;
 using System.IO;
 
-using Org.BouncyCastle.Asn1.Utilities;
-
 namespace Org.BouncyCastle.Utilities.IO
 {
 	public class PushbackStream
 		: FilterStream
 	{
-		private int buf = -1;
+		private int m_buf = -1;
 
-		public PushbackStream(
-			Stream s)
+		public PushbackStream(Stream s)
 			: base(s)
 		{
 		}
 
 		public override int ReadByte()
 		{
-			if (buf != -1)
+			if (m_buf != -1)
 			{
-				int tmp = buf;
-				buf = -1;
+				int tmp = m_buf;
+				m_buf = -1;
 				return tmp;
 			}
 
@@ -30,11 +27,13 @@ namespace Org.BouncyCastle.Utilities.IO
 
 		public override int Read(byte[] buffer, int offset, int count)
 		{
-			if (buf != -1 && count > 0)
+            if (count < 1)
+                return 0;
+
+			if (m_buf != -1)
 			{
-				// TODO Can this case be made more efficient?
-				buffer[offset] = (byte) buf;
-				buf = -1;
+				buffer[offset] = (byte)m_buf;
+				m_buf = -1;
 				return 1;
 			}
 
@@ -43,10 +42,10 @@ namespace Org.BouncyCastle.Utilities.IO
 
 		public virtual void Unread(int b)
 		{
-			if (buf != -1)
+			if (m_buf != -1)
 				throw new InvalidOperationException("Can only push back one byte");
 
-			buf = b & 0xFF;
+			m_buf = b & 0xFF;
 		}
 	}
 }
diff --git a/crypto/src/x509/X509AttrCertParser.cs b/crypto/src/x509/X509AttrCertParser.cs
index a5c07362e..ce708ed8d 100644
--- a/crypto/src/x509/X509AttrCertParser.cs
+++ b/crypto/src/x509/X509AttrCertParser.cs
@@ -131,20 +131,27 @@ namespace Org.BouncyCastle.X509
 					return null;
 				}
 
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
-
-				if (tag < 0)
-					return null;
-
-				pis.Unread(tag);
-
-				if (tag != 0x30)  // assume ascii PEM encoded.
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
+
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
+
+                if (tag != 0x30)  // assume ascii PEM encoded.
 				{
-					return ReadPemCertificate(pis);
+					return ReadPemCertificate(inStream);
 				}
 
-				return ReadDerCertificate(new Asn1InputStream(pis));
+				return ReadDerCertificate(new Asn1InputStream(inStream));
 			}
 			catch (Exception e)
 			{
diff --git a/crypto/src/x509/X509CertPairParser.cs b/crypto/src/x509/X509CertPairParser.cs
index 82612599b..89a8a8e96 100644
--- a/crypto/src/x509/X509CertPairParser.cs
+++ b/crypto/src/x509/X509CertPairParser.cs
@@ -62,15 +62,22 @@ namespace Org.BouncyCastle.X509
 
 			try
 			{
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
 
-				if (tag < 0)
-					return null;
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
 
-				pis.Unread(tag);
-
-				return ReadDerCrossCertificatePair(pis);
+                return ReadDerCrossCertificatePair(inStream);
 			}
 			catch (Exception e)
 			{
diff --git a/crypto/src/x509/X509CertificateParser.cs b/crypto/src/x509/X509CertificateParser.cs
index 8f0e7406c..a8bba7bc2 100644
--- a/crypto/src/x509/X509CertificateParser.cs
+++ b/crypto/src/x509/X509CertificateParser.cs
@@ -141,20 +141,27 @@ namespace Org.BouncyCastle.X509
 					return null;
 				}
 
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
-
-				if (tag < 0)
-					return null;
-
-				pis.Unread(tag);
-
-				if (tag != 0x30)  // assume ascii PEM encoded.
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
+
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
+
+                if (tag != 0x30)  // assume ascii PEM encoded.
 				{
-					return ReadPemCertificate(pis);
+					return ReadPemCertificate(inStream);
 				}
 
-				return ReadDerCertificate(new Asn1InputStream(pis));
+				return ReadDerCertificate(new Asn1InputStream(inStream));
 			}
 			catch (Exception e)
 			{
diff --git a/crypto/src/x509/X509CrlParser.cs b/crypto/src/x509/X509CrlParser.cs
index d830bb9a6..59f7a23c4 100644
--- a/crypto/src/x509/X509CrlParser.cs
+++ b/crypto/src/x509/X509CrlParser.cs
@@ -140,22 +140,29 @@ namespace Org.BouncyCastle.X509
 					return null;
 				}
 
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
-
-				if (tag < 0)
-					return null;
-
-				pis.Unread(tag);
-
-				if (tag != 0x30)	// assume ascii PEM encoded.
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
+
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
+
+                if (tag != 0x30)	// assume ascii PEM encoded.
 				{
-					return ReadPemCrl(pis);
+					return ReadPemCrl(inStream);
 				}
 
 				Asn1InputStream asn1 = lazyAsn1
-					?	new LazyAsn1InputStream(pis)
-					:	new Asn1InputStream(pis);
+					?	new LazyAsn1InputStream(inStream)
+					:	new Asn1InputStream(inStream);
 
 				return ReadDerCrl(asn1);
 			}