1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.asn1.ber.tlv;
21
22
23 import org.apache.directory.api.i18n.I18n;
24 import org.apache.directory.api.util.Strings;
25
26
27
28
29
30
31
32 public final class IntegerDecoder
33 {
34
35 private static final int[] MASK = new int[]
36 { 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF };
37
38
39 private IntegerDecoder()
40 {
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54 public static int parse( BerValue value, int min, int max ) throws IntegerDecoderException
55 {
56 int result = parseInt( value );
57
58 if ( ( result >= min ) && ( result <= max ) )
59 {
60 return result;
61 }
62 else
63 {
64 throw new IntegerDecoderException( I18n.err( I18n.ERR_00038_VALUE_NOT_IN_RANGE, min, max ) );
65 }
66 }
67
68
69
70
71
72
73
74
75
76 public static int parse( BerValue value ) throws IntegerDecoderException
77 {
78 return parseInt( value );
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 private static int parseInt( BerValue value ) throws IntegerDecoderException
106 {
107 int result = 0;
108
109 byte[] bytes = value.getData();
110
111 if ( Strings.isEmpty( bytes ) )
112 {
113 throw new IntegerDecoderException( I18n.err( I18n.ERR_00036_0_BYTES_LONG_INTEGER ) );
114 }
115
116 boolean positive = true;
117
118 switch ( bytes.length )
119 {
120 case 5:
121 if ( bytes[0] == 0x00 )
122 {
123 if ( ( bytes[1] & ( byte ) 0x80 ) != ( byte ) 0x80 )
124 {
125 throw new IntegerDecoderException( I18n.err( I18n.ERR_00036_0_BYTES_LONG_INTEGER ) );
126 }
127
128 result = bytes[1] & 0x00FF;
129 result = ( result << 8 ) | ( bytes[2] & 0x00FF );
130 result = ( result << 8 ) | ( bytes[3] & 0x00FF );
131 result = ( result << 8 ) | ( bytes[4] & 0x00FF );
132 }
133 else
134 {
135 throw new IntegerDecoderException( I18n.err( I18n.ERR_00036_0_BYTES_LONG_INTEGER ) );
136 }
137
138 break;
139
140 case 4:
141 if ( bytes[0] == 0x00 )
142 {
143 result = bytes[1] & 0x00FF;
144 }
145 else
146 {
147 result = bytes[0] & 0x00FF;
148
149 if ( ( bytes[0] & ( byte ) 0x80 ) == ( byte ) 0x80 )
150 {
151 positive = false;
152 }
153
154 result = ( result << 8 ) | ( bytes[1] & 0x00FF );
155 }
156
157 result = ( result << 8 ) | ( bytes[2] & 0x00FF );
158 result = ( result << 8 ) | ( bytes[3] & 0x00FF );
159
160 break;
161
162 case 3:
163 if ( bytes[0] == 0x00 )
164 {
165 result = bytes[1] & 0x00FF;
166 }
167 else
168 {
169 result = bytes[0] & 0x00FF;
170
171 if ( ( bytes[0] & ( byte ) 0x80 ) == ( byte ) 0x80 )
172 {
173 positive = false;
174 }
175
176 result = ( result << 8 ) | ( bytes[1] & 0x00FF );
177 }
178
179 result = ( result << 8 ) | ( bytes[2] & 0x00FF );
180
181 break;
182
183 case 2:
184 if ( bytes[0] == 0x00 )
185 {
186 result = bytes[1] & 0x00FF;
187 }
188 else
189 {
190 result = bytes[0] & 0x00FF;
191
192 if ( ( bytes[0] & ( byte ) 0x80 ) == ( byte ) 0x80 )
193 {
194 positive = false;
195 }
196
197 result = ( result << 8 ) | ( bytes[1] & 0x00FF );
198 }
199
200 break;
201
202 case 1:
203 result = ( result << 8 ) | ( bytes[0] & 0x00FF );
204
205 if ( ( bytes[0] & ( byte ) 0x80 ) == ( byte ) 0x80 )
206 {
207 positive = false;
208 }
209
210 break;
211
212 default:
213 throw new IntegerDecoderException( I18n.err( I18n.ERR_00037_ABOVE_4_BYTES_INTEGER ) );
214 }
215
216 if ( !positive )
217 {
218 result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] );
219 }
220
221 return result;
222 }
223 }