Skip to content

Commit

Permalink
removed isSorted boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Mar 10, 2023
1 parent a980a60 commit 0c57689
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
40 changes: 18 additions & 22 deletions core/src/main/java/org/bouncycastle/asn1/ASN1Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,13 @@ public static ASN1Set getInstance(ASN1TaggedObject taggedObject, boolean explici
}

protected final ASN1Encodable[] elements;
protected final boolean isSorted;

protected ASN1Encodable[] sortedElements;

protected ASN1Set()
{
this.elements = ASN1EncodableVector.EMPTY_ELEMENTS;
this.isSorted = true;
this.sortedElements = elements;
}

/**
Expand All @@ -188,7 +187,7 @@ protected ASN1Set(ASN1Encodable element)
}

this.elements = new ASN1Encodable[]{ element };
this.isSorted = true;
this.sortedElements = elements;
}

/**
Expand All @@ -215,7 +214,7 @@ protected ASN1Set(ASN1EncodableVector elementVector, boolean doSort)
}

this.elements = tmp;
this.isSorted = doSort || tmp.length < 2;
this.sortedElements = (doSort || tmp.length < 2) ? elements : null;
}

/**
Expand All @@ -237,13 +236,19 @@ protected ASN1Set(ASN1Encodable[] elements, boolean doSort)
}

this.elements = tmp;
this.isSorted = doSort || tmp.length < 2;
this.sortedElements = (doSort || tmp.length < 2) ? elements : null;
}

ASN1Set(boolean isSorted, ASN1Encodable[] elements)
{
this.elements = elements;
this.isSorted = isSorted || elements.length < 2;
this.sortedElements = (isSorted || elements.length < 2) ? elements : null;
}

ASN1Set(ASN1Encodable[] elements, ASN1Encodable[] sortedElements)
{
this.elements = elements;
this.sortedElements = sortedElements;
}

public Enumeration getObjects()
Expand Down Expand Up @@ -355,22 +360,13 @@ public int hashCode()
*/
ASN1Primitive toDERObject()
{
ASN1Encodable[] tmp;
if (isSorted)
if (sortedElements == null)
{
tmp = elements;
}
else
{
if (sortedElements == null)
{
sortedElements = (ASN1Encodable[])elements.clone();
sort(sortedElements);
}
tmp = sortedElements;
sortedElements = (ASN1Encodable[])elements.clone();
sort(sortedElements);
}

return new DERSet(true, tmp);
return new DERSet(true, sortedElements);
}

/**
Expand All @@ -379,7 +375,7 @@ ASN1Primitive toDERObject()
*/
ASN1Primitive toDLObject()
{
return new DLSet(isSorted, elements);
return new DLSet(elements, sortedElements);
}

boolean asn1Equals(ASN1Primitive other)
Expand Down Expand Up @@ -478,8 +474,8 @@ private static boolean lessThanOrEqual(byte[] a, byte[] b)
* primitive form accordingly. Failing to ignore the CONSTRUCTED bit could therefore lead to
* ordering inversions.
*/
int a0 = a[0] & ~BERTags.CONSTRUCTED & 0xFF;
int b0 = b[0] & ~BERTags.CONSTRUCTED & 0xFF;
int a0 = a[0] & (~BERTags.CONSTRUCTED & 0xff);
int b0 = b[0] & (~BERTags.CONSTRUCTED & 0xff);
if (a0 != b0)
{
return a0 < b0;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/bouncycastle/asn1/DERSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void encode(ASN1OutputStream out, boolean withTag) throws IOException

ASN1Primitive toDERObject()
{
return isSorted ? this : super.toDERObject();
return (sortedElements != null) ? this : super.toDERObject();
}

ASN1Primitive toDLObject()
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/bouncycastle/asn1/DLSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public DLSet(ASN1Encodable[] elements)
super(isSorted, elements);
}

DLSet(ASN1Encodable[] elements, ASN1Encodable[] sortedElements)
{
super(elements, sortedElements);
}

private int getContentsLength() throws IOException
{
if (contentsLength < 0)
Expand Down

0 comments on commit 0c57689

Please # to comment.