summary refs log tree commit diff
path: root/crypto/src/asn1/DefiniteLengthInputStream.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/DefiniteLengthInputStream.cs')
-rw-r--r--crypto/src/asn1/DefiniteLengthInputStream.cs64
1 files changed, 35 insertions, 29 deletions
diff --git a/crypto/src/asn1/DefiniteLengthInputStream.cs b/crypto/src/asn1/DefiniteLengthInputStream.cs
index 0e4d4c41f..ed5bd2446 100644
--- a/crypto/src/asn1/DefiniteLengthInputStream.cs
+++ b/crypto/src/asn1/DefiniteLengthInputStream.cs
@@ -21,7 +21,7 @@ namespace Org.BouncyCastle.Asn1
                 if (length < 0)
                     throw new ArgumentException("negative lengths not allowed", "length");
 
-                SetParentEofDetect(true);
+                SetParentEofDetect();
             }
 
             this._originalLength = length;
@@ -35,43 +35,49 @@ namespace Org.BouncyCastle.Asn1
 
 		public override int ReadByte()
         {
-			if (_remaining == 0)
-				return -1;
+            if (_remaining < 2)
+            {
+                if (_remaining == 0)
+                    return -1;
 
-			int b = _in.ReadByte();
+                int b = _in.ReadByte();
+                if (b < 0)
+                    throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
 
-			if (b < 0)
-				throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
+                _remaining = 0;
+                SetParentEofDetect();
 
-			if (--_remaining == 0)
-			{
-				SetParentEofDetect(true);
-			}
+                return b;
+            }
+            else
+            {
+                int b = _in.ReadByte();
+                if (b < 0)
+                    throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
 
-			return b;
+                --_remaining;
+                return b;
+            }
         }
 
-		public override int Read(
-			byte[]	buf,
-			int		off,
-			int		len)
+		public override int Read(byte[] buf, int off, int len)
 		{
-			if (_remaining == 0)
-				return 0;
+            if (_remaining == 0)
+                return 0;
 
-			int toRead = System.Math.Min(len, _remaining);
-			int numRead = _in.Read(buf, off, toRead);
+            int toRead = System.Math.Min(len, _remaining);
+            int numRead = _in.Read(buf, off, toRead);
 
-			if (numRead < 1)
-				throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
+            if (numRead < 1)
+                throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
 
-			if ((_remaining -= numRead) == 0)
-			{
-				SetParentEofDetect(true);
-			}
+            if ((_remaining -= numRead) == 0)
+            {
+                SetParentEofDetect();
+            }
 
-			return numRead;
-		}
+            return numRead;
+        }
 
         internal void ReadAllIntoByteArray(byte[] buf)
         {
@@ -88,7 +94,7 @@ namespace Org.BouncyCastle.Asn1
 
             if ((_remaining -= Streams.ReadFully(_in, buf, 0, buf.Length)) != 0)
                 throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
-            SetParentEofDetect(true);
+            SetParentEofDetect();
         }
 
         internal byte[] ToArray()
@@ -104,7 +110,7 @@ namespace Org.BouncyCastle.Asn1
             byte[] bytes = new byte[_remaining];
 			if ((_remaining -= Streams.ReadFully(_in, bytes, 0, bytes.Length)) != 0)
 				throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
-			SetParentEofDetect(true);
+			SetParentEofDetect();
 			return bytes;
 		}
     }