X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fbouncycastle%2Fasn1%2FDERPrintableString.java;h=fdb5b1df38ca6eb499f51548f4af767ef3353e0c;hp=7834edff0a6f170831268d09e0c3ec6daf742b8a;hb=da1f843588c8bd2b2c7cc74a5b4ffff8d57ab712;hpb=e5e9355b4f4e0e2c8de9068a71c1e3cc26fa9905 diff --git a/src/org/bouncycastle/asn1/DERPrintableString.java b/src/org/bouncycastle/asn1/DERPrintableString.java index 7834edf..fdb5b1d 100644 --- a/src/org/bouncycastle/asn1/DERPrintableString.java +++ b/src/org/bouncycastle/asn1/DERPrintableString.java @@ -12,19 +12,61 @@ public class DERPrintableString String string; /** + * return a printable string from the passed in object. + * + * @exception IllegalArgumentException if the object cannot be converted. + */ + public static DERPrintableString getInstance( + Object obj) + { + if (obj == null || obj instanceof DERPrintableString) + { + return (DERPrintableString)obj; + } + + if (obj instanceof ASN1OctetString) + { + return new DERPrintableString(((ASN1OctetString)obj).getOctets()); + } + + if (obj instanceof ASN1TaggedObject) + { + return getInstance(((ASN1TaggedObject)obj).getObject()); + } + + throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); + } + + /** + * return a Printable String from a tagged object. + * + * @param obj the tagged object holding the object we want + * @param explicit true if the object is meant to be explicitly + * tagged false otherwise. + * @exception IllegalArgumentException if the tagged object cannot + * be converted. + */ + public static DERPrintableString getInstance( + ASN1TaggedObject obj, + boolean explicit) + { + return getInstance(obj.getObject()); + } + + /** * basic constructor - byte encoded string. */ public DERPrintableString( byte[] string) { - try - { - this.string = new String(string, "US-ASCII"); - } - catch(UnsupportedEncodingException e) + char[] cs = new char[string.length]; + + for (int i = 0; i != cs.length; i++) { - throw new RuntimeException("PANIC: " + e); + cs[i] = (char)(string[i] & 0xff); } + + this.string = new String(cs); } /** @@ -43,14 +85,15 @@ public class DERPrintableString public byte[] getOctets() { - try - { - return string.getBytes("US-ASCII"); - } - catch(UnsupportedEncodingException e) + char[] cs = string.toCharArray(); + byte[] bs = new byte[cs.length]; + + for (int i = 0; i != cs.length; i++) { - throw new RuntimeException("PANIC: " + e); + bs[i] = (byte)cs[i]; } + + return bs; } void encode( @@ -59,4 +102,22 @@ public class DERPrintableString { out.writeEncoded(PRINTABLE_STRING, this.getOctets()); } + + public int hashCode() + { + return this.getString().hashCode(); + } + + public boolean equals( + Object o) + { + if (!(o instanceof DERPrintableString)) + { + return false; + } + + DERPrintableString s = (DERPrintableString)o; + + return this.getString().equals(s.getString()); + } }