Home | History | Annotate | Download | only in impl
      1 // =================================================================================================
      2 // ADOBE SYSTEMS INCORPORATED
      3 // Copyright 2001 Adobe Systems Incorporated
      4 // All Rights Reserved
      5 //
      6 // NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
      7 // of the Adobe license agreement accompanying it.
      8 // =================================================================================================
      9 
     10 package com.adobe.xmp.impl;
     11 
     12 
     13 
     14 
     15 /**
     16  * A utility class to perform base64 encoding and decoding as specified
     17  * in RFC-1521. See also RFC 1421.
     18  *
     19  * @version     $Revision: 1.4 $
     20  */
     21 public class  Base64
     22 {
     23 	/** marker for invalid bytes */
     24 	private static final byte INVALID = -1;
     25 	/** marker for accepted whitespace bytes */
     26 	private static final byte WHITESPACE = -2;
     27 	/** marker for an equal symbol */
     28 	private static final byte EQUAL = -3;
     29 
     30 	/** */
     31     private static byte[] base64 = {
     32         (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',   //  0 to  3
     33         (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',   //  4 to  7
     34         (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',   //  8 to 11
     35         (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',   // 11 to 15
     36         (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',   // 16 to 19
     37         (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',   // 20 to 23
     38         (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b',   // 24 to 27
     39         (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',   // 28 to 31
     40         (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',   // 32 to 35
     41         (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',   // 36 to 39
     42         (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r',   // 40 to 43
     43         (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',   // 44 to 47
     44         (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',   // 48 to 51
     45         (byte) '0', (byte) '1', (byte) '2', (byte) '3',   // 52 to 55
     46         (byte) '4', (byte) '5', (byte) '6', (byte) '7',   // 56 to 59
     47         (byte) '8', (byte) '9', (byte) '+', (byte) '/'    // 60 to 63
     48     };
     49     /** */
     50     private static byte[] ascii = new byte[255];
     51     /** */
     52     static {
     53     	// not valid bytes
     54         for (int idx = 0; idx < 255; idx++)
     55 		{
     56 			ascii[idx] = INVALID;
     57 		}
     58     	// valid bytes
     59 		for (int idx = 0; idx < base64.length; idx++)
     60 		{
     61 			ascii[base64[idx]] = (byte) idx;
     62 		}
     63 		// whitespaces
     64 		ascii[0x09] = WHITESPACE;
     65 		ascii[0x0A] = WHITESPACE;
     66 		ascii[0x0D] = WHITESPACE;
     67 		ascii[0x20] = WHITESPACE;
     68 
     69 		// trailing equals
     70 		ascii[0x3d] = EQUAL;
     71     }
     72 
     73 
     74     /**
     75 	 * Encode the given byte[].
     76 	 *
     77 	 * @param src the source string.
     78 	 * @return the base64-encoded data.
     79 	 */
     80     public static final byte[] encode(byte[] src)
     81 	{
     82     	return encode(src, 0);
     83 	}
     84 
     85 
     86     /**
     87 	 * Encode the given byte[].
     88 	 *
     89 	 * @param src the source string.
     90 	 * @param lineFeed a linefeed is added after <code>linefeed</code> characters;
     91 	 *            must be dividable by four; 0 means no linefeeds
     92 	 * @return the base64-encoded data.
     93 	 */
     94     public static final byte[] encode(byte[] src, int lineFeed)
     95 	{
     96     	// linefeed must be dividable by 4
     97     	lineFeed = lineFeed / 4 * 4;
     98     	if (lineFeed < 0)
     99     	{
    100     		lineFeed = 0;
    101     	}
    102 
    103 		// determine code length
    104     	int codeLength = ((src.length + 2) / 3) * 4;
    105 		if (lineFeed > 0)
    106 		{
    107 			codeLength += (codeLength - 1) / lineFeed;
    108 		}
    109 
    110 		byte[] dst = new byte[codeLength];
    111 		int bits24;
    112 		int bits6;
    113 		//
    114 		// Do 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
    115 		//
    116 		int didx = 0;
    117 		int sidx = 0;
    118 		int lf = 0;
    119 		while (sidx + 3 <= src.length)
    120 		{
    121             bits24  = (src[sidx++] & 0xFF) << 16;
    122             bits24 |= (src[sidx++] & 0xFF) <<  8;
    123             bits24 |= (src[sidx++] & 0xFF) <<  0;
    124             bits6   = (bits24 & 0x00FC0000) >> 18;
    125             dst[didx++] = base64[bits6];
    126             bits6   = (bits24 & 0x0003F000) >> 12;
    127             dst[didx++] = base64[bits6];
    128             bits6   = (bits24 & 0x00000FC0) >>  6;
    129             dst[didx++] = base64[bits6];
    130             bits6   = (bits24 & 0x0000003F);
    131             dst[didx++] = base64[bits6];
    132 
    133             lf += 4;
    134             if (didx < codeLength  &&  lineFeed > 0  &&  lf % lineFeed == 0)
    135             {
    136             	dst[didx++] = 0x0A;
    137             }
    138         }
    139         if (src.length - sidx == 2)
    140 		{
    141             bits24  = (src[sidx    ] & 0xFF) << 16;
    142             bits24 |= (src[sidx + 1] & 0xFF) <<  8;
    143             bits6 = (bits24 & 0x00FC0000) >> 18;
    144             dst[didx++] = base64[bits6];
    145             bits6 = (bits24 & 0x0003F000) >> 12;
    146             dst[didx++] = base64[bits6];
    147             bits6 = (bits24 & 0x00000FC0) >>  6;
    148             dst[didx++] = base64[bits6];
    149             dst[didx++] = (byte) '=';
    150         }
    151         else if (src.length - sidx == 1)
    152 		{
    153             bits24 = (src[sidx] & 0xFF) << 16;
    154             bits6  = (bits24 & 0x00FC0000) >> 18;
    155             dst[didx++] = base64[bits6];
    156             bits6  = (bits24 & 0x0003F000) >> 12;
    157             dst[didx++] = base64[bits6];
    158             dst[didx++] = (byte) '=';
    159             dst[didx++] = (byte) '=';
    160         }
    161         return dst;
    162     }
    163 
    164 
    165     /**
    166      * Encode the given string.
    167      * @param src the source string.
    168      * @return the base64-encoded string.
    169      */
    170     public static final String encode(String src)
    171 	{
    172 		return new String(encode(src.getBytes()));
    173 	}
    174 
    175 
    176     /**
    177 	 * Decode the given byte[].
    178 	 *
    179 	 * @param src
    180 	 *            the base64-encoded data.
    181 	 * @return the decoded data.
    182      * @throws IllegalArgumentException Thrown if the base 64 strings contains non-valid characters,
    183      * 		beside the bas64 chars, LF, CR, tab and space are accepted.
    184 	 */
    185     public static final byte[] decode(byte[] src) throws IllegalArgumentException
    186 	{
    187         //
    188         // Do ascii printable to 0-63 conversion.
    189         //
    190         int sidx;
    191         int srcLen = 0;
    192         for (sidx = 0; sidx < src.length; sidx++)
    193         {
    194         	byte val = ascii[src[sidx]];
    195         	if (val >= 0)
    196         	{
    197         		src[srcLen++] = val;
    198         	}
    199         	else if (val == INVALID)
    200         	{
    201         		throw new IllegalArgumentException("Invalid base 64 string");
    202         	}
    203         }
    204 
    205 		//
    206 		// Trim any padding.
    207 		//
    208 		while (srcLen > 0  &&  src[srcLen - 1] == EQUAL)
    209 		{
    210 			srcLen--;
    211 		}
    212 		byte[] dst = new byte[srcLen * 3 / 4];
    213 
    214 		//
    215         // Do 4-byte to 3-byte conversion.
    216         //
    217         int didx;
    218         for (sidx = 0, didx = 0; didx < dst.length - 2; sidx += 4, didx += 3)
    219         {
    220             dst[didx    ] = (byte) (((src[sidx    ] << 2) & 0xFF)
    221                             | ((src[sidx + 1] >>> 4) & 0x03));
    222             dst[didx + 1] = (byte) (((src[sidx + 1] << 4) & 0xFF)
    223                             | ((src[sidx + 2] >>> 2) & 0x0F));
    224             dst[didx + 2] = (byte) (((src[sidx + 2] << 6) & 0xFF)
    225                             | ((src[sidx + 3]) & 0x3F));
    226         }
    227         if (didx < dst.length)
    228         {
    229             dst[didx]     = (byte) (((src[sidx    ] << 2) & 0xFF)
    230                             | ((src[sidx + 1] >>> 4) & 0x03));
    231         }
    232         if (++didx < dst.length)
    233         {
    234             dst[didx]     = (byte) (((src[sidx + 1] << 4) & 0xFF)
    235                             | ((src[sidx + 2] >>> 2) & 0x0F));
    236         }
    237         return dst;
    238     }
    239 
    240 
    241     /**
    242 	 * Decode the given string.
    243 	 *
    244 	 * @param src the base64-encoded string.
    245 	 * @return the decoded string.
    246 	 */
    247 	public static final String decode(String src)
    248 	{
    249 		return new String(decode(src.getBytes()));
    250 	}
    251 }
    252