summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java
diff options
context:
space:
mode:
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java')
-rw-r--r--bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java221
1 files changed, 24 insertions, 197 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java b/bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java
index 4852a796..d74bc00e 100644
--- a/bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java
+++ b/bcprov/src/main/java/org/bouncycastle/asn1/DERBitString.java
@@ -1,97 +1,13 @@
package org.bouncycastle.asn1;
-import java.io.ByteArrayOutputStream;
-import java.io.EOFException;
import java.io.IOException;
-import java.io.InputStream;
-
-import org.bouncycastle.util.Arrays;
-import org.bouncycastle.util.io.Streams;
+/**
+ * A BIT STRING with DER encoding.
+ */
public class DERBitString
- extends ASN1Primitive
- implements ASN1String
+ extends ASN1BitString
{
- private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-
- protected byte[] data;
- protected int padBits;
-
- /**
- * @param bitString an int containing the BIT STRING
- * @return the correct number of pad bits for a bit string defined in
- * a 32 bit constant
- */
- static protected int getPadBits(
- int bitString)
- {
- int val = 0;
- for (int i = 3; i >= 0; i--)
- {
- //
- // this may look a little odd, but if it isn't done like this pre jdk1.2
- // JVM's break!
- //
- if (i != 0)
- {
- if ((bitString >> (i * 8)) != 0)
- {
- val = (bitString >> (i * 8)) & 0xFF;
- break;
- }
- }
- else
- {
- if (bitString != 0)
- {
- val = bitString & 0xFF;
- break;
- }
- }
- }
-
- if (val == 0)
- {
- return 7;
- }
-
-
- int bits = 1;
-
- while (((val <<= 1) & 0xFF) != 0)
- {
- bits++;
- }
-
- return 8 - bits;
- }
-
- /**
- * @param bitString an int containing the BIT STRING
- * @return the correct number of bytes for a bit string defined in
- * a 32 bit constant
- */
- static protected byte[] getBytes(int bitString)
- {
- int bytes = 4;
- for (int i = 3; i >= 1; i--)
- {
- if ((bitString & (0xFF << (i * 8))) != 0)
- {
- break;
- }
- bytes--;
- }
-
- byte[] result = new byte[bytes];
- for (int i = 0; i < bytes; i++)
- {
- result[i] = (byte) ((bitString >> (i * 8)) & 0xFF);
- }
-
- return result;
- }
-
/**
* return a Bit String from the passed in object
*
@@ -106,6 +22,10 @@ public class DERBitString
{
return (DERBitString)obj;
}
+ if (obj instanceof DLBitString)
+ {
+ return new DERBitString(((DLBitString)obj).data, ((DLBitString)obj).padBits);
+ }
throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
}
@@ -140,9 +60,16 @@ public class DERBitString
byte data,
int padBits)
{
- this.data = new byte[1];
- this.data[0] = data;
- this.padBits = padBits;
+ this(toByteArray(data), padBits);
+ }
+
+ private static byte[] toByteArray(byte data)
+ {
+ byte[] rv = new byte[1];
+
+ rv[0] = data;
+
+ return rv;
}
/**
@@ -153,8 +80,7 @@ public class DERBitString
byte[] data,
int padBits)
{
- this.data = data;
- this.padBits = padBits;
+ super(data, padBits);
}
public DERBitString(
@@ -166,42 +92,14 @@ public class DERBitString
public DERBitString(
int value)
{
- this.data = getBytes(value);
- this.padBits = getPadBits(value);
+ super(getBytes(value), getPadBits(value));
}
public DERBitString(
ASN1Encodable obj)
throws IOException
{
- this.data = obj.toASN1Primitive().getEncoded(ASN1Encoding.DER);
- this.padBits = 0;
- }
-
- public byte[] getBytes()
- {
- return data;
- }
-
- public int getPadBits()
- {
- return padBits;
- }
-
-
- /**
- * @return the value of the bit string as an int (truncating if necessary)
- */
- public int intValue()
- {
- int value = 0;
-
- for (int i = 0; i != data.length && i != 4; i++)
- {
- value |= (data[i] & 0xff) << (8 * i);
- }
-
- return value;
+ super(obj.toASN1Primitive().getEncoded(ASN1Encoding.DER), 0);
}
boolean isConstructed()
@@ -218,64 +116,15 @@ public class DERBitString
ASN1OutputStream out)
throws IOException
{
- byte[] bytes = new byte[getBytes().length + 1];
+ byte[] string = derForm(data, padBits);
+ byte[] bytes = new byte[string.length + 1];
bytes[0] = (byte)getPadBits();
- System.arraycopy(getBytes(), 0, bytes, 1, bytes.length - 1);
+ System.arraycopy(string, 0, bytes, 1, bytes.length - 1);
out.writeEncoded(BERTags.BIT_STRING, bytes);
}
- public int hashCode()
- {
- return padBits ^ Arrays.hashCode(data);
- }
-
- protected boolean asn1Equals(
- ASN1Primitive o)
- {
- if (!(o instanceof DERBitString))
- {
- return false;
- }
-
- DERBitString other = (DERBitString)o;
-
- return this.padBits == other.padBits
- && Arrays.areEqual(this.data, other.data);
- }
-
- public String getString()
- {
- StringBuffer buf = new StringBuffer("#");
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- ASN1OutputStream aOut = new ASN1OutputStream(bOut);
-
- try
- {
- aOut.writeObject(this);
- }
- catch (IOException e)
- {
- throw new RuntimeException("internal error encoding BitString");
- }
-
- byte[] string = bOut.toByteArray();
-
- for (int i = 0; i != string.length; i++)
- {
- buf.append(table[(string[i] >>> 4) & 0xf]);
- buf.append(table[string[i] & 0xf]);
- }
-
- return buf.toString();
- }
-
- public String toString()
- {
- return getString();
- }
-
static DERBitString fromOctetString(byte[] bytes)
{
if (bytes.length < 1)
@@ -293,26 +142,4 @@ public class DERBitString
return new DERBitString(data, padBits);
}
-
- static DERBitString fromInputStream(int length, InputStream stream)
- throws IOException
- {
- if (length < 1)
- {
- throw new IllegalArgumentException("truncated BIT STRING detected");
- }
-
- int padBits = stream.read();
- byte[] data = new byte[length - 1];
-
- if (data.length != 0)
- {
- if (Streams.readFully(stream, data) != data.length)
- {
- throw new EOFException("EOF encountered in middle of BIT STRING");
- }
- }
-
- return new DERBitString(data, padBits);
- }
}