Skip to content

Commit

Permalink
Add capacity hint to OidCollection (#97106)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones committed Jan 18, 2024
1 parent fd44d5c commit a7b63ae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using Internal.Cryptography;
using System.Diagnostics;

namespace System.Security.Cryptography
{
public sealed class OidCollection : ICollection
{
private Oid[] _oids = Array.Empty<Oid>();
private Oid[] _oids;
private int _count;

public OidCollection() { }
public OidCollection()
{
_oids = [];
}

internal OidCollection(int initialCapacity)
{
Debug.Assert(initialCapacity >= 0);
_oids = initialCapacity == 0 ? [] : new Oid[initialCapacity];
}

public int Add(Oid oid)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,14 @@ public override void DecodeX509BasicConstraints2Extension(

public override void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollection usages)
{
OidCollection oids = new OidCollection();
OidCollection oids;

using (SafeEkuExtensionHandle eku = Interop.Crypto.DecodeExtendedKeyUsage(encoded, encoded.Length))
{
Interop.Crypto.CheckValidOpenSslHandle(eku);

int count = Interop.Crypto.GetX509EkuFieldCount(eku);
oids = new OidCollection(count);

for (int i = 0; i < count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ public OidCollection EnhancedKeyUsages
X509Pal.Instance.DecodeX509EnhancedKeyUsageExtension(RawData, out _enhancedKeyUsages);
_decoded = true;
}
OidCollection oids = new OidCollection();
foreach (Oid oid in _enhancedKeyUsages!)

OidCollection oids = new OidCollection(_enhancedKeyUsages!.Count);

foreach (Oid oid in _enhancedKeyUsages)
{
oids.Add(oid);
}

return oids;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ public void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCollectio
CryptDecodeObjectStructType.X509_ENHANCED_KEY_USAGE,
static delegate (void* pvDecoded, int cbDecoded)
{
var localUsages = new OidCollection();

Debug.Assert(cbDecoded >= sizeof(CERT_ENHKEY_USAGE));
CERT_ENHKEY_USAGE* pEnhKeyUsage = (CERT_ENHKEY_USAGE*)pvDecoded;
int count = pEnhKeyUsage->cUsageIdentifier;
var localUsages = new OidCollection(count);
for (int i = 0; i < count; i++)
{
IntPtr oidValuePointer = pEnhKeyUsage->rgpszUsageIdentifier[i];
Expand Down

0 comments on commit a7b63ae

Please sign in to comment.