| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
package org.ietf.ldap.ber.stream; |
| 39 |
|
| 40 |
import java.io.IOException; |
| 41 |
import java.io.InputStream; |
| 42 |
import java.io.OutputStream; |
| 43 |
import java.util.BitSet; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public class BERBitString extends BERElement { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private BitSet m_value; |
| 67 |
private int m_value_num_bits; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public BERBitString(BitSet value) { |
| 74 |
m_value = value; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public BERBitString(BERTagDecoder decoder, InputStream stream, |
| 86 |
int[] bytes_read) throws IOException { |
| 87 |
int octet; |
| 88 |
int contents_length = super.readLengthOctets(stream, bytes_read); |
| 89 |
int[] component_length = new int[1]; |
| 90 |
BERElement element = null; |
| 91 |
|
| 92 |
if (contents_length == -1) { |
| 93 |
|
| 94 |
{ |
| 95 |
component_length[0] = 0; |
| 96 |
element = getElement(decoder,stream,component_length); |
| 97 |
if (element != null) { |
| 98 |
|
| 99 |
BERBitString bit_string_element = (BERBitString)element; |
| 100 |
|
| 101 |
BitSet new_bit_set = new BitSet(m_value_num_bits + |
| 102 |
bit_string_element.getSize()); |
| 103 |
|
| 104 |
for (int i = 0; i<m_value_num_bits; i++) |
| 105 |
if (m_value.get(i)) |
| 106 |
new_bit_set.set(i); |
| 107 |
for (int j = 0; j<bit_string_element.getSize(); j++) |
| 108 |
if (bit_string_element.getValue().get(j)) |
| 109 |
new_bit_set.set(m_value_num_bits+j); |
| 110 |
m_value = new_bit_set; |
| 111 |
m_value_num_bits += bit_string_element.getSize(); |
| 112 |
} |
| 113 |
} while (element != null); |
| 114 |
} else { |
| 115 |
|
| 116 |
bytes_read[0] += contents_length; |
| 117 |
while (contents_length > 0) { |
| 118 |
component_length[0] = 0; |
| 119 |
element = getElement(decoder,stream,component_length); |
| 120 |
if (element != null) { |
| 121 |
|
| 122 |
BERBitString bit_string_element = (BERBitString)element; |
| 123 |
|
| 124 |
BitSet new_bit_set = new BitSet(m_value_num_bits + |
| 125 |
bit_string_element.getSize()); |
| 126 |
for (int i = 0; i<m_value_num_bits; i++) |
| 127 |
if (m_value.get(i)) |
| 128 |
new_bit_set.set(i); |
| 129 |
for (int j = 0; j<bit_string_element.getSize(); j++) |
| 130 |
if (bit_string_element.getValue().get(j)) |
| 131 |
new_bit_set.set(m_value_num_bits+j); |
| 132 |
m_value = new_bit_set; |
| 133 |
m_value_num_bits += bit_string_element.getSize(); |
| 134 |
} |
| 135 |
contents_length -= component_length[0]; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public BERBitString(InputStream stream, int[] bytes_read) |
| 149 |
throws IOException { |
| 150 |
|
| 151 |
|
| 152 |
int octet; |
| 153 |
int contents_length = super.readLengthOctets(stream, bytes_read); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
int last_unused_bits = stream.read(); |
| 161 |
bytes_read[0]++; |
| 162 |
contents_length--; |
| 163 |
|
| 164 |
m_value_num_bits = ((contents_length-1)*8) + (8-last_unused_bits); |
| 165 |
m_value = new BitSet(); |
| 166 |
|
| 167 |
int bit_num = 0; |
| 168 |
for (int i = 0; i < contents_length-1; i++) { |
| 169 |
octet = stream.read(); |
| 170 |
int mask = 0x80; |
| 171 |
for (int j = 0; j < 8; j++) { |
| 172 |
if ((octet & mask) > 0) { |
| 173 |
m_value.set(bit_num); |
| 174 |
} |
| 175 |
else |
| 176 |
m_value.clear(bit_num); |
| 177 |
bit_num++; |
| 178 |
mask = mask / 2; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
octet = stream.read(); |
| 183 |
int mask = 0x80; |
| 184 |
for (int j = 0; j < 8-last_unused_bits; j++) { |
| 185 |
if ((octet & mask) > 0) |
| 186 |
m_value.set(bit_num); |
| 187 |
else |
| 188 |
m_value.clear(bit_num); |
| 189 |
bit_num++; |
| 190 |
mask = mask / 2; |
| 191 |
} |
| 192 |
|
| 193 |
bytes_read[0] += contents_length; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public void write(OutputStream stream) throws IOException { |
| 202 |
stream.write(BERElement.BITSTRING); |
| 203 |
|
| 204 |
|
| 205 |
int num_bits = m_value_num_bits; |
| 206 |
|
| 207 |
|
| 208 |
int last_unused_bits = 8 - (num_bits % 8); |
| 209 |
|
| 210 |
|
| 211 |
int num_content_octets = num_bits/8 + 1; |
| 212 |
if (last_unused_bits > 0) |
| 213 |
num_content_octets += 1; |
| 214 |
stream.write(num_content_octets); |
| 215 |
|
| 216 |
stream.write(last_unused_bits); |
| 217 |
|
| 218 |
for (int i = 0; i < num_bits/8; i++) { |
| 219 |
int new_octet = 0; |
| 220 |
int bit = 0x80; |
| 221 |
for (int j = 0; j < 8; j++) { |
| 222 |
if (m_value.get(i*8+j)) |
| 223 |
new_octet += bit; |
| 224 |
bit = bit/2; |
| 225 |
} |
| 226 |
stream.write(new_octet); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if (last_unused_bits > 0) { |
| 234 |
int new_octet = 0; |
| 235 |
int bit = 0x80; |
| 236 |
for (int j = 0; j < last_unused_bits; j++) { |
| 237 |
if (m_value.get((num_bits/8)*8+j)) |
| 238 |
new_octet += bit; |
| 239 |
bit = bit/2; |
| 240 |
} |
| 241 |
stream.write(new_octet); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public BitSet getValue() { |
| 250 |
return m_value; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public int getSize() { |
| 258 |
return m_value_num_bits; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public int getType() { |
| 266 |
return BERElement.BITSTRING; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public String toString() { |
| 274 |
String hex_string = ""; |
| 275 |
int octet; |
| 276 |
|
| 277 |
|
| 278 |
int num_bits = m_value_num_bits; |
| 279 |
for (int i = 0; i < num_bits/8; i++) { |
| 280 |
octet = 0; |
| 281 |
int bit = 0x80; |
| 282 |
for (int j = 0; j < 8; j++) { |
| 283 |
if (m_value.get(i*8+j)) |
| 284 |
octet += bit; |
| 285 |
bit = bit/2; |
| 286 |
} |
| 287 |
hex_string += " " + (byte)octet; |
| 288 |
} |
| 289 |
|
| 290 |
int bit = 0x80; |
| 291 |
octet = 0; |
| 292 |
for (int k = 0; k < num_bits-num_bits/8; k++) { |
| 293 |
if (m_value.get((num_bits/8)*8+k)) |
| 294 |
octet += bit; |
| 295 |
bit = bit/2; |
| 296 |
} |
| 297 |
hex_string += " " + (byte)octet; |
| 298 |
|
| 299 |
return "Bitstring {" + hex_string + " }"; |
| 300 |
} |
| 301 |
} |