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
25
26
27
28
29
30
31 public final class LongDecoder
32 {
33
34 private static final long[] MASK = new long[]
35 { 0x00000000000000FFL, 0x000000000000FFFFL, 0x0000000000FFFFFFL, 0x00000000FFFFFFFFL, 0x000000FFFFFFFFFFL,
36 0x0000FFFFFFFFFFFFL, 0x00FFFFFFFFFFFFFFL, 0xFFFFFFFFFFFFFFFFL };
37
38
39
40
41
42
43
44
45
46
47
48
49 public static long parse( BerValue value, long min, long max ) throws LongDecoderException
50 {
51 long result = parseLong( value );
52
53 if ( ( result >= min ) && ( result <= max ) )
54 {
55 return result;
56 }
57 else
58 {
59 throw new LongDecoderException( I18n.err( I18n.ERR_00038_VALUE_NOT_IN_RANGE, min, max ) );
60 }
61 }
62
63
64
65
66
67
68
69
70
71 public static long parse( BerValue value ) throws LongDecoderException
72 {
73 return parseLong( value );
74 }
75
76
77
78
79
80
81 public static long parseLong( BerValue value ) throws LongDecoderException
82 {
83 long result = 0;
84
85 byte[] bytes = value.getData();
86
87 if ( ( bytes == null ) || ( bytes.length == 0 ) )
88 {
89 throw new LongDecoderException( I18n.err( I18n.ERR_00039_0_BYTES_LONG_LONG ) );
90 }
91
92 if ( bytes.length > 8 )
93 {
94 throw new LongDecoderException( I18n.err( I18n.ERR_00039_0_BYTES_LONG_LONG ) );
95 }
96
97 for ( int i = 0; ( i < bytes.length ) && ( i < 9 ); i++ )
98 {
99 result = ( result << 8 ) | ( bytes[i] & 0x00FF );
100 }
101
102 if ( ( bytes[0] & 0x80 ) == 0x80 )
103 {
104 result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] );
105 }
106
107 return result;
108 }
109 }