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