KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > util > Base64


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv.util;
18
19 /**
20  * This class provides encode/decode for RFC 2045 Base64 as
21  * defined by RFC 2045, N. Freed and N. Borenstein.
22  * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
23  * Part One: Format of Internet Message Bodies. Reference
24  * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
25  * This class is used by XML Schema binary format validation
26  *
27  * This implementation does not encode/decode streaming
28  * data. You need the data that you will encode/decode
29  * already on a byte arrray.
30  *
31  * @xerces.internal
32  *
33  * @author Jeffrey Rodriguez
34  * @author Sandy Gao
35  * @version $Id: Base64.java,v 1.15 2004/10/14 15:20:18 mrglavas Exp $
36  */

37 public final class Base64 {
38
39     static private final int BASELENGTH = 128;
40     static private final int LOOKUPLENGTH = 64;
41     static private final int TWENTYFOURBITGROUP = 24;
42     static private final int EIGHTBIT = 8;
43     static private final int SIXTEENBIT = 16;
44     static private final int SIXBIT = 6;
45     static private final int FOURBYTE = 4;
46     static private final int SIGN = -128;
47     static private final char PAD = '=';
48     static private final boolean fDebug = false;
49     static final private byte [] base64Alphabet = new byte[BASELENGTH];
50     static final private char [] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
51
52     static {
53
54         for (int i = 0; i < BASELENGTH; ++i) {
55             base64Alphabet[i] = -1;
56         }
57         for (int i = 'Z'; i >= 'A'; i--) {
58             base64Alphabet[i] = (byte) (i-'A');
59         }
60         for (int i = 'z'; i>= 'a'; i--) {
61             base64Alphabet[i] = (byte) ( i-'a' + 26);
62         }
63
64         for (int i = '9'; i >= '0'; i--) {
65             base64Alphabet[i] = (byte) (i-'0' + 52);
66         }
67
68         base64Alphabet['+'] = 62;
69         base64Alphabet['/'] = 63;
70
71         for (int i = 0; i<=25; i++)
72             lookUpBase64Alphabet[i] = (char)('A'+i);
73
74         for (int i = 26, j = 0; i<=51; i++, j++)
75             lookUpBase64Alphabet[i] = (char)('a'+ j);
76
77         for (int i = 52, j = 0; i<=61; i++, j++)
78             lookUpBase64Alphabet[i] = (char)('0' + j);
79         lookUpBase64Alphabet[62] = (char)'+';
80         lookUpBase64Alphabet[63] = (char)'/';
81
82     }
83
84     protected static boolean isWhiteSpace(char octect) {
85         return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
86     }
87
88     protected static boolean isPad(char octect) {
89         return (octect == PAD);
90     }
91
92     protected static boolean isData(char octect) {
93         return (octect < BASELENGTH && base64Alphabet[octect] != -1);
94     }
95
96     protected static boolean isBase64(char octect) {
97         return (isWhiteSpace(octect) || isPad(octect) || isData(octect));
98     }
99
100     /**
101      * Encodes hex octects into Base64
102      *
103      * @param binaryData Array containing binaryData
104      * @return Encoded Base64 array
105      */

106     public static String JavaDoc encode(byte[] binaryData) {
107
108         if (binaryData == null)
109             return null;
110
111         int lengthDataBits = binaryData.length*EIGHTBIT;
112         if (lengthDataBits == 0) {
113             return "";
114         }
115         
116         int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
117         int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
118         int numberQuartet = fewerThan24bits != 0 ? numberTriplets+1 : numberTriplets;
119         char encodedData[] = null;
120
121         encodedData = new char[numberQuartet*4];
122
123         byte k=0, l=0, b1=0,b2=0,b3=0;
124
125         int encodedIndex = 0;
126         int dataIndex = 0;
127         if (fDebug) {
128             System.out.println("number of triplets = " + numberTriplets );
129         }
130
131         for (int i=0; i<numberTriplets; i++) {
132             b1 = binaryData[dataIndex++];
133             b2 = binaryData[dataIndex++];
134             b3 = binaryData[dataIndex++];
135
136             if (fDebug) {
137                 System.out.println( "b1= " + b1 +", b2= " + b2 + ", b3= " + b3 );
138             }
139
140             l = (byte)(b2 & 0x0f);
141             k = (byte)(b1 & 0x03);
142
143             byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
144
145             byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
146             byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
147
148             if (fDebug) {
149                 System.out.println( "val2 = " + val2 );
150                 System.out.println( "k4 = " + (k<<4));
151                 System.out.println( "vak = " + (val2 | (k<<4)));
152             }
153
154             encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
155             encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
156             encodedData[encodedIndex++] = lookUpBase64Alphabet[ (l <<2 ) | val3 ];
157             encodedData[encodedIndex++] = lookUpBase64Alphabet[ b3 & 0x3f ];
158         }
159
160         // form integral number of 6-bit groups
161
if (fewerThan24bits == EIGHTBIT) {
162             b1 = binaryData[dataIndex];
163             k = (byte) ( b1 &0x03 );
164             if (fDebug) {
165                 System.out.println("b1=" + b1);
166                 System.out.println("b1<<2 = " + (b1>>2) );
167             }
168             byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
169             encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
170             encodedData[encodedIndex++] = lookUpBase64Alphabet[ k<<4 ];
171             encodedData[encodedIndex++] = PAD;
172             encodedData[encodedIndex++] = PAD;
173         } else if (fewerThan24bits == SIXTEENBIT) {
174             b1 = binaryData[dataIndex];
175             b2 = binaryData[dataIndex +1 ];
176             l = ( byte ) ( b2 &0x0f );
177             k = ( byte ) ( b1 &0x03 );
178
179             byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
180             byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
181
182             encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
183             encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
184             encodedData[encodedIndex++] = lookUpBase64Alphabet[ l<<2 ];
185             encodedData[encodedIndex++] = PAD;
186         }
187
188         return new String JavaDoc(encodedData);
189     }
190
191     /**
192      * Decodes Base64 data into octects
193      *
194      * @param encoded string containing Base64 data
195      * @return Array containind decoded data.
196      */

197     public static byte[] decode(String JavaDoc encoded) {
198
199         if (encoded == null)
200             return null;
201
202         char[] base64Data = encoded.toCharArray();
203         // remove white spaces
204
int len = removeWhiteSpace(base64Data);
205         
206         if (len%FOURBYTE != 0) {
207             return null;//should be divisible by four
208
}
209
210         int numberQuadruple = (len/FOURBYTE );
211
212         if (numberQuadruple == 0)
213             return new byte[0];
214
215         byte decodedData[] = null;
216         byte b1=0,b2=0,b3=0,b4=0;
217         char d1=0,d2=0,d3=0,d4=0;
218
219         int i = 0;
220         int encodedIndex = 0;
221         int dataIndex = 0;
222         decodedData = new byte[ (numberQuadruple)*3];
223
224         for (; i<numberQuadruple-1; i++) {
225
226             if (!isData( (d1 = base64Data[dataIndex++]) )||
227                 !isData( (d2 = base64Data[dataIndex++]) )||
228                 !isData( (d3 = base64Data[dataIndex++]) )||
229                 !isData( (d4 = base64Data[dataIndex++]) ))
230                 return null;//if found "no data" just return null
231

232             b1 = base64Alphabet[d1];
233             b2 = base64Alphabet[d2];
234             b3 = base64Alphabet[d3];
235             b4 = base64Alphabet[d4];
236
237             decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ;
238             decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
239             decodedData[encodedIndex++] = (byte)( b3<<6 | b4 );
240         }
241
242         if (!isData( (d1 = base64Data[dataIndex++]) ) ||
243             !isData( (d2 = base64Data[dataIndex++]) )) {
244             return null;//if found "no data" just return null
245
}
246
247         b1 = base64Alphabet[d1];
248         b2 = base64Alphabet[d2];
249
250         d3 = base64Data[dataIndex++];
251         d4 = base64Data[dataIndex++];
252         if (!isData( (d3 ) ) ||
253             !isData( (d4 ) )) {//Check if they are PAD characters
254
if (isPad( d3 ) && isPad( d4)) { //Two PAD e.g. 3c[Pad][Pad]
255
if ((b2 & 0xf) != 0)//last 4 bits should be zero
256
return null;
257                 byte[] tmp = new byte[ i*3 + 1 ];
258                 System.arraycopy( decodedData, 0, tmp, 0, i*3 );
259                 tmp[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
260                 return tmp;
261             } else if (!isPad( d3) && isPad(d4)) { //One PAD e.g. 3cQ[Pad]
262
b3 = base64Alphabet[ d3 ];
263                 if ((b3 & 0x3 ) != 0)//last 2 bits should be zero
264
return null;
265                 byte[] tmp = new byte[ i*3 + 2 ];
266                 System.arraycopy( decodedData, 0, tmp, 0, i*3 );
267                 tmp[encodedIndex++] = (byte)( b1 <<2 | b2>>4 );
268                 tmp[encodedIndex] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
269                 return tmp;
270             } else {
271                 return null;//an error like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data
272
}
273         } else { //No PAD e.g 3cQl
274
b3 = base64Alphabet[ d3 ];
275             b4 = base64Alphabet[ d4 ];
276             decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ;
277             decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
278             decodedData[encodedIndex++] = (byte)( b3<<6 | b4 );
279
280         }
281
282         return decodedData;
283     }
284
285     /**
286      * remove WhiteSpace from MIME containing encoded Base64 data.
287      *
288      * @param data the byte array of base64 data (with WS)
289      * @return the new length
290      */

291     protected static int removeWhiteSpace(char[] data) {
292         if (data == null)
293             return 0;
294
295         // count characters that's not whitespace
296
int newSize = 0;
297         int len = data.length;
298         for (int i = 0; i < len; i++) {
299             if (!isWhiteSpace(data[i]))
300                 data[newSize++] = data[i];
301         }
302         return newSize;
303     }
304 }
305
Popular Tags