Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/ber/stream/BERElement.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
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 * This class is for the tagged object type. A nested tag is
47 * allowed. A tagged element contains another BER element.
48 * <P>See CCITT X.209.
49 *
50 * @version 1.0
51 */
52 public abstract class BERElement implements Serializable {
53 /**
54 * Possible element types.
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; /* always constructed */
65 public final static int SEQUENCE = 0x30; /* always constructed */
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 * Internal (non-transmitted) tags.
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 * Possible tags.
85 */
86 public final static int EOC = 0x00; /* End Of Construction */
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 * Gets a ber element from the input stream.
109 * @param decoder decoder for application specific BER
110 * @param stream source of ber encoding
111 * @param bytes_read array of 1 int; value incremented by
112 * number of bytes read from stream
113 * @exception IOException failed to decode an element.
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(); /* length octet (always zero) */
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 * Reads and decodes a length byte and then that many octets
176 * from the input stream.
177 * @param stream input stream from which to read
178 * @param bytes_read array of 1 int; value incremented by
179 * number of bytes read from stream
180 * @return length of contents or -1 if indefinite length.
181 * @exception IOException failed to read octets
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 /* Indefinite length */
191 contents_length = -1;
192 else if ((octet & 0x80) > 0) {
193 /* Definite (long form) - num octets encoded in 7 rightmost bits */
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 /* Definite (short form) - one length octet. Value encoded in */
203 /* 7 rightmost bits. */
204 contents_length = octet;
205 }
206 return contents_length;
207 }
208
209 /**
210 * Writes length octets (definite length only) to stream.
211 * Uses shortform whenever possible.
212 * @param stream output stream to write to
213 * @param num_content_octets value to be encode into length octets
214 * @return number of bytes sent to stream.
215 * @exception IOException failed to read octets
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 /* Use short form */
224 stream.write(num_content_octets);
225 } else {
226 /* Using long form:
227 * Need to determine how many octets are required to
228 * encode the length.
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 * Reads a number of bytes from an input stream and form
254 * an integer..
255 * @param stream source of data
256 * @param bytes_read number of bytes read
257 * @param length number of bytes to be read (1 to 4)
258 * @return the value of the data as two's complement.
259 * @exception IOException failed to read octets
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 * Reads the two's complement representation of an integer from
277 * an input stream.
278 * @param stream source of data
279 * @param bytes_read number of bytes read
280 * @param length number of bytes to be read
281 * @return the integer value as two's complement.
282 * @exception IOException failed to read octets
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) /* left-most bit is 1. */
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) /* convert to 2's complement */
305 value = (value + 1) * -1;
306 }
307 return value;
308 }
309
310 /**
311 * Converts byte to hex string.
312 * @param value byte value
313 * @return string representation of Hex String
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 * Sends the BER encoding directly to a stream.
324 * @param stream output stream
325 * @return bytes written to stream.
326 */
327 public abstract void write(OutputStream stream) throws IOException;
328
329 /**
330 * Gets the element type.
331 * @return element type.
332 */
333 public abstract int getType();
334
335 /**
336 * Gets the string representation.
337 * @return string representation of an element.
338 */
339 public abstract String toString();
340 }