summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1InputStream.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-27 20:55:50 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-27 20:55:50 +0700
commit63f71f856580891128f6c4ab6e13298b382148c7 (patch)
tree0defe404f836b94acc587ee2183c1e128f2a75ca /crypto/src/asn1/Asn1InputStream.cs
parentUse string.Create when available (diff)
downloadBouncyCastle.NET-ed25519-63f71f856580891128f6c4ab6e13298b382148c7.tar.xz
FIx TODOs in Asn1InputStream
Diffstat (limited to '')
-rw-r--r--crypto/src/asn1/Asn1InputStream.cs25
1 files changed, 17 insertions, 8 deletions
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs
index b09322234..82ff77fda 100644
--- a/crypto/src/asn1/Asn1InputStream.cs
+++ b/crypto/src/asn1/Asn1InputStream.cs
@@ -340,12 +340,13 @@ namespace Org.BouncyCastle.Asn1
             return length;
         }
 
-        private static byte[] GetBuffer(DefiniteLengthInputStream defIn, byte[][] tmpBuffers)
+        private static bool GetBuffer(DefiniteLengthInputStream defIn, byte[][] tmpBuffers, out byte[] contents)
         {
             int len = defIn.Remaining;
             if (len >= tmpBuffers.Length)
             {
-                return defIn.ToArray();
+                contents = defIn.ToArray();
+                return false;
             }
 
             byte[] buf = tmpBuffers[len];
@@ -356,7 +357,8 @@ namespace Org.BouncyCastle.Asn1
 
             defIn.ReadAllIntoByteArray(buf);
 
-            return buf;
+            contents = buf;
+            return true;
         }
 
         internal static Asn1Object CreatePrimitiveDerObject(int tagNo, DefiniteLengthInputStream defIn,
@@ -367,13 +369,20 @@ namespace Org.BouncyCastle.Asn1
             case Asn1Tags.BmpString:
                 return CreateDerBmpString(defIn);
             case Asn1Tags.Boolean:
-                return DerBoolean.CreatePrimitive(GetBuffer(defIn, tmpBuffers));
+            {
+                GetBuffer(defIn, tmpBuffers, out var contents);
+                return DerBoolean.CreatePrimitive(contents);
+            }
             case Asn1Tags.Enumerated:
-                // TODO Ideally only clone if we used a buffer
-                return DerEnumerated.CreatePrimitive(GetBuffer(defIn, tmpBuffers), true);
+            {
+                bool usedBuffer = GetBuffer(defIn, tmpBuffers, out var contents);
+                return DerEnumerated.CreatePrimitive(contents, clone: usedBuffer);
+            }
             case Asn1Tags.ObjectIdentifier:
-                // TODO Ideally only clone if we used a buffer
-                return DerObjectIdentifier.CreatePrimitive(GetBuffer(defIn, tmpBuffers), true);
+            {
+                bool usedBuffer = GetBuffer(defIn, tmpBuffers, out var contents);
+                return DerObjectIdentifier.CreatePrimitive(contents, clone: usedBuffer);
+            }
             }
 
             byte[] bytes = defIn.ToArray();