1 16 17 package org.apache.xerces.impl.dv.util; 18 19 37 public final class Base64 { 38 39 static private final int BASELENGTH = 128; 40 static private final int LOOKUPLENGTH = 64; 41 static private final int TWENTYFOURBITGROUP = 24; 42 static private final int EIGHTBIT = 8; 43 static private final int SIXTEENBIT = 16; 44 static private final int SIXBIT = 6; 45 static private final int FOURBYTE = 4; 46 static private final int SIGN = -128; 47 static private final char PAD = '='; 48 static private final boolean fDebug = false; 49 static final private byte [] base64Alphabet = new byte[BASELENGTH]; 50 static final private char [] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; 51 52 static { 53 54 for (int i = 0; i < BASELENGTH; ++i) { 55 base64Alphabet[i] = -1; 56 } 57 for (int i = 'Z'; i >= 'A'; i--) { 58 base64Alphabet[i] = (byte) (i-'A'); 59 } 60 for (int i = 'z'; i>= 'a'; i--) { 61 base64Alphabet[i] = (byte) ( i-'a' + 26); 62 } 63 64 for (int i = '9'; i >= '0'; i--) { 65 base64Alphabet[i] = (byte) (i-'0' + 52); 66 } 67 68 base64Alphabet['+'] = 62; 69 base64Alphabet['/'] = 63; 70 71 for (int i = 0; i<=25; i++) 72 lookUpBase64Alphabet[i] = (char)('A'+i); 73 74 for (int i = 26, j = 0; i<=51; i++, j++) 75 lookUpBase64Alphabet[i] = (char)('a'+ j); 76 77 for (int i = 52, j = 0; i<=61; i++, j++) 78 lookUpBase64Alphabet[i] = (char)('0' + j); 79 lookUpBase64Alphabet[62] = (char)'+'; 80 lookUpBase64Alphabet[63] = (char)'/'; 81 82 } 83 84 protected static boolean isWhiteSpace(char octect) { 85 return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); 86 } 87 88 protected static boolean isPad(char octect) { 89 return (octect == PAD); 90 } 91 92 protected static boolean isData(char octect) { 93 return (octect < BASELENGTH && base64Alphabet[octect] != -1); 94 } 95 96 protected static boolean isBase64(char octect) { 97 return (isWhiteSpace(octect) || isPad(octect) || isData(octect)); 98 } 99 100 106 public static String encode(byte[] binaryData) { 107 108 if (binaryData == null) 109 return null; 110 111 int lengthDataBits = binaryData.length*EIGHTBIT; 112 if (lengthDataBits == 0) { 113 return ""; 114 } 115 116 int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP; 117 int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP; 118 int numberQuartet = fewerThan24bits != 0 ? numberTriplets+1 : numberTriplets; 119 char encodedData[] = null; 120 121 encodedData = new char[numberQuartet*4]; 122 123 byte k=0, l=0, b1=0,b2=0,b3=0; 124 125 int encodedIndex = 0; 126 int dataIndex = 0; 127 if (fDebug) { 128 System.out.println("number of triplets = " + numberTriplets ); 129 } 130 131 for (int i=0; i<numberTriplets; i++) { 132 b1 = binaryData[dataIndex++]; 133 b2 = binaryData[dataIndex++]; 134 b3 = binaryData[dataIndex++]; 135 136 if (fDebug) { 137 System.out.println( "b1= " + b1 +", b2= " + b2 + ", b3= " + b3 ); 138 } 139 140 l = (byte)(b2 & 0x0f); 141 k = (byte)(b1 & 0x03); 142 143 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 144 145 byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 146 byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc); 147 148 if (fDebug) { 149 System.out.println( "val2 = " + val2 ); 150 System.out.println( "k4 = " + (k<<4)); 151 System.out.println( "vak = " + (val2 | (k<<4))); 152 } 153 154 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 155 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )]; 156 encodedData[encodedIndex++] = lookUpBase64Alphabet[ (l <<2 ) | val3 ]; 157 encodedData[encodedIndex++] = lookUpBase64Alphabet[ b3 & 0x3f ]; 158 } 159 160 if (fewerThan24bits == EIGHTBIT) { 162 b1 = binaryData[dataIndex]; 163 k = (byte) ( b1 &0x03 ); 164 if (fDebug) { 165 System.out.println("b1=" + b1); 166 System.out.println("b1<<2 = " + (b1>>2) ); 167 } 168 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 169 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 170 encodedData[encodedIndex++] = lookUpBase64Alphabet[ k<<4 ]; 171 encodedData[encodedIndex++] = PAD; 172 encodedData[encodedIndex++] = PAD; 173 } else if (fewerThan24bits == SIXTEENBIT) { 174 b1 = binaryData[dataIndex]; 175 b2 = binaryData[dataIndex +1 ]; 176 l = ( byte ) ( b2 &0x0f ); 177 k = ( byte ) ( b1 &0x03 ); 178 179 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 180 byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 181 182 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 183 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )]; 184 encodedData[encodedIndex++] = lookUpBase64Alphabet[ l<<2 ]; 185 encodedData[encodedIndex++] = PAD; 186 } 187 188 return new String (encodedData); 189 } 190 191 197 public static byte[] decode(String encoded) { 198 199 if (encoded == null) 200 return null; 201 202 char[] base64Data = encoded.toCharArray(); 203 int len = removeWhiteSpace(base64Data); 205 206 if (len%FOURBYTE != 0) { 207 return null; } 209 210 int numberQuadruple = (len/FOURBYTE ); 211 212 if (numberQuadruple == 0) 213 return new byte[0]; 214 215 byte decodedData[] = null; 216 byte b1=0,b2=0,b3=0,b4=0; 217 char d1=0,d2=0,d3=0,d4=0; 218 219 int i = 0; 220 int encodedIndex = 0; 221 int dataIndex = 0; 222 decodedData = new byte[ (numberQuadruple)*3]; 223 224 for (; i<numberQuadruple-1; i++) { 225 226 if (!isData( (d1 = base64Data[dataIndex++]) )|| 227 !isData( (d2 = base64Data[dataIndex++]) )|| 228 !isData( (d3 = base64Data[dataIndex++]) )|| 229 !isData( (d4 = base64Data[dataIndex++]) )) 230 return null; 232 b1 = base64Alphabet[d1]; 233 b2 = base64Alphabet[d2]; 234 b3 = base64Alphabet[d3]; 235 b4 = base64Alphabet[d4]; 236 237 decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ; 238 decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 239 decodedData[encodedIndex++] = (byte)( b3<<6 | b4 ); 240 } 241 242 if (!isData( (d1 = base64Data[dataIndex++]) ) || 243 !isData( (d2 = base64Data[dataIndex++]) )) { 244 return null; } 246 247 b1 = base64Alphabet[d1]; 248 b2 = base64Alphabet[d2]; 249 250 d3 = base64Data[dataIndex++]; 251 d4 = base64Data[dataIndex++]; 252 if (!isData( (d3 ) ) || 253 !isData( (d4 ) )) { if (isPad( d3 ) && isPad( d4)) { if ((b2 & 0xf) != 0) return null; 257 byte[] tmp = new byte[ i*3 + 1 ]; 258 System.arraycopy( decodedData, 0, tmp, 0, i*3 ); 259 tmp[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ; 260 return tmp; 261 } else if (!isPad( d3) && isPad(d4)) { b3 = base64Alphabet[ d3 ]; 263 if ((b3 & 0x3 ) != 0) return null; 265 byte[] tmp = new byte[ i*3 + 2 ]; 266 System.arraycopy( decodedData, 0, tmp, 0, i*3 ); 267 tmp[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ); 268 tmp[encodedIndex] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 269 return tmp; 270 } else { 271 return null; } 273 } else { b3 = base64Alphabet[ d3 ]; 275 b4 = base64Alphabet[ d4 ]; 276 decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ; 277 decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 278 decodedData[encodedIndex++] = (byte)( b3<<6 | b4 ); 279 280 } 281 282 return decodedData; 283 } 284 285 291 protected static int removeWhiteSpace(char[] data) { 292 if (data == null) 293 return 0; 294 295 int newSize = 0; 297 int len = data.length; 298 for (int i = 0; i < len; i++) { 299 if (!isWhiteSpace(data[i])) 300 data[newSize++] = data[i]; 301 } 302 return newSize; 303 } 304 } 305 | Popular Tags |