1/*
2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
27
28package com.sun.xml.internal.fastinfoset.algorithm;
29
30import java.io.IOException;
31import java.io.InputStream;
32import java.io.OutputStream;
33import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
34import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
35
36public class BASE64EncodingAlgorithm extends BuiltInEncodingAlgorithm {
37
38    /* package */ static final char encodeBase64[] = {
39        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
40        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
41        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
42        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
43        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
44    };
45
46    /* package */ static final int decodeBase64[] = {
47        /*'+'*/ 62,
48        -1, -1, -1,
49        /*'/'*/ 63,
50        /*'0'*/ 52,
51        /*'1'*/ 53,
52        /*'2'*/ 54,
53        /*'3'*/ 55,
54        /*'4'*/ 56,
55        /*'5'*/ 57,
56        /*'6'*/ 58,
57        /*'7'*/ 59,
58        /*'8'*/ 60,
59        /*'9'*/ 61,
60        -1, -1, -1, -1, -1, -1, -1,
61        /*'A'*/ 0,
62        /*'B'*/ 1,
63        /*'C'*/ 2,
64        /*'D'*/ 3,
65        /*'E'*/ 4,
66        /*'F'*/ 5,
67        /*'G'*/ 6,
68        /*'H'*/ 7,
69        /*'I'*/ 8,
70        /*'J'*/ 9,
71        /*'K'*/ 10,
72        /*'L'*/ 11,
73        /*'M'*/ 12,
74        /*'N'*/ 13,
75        /*'O'*/ 14,
76        /*'P'*/ 15,
77        /*'Q'*/ 16,
78        /*'R'*/ 17,
79        /*'S'*/ 18,
80        /*'T'*/ 19,
81        /*'U'*/ 20,
82        /*'V'*/ 21,
83        /*'W'*/ 22,
84        /*'X'*/ 23,
85        /*'Y'*/ 24,
86        /*'Z'*/ 25,
87        -1, -1, -1, -1, -1, -1,
88        /*'a'*/ 26,
89        /*'b'*/ 27,
90        /*'c'*/ 28,
91        /*'d'*/ 29,
92        /*'e'*/ 30,
93        /*'f'*/ 31,
94        /*'g'*/ 32,
95        /*'h'*/ 33,
96        /*'i'*/ 34,
97        /*'j'*/ 35,
98        /*'k'*/ 36,
99        /*'l'*/ 37,
100        /*'m'*/ 38,
101        /*'n'*/ 39,
102        /*'o'*/ 40,
103        /*'p'*/ 41,
104        /*'q'*/ 42,
105        /*'r'*/ 43,
106        /*'s'*/ 44,
107        /*'t'*/ 45,
108        /*'u'*/ 46,
109        /*'v'*/ 47,
110        /*'w'*/ 48,
111        /*'x'*/ 49,
112        /*'y'*/ 50,
113        /*'z'*/ 51
114    };
115
116    public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
117        final byte[] data = new byte[length];
118        System.arraycopy(b, start, data, 0, length);
119        return data;
120    }
121
122    public final Object decodeFromInputStream(InputStream s) throws IOException {
123        throw new UnsupportedOperationException(CommonResourceBundle.getInstance().getString("message.notImplemented"));
124    }
125
126
127    public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
128        if (!(data instanceof byte[])) {
129            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotByteArray"));
130        }
131
132        s.write((byte[])data);
133    }
134
135    public final Object convertFromCharacters(char[] ch, int start, int length) {
136        if (length == 0) {
137            return new byte[0];
138        }
139
140        StringBuilder encodedValue = removeWhitespace(ch, start, length);
141        int encodedLength = encodedValue.length();
142        if (encodedLength == 0) {
143            return new byte[0];
144        }
145
146        int blockCount = encodedLength / 4;
147        int partialBlockLength = 3;
148
149        if (encodedValue.charAt(encodedLength - 1) == '=') {
150            --partialBlockLength;
151            if (encodedValue.charAt(encodedLength - 2) == '=') {
152                --partialBlockLength;
153            }
154        }
155
156        int valueLength = (blockCount - 1) * 3 + partialBlockLength;
157        byte[] value = new byte[valueLength];
158
159        int idx = 0;
160        int encodedIdx = 0;
161        for (int i = 0; i < blockCount; ++i) {
162            int x1 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
163            int x2 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
164            int x3 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
165            int x4 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
166
167            value[idx++] = (byte) ((x1 << 2) | (x2 >> 4));
168            if (idx < valueLength) {
169                value[idx++] = (byte) (((x2 & 0x0f) << 4) | (x3 >> 2));
170            }
171            if (idx < valueLength) {
172                value[idx++] = (byte) (((x3 & 0x03) << 6) | x4);
173            }
174        }
175
176        return value;
177    }
178
179    public final void convertToCharacters(Object data, StringBuffer s) {
180        if (data == null) {
181            return;
182        }
183        final byte[] value = (byte[]) data;
184
185        convertToCharacters(value, 0, value.length, s);
186    }
187
188    public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
189        return octetLength;
190    }
191
192    public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
193        return primitiveLength;
194    }
195
196    public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
197        System.arraycopy((byte[])array, astart, b, start, alength);
198    }
199
200    public final void convertToCharacters(byte[] data, int offset, int length, StringBuffer s) {
201        if (data == null) {
202            return;
203        }
204        final byte[] value = data;
205        if (length == 0) {
206            return;
207        }
208
209        final int partialBlockLength = length % 3;
210        final int blockCount = (partialBlockLength != 0) ?
211            length / 3 + 1 :
212            length / 3;
213
214        final int encodedLength = blockCount * 4;
215        final int originalBufferSize = s.length();
216        s.ensureCapacity(encodedLength + originalBufferSize);
217
218        int idx = offset;
219        int lastIdx = offset + length;
220        for (int i = 0; i < blockCount; ++i) {
221            int b1 = value[idx++] & 0xFF;
222            int b2 = (idx < lastIdx) ? value[idx++] & 0xFF : 0;
223            int b3 = (idx < lastIdx) ? value[idx++] & 0xFF : 0;
224
225            s.append(encodeBase64[b1 >> 2]);
226
227            s.append(encodeBase64[((b1 & 0x03) << 4) | (b2 >> 4)]);
228
229            s.append(encodeBase64[((b2 & 0x0f) << 2) | (b3 >> 6)]);
230
231            s.append(encodeBase64[b3 & 0x3f]);
232        }
233
234        switch (partialBlockLength) {
235            case 1 :
236                s.setCharAt(originalBufferSize + encodedLength - 1, '=');
237                s.setCharAt(originalBufferSize + encodedLength - 2, '=');
238                break;
239            case 2 :
240                s.setCharAt(originalBufferSize + encodedLength - 1, '=');
241                break;
242        }
243    }
244}
245