001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020
021package org.apache.directory.api.util;
022
023
024import org.apache.directory.api.i18n.I18n;
025
026
027/**
028 * Encoding and decoding of Base64 characters to and from raw bytes.
029 * 
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 */
032public final class Base64
033{
034
035    /**
036     * Private constructor.
037     */
038    private Base64()
039    {
040    }
041
042
043    /**
044     * Encodes binary data to a Base64 encoded characters.
045     * 
046     * @param data
047     *            the array of bytes to encode
048     * @return base64-coded character array.
049     */
050    public static char[] encode( byte[] data )
051    {
052        char[] out = new char[( ( data.length + 2 ) / 3 ) * 4];
053
054        //
055        // 3 bytes encode to 4 chars. Output is always an even
056        // multiple of 4 characters.
057        //
058        for ( int ii = 0, index = 0; ii < data.length; ii += 3, index += 4 )
059        {
060            boolean isQuadrupel = false;
061            boolean isTripel = false;
062
063            int val = ( 0xFF & data[ii] );
064            val <<= 8;
065            if ( ( ii + 1 ) < data.length )
066            {
067                val |= ( 0xFF & data[ii + 1] );
068                isTripel = true;
069            }
070
071            val <<= 8;
072            if ( ( ii + 2 ) < data.length )
073            {
074                val |= ( 0xFF & data[ii + 2] );
075                isQuadrupel = true;
076            }
077
078            out[index + 3] = ALPHABET[( isQuadrupel ? ( val & 0x3F ) : 64 )];
079            val >>= 6;
080            out[index + 2] = ALPHABET[( isTripel ? ( val & 0x3F ) : 64 )];
081            val >>= 6;
082            out[index + 1] = ALPHABET[val & 0x3F];
083            val >>= 6;
084            out[index + 0] = ALPHABET[val & 0x3F];
085        }
086        return out;
087    }
088
089
090    /**
091     * Decodes a BASE-64 encoded stream to recover the original data. White
092     * space before and after will be trimmed away, but no other manipulation of
093     * the input will be performed. As of version 1.2 this method will properly
094     * handle input containing junk characters (newlines and the like) rather
095     * than throwing an error. It does this by pre-parsing the input and
096     * generating from that a count of VALID input characters.
097     * 
098     * @param data
099     *            data to decode.
100     * @return the decoded binary data.
101     */
102    public static byte[] decode( char[] data )
103    {
104        // as our input could contain non-BASE64 data (newlines,
105        // whitespace of any sort, whatever) we must first adjust
106        // our count of USABLE data so that...
107        // (a) we don't misallocate the output array, and
108        // (b) think that we miscalculated our data length
109        // just because of extraneous throw-away junk
110
111        int tempLen = data.length;
112
113        for ( char c : data )
114        {
115            if ( ( c > 255 ) || CODES[c] < 0 )
116            {
117                --tempLen; // ignore non-valid chars and padding
118            }
119        }
120        // calculate required length:
121        // -- 3 bytes for every 4 valid base64 chars
122        // -- plus 2 bytes if there are 3 extra base64 chars,
123        // or plus 1 byte if there are 2 extra.
124
125        int len = ( tempLen / 4 ) * 3;
126
127        if ( ( tempLen % 4 ) == 3 )
128        {
129            len += 2;
130        }
131
132        if ( ( tempLen % 4 ) == 2 )
133        {
134            len += 1;
135        }
136
137        byte[] out = new byte[len];
138
139        int shift = 0; // # of excess bits stored in accum
140        int accum = 0; // excess bits
141        int index = 0;
142
143        // we now go through the entire array (NOT using the 'tempLen' value)
144        for ( char c : data )
145        {
146            int value = ( c > 255 ) ? -1 : CODES[c];
147
148            if ( value >= 0 ) // skip over non-code
149            {
150                accum <<= 6; // bits shift up by 6 each time thru
151                shift += 6; // loop, with new bits being put in
152                accum |= value; // at the bottom. whenever there
153                if ( shift >= 8 ) // are 8 or more shifted in, write them
154                {
155                    shift -= 8; // out (from the top, leaving any excess
156                    out[index++] = // at the bottom for next iteration.
157                    ( byte ) ( ( accum >> shift ) & 0xff );
158                }
159            }
160            // we will also have skipped processing a padding null byte ('=')
161            // here;
162            // these are used ONLY for padding to an even length and do not
163            // legally
164            // occur as encoded data. for this reason we can ignore the fact
165            // that
166            // no index++ operation occurs in that special case: the out[] array
167            // is
168            // initialized to all-zero bytes to start with and that works to our
169            // advantage in this combination.
170        }
171
172        // if there is STILL something wrong we just have to throw up now!
173        if ( index != out.length )
174        {
175            throw new Error( I18n.err( I18n.ERR_04348, index, out.length ) );
176        }
177
178        return out;
179    }
180
181    /** code characters for values 0..63 */
182    private static final char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
183        .toCharArray();
184
185    /** lookup table for converting base64 characters to value in range 0..63 */
186    private static final byte[] CODES = new byte[256];
187
188    static
189    {
190        for ( int ii = 0; ii < 256; ii++ )
191        {
192            CODES[ii] = -1;
193        }
194
195        for ( int ii = 'A'; ii <= 'Z'; ii++ )
196        {
197            CODES[ii] = ( byte ) ( ii - 'A' );
198        }
199
200        for ( int ii = 'a'; ii <= 'z'; ii++ )
201        {
202            CODES[ii] = ( byte ) ( 26 + ii - 'a' );
203        }
204
205        for ( int ii = '0'; ii <= '9'; ii++ )
206        {
207            CODES[ii] = ( byte ) ( 52 + ii - '0' );
208        }
209
210        CODES['+'] = 62;
211        CODES['/'] = 63;
212    }
213}