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