DERBoolean.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1;
2
3import java.io.IOException;
4
5import org.bouncycastle.util.Arrays;
6
7public class DERBoolean
8    extends ASN1Primitive
9{
10    private static final byte[] TRUE_VALUE = new byte[] { (byte)0xff };
11    private static final byte[] FALSE_VALUE = new byte[] { 0 };
12
13    // BEGIN android-changed
14    final private byte[]         value;
15    // END android-changed
16
17    public static final ASN1Boolean FALSE = new ASN1Boolean(false);
18    public static final ASN1Boolean TRUE  = new ASN1Boolean(true);
19
20
21    /**
22     * return a boolean from the passed in object.
23     *
24     * @exception IllegalArgumentException if the object cannot be converted.
25     */
26    public static ASN1Boolean getInstance(
27        Object  obj)
28    {
29        if (obj == null || obj instanceof ASN1Boolean)
30        {
31            return (ASN1Boolean)obj;
32        }
33
34        if (obj instanceof DERBoolean)
35        {
36            return ((DERBoolean)obj).isTrue() ? DERBoolean.TRUE : DERBoolean.FALSE;
37        }
38
39        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
40    }
41
42    /**
43     * return a DERBoolean from the passed in boolean.
44     */
45    public static ASN1Boolean getInstance(
46        boolean  value)
47    {
48        return (value ? TRUE : FALSE);
49    }
50
51    // BEGIN android-added
52    /**
53     * return a DERBoolean from the passed in array.
54     */
55    public static DERBoolean getInstance(
56        byte[] octets)
57    {
58        return (octets[0] != 0) ? TRUE : FALSE;
59    }
60
61    // END android-added
62    /**
63     * return a Boolean from a tagged object.
64     *
65     * @param obj the tagged object holding the object we want
66     * @param explicit true if the object is meant to be explicitly
67     *              tagged false otherwise.
68     * @exception IllegalArgumentException if the tagged object cannot
69     *               be converted.
70     */
71    public static DERBoolean getInstance(
72        ASN1TaggedObject obj,
73        boolean          explicit)
74    {
75        ASN1Primitive o = obj.getObject();
76
77        if (explicit || o instanceof DERBoolean)
78        {
79            return getInstance(o);
80        }
81        else
82        {
83            return ASN1Boolean.fromOctetString(((ASN1OctetString)o).getOctets());
84        }
85    }
86
87    // BEGIN android-changed
88    protected DERBoolean(
89    // END android-changed
90        byte[]       value)
91    {
92        if (value.length != 1)
93        {
94            throw new IllegalArgumentException("byte value should have 1 byte in it");
95        }
96
97        if (value[0] == 0)
98        {
99            this.value = FALSE_VALUE;
100        }
101        else if (value[0] == 0xff)
102        {
103            this.value = TRUE_VALUE;
104        }
105        else
106        {
107            this.value = Arrays.clone(value);
108        }
109    }
110
111    // BEGIN android-changed
112    protected DERBoolean(
113        boolean     value)
114    // END android-changed
115    {
116        this.value = (value) ? TRUE_VALUE : FALSE_VALUE;
117    }
118
119    public boolean isTrue()
120    {
121        return (value[0] != 0);
122    }
123
124    boolean isConstructed()
125    {
126        return false;
127    }
128
129    int encodedLength()
130    {
131        return 3;
132    }
133
134    void encode(
135        ASN1OutputStream out)
136        throws IOException
137    {
138        out.writeEncoded(BERTags.BOOLEAN, value);
139    }
140
141    protected boolean asn1Equals(
142        ASN1Primitive  o)
143    {
144        if ((o == null) || !(o instanceof DERBoolean))
145        {
146            return false;
147        }
148
149        return (value[0] == ((DERBoolean)o).value[0]);
150    }
151
152    public int hashCode()
153    {
154        return value[0];
155    }
156
157
158    public String toString()
159    {
160      return (value[0] != 0) ? "TRUE" : "FALSE";
161    }
162
163    static ASN1Boolean fromOctetString(byte[] value)
164    {
165        if (value.length != 1)
166        {
167            throw new IllegalArgumentException("byte value should have 1 byte in it");
168        }
169
170        if (value[0] == 0)
171        {
172            return FALSE;
173        }
174        else if (value[0] == 0xff)
175        {
176            return TRUE;
177        }
178        else
179        {
180            return new ASN1Boolean(value);
181        }
182    }
183}
184