Skip to content

Commit

Permalink
Add ASN1RelativeOID cache
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdettman committed Mar 8, 2024
1 parent 1c34996 commit b093c24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ static ASN1Primitive createPrimitiveDERObject(
case PRINTABLE_STRING:
return ASN1PrintableString.createPrimitive(defIn.toByteArray());
case RELATIVE_OID:
return ASN1RelativeOID.createPrimitive(defIn.toByteArray(), false);
// TODO Ideally only clone if we used a buffer
return ASN1RelativeOID.createPrimitive(getBuffer(defIn, tmpBuffers), true);
case T61_STRING:
return ASN1T61String.createPrimitive(defIn.toByteArray());
case UNIVERSAL_STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public ASN1ObjectIdentifier intern()
return oid;
}

private static class OidHandle
static class OidHandle
{
private final int key;
private final byte[] contents;
Expand Down Expand Up @@ -452,10 +452,10 @@ static ASN1ObjectIdentifier createPrimitive(byte[] contents, boolean clone)
{
final OidHandle hdl = new OidHandle(contents);
ASN1ObjectIdentifier oid = pool.get(hdl);
if (oid == null)
if (oid != null)
{
return new ASN1ObjectIdentifier(contents, clone);
return oid;
}
return oid;
return new ASN1ObjectIdentifier(contents, clone);
}
}
11 changes: 11 additions & 0 deletions core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.bouncycastle.util.Arrays;

Expand Down Expand Up @@ -78,6 +80,9 @@ public static ASN1RelativeOID tryFromID(String identifier)

private static final long LONG_LIMIT = (Long.MAX_VALUE >> 7) - 0x7F;

private static final ConcurrentMap<ASN1ObjectIdentifier.OidHandle, ASN1RelativeOID> pool =
new ConcurrentHashMap<ASN1ObjectIdentifier.OidHandle, ASN1RelativeOID>();

private final byte[] contents;
private String identifier;

Expand Down Expand Up @@ -180,6 +185,12 @@ boolean encodeConstructed()

static ASN1RelativeOID createPrimitive(byte[] contents, boolean clone)
{
final ASN1ObjectIdentifier.OidHandle hdl = new ASN1ObjectIdentifier.OidHandle(contents);
ASN1RelativeOID oid = pool.get(hdl);
if (oid != null)
{
return oid;
}
return new ASN1RelativeOID(contents, clone);
}

Expand Down

0 comments on commit b093c24

Please sign in to comment.