src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/util/Base64.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.messaging.saaj.util;
ohair@286 27
ohair@286 28
ohair@286 29 // Cut & paste from tomcat
ohair@286 30
ohair@286 31 /**
ohair@286 32 * This class provides encode/decode for RFC 2045 Base64 as
ohair@286 33 * defined by RFC 2045, N. Freed and N. Borenstein.
ohair@286 34 * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
ohair@286 35 * Part One: Format of Internet Message Bodies. Reference
ohair@286 36 * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
ohair@286 37 * This class is used by XML Schema binary format validation
ohair@286 38 *
ohair@286 39 * @author Jeffrey Rodriguez
ohair@286 40 * @version
ohair@286 41 */
ohair@286 42 public final class Base64 {
ohair@286 43
ohair@286 44
ohair@286 45 static private final int BASELENGTH = 255;
ohair@286 46 static private final int LOOKUPLENGTH = 63;
ohair@286 47 static private final int TWENTYFOURBITGROUP = 24;
ohair@286 48 static private final int EIGHTBIT = 8;
ohair@286 49 static private final int SIXTEENBIT = 16;
ohair@286 50 static private final int SIXBIT = 6;
ohair@286 51 static private final int FOURBYTE = 4;
ohair@286 52
ohair@286 53
ohair@286 54 static private final byte PAD = ( byte ) '=';
ohair@286 55 static private byte [] base64Alphabet = new byte[BASELENGTH];
ohair@286 56 static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
ohair@286 57
ohair@286 58 static {
ohair@286 59
ohair@286 60 for (int i = 0; i<BASELENGTH; i++ ) {
ohair@286 61 base64Alphabet[i] = -1;
ohair@286 62 }
ohair@286 63 for ( int i = 'Z'; i >= 'A'; i-- ) {
ohair@286 64 base64Alphabet[i] = (byte) (i-'A');
ohair@286 65 }
ohair@286 66 for ( int i = 'z'; i>= 'a'; i--) {
ohair@286 67 base64Alphabet[i] = (byte) ( i-'a' + 26);
ohair@286 68 }
ohair@286 69
ohair@286 70 for ( int i = '9'; i >= '0'; i--) {
ohair@286 71 base64Alphabet[i] = (byte) (i-'0' + 52);
ohair@286 72 }
ohair@286 73
ohair@286 74 base64Alphabet['+'] = 62;
ohair@286 75 base64Alphabet['/'] = 63;
ohair@286 76
ohair@286 77 for (int i = 0; i<=25; i++ )
ohair@286 78 lookUpBase64Alphabet[i] = (byte) ('A'+i );
ohair@286 79
ohair@286 80 for (int i = 26, j = 0; i<=51; i++, j++ )
ohair@286 81 lookUpBase64Alphabet[i] = (byte) ('a'+ j );
ohair@286 82
ohair@286 83 for (int i = 52, j = 0; i<=61; i++, j++ )
ohair@286 84 lookUpBase64Alphabet[i] = (byte) ('0' + j );
ohair@286 85
ohair@286 86 }
ohair@286 87
ohair@286 88
ohair@286 89 static boolean isBase64( byte octect ) {
ohair@286 90 //shall we ignore white space? JEFF??
ohair@286 91 return(octect == PAD || base64Alphabet[octect] != -1 );
ohair@286 92 }
ohair@286 93
ohair@286 94
ohair@286 95 static boolean isArrayByteBase64( byte[] arrayOctect ) {
ohair@286 96 int length = arrayOctect.length;
ohair@286 97 if ( length == 0 )
ohair@286 98 return false;
ohair@286 99 for ( int i=0; i < length; i++ ) {
ohair@286 100 if ( Base64.isBase64( arrayOctect[i] ) == false)
ohair@286 101 return false;
ohair@286 102 }
ohair@286 103 return true;
ohair@286 104 }
ohair@286 105
ohair@286 106 /**
ohair@286 107 * Encodes hex octects into Base64
ohair@286 108 *
ohair@286 109 * @param binaryData Array containing binaryData
ohair@286 110 * @return Encoded Base64 array
ohair@286 111 */
ohair@286 112 public static byte[] encode( byte[] binaryData ) {
ohair@286 113 int lengthDataBits = binaryData.length*EIGHTBIT;
ohair@286 114 int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
ohair@286 115 int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
ohair@286 116 byte encodedData[] = null;
ohair@286 117
ohair@286 118
ohair@286 119 if ( fewerThan24bits != 0 ) //data not divisible by 24 bit
ohair@286 120 encodedData = new byte[ (numberTriplets + 1 )*4 ];
ohair@286 121 else // 16 or 8 bit
ohair@286 122 encodedData = new byte[ numberTriplets*4 ];
ohair@286 123
ohair@286 124 byte k=0, l=0, b1=0,b2=0,b3=0;
ohair@286 125
ohair@286 126 int encodedIndex = 0;
ohair@286 127 int dataIndex = 0;
ohair@286 128 int i = 0;
ohair@286 129 for ( i = 0; i<numberTriplets; i++ ) {
ohair@286 130
ohair@286 131 dataIndex = i*3;
ohair@286 132 b1 = binaryData[dataIndex];
ohair@286 133 b2 = binaryData[dataIndex + 1];
ohair@286 134 b3 = binaryData[dataIndex + 2];
ohair@286 135
ohair@286 136 l = (byte)(b2 & 0x0f);
ohair@286 137 k = (byte)(b1 & 0x03);
ohair@286 138
ohair@286 139 encodedIndex = i*4;
ohair@286 140 encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
ohair@286 141 encodedData[encodedIndex+1] = lookUpBase64Alphabet[(b2 >>4 ) |
ohair@286 142 ( k<<4 )];
ohair@286 143 encodedData[encodedIndex+2] = lookUpBase64Alphabet[ (l <<2 ) |
ohair@286 144 ( b3>>6)];
ohair@286 145 encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
ohair@286 146 }
ohair@286 147
ohair@286 148 // form integral number of 6-bit groups
ohair@286 149 dataIndex = i*3;
ohair@286 150 encodedIndex = i*4;
ohair@286 151 if (fewerThan24bits == EIGHTBIT ) {
ohair@286 152 b1 = binaryData[dataIndex];
ohair@286 153 k = (byte) ( b1 &0x03 );
ohair@286 154 encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
ohair@286 155 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ];
ohair@286 156 encodedData[encodedIndex + 2] = PAD;
ohair@286 157 encodedData[encodedIndex + 3] = PAD;
ohair@286 158 } else if ( fewerThan24bits == SIXTEENBIT ) {
ohair@286 159
ohair@286 160 b1 = binaryData[dataIndex];
ohair@286 161 b2 = binaryData[dataIndex +1 ];
ohair@286 162 l = ( byte ) ( b2 &0x0f );
ohair@286 163 k = ( byte ) ( b1 &0x03 );
ohair@286 164 encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
ohair@286 165 encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ (b2 >>4 )
ohair@286 166 | ( k<<4 )];
ohair@286 167 encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
ohair@286 168 encodedData[encodedIndex + 3] = PAD;
ohair@286 169 }
ohair@286 170 return encodedData;
ohair@286 171 }
ohair@286 172
ohair@286 173
ohair@286 174 /**
ohair@286 175 * Decodes Base64 data into octects
ohair@286 176 *
ohair@286 177 * @param binaryData Byte array containing Base64 data
ohair@286 178 * @return Array containind decoded data.
ohair@286 179 */
ohair@286 180 public byte[] decode( byte[] base64Data ) {
ohair@286 181 int numberQuadruple = base64Data.length/FOURBYTE;
ohair@286 182 byte decodedData[] = null;
ohair@286 183 byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;
ohair@286 184
ohair@286 185 // Throw away anything not in base64Data
ohair@286 186 // Adjust size
ohair@286 187
ohair@286 188 int encodedIndex = 0;
ohair@286 189 int dataIndex = 0;
ohair@286 190 decodedData = new byte[ numberQuadruple*3 + 1 ];
ohair@286 191
ohair@286 192 for (int i = 0; i<numberQuadruple; i++ ) {
ohair@286 193 dataIndex = i*4;
ohair@286 194 marker0 = base64Data[dataIndex +2];
ohair@286 195 marker1 = base64Data[dataIndex +3];
ohair@286 196
ohair@286 197 b1 = base64Alphabet[base64Data[dataIndex]];
ohair@286 198 b2 = base64Alphabet[base64Data[dataIndex +1]];
ohair@286 199
ohair@286 200 if ( marker0 != PAD && marker1 != PAD ) { //No PAD e.g 3cQl
ohair@286 201 b3 = base64Alphabet[ marker0 ];
ohair@286 202 b4 = base64Alphabet[ marker1 ];
ohair@286 203
ohair@286 204 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
ohair@286 205 decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |(
ohair@286 206 (b3>>2) & 0xf) );
ohair@286 207 decodedData[encodedIndex+2] = (byte)( b3<<6 | b4 );
ohair@286 208 } else if ( marker0 == PAD ) { //Two PAD e.g. 3c[Pad][Pad]
ohair@286 209 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
ohair@286 210 decodedData[encodedIndex+1] = (byte)((b2 & 0xf)<<4 );
ohair@286 211 decodedData[encodedIndex+2] = (byte) 0;
ohair@286 212 } else if ( marker1 == PAD ) { //One PAD e.g. 3cQ[Pad]
ohair@286 213 b3 = base64Alphabet[ marker0 ];
ohair@286 214
ohair@286 215 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 );
ohair@286 216 decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |(
ohair@286 217 (b3>>2) & 0xf) );
ohair@286 218 decodedData[encodedIndex+2] = (byte)( b3<<6);
ohair@286 219 }
ohair@286 220 encodedIndex += 3;
ohair@286 221 }
ohair@286 222 return decodedData;
ohair@286 223
ohair@286 224 }
ohair@286 225
ohair@286 226 static final int base64[]= {
ohair@286 227 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 228 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 229 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
ohair@286 230 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
ohair@286 231 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
ohair@286 232 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
ohair@286 233 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
ohair@286 234 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
ohair@286 235 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 236 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 237 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 238 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 239 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 240 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 241 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
ohair@286 242 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
ohair@286 243 };
ohair@286 244
ohair@286 245 public static String base64Decode( String orig ) {
ohair@286 246 char chars[]=orig.toCharArray();
ohair@286 247 StringBuffer sb=new StringBuffer();
ohair@286 248 int i=0;
ohair@286 249
ohair@286 250 int shift = 0; // # of excess bits stored in accum
ohair@286 251 int acc = 0;
ohair@286 252
ohair@286 253 for (i=0; i<chars.length; i++) {
ohair@286 254 int v = base64[ chars[i] & 0xFF ];
ohair@286 255
ohair@286 256 if ( v >= 64 ) {
ohair@286 257 if( chars[i] != '=' )
ohair@286 258 System.out.println("Wrong char in base64: " + chars[i]);
ohair@286 259 } else {
ohair@286 260 acc= ( acc << 6 ) | v;
ohair@286 261 shift += 6;
ohair@286 262 if ( shift >= 8 ) {
ohair@286 263 shift -= 8;
ohair@286 264 sb.append( (char) ((acc >> shift) & 0xff));
ohair@286 265 }
ohair@286 266 }
ohair@286 267 }
ohair@286 268 return sb.toString();
ohair@286 269 }
ohair@286 270
ohair@286 271
ohair@286 272 }

mercurial