Source for gnu.java.security.util.Base64

   1: /* Base64.java -- 
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.java.security.util;
  40: 
  41: import gnu.java.security.Configuration;
  42: 
  43: import java.io.UnsupportedEncodingException;
  44: import java.util.logging.Logger;
  45: 
  46: /**
  47:  * Most of this implementation is from Robert Harder's public domain Base64
  48:  * code (version 1.4.1 available from <http://iharder.net/xmlizable>).
  49:  */
  50: public class Base64
  51: {
  52:   private static final Logger log = Logger.getLogger(Base64.class.getName());
  53: 
  54:   /** Maximum line length (76) of Base64 output. */
  55:   private static final int MAX_LINE_LENGTH = 76;
  56: 
  57:   /** The new line character (\n) as one byte. */
  58:   private static final byte NEW_LINE = (byte) '\n';
  59: 
  60:   /** The equals sign (=) as a byte. */
  61:   private static final byte EQUALS_SIGN = (byte) '=';
  62: 
  63:   private static final byte WHITE_SPACE_ENC = -5; // white space in encoding
  64: 
  65:   private static final byte EQUALS_SIGN_ENC = -1; // equals sign in encoding
  66: 
  67:   /** The 64 valid Base64 values. */
  68:   private static final byte[] ALPHABET = {
  69:       (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
  70:       (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
  71:       (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R',
  72:       (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
  73:       (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
  74:       (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',
  75:       (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
  76:       (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
  77:       (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1',
  78:       (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
  79:       (byte) '8', (byte) '9', (byte) '+', (byte) '/'
  80:   };
  81: 
  82:   /**
  83:    * Translates a Base64 value to either its 6-bit reconstruction value or a
  84:    * negative number indicating some other meaning.
  85:    */
  86:   private static final byte[] DECODABET = {
  87:       -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8
  88:       -5, -5, // Whitespace: Tab and Linefeed
  89:       -9, -9, // Decimal 11 - 12
  90:       -5, // Whitespace: Carriage Return
  91:       -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
  92:       -9, -9, -9, -9, -9, // Decimal 27 - 31
  93:       -5, // Whitespace: Space
  94:       -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
  95:       62, // Plus sign at decimal 43
  96:       -9, -9, -9, // Decimal 44 - 46
  97:       63, // Slash at decimal 47
  98:       52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
  99:       -9, -9, -9, // Decimal 58 - 60
 100:       -1, // Equals sign at decimal 61
 101:       -9, -9, -9, // Decimal 62 - 64
 102:       0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
 103:       14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
 104:       -9, -9, -9, -9, -9, -9, // Decimal 91 - 96
 105:       26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
 106:       39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
 107:       -9, -9, -9, -9 // Decimal 123 - 126
 108:   };
 109: 
 110:   /** Trivial private ctor to enfore Singleton pattern. */
 111:   private Base64()
 112:   {
 113:     super();
 114:   }
 115: 
 116:   /**
 117:    * Encodes a byte array into Base64 notation. Equivalent to calling
 118:    * <code>encode(source, 0, source.length)</code>.
 119:    * 
 120:    * @param src the data to convert.
 121:    */
 122:   public static final String encode(final byte[] src)
 123:   {
 124:     return encode(src, 0, src.length, true);
 125:   }
 126: 
 127:   /**
 128:    * Encodes a byte array into Base64 notation.
 129:    * 
 130:    * @param src the data to convert.
 131:    * @param off offset in array where conversion should begin.
 132:    * @param len length of data to convert.
 133:    * @param breakLines break lines at 80 characters or less.
 134:    */
 135:   public static final String encode(final byte[] src, final int off,
 136:                                     final int len, final boolean breakLines)
 137:   {
 138:     final int len43 = len * 4 / 3;
 139:     final byte[] outBuff = new byte[len43 // Main 4:3
 140:                                     + ((len % 3) > 0 ? 4 : 0) // Account for padding
 141:                                     + (breakLines ? (len43 / MAX_LINE_LENGTH)
 142:                                                   : 0)]; // New lines
 143:     int d = 0;
 144:     int e = 0;
 145:     final int len2 = len - 2;
 146:     int lineLength = 0;
 147:     for (; d < len2; d += 3, e += 4)
 148:       {
 149:         encode3to4(src, d + off, 3, outBuff, e);
 150:         lineLength += 4;
 151:         if (breakLines && lineLength == MAX_LINE_LENGTH)
 152:           {
 153:             outBuff[e + 4] = NEW_LINE;
 154:             e++;
 155:             lineLength = 0;
 156:           }
 157:       }
 158:     if (d < len) // padding needed
 159:       {
 160:         encode3to4(src, d + off, len - d, outBuff, e);
 161:         e += 4;
 162:       }
 163:     return new String(outBuff, 0, e);
 164:   }
 165: 
 166:   /**
 167:    * Decodes data from Base64 notation.
 168:    *
 169:    * @param s the string to decode.
 170:    * @return the decoded data.
 171:    */
 172:   public static final byte[] decode(final String s)
 173:       throws UnsupportedEncodingException
 174:   {
 175:     final byte[] bytes;
 176:     bytes = s.getBytes("US-ASCII");
 177:     return decode(bytes, 0, bytes.length);
 178:   }
 179: 
 180:   /**
 181:    * Decodes Base64 content in byte array format and returns the decoded byte
 182:    * array.
 183:    *
 184:    * @param src the Base64 encoded data.
 185:    * @param off the offset of where to begin decoding.
 186:    * @param len the length of characters to decode.
 187:    * @return the decoded data.
 188:    * @throws IllegalArgumentException if <code>src</code> contains an illegal
 189:    * Base-64 character.
 190:    */
 191:   public static byte[] decode(final byte[] src, final int off, final int len)
 192:   {
 193:     final int len34 = len * 3 / 4;
 194:     final byte[] outBuff = new byte[len34]; // Upper limit on size of output
 195:     int outBuffPosn = 0;
 196:     final byte[] b4 = new byte[4];
 197:     int b4Posn = 0;
 198:     int i;
 199:     byte sbiCrop, sbiDecode;
 200:     for (i = off; i < off + len; i++)
 201:       {
 202:         sbiCrop = (byte) (src[i] & 0x7F); // Only the low seven bits
 203:         sbiDecode = DECODABET[sbiCrop];
 204:         if (sbiDecode >= WHITE_SPACE_ENC)
 205:           { // White space, Equals sign or better
 206:             if (sbiDecode >= EQUALS_SIGN_ENC)
 207:               {
 208:                 b4[b4Posn++] = sbiCrop;
 209:                 if (b4Posn > 3)
 210:                   {
 211:                     outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);
 212:                     b4Posn = 0;
 213:                     // If that was the equals sign, break out of 'for' loop
 214:                     if (sbiCrop == EQUALS_SIGN)
 215:                       break;
 216:                   } // end if: quartet built
 217:               } // end if: equals sign or better
 218:           }
 219:         throw new IllegalArgumentException("Illegal BASE-64 character at #"
 220:                                            + i + ": " + src[i] + "(decimal)");
 221:       }
 222:     final byte[] result = new byte[outBuffPosn];
 223:     System.arraycopy(outBuff, 0, result, 0, outBuffPosn);
 224:     return result;
 225:   }
 226: 
 227:   /**
 228:    * Encodes up to three bytes of the array <code>src</code> and writes the
 229:    * resulting four Base64 bytes to <code>dest</code>. The source and
 230:    * destination arrays can be manipulated anywhere along their length by
 231:    * specifying <code>sOffset</code> and <code>dOffset</code>.
 232:    * <p>
 233:    * This method does not check to make sure the arrays are large enough to
 234:    * accomodate <code>sOffset + 3</code> for the <code>src</code> array or
 235:    * <code>dOffset + 4</code> for the <code>dest</code> array. The actual
 236:    * number of significant bytes in the input array is given by
 237:    * <code>numBytes</code>.
 238:    * 
 239:    * @param src the array to convert.
 240:    * @param sOffset the index where conversion begins.
 241:    * @param numBytes the number of significant bytes in your array.
 242:    * @param dest the array to hold the conversion.
 243:    * @param dOffset the index where output will be put.
 244:    * @return the <code>destination</code> array.
 245:    */
 246:   private static final byte[] encode3to4(final byte[] src, final int sOffset,
 247:                                          final int numBytes, final byte[] dest,
 248:                                          final int dOffset)
 249:   {
 250:     //           1         2         3
 251:     // 01234567890123456789012345678901 Bit position
 252:     // --------000000001111111122222222 Array position from threeBytes
 253:     // --------|    ||    ||    ||    | Six bit groups to index ALPHABET
 254:     //          >>18  >>12  >> 6  >> 0  Right shift necessary
 255:     //                0x3F  0x3F  0x3F  Additional AND
 256: 
 257:     // Create buffer with zero-padding if there are only one or two
 258:     // significant bytes passed in the array.
 259:     // We have to shift left 24 in order to flush out the 1's that appear
 260:     // when Java treats a value as negative that is cast from a byte to an int.
 261:     final int inBuff =   (numBytes > 0 ? ((src[sOffset]     << 24) >>>  8) : 0)
 262:                        | (numBytes > 1 ? ((src[sOffset + 1] << 24) >>> 16) : 0)
 263:                        | (numBytes > 2 ? ((src[sOffset + 2] << 24) >>> 24) : 0);
 264:     switch (numBytes)
 265:       {
 266:       case 3:
 267:         dest[dOffset    ] = ALPHABET[(inBuff >>> 18)];
 268:         dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];
 269:         dest[dOffset + 2] = ALPHABET[(inBuff >>>  6) & 0x3F];
 270:         dest[dOffset + 3] = ALPHABET[(inBuff)        & 0x3F];
 271:         break;
 272:       case 2:
 273:         dest[dOffset    ] = ALPHABET[(inBuff >>> 18)];
 274:         dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];
 275:         dest[dOffset + 2] = ALPHABET[(inBuff >>>  6) & 0x3F];
 276:         dest[dOffset + 3] = EQUALS_SIGN;
 277:         break;
 278:       case 1:
 279:         dest[dOffset    ] = ALPHABET[(inBuff >>> 18)];
 280:         dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];
 281:         dest[dOffset + 2] = EQUALS_SIGN;
 282:         dest[dOffset + 3] = EQUALS_SIGN;
 283:         break;
 284:       }
 285:     return dest;
 286:   }
 287: 
 288:   /**
 289:    * Decodes four bytes from array <code>src</code> and writes the resulting
 290:    * bytes (up to three of them) to <code>dest</code>.
 291:    * <p>
 292:    * The source and destination arrays can be manipulated anywhere along their
 293:    * length by specifying <code>sOffset</code> and <code>dOffset</code>.
 294:    * <p>
 295:    * This method does not check to make sure your arrays are large enough to
 296:    * accomodate <code>sOffset + 4</code> for the <code>src</code> array or
 297:    * <code>dOffset + 3</code> for the <code>dest</code> array. This method
 298:    * returns the actual number of bytes that were converted from the Base64
 299:    * encoding.
 300:    * 
 301:    * @param src the array to convert.
 302:    * @param sOffset the index where conversion begins.
 303:    * @param dest the array to hold the conversion.
 304:    * @param dOffset the index where output will be put.
 305:    * @return the number of decoded bytes converted.
 306:    */
 307:   private static final int decode4to3(final byte[] src, final int sOffset,
 308:                                       final byte[] dest, final int dOffset)
 309:   {
 310:     if (src[sOffset + 2] == EQUALS_SIGN) // Example: Dk==
 311:       {
 312:         final int outBuff = ((DECODABET[src[sOffset    ]] & 0xFF) << 18)
 313:                           | ((DECODABET[src[sOffset + 1]] & 0xFF) << 12);
 314:         dest[dOffset] = (byte)(outBuff >>> 16);
 315:         return 1;
 316:       }
 317:     if (src[sOffset + 3] == EQUALS_SIGN) // Example: DkL=
 318:       {
 319:         final int outBuff = ((DECODABET[src[sOffset    ]] & 0xFF) << 18)
 320:                           | ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)
 321:                           | ((DECODABET[src[sOffset + 2]] & 0xFF) << 6);
 322:         dest[dOffset    ] = (byte)(outBuff >>> 16);
 323:         dest[dOffset + 1] = (byte)(outBuff >>> 8);
 324:         return 2;
 325:       }
 326:     try // Example: DkLE
 327:       {
 328:         final int outBuff = ((DECODABET[src[sOffset    ]] & 0xFF) << 18)
 329:                           | ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)
 330:                           | ((DECODABET[src[sOffset + 2]] & 0xFF) << 6)
 331:                           | ((DECODABET[src[sOffset + 3]] & 0xFF));
 332:         dest[dOffset    ] = (byte)(outBuff >> 16);
 333:         dest[dOffset + 1] = (byte)(outBuff >> 8);
 334:         dest[dOffset + 2] = (byte) outBuff;
 335:         return 3;
 336:       }
 337:     catch (Exception x)
 338:       {
 339:         if (Configuration.DEBUG)
 340:           {
 341:             log.fine("" + src[sOffset    ] + ": " + (DECODABET[src[sOffset    ]]));
 342:             log.fine("" + src[sOffset + 1] + ": " + (DECODABET[src[sOffset + 1]]));
 343:             log.fine("" + src[sOffset + 2] + ": " + (DECODABET[src[sOffset + 2]]));
 344:             log.fine("" + src[sOffset + 3] + ": " + (DECODABET[src[sOffset + 3]]));
 345:           }
 346:         return -1;
 347:       }
 348:   }
 349: }