• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.asn1;
2 
3 import java.io.IOException;
4 
5 /**
6  * A Definite length BIT STRING
7  */
8 public class DLBitString
9     extends ASN1BitString
10 {
11     /**
12      * return a Bit String that can be definite-length encoded from the passed in object.
13      *
14      * @param obj a DL or DER BitString or an object that can be converted into one.
15      * @exception IllegalArgumentException if the object cannot be converted.
16      * @return an ASN1BitString instance, or null.
17      */
getInstance( Object obj)18     public static ASN1BitString getInstance(
19         Object  obj)
20     {
21         if (obj == null || obj instanceof DLBitString)
22         {
23             return (DLBitString)obj;
24         }
25         if (obj instanceof DERBitString)
26         {
27             return (DERBitString)obj;
28         }
29         if (obj instanceof byte[])
30         {
31             try
32             {
33                 return (ASN1BitString)fromByteArray((byte[])obj);
34             }
35             catch (Exception e)
36             {
37                 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
38             }
39         }
40 
41         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
42     }
43 
44     /**
45      * return a Bit String from a tagged object.
46      *
47      * @param obj the tagged object holding the object we want
48      * @param explicit true if the object is meant to be explicitly
49      *              tagged false otherwise.
50      * @exception IllegalArgumentException if the tagged object cannot
51      *               be converted.
52      * @return an ASN1BitString instance, or null.
53      */
getInstance( ASN1TaggedObject obj, boolean explicit)54     public static ASN1BitString getInstance(
55         ASN1TaggedObject obj,
56         boolean          explicit)
57     {
58         ASN1Primitive o = obj.getObject();
59 
60         if (explicit || o instanceof DLBitString)
61         {
62             return getInstance(o);
63         }
64         else
65         {
66             return fromOctetString(ASN1OctetString.getInstance(o).getOctets());
67         }
68     }
69 
DLBitString(byte data, int padBits)70     protected DLBitString(byte data, int padBits)
71     {
72         super(data, padBits);
73     }
74 
75     /**
76      * @param data the octets making up the bit string.
77      * @param padBits the number of extra bits at the end of the string.
78      */
DLBitString( byte[] data, int padBits)79     public DLBitString(
80         byte[]  data,
81         int     padBits)
82     {
83         super(data, padBits);
84     }
85 
DLBitString( byte[] data)86     public DLBitString(
87         byte[]  data)
88     {
89         this(data, 0);
90     }
91 
DLBitString( int value)92     public DLBitString(
93         int value)
94     {
95         super(getBytes(value), getPadBits(value));
96     }
97 
DLBitString( ASN1Encodable obj)98     public DLBitString(
99         ASN1Encodable obj)
100         throws IOException
101     {
102         super(obj.toASN1Primitive().getEncoded(ASN1Encoding.DER), 0);
103     }
104 
isConstructed()105     boolean isConstructed()
106     {
107         return false;
108     }
109 
encodedLength()110     int encodedLength()
111     {
112         return 1 + StreamUtil.calculateBodyLength(data.length + 1) + data.length + 1;
113     }
114 
encode(ASN1OutputStream out, boolean withTag)115     void encode(ASN1OutputStream out, boolean withTag) throws IOException
116     {
117         out.writeEncoded(withTag, BERTags.BIT_STRING, (byte)padBits, data);
118     }
119 
toDLObject()120     ASN1Primitive toDLObject()
121     {
122         return this;
123     }
124 
fromOctetString(byte[] bytes)125     static DLBitString fromOctetString(byte[] bytes)
126     {
127         if (bytes.length < 1)
128         {
129             throw new IllegalArgumentException("truncated BIT STRING detected");
130         }
131 
132         int padBits = bytes[0];
133         byte[] data = new byte[bytes.length - 1];
134 
135         if (data.length != 0)
136         {
137             System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
138         }
139 
140         return new DLBitString(data, padBits);
141     }
142 }
143