| 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.io.Serializable; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public abstract class BERElement implements Serializable { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public final static int BOOLEAN = 0x01; |
| 57 |
public final static int INTEGER = 0x02; |
| 58 |
public final static int BITSTRING = 0x03; |
| 59 |
public final static int OCTETSTRING = 0x04; |
| 60 |
public final static int NULL = 0x05; |
| 61 |
public final static int OBJECTID = 0x06; |
| 62 |
public final static int REAL = 0x09; |
| 63 |
public final static int ENUMERATED = 0x0a; |
| 64 |
public final static int SET = 0x31; |
| 65 |
public final static int SEQUENCE = 0x30; |
| 66 |
public final static int NUMERICSTRING = 0x12; |
| 67 |
public final static int PRINTABLESTRING = 0x13; |
| 68 |
public final static int TELETEXSTRING = 0x14; |
| 69 |
public final static int VIDEOTEXSTRING = 0x15; |
| 70 |
public final static int IA5STRING = 0x16; |
| 71 |
public final static int UTCTIME = 0x17; |
| 72 |
public final static int GRAPHICSTRING = 0x19; |
| 73 |
public final static int VISIBLESTRING = 0x1A; |
| 74 |
public final static int GENERALSTRING = 0x1B; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public final static int TAG = -1; |
| 80 |
public final static int CHOICE = -2; |
| 81 |
public final static int ANY = -3; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public final static int EOC = 0x00; |
| 87 |
public final static int UNIVERSAL = 0x00; |
| 88 |
public final static int APPLICATION = 0x40; |
| 89 |
public final static int CONTEXT = 0x80; |
| 90 |
public final static int SASLCONTEXT = 0xa0; |
| 91 |
public final static int PRIVATE = 0xC0; |
| 92 |
public final static int PRIMITIVE = 0x00; |
| 93 |
public final static int CONSTRUCTED = 0x20; |
| 94 |
|
| 95 |
public final static int MRA_OID = 0x01; |
| 96 |
public final static int MRA_TYPE = 0x02; |
| 97 |
public final static int MRA_VALUE = 0x03; |
| 98 |
public final static int MRA_DNATTRS = 0x04; |
| 99 |
public final static int EXOP_REQ_OID = 0x00; |
| 100 |
public final static int EXOP_REQ_VALUE = 0x01; |
| 101 |
public final static int EXOP_RES_OID = 0x0a; |
| 102 |
public final static int EXOP_RES_VALUE = 0x0b; |
| 103 |
public final static int SK_MATCHRULE = 0x00; |
| 104 |
public final static int SK_REVERSE = 0x01; |
| 105 |
public final static int SR_ATTRTYPE = 0x00; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static BERElement getElement(BERTagDecoder decoder, |
| 116 |
InputStream stream, int[] bytes_read) throws IOException { |
| 117 |
|
| 118 |
BERElement element = null; |
| 119 |
int tag = stream.read(); |
| 120 |
bytes_read[0] = 1; |
| 121 |
|
| 122 |
if (tag == EOC) { |
| 123 |
stream.read(); |
| 124 |
bytes_read[0] = 1; |
| 125 |
element = null; |
| 126 |
} else if (tag == BOOLEAN) { |
| 127 |
element = new BERBoolean(stream, bytes_read); |
| 128 |
} else if (tag == INTEGER) { |
| 129 |
element = new BERInteger(stream, bytes_read); |
| 130 |
} else if (tag == BITSTRING) { |
| 131 |
element = new BERBitString(stream, bytes_read); |
| 132 |
} else if (tag == (BITSTRING | CONSTRUCTED)) { |
| 133 |
element = new BERBitString(decoder, stream, bytes_read); |
| 134 |
} else if (tag == OCTETSTRING) { |
| 135 |
element = new BEROctetString(stream, bytes_read); |
| 136 |
} else if (tag == (OCTETSTRING | CONSTRUCTED)) { |
| 137 |
element = new BEROctetString(decoder, stream, bytes_read); |
| 138 |
} else if (tag == NULL) { |
| 139 |
element = new BERNull(stream, bytes_read); |
| 140 |
} else if (tag == OBJECTID) { |
| 141 |
element = new BERObjectId(stream, bytes_read); |
| 142 |
} else if (tag == REAL) { |
| 143 |
element = new BERReal(stream, bytes_read); |
| 144 |
} else if (tag == ENUMERATED) { |
| 145 |
element = new BEREnumerated(stream, bytes_read); |
| 146 |
} else if (tag == SEQUENCE) { |
| 147 |
element = new BERSequence(decoder, stream, bytes_read); |
| 148 |
} else if (tag == SET) { |
| 149 |
element = new BERSet(decoder, stream, bytes_read); |
| 150 |
} else if (tag == NUMERICSTRING) { |
| 151 |
element = new BERNumericString(stream, bytes_read); |
| 152 |
} else if (tag == (NUMERICSTRING | CONSTRUCTED)) { |
| 153 |
element = new BERNumericString(decoder, stream, bytes_read); |
| 154 |
} else if (tag == PRINTABLESTRING) { |
| 155 |
element = new BERPrintableString(stream, bytes_read); |
| 156 |
} else if (tag == (PRINTABLESTRING | CONSTRUCTED)) { |
| 157 |
element = new BERPrintableString(decoder, stream, bytes_read); |
| 158 |
} else if (tag == UTCTIME) { |
| 159 |
element = new BERUTCTime(stream, bytes_read); |
| 160 |
} else if (tag == (UTCTIME | CONSTRUCTED)) { |
| 161 |
element = new BERUTCTime(decoder, stream, bytes_read); |
| 162 |
} else if (tag == VISIBLESTRING) { |
| 163 |
element = new BERVisibleString(stream, bytes_read); |
| 164 |
} else if (tag == (VISIBLESTRING | CONSTRUCTED)) { |
| 165 |
element = new BERVisibleString(decoder, stream, bytes_read); |
| 166 |
} else if ((tag & (APPLICATION | PRIVATE | CONTEXT)) > 0) { |
| 167 |
element = new BERTag(decoder, tag, stream, bytes_read); |
| 168 |
} else |
| 169 |
throw new IOException("invalid tag " + tag); |
| 170 |
|
| 171 |
return element; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static int readLengthOctets(InputStream stream, int[] bytes_read) |
| 184 |
throws IOException { |
| 185 |
int contents_length = 0; |
| 186 |
int octet = stream.read(); |
| 187 |
bytes_read[0]++; |
| 188 |
|
| 189 |
if (octet == 0x80) |
| 190 |
|
| 191 |
contents_length = -1; |
| 192 |
else if ((octet & 0x80) > 0) { |
| 193 |
|
| 194 |
int num_length_octets = (octet & 0x7F); |
| 195 |
|
| 196 |
for (int i = 0; i < num_length_octets; i++) { |
| 197 |
octet = stream.read(); |
| 198 |
bytes_read[0]++; |
| 199 |
contents_length = (contents_length<<8) + octet; |
| 200 |
} |
| 201 |
} else { |
| 202 |
|
| 203 |
|
| 204 |
contents_length = octet; |
| 205 |
} |
| 206 |
return contents_length; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static void sendDefiniteLength(OutputStream stream, |
| 218 |
int num_content_octets) throws IOException { |
| 219 |
|
| 220 |
int bytes_written = 0; |
| 221 |
|
| 222 |
if (num_content_octets <= 127) { |
| 223 |
|
| 224 |
stream.write(num_content_octets); |
| 225 |
} else { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int num_length_octets = 0; |
| 231 |
int num = num_content_octets; |
| 232 |
while (num >= 0) { |
| 233 |
num_length_octets++; |
| 234 |
num = (num>>8); |
| 235 |
if (num <= 0) |
| 236 |
break; |
| 237 |
} |
| 238 |
|
| 239 |
byte[] buffer = new byte[num_length_octets+1]; |
| 240 |
buffer[0] = (byte)(0x80 | num_length_octets); |
| 241 |
|
| 242 |
num = num_content_octets; |
| 243 |
for (int i = num_length_octets; i > 0; i--) { |
| 244 |
buffer[i] = (byte)(num & 0xFF); |
| 245 |
num = (num>>8); |
| 246 |
} |
| 247 |
|
| 248 |
stream.write(buffer); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
protected int readUnsignedBinary(InputStream stream, |
| 262 |
int[] bytes_read, int length) throws IOException { |
| 263 |
int value = 0; |
| 264 |
int octet; |
| 265 |
|
| 266 |
for (int i = 0; i < length; i++) { |
| 267 |
octet = stream.read(); |
| 268 |
bytes_read[0]++; |
| 269 |
value = (value<<8) + octet; |
| 270 |
} |
| 271 |
|
| 272 |
return value; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
protected int readTwosComplement(InputStream stream, |
| 285 |
int[] bytes_read, int length) throws IOException { |
| 286 |
int value = 0; |
| 287 |
if (length > 0) { |
| 288 |
boolean negative = false; |
| 289 |
int octet = stream.read(); |
| 290 |
bytes_read[0]++; |
| 291 |
if ((octet & 0x80) > 0) |
| 292 |
negative = true; |
| 293 |
|
| 294 |
for (int i = 0; i < length; i++) { |
| 295 |
if (i > 0) { |
| 296 |
octet = stream.read(); |
| 297 |
bytes_read[0]++; |
| 298 |
} |
| 299 |
if (negative) |
| 300 |
value = (value<<8) + (octet^0xFF)&0xFF; |
| 301 |
else |
| 302 |
value = (value<<8) + (octet&0xFF); |
| 303 |
} |
| 304 |
if (negative) |
| 305 |
value = (value + 1) * -1; |
| 306 |
} |
| 307 |
return value; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public String byteToHexString(byte value) { |
| 316 |
if (value < 0) |
| 317 |
return Integer.toHexString((value & 0x7F) + 128); |
| 318 |
else |
| 319 |
return Integer.toHexString(value); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public abstract void write(OutputStream stream) throws IOException; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public abstract int getType(); |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public abstract String toString(); |
| 340 |
} |