1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DER BMPString object.
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERBMPString
9c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    extends ASN1Object
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements DERString
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    String  string;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a BMP String from the given object.
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the object we want converted.
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERBMPString getInstance(
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof DERBMPString)
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (DERBMPString)obj;
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a BMP String from a tagged object.
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              be converted.
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERBMPString getInstance(
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
446e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        DERObject o = obj.getObject();
456e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
466e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (explicit || o instanceof DERBMPString)
476e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
486e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return getInstance(o);
496e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
506e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        else
516e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
526e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return new DERBMPString(ASN1OctetString.getInstance(o).getOctets());
536e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * basic constructor - byte encoded string.
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERBMPString(
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]   string)
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        char[]  cs = new char[string.length / 2];
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != cs.length; i++)
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = new String(cs);
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * basic constructor
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERBMPString(
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        String   string)
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = string;
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getString()
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return string;
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
88c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return string;
90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.getString().hashCode();
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    protected boolean asn1Equals(
98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObject  o)
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(o instanceof DERBMPString))
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DERBMPString  s = (DERBMPString)o;
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.getString().equals(s.getString());
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    void encode(
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEROutputStream  out)
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        char[]  c = string.toCharArray();
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]  b = new byte[c.length * 2];
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != c.length; i++)
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            b[2 * i] = (byte)(c[i] >> 8);
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            b[2 * i + 1] = (byte)c[i];
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        out.writeEncoded(BMP_STRING, b);
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
126