View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.api.util;
22  
23  
24  import org.apache.directory.api.i18n.I18n;
25  
26  
27  /**
28   * Encoding and decoding of Base64 characters to and from raw bytes.
29   * 
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   */
32  public final class Base64
33  {
34  
35      /**
36       * Private constructor.
37       */
38      private Base64()
39      {
40      }
41  
42  
43      /**
44       * Encodes binary data to a Base64 encoded characters.
45       * 
46       * @param data
47       *            the array of bytes to encode
48       * @return base64-coded character array.
49       */
50      public static char[] encode( byte[] data )
51      {
52          char[] out = new char[( ( data.length + 2 ) / 3 ) * 4];
53  
54          //
55          // 3 bytes encode to 4 chars. Output is always an even
56          // multiple of 4 characters.
57          //
58          for ( int ii = 0, index = 0; ii < data.length; ii += 3, index += 4 )
59          {
60              boolean isQuadrupel = false;
61              boolean isTripel = false;
62  
63              int val = ( 0xFF & data[ii] );
64              val <<= 8;
65              if ( ( ii + 1 ) < data.length )
66              {
67                  val |= ( 0xFF & data[ii + 1] );
68                  isTripel = true;
69              }
70  
71              val <<= 8;
72              if ( ( ii + 2 ) < data.length )
73              {
74                  val |= ( 0xFF & data[ii + 2] );
75                  isQuadrupel = true;
76              }
77  
78              out[index + 3] = ALPHABET[( isQuadrupel ? ( val & 0x3F ) : 64 )];
79              val >>= 6;
80              out[index + 2] = ALPHABET[( isTripel ? ( val & 0x3F ) : 64 )];
81              val >>= 6;
82              out[index + 1] = ALPHABET[val & 0x3F];
83              val >>= 6;
84              out[index + 0] = ALPHABET[val & 0x3F];
85          }
86          return out;
87      }
88  
89  
90      /**
91       * Decodes a BASE-64 encoded stream to recover the original data. White
92       * space before and after will be trimmed away, but no other manipulation of
93       * the input will be performed. As of version 1.2 this method will properly
94       * handle input containing junk characters (newlines and the like) rather
95       * than throwing an error. It does this by pre-parsing the input and
96       * generating from that a count of VALID input characters.
97       * 
98       * @param data
99       *            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 }