1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 23 public class DERBitString 24 extends DERObject 25 implements DERString 26 { 27 private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 28 29 protected byte[] data; 30 protected int padBits; 31 32 36 static protected int getPadBits( 37 int bitString) 38 { 39 int val = 0; 40 for (int i = 3; i >= 0; i--) 41 { 42 if (i != 0) 47 { 48 if ((bitString >> (i * 8)) != 0) 49 { 50 val = (bitString >> (i * 8)) & 0xFF; 51 break; 52 } 53 } 54 else 55 { 56 if (bitString != 0) 57 { 58 val = bitString & 0xFF; 59 break; 60 } 61 } 62 } 63 64 if (val == 0) 65 { 66 return 7; 67 } 68 69 70 int bits = 1; 71 72 while (((val <<= 1) & 0xFF) != 0) 73 { 74 bits++; 75 } 76 77 return 8 - bits; 78 } 79 80 84 static protected byte[] getBytes(int bitString) 85 { 86 int bytes = 4; 87 for (int i = 3; i >= 1; i--) 88 { 89 if ((bitString & (0xFF << (i * 8))) != 0) 90 { 91 break; 92 } 93 bytes--; 94 } 95 96 byte[] result = new byte[bytes]; 97 for (int i = 0; i < bytes; i++) 98 { 99 result[i] = (byte) ((bitString >> (i * 8)) & 0xFF); 100 } 101 102 return result; 103 } 104 105 110 public static DERBitString getInstance( 111 Object obj) 112 { 113 if (obj == null || obj instanceof DERBitString) 114 { 115 return (DERBitString)obj; 116 } 117 118 if (obj instanceof ASN1OctetString) 119 { 120 byte[] bytes = ((ASN1OctetString)obj).getOctets(); 121 int padBits = bytes[0]; 122 byte[] data = new byte[bytes.length - 1]; 123 124 System.arraycopy(bytes, 1, data, 0, bytes.length - 1); 125 126 return new DERBitString(data, padBits); 127 } 128 129 if (obj instanceof ASN1TaggedObject) 130 { 131 return getInstance(((ASN1TaggedObject)obj).getObject()); 132 } 133 134 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 135 } 136 137 146 public static DERBitString getInstance( 147 ASN1TaggedObject obj, 148 boolean explicit) 149 { 150 return getInstance(obj.getObject()); 151 } 152 153 protected DERBitString( 154 byte data, 155 int padBits) 156 { 157 this.data = new byte[1]; 158 this.data[0] = data; 159 this.padBits = padBits; 160 } 161 162 166 public DERBitString( 167 byte[] data, 168 int padBits) 169 { 170 this.data = data; 171 this.padBits = padBits; 172 } 173 174 public DERBitString( 175 byte[] data) 176 { 177 this(data, 0); 178 } 179 180 public DERBitString( 181 DEREncodable obj) 182 { 183 try 184 { 185 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 186 DEROutputStream dOut = new DEROutputStream(bOut); 187 188 dOut.writeObject(obj); 189 dOut.close(); 190 191 this.data = bOut.toByteArray(); 192 this.padBits = 0; 193 } 194 catch (IOException e) 195 { 196 throw new IllegalArgumentException ("Error processing object : " + e.toString()); 197 } 198 } 199 200 public byte[] getBytes() 201 { 202 return data; 203 } 204 205 public int getPadBits() 206 { 207 return padBits; 208 } 209 210 211 214 public int intValue() 215 { 216 int value = 0; 217 218 for (int i = 0; i != data.length && i != 4; i++) 219 { 220 value |= (data[i] & 0xff) << (8 * i); 221 } 222 223 return value; 224 } 225 226 void encode( 227 DEROutputStream out) 228 throws IOException 229 { 230 byte[] bytes = new byte[getBytes().length + 1]; 231 232 bytes[0] = (byte)getPadBits(); 233 System.arraycopy(getBytes(), 0, bytes, 1, bytes.length - 1); 234 235 out.writeEncoded(BIT_STRING, bytes); 236 } 237 238 public int hashCode() 239 { 240 int value = 0; 241 242 for (int i = 0; i != data.length; i++) 243 { 244 value ^= (data[i] & 0xff) << (i % 4); 245 } 246 247 return value; 248 } 249 250 public boolean equals( 251 Object o) 252 { 253 if (o == null || !(o instanceof DERBitString)) 254 { 255 return false; 256 } 257 258 DERBitString other = (DERBitString)o; 259 260 if (data.length != other.data.length) 261 { 262 return false; 263 } 264 265 for (int i = 0; i != data.length; i++) 266 { 267 if (data[i] != other.data[i]) 268 { 269 return false; 270 } 271 } 272 273 return (padBits == other.padBits); 274 } 275 276 public String getString() 277 { 278 StringBuffer buf = new StringBuffer ("#"); 279 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 280 ASN1OutputStream aOut = new ASN1OutputStream(bOut); 281 282 try 283 { 284 aOut.writeObject(this); 285 } 286 catch (IOException e) 287 { 288 throw new RuntimeException ("internal error encoding BitString"); 289 } 290 291 byte[] string = bOut.toByteArray(); 292 293 for (int i = 0; i != string.length; i++) 294 { 295 buf.append(table[(string[i] >>> 4) % 0xf]); 296 buf.append(table[string[i] & 0xf]); 297 } 298 299 return buf.toString(); 300 } 301 } 302 | Popular Tags |