1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.dv.util;
23
24/**
25 * This class provides encode/decode for RFC 2045 Base64 as
26 * defined by RFC 2045, N. Freed and N. Borenstein.
27 * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
28 * Part One: Format of Internet Message Bodies. Reference
29 * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
30 * This class is used by XML Schema binary format validation
31 *
32 * This implementation does not encode/decode streaming
33 * data. You need the data that you will encode/decode
34 * already on a byte arrray.
35 *
36 * @xerces.internal
37 *
38 * @author Jeffrey Rodriguez
39 * @author Sandy Gao
40 */
41public final class  Base64 {
42
43    static private final int  BASELENGTH         = 128;
44    static private final int  LOOKUPLENGTH       = 64;
45    static private final int  TWENTYFOURBITGROUP = 24;
46    static private final int  EIGHTBIT           = 8;
47    static private final int  SIXTEENBIT         = 16;
48    static private final int  SIXBIT             = 6;
49    static private final int  FOURBYTE           = 4;
50    static private final int  SIGN               = -128;
51    static private final char PAD                = '=';
52    static private final boolean fDebug          = false;
53    static final private byte [] base64Alphabet        = new byte[BASELENGTH];
54    static final private char [] lookUpBase64Alphabet  = new char[LOOKUPLENGTH];
55
56    static {
57
58        for (int i = 0; i < BASELENGTH; ++i) {
59            base64Alphabet[i] = -1;
60        }
61        for (int i = 'Z'; i >= 'A'; i--) {
62            base64Alphabet[i] = (byte) (i-'A');
63        }
64        for (int i = 'z'; i>= 'a'; i--) {
65            base64Alphabet[i] = (byte) ( i-'a' + 26);
66        }
67
68        for (int i = '9'; i >= '0'; i--) {
69            base64Alphabet[i] = (byte) (i-'0' + 52);
70        }
71
72        base64Alphabet['+']  = 62;
73        base64Alphabet['/']  = 63;
74
75        for (int i = 0; i<=25; i++)
76            lookUpBase64Alphabet[i] = (char)('A'+i);
77
78        for (int i = 26,  j = 0; i<=51; i++, j++)
79            lookUpBase64Alphabet[i] = (char)('a'+ j);
80
81        for (int i = 52,  j = 0; i<=61; i++, j++)
82            lookUpBase64Alphabet[i] = (char)('0' + j);
83        lookUpBase64Alphabet[62] = (char)'+';
84        lookUpBase64Alphabet[63] = (char)'/';
85
86    }
87
88    protected static boolean isWhiteSpace(char octect) {
89        return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
90    }
91
92    protected static boolean isPad(char octect) {
93        return (octect == PAD);
94    }
95
96    protected static boolean isData(char octect) {
97        return (octect < BASELENGTH && base64Alphabet[octect] != -1);
98    }
99
100    protected static boolean isBase64(char octect) {
101        return (isWhiteSpace(octect) || isPad(octect) || isData(octect));
102    }
103
104    /**
105     * Encodes hex octects into Base64
106     *
107     * @param binaryData Array containing binaryData
108     * @return Encoded Base64 array
109     */
110    public static String encode(byte[] binaryData) {
111
112        if (binaryData == null)
113            return null;
114
115        int      lengthDataBits    = binaryData.length*EIGHTBIT;
116        if (lengthDataBits == 0) {
117            return "";
118        }
119
120        int      fewerThan24bits   = lengthDataBits%TWENTYFOURBITGROUP;
121        int      numberTriplets    = lengthDataBits/TWENTYFOURBITGROUP;
122        int      numberQuartet     = fewerThan24bits != 0 ? numberTriplets+1 : numberTriplets;
123        char     encodedData[]     = null;
124
125        encodedData = new char[numberQuartet*4];
126
127        byte k=0, l=0, b1=0,b2=0,b3=0;
128
129        int encodedIndex = 0;
130        int dataIndex   = 0;
131        if (fDebug) {
132            System.out.println("number of triplets = " + numberTriplets );
133        }
134
135        for (int i=0; i<numberTriplets; i++) {
136            b1 = binaryData[dataIndex++];
137            b2 = binaryData[dataIndex++];
138            b3 = binaryData[dataIndex++];
139
140            if (fDebug) {
141                System.out.println( "b1= " + b1 +", b2= " + b2 + ", b3= " + b3 );
142            }
143
144            l  = (byte)(b2 & 0x0f);
145            k  = (byte)(b1 & 0x03);
146
147            byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
148
149            byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
150            byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
151
152            if (fDebug) {
153                System.out.println( "val2 = " + val2 );
154                System.out.println( "k4   = " + (k<<4));
155                System.out.println( "vak  = " + (val2 | (k<<4)));
156            }
157
158            encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
159            encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
160            encodedData[encodedIndex++] = lookUpBase64Alphabet[ (l <<2 ) | val3 ];
161            encodedData[encodedIndex++] = lookUpBase64Alphabet[ b3 & 0x3f ];
162        }
163
164        // form integral number of 6-bit groups
165        if (fewerThan24bits == EIGHTBIT) {
166            b1 = binaryData[dataIndex];
167            k = (byte) ( b1 &0x03 );
168            if (fDebug) {
169                System.out.println("b1=" + b1);
170                System.out.println("b1<<2 = " + (b1>>2) );
171            }
172            byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
173            encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
174            encodedData[encodedIndex++] = lookUpBase64Alphabet[ k<<4 ];
175            encodedData[encodedIndex++] = PAD;
176            encodedData[encodedIndex++] = PAD;
177        } else if (fewerThan24bits == SIXTEENBIT) {
178            b1 = binaryData[dataIndex];
179            b2 = binaryData[dataIndex +1 ];
180            l = ( byte ) ( b2 &0x0f );
181            k = ( byte ) ( b1 &0x03 );
182
183            byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
184            byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
185
186            encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
187            encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
188            encodedData[encodedIndex++] = lookUpBase64Alphabet[ l<<2 ];
189            encodedData[encodedIndex++] = PAD;
190        }
191
192        return new String(encodedData);
193    }
194
195    /**
196     * Decodes Base64 data into octects
197     *
198     * @param encoded string containing Base64 data
199     * @return Array containind decoded data.
200     */
201    public static byte[] decode(String encoded) {
202
203        if (encoded == null)
204            return null;
205
206        char[] base64Data = encoded.toCharArray();
207        // remove white spaces
208        int len = removeWhiteSpace(base64Data);
209
210        if (len%FOURBYTE != 0) {
211            return null;//should be divisible by four
212        }
213
214        int      numberQuadruple    = (len/FOURBYTE );
215
216        if (numberQuadruple == 0)
217            return new byte[0];
218
219        byte     decodedData[]      = null;
220        byte     b1=0,b2=0,b3=0,b4=0;
221        char     d1=0,d2=0,d3=0,d4=0;
222
223        int i = 0;
224        int encodedIndex = 0;
225        int dataIndex    = 0;
226        decodedData      = new byte[ (numberQuadruple)*3];
227
228        for (; i<numberQuadruple-1; i++) {
229
230            if (!isData( (d1 = base64Data[dataIndex++]) )||
231                !isData( (d2 = base64Data[dataIndex++]) )||
232                !isData( (d3 = base64Data[dataIndex++]) )||
233                !isData( (d4 = base64Data[dataIndex++]) ))
234                return null;//if found "no data" just return null
235
236            b1 = base64Alphabet[d1];
237            b2 = base64Alphabet[d2];
238            b3 = base64Alphabet[d3];
239            b4 = base64Alphabet[d4];
240
241            decodedData[encodedIndex++] = (byte)(  b1 <<2 | b2>>4 ) ;
242            decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
243            decodedData[encodedIndex++] = (byte)( b3<<6 | b4 );
244        }
245
246        if (!isData( (d1 = base64Data[dataIndex++]) ) ||
247            !isData( (d2 = base64Data[dataIndex++]) )) {
248            return null;//if found "no data" just return null
249        }
250
251        b1 = base64Alphabet[d1];
252        b2 = base64Alphabet[d2];
253
254        d3 = base64Data[dataIndex++];
255        d4 = base64Data[dataIndex++];
256        if (!isData( (d3 ) ) ||
257            !isData( (d4 ) )) {//Check if they are PAD characters
258            if (isPad( d3 ) && isPad( d4)) {               //Two PAD e.g. 3c[Pad][Pad]
259                if ((b2 & 0xf) != 0)//last 4 bits should be zero
260                    return null;
261                byte[] tmp = new byte[ i*3 + 1 ];
262                System.arraycopy( decodedData, 0, tmp, 0, i*3 );
263                tmp[encodedIndex]   = (byte)(  b1 <<2 | b2>>4 ) ;
264                return tmp;
265            } else if (!isPad( d3) && isPad(d4)) {               //One PAD  e.g. 3cQ[Pad]
266                b3 = base64Alphabet[ d3 ];
267                if ((b3 & 0x3 ) != 0)//last 2 bits should be zero
268                    return null;
269                byte[] tmp = new byte[ i*3 + 2 ];
270                System.arraycopy( decodedData, 0, tmp, 0, i*3 );
271                tmp[encodedIndex++] = (byte)(  b1 <<2 | b2>>4 );
272                tmp[encodedIndex]   = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
273                return tmp;
274            } else {
275                return null;//an error  like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data
276            }
277        } else { //No PAD e.g 3cQl
278            b3 = base64Alphabet[ d3 ];
279            b4 = base64Alphabet[ d4 ];
280            decodedData[encodedIndex++] = (byte)(  b1 <<2 | b2>>4 ) ;
281            decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
282            decodedData[encodedIndex++] = (byte)( b3<<6 | b4 );
283
284        }
285
286        return decodedData;
287    }
288
289    /**
290     * remove WhiteSpace from MIME containing encoded Base64 data.
291     *
292     * @param data  the byte array of base64 data (with WS)
293     * @return      the new length
294     */
295    protected static int removeWhiteSpace(char[] data) {
296        if (data == null)
297            return 0;
298
299        // count characters that's not whitespace
300        int newSize = 0;
301        int len = data.length;
302        for (int i = 0; i < len; i++) {
303            if (!isWhiteSpace(data[i]))
304                data[newSize++] = data[i];
305        }
306        return newSize;
307    }
308}
309