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