resolve darcs stupidity
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERBitString.java
index 0c888d5..56046a7 100644 (file)
@@ -8,6 +8,113 @@ public class DERBitString
     protected byte[]      data;
     protected int         padBits;
 
+    /**
+     * return the correct number of pad bits for a bit string defined in
+     * a 16 bit constant
+     */
+    static protected int getPadBits(
+        int bitString)
+    {
+        int val;
+
+        if (bitString == 0)
+        {
+            return 7;
+        }
+
+        if (bitString > 255)
+        {
+            val = ((bitString >> 8) & 0xFF);
+        }
+        else
+        {
+            val = (bitString & 0xFF);
+        }
+
+        int bits = 1;
+
+        while (((val <<= 1) & 0xFF) != 0)
+        {
+            bits++;
+        }
+
+        return 8 - bits;
+    }
+
+    /**
+     * return the correct number of bytes for a bit string defined in
+     * a 16 bit constant
+     */
+    static protected byte[] getBytes(
+        int bitString)
+    {
+        if (bitString > 255)
+        {
+            byte[]  bytes = new byte[2];
+
+            bytes[0] = (byte)(bitString & 0xFF);
+            bytes[1] = (byte)((bitString >> 8) & 0xFF);
+
+            return bytes;
+        }
+        else
+        {
+            byte[]  bytes = new byte[1];
+
+            bytes[0] = (byte)(bitString & 0xFF);
+
+            return bytes;
+        }
+    }
+
+    /**
+     * return a Bit String from the passed in object
+     *
+     * @exception IllegalArgumentException if the object cannot be converted.
+     */
+    public static DERBitString getInstance(
+        Object  obj)
+    {
+        if (obj == null || obj instanceof DERBitString)
+        {
+            return (DERBitString)obj;
+        }
+
+        if (obj instanceof ASN1OctetString)
+        {
+            byte[]  bytes = ((ASN1OctetString)obj).getOctets();
+            int     padBits = bytes[0];
+            byte[]  data = new byte[bytes.length - 1];
+
+            System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
+
+            return new DERBitString(data, padBits);
+        }
+
+        if (obj instanceof ASN1TaggedObject)
+        {
+            return getInstance(((ASN1TaggedObject)obj).getObject());
+        }
+
+        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
+    }
+
+    /**
+     * return a Bit String from a tagged object.
+     *
+     * @param obj the tagged object holding the object we want
+     * @param explicit true if the object is meant to be explicitly
+     *              tagged false otherwise.
+     * @exception IllegalArgumentException if the tagged object cannot
+     *               be converted.
+     */
+    public static DERBitString getInstance(
+        ASN1TaggedObject obj,
+        boolean          explicit)
+    {
+        return getInstance(obj.getObject());
+    }
+    
     protected DERBitString(
         byte    data,
         int     padBits)
@@ -36,7 +143,7 @@ public class DERBitString
     }
 
     public DERBitString(
-        DERObject  obj)
+        DEREncodable  obj)
     {
         try
         {
@@ -55,12 +162,6 @@ public class DERBitString
         }
     }
 
-    public DERBitString(
-        DEREncodable  obj)
-    {
-        this(obj.getDERObject());
-    }
-
     public byte[] getBytes()
     {
         return data;
@@ -82,4 +183,30 @@ public class DERBitString
 
         out.writeEncoded(BIT_STRING, bytes);
     }
+    
+    public boolean equals(
+        Object  o)
+    {
+        if (o == null || !(o instanceof DERBitString))
+        {
+            return false;
+        }
+
+        DERBitString  other = (DERBitString)o;
+
+        if (data.length != other.data.length)
+        {
+            return false;
+        }
+
+        for (int i = 0; i != data.length; i++)
+        {
+            if (data[i] != other.data[i])
+            {
+                return false;
+            }
+        }
+
+        return (padBits == other.padBits);
+    }
 }