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                // ignore non-valid chars and padding
118                --tempLen;
119            }
120        }
121        // calculate required length:
122        // -- 3 bytes for every 4 valid base64 chars
123        // -- plus 2 bytes if there are 3 extra base64 chars,
124        // or plus 1 byte if there are 2 extra.
125
126        int len = ( tempLen / 4 ) * 3;
127
128        if ( ( tempLen % 4 ) == 3 )
129        {
130            len += 2;
131        }
132
133        if ( ( tempLen % 4 ) == 2 )
134        {
135            len += 1;
136        }
137
138        byte[] out = new byte[len];
139
140        // # of excess bits stored in accum excess bits
141        int shift = 0;
142        int accum = 0;
143        int index = 0;
144
145        // we now go through the entire array (NOT using the 'tempLen' value)
146        for ( char c : data )
147        {
148            int value = ( c > 255 ) ? -1 : CODES[c];
149
150            // skip over non-code bits 
151            if ( value >= 0 )
152            {
153                // shift up by 6 each time thru
154                // loop, with new bits being put in
155                // at the bottom. whenever there
156                // are 8 or more shifted in, write them
157                // out (from the top, leaving any excess
158                // at the bottom for next iteration.
159                accum <<= 6;
160                shift += 6;
161                accum |= value;
162
163                if ( shift >= 8 )
164                {
165                    shift -= 8;
166                    out[index++] = ( byte ) ( ( accum >> shift ) & 0xff );
167                }
168            }
169            // we will also have skipped processing a padding null byte ('=') here;
170            // these are used ONLY for padding to an even length and do not legally
171            // occur as encoded data. for this reason we can ignore the fact
172            // that no index++ operation occurs in that special case: the out[] array
173            // is initialized to all-zero bytes to start with and that works to our
174            // advantage in this combination.
175        }
176
177        // if there is STILL something wrong we just have to throw up now!
178        if ( index != out.length )
179        {
180            throw new Error( I18n.err( I18n.ERR_04348, index, out.length ) );
181        }
182
183        return out;
184    }
185
186    /** code characters for values 0..63 */
187    private static final char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
188        .toCharArray();
189
190    /** lookup table for converting base64 characters to value in range 0..63 */
191    private static final byte[] CODES = new byte[256];
192
193    static
194    {
195        for ( int ii = 0; ii < 256; ii++ )
196        {
197            CODES[ii] = -1;
198        }
199
200        for ( int ii = 'A'; ii <= 'Z'; ii++ )
201        {
202            CODES[ii] = ( byte ) ( ii - 'A' );
203        }
204
205        for ( int ii = 'a'; ii <= 'z'; ii++ )
206        {
207            CODES[ii] = ( byte ) ( 26 + ii - 'a' );
208        }
209
210        for ( int ii = '0'; ii <= '9'; ii++ )
211        {
212            CODES[ii] = ( byte ) ( 52 + ii - '0' );
213        }
214
215        CODES['+'] = 62;
216        CODES['/'] = 63;
217    }
218}