Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/ber/stream/BERReal.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
44 /**
45 * This class is for the Real object.
46 * <P>See CCITT X.209.
47 *
48 * <pre>
49 * ENCODING RULE:
50 * tag = 0x09
51 * </pre>
52 *
53 * @version 1.0
54 */
55 public class BERReal extends BERElement {
56 /**
57 * Constants: special ASN.1 real values
58 */
59 public final static float PLUS_INFINITY = 1.0f/0.0f;
60 public final static float MINUS_INFINITY = -1.0f/0.0f;
61
62 /**
63 * Value of element
64 */
65 private float m_value = 0;
66
67 /**
68 * Constructs a real element with a value
69 * @param value float value
70 */
71 public BERReal(float value) {
72 m_value = value;
73 }
74
75 /**
76 * Constructs a real element from an input stream.
77 * @param stream source
78 * @param bytes_read array of 1 int; value incremented by
79 * number of bytes read from stream.
80 * @exception IOException failed to construct
81 */
82 public BERReal(InputStream stream, int[] bytes_read) throws IOException {
83 int length = super.readLengthOctets(stream, bytes_read);
84
85 if (length == 0)
86 /* zero length indicates value is zero */
87 m_value = 0;
88 else {
89 int octet = stream.read(); /* first content octet */
90 bytes_read[0]++;
91
92 if (octet == 0x40) /* bit 8 = 1 */
93 m_value = PLUS_INFINITY;
94 else if (octet == 0x41) /* bit 8 = 1 and bit 1 = 1 */
95 m_value = MINUS_INFINITY;
96 else if ((octet & 0x80) > 0) {
97 /* bit 8 = 1 */
98 /* Binary encoding */
99 /* M = S*N*2F where S = -1 or 1, 0<= F <= 3. */
100 int sign;
101 int base;
102 int number;
103 int f; /* binary scaling factor */
104 int exponent;
105 int mantissa;
106 int num_exponent_octets;
107 int contents_length_left;
108
109 if ((octet & 0x40) > 0) sign = -1;
110 else sign = 1;
111
112 if ((octet & 0x20) > 0) {
113 if ((octet & 0x10) > 0) {
114 /* bits 6+5 = 11 */
115 /* reserved for future use */
116 base = 0;
117 } else {
118 /* bits 6+5 = 10 */
119 base = 16;
120 }
121 } else if ((octet & 0x10) > 0) {
122 /* bits 6+5 = 01 */
123 base = 8;
124 } else {
125 /* bits 6+5 = 00 */
126 base = 2;
127 }
128
129 if ((octet & 0x08) > 0) {
130 if ((octet & 0x04) > 0) {
131 /* bits 4+3 = 11 */
132 f = 3;
133 } else {
134 /* bits 4+3 = 10 */
135 f = 2;
136 }
137 } else if ((octet & 0x04) > 0) {
138 /* bits 4+3 = 01 */
139 f = 1;
140 } else {
141 /* bits 4+3 = 00 */
142 f = 0;
143 }
144
145 if ((octet & 0x02) > 0) {
146 if ((octet & 0x01) > 0) {
147 /* bits 2+1 = 11 */
148 /* Following octet encodes the number of octets used to
149 * encode the exponent.
150 */
151 num_exponent_octets = stream.read();
152 bytes_read[0]++;
153 exponent = readTwosComplement(stream,bytes_read,num_exponent_octets);
154 } else {
155 /* bits 2+1 = 10 */
156 num_exponent_octets = 3;
157 exponent = readTwosComplement(stream,bytes_read,num_exponent_octets);
158 }
159 } else if ((octet & 0x01) > 0) {
160 /* bits 2+1 = 01 */
161 num_exponent_octets = 2;
162 exponent = readTwosComplement(stream,bytes_read,num_exponent_octets);
163 } else {
164 /* bits 2+1 = 00 */
165 num_exponent_octets = 1;
166 exponent = readTwosComplement(stream,bytes_read,num_exponent_octets);
167 }
168
169 contents_length_left = length - 1 - num_exponent_octets;
170 number = readUnsignedBinary(stream, bytes_read, contents_length_left);
171
172 mantissa = (int)(sign * number * Math.pow(2, f));
173 m_value = mantissa * (float)Math.pow(base,exponent);
174 } else {
175 /* bits 8 + 7 = 00 */
176 /* ISO 6093 field */
177 /*** NOTE: It has been agreed that this feature will
178 * not be provided right now.
179 */
180 throw new IOException("real ISO6093 not supported. ");
181 }
182 }
183 }
184
185 /**
186 * Sends the BER encoding directly to a stream.
187 * @param stream output stream
188 * @exception IOException failed to write
189 */
190 public void write(OutputStream stream) throws IOException {
191 if (m_value == 0) {
192 stream.write(BERElement.REAL);
193 stream.write(0x00); /* length */
194 } else if (m_value == PLUS_INFINITY) {
195 stream.write(BERElement.REAL);
196 stream.write(0x01); /* length */
197 stream.write(0x40); /* contents */
198 } else if (m_value == MINUS_INFINITY) {
199 stream.write(BERElement.REAL);
200 stream.write(0x01); /* length */
201 stream.write(0x41); /* contents */
202 } else {
203 /* Non-special real value */
204 /* NOTE: currently always sends as a base 2 binary encoding
205 * (see X.2 09 section 10.5.)
206 *
207 * M = S * N * 2F
208 *
209 * Simple encoding always uses F = 1.
210 */
211 // bcm - incomplete.
212 }
213 }
214
215 /**
216 * Gets the element type.
217 * @return element type
218 */
219 public int getType() {
220 return BERElement.REAL;
221 }
222
223 /**
224 * Gets the string representation.
225 * @return string representation of tag
226 */
227 public String toString() {
228 return "Real {" + m_value + "}";
229 }
230 }