    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     * Place holder for the bytes we're dealing with for our based logic.
    129     * Bitwise operations store and extract the encoding or decoding from this variable.
    130     */
    131
    132    /**
    133     * Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
    134     * {@code decodeSize = {@link #BYTES_PER_ENCODED_BLOCK} - 1 + lineSeparator.length;}
    135     */
    136    private final int decodeSize;
    137
    138    /**
    139     * Decode table to use.
    140     */
    141    private final byte[] decodeTable;
    142
    143    /**
    144     * Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
    145     * {@code encodeSize = {@link #BYTES_PER_ENCODED_BLOCK} + lineSeparator.length;}
    146     */
    147    private final int encodeSize;
    148
    149    /**
    150     * Encode table to use.
    151     */
    152    private final byte[] encodeTable;
    153
    154    /**
    155     * Line separator for encoding. Not used when decoding. Only used if lineLength &gt; 0.
    156     */
    157    private final byte[] lineSeparator;
    158
    159    /**
    160     * Creates a Base32 codec used for decoding and encoding.
    161     * <p>
    162     * When encoding the line length is 0 (no chunking).
    163     * </p>
    164     *
    165     */
    166    public Base32() {
    167        this(false);
    168    }
    169
    170    /**
    171     * Creates a Base32 codec used for decoding and encoding.
    172     * <p>
    173     * When encoding the line length is 0 (no chunking).
    174     * </p>
    175     * @param useHex if {@code true} then use Base32 Hex alphabet
    176     */
    177    public Base32(final boolean useHex) {
    178        this(0, null, useHex, PAD_DEFAULT);
    179    }
    180
    181    /**
    182     * Creates a Base32 codec used for decoding and encoding.
    183     * <p>
    184     * When encoding the line length is 0 (no chunking).
    185     * </p>
    186     * @param useHex if {@code true} then use Base32 Hex alphabet
    187     * @param padding byte used as padding byte.
    188     */
    189    public Base32(final boolean useHex, final byte padding) {
    190        this(0, null, useHex, padding);
    191    }
    192
    193    /**
    194     * Creates a Base32 codec used for decoding and encoding.
    195     * <p>
    196     * When encoding the line length is 0 (no chunking).
    197     * </p>
    198     * @param pad byte used as padding byte.
    199     */
    200    public Base32(final byte pad) {
    201        this(false, pad);
    202    }
    203
    204    /**
    205     * Creates a Base32 codec used for decoding and encoding.
    206     * <p>
    207     * When encoding the line length is given in the constructor, the line separator is CRLF.
    208     * </p>
    209     *
    210     * @param lineLength
    211     *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
    212     *            8). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
    213     *            decoding.
    214     */
    215    public Base32(final int lineLength) {
    216        this(lineLength, CHUNK_SEPARATOR);
    217    }
    218
    219    /**
    220     * Creates a Base32 codec used for decoding and encoding.
    221     * <p>
    222     * When encoding the line length and line separator are given in the constructor.
    223     * </p>
    224     * <p>
    225     * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
    226     * </p>
    227     *
    228     * @param lineLength
    229     *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
    230     *            8). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
    231     *            decoding.
    232     * @param lineSeparator
    233     *            Each line of encoded data will end with this sequence of bytes.
    234     * @throws IllegalArgumentException
    235     *             Thrown when the {@code lineSeparator} contains Base32 characters.
    236     */
    237    public Base32(final int lineLength, final byte[] lineSeparator) {
    238        this(lineLength, lineSeparator, false, PAD_DEFAULT);
    239    }
    240
    241    /**
    242     * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
    243     * <p>
    244     * When encoding the line length and line separator are given in the constructor.
    245     * </p>
    246     * <p>
    247     * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
    248     * </p>
    249     *
    250     * @param lineLength
    251     *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
    252     *            8). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
    253     *            decoding.
    254     * @param lineSeparator
    255     *            Each line of encoded data will end with this sequence of bytes.
    256     * @param useHex
    257     *            if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
    258     * @throws IllegalArgumentException
    259     *             Thrown when the {@code lineSeparator} contains Base32 characters. Or the
    260     *             lineLength &gt; 0 and lineSeparator is null.
    261     */
    262    public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex) {
    263        this(lineLength, lineSeparator, useHex, PAD_DEFAULT);
    264    }
    265
    266    /**
    267     * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
    268     * <p>
    269     * When encoding the line length and line separator are given in the constructor.
    270     * </p>
    271     * <p>
    272     * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
    273     * </p>
    274     *
    275     * @param lineLength
    276     *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
    277     *            8). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
    278     *            decoding.
    279     * @param lineSeparator
    280     *            Each line of encoded data will end with this sequence of bytes.
    281     * @param useHex
    282     *            if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
    283     * @param padding byte used as padding byte.
    284     * @throws IllegalArgumentException
    285     *             Thrown when the {@code lineSeparator} contains Base32 characters. Or the
    286     *             lineLength &gt; 0 and lineSeparator is null.
    287     */
    288    public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte padding) {
    289        this(lineLength, lineSeparator, useHex, padding, DECODING_POLICY_DEFAULT);
    290    }
    291
    292    /**
    293     * Creates a Base32 / Base32 Hex codec used for decoding and encoding.
    294     * <p>
    295     * When encoding the line length and line separator are given in the constructor.
    296     * </p>
    297     * <p>
    298     * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data.
    299     * </p>
    300     *
    301     * @param lineLength
    302     *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
    303     *            8). If lineLength &lt;= 0, then the output will not be divided into lines (chunks). Ignored when
    304     *            decoding.
    305     * @param lineSeparator
    306     *            Each line of encoded data will end with this sequence of bytes.
    307     * @param useHex
    308     *            if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
    309     * @param padding byte used as padding byte.
    310     * @param decodingPolicy The decoding policy.
    311     * @throws IllegalArgumentException
    312     *             Thrown when the {@code lineSeparator} contains Base32 characters. Or the
    313     *             lineLength &gt; 0 and lineSeparator is null.
    314     * @since 1.15
    315     */
    316    public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte padding, CodecPolicy decodingPolicy) {
    317        super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength,
    318                lineSeparator == null ? 0 : lineSeparator.length, padding, decodingPolicy);
    319        if (useHex) {
    320            this.encodeTable = HEX_ENCODE_TABLE;
    321            this.decodeTable = HEX_DECODE_TABLE;
    322        } else {
    323            this.encodeTable = ENCODE_TABLE;
    324            this.decodeTable = DECODE_TABLE;
    325        }
    326        if (lineLength > 0) {
    327            if (lineSeparator == null) {
    328                throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null");
    329            }
    330            // Must be done after initializing the tables
    331            if (containsAlphabetOrPad(lineSeparator)) {
    332                final String sep = StringUtils.newStringUtf8(lineSeparator);
    333                throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]");
    334            }
    335            this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
    336            this.lineSeparator = new byte[lineSeparator.length];
    337            System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
    338        } else {
    339            this.encodeSize = BYTES_PER_ENCODED_BLOCK;
    340            this.lineSeparator = null;
    341        }
    342        this.decodeSize = this.encodeSize - 1;
    343
    344        if (isInAlphabet(padding) || isWhiteSpace(padding)) {
    345            throw new IllegalArgumentException("pad must not be in alphabet or whitespace");
    346        }
    347    }
    348
    349    /**
    350     * <p>
    351     * Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once
    352     * with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1"
    353     * call is not necessary when decoding, but it doesn't hurt, either.
    354     * </p>
    355     * <p>
    356     * Ignores all non-Base32 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are
    357     * silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in,
    358     * garbage-out philosophy: it will not check the provided data for validity.
    359     * </p>
    360     * <p>
    361     * Output is written to {@link org.apache.commons.codec.binary.BaseNCodec.Context#buffer Context#buffer} as 8-bit
    362     * octets, using {@link org.apache.commons.codec.binary.BaseNCodec.Context#pos Context#pos} as the buffer position
    363     * </p>
    364     *
    365     * @param input byte[] array of ascii data to Base32 decode.
    366     * @param inPos Position to start reading data from.
    367     * @param inAvail Amount of bytes available from input for decoding.
    368     * @param context the context to be used
    369     */
    370    @Override
    371    void decode(final byte[] input, int inPos, final int inAvail, final Context context) {
    372        // package protected for access from I/O streams
    373
    374        if (context.eof) {
    375            return;
    376        }
    377        if (inAvail < 0) {
    378            context.eof = true;
    379        }
    380        for (int i = 0; i < inAvail; i++) {
    381            final byte b = input[inPos++];
    382            if (b == pad) {
    383                // We're done.
    384                context.eof = true;
    385                break;
    386            }
    387            final byte[] buffer = ensureBufferSize(decodeSize, context);
    388            if (b >= 0 && b < this.decodeTable.length) {
    389                final int result = this.decodeTable[b];
    390                if (result >= 0) {
    391                    context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK;
    392                    // collect decoded bytes
    393                    context.lbitWorkArea = (context.lbitWorkArea << BITS_PER_ENCODED_BYTE) + result;
    394                    if (context.modulus == 0) { // we can output the 5 bytes
    395                        buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 32) & MASK_8BITS);
    396                        buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 24) & MASK_8BITS);
    397                        buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
    398                        buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
    399                        buffer[context.pos++] = (byte) (context.lbitWorkArea & MASK_8BITS);
    400                    }
    401                }
    402            }
    403        }
    404
    405        // Two forms of EOF as far as Base32 decoder is concerned: actual
    406        // EOF (-1) and first time '=' character is encountered in stream.
    407        // This approach makes the '=' padding characters completely optional.
    408        if (context.eof && context.modulus > 0) { // if modulus == 0, nothing to do
    409            final byte[] buffer = ensureBufferSize(decodeSize, context);
    410
    411            // We ignore partial bytes, i.e. only multiples of 8 count.
    412            // Any combination not part of a valid encoding is either partially decoded
    413            // or will raise an exception. Possible trailing characters are 2, 4, 5, 7.
    414            // It is not possible to encode with 1, 3, 6 trailing characters.
    415            // For backwards compatibility 3 & 6 chars are decoded anyway rather than discarded.
    416            // See the encode(byte[]) method EOF section.
    417            switch (context.modulus) {
    418//              case 0 : // impossible, as excluded above
    419                case 1 : // 5 bits - either ignore entirely, or raise an exception
    420                    validateTrailingCharacters();
    421                case 2 : // 10 bits, drop 2 and output one byte
    422                    validateCharacter(MASK_2BITS, context);
    423                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 2) & MASK_8BITS);
    424                    break;
    425                case 3 : // 15 bits, drop 7 and output 1 byte, or raise an exception
    426                    validateTrailingCharacters();
    427                    // Not possible from a valid encoding but decode anyway
    428                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 7) & MASK_8BITS);
    429                    break;
    430                case 4 : // 20 bits = 2*8 + 4
    431                    validateCharacter(MASK_4BITS, context);
    432                    context.lbitWorkArea = context.lbitWorkArea >> 4; // drop 4 bits
    433                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
    434                    buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
    435                    break;
    436                case 5 : // 25 bits = 3*8 + 1
    437                    validateCharacter(MASK_1BITS, context);
    438                    context.lbitWorkArea = context.lbitWorkArea >> 1;
    439                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
    440                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
    441                    buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
    442                    break;
    443                case 6 : // 30 bits = 3*8 + 6, or raise an exception
    444                    validateTrailingCharacters();
    445                    // Not possible from a valid encoding but decode anyway
    446                    context.lbitWorkArea = context.lbitWorkArea >> 6;
    447                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
    448                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
    449                    buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
    450                    break;
    451                case 7 : // 35 bits = 4*8 +3
    452                    validateCharacter(MASK_3BITS, context);
    453                    context.lbitWorkArea = context.lbitWorkArea >> 3;
    454                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 24) & MASK_8BITS);
    455                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 16) & MASK_8BITS);
    456                    buffer[context.pos++] = (byte) ((context.lbitWorkArea >> 8) & MASK_8BITS);
    457                    buffer[context.pos++] = (byte) ((context.lbitWorkArea) & MASK_8BITS);
    458                    break;
    459                default:
    460                    // modulus can be 0-7, and we excluded 0,1 already
    461                    throw new IllegalStateException("Impossible modulus " + context.modulus);
    462            }
    463        }
    464    }
    465
    466    /**
    467     * <p>
    468     * Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with
    469     * the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last
    470     * remaining bytes (if not multiple of 5).
    471     * </p>
    472     *
    473     * @param input
    474     *            byte[] array of binary data to Base32 encode.
    475     * @param inPos
    476     *            Position to start reading data from.
    477     * @param inAvail
    478     *            Amount of bytes available from input for encoding.
    479     * @param context the context to be used
    480     */
    481    @Override
    482    void encode(final byte[] input, int inPos, final int inAvail, final Context context) {
    483        // package protected for access from I/O streams
    484
    485        if (context.eof) {
    486            return;
    487        }
    488        // inAvail < 0 is how we're informed of EOF in the underlying data we're
    489        // encoding.
    490        if (inAvail < 0) {
    491            context.eof = true;
    492            if (0 == context.modulus && lineLength == 0) {
    493                return; // no leftovers to process and not using chunking
    494            }
    495            final byte[] buffer = ensureBufferSize(encodeSize, context);
    496            final int savedPos = context.pos;
    497            switch (context.modulus) { // % 5
    498                case 0 :
    499                    break;
    500                case 1 : // Only 1 octet; take top 5 bits then remainder
    501                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 3) & MASK_5BITS]; // 8-1*5 = 3
    502                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 2) & MASK_5BITS]; // 5-3=2
    503                    buffer[context.pos++] = pad;
    504                    buffer[context.pos++] = pad;
    505                    buffer[context.pos++] = pad;
    506                    buffer[context.pos++] = pad;
    507                    buffer[context.pos++] = pad;
    508                    buffer[context.pos++] = pad;
    509                    break;
    510                case 2 : // 2 octets = 16 bits to use
    511                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 11) & MASK_5BITS]; // 16-1*5 = 11
    512                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >>  6) & MASK_5BITS]; // 16-2*5 = 6
    513                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >>  1) & MASK_5BITS]; // 16-3*5 = 1
    514                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea <<  4) & MASK_5BITS]; // 5-1 = 4
    515                    buffer[context.pos++] = pad;
    516                    buffer[context.pos++] = pad;
    517                    buffer[context.pos++] = pad;
    518                    buffer[context.pos++] = pad;
    519                    break;
    520                case 3 : // 3 octets = 24 bits to use
    521                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 19) & MASK_5BITS]; // 24-1*5 = 19
    522                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 14) & MASK_5BITS]; // 24-2*5 = 14
    523                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >>  9) & MASK_5BITS]; // 24-3*5 = 9
    524                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >>  4) & MASK_5BITS]; // 24-4*5 = 4
    525                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea <<  1) & MASK_5BITS]; // 5-4 = 1
    526                    buffer[context.pos++] = pad;
    527                    buffer[context.pos++] = pad;
    528                    buffer[context.pos++] = pad;
    529                    break;
    530                case 4 : // 4 octets = 32 bits to use
    531                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 27) & MASK_5BITS]; // 32-1*5 = 27
    532                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 22) & MASK_5BITS]; // 32-2*5 = 22
    533                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 17) & MASK_5BITS]; // 32-3*5 = 17
    534                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 12) & MASK_5BITS]; // 32-4*5 = 12
    535                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >>  7) & MASK_5BITS]; // 32-5*5 =  7
    536                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >>  2) & MASK_5BITS]; // 32-6*5 =  2
    537                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea <<  3) & MASK_5BITS]; // 5-2 = 3
    538                    buffer[context.pos++] = pad;
    539                    break;
    540                default:
    541                    throw new IllegalStateException("Impossible modulus " + context.modulus);
    542            }
    543            context.currentLinePos += context.pos - savedPos; // keep track of current line position
    544            // if currentPos == 0 we are at the start of a line, so don't add CRLF
    545            if (lineLength > 0 && context.currentLinePos > 0){ // add chunk separator if required
    546                System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
    547                context.pos += lineSeparator.length;
    548            }
    549        } else {
    550            for (int i = 0; i < inAvail; i++) {
    551                final byte[] buffer = ensureBufferSize(encodeSize, context);
    552                context.modulus = (context.modulus+1) % BYTES_PER_UNENCODED_BLOCK;
    553                int b = input[inPos++];
    554                if (b < 0) {
    555                    b += 256;
    556                }
    557                context.lbitWorkArea = (context.lbitWorkArea << 8) + b; // BITS_PER_BYTE
    558                if (0 == context.modulus) { // we have enough bytes to create our output
    559                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 35) & MASK_5BITS];
    560                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 30) & MASK_5BITS];
    561                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 25) & MASK_5BITS];
    562                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 20) & MASK_5BITS];
    563                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 15) & MASK_5BITS];
    564                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 10) & MASK_5BITS];
    565                    buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 5) & MASK_5BITS];
    566                    buffer[context.pos++] = encodeTable[(int)context.lbitWorkArea & MASK_5BITS];
    567                    context.currentLinePos += BYTES_PER_ENCODED_BLOCK;
    568                    if (lineLength > 0 && lineLength <= context.currentLinePos) {
    569                        System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
    570                        context.pos += lineSeparator.length;
    571                        context.currentLinePos = 0;
    572                    }
    573                }
    574            }
    575        }
    576    }
    577
    578    /**
    579     * Returns whether or not the {@code octet} is in the Base32 alphabet.
    580     *
    581     * @param octet
    582     *            The value to test
    583     * @return {@code true} if the value is defined in the the Base32 alphabet {@code false} otherwise.
    584     */
    585    @Override
    586    public boolean isInAlphabet(final byte octet) {
    587        return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
    588    }
    589
    590    /**
    591     * Validates whether decoding the final trailing character is possible in the context
    592     * of the set of possible base 32 values.
    593     *
    594     * <p>The character is valid if the lower bits within the provided mask are zero. This
    595     * is used to test the final trailing base-32 digit is zero in the bits that will be discarded.
    596     *
    597     * @param emptyBitsMask The mask of the lower bits that should be empty
    598     * @param context the context to be used
    599     *
    600     * @throws IllegalArgumentException if the bits being checked contain any non-zero value
    601     */
    602    private void validateCharacter(final long emptyBitsMask, final Context context) {
    603        // Use the long bit work area
    604        if (isStrictDecoding() && (context.lbitWorkArea & emptyBitsMask) != 0) {
    605            throw new IllegalArgumentException(
    606                "Strict decoding: Last encoded character (before the paddings if any) is a valid base 32 alphabet but not a possible encoding. " +
    607                "Expected the discarded bits from the character to be zero.");
    608        }
    609    }
    610
    611    /**
    612     * Validates whether decoding allows final trailing characters that cannot be
    613     * created during encoding.
    614     *
    615     * @throws IllegalArgumentException if strict decoding is enabled
    616     */
    617    private void validateTrailingCharacters() {
    618        if (isStrictDecoding()) {
    619            throw new IllegalArgumentException(
    620                "Strict decoding: Last encoded character(s) (before the paddings if any) are valid base 32 alphabet but not a possible encoding. " +
    621                "Decoding requires either 2, 4, 5, or 7 trailing 5-bit characters to create bytes.");
    622        }
    623    }
    624}