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