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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.ietf.ldap.ber.stream;
import java.util.*;
import java.io.*;
/**
* This class is for the tagged object type. A nested tag is
* allowed. A tagged element contains another BER element.
* <P>See CCITT X.209.
*
* @version 1.0
*/
public abstract class BERElement implements Serializable {
/**
* Possible element types.
*/
public final static int BOOLEAN = 0x01;
public final static int INTEGER = 0x02;
public final static int BITSTRING = 0x03;
public final static int OCTETSTRING = 0x04;
public final static int NULL = 0x05;
public final static int OBJECTID = 0x06;
public final static int REAL = 0x09;
public final static int ENUMERATED = 0x0a;
public final static int SET = 0x31; /* always constructed */
public final static int SEQUENCE = 0x30; /* always constructed */
public final static int NUMERICSTRING = 0x12;
public final static int PRINTABLESTRING = 0x13;
public final static int TELETEXSTRING = 0x14;
public final static int VIDEOTEXSTRING = 0x15;
public final static int IA5STRING = 0x16;
public final static int UTCTIME = 0x17;
public final static int GRAPHICSTRING = 0x19;
public final static int VISIBLESTRING = 0x1A;
public final static int GENERALSTRING = 0x1B;
/**
* Internal (non-transmitted) tags.
*/
public final static int TAG = -1;
public final static int CHOICE = -2;
public final static int ANY = -3;
/**
* Possible tags.
*/
public final static int EOC = 0x00; /* End Of Construction */
public final static int UNIVERSAL = 0x00;
public final static int APPLICATION = 0x40;
public final static int CONTEXT = 0x80;
public final static int SASLCONTEXT = 0xa0;
public final static int PRIVATE = 0xC0;
public final static int PRIMITIVE = 0x00;
public final static int CONSTRUCTED = 0x20;
public final static int MRA_OID = 0x01;
public final static int MRA_TYPE = 0x02;
public final static int MRA_VALUE = 0x03;
public final static int MRA_DNATTRS = 0x04;
public final static int EXOP_REQ_OID = 0x00;
public final static int EXOP_REQ_VALUE = 0x01;
public final static int EXOP_RES_OID = 0x0a;
public final static int EXOP_RES_VALUE = 0x0b;
public final static int SK_MATCHRULE = 0x00;
public final static int SK_REVERSE = 0x01;
public final static int SR_ATTRTYPE = 0x00;
/**
* Gets a ber element from the input stream.
* @param decoder decoder for application specific BER
* @param stream source of ber encoding
* @param bytes_read array of 1 int; value incremented by
* number of bytes read from stream
* @exception IOException failed to decode an element.
*/
public static BERElement getElement(BERTagDecoder decoder,
InputStream stream, int[] bytes_read) throws IOException {
BERElement element = null;
int tag = stream.read();
bytes_read[0] = 1;
if (tag == EOC) {
stream.read(); /* length octet (always zero) */
bytes_read[0] = 1;
element = null;
} else if (tag == BOOLEAN) {
element = new BERBoolean(stream, bytes_read);
} else if (tag == INTEGER) {
element = new BERInteger(stream, bytes_read);
} else if (tag == BITSTRING) {
element = new BERBitString(stream, bytes_read);
} else if (tag == (BITSTRING | CONSTRUCTED)) {
element = new BERBitString(decoder, stream, bytes_read);
} else if (tag == OCTETSTRING) {
element = new BEROctetString(stream, bytes_read);
} else if (tag == (OCTETSTRING | CONSTRUCTED)) {
element = new BEROctetString(decoder, stream, bytes_read);
} else if (tag == NULL) {
element = new BERNull(stream, bytes_read);
} else if (tag == OBJECTID) {
element = new BERObjectId(stream, bytes_read);
} else if (tag == REAL) {
element = new BERReal(stream, bytes_read);
} else if (tag == ENUMERATED) {
element = new BEREnumerated(stream, bytes_read);
} else if (tag == SEQUENCE) {
element = new BERSequence(decoder, stream, bytes_read);
} else if (tag == SET) {
element = new BERSet(decoder, stream, bytes_read);
} else if (tag == NUMERICSTRING) {
element = new BERNumericString(stream, bytes_read);
} else if (tag == (NUMERICSTRING | CONSTRUCTED)) {
element = new BERNumericString(decoder, stream, bytes_read);
} else if (tag == PRINTABLESTRING) {
element = new BERPrintableString(stream, bytes_read);
} else if (tag == (PRINTABLESTRING | CONSTRUCTED)) {
element = new BERPrintableString(decoder, stream, bytes_read);
} else if (tag == UTCTIME) {
element = new BERUTCTime(stream, bytes_read);
} else if (tag == (UTCTIME | CONSTRUCTED)) {
element = new BERUTCTime(decoder, stream, bytes_read);
} else if (tag == VISIBLESTRING) {
element = new BERVisibleString(stream, bytes_read);
} else if (tag == (VISIBLESTRING | CONSTRUCTED)) {
element = new BERVisibleString(decoder, stream, bytes_read);
} else if ((tag & (APPLICATION | PRIVATE | CONTEXT)) > 0) {
element = new BERTag(decoder, tag, stream, bytes_read);
} else
throw new IOException("invalid tag " + tag);
return element;
}
/**
* Reads and decodes a length byte and then that many octets
* from the input stream.
* @param stream input stream from which to read
* @param bytes_read array of 1 int; value incremented by
* number of bytes read from stream
* @return length of contents or -1 if indefinite length.
* @exception IOException failed to read octets
*/
public static int readLengthOctets(InputStream stream, int[] bytes_read)
throws IOException {
int contents_length = 0;
int octet = stream.read();
bytes_read[0]++;
if (octet == 0x80)
/* Indefinite length */
contents_length = -1;
else if ((octet & 0x80) > 0) {
/* Definite (long form) - num octets encoded in 7 rightmost bits */
int num_length_octets = (octet & 0x7F);
for (int i = 0; i < num_length_octets; i++) {
octet = stream.read();
bytes_read[0]++;
contents_length = (contents_length<<8) + octet;
}
} else {
/* Definite (short form) - one length octet. Value encoded in */
/* 7 rightmost bits. */
contents_length = octet;
}
return contents_length;
}
/**
* Writes length octets (definite length only) to stream.
* Uses shortform whenever possible.
* @param stream output stream to write to
* @param num_content_octets value to be encode into length octets
* @return number of bytes sent to stream.
* @exception IOException failed to read octets
*/
public static void sendDefiniteLength(OutputStream stream,
int num_content_octets) throws IOException {
int bytes_written = 0;
if (num_content_octets <= 127) {
/* Use short form */
stream.write(num_content_octets);
} else {
/* Using long form:
* Need to determine how many octets are required to
* encode the length.
*/
int num_length_octets = 0;
int num = num_content_octets;
while (num >= 0) {
num_length_octets++;
num = (num>>8);
if (num <= 0)
break;
}
byte[] buffer = new byte[num_length_octets+1];
buffer[0] = (byte)(0x80 | num_length_octets);
num = num_content_octets;
for (int i = num_length_octets; i > 0; i--) {
buffer[i] = (byte)(num & 0xFF);
num = (num>>8);
}
stream.write(buffer);
}
}
/**
* Reads a number of bytes from an input stream and form
* an integer..
* @param stream source of data
* @param bytes_read number of bytes read
* @param length number of bytes to be read (1 to 4)
* @return the value of the data as two's complement.
* @exception IOException failed to read octets
*/
protected int readUnsignedBinary(InputStream stream,
int[] bytes_read, int length) throws IOException {
int value = 0;
int octet;
for (int i = 0; i < length; i++) {
octet = stream.read();
bytes_read[0]++;
value = (value<<8) + octet;
}
return value;
}
/**
* Reads the two's complement representation of an integer from
* an input stream.
* @param stream source of data
* @param bytes_read number of bytes read
* @param length number of bytes to be read
* @return the integer value as two's complement.
* @exception IOException failed to read octets
*/
protected int readTwosComplement(InputStream stream,
int[] bytes_read, int length) throws IOException {
int value = 0;
if (length > 0) {
boolean negative = false;
int octet = stream.read();
bytes_read[0]++;
if ((octet & 0x80) > 0) /* left-most bit is 1. */
negative = true;
for (int i = 0; i < length; i++) {
if (i > 0) {
octet = stream.read();
bytes_read[0]++;
}
if (negative)
value = (value<<8) + (int)(octet^0xFF)&0xFF;
else
value = (value<<8) + (int)(octet&0xFF);
}
if (negative) /* convert to 2's complement */
value = (value + 1) * -1;
}
return value;
}
/**
* Converts byte to hex string.
* @param value byte value
* @return string representation of Hex String
*/
public String byteToHexString(byte value) {
if (value < 0)
return Integer.toHexString((value & 0x7F) + 128);
else
return Integer.toHexString(value);
}
/**
* Sends the BER encoding directly to a stream.
* @param stream output stream
* @return bytes written to stream.
*/
public abstract void write(OutputStream stream) throws IOException;
/**
* Gets the element type.
* @return element type.
*/
public abstract int getType();
/**
* Gets the string representation.
* @return string representation of an element.
*/
public abstract String toString();
}
|