Skip to content

Commit

Permalink
Use initialCapacity in ASN.1 templates for default int/bool
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored Feb 6, 2023
1 parent 9dd3bc8 commit 876a97d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand All @@ -113,7 +114,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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">

Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ internal void Encode(AsnWriter writer, Asn1Tag tag)

// 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))
Expand Down

0 comments on commit 876a97d

Please sign in to comment.