1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.StringReader ; 26 import java.math.BigInteger ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 31 import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.Text ; 36 import org.xml.sax.InputSource ; 37 38 39 50 public class Base64 { 51 52 53 static java.util.logging.Logger log = 54 java.util.logging.Logger.getLogger(Base64.class.getName()); 55 56 57 58 public static final int BASE64DEFAULTLENGTH = 76; 59 60 61 static int _base64length = Base64.BASE64DEFAULTLENGTH; 62 63 private Base64() { 64 } 66 67 78 static byte[] getBytes(BigInteger big, int bitlen) { 79 80 bitlen = ((bitlen + 7) >> 3) << 3; 82 83 if (bitlen < big.bitLength()) { 84 throw new IllegalArgumentException (I18n 85 .translate("utils.Base64.IllegalBitlength")); 86 } 87 88 byte[] bigBytes = big.toByteArray(); 89 90 if (((big.bitLength() % 8) != 0) 91 && (((big.bitLength() / 8) + 1) == (bitlen / 8))) { 92 return bigBytes; 93 } 94 95 int startSrc = 0; int bigLen = bigBytes.length; 99 if ((big.bitLength() % 8) == 0) { startSrc = 1; 102 bigLen--; } 104 105 int startDst = bitlen / 8 - bigLen; byte[] resizedBytes = new byte[bitlen / 8]; 107 108 System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, bigLen); 109 110 return resizedBytes; 111 112 } 113 114 120 public static String encode(BigInteger big) { 121 return encode(getBytes(big, big.bitLength())); 122 } 123 124 135 public static byte[] encode(BigInteger big, int bitlen) { 136 137 bitlen = ((bitlen + 7) >> 3) << 3; 139 140 if (bitlen < big.bitLength()) { 141 throw new IllegalArgumentException (I18n 142 .translate("utils.Base64.IllegalBitlength")); 143 } 144 145 byte[] bigBytes = big.toByteArray(); 146 147 if (((big.bitLength() % 8) != 0) 148 && (((big.bitLength() / 8) + 1) == (bitlen / 8))) { 149 return bigBytes; 150 } 151 152 int startSrc = 0; int bigLen = bigBytes.length; 156 if ((big.bitLength() % 8) == 0) { startSrc = 1; 159 bigLen--; } 161 162 int startDst = bitlen / 8 - bigLen; byte[] resizedBytes = new byte[bitlen / 8]; 164 165 System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, bigLen); 166 167 return resizedBytes; 168 169 } 170 171 178 public static BigInteger decodeBigIntegerFromElement(Element element) throws Base64DecodingException 179 { 180 return new BigInteger (1, Base64.decode(element)); 181 } 182 183 190 public static BigInteger decodeBigIntegerFromText(Text text) throws Base64DecodingException 191 { 192 return new BigInteger (1, Base64.decode(text.getData())); 193 } 194 195 202 public static void fillElementWithBigInteger(Element element, 203 BigInteger biginteger) { 204 205 String encodedInt = encode(biginteger); 206 207 if (encodedInt.length() > 76) { 208 encodedInt = "\n" + encodedInt + "\n"; 209 } 210 211 Document doc = element.getOwnerDocument(); 212 Text text = doc.createTextNode(encodedInt); 213 214 element.appendChild(text); 215 } 216 217 228 public static byte[] decode(Element element) throws Base64DecodingException { 229 230 Node sibling = element.getFirstChild(); 231 StringBuffer sb = new StringBuffer (); 232 233 while (sibling!=null) { 234 if (sibling.getNodeType() == Node.TEXT_NODE) { 235 Text t = (Text ) sibling; 236 237 sb.append(t.getData()); 238 } 239 sibling=sibling.getNextSibling(); 240 } 241 242 return decode(sb.toString()); 243 } 244 245 254 public static Element encodeToElement(Document doc, String localName, 255 byte[] bytes) { 256 257 Element el = XMLUtils.createElementInSignatureSpace(doc, localName); 258 Text text = doc.createTextNode(encode(bytes)); 259 260 el.appendChild(text); 261 262 return el; 263 } 264 265 274 public static byte[] decode(byte[] base64) throws Base64DecodingException { 275 return decodeInternal(base64); 276 } 277 278 279 280 286 public static String encode(byte[] binaryData) { 287 return encode(binaryData,BASE64DEFAULTLENGTH); 288 } 289 290 301 public static byte[] decode(BufferedReader reader) 302 throws IOException , Base64DecodingException { 303 304 UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream(); 305 String line; 306 307 while (null != (line = reader.readLine())) { 308 byte[] bytes = decode(line); 309 310 baos.write(bytes); 311 } 312 313 return baos.toByteArray(); 314 } 315 316 324 public static void main(String [] args) throws Exception { 325 326 DocumentBuilderFactory docBuilderFactory = 327 DocumentBuilderFactory.newInstance(); 328 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 329 String testString1 = 330 "<container><base64 value=\"Should be 'Hallo'\">SGFsbG8=</base64></container>"; 331 InputSource inputSource = new InputSource (new StringReader (testString1)); 332 Document doc = docBuilder.parse(inputSource); 333 Element base64Elem = 334 (Element ) doc.getDocumentElement().getChildNodes().item(0); 335 336 System.out.println(new String (decode(base64Elem))); 337 } 338 static private final int BASELENGTH = 255; 339 static private final int LOOKUPLENGTH = 64; 340 static private final int TWENTYFOURBITGROUP = 24; 341 static private final int EIGHTBIT = 8; 342 static private final int SIXTEENBIT = 16; 343 static private final int FOURBYTE = 4; 344 static private final int SIGN = -128; 345 static private final char PAD = '='; 346 static private final boolean fDebug = false; 347 static final private byte [] base64Alphabet = new byte[BASELENGTH]; 348 static final private char [] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; 349 350 static { 351 352 for (int i = 0; i<BASELENGTH; i++) { 353 base64Alphabet[i] = -1; 354 } 355 for (int i = 'Z'; i >= 'A'; i--) { 356 base64Alphabet[i] = (byte) (i-'A'); 357 } 358 for (int i = 'z'; i>= 'a'; i--) { 359 base64Alphabet[i] = (byte) ( i-'a' + 26); 360 } 361 362 for (int i = '9'; i >= '0'; i--) { 363 base64Alphabet[i] = (byte) (i-'0' + 52); 364 } 365 366 base64Alphabet['+'] = 62; 367 base64Alphabet['/'] = 63; 368 369 for (int i = 0; i<=25; i++) 370 lookUpBase64Alphabet[i] = (char)('A'+i); 371 372 for (int i = 26, j = 0; i<=51; i++, j++) 373 lookUpBase64Alphabet[i] = (char)('a'+ j); 374 375 for (int i = 52, j = 0; i<=61; i++, j++) 376 lookUpBase64Alphabet[i] = (char)('0' + j); 377 lookUpBase64Alphabet[62] = '+'; 378 lookUpBase64Alphabet[63] = '/'; 379 380 } 381 382 protected static final boolean isWhiteSpace(byte octect) { 383 return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); 384 } 385 386 protected static final boolean isPad(byte octect) { 387 return (octect == PAD); 388 } 389 390 391 397 405 public static String encode(byte[] binaryData,int length) { 406 407 if (length<4) { 408 length=Integer.MAX_VALUE; 409 } 410 411 if (binaryData == null) 412 return null; 413 414 int lengthDataBits = binaryData.length*EIGHTBIT; 415 if (lengthDataBits == 0) { 416 return ""; 417 } 418 419 int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP; 420 int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP; 421 int numberQuartet = fewerThan24bits != 0 ? numberTriplets+1 : numberTriplets; 422 int quartesPerLine = length/4; 423 int numberLines = (numberQuartet-1)/quartesPerLine; 424 char encodedData[] = null; 425 426 encodedData = new char[numberQuartet*4+numberLines]; 427 428 byte k=0, l=0, b1=0,b2=0,b3=0; 429 430 int encodedIndex = 0; 431 int dataIndex = 0; 432 int i = 0; 433 if (fDebug) { 434 System.out.println("number of triplets = " + numberTriplets ); 435 } 436 437 for (int line = 0; line < numberLines; line++) { 438 for (int quartet = 0; quartet < 19; quartet++) { 439 b1 = binaryData[dataIndex++]; 440 b2 = binaryData[dataIndex++]; 441 b3 = binaryData[dataIndex++]; 442 443 if (fDebug) { 444 System.out.println( "b1= " + b1 +", b2= " + b2 + ", b3= " + b3 ); 445 } 446 447 l = (byte)(b2 & 0x0f); 448 k = (byte)(b1 & 0x03); 449 450 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 451 452 byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 453 byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc); 454 455 if (fDebug) { 456 System.out.println( "val2 = " + val2 ); 457 System.out.println( "k4 = " + (k<<4)); 458 System.out.println( "vak = " + (val2 | (k<<4))); 459 } 460 461 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 462 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )]; 463 encodedData[encodedIndex++] = lookUpBase64Alphabet[ (l <<2 ) | val3 ]; 464 encodedData[encodedIndex++] = lookUpBase64Alphabet[ b3 & 0x3f ]; 465 466 i++; 467 } 468 encodedData[encodedIndex++] = 0xa; 469 } 470 471 for (; i<numberTriplets; i++) { 472 b1 = binaryData[dataIndex++]; 473 b2 = binaryData[dataIndex++]; 474 b3 = binaryData[dataIndex++]; 475 476 if (fDebug) { 477 System.out.println( "b1= " + b1 +", b2= " + b2 + ", b3= " + b3 ); 478 } 479 480 l = (byte)(b2 & 0x0f); 481 k = (byte)(b1 & 0x03); 482 483 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 484 485 byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 486 byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc); 487 488 if (fDebug) { 489 System.out.println( "val2 = " + val2 ); 490 System.out.println( "k4 = " + (k<<4)); 491 System.out.println( "vak = " + (val2 | (k<<4))); 492 } 493 494 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 495 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )]; 496 encodedData[encodedIndex++] = lookUpBase64Alphabet[ (l <<2 ) | val3 ]; 497 encodedData[encodedIndex++] = lookUpBase64Alphabet[ b3 & 0x3f ]; 498 } 499 500 if (fewerThan24bits == EIGHTBIT) { 502 b1 = binaryData[dataIndex]; 503 k = (byte) ( b1 &0x03 ); 504 if (fDebug) { 505 System.out.println("b1=" + b1); 506 System.out.println("b1<<2 = " + (b1>>2) ); 507 } 508 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 509 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 510 encodedData[encodedIndex++] = lookUpBase64Alphabet[ k<<4 ]; 511 encodedData[encodedIndex++] = PAD; 512 encodedData[encodedIndex++] = PAD; 513 } else if (fewerThan24bits == SIXTEENBIT) { 514 b1 = binaryData[dataIndex]; 515 b2 = binaryData[dataIndex +1 ]; 516 l = ( byte ) ( b2 &0x0f ); 517 k = ( byte ) ( b1 &0x03 ); 518 519 byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0); 520 byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0); 521 522 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ]; 523 encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )]; 524 encodedData[encodedIndex++] = lookUpBase64Alphabet[ l<<2 ]; 525 encodedData[encodedIndex++] = PAD; 526 } 527 528 530 return new String (encodedData); 531 } 532 533 540 public final static byte[] decode(String encoded) throws Base64DecodingException { 541 542 if (encoded == null) 543 return null; 544 545 return decodeInternal(encoded.getBytes()); 546 } 547 protected final static byte[] decodeInternal(byte[] base64Data) throws Base64DecodingException { 548 int len = removeWhiteSpace(base64Data); 550 551 if (len%FOURBYTE != 0) { 552 throw new Base64DecodingException("decoding.divisible.four"); 553 } 555 556 int numberQuadruple = (len/FOURBYTE ); 557 558 if (numberQuadruple == 0) 559 return new byte[0]; 560 561 byte decodedData[] = null; 562 byte b1=0,b2=0,b3=0, b4=0; 563 564 565 int i = 0; 566 int encodedIndex = 0; 567 int dataIndex = 0; 568 569 dataIndex=(numberQuadruple-1)*4; 571 encodedIndex=(numberQuadruple-1)*3; 572 b1 = base64Alphabet[base64Data[dataIndex++]]; 574 b2 = base64Alphabet[base64Data[dataIndex++]]; 575 if ((b1==-1) || (b2==-1)) { 576 throw new Base64DecodingException("decoding.general"); } 578 579 580 byte d3,d4; 581 b3 = base64Alphabet[d3=base64Data[dataIndex++]]; 582 b4 = base64Alphabet[d4=base64Data[dataIndex++]]; 583 if ((b3==-1 ) || (b4==-1) ) { 584 if (isPad( d3 ) && isPad( d4)) { if ((b2 & 0xf) != 0) throw new Base64DecodingException("decoding.general"); 588 decodedData = new byte[ encodedIndex + 1 ]; 589 decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ; 590 } else if (!isPad( d3) && isPad(d4)) { if ((b3 & 0x3 ) != 0) throw new Base64DecodingException("decoding.general"); 593 decodedData = new byte[ encodedIndex + 2 ]; 594 decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ); 595 decodedData[encodedIndex] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 596 } else { 597 throw new Base64DecodingException("decoding.general"); } 599 } else { 600 decodedData = new byte[encodedIndex+3]; 602 decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ; 603 decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 604 decodedData[encodedIndex++] = (byte)( b3<<6 | b4 ); 605 } 606 encodedIndex=0; 607 dataIndex=0; 608 for (i=numberQuadruple-1; i>0; i--) { 610 b1 = base64Alphabet[base64Data[dataIndex++]]; 611 b2 = base64Alphabet[base64Data[dataIndex++]]; 612 b3 = base64Alphabet[base64Data[dataIndex++]]; 613 b4 = base64Alphabet[base64Data[dataIndex++]]; 614 615 if ( (b1==-1) || 616 (b2==-1) || 617 (b3==-1) || 618 (b4==-1) ) { 619 throw new Base64DecodingException("decoding.general"); } 621 622 decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ; 623 decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ); 624 decodedData[encodedIndex++] = (byte)( b3<<6 | b4 ); 625 } 626 return decodedData; 627 } 628 629 637 public final static void decode(byte[] base64Data, 638 OutputStream os) throws Base64DecodingException, IOException { 639 int len = removeWhiteSpace(base64Data); 641 642 if (len%FOURBYTE != 0) { 643 throw new Base64DecodingException("decoding.divisible.four"); 644 } 646 647 int numberQuadruple = (len/FOURBYTE ); 648 649 if (numberQuadruple == 0) 650 return; 651 652 byte b1=0,b2=0,b3=0, b4=0; 654 655 int i = 0; 656 657 int dataIndex = 0; 658 659 for (i=numberQuadruple-1; i>0; i--) { 661 b1 = base64Alphabet[base64Data[dataIndex++]]; 662 b2 = base64Alphabet[base64Data[dataIndex++]]; 663 b3 = base64Alphabet[base64Data[dataIndex++]]; 664 b4 = base64Alphabet[base64Data[dataIndex++]]; 665 if ( (b1==-1) || 666 (b2==-1) || 667 (b3==-1) || 668 (b4==-1) ) 669 throw new Base64DecodingException("decoding.general"); 671 672 673 os.write((byte)( b1 <<2 | b2>>4 ) ); 674 os.write((byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) )); 675 os.write( (byte)( b3<<6 | b4 )); 676 } 677 b1 = base64Alphabet[base64Data[dataIndex++]]; 678 b2 = base64Alphabet[base64Data[dataIndex++]]; 679 680 if ((b1==-1) || 682 (b2==-1) ){ 683 throw new Base64DecodingException("decoding.general"); } 685 686 byte d3,d4; 687 b3= base64Alphabet[d3 = base64Data[dataIndex++]]; 688 b4= base64Alphabet[d4 = base64Data[dataIndex++]]; 689 if ((b3==-1 ) || 690 (b4==-1) ) { if (isPad( d3 ) && isPad( d4)) { if ((b2 & 0xf) != 0) throw new Base64DecodingException("decoding.general"); 694 os.write( (byte)( b1 <<2 | b2>>4 ) ); 695 } else if (!isPad( d3) && isPad(d4)) { if ((b3 & 0x3 ) != 0) throw new Base64DecodingException("decoding.general"); 698 os.write( (byte)( b1 <<2 | b2>>4 )); 699 os.write( (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) )); 700 } else { 701 throw new Base64DecodingException("decoding.general"); } 703 } else { 704 os.write((byte)( b1 <<2 | b2>>4 ) ); 706 os.write( (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) )); 707 os.write((byte)( b3<<6 | b4 )); 708 } 709 return ; 710 } 711 712 720 public final static void decode(InputStream is, 721 OutputStream os) throws Base64DecodingException, IOException { 722 byte b1=0,b2=0,b3=0, b4=0; 724 725 int index=0; 726 byte []data=new byte[4]; 727 int read; 728 while ((read=is.read())>0) { 730 byte readed=(byte)read; 731 if (isWhiteSpace(readed)) { 732 continue; 733 } 734 if (isPad(readed)) { 735 data[index++]=readed; 736 if (index==3) 737 data[index++]=(byte)is.read(); 738 break; 739 } 740 741 742 if ((data[index++]=readed)==-1) { 743 throw new Base64DecodingException("decoding.general"); } 745 746 if (index!=4) { 747 continue; 748 } 749 index=0; 750 b1 = base64Alphabet[data[0]]; 751 b2 = base64Alphabet[data[1]]; 752 b3 = base64Alphabet[data[2]]; 753 b4 = base64Alphabet[data[3]]; 754 755 os.write((byte)( b1 <<2 | b2>>4 ) ); 756 os.write((byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) )); 757 os.write( (byte)( b3<<6 | b4 )); 758 } 759 760 761 byte d1=data[0],d2=data[1],d3=data[2], d4=data[3]; 762 b1 = base64Alphabet[d1]; 763 b2 = base64Alphabet[d2]; 764 b3 = base64Alphabet[ d3 ]; 765 b4 = base64Alphabet[ d4 ]; 766 if ((b3==-1 ) || 767 (b4==-1) ) { if (isPad( d3 ) && isPad( d4)) { if ((b2 & 0xf) != 0) throw new Base64DecodingException("decoding.general"); 771 os.write( (byte)( b1 <<2 | b2>>4 ) ); 772 } else if (!isPad( d3) && isPad(d4)) { b3 = base64Alphabet[ d3 ]; 774 if ((b3 & 0x3 ) != 0) throw new Base64DecodingException("decoding.general"); 776 os.write( (byte)( b1 <<2 | b2>>4 )); 777 os.write( (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) )); 778 } else { 779 throw new Base64DecodingException("decoding.general"); } 781 } else { 782 784 os.write((byte)( b1 <<2 | b2>>4 ) ); 785 os.write( (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) )); 786 os.write((byte)( b3<<6 | b4 )); 787 } 788 789 return ; 790 } 791 797 protected static int removeWhiteSpace(byte[] data) { 798 if (data == null) 799 return 0; 800 801 int newSize = 0; 803 int len = data.length; 804 for (int i = 0; i < len; i++) { 805 byte dataS=data[i]; 806 if (!isWhiteSpace(dataS)) 807 data[newSize++] = dataS; 808 } 809 return newSize; 810 } 811 } 812 | Popular Tags |