diff options
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/asn1/ASN1Enumerated.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/asn1/ASN1Enumerated.java | 73 |
1 files changed, 51 insertions, 22 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/asn1/ASN1Enumerated.java b/bcprov/src/main/java/org/bouncycastle/asn1/ASN1Enumerated.java index aa89eb5d..eff24a9f 100644 --- a/bcprov/src/main/java/org/bouncycastle/asn1/ASN1Enumerated.java +++ b/bcprov/src/main/java/org/bouncycastle/asn1/ASN1Enumerated.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.math.BigInteger; import org.bouncycastle.util.Arrays; -import org.bouncycastle.util.Properties; /** * Class representing the ASN.1 ENUMERATED type. @@ -13,6 +12,7 @@ public class ASN1Enumerated extends ASN1Primitive { private final byte[] bytes; + private final int start; /** * return an enumerated from the passed in object @@ -66,7 +66,7 @@ public class ASN1Enumerated } else { - return fromOctetString(((ASN1OctetString)o).getOctets()); + return fromOctetString(ASN1OctetString.getInstance(o).getOctets()); } } @@ -75,10 +75,15 @@ public class ASN1Enumerated * * @param value the value of this enumerated. */ - public ASN1Enumerated( - int value) + public ASN1Enumerated(int value) { - bytes = BigInteger.valueOf(value).toByteArray(); + if (value < 0) + { + throw new IllegalArgumentException("enumerated must be non-negative"); + } + + this.bytes = BigInteger.valueOf(value).toByteArray(); + this.start = 0; } /** @@ -86,10 +91,15 @@ public class ASN1Enumerated * * @param value the value of this enumerated. */ - public ASN1Enumerated( - BigInteger value) + public ASN1Enumerated(BigInteger value) { - bytes = value.toByteArray(); + if (value.signum() < 0) + { + throw new IllegalArgumentException("enumerated must be non-negative"); + } + + this.bytes = value.toByteArray(); + this.start = 0; } /** @@ -97,17 +107,19 @@ public class ASN1Enumerated * * @param bytes the value of this enumerated as an encoded BigInteger (signed). */ - public ASN1Enumerated( - byte[] bytes) + public ASN1Enumerated(byte[] bytes) { - if (!Properties.isOverrideSet("org.bouncycastle.asn1.allow_unsafe_integer")) + if (ASN1Integer.isMalformed(bytes)) { - if (ASN1Integer.isMalformed(bytes)) - { - throw new IllegalArgumentException("malformed enumerated"); - } + throw new IllegalArgumentException("malformed enumerated"); } + if (0 != (bytes[0] & 0x80)) + { + throw new IllegalArgumentException("enumerated must be non-negative"); + } + this.bytes = Arrays.clone(bytes); + this.start = ASN1Integer.signBytesToSkip(bytes); } public BigInteger getValue() @@ -115,6 +127,25 @@ public class ASN1Enumerated return new BigInteger(bytes); } + public boolean hasValue(BigInteger x) + { + return null != x + // Fast check to avoid allocation + && ASN1Integer.intValue(bytes, start, ASN1Integer.SIGN_EXT_SIGNED) == x.intValue() + && getValue().equals(x); + } + + public int intValueExact() + { + int count = bytes.length - start; + if (count > 4) + { + throw new ArithmeticException("ASN.1 Enumerated out of int range"); + } + + return ASN1Integer.intValue(bytes, start, ASN1Integer.SIGN_EXT_SIGNED); + } + boolean isConstructed() { return false; @@ -125,13 +156,11 @@ public class ASN1Enumerated return 1 + StreamUtil.calculateBodyLength(bytes.length) + bytes.length; } - void encode( - ASN1OutputStream out) - throws IOException + void encode(ASN1OutputStream out, boolean withTag) throws IOException { - out.writeEncoded(BERTags.ENUMERATED, bytes); + out.writeEncoded(withTag, BERTags.ENUMERATED, bytes); } - + boolean asn1Equals( ASN1Primitive o) { @@ -167,14 +196,14 @@ public class ASN1Enumerated if (value >= cache.length) { - return new ASN1Enumerated(Arrays.clone(enc)); + return new ASN1Enumerated(enc); } ASN1Enumerated possibleMatch = cache[value]; if (possibleMatch == null) { - possibleMatch = cache[value] = new ASN1Enumerated(Arrays.clone(enc)); + possibleMatch = cache[value] = new ASN1Enumerated(enc); } return possibleMatch; |