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.util;
21
22
23 import javax.naming.InvalidNameException;
24
25 import org.apache.directory.api.i18n.I18n;
26
27
28
29
30
31
32
33
34
35 public class Hex
36 {
37
38 public static final byte[] HEX_VALUE =
39 {
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
44 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
48 };
49
50
51 public static final char[] HEX_CHAR =
52 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
53
54
55
56
57
58
59
60
61
62
63 public static byte getHexValue( char high, char low )
64 {
65 if ( ( high > 127 ) || ( low > 127 ) || ( high < 0 ) | ( low < 0 ) )
66 {
67 return -1;
68 }
69
70 return ( byte ) ( ( HEX_VALUE[high] << 4 ) | HEX_VALUE[low] );
71 }
72
73
74
75
76
77
78
79
80
81
82 public static byte getHexValue( byte high, byte low )
83 {
84 if ( ( ( high & 0x7F ) != high ) || ( ( low & 0x7F ) != low ) )
85 {
86 return -1;
87 }
88
89 return ( byte ) ( ( HEX_VALUE[high] << 4 ) | HEX_VALUE[low] );
90 }
91
92
93
94
95
96
97
98
99
100 public static byte getHexValue( char c )
101 {
102 if ( ( c > 127 ) || ( c < 0 ) )
103 {
104 return -1;
105 }
106
107 return HEX_VALUE[c];
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 public static String decodeHexString( String str ) throws InvalidNameException
122 {
123 if ( str == null || str.length() == 0 )
124 {
125 throw new InvalidNameException( I18n.err( I18n.ERR_04431 ) );
126 }
127
128 char[] chars = str.toCharArray();
129
130 if ( chars[0] != '#' )
131 {
132 throw new InvalidNameException( I18n.err( I18n.ERR_04432, str ) );
133 }
134
135
136
137 byte[] decoded = new byte[( chars.length - 1 ) >> 1];
138
139 for ( int ii = 1, jj = 0; ii < chars.length; ii += 2, jj++ )
140 {
141 int ch = ( HEX_VALUE[chars[ii]] << 4 )
142 + HEX_VALUE[chars[ii + 1]];
143 decoded[jj] = ( byte ) ch;
144 }
145
146 return Strings.utf8ToString( decoded );
147 }
148
149
150
151
152
153
154
155
156 public static byte[] convertEscapedHex( String str ) throws InvalidNameException
157 {
158 if ( str == null )
159 {
160 throw new InvalidNameException( I18n.err( I18n.ERR_04433 ) );
161 }
162
163 int length = str.length();
164
165 if ( length == 0 )
166 {
167 throw new InvalidNameException( I18n.err( I18n.ERR_04434 ) );
168 }
169
170
171 byte[] buf = new byte[str.length() / 3];
172 int pos = 0;
173
174
175 for ( int i = 0; i < length; i++ )
176 {
177 char c = str.charAt( i );
178
179 if ( c == '\\' )
180 {
181
182 if ( Chars.isHex( str, i + 1 ) && Chars.isHex( str, i + 2 ) )
183 {
184 byte value = ( byte ) ( ( HEX_VALUE[str.charAt( i + 1 )] << 4 )
185 + HEX_VALUE[str.charAt( i + 2 )] );
186
187 i += 2;
188 buf[pos++] = value;
189 }
190 }
191 else
192 {
193 throw new InvalidNameException( I18n.err( I18n.ERR_04435 ) );
194 }
195 }
196
197 return buf;
198 }
199
200
201
202
203
204
205
206
207
208
209
210 public static char[] encodeHex( byte[] data )
211 {
212 int l = data.length;
213
214 char[] out = new char[l << 1];
215
216
217 for ( int i = 0, j = 0; i < l; i++ )
218 {
219 out[j++] = HEX_CHAR[( 0xF0 & data[i] ) >>> 4];
220 out[j++] = HEX_CHAR[0x0F & data[i]];
221 }
222
223 return out;
224 }
225 }