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 --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 }