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