summary refs log tree commit diff
path: root/crypto/src/asn1/DLTaggedObject.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-16 22:37:52 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-16 22:37:52 +0700
commitbe15049124860ccb7335b92215e8b8dfa1821bf9 (patch)
tree7a86ed2110021307465a617076d601ca812c69ed /crypto/src/asn1/DLTaggedObject.cs
parentMake cast more specific (diff)
downloadBouncyCastle.NET-ed25519-be15049124860ccb7335b92215e8b8dfa1821bf9.tar.xz
ASN.1: Staged encoding
Diffstat (limited to 'crypto/src/asn1/DLTaggedObject.cs')
-rw-r--r--crypto/src/asn1/DLTaggedObject.cs72
1 files changed, 11 insertions, 61 deletions
diff --git a/crypto/src/asn1/DLTaggedObject.cs b/crypto/src/asn1/DLTaggedObject.cs
index 314b42799..c06dd8e87 100644
--- a/crypto/src/asn1/DLTaggedObject.cs
+++ b/crypto/src/asn1/DLTaggedObject.cs
@@ -5,8 +5,6 @@ namespace Org.BouncyCastle.Asn1
     internal class DLTaggedObject
         : DerTaggedObject
     {
-        private int m_contentsLengthDL = -1;
-
         internal DLTaggedObject(int tagNo, Asn1Encodable obj)
             : base(tagNo, obj)
         {
@@ -38,68 +36,30 @@ namespace Org.BouncyCastle.Asn1
             get { return Ber; }
         }
 
-        internal override bool EncodeConstructed(int encoding)
+        internal override IAsn1Encoding GetEncoding(int encoding)
         {
             if (Asn1OutputStream.EncodingDer == encoding)
-                return base.EncodeConstructed(encoding);
-
-            // TODO[asn1] Use DL encoding when supported
-            //encoding = Asn1OutputStream.EncodingDL;
-
-            return IsExplicit() || GetBaseObject().ToAsn1Object().EncodeConstructed(encoding);
-        }
-
-        internal override int EncodedLength(int encoding, bool withID)
-        {
-            if (Asn1OutputStream.EncodingDer == encoding)
-                return base.EncodedLength(encoding, withID);
+                return base.GetEncoding(encoding);
 
             Asn1Object baseObject = GetBaseObject().ToAsn1Object();
-            bool withBaseID = IsExplicit();
-
-            int length = GetContentsLengthDL(baseObject, withBaseID);
 
-            if (withBaseID)
-            {
-                length += Asn1OutputStream.GetLengthOfDL(length);
-            }
+            if (!IsExplicit())
+                return baseObject.GetEncodingImplicit(encoding, TagClass, TagNo);
 
-            length += withID ? Asn1OutputStream.GetLengthOfIdentifier(TagNo) : 0;
-
-            return length;
+            return new ConstructedDLEncoding(TagClass, TagNo, new IAsn1Encoding[]{ baseObject.GetEncoding(encoding) });
         }
 
-        internal override void Encode(Asn1OutputStream asn1Out, bool withID)
+        internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
         {
-            if (Asn1OutputStream.EncodingDer == asn1Out.Encoding)
-            {
-                base.Encode(asn1Out, withID);
-                return;
-            }
-
-            // TODO[asn1] Use DL encoding when supported
-            //asn1Out = asn1Out.GetDLSubStream();
+            if (Asn1OutputStream.EncodingDer == encoding)
+                return base.GetEncodingImplicit(encoding, tagClass, tagNo);
 
             Asn1Object baseObject = GetBaseObject().ToAsn1Object();
-            bool withBaseID = IsExplicit();
-
-            if (withID)
-            {
-                int flags = TagClass;
-                if (withBaseID || baseObject.EncodeConstructed(asn1Out.Encoding))
-                {
-                    flags |= Asn1Tags.Constructed;
-                }
 
-                asn1Out.WriteIdentifier(true, flags, TagNo);
-            }
+            if (!IsExplicit())
+                return baseObject.GetEncodingImplicit(encoding, tagClass, tagNo);
 
-            if (withBaseID)
-            {
-                asn1Out.WriteDL(GetContentsLengthDL(baseObject, true));
-            }
-
-            baseObject.Encode(asn1Out, withBaseID);
+            return new ConstructedDLEncoding(tagClass, tagNo, new IAsn1Encoding[]{ baseObject.GetEncoding(encoding) });
         }
 
         internal override Asn1Sequence RebuildConstructed(Asn1Object asn1Object)
@@ -111,15 +71,5 @@ namespace Org.BouncyCastle.Asn1
         {
             return new DLTaggedObject(explicitness, tagClass, tagNo, obj);
         }
-
-        private int GetContentsLengthDL(Asn1Object baseObject, bool withBaseID)
-        {
-            if (m_contentsLengthDL < 0)
-            {
-                // TODO[asn1] Use DL encoding when supported
-                m_contentsLengthDL = baseObject.EncodedLength(Asn1OutputStream.EncodingBer, withBaseID);
-            }
-            return m_contentsLengthDL;
-        }
     }
 }