Use initialCapacity in ASN.1 templates for default int/bool
authorKevin Jones <kevin@vcsjones.com>
Mon, 6 Feb 2023 18:07:54 +0000 (13:07 -0500)
committerGitHub <noreply@github.com>
Mon, 6 Feb 2023 18:07:54 +0000 (10:07 -0800)
src/libraries/Common/src/System/Security/Cryptography/Asn1/Pkcs12/MacData.xml.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1/PssParamsAsn.xml.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1/X509ExtensionAsn.xml.cs
src/libraries/Common/src/System/Security/Cryptography/Asn1/asn.xslt
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Asn1/Rfc3161TimeStampReq.xml.cs
src/libraries/System.Security.Cryptography.Pkcs/src/System/Security/Cryptography/Pkcs/Asn1/Rfc3161TstInfo.xml.cs
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/Asn1/BasicConstraintsAsn.xml.cs
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/Asn1/TbsCertificateAsn.xml.cs

index 3841b1f..25b6042 100644 (file)
@@ -48,7 +48,8 @@ namespace System.Security.Cryptography.Asn1.Pkcs12
 
             // DEFAULT value handler for IterationCount.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnManagedIntegerDerMaxEncodeSize = 6;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnManagedIntegerDerMaxEncodeSize);
                 tmp.WriteInteger(IterationCount);
 
                 if (!tmp.EncodedValueEquals(DefaultIterationCount))
index 5fae8bd..c14b284 100644 (file)
@@ -99,7 +99,8 @@ namespace System.Security.Cryptography.Asn1
 
             // DEFAULT value handler for SaltLength.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnManagedIntegerDerMaxEncodeSize = 6;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnManagedIntegerDerMaxEncodeSize);
                 tmp.WriteInteger(SaltLength);
 
                 if (!tmp.EncodedValueEquals(DefaultSaltLength))
@@ -113,7 +114,8 @@ namespace System.Security.Cryptography.Asn1
 
             // DEFAULT value handler for TrailerField.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnManagedIntegerDerMaxEncodeSize = 6;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnManagedIntegerDerMaxEncodeSize);
                 tmp.WriteInteger(TrailerField);
 
                 if (!tmp.EncodedValueEquals(DefaultTrailerField))
index 68024d6..d6c36b6 100644 (file)
@@ -49,7 +49,8 @@ namespace System.Security.Cryptography.Asn1
 
             // DEFAULT value handler for Critical.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnBoolDerEncodeSize = 3;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnBoolDerEncodeSize);
                 tmp.WriteBoolean(Critical);
 
                 if (!tmp.EncodedValueEquals(DefaultCritical))
index fbf29a9..581a72b 100644 (file)
@@ -276,8 +276,10 @@ namespace <xsl:value-of select="@namespace" />
       <xsl:when test="@defaultDerInit and not(@explicitTag)" xml:space="preserve">
 
             // DEFAULT value handler for <xsl:value-of select="@name" />.
-            {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);<xsl:apply-templates select="." mode="EncodeValue">
+            {<xsl:apply-templates select="." mode="AsnWriterDefaultDer">
+                      <xsl:with-param name="writerName" select="'tmp'" />
+                      <xsl:with-param name="indent" select="concat('    ', $indent)" />
+                    </xsl:apply-templates><xsl:apply-templates select="." mode="EncodeValue">
                       <xsl:with-param name="writerName" select="'tmp'" />
                       <xsl:with-param name="indent" select="concat('    ', $indent)" />
                     </xsl:apply-templates>
@@ -297,6 +299,21 @@ namespace <xsl:value-of select="@namespace" />
     </xsl:choose>
   </xsl:template>
 
+  <xsl:template match="*" mode="AsnWriterDefaultDer" xml:space="default">
+    <xsl:param name="writerName" />
+    <xsl:param name="indent" />
+    <xsl:choose>
+      <xsl:when test="self::asn:Boolean" xml:space="preserve">
+            <xsl:value-of select="$indent"/>const int AsnBoolDerEncodeSize = 3;
+            <xsl:value-of select="$indent"/>AsnWriter <xsl:value-of select="$writerName"/> = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnBoolDerEncodeSize);</xsl:when>
+      <xsl:when test="self::asn:Integer[@backingType = 'int']" xml:space="preserve">
+            <xsl:value-of select="$indent"/>const int AsnManagedIntegerDerMaxEncodeSize = 6;
+            <xsl:value-of select="$indent"/>AsnWriter <xsl:value-of select="$writerName"/> = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnManagedIntegerDerMaxEncodeSize);</xsl:when>
+      <xsl:otherwise xml:space="preserve">
+            <xsl:value-of select="$indent"/>AsnWriter <xsl:value-of select="$writerName"/> = new AsnWriter(AsnEncodingRules.DER);</xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
   <!-- Struct OPTIONAL -->
   <xsl:template match="asn:AsnType | asn:AnyValue | asn:Boolean | asn:Integer | asn:BitString | asn:NamedBitList | asn:OctetString | asn:Enumerated | asn:UtcTime | asn:GeneralizedTime" mode="EncodeOptional">
 
@@ -342,8 +359,10 @@ namespace <xsl:value-of select="@namespace" />
       <xsl:when test="@defaultDerInit and @explicitTag" xml:space="preserve">
 
             // DEFAULT value handler for <xsl:value-of select="@name" />.
-            {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);<xsl:apply-templates select="." mode="EncodeSimpleValue">
+            {<xsl:apply-templates select="." mode="AsnWriterDefaultDer">
+                      <xsl:with-param name="writerName" select="'tmp'" />
+                      <xsl:with-param name="indent" select="concat('    ', $indent)" />
+                </xsl:apply-templates><xsl:apply-templates select="." mode="EncodeSimpleValue">
                   <xsl:with-param name="writerName" select="'tmp'" />
                   <xsl:with-param name="indent" select="concat('    ', $indent)" />
                 </xsl:apply-templates>
index e195a52..5e1e12f 100644 (file)
@@ -66,7 +66,8 @@ namespace System.Security.Cryptography.Pkcs.Asn1
 
             // DEFAULT value handler for CertReq.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnBoolDerEncodeSize = 3;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnBoolDerEncodeSize);
                 tmp.WriteBoolean(CertReq);
 
                 if (!tmp.EncodedValueEquals(DefaultCertReq))
index 7b60686..918e0a1 100644 (file)
@@ -67,7 +67,8 @@ namespace System.Security.Cryptography.Pkcs.Asn1
 
             // DEFAULT value handler for Ordering.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnBoolDerEncodeSize = 3;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnBoolDerEncodeSize);
                 tmp.WriteBoolean(Ordering);
 
                 if (!tmp.EncodedValueEquals(DefaultOrdering))
index b090305..6debd8d 100644 (file)
@@ -40,7 +40,8 @@ namespace System.Security.Cryptography.X509Certificates.Asn1
 
             // DEFAULT value handler for CA.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnBoolDerEncodeSize = 3;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnBoolDerEncodeSize);
                 tmp.WriteBoolean(CA);
 
                 if (!tmp.EncodedValueEquals(DefaultCA))
index a37f1cf..4e7df30 100644 (file)
@@ -54,7 +54,8 @@ namespace System.Security.Cryptography.X509Certificates.Asn1
 
             // DEFAULT value handler for Version.
             {
-                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
+                const int AsnManagedIntegerDerMaxEncodeSize = 6;
+                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER, initialCapacity: AsnManagedIntegerDerMaxEncodeSize);
                 tmp.WriteInteger(Version);
 
                 if (!tmp.EncodedValueEquals(DefaultVersion))