1 /*
2 * Copyright (c) 1997, 2012, 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
26 package com.sun.xml.internal.messaging.saaj.util;
27
28
29 // Cut & paste from tomcat
30
31 /**
32 * This class provides encode/decode for RFC 2045 Base64 as
33 * defined by RFC 2045, N. Freed and N. Borenstein.
34 * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
35 * Part One: Format of Internet Message Bodies. Reference
36 * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
37 * This class is used by XML Schema binary format validation
38 *
39 * @author Jeffrey Rodriguez
40 * @version
41 */
42 public final class Base64 {
43
44
45 static private final int BASELENGTH = 255;
46 static private final int LOOKUPLENGTH = 63;
47 static private final int TWENTYFOURBITGROUP = 24;
48 static private final int EIGHTBIT = 8;
49 static private final int SIXTEENBIT = 16;
50 static private final int FOURBYTE = 4;
51
52
53 static private final byte PAD = ( byte ) '=';
54 static private byte [] base64Alphabet = new byte[BASELENGTH];
55 static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
56
57 static {
58
59 for (int i = 0; i<BASELENGTH; i++ ) {
60 base64Alphabet[i] = -1;
156 encodedData[encodedIndex + 3] = PAD;
157 } else if ( fewerThan24bits == SIXTEENBIT ) {
158
159 b1 = binaryData[dataIndex];
160 b2 = binaryData[dataIndex +1 ];
161 l = ( byte ) ( b2 &0x0f );
162 k = ( byte ) ( b1 &0x03 );
163 encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
164 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ (b2 >>4 )
165 | ( k<<4 )];
166 encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
167 encodedData[encodedIndex + 3] = PAD;
168 }
169 return encodedData;
170 }
171
172
173 /**
174 * Decodes Base64 data into octects
175 *
176 * @param binaryData Byte array containing Base64 data
177 * @return Array containind decoded data.
178 */
179 public byte[] decode( byte[] base64Data ) {
180 int numberQuadruple = base64Data.length/FOURBYTE;
181 byte decodedData[] = null;
182 byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;
183
184 // Throw away anything not in base64Data
185 // Adjust size
186
187 int encodedIndex = 0;
188 int dataIndex = 0;
189 decodedData = new byte[ numberQuadruple*3 + 1 ];
190
191 for (int i = 0; i<numberQuadruple; i++ ) {
192 dataIndex = i*4;
193 marker0 = base64Data[dataIndex +2];
194 marker1 = base64Data[dataIndex +3];
195
196 b1 = base64Alphabet[base64Data[dataIndex]];
|
1 /*
2 * Copyright (c) 1997, 2017, 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
26 package com.sun.xml.internal.messaging.saaj.util;
27
28
29 // Cut & paste from tomcat
30
31 /**
32 * This class provides encode/decode for RFC 2045 Base64 as
33 * defined by RFC 2045, N. Freed and N. Borenstein.
34 * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
35 * Part One: Format of Internet Message Bodies. Reference
36 * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
37 * This class is used by XML Schema binary format validation
38 *
39 * @author Jeffrey Rodriguez
40 */
41 public final class Base64 {
42
43
44 static private final int BASELENGTH = 255;
45 static private final int LOOKUPLENGTH = 63;
46 static private final int TWENTYFOURBITGROUP = 24;
47 static private final int EIGHTBIT = 8;
48 static private final int SIXTEENBIT = 16;
49 static private final int FOURBYTE = 4;
50
51
52 static private final byte PAD = ( byte ) '=';
53 static private byte [] base64Alphabet = new byte[BASELENGTH];
54 static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
55
56 static {
57
58 for (int i = 0; i<BASELENGTH; i++ ) {
59 base64Alphabet[i] = -1;
155 encodedData[encodedIndex + 3] = PAD;
156 } else if ( fewerThan24bits == SIXTEENBIT ) {
157
158 b1 = binaryData[dataIndex];
159 b2 = binaryData[dataIndex +1 ];
160 l = ( byte ) ( b2 &0x0f );
161 k = ( byte ) ( b1 &0x03 );
162 encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
163 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ (b2 >>4 )
164 | ( k<<4 )];
165 encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
166 encodedData[encodedIndex + 3] = PAD;
167 }
168 return encodedData;
169 }
170
171
172 /**
173 * Decodes Base64 data into octects
174 *
175 * @param base64Data Byte array containing Base64 data
176 * @return Array containind decoded data.
177 */
178 public byte[] decode( byte[] base64Data ) {
179 int numberQuadruple = base64Data.length/FOURBYTE;
180 byte decodedData[] = null;
181 byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;
182
183 // Throw away anything not in base64Data
184 // Adjust size
185
186 int encodedIndex = 0;
187 int dataIndex = 0;
188 decodedData = new byte[ numberQuadruple*3 + 1 ];
189
190 for (int i = 0; i<numberQuadruple; i++ ) {
191 dataIndex = i*4;
192 marker0 = base64Data[dataIndex +2];
193 marker1 = base64Data[dataIndex +3];
194
195 b1 = base64Alphabet[base64Data[dataIndex]];
|