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.net.util;
019
020import java.io.UnsupportedEncodingException;
021import java.math.BigInteger;
022
023
024
025/**
026 * Provides Base64 encoding and decoding as defined by RFC 2045.
027 *
028 * <p>
029 * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose
030 * Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein.
031 * </p>
032 * <p>
033 * The class can be parameterized in the following manner with various constructors:
034 * <ul>
035 * <li>URL-safe mode: Default off.</li>
036 * <li>Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of
037 * 4 in the encoded data.
038 * <li>Line separator: Default is CRLF ("\r\n")</li>
039 * </ul>
040 * <p>
041 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode
042 * character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
043 * </p>
044 *
045 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
046 * @since 2.2
047 */
048public class Base64 {
049    private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2;
050
051    private static final int DEFAULT_BUFFER_SIZE = 8192;
052
053    /**
054     * Chunk size per RFC 2045 section 6.8.
055     *
056     * <p>
057     * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
058     * equal signs.
059     * </p>
060     *
061     * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>
062     */
063    static final int CHUNK_SIZE = 76;
064
065    /**
066     * Chunk separator per RFC 2045 section 2.1.
067     *
068     * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
069     */
070    private static final byte[] CHUNK_SEPARATOR = {'\r', '\n'};
071
072    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
073
074    /**
075     * This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet"
076     * equivalents as specified in Table 1 of RFC 2045.
077     *
078     * Thanks to "commons" project in ws.apache.org for this code.
079     * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
080     */
081    private static final byte[] STANDARD_ENCODE_TABLE = {
082            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
083            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
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            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
087    };
088
089    /**
090     * This is a copy of the STANDARD_ENCODE_TABLE above, but with + and /
091     * changed to - and _ to make the encoded Base64 results more URL-SAFE.
092     * This table is only used when the Base64's mode is set to URL-SAFE.
093     */
094    private static final byte[] URL_SAFE_ENCODE_TABLE = {
095            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
096            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
097            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
098            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
099            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
100    };
101
102    /**
103     * Byte used to pad output.
104     */
105    private static final byte PAD = '=';
106
107    /**
108     * This array is a lookup table that translates Unicode characters drawn from the "Base64 Alphabet" (as specified in
109     * Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in the Base64
110     * alphabet but fall within the bounds of the array are translated to -1.
111     *
112     * Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder seamlessly handles both
113     * URL_SAFE and STANDARD base64. (The encoder, on the other hand, needs to know ahead of time what to emit).
114     *
115     * Thanks to "commons" project in ws.apache.org for this code.
116     * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
117     */
118    private static final byte[] DECODE_TABLE = {
119            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
121            -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54,
122            55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
123            5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
124            24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
125            35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
126    };
127
128    /** Mask used to extract 6 bits, used when encoding */
129    private static final int MASK_6BITS = 0x3f;
130
131    /** Mask used to extract 8 bits, used in decoding base64 bytes */
132    private static final int MASK_8BITS = 0xff;
133
134    // The static final fields above are used for the original static byte[] methods on Base64.
135    // The private member fields below are used with the new streaming approach, which requires
136    // some state be preserved between calls of encode() and decode().
137
138    /**
139     * Encode table to use: either STANDARD or URL_SAFE. Note: the DECODE_TABLE above remains static because it is able
140     * to decode both STANDARD and URL_SAFE streams, but the encodeTable must be a member variable so we can switch
141     * between the two modes.
142     */
143    private final byte[] encodeTable;
144
145    /**
146     * Line length for encoding. Not used when decoding. A value of zero or less implies no chunking of the base64
147     * encoded data.
148     */
149    private final int lineLength;
150
151    /**
152     * Line separator for encoding. Not used when decoding. Only used if lineLength > 0.
153     */
154    private final byte[] lineSeparator;
155
156    /**
157     * Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
158     * <code>decodeSize = 3 + lineSeparator.length;</code>
159     */
160    private final int decodeSize;
161
162    /**
163     * Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
164     * <code>encodeSize = 4 + lineSeparator.length;</code>
165     */
166    private final int encodeSize;
167
168    /**
169     * Buffer for streaming.
170     */
171    private byte[] buffer;
172
173    /**
174     * Position where next character should be written in the buffer.
175     */
176    private int pos;
177
178    /**
179     * Position where next character should be read from the buffer.
180     */
181    private int readPos;
182
183    /**
184     * Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to
185     * make sure each encoded line never goes beyond lineLength (if lineLength > 0).
186     */
187    private int currentLinePos;
188
189    /**
190     * Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when decoding. This variable
191     * helps track that.
192     */
193    private int modulus;
194
195    /**
196     * Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this Base64 object becomes useless,
197     * and must be thrown away.
198     */
199    private boolean eof;
200
201    /**
202     * Place holder for the 3 bytes we're dealing with for our base64 logic. Bitwise operations store and extract the
203     * base64 encoding or decoding from this variable.
204     */
205    private int x;
206
207    /**
208     * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
209     * <p>
210     * When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.
211     * </p>
212     *
213     * <p>
214     * When decoding all variants are supported.
215     * </p>
216     */
217    public Base64() {
218        this(false);
219    }
220
221    /**
222     * Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.
223     * <p>
224     * When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.
225     * </p>
226     *
227     * <p>
228     * When decoding all variants are supported.
229     * </p>
230     *
231     * @param urlSafe
232     *            if <code>true</code>, URL-safe encoding is used. In most cases this should be set to
233     *            <code>false</code>.
234     * @since 1.4
235     */
236    public Base64(final boolean urlSafe) {
237        this(CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
238    }
239
240    /**
241     * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
242     * <p>
243     * When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is
244     * STANDARD_ENCODE_TABLE.
245     * </p>
246     * <p>
247     * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
248     * </p>
249     * <p>
250     * When decoding all variants are supported.
251     * </p>
252     *
253     * @param lineLength
254     *            Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4).
255     *            If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding.
256     * @since 1.4
257     */
258    public Base64(final int lineLength) {
259        this(lineLength, CHUNK_SEPARATOR);
260    }
261
262    /**
263     * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
264     * <p>
265     * When encoding the line length and line separator are given in the constructor, and the encoding table is
266     * STANDARD_ENCODE_TABLE.
267     * </p>
268     * <p>
269     * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
270     * </p>
271     * <p>
272     * When decoding all variants are supported.
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 4).
277     *            If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding.
278     * @param lineSeparator
279     *            Each line of encoded data will end with this sequence of bytes.
280     * @throws IllegalArgumentException
281     *             Thrown when the provided lineSeparator included some base64 characters.
282     * @since 1.4
283     */
284    public Base64(final int lineLength, final byte[] lineSeparator) {
285        this(lineLength, lineSeparator, false);
286    }
287
288    /**
289     * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
290     * <p>
291     * When encoding the line length and line separator are given in the constructor, and the encoding table is
292     * STANDARD_ENCODE_TABLE.
293     * </p>
294     * <p>
295     * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
296     * </p>
297     * <p>
298     * When decoding all variants are supported.
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 4).
303     *            If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding.
304     * @param lineSeparator
305     *            Each line of encoded data will end with this sequence of bytes.
306     * @param urlSafe
307     *            Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode
308     *            operations. Decoding seamlessly handles both modes.
309     * @throws IllegalArgumentException
310     *             The provided lineSeparator included some base64 characters. That's not going to work!
311     * @since 1.4
312     */
313    public Base64(int lineLength, byte[] lineSeparator, final boolean urlSafe) {
314        if (lineSeparator == null) {
315            lineLength = 0;  // disable chunk-separating
316            lineSeparator = EMPTY_BYTE_ARRAY;  // this just gets ignored
317        }
318        this.lineLength = lineLength > 0 ? (lineLength / 4) * 4 : 0;
319        this.lineSeparator = new byte[lineSeparator.length];
320        System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
321        if (lineLength > 0) {
322            this.encodeSize = 4 + lineSeparator.length;
323        } else {
324            this.encodeSize = 4;
325        }
326        this.decodeSize = this.encodeSize - 1;
327        if (containsBase64Byte(lineSeparator)) {
328            final String sep = newStringUtf8(lineSeparator);
329            throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]");
330        }
331        this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
332    }
333
334    /**
335     * Returns our current encode mode. True if we're URL-SAFE, false otherwise.
336     *
337     * @return true if we're in URL-SAFE mode, false otherwise.
338     * @since 1.4
339     */
340    public boolean isUrlSafe() {
341        return this.encodeTable == URL_SAFE_ENCODE_TABLE;
342    }
343
344    /**
345     * Returns true if this Base64 object has buffered data for reading.
346     *
347     * @return true if there is Base64 object still available for reading.
348     */
349    boolean hasData() {
350        return this.buffer != null;
351    }
352
353    /**
354     * Returns the amount of buffered data available for reading.
355     *
356     * @return The amount of buffered data available for reading.
357     */
358    int avail() {
359        return buffer != null ? pos - readPos : 0;
360    }
361
362    /** Doubles our buffer. */
363    private void resizeBuffer() {
364        if (buffer == null) {
365            buffer = new byte[DEFAULT_BUFFER_SIZE];
366            pos = 0;
367            readPos = 0;
368        } else {
369            final byte[] b = new byte[buffer.length * DEFAULT_BUFFER_RESIZE_FACTOR];
370            System.arraycopy(buffer, 0, b, 0, buffer.length);
371            buffer = b;
372        }
373    }
374
375    /**
376     * Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail
377     * bytes. Returns how many bytes were actually extracted.
378     *
379     * @param b
380     *            byte[] array to extract the buffered data into.
381     * @param bPos
382     *            position in byte[] array to start extraction at.
383     * @param bAvail
384     *            amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).
385     * @return The number of bytes successfully extracted into the provided byte[] array.
386     */
387    int readResults(final byte[] b, final int bPos, final int bAvail) {
388        if (buffer != null) {
389            final int len = Math.min(avail(), bAvail);
390            if (buffer != b) {
391                System.arraycopy(buffer, readPos, b, bPos, len);
392                readPos += len;
393                if (readPos >= pos) {
394                    buffer = null;
395                }
396            } else {
397                // Re-using the original consumer's output array is only
398                // allowed for one round.
399                buffer = null;
400            }
401            return len;
402        }
403        return eof ? -1 : 0;
404    }
405
406    /**
407     * Sets the streaming buffer. This is a small optimization where we try to buffer directly to the consumer's output
408     * array for one round (if the consumer calls this method first) instead of starting our own buffer.
409     *
410     * @param out
411     *            byte[] array to buffer directly to.
412     * @param outPos
413     *            Position to start buffering into.
414     * @param outAvail
415     *            Amount of bytes available for direct buffering.
416     */
417    void setInitialBuffer(final byte[] out, final int outPos, final int outAvail) {
418        // We can re-use consumer's original output array under
419        // special circumstances, saving on some System.arraycopy().
420        if (out != null && out.length == outAvail) {
421            buffer = out;
422            pos = outPos;
423            readPos = outPos;
424        }
425    }
426
427    /**
428     * <p>
429     * Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with
430     * the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last
431     * remaining bytes (if not multiple of 3).
432     * </p>
433     * <p>
434     * Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach.
435     * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
436     * </p>
437     *
438     * @param in
439     *            byte[] array of binary data to base64 encode.
440     * @param inPos
441     *            Position to start reading data from.
442     * @param inAvail
443     *            Amount of bytes available from input for encoding.
444     */
445    void encode(final byte[] in, int inPos, final int inAvail) {
446        if (eof) {
447            return;
448        }
449        // inAvail < 0 is how we're informed of EOF in the underlying data we're
450        // encoding.
451        if (inAvail < 0) {
452            eof = true;
453            if (buffer == null || buffer.length - pos < encodeSize) {
454                resizeBuffer();
455            }
456            switch (modulus) {
457                case 1 :
458                    buffer[pos++] = encodeTable[(x >> 2) & MASK_6BITS];
459                    buffer[pos++] = encodeTable[(x << 4) & MASK_6BITS];
460                    // URL-SAFE skips the padding to further reduce size.
461                    if (encodeTable == STANDARD_ENCODE_TABLE) {
462                        buffer[pos++] = PAD;
463                        buffer[pos++] = PAD;
464                    }
465                    break;
466
467                case 2 :
468                    buffer[pos++] = encodeTable[(x >> 10) & MASK_6BITS];
469                    buffer[pos++] = encodeTable[(x >> 4) & MASK_6BITS];
470                    buffer[pos++] = encodeTable[(x << 2) & MASK_6BITS];
471                    // URL-SAFE skips the padding to further reduce size.
472                    if (encodeTable == STANDARD_ENCODE_TABLE) {
473                        buffer[pos++] = PAD;
474                    }
475                    break;
476                default:
477                    break;  // other values ignored
478            }
479            if (lineLength > 0 && pos > 0) {
480                System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
481                pos += lineSeparator.length;
482            }
483        } else {
484            for (int i = 0; i < inAvail; i++) {
485                if (buffer == null || buffer.length - pos < encodeSize) {
486                    resizeBuffer();
487                }
488                modulus = (++modulus) % 3;
489                int b = in[inPos++];
490                if (b < 0) {
491                    b += 256;
492                }
493                x = (x << 8) + b;
494                if (0 == modulus) {
495                    buffer[pos++] = encodeTable[(x >> 18) & MASK_6BITS];
496                    buffer[pos++] = encodeTable[(x >> 12) & MASK_6BITS];
497                    buffer[pos++] = encodeTable[(x >> 6) & MASK_6BITS];
498                    buffer[pos++] = encodeTable[x & MASK_6BITS];
499                    currentLinePos += 4;
500                    if (lineLength > 0 && lineLength <= currentLinePos) {
501                        System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
502                        pos += lineSeparator.length;
503                        currentLinePos = 0;
504                    }
505                }
506            }
507        }
508    }
509
510    /**
511     * <p>
512     * Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once
513     * with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1"
514     * call is not necessary when decoding, but it doesn't hurt, either.
515     * </p>
516     * <p>
517     * Ignores all non-base64 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are
518     * silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in,
519     * garbage-out philosophy: it will not check the provided data for validity.
520     * </p>
521     * <p>
522     * Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach.
523     * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
524     * </p>
525     *
526     * @param in
527     *            byte[] array of ascii data to base64 decode.
528     * @param inPos
529     *            Position to start reading data from.
530     * @param inAvail
531     *            Amount of bytes available from input for encoding.
532     */
533    void decode(final byte[] in, int inPos, final int inAvail) {
534        if (eof) {
535            return;
536        }
537        if (inAvail < 0) {
538            eof = true;
539        }
540        for (int i = 0; i < inAvail; i++) {
541            if (buffer == null || buffer.length - pos < decodeSize) {
542                resizeBuffer();
543            }
544            final byte b = in[inPos++];
545            if (b == PAD) {
546                // We're done.
547                eof = true;
548                break;
549            }
550            if (b >= 0 && b < DECODE_TABLE.length) {
551                final int result = DECODE_TABLE[b];
552                if (result >= 0) {
553                    modulus = (++modulus) % 4;
554                    x = (x << 6) + result;
555                    if (modulus == 0) {
556                        buffer[pos++] = (byte) ((x >> 16) & MASK_8BITS);
557                        buffer[pos++] = (byte) ((x >> 8) & MASK_8BITS);
558                        buffer[pos++] = (byte) (x & MASK_8BITS);
559                    }
560                }
561            }
562        }
563
564        // Two forms of EOF as far as base64 decoder is concerned: actual
565        // EOF (-1) and first time '=' character is encountered in stream.
566        // This approach makes the '=' padding characters completely optional.
567        if (eof && modulus != 0) {
568            x = x << 6;
569            switch (modulus) {
570                case 2 :
571                    x = x << 6;
572                    buffer[pos++] = (byte) ((x >> 16) & MASK_8BITS);
573                    break;
574                case 3 :
575                    buffer[pos++] = (byte) ((x >> 16) & MASK_8BITS);
576                    buffer[pos++] = (byte) ((x >> 8) & MASK_8BITS);
577                    break;
578                default:
579                    break;  // other values ignored
580            }
581        }
582    }
583
584    /**
585     * Returns whether or not the <code>octet</code> is in the base 64 alphabet.
586     *
587     * @param octet
588     *            The value to test
589     * @return <code>true</code> if the value is defined in the the base 64 alphabet, <code>false</code> otherwise.
590     * @since 1.4
591     */
592    public static boolean isBase64(final byte octet) {
593        return octet == PAD || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1);
594    }
595
596    /**
597     * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the
598     * method treats whitespace as valid.
599     *
600     * @param arrayOctet
601     *            byte array to test
602     * @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;
603     *         false, otherwise
604     */
605    public static boolean isArrayByteBase64(final byte[] arrayOctet) {
606        for (final byte element : arrayOctet) {
607            if (!isBase64(element) && !isWhiteSpace(element)) {
608                return false;
609            }
610        }
611        return true;
612    }
613
614    /**
615     * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
616     *
617     * @param arrayOctet
618     *            byte array to test
619     * @return <code>true</code> if any byte is a valid character in the Base64 alphabet; false herwise
620     */
621    private static boolean containsBase64Byte(final byte[] arrayOctet) {
622        for (final byte element : arrayOctet)
623        {
624            if (isBase64(element)) {
625                return true;
626            }
627        }
628        return false;
629    }
630
631    /**
632     * Encodes binary data using the base64 algorithm but does not chunk the output.
633     *
634     * @param binaryData
635     *            binary data to encode
636     * @return byte[] containing Base64 characters in their UTF-8 representation.
637     */
638    public static byte[] encodeBase64(final byte[] binaryData) {
639        return encodeBase64(binaryData, false);
640    }
641
642    /**
643     * Encodes binary data using the base64 algorithm into 76 character blocks separated by CRLF.
644     * <p>
645     * For a non-chunking version, see {@link #encodeBase64StringUnChunked(byte[])}.
646     *
647     * @param binaryData
648     *            binary data to encode
649     * @return String containing Base64 characters.
650     * @since 1.4
651     */
652    public static String encodeBase64String(final byte[] binaryData) {
653        return newStringUtf8(encodeBase64(binaryData, true));
654    }
655
656    /**
657     * Encodes binary data using the base64 algorithm, without using chunking.
658     * <p>
659     * For a chunking version, see {@link #encodeBase64String(byte[])}.
660     *
661     * @param binaryData
662     *            binary data to encode
663     * @return String containing Base64 characters.
664     * @since 3.2
665     */
666    public static String encodeBase64StringUnChunked(final byte[] binaryData) {
667        return newStringUtf8(encodeBase64(binaryData, false));
668    }
669
670    /**
671     * Encodes binary data using the base64 algorithm.
672     *
673     * @param binaryData
674     *            binary data to encode
675     * @param useChunking whether to split the output into chunks
676     * @return String containing Base64 characters.
677     * @since 3.2
678     */
679    public static String encodeBase64String(final byte[] binaryData, final boolean useChunking) {
680        return newStringUtf8(encodeBase64(binaryData, useChunking));
681    }
682
683    /**
684     * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The
685     * url-safe variation emits - and _ instead of + and / characters.
686     *
687     * @param binaryData
688     *            binary data to encode
689     * @return byte[] containing Base64 characters in their UTF-8 representation.
690     * @since 1.4
691     */
692    public static byte[] encodeBase64URLSafe(final byte[] binaryData) {
693        return encodeBase64(binaryData, false, true);
694    }
695
696    /**
697     * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The
698     * url-safe variation emits - and _ instead of + and / characters.
699     *
700     * @param binaryData
701     *            binary data to encode
702     * @return String containing Base64 characters
703     * @since 1.4
704     */
705    public static String encodeBase64URLSafeString(final byte[] binaryData) {
706        return newStringUtf8(encodeBase64(binaryData, false, true));
707    }
708
709    /**
710     * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
711     *
712     * @param binaryData
713     *            binary data to encode
714     * @return Base64 characters chunked in 76 character blocks
715     */
716    public static byte[] encodeBase64Chunked(final byte[] binaryData) {
717        return encodeBase64(binaryData, true);
718    }
719
720    /**
721     * Decodes a String containing containing characters in the Base64 alphabet.
722     *
723     * @param pArray
724     *            A String containing Base64 character data
725     * @return a byte array containing binary data
726     * @since 1.4
727     */
728    public byte[] decode(final String pArray) {
729        return decode(getBytesUtf8(pArray));
730    }
731
732    private byte[] getBytesUtf8(final String pArray) {
733        try {
734            return pArray.getBytes("UTF8");
735        } catch (final UnsupportedEncodingException e) {
736            throw new RuntimeException(e);
737        }
738    }
739
740    /**
741     * Decodes a byte[] containing containing characters in the Base64 alphabet.
742     *
743     * @param pArray
744     *            A byte array containing Base64 character data
745     * @return a byte array containing binary data
746     */
747    public byte[] decode(final byte[] pArray) {
748        reset();
749        if (pArray == null || pArray.length == 0) {
750            return pArray;
751        }
752        final long len = (pArray.length * 3) / 4;
753        final byte[] buf = new byte[(int) len];
754        setInitialBuffer(buf, 0, buf.length);
755        decode(pArray, 0, pArray.length);
756        decode(pArray, 0, -1); // Notify decoder of EOF.
757
758        // Would be nice to just return buf (like we sometimes do in the encode
759        // logic), but we have no idea what the line-length was (could even be
760        // variable).  So we cannot determine ahead of time exactly how big an
761        // array is necessary.  Hence the need to construct a 2nd byte array to
762        // hold the final result:
763
764        final byte[] result = new byte[pos];
765        readResults(result, 0, result.length);
766        return result;
767    }
768
769    /**
770     * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
771     *
772     * @param binaryData
773     *            Array containing binary data to encode.
774     * @param isChunked
775     *            if <code>true</code> this encoder will chunk the base64 output into 76 character blocks
776     * @return Base64-encoded data.
777     * @throws IllegalArgumentException
778     *             Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE}
779     */
780    public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked) {
781        return encodeBase64(binaryData, isChunked, false);
782    }
783
784    /**
785     * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
786     *
787     * @param binaryData
788     *            Array containing binary data to encode.
789     * @param isChunked
790     *            if <code>true</code> this encoder will chunk the base64 output into 76 character blocks
791     * @param urlSafe
792     *            if <code>true</code> this encoder will emit - and _ instead of the usual + and / characters.
793     * @return Base64-encoded data.
794     * @throws IllegalArgumentException
795     *             Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE}
796     * @since 1.4
797     */
798    public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe) {
799        return encodeBase64(binaryData, isChunked, urlSafe, Integer.MAX_VALUE);
800    }
801
802    /**
803     * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
804     *
805     * @param binaryData
806     *            Array containing binary data to encode.
807     * @param isChunked
808     *            if <code>true</code> this encoder will chunk the base64 output into 76 character blocks
809     * @param urlSafe
810     *            if <code>true</code> this encoder will emit - and _ instead of the usual + and / characters.
811     * @param maxResultSize
812     *            The maximum result size to accept.
813     * @return Base64-encoded data.
814     * @throws IllegalArgumentException
815     *             Thrown when the input array needs an output array bigger than maxResultSize
816     * @since 1.4
817     */
818    public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe,
819            final int maxResultSize) {
820        if (binaryData == null || binaryData.length == 0) {
821            return binaryData;
822        }
823
824        final long len = getEncodeLength(binaryData, isChunked ? CHUNK_SIZE : 0,
825                isChunked ? CHUNK_SEPARATOR : EMPTY_BYTE_ARRAY);
826        if (len > maxResultSize) {
827            throw new IllegalArgumentException("Input array too big, the output array would be bigger (" + len
828                    + ") than the specified maxium size of " + maxResultSize);
829        }
830
831        final Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe);
832        return b64.encode(binaryData);
833    }
834
835    /**
836     * Decodes a Base64 String into octets
837     *
838     * @param base64String
839     *            String containing Base64 data
840     * @return Array containing decoded data.
841     * @since 1.4
842     */
843    public static byte[] decodeBase64(final String base64String) {
844        return new Base64().decode(base64String);
845    }
846
847    /**
848     * Decodes Base64 data into octets
849     *
850     * @param base64Data
851     *            Byte array containing Base64 data
852     * @return Array containing decoded data.
853     */
854    public static byte[] decodeBase64(final byte[] base64Data) {
855        return new Base64().decode(base64Data);
856    }
857
858
859
860    /**
861     * Checks if a byte value is whitespace or not.
862     *
863     * @param byteToCheck
864     *            the byte to check
865     * @return true if byte is whitespace, false otherwise
866     */
867    private static boolean isWhiteSpace(final byte byteToCheck) {
868        switch (byteToCheck) {
869            case ' ' :
870            case '\n' :
871            case '\r' :
872            case '\t' :
873                return true;
874            default :
875                return false;
876        }
877    }
878
879    /**
880     * Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet.
881     *
882     * @param pArray
883     *            a byte array containing binary data
884     * @return A String containing only Base64 character data
885     * @since 1.4
886     */
887    public String encodeToString(final byte[] pArray) {
888        return newStringUtf8(encode(pArray));
889    }
890
891    private static String newStringUtf8(final byte[] encode) {
892        String str = null;
893        try {
894            str = new String(encode, "UTF8");
895        } catch (final UnsupportedEncodingException ue) {
896            throw new RuntimeException(ue);
897        }
898        return str;
899    }
900
901    /**
902     * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
903     *
904     * @param pArray
905     *            a byte array containing binary data
906     * @return A byte array containing only Base64 character data
907     */
908    public byte[] encode(final byte[] pArray) {
909        reset();
910        if (pArray == null || pArray.length == 0) {
911            return pArray;
912        }
913        final long len = getEncodeLength(pArray, lineLength, lineSeparator);
914        byte[] buf = new byte[(int) len];
915        setInitialBuffer(buf, 0, buf.length);
916        encode(pArray, 0, pArray.length);
917        encode(pArray, 0, -1); // Notify encoder of EOF.
918        // Encoder might have resized, even though it was unnecessary.
919        if (buffer != buf) {
920            readResults(buf, 0, buf.length);
921        }
922        // In URL-SAFE mode we skip the padding characters, so sometimes our
923        // final length is a bit smaller.
924        if (isUrlSafe() && pos < buf.length) {
925            final byte[] smallerBuf = new byte[pos];
926            System.arraycopy(buf, 0, smallerBuf, 0, pos);
927            buf = smallerBuf;
928        }
929        return buf;
930    }
931
932    /**
933     * Pre-calculates the amount of space needed to base64-encode the supplied array.
934     *
935     * @param pArray byte[] array which will later be encoded
936     * @param chunkSize line-length of the output (<= 0 means no chunking) between each
937     *        chunkSeparator (e.g. CRLF).
938     * @param chunkSeparator the sequence of bytes used to separate chunks of output (e.g. CRLF).
939     *
940     * @return amount of space needed to encoded the supplied array.  Returns
941     *         a long since a max-len array will require Integer.MAX_VALUE + 33%.
942     */
943    private static long getEncodeLength(final byte[] pArray, int chunkSize, final byte[] chunkSeparator) {
944        // base64 always encodes to multiples of 4.
945        chunkSize = (chunkSize / 4) * 4;
946
947        long len = (pArray.length * 4) / 3;
948        final long mod = len % 4;
949        if (mod != 0) {
950            len += 4 - mod;
951        }
952        if (chunkSize > 0) {
953            final boolean lenChunksPerfectly = len % chunkSize == 0;
954            len += (len / chunkSize) * chunkSeparator.length;
955            if (!lenChunksPerfectly) {
956                len += chunkSeparator.length;
957            }
958        }
959        return len;
960    }
961
962    // Implementation of integer encoding used for crypto
963    /**
964     * Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
965     *
966     * @param pArray
967     *            a byte array containing base64 character data
968     * @return A BigInteger
969     * @since 1.4
970     */
971    public static BigInteger decodeInteger(final byte[] pArray) {
972        return new BigInteger(1, decodeBase64(pArray));
973    }
974
975    /**
976     * Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
977     *
978     * @param bigInt
979     *            a BigInteger
980     * @return A byte array containing base64 character data
981     * @throws NullPointerException
982     *             if null is passed in
983     * @since 1.4
984     */
985    public static byte[] encodeInteger(final BigInteger bigInt) {
986        if (bigInt == null) {
987            throw new NullPointerException("encodeInteger called with null parameter");
988        }
989        return encodeBase64(toIntegerBytes(bigInt), false);
990    }
991
992    /**
993     * Returns a byte-array representation of a <code>BigInteger</code> without sign bit.
994     *
995     * @param bigInt
996     *            <code>BigInteger</code> to be converted
997     * @return a byte array representation of the BigInteger parameter
998     */
999    static byte[] toIntegerBytes(final BigInteger bigInt) {
1000        int bitlen = bigInt.bitLength();
1001        // round bitlen
1002        bitlen = ((bitlen + 7) >> 3) << 3;
1003        final byte[] bigBytes = bigInt.toByteArray();
1004
1005        if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
1006            return bigBytes;
1007        }
1008        // set up params for copying everything but sign bit
1009        int startSrc = 0;
1010        int len = bigBytes.length;
1011
1012        // if bigInt is exactly byte-aligned, just skip signbit in copy
1013        if ((bigInt.bitLength() % 8) == 0) {
1014            startSrc = 1;
1015            len--;
1016        }
1017        final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
1018        final byte[] resizedBytes = new byte[bitlen / 8];
1019        System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
1020        return resizedBytes;
1021    }
1022
1023    /**
1024     * Resets this Base64 object to its initial newly constructed state.
1025     */
1026    private void reset() {
1027        buffer = null;
1028        pos = 0;
1029        readPos = 0;
1030        currentLinePos = 0;
1031        modulus = 0;
1032        eof = false;
1033    }
1034
1035    // Getters for use in testing
1036
1037    int getLineLength() {
1038        return lineLength;
1039    }
1040
1041    byte[] getLineSeparator() {
1042        return lineSeparator.clone();
1043    }
1044}