From 4ecb2d166e5ad4edf544bc8bc60d03ef9f759e70 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 1 Aug 2019 17:22:34 +0700 Subject: Inline BuildEncodableVector to shrink recursive call stacks --- crypto/src/asn1/Asn1InputStream.cs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs index a94ae5235..0c7461c98 100644 --- a/crypto/src/asn1/Asn1InputStream.cs +++ b/crypto/src/asn1/Asn1InputStream.cs @@ -98,13 +98,13 @@ namespace Org.BouncyCastle.Asn1 // // yes, people actually do this... // - return new BerOctetString(BuildDerEncodableVector(defIn)); + return new BerOctetString(ReadVector(defIn)); case Asn1Tags.Sequence: return CreateDerSequence(defIn); case Asn1Tags.Set: return CreateDerSet(defIn); case Asn1Tags.External: - return new DerExternal(BuildDerEncodableVector(defIn)); + return new DerExternal(ReadVector(defIn)); default: throw new IOException("unknown tag " + tagNo + " encountered"); } @@ -113,12 +113,15 @@ namespace Org.BouncyCastle.Asn1 return CreatePrimitiveDerObject(tagNo, defIn, tmpBuffers); } - internal Asn1EncodableVector BuildEncodableVector() + internal virtual Asn1EncodableVector ReadVector(DefiniteLengthInputStream dIn) { - Asn1EncodableVector v = new Asn1EncodableVector(); + if (dIn.Remaining < 1) + return new Asn1EncodableVector(0); + Asn1InputStream subStream = new Asn1InputStream(dIn); + Asn1EncodableVector v = new Asn1EncodableVector(); Asn1Object o; - while ((o = ReadObject()) != null) + while ((o = subStream.ReadObject()) != null) { v.Add(o); } @@ -126,22 +129,16 @@ namespace Org.BouncyCastle.Asn1 return v; } - internal virtual Asn1EncodableVector BuildDerEncodableVector( - DefiniteLengthInputStream dIn) - { - return new Asn1InputStream(dIn).BuildEncodableVector(); - } - internal virtual DerSequence CreateDerSequence( DefiniteLengthInputStream dIn) { - return DerSequence.FromVector(BuildDerEncodableVector(dIn)); + return DerSequence.FromVector(ReadVector(dIn)); } internal virtual DerSet CreateDerSet( DefiniteLengthInputStream dIn) { - return DerSet.FromVector(BuildDerEncodableVector(dIn), false); + return DerSet.FromVector(ReadVector(dIn), false); } public Asn1Object ReadObject() -- cgit 1.5.1