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