View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.binary;
19  
20  import org.apache.commons.codec.CodecPolicy;
21  
22  /**
23   * Provides Base32 encoding and decoding as defined by <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>.
24   *
25   * <p>
26   * The class can be parameterized in the following manner with various constructors:
27   * </p>
28   * <ul>
29   * <li>Whether to use the "base32hex" variant instead of the default "base32"</li>
30   * <li>Line length: Default 76. Line length that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
31   * <li>Line separator: Default is CRLF ("\r\n")</li>
32   * </ul>
33   * <p>
34   * This class operates directly on byte streams, and not character streams.
35   * </p>
36   * <p>
37   * This class is thread-safe.
38   * </p>
39   *
40   * @see <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a>
41   *
42   * @since 1.5
43   */
44  public class Base32 extends BaseNCodec {
45  
46      /**
47       * BASE32 characters are 5 bits in length. They are formed by taking a block of five octets to form a 40-bit string, which is converted into eight BASE32
48       * characters.
49       */
50      private static final int BITS_PER_ENCODED_BYTE = 5;
51      private static final int BYTES_PER_ENCODED_BLOCK = 8;
52      private static final int BYTES_PER_UNENCODED_BLOCK = 5;
53  
54      /**
55       * This array is a lookup table that translates Unicode characters drawn from the "Base32 Alphabet" (as specified in Table 3 of RFC 4648) into their 5-bit
56       * positive integer equivalents. Characters that are not in the Base32 alphabet but fall within the bounds of the array are translated to -1.
57       */
58      // @formatter:off
59      private static final byte[] DECODE_TABLE = {
60           //  0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
61              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
62              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
63              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
64              -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 30-3f 2-7
65              -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, // 40-4f A-O
66              15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,                     // 50-5a P-Z
67                                                          -1, -1, -1, -1, -1, // 5b-5f
68              -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, // 60-6f a-o
69              15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,                     // 70-7a p-z
70      };
71      // @formatter:on
72  
73      /**
74       * This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Alphabet" equivalents as specified in Table 3 of RFC
75       * 4648.
76       */
77      // @formatter:off
78      private static final byte[] ENCODE_TABLE = {
79              'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
80              'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
81              '2', '3', '4', '5', '6', '7',
82      };
83      // @formatter:on
84  
85      /**
86       * This array is a lookup table that translates Unicode characters drawn from the "Base32 Hex Alphabet" (as specified in Table 4 of RFC 4648) into their
87       * 5-bit positive integer equivalents. Characters that are not in the Base32 Hex alphabet but fall within the bounds of the array are translated to -1.
88       */
89      // @formatter:off
90      private static final byte[] HEX_DECODE_TABLE = {
91           //  0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
92              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
93              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
94              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
95               0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, // 30-3f 0-9
96              -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 40-4f A-O
97              25, 26, 27, 28, 29, 30, 31,                                     // 50-56 P-V
98                                          -1, -1, -1, -1, -1, -1, -1, -1, -1, // 57-5f
99              -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 60-6f a-o
100             25, 26, 27, 28, 29, 30, 31                                      // 70-76 p-v
101     };
102     // @formatter:on
103 
104     /**
105      * This array is a lookup table that translates 5-bit positive integer index values into their "Base32 Hex Alphabet" equivalents as specified in Table 4 of
106      * RFC 4648.
107      */
108     // @formatter:off
109     private static final byte[] HEX_ENCODE_TABLE = {
110             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
111             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
112             'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
113     };
114     // @formatter:on
115 
116     /** Mask used to extract 5 bits, used when encoding Base32 bytes */
117     private static final int MASK_5BITS = 0x1f;
118 
119     /** Mask used to extract 4 bits, used when decoding final trailing character. */
120     private static final long MASK_4BITS = 0x0fL;
121 
122     /** Mask used to extract 3 bits, used when decoding final trailing character. */
123     private static final long MASK_3BITS = 0x07L;
124 
125     /** Mask used to extract 2 bits, used when decoding final trailing character. */
126     private static final long MASK_2BITS = 0x03L;
127 
128     /** Mask used to extract 1 bits, used when decoding final trailing character. */
129     private static final long MASK_1BITS = 0x01L;
130 
131     // The static final fields above are used for the original static byte[] methods on Base32.
132     // The private member fields below are used with the new streaming approach, which requires
133     // some state be preserved between calls of encode() and decode().
134 
135     /**
136      * Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. {@code decodeSize = {@link
137      * #BYTES_PER_ENCODED_BLOCK} - 1 + lineSeparator.length;}
138      */
139     private final int decodeSize;
140 
141     /**
142      * Decode table to use.
143      */
144     private final byte[] decodeTable;
145 
146     /**
147      * Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. {@code encodeSize = {@link
148      * #BYTES_PER_ENCODED_BLOCK} + lineSeparator.length;}
149      */
150     private final int encodeSize;
151 
152     /**
153      * Encode table to use.
154      */
155     private final byte[] encodeTable;
156 
157     /**
158      * Line separator for encoding. Not used when decoding. Only used if lineLength &gt; 0.
159      */
160     private final byte[] lineSeparator;
161 
162     /**
163      * Creates a Base32 codec used for decoding and encoding.
164      * <p>
165      * When encoding the line length is 0 (no chunking).
166      * </p>
167      */
168     public Base32() {
169         this(false);
170     }
171 
172     /**
173      * Creates a Base32 codec used for decoding and encoding.
174      * <p>
175      * When encoding the line length is 0 (no chunking).
176      * </p>
177      *
178      * @param useHex if {@code true} then use Base32 Hex alphabet
179      */
180     public Base32(final boolean useHex) {
181         this(0, null, useHex, PAD_DEFAULT);
182     }
183 
184     /**
185      * Creates a Base32 codec used for decoding and encoding.
186      * <p>
187      * When encoding the line length is 0 (no chunking).
188      * </p>
189      *
190      * @param useHex  if {@code true} then use Base32 Hex alphabet
191      * @param padding byte used as padding byte.
192      */
193     public Base32(final boolean useHex, final byte padding) {
194         this(0, null, useHex, padding);
195     }
196 
197     /**
198      * Creates a Base32 codec used for decoding and encoding.
199      * <p>
200      * When encoding the line length is 0 (no chunking).
201      * </p>
202      *
203      * @param pad byte used as padding byte.
204      */
205     public Base32(final byte pad) {
206         this(false, pad);
207     }
208 
209     /**
210      * Creates a Base32 codec used for decoding and encoding.
211      * <p>
212      * When encoding the line length is given in the constructor, the line separator is CRLF.
213      * </p>
214      *
215      * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength &lt;= 0, then
216      *                   the output will not be divided into lines (chunks). Ignored when decoding.
217      */
218     public Base32(final int lineLength) {
219         this(lineLength, CHUNK_SEPARATOR);
220     }
221 
222     /**
223      * Creates a Base32 codec used for decoding and encoding.
224      * <p>
225      * When encoding the line length and line separator are given in the constructor.
226      * </p>
227      * <p>
228      * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
229      * </p>
230      *
231      * @param lineLength    Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength &lt;= 0,
232      *                      then the output will not be divided into lines (chunks). Ignored when decoding.
233      * @param lineSeparator Each line of encoded data will end with this sequence of bytes.
234      * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base32 characters.
235      */
236     public Base32(final int lineLength, final byte[] lineSeparator) {
237         this(lineLength, lineSeparator, false, PAD_DEFAULT);
238     }
239 
240     /**
241      * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
242      * <p>
243      * When encoding the line length and line separator are given in the constructor.
244      * </p>
245      * <p>
246      * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
247      * </p>
248      *
249      * @param lineLength    Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength &lt;= 0,
250      *                      then the output will not be divided into lines (chunks). Ignored when decoding.
251      * @param lineSeparator Each line of encoded data will end with this sequence of bytes.
252      * @param useHex        if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
253      * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base32 characters. Or the lineLength &gt; 0 and lineSeparator is null.
254      */
255     public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex) {
256         this(lineLength, lineSeparator, useHex, PAD_DEFAULT);
257     }
258 
259     /**
260      * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
261      * <p>
262      * When encoding the line length and line separator are given in the constructor.
263      * </p>
264      * <p>
265      * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
266      * </p>
267      *
268      * @param lineLength    Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength &lt;= 0,
269      *                      then the output will not be divided into lines (chunks). Ignored when decoding.
270      * @param lineSeparator Each line of encoded data will end with this sequence of bytes.
271      * @param useHex        if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
272      * @param padding       byte used as padding byte.
273      * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base32 characters. Or the lineLength &gt; 0 and lineSeparator is null.
274      */
275     public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte padding) {
276         this(lineLength, lineSeparator, useHex, padding, DECODING_POLICY_DEFAULT);
277     }
278 
279     /**
280      * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
281      * <p>
282      * When encoding the line length and line separator are given in the constructor.
283      * </p>
284      * <p>
285      * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
286      * </p>
287      *
288      * @param lineLength     Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength &lt;= 0,
289      *                       then the output will not be divided into lines (chunks). Ignored when decoding.
290      * @param lineSeparator  Each line of encoded data will end with this sequence of bytes.
291      * @param useHex         if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
292      * @param padding        byte used as padding byte.
293      * @param decodingPolicy The decoding policy.
294      * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base32 characters. Or the lineLength &gt; 0 and lineSeparator is null.
295      * @since 1.15
296      */
297     public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte padding, final CodecPolicy decodingPolicy) {
298         super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, lineSeparator == null ? 0 : lineSeparator.length, padding, decodingPolicy);
299         if (useHex) {
300             this.encodeTable = HEX_ENCODE_TABLE;
301             this.decodeTable = HEX_DECODE_TABLE;
302         } else {
303             this.encodeTable = ENCODE_TABLE;
304             this.decodeTable = DECODE_TABLE;
305         }
306         if (lineLength > 0) {
307             if (lineSeparator == null) {
308                 throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null");
309             }
310             // Must be done after initializing the tables
311             if (containsAlphabetOrPad(lineSeparator)) {
312                 final String sep = StringUtils.newStringUtf8(lineSeparator);
313                 throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
314             }
315             this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
316             this.lineSeparator = lineSeparator.clone();
317         } else {
318             this.encodeSize = BYTES_PER_ENCODED_BLOCK;
319             this.lineSeparator = null;
320         }
321         this.decodeSize = this.encodeSize - 1;
322 
323         if (isInAlphabet(padding) || Character.isWhitespace(padding)) {
324             throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
325         }
326     }
327 
328     /**
329      * <p>
330      * Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once with the data to decode, and once with
331      * inAvail set to "-1" to alert decoder that EOF has been reached. The "-1" call is not necessary when decoding, but it doesn't hurt, either.
332      * </p>
333      * <p>
334      * Ignores all non-Base32 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are silently ignored, but has implications
335      * for other bytes, too. This method subscribes to the garbage-in, garbage-out philosophy: it will not check the provided data for validity.
336      * </p>
337      * <p>
338      * Output is written to {@link org.apache.commons.codec.binary.BaseNCodec.Context#buffer Context#buffer} as 8-bit octets, using
339      * {@link org.apache.commons.codec.binary.BaseNCodec.Context#pos Context#pos} as the buffer position
340      * </p>
341      *
342      * @param input   byte[] array of ASCII data to Base32 decode.
343      * @param inPos   Position to start reading data from.
344      * @param inAvail Amount of bytes available from input for decoding.
345      * @param context the context to be used
346      */
347     @Override
348     void decode(final byte[] input, int inPos, final int inAvail, final Context context) {
349         // package protected for access from I/O streams
350 
351         if (context.eof) {
352             return;
353         }
354         if (inAvail < 0) {
355             context.eof = true;
356         }
357         for (int i = 0; i < inAvail; i++) {
358             final byte b = input[inPos++];
359             if (b == pad) {
360                 // We're done.
361                 context.eof = true;
362                 break;
363             }
364             final byte[] buffer = ensureBufferSize(decodeSize, context);
365             if (b >= 0 && b < this.decodeTable.length) {
366                 final int result = this.decodeTable[b];
367                 if (result >= 0) {
368                     context.modulus = (context.modulus + 1) % BYTES_PER_ENCODED_BLOCK;
369                     // collect decoded bytes
370                     context.lbitWorkArea = (context.lbitWorkArea << BITS_PER_ENCODED_BYTE) + result;
371                     if (context.modulus == 0) { // we can output the 5 bytes
372                         buffer[context.pos++] = (byte) (context.lbitWorkArea >> 32 & MASK_8BITS);
373                         buffer[context.pos++] = (byte) (context.lbitWorkArea >> 24 & MASK_8BITS);
374                         buffer[context.pos++] = (byte) (context.lbitWorkArea >> 16 & MASK_8BITS);
375                         buffer[context.pos++] = (byte) (context.lbitWorkArea >> 8 & MASK_8BITS);
376                         buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
377                     }
378                 }
379             }
380         }
381 
382         // Two forms of EOF as far as Base32 decoder is concerned: actual
383         // EOF (-1) and first time '=' character is encountered in stream.
384         // This approach makes the '=' padding characters completely optional.
385         if (context.eof && context.modulus > 0) { // if modulus == 0, nothing to do
386             final byte[] buffer = ensureBufferSize(decodeSize, context);
387 
388             // We ignore partial bytes, i.e. only multiples of 8 count.
389             // Any combination not part of a valid encoding is either partially decoded
390             // or will raise an exception. Possible trailing characters are 2, 4, 5, 7.
391             // It is not possible to encode with 1, 3, 6 trailing characters.
392             // For backwards compatibility 3 & 6 chars are decoded anyway rather than discarded.
393             // See the encode(byte[]) method EOF section.
394             switch (context.modulus) {
395 //              case 0 : // impossible, as excluded above
396             case 1: // 5 bits - either ignore entirely, or raise an exception
397                 validateTrailingCharacters();
398             case 2: // 10 bits, drop 2 and output one byte
399                 validateCharacter(MASK_2BITS, context);
400                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 2 & MASK_8BITS);
401                 break;
402             case 3: // 15 bits, drop 7 and output 1 byte, or raise an exception
403                 validateTrailingCharacters();
404                 // Not possible from a valid encoding but decode anyway
405                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 7 & MASK_8BITS);
406                 break;
407             case 4: // 20 bits = 2*8 + 4
408                 validateCharacter(MASK_4BITS, context);
409                 context.lbitWorkArea = context.lbitWorkArea >> 4; // drop 4 bits
410                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 8 & MASK_8BITS);
411                 buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
412                 break;
413             case 5: // 25 bits = 3*8 + 1
414                 validateCharacter(MASK_1BITS, context);
415                 context.lbitWorkArea = context.lbitWorkArea >> 1;
416                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 16 & MASK_8BITS);
417                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 8 & MASK_8BITS);
418                 buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
419                 break;
420             case 6: // 30 bits = 3*8 + 6, or raise an exception
421                 validateTrailingCharacters();
422                 // Not possible from a valid encoding but decode anyway
423                 context.lbitWorkArea = context.lbitWorkArea >> 6;
424                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 16 & MASK_8BITS);
425                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 8 & MASK_8BITS);
426                 buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
427                 break;
428             case 7: // 35 bits = 4*8 +3
429                 validateCharacter(MASK_3BITS, context);
430                 context.lbitWorkArea = context.lbitWorkArea >> 3;
431                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 24 & MASK_8BITS);
432                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 16 & MASK_8BITS);
433                 buffer[context.pos++] = (byte) (context.lbitWorkArea >> 8 & MASK_8BITS);
434                 buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
435                 break;
436             default:
437                 // modulus can be 0-7, and we excluded 0,1 already
438                 throw new IllegalStateException("Impossible modulus " + context.modulus);
439             }
440         }
441     }
442 
443     /**
444      * <p>
445      * Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with the data to encode, and once with
446      * inAvail set to "-1" to alert encoder that EOF has been reached, so flush last remaining bytes (if not multiple of 5).
447      * </p>
448      *
449      * @param input   byte[] array of binary data to Base32 encode.
450      * @param inPos   Position to start reading data from.
451      * @param inAvail Amount of bytes available from input for encoding.
452      * @param context the context to be used
453      */
454     @Override
455     void encode(final byte[] input, int inPos, final int inAvail, final Context context) {
456         // package protected for access from I/O streams
457 
458         if (context.eof) {
459             return;
460         }
461         // inAvail < 0 is how we're informed of EOF in the underlying data we're
462         // encoding.
463         if (inAvail < 0) {
464             context.eof = true;
465             if (0 == context.modulus && lineLength == 0) {
466                 return; // no leftovers to process and not using chunking
467             }
468             final byte[] buffer = ensureBufferSize(encodeSize, context);
469             final int savedPos = context.pos;
470             switch (context.modulus) { // % 5
471             case 0:
472                 break;
473             case 1: // Only 1 octet; take top 5 bits then remainder
474                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 3) & MASK_5BITS]; // 8-1*5 = 3
475                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea << 2) & MASK_5BITS]; // 5-3=2
476                 buffer[context.pos++] = pad;
477                 buffer[context.pos++] = pad;
478                 buffer[context.pos++] = pad;
479                 buffer[context.pos++] = pad;
480                 buffer[context.pos++] = pad;
481                 buffer[context.pos++] = pad;
482                 break;
483             case 2: // 2 octets = 16 bits to use
484                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 11) & MASK_5BITS]; // 16-1*5 = 11
485                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 6) & MASK_5BITS]; // 16-2*5 = 6
486                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 1) & MASK_5BITS]; // 16-3*5 = 1
487                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea << 4) & MASK_5BITS]; // 5-1 = 4
488                 buffer[context.pos++] = pad;
489                 buffer[context.pos++] = pad;
490                 buffer[context.pos++] = pad;
491                 buffer[context.pos++] = pad;
492                 break;
493             case 3: // 3 octets = 24 bits to use
494                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 19) & MASK_5BITS]; // 24-1*5 = 19
495                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 14) & MASK_5BITS]; // 24-2*5 = 14
496                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 9) & MASK_5BITS]; // 24-3*5 = 9
497                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 4) & MASK_5BITS]; // 24-4*5 = 4
498                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea << 1) & MASK_5BITS]; // 5-4 = 1
499                 buffer[context.pos++] = pad;
500                 buffer[context.pos++] = pad;
501                 buffer[context.pos++] = pad;
502                 break;
503             case 4: // 4 octets = 32 bits to use
504                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 27) & MASK_5BITS]; // 32-1*5 = 27
505                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 22) & MASK_5BITS]; // 32-2*5 = 22
506                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 17) & MASK_5BITS]; // 32-3*5 = 17
507                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 12) & MASK_5BITS]; // 32-4*5 = 12
508                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 7) & MASK_5BITS]; // 32-5*5 = 7
509                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 2) & MASK_5BITS]; // 32-6*5 = 2
510                 buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea << 3) & MASK_5BITS]; // 5-2 = 3
511                 buffer[context.pos++] = pad;
512                 break;
513             default:
514                 throw new IllegalStateException("Impossible modulus " + context.modulus);
515             }
516             context.currentLinePos += context.pos - savedPos; // keep track of current line position
517             // if currentPos == 0 we are at the start of a line, so don't add CRLF
518             if (lineLength > 0 && context.currentLinePos > 0) { // add chunk separator if required
519                 System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
520                 context.pos += lineSeparator.length;
521             }
522         } else {
523             for (int i = 0; i < inAvail; i++) {
524                 final byte[] buffer = ensureBufferSize(encodeSize, context);
525                 context.modulus = (context.modulus + 1) % BYTES_PER_UNENCODED_BLOCK;
526                 int b = input[inPos++];
527                 if (b < 0) {
528                     b += 256;
529                 }
530                 context.lbitWorkArea = (context.lbitWorkArea << 8) + b; // BITS_PER_BYTE
531                 if (0 == context.modulus) { // we have enough bytes to create our output
532                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 35) & MASK_5BITS];
533                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 30) & MASK_5BITS];
534                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 25) & MASK_5BITS];
535                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 20) & MASK_5BITS];
536                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 15) & MASK_5BITS];
537                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 10) & MASK_5BITS];
538                     buffer[context.pos++] = encodeTable[(int) (context.lbitWorkArea >> 5) & MASK_5BITS];
539                     buffer[context.pos++] = encodeTable[(int) context.lbitWorkArea & MASK_5BITS];
540                     context.currentLinePos += BYTES_PER_ENCODED_BLOCK;
541                     if (lineLength > 0 && lineLength <= context.currentLinePos) {
542                         System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
543                         context.pos += lineSeparator.length;
544                         context.currentLinePos = 0;
545                     }
546                 }
547             }
548         }
549     }
550 
551     /**
552      * Returns whether or not the {@code octet} is in the Base32 alphabet.
553      *
554      * @param octet The value to test
555      * @return {@code true} if the value is defined in the Base32 alphabet {@code false} otherwise.
556      */
557     @Override
558     public boolean isInAlphabet(final byte octet) {
559         return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
560     }
561 
562     /**
563      * Validates whether decoding the final trailing character is possible in the context of the set of possible base 32 values.
564      * <p>
565      * The character is valid if the lower bits within the provided mask are zero. This is used to test the final trailing base-32 digit is zero in the bits
566      * that will be discarded.
567      * </p>
568      *
569      * @param emptyBitsMask The mask of the lower bits that should be empty
570      * @param context       the context to be used
571      *
572      * @throws IllegalArgumentException if the bits being checked contain any non-zero value
573      */
574     private void validateCharacter(final long emptyBitsMask, final Context context) {
575         // Use the long bit work area
576         if (isStrictDecoding() && (context.lbitWorkArea & emptyBitsMask) != 0) {
577             throw new IllegalArgumentException("Strict decoding: Last encoded character (before the paddings if any) is a valid " +
578                     "base 32 alphabet but not a possible encoding. Expected the discarded bits from the character to be zero.");
579         }
580     }
581 
582     /**
583      * Validates whether decoding allows final trailing characters that cannot be created during encoding.
584      *
585      * @throws IllegalArgumentException if strict decoding is enabled
586      */
587     private void validateTrailingCharacters() {
588         if (isStrictDecoding()) {
589             throw new IllegalArgumentException("Strict decoding: Last encoded character(s) (before the paddings if any) are valid " +
590                     "base 32 alphabet but not a possible encoding. Decoding requires either 2, 4, 5, or 7 trailing 5-bit characters to create bytes.");
591         }
592     }
593 }