Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/ber/stream/BERBitString.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.util.BitSet;
44
45 /**
46 * This class is for the BitString object. Note that the BitSet class
47 * has a bug: size() returns the size of the internal allocated memory
48 * rather than the number of bits. Current work-around is to maintain
49 * the number of bits ourselves in m_value_num_bits.
50 * Change is required when BitSet is fixed.
51 * <P>See CCITT X.209.
52 *
53 *
54 * <pre>
55 * ENCODING RULE:
56 * Primitive Definite length.
57 * tag = 0x03
58 * </pre>
59 *
60 * @version 1.0
61 */
62 public class BERBitString extends BERElement {
63 /**
64 * Internal variables
65 */
66 private BitSet m_value;
67 private int m_value_num_bits;
68
69 /**
70 * Constructs a boolean element.
71 * @param value boolean value
72 */
73 public BERBitString(BitSet value) {
74 m_value = value;
75 }
76
77 /**
78 * Constructs a bitstring element from an input stream
79 * (for constructed encodings).
80 * @param stream source
81 * @param bytes_read array of 1 int; value incremented by
82 * number of bytes read from stream
83 * @exception IOException failed to construct
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 /* Constructed - indefinite length. */
94 {
95 component_length[0] = 0;
96 element = getElement(decoder,stream,component_length);
97 if (element != null) {
98 /* element is a bitstring - add it to the existing BitSet */
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 /* Constructed - definite length */
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 /* element is a bitstring - add it to the existing BitSet */
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 * Constructs a bitstring element from an input stream
142 * (for primitive encodings).
143 * @param stream source
144 * @param bytes_read array of 1 int; value incremented by
145 * number of bytes read from stream
146 * @exception IOException failed to construct
147 */
148 public BERBitString(InputStream stream, int[] bytes_read)
149 throws IOException {
150 /* Primitive - definite length content octets string. */
151
152 int octet;
153 int contents_length = super.readLengthOctets(stream, bytes_read);
154
155
156 /* First content octect doesn't encode any of
157 * the string - it encodes the number of unused
158 * bits in the final content octet.
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(); /* last content octet */
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 * Sends the BER encoding directly to a stream.
198 * Always sends in primitive form.
199 * @param stream output stream
200 */
201 public void write(OutputStream stream) throws IOException {
202 stream.write(BERElement.BITSTRING);
203
204 //int num_bits = m_value.size(); /* number of bits to send */
205 int num_bits = m_value_num_bits;
206
207 /* Number of bits unused int the last contents octet */
208 int last_unused_bits = 8 - (num_bits % 8);
209
210 /* Figure out the number of content octets */
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); /* length octet */
215
216 stream.write(last_unused_bits); /* first content octet */
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 * Last octet may not use all bits. If last octet DOES use all
231 * bits then it has already been written above.
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 * Gets the bitstring value.
247 * @param element type
248 */
249 public BitSet getValue() {
250 return m_value;
251 }
252
253 /**
254 * Gets the number of bits.
255 * @return bit numbers.
256 */
257 public int getSize() {
258 return m_value_num_bits;
259 }
260
261 /**
262 * Gets the element type.
263 * @param element type
264 */
265 public int getType() {
266 return BERElement.BITSTRING;
267 }
268
269 /**
270 * Gets the string representation.
271 * @return string representation of tag.
272 */
273 public String toString() {
274 String hex_string = "";
275 int octet;
276
277 //int num_bits = m_value.size();
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 }