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