1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.jsql.util.bruter;
19
20 import org.apache.commons.codec.binary.Base32;
21
22 import java.util.Base64;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class Base16 extends BaseNCodec {
47
48
49
50
51
52
53 private static final int BITS_PER_ENCODED_BYTE = 4;
54 private static final int BYTES_PER_ENCODED_BLOCK = 2;
55 private static final int BYTES_PER_UNENCODED_BLOCK = 1;
56
57
58
59
60
61
62 private static final byte[] UPPER_CASE_DECODE_TABLE = {
63
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
68 -1, 10, 11, 12, 13, 14, 15
69 };
70
71
72
73
74
75 private static final byte[] UPPER_CASE_ENCODE_TABLE = {
76 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
77 'A', 'B', 'C', 'D', 'E', 'F'
78 };
79
80
81
82
83
84
85 private static final byte[] LOWER_CASE_DECODE_TABLE = {
86
87 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
88 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
89 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
91 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
92 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
93 -1, 10, 11, 12, 13, 14, 15
94 };
95
96
97
98
99
100 private static final byte[] LOWER_CASE_ENCODE_TABLE = {
101 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
102 'a', 'b', 'c', 'd', 'e', 'f'
103 };
104
105
106 private static final int MASK_4BITS = 0x0f;
107
108
109
110
111 private final byte[] decodeTable;
112
113
114
115
116 private final byte[] encodeTable;
117
118
119
120
121 public Base16() {
122 this(false);
123 }
124
125
126
127
128
129
130 public Base16(final boolean lowerCase) {
131 this(lowerCase, BaseNCodec.DECODING_POLICY_DEFAULT);
132 }
133
134
135
136
137
138
139
140 public Base16(final boolean lowerCase, final CodecPolicy decodingPolicy) {
141 super(Base16.BYTES_PER_UNENCODED_BLOCK, Base16.BYTES_PER_ENCODED_BLOCK, 0, 0, BaseNCodec.PAD_DEFAULT, decodingPolicy);
142 if (lowerCase) {
143 this.encodeTable = Base16.LOWER_CASE_ENCODE_TABLE;
144 this.decodeTable = Base16.LOWER_CASE_DECODE_TABLE;
145 } else {
146 this.encodeTable = Base16.UPPER_CASE_ENCODE_TABLE;
147 this.decodeTable = Base16.UPPER_CASE_DECODE_TABLE;
148 }
149 }
150
151 @Override
152 public void decode(final byte[] data, int offsetInput, final int length, final Context context) {
153 int offset = offsetInput;
154
155 if (context.eof || length < 0) {
156 context.eof = true;
157 if (context.ibitWorkArea != 0) {
158 this.validateTrailingCharacter();
159 }
160 return;
161 }
162
163 final int dataLen = Math.min(data.length - offset, length);
164 final int availableChars = (context.ibitWorkArea != 0 ? 1 : 0) + dataLen;
165
166
167 if (availableChars == 1 && availableChars == dataLen) {
168 context.ibitWorkArea = this.decodeOctet(data[offset]) + 1;
169 return;
170 }
171
172
173 final int charsToProcess = availableChars % Base16.BYTES_PER_ENCODED_BLOCK == 0 ? availableChars : availableChars - 1;
174 final byte[] buffer = this.ensureBufferSize(charsToProcess / Base16.BYTES_PER_ENCODED_BLOCK, context);
175
176 int result;
177 var i = 0;
178 if (dataLen < availableChars) {
179
180 result = (context.ibitWorkArea - 1) << Base16.BITS_PER_ENCODED_BYTE;
181 result |= this.decodeOctet(data[offset++]);
182 i = 2;
183 buffer[context.pos++] = (byte) result;
184
185 context.ibitWorkArea = 0;
186 }
187
188 while (i < charsToProcess) {
189 result = this.decodeOctet(data[offset++]) << Base16.BITS_PER_ENCODED_BYTE;
190 result |= this.decodeOctet(data[offset++]);
191 i += 2;
192 buffer[context.pos++] = (byte) result;
193 }
194
195
196 if (i < dataLen) {
197 context.ibitWorkArea = this.decodeOctet(data[i]) + 1;
198 }
199 }
200
201 private int decodeOctet(final byte octet) {
202 int decoded = -1;
203 if ((octet & 0xff) < this.decodeTable.length) {
204 decoded = this.decodeTable[octet];
205 }
206 if (decoded == -1) {
207 throw new IllegalArgumentException("Invalid octet in encoded value: " + (int) octet);
208 }
209 return decoded;
210 }
211
212 @Override
213 public void encode(final byte[] data, final int offset, final int length, final Context context) {
214 if (context.eof) {
215 return;
216 }
217 if (length < 0) {
218 context.eof = true;
219 return;
220 }
221 final int size = length * Base16.BYTES_PER_ENCODED_BLOCK;
222 if (size < 0) {
223 throw new IllegalArgumentException("Input length exceeds maximum size for encoded data: " + length);
224 }
225 final byte[] buffer = this.ensureBufferSize(size, context);
226 final int end = offset + length;
227 for (int i = offset; i < end; i++) {
228 final int value = data[i];
229 final int high = (value >> Base16.BITS_PER_ENCODED_BYTE) & Base16.MASK_4BITS;
230 final int low = value & Base16.MASK_4BITS;
231 buffer[context.pos++] = this.encodeTable[high];
232 buffer[context.pos++] = this.encodeTable[low];
233 }
234 }
235
236
237
238
239
240
241
242
243 @Override
244 public boolean isInAlphabet(final byte octet) {
245 return (octet & 0xff) < this.decodeTable.length && this.decodeTable[octet] != -1;
246 }
247
248
249
250
251
252
253
254 private void validateTrailingCharacter() {
255 if (this.isStrictDecoding()) {
256 throw new IllegalArgumentException(
257 "Strict decoding: Last encoded character is a valid base 16 alphabet" +
258 "character but not a possible encoding. " +
259 "Decoding requires at least two characters to create one byte."
260 );
261 }
262 }
263 }
264