1package org.bouncycastle.asn1;
2
3import java.io.IOException;
4
5import org.bouncycastle.util.Arrays;
6
7/**
8 * Carrier class for DER encoding BMPString object.
9 */
10public class DERBMPString
11    extends ASN1Primitive
12    implements ASN1String
13{
14    private final char[]  string;
15
16    /**
17     * return a BMP String from the given object.
18     *
19     * @param obj the object we want converted.
20     * @exception IllegalArgumentException if the object cannot be converted.
21     * @return a DERBMPString instance, or null.
22     */
23    public static DERBMPString getInstance(
24        Object  obj)
25    {
26        if (obj == null || obj instanceof DERBMPString)
27        {
28            return (DERBMPString)obj;
29        }
30
31        if (obj instanceof byte[])
32        {
33            try
34            {
35                return (DERBMPString)fromByteArray((byte[])obj);
36            }
37            catch (Exception e)
38            {
39                throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
40            }
41        }
42
43        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
44    }
45
46    /**
47     * return a BMP String from a tagged object.
48     *
49     * @param obj the tagged object holding the object we want
50     * @param explicit true if the object is meant to be explicitly
51     *              tagged false otherwise.
52     * @exception IllegalArgumentException if the tagged object cannot
53     *              be converted.
54     * @return a DERBMPString instance.
55     */
56    public static DERBMPString getInstance(
57        ASN1TaggedObject obj,
58        boolean          explicit)
59    {
60        ASN1Primitive o = obj.getObject();
61
62        if (explicit || o instanceof DERBMPString)
63        {
64            return getInstance(o);
65        }
66        else
67        {
68            return new DERBMPString(ASN1OctetString.getInstance(o).getOctets());
69        }
70    }
71
72    /**
73     * basic constructor - byte encoded string.
74     * @param string the encoded BMP STRING to wrap.
75     */
76    DERBMPString(
77        byte[]   string)
78    {
79        char[]  cs = new char[string.length / 2];
80
81        for (int i = 0; i != cs.length; i++)
82        {
83            cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
84        }
85
86        this.string = cs;
87    }
88
89    DERBMPString(char[] string)
90    {
91        this.string = string;
92    }
93
94    /**
95     * basic constructor
96     * @param string a String to wrap as a BMP STRING.
97     */
98    public DERBMPString(
99        String   string)
100    {
101        this.string = string.toCharArray();
102    }
103
104    public String getString()
105    {
106        return new String(string);
107    }
108
109    public String toString()
110    {
111        return getString();
112    }
113
114    public int hashCode()
115    {
116        return Arrays.hashCode(string);
117    }
118
119    protected boolean asn1Equals(
120        ASN1Primitive o)
121    {
122        if (!(o instanceof DERBMPString))
123        {
124            return false;
125        }
126
127        DERBMPString  s = (DERBMPString)o;
128
129        return Arrays.areEqual(string, s.string);
130    }
131
132    boolean isConstructed()
133    {
134        return false;
135    }
136
137    int encodedLength()
138    {
139        return 1 + StreamUtil.calculateBodyLength(string.length * 2) + (string.length * 2);
140    }
141
142    void encode(
143        ASN1OutputStream out)
144        throws IOException
145    {
146        out.write(BERTags.BMP_STRING);
147        out.writeLength(string.length * 2);
148
149        for (int i = 0; i != string.length; i++)
150        {
151            char c = string[i];
152
153            out.write((byte)(c >> 8));
154            out.write((byte)c);
155        }
156    }
157}
158