1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class Base32 extends BaseNCodec {
45
46
47
48
49
50
51 private static final int BITS_PER_ENCODED_BYTE = 5;
52 private static final int BYTES_PER_ENCODED_BLOCK = 8;
53 private static final int BYTES_PER_UNENCODED_BLOCK = 5;
54
55
56
57
58
59
60 private static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
61
62
63
64
65
66
67 private static final byte[] DECODE_TABLE = {
68
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
74 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
75 };
76
77
78
79
80
81 private static final byte[] ENCODE_TABLE = {
82 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
83 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
84 '2', '3', '4', '5', '6', '7',
85 };
86
87
88
89
90
91
92 private static final byte[] HEX_DECODE_TABLE = {
93
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
96 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
97 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
98 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
99 25, 26, 27, 28, 29, 30, 31, 32,
100 };
101
102
103
104
105
106 private static final byte[] HEX_ENCODE_TABLE = {
107 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
108 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
109 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
110 };
111
112
113 private static final int MASK_5BITS = 0x1f;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 private final int decodeSize;
129
130
131
132
133 private final byte[] decodeTable;
134
135
136
137
138
139 private final int encodeSize;
140
141
142
143
144 private final byte[] encodeTable;
145
146
147
148
149 private final byte[] lineSeparator;
150
151
152
153
154
155
156
157
158 public Base32() {
159 this(false);
160 }
161
162
163
164
165
166
167
168
169 public Base32(final byte pad) {
170 this(false, pad);
171 }
172
173
174
175
176
177
178
179
180 public Base32(final boolean useHex) {
181 this(0, null, useHex, PAD_DEFAULT);
182 }
183
184
185
186
187
188
189
190
191
192 public Base32(final boolean useHex, final byte pad) {
193 this(0, null, useHex, pad);
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207 public Base32(final int lineLength) {
208 this(lineLength, CHUNK_SEPARATOR);
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 public Base32(final int lineLength, final byte[] lineSeparator) {
230 this(lineLength, lineSeparator, false, PAD_DEFAULT);
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex) {
255 this(lineLength, lineSeparator, useHex, PAD_DEFAULT);
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte pad) {
281 super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength,
282 lineSeparator == null ? 0 : lineSeparator.length, pad);
283 if (useHex) {
284 this.encodeTable = HEX_ENCODE_TABLE;
285 this.decodeTable = HEX_DECODE_TABLE;
286 } else {
287 this.encodeTable = ENCODE_TABLE;
288 this.decodeTable = DECODE_TABLE;
289 }
290 if (lineLength > 0) {
291 if (lineSeparator == null) {
292 throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null");
293 }
294
295 if (containsAlphabetOrPad(lineSeparator)) {
296 final String sep = StringUtils.newStringUtf8(lineSeparator);
297 throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
298 }
299 this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
300 this.lineSeparator = new byte[lineSeparator.length];
301 System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
302 } else {
303 this.encodeSize = BYTES_PER_ENCODED_BLOCK;
304 this.lineSeparator = null;
305 }
306 this.decodeSize = this.encodeSize - 1;
307
308 if (isInAlphabet(pad) || isWhiteSpace(pad)) {
309 throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
310 }
311 }
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 @Override
336 void decode(final byte[] in, int inPos, final int inAvail, final Context context) {
337
338
339 if (context.eof) {
340 return;
341 }
342 if (inAvail < 0) {
343 context.eof = true;
344 }
345 for (int i = 0; i < inAvail; i++) {
346 final byte b = in[inPos++];
347 if (b == pad) {
348
349 context.eof = true;
350 break;
351 } else {
352 final byte[] buffer = ensureBufferSize(decodeSize, context);
353 if (b >= 0 && b < this.decodeTable.length) {
354 final int result = this.decodeTable[b];
355 if (result >= 0) {
356 context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK;
357
358 context.lbitWorkArea = (context.lbitWorkArea << BITS_PER_ENCODED_BYTE) + result;
359 if (context.modulus == 0) {
360 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 32) & MASK_8BITS);
361 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 24) & MASK_8BITS);
362 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
363 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
364 buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
365 }
366 }
367 }
368 }
369 }
370
371
372
373
374 if (context.eof && context.modulus >= 2) {
375 final byte[] buffer = ensureBufferSize(decodeSize, context);
376
377
378 switch (context.modulus) {
379 case 2 :
380 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 2) & MASK_8BITS);
381 break;
382 case 3 :
383 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 7) & MASK_8BITS);
384 break;
385 case 4 :
386 context.lbitWorkArea = context.lbitWorkArea >> 4;
387 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
388 buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
389 break;
390 case 5 :
391 context.lbitWorkArea = context.lbitWorkArea >> 1;
392 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
393 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
394 buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
395 break;
396 case 6 :
397 context.lbitWorkArea = context.lbitWorkArea >> 6;
398 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
399 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
400 buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
401 break;
402 case 7 :
403 context.lbitWorkArea = context.lbitWorkArea >> 3;
404 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 24) & MASK_8BITS);
405 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
406 buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
407 buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
408 break;
409 default:
410
411 throw new IllegalStateException("Impossible modulus "+context.modulus);
412 }
413 }
414 }
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431 @Override
432 void encode(final byte[] in, int inPos, final int inAvail, final Context context) {
433
434
435 if (context.eof) {
436 return;
437 }
438
439
440 if (inAvail < 0) {
441 context.eof = true;
442 if (0 == context.modulus && lineLength == 0) {
443 return;
444 }
445 final byte[] buffer = ensureBufferSize(encodeSize, context);
446 final int savedPos = context.pos;
447 switch (context.modulus) {
448 case 0 :
449 break;
450 case 1 :
451 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 3) & MASK_5BITS];
452 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 2) & MASK_5BITS];
453 buffer[context.pos++] = pad;
454 buffer[context.pos++] = pad;
455 buffer[context.pos++] = pad;
456 buffer[context.pos++] = pad;
457 buffer[context.pos++] = pad;
458 buffer[context.pos++] = pad;
459 break;
460 case 2 :
461 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 11) & MASK_5BITS];
462 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 6) & MASK_5BITS];
463 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 1) & MASK_5BITS];
464 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 4) & MASK_5BITS];
465 buffer[context.pos++] = pad;
466 buffer[context.pos++] = pad;
467 buffer[context.pos++] = pad;
468 buffer[context.pos++] = pad;
469 break;
470 case 3 :
471 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 19) & MASK_5BITS];
472 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 14) & MASK_5BITS];
473 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 9) & MASK_5BITS];
474 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 4) & MASK_5BITS];
475 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 1) & MASK_5BITS];
476 buffer[context.pos++] = pad;
477 buffer[context.pos++] = pad;
478 buffer[context.pos++] = pad;
479 break;
480 case 4 :
481 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 27) & MASK_5BITS];
482 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 22) & MASK_5BITS];
483 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 17) & MASK_5BITS];
484 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 12) & MASK_5BITS];
485 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 7) & MASK_5BITS];
486 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 2) & MASK_5BITS];
487 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 3) & MASK_5BITS];
488 buffer[context.pos++] = pad;
489 break;
490 default:
491 throw new IllegalStateException("Impossible modulus "+context.modulus);
492 }
493 context.currentLinePos += context.pos - savedPos;
494
495 if (lineLength > 0 && context.currentLinePos > 0){
496 System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
497 context.pos += lineSeparator.length;
498 }
499 } else {
500 for (int i = 0; i < inAvail; i++) {
501 final byte[] buffer = ensureBufferSize(encodeSize, context);
502 context.modulus = (context.modulus+1) % BYTES_PER_UNENCODED_BLOCK;
503 int b = in[inPos++];
504 if (b < 0) {
505 b += 256;
506 }
507 context.lbitWorkArea = (context.lbitWorkArea << 8) + b;
508 if (0 == context.modulus) {
509 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 35) & MASK_5BITS];
510 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 30) & MASK_5BITS];
511 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 25) & MASK_5BITS];
512 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 20) & MASK_5BITS];
513 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 15) & MASK_5BITS];
514 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 10) & MASK_5BITS];
515 buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 5) & MASK_5BITS];
516 buffer[context.pos++] = encodeTable[(int)context.lbitWorkArea & MASK_5BITS];
517 context.currentLinePos += BYTES_PER_ENCODED_BLOCK;
518 if (lineLength > 0 && lineLength <= context.currentLinePos) {
519 System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
520 context.pos += lineSeparator.length;
521 context.currentLinePos = 0;
522 }
523 }
524 }
525 }
526 }
527
528
529
530
531
532
533
534
535 @Override
536 public boolean isInAlphabet(final byte octet) {
537 return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
538 }
539 }