001: package org.postgresql.util;
002:
003: /**
004: * This code is a stripped down version of Robert Harder's Public Domain
005: * Base64 implementation. GZIP support, InputStream and OutputStream stuff
006: * and some unneeded encode/decode methods have been removed.
007: *
008: * -- Original comments follow --
009: *
010: * Encodes and decodes to and from Base64 notation.
011: *
012: * <p>
013: * Change Log:
014: * </p>
015: * <ul>
016: * <li>v2.1 - Cleaned up javadoc comments and unused variables and methods. Added
017: * some convenience methods for reading and writing to and from files.</li>
018: * <li>v2.0.2 - Now specifies UTF-8 encoding in places where the code fails on systems
019: * with other encodings (like EBCDIC).</li>
020: * <li>v2.0.1 - Fixed an error when decoding a single byte, that is, when the
021: * encoded data was a single byte.</li>
022: * <li>v2.0 - I got rid of methods that used booleans to set options.
023: * Now everything is more consolidated and cleaner. The code now detects
024: * when data that's being decoded is gzip-compressed and will decompress it
025: * automatically. Generally things are cleaner. You'll probably have to
026: * change some method calls that you were making to support the new
027: * options format (<tt>int</tt>s that you "OR" together).</li>
028: * <li>v1.5.1 - Fixed bug when decompressing and decoding to a
029: * byte[] using <tt>decode( String s, boolean gzipCompressed )</tt>.
030: * Added the ability to "suspend" encoding in the Output Stream so
031: * you can turn on and off the encoding if you need to embed base64
032: * data in an otherwise "normal" stream (like an XML file).</li>
033: * <li>v1.5 - Output stream pases on flush() command but doesn't do anything itself.
034: * This helps when using GZIP streams.
035: * Added the ability to GZip-compress objects before encoding them.</li>
036: * <li>v1.4 - Added helper methods to read/write files.</li>
037: * <li>v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.</li>
038: * <li>v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream
039: * where last buffer being read, if not completely full, was not returned.</li>
040: * <li>v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.</li>
041: * <li>v1.3.3 - Fixed I/O streams which were totally messed up.</li>
042: * </ul>
043: *
044: * <p>
045: * I am placing this code in the Public Domain. Do with it as you will.
046: * This software comes with no guarantees or warranties but with
047: * plenty of well-wishing instead!
048: * Please visit <a href="http://iharder.net/base64">http://iharder.net/base64</a>
049: * periodically to check for updates or to contribute improvements.
050: * </p>
051: *
052: * @author Robert Harder
053: * @author rob@iharder.net
054: * @version 2.1
055: */
056: public class Base64 {
057:
058: /* ******** P U B L I C F I E L D S ******** */
059:
060: /** No options specified. Value is zero. */
061: public final static int NO_OPTIONS = 0;
062:
063: /** Specify encoding. */
064: public final static int ENCODE = 1;
065:
066: /** Specify decoding. */
067: public final static int DECODE = 0;
068:
069: /** Don't break lines when encoding (violates strict Base64 specification) */
070: public final static int DONT_BREAK_LINES = 8;
071:
072: /* ******** P R I V A T E F I E L D S ******** */
073:
074: /** Maximum line length (76) of Base64 output. */
075: private final static int MAX_LINE_LENGTH = 76;
076:
077: /** The equals sign (=) as a byte. */
078: private final static byte EQUALS_SIGN = (byte) '=';
079:
080: /** The new line character (\n) as a byte. */
081: private final static byte NEW_LINE = (byte) '\n';
082:
083: /** Preferred encoding. */
084: private final static String PREFERRED_ENCODING = "UTF-8";
085:
086: /** The 64 valid Base64 values. */
087: private final static byte[] ALPHABET;
088: private final static byte[] _NATIVE_ALPHABET = /* May be something funny like EBCDIC */
089: { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
090: (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
091: (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
092: (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
093: (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
094: (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
095: (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
096: (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
097: (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
098: (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
099: (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
100: (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
101: (byte) '8', (byte) '9', (byte) '+', (byte) '/' };
102:
103: /** Determine which ALPHABET to use. */
104: static {
105: byte[] __bytes;
106: try {
107: __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
108: .getBytes(PREFERRED_ENCODING);
109: } // end try
110: catch (java.io.UnsupportedEncodingException use) {
111: __bytes = _NATIVE_ALPHABET; // Fall back to native encoding
112: } // end catch
113: ALPHABET = __bytes;
114: } // end static
115:
116: /**
117: * Translates a Base64 value to either its 6-bit reconstruction value
118: * or a negative number indicating some other meaning.
119: **/
120: private final static byte[] DECODABET = { -9, -9, -9, -9, -9, -9,
121: -9, -9, -9, // Decimal 0 - 8
122: -5, -5, // Whitespace: Tab and Linefeed
123: -9, -9, // Decimal 11 - 12
124: -5, // Whitespace: Carriage Return
125: -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
126: -9, -9, -9, -9, -9, // Decimal 27 - 31
127: -5, // Whitespace: Space
128: -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
129: 62, // Plus sign at decimal 43
130: -9, -9, -9, // Decimal 44 - 46
131: 63, // Slash at decimal 47
132: 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
133: -9, -9, -9, // Decimal 58 - 60
134: -1, // Equals sign at decimal 61
135: -9, -9, -9, // Decimal 62 - 64
136: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
137: 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
138: -9, -9, -9, -9, -9, -9, // Decimal 91 - 96
139: 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
140: 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
141: -9, -9, -9, -9 // Decimal 123 - 126
142: /*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
143: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
144: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
145: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
146: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 179 - 191
147: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 192 - 204
148: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
149: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
150: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
151: -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
152: };
153:
154: // I think I end up not using the BAD_ENCODING indicator.
155: //private final static byte BAD_ENCODING = -9; // Indicates error in encoding
156: private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
157: private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
158:
159: /** Defeats instantiation. */
160: private Base64() {
161: }
162:
163: /* ******** E N C O D I N G M E T H O D S ******** */
164:
165: /**
166: * Encodes up to the first three bytes of array <var>threeBytes</var>
167: * and returns a four-byte array in Base64 notation.
168: * The actual number of significant bytes in your array is
169: * given by <var>numSigBytes</var>.
170: * The array <var>threeBytes</var> needs only be as big as
171: * <var>numSigBytes</var>.
172: * Code can reuse a byte array by passing a four-byte array as <var>b4</var>.
173: *
174: * @param b4 A reusable byte array to reduce array instantiation
175: * @param threeBytes the array to convert
176: * @param numSigBytes the number of significant bytes in your array
177: * @return four byte array in Base64 notation.
178: * @since 1.5.1
179: */
180: private static byte[] encode3to4(byte[] b4, byte[] threeBytes,
181: int numSigBytes) {
182: encode3to4(threeBytes, 0, numSigBytes, b4, 0);
183: return b4;
184: } // end encode3to4
185:
186: /**
187: * Encodes up to three bytes of the array <var>source</var>
188: * and writes the resulting four Base64 bytes to <var>destination</var>.
189: * The source and destination arrays can be manipulated
190: * anywhere along their length by specifying
191: * <var>srcOffset</var> and <var>destOffset</var>.
192: * This method does not check to make sure your arrays
193: * are large enough to accomodate <var>srcOffset</var> + 3 for
194: * the <var>source</var> array or <var>destOffset</var> + 4 for
195: * the <var>destination</var> array.
196: * The actual number of significant bytes in your array is
197: * given by <var>numSigBytes</var>.
198: *
199: * @param source the array to convert
200: * @param srcOffset the index where conversion begins
201: * @param numSigBytes the number of significant bytes in your array
202: * @param destination the array to hold the conversion
203: * @param destOffset the index where output will be put
204: * @return the <var>destination</var> array
205: * @since 1.3
206: */
207: private static byte[] encode3to4(byte[] source, int srcOffset,
208: int numSigBytes, byte[] destination, int destOffset) {
209: // 1 2 3
210: // 01234567890123456789012345678901 Bit position
211: // --------000000001111111122222222 Array position from threeBytes
212: // --------| || || || | Six bit groups to index ALPHABET
213: // >>18 >>12 >> 6 >> 0 Right shift necessary
214: // 0x3f 0x3f 0x3f Additional AND
215:
216: // Create buffer with zero-padding if there are only one or two
217: // significant bytes passed in the array.
218: // We have to shift left 24 in order to flush out the 1's that appear
219: // when Java treats a value as negative that is cast from a byte to an int.
220: int inBuff = (numSigBytes > 0 ? ((source[srcOffset] << 24) >>> 8)
221: : 0)
222: | (numSigBytes > 1 ? ((source[srcOffset + 1] << 24) >>> 16)
223: : 0)
224: | (numSigBytes > 2 ? ((source[srcOffset + 2] << 24) >>> 24)
225: : 0);
226:
227: switch (numSigBytes) {
228: case 3:
229: destination[destOffset] = ALPHABET[(inBuff >>> 18)];
230: destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
231: destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f];
232: destination[destOffset + 3] = ALPHABET[(inBuff) & 0x3f];
233: return destination;
234:
235: case 2:
236: destination[destOffset] = ALPHABET[(inBuff >>> 18)];
237: destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
238: destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f];
239: destination[destOffset + 3] = EQUALS_SIGN;
240: return destination;
241:
242: case 1:
243: destination[destOffset] = ALPHABET[(inBuff >>> 18)];
244: destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
245: destination[destOffset + 2] = EQUALS_SIGN;
246: destination[destOffset + 3] = EQUALS_SIGN;
247: return destination;
248:
249: default:
250: return destination;
251: } // end switch
252: } // end encode3to4
253:
254: /**
255: * Encodes a byte array into Base64 notation.
256: * Does not GZip-compress data.
257: *
258: * @param source The data to convert
259: * @since 1.4
260: */
261: public static String encodeBytes(byte[] source) {
262: return encodeBytes(source, 0, source.length, NO_OPTIONS);
263: } // end encodeBytes
264:
265: /**
266: * Encodes a byte array into Base64 notation.
267: * <p>
268: * Valid options:<pre>
269: * GZIP: gzip-compresses object before encoding it.
270: * DONT_BREAK_LINES: don't break lines at 76 characters
271: * <i>Note: Technically, this makes your encoding non-compliant.</i>
272: * </pre>
273: * <p>
274: * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
275: * <p>
276: * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
277: *
278: *
279: * @param source The data to convert
280: * @param options Specified options
281: * @see Base64#DONT_BREAK_LINES
282: * @since 2.0
283: */
284: public static String encodeBytes(byte[] source, int options) {
285: return encodeBytes(source, 0, source.length, options);
286: } // end encodeBytes
287:
288: /**
289: * Encodes a byte array into Base64 notation.
290: * Does not GZip-compress data.
291: *
292: * @param source The data to convert
293: * @param off Offset in array where conversion should begin
294: * @param len Length of data to convert
295: * @since 1.4
296: */
297: public static String encodeBytes(byte[] source, int off, int len) {
298: return encodeBytes(source, off, len, NO_OPTIONS);
299: } // end encodeBytes
300:
301: /**
302: * Encodes a byte array into Base64 notation.
303: * <p>
304: * Valid options:<pre>
305: * GZIP: gzip-compresses object before encoding it.
306: * DONT_BREAK_LINES: don't break lines at 76 characters
307: * <i>Note: Technically, this makes your encoding non-compliant.</i>
308: * </pre>
309: * <p>
310: * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
311: * <p>
312: * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
313: *
314: *
315: * @param source The data to convert
316: * @param off Offset in array where conversion should begin
317: * @param len Length of data to convert
318: * @param options Specified options
319: * @see Base64#DONT_BREAK_LINES
320: * @since 2.0
321: */
322: public static String encodeBytes(byte[] source, int off, int len,
323: int options) {
324: // Isolate options
325: int dontBreakLines = (options & DONT_BREAK_LINES);
326:
327: // Else, don't compress. Better not to use streams at all then.
328: {
329: // Convert option to boolean in way that code likes it.
330: boolean breakLines = dontBreakLines == 0;
331:
332: int len43 = len * 4 / 3;
333: byte[] outBuff = new byte[(len43) // Main 4:3
334: + ((len % 3) > 0 ? 4 : 0) // Account for padding
335: + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines
336: int d = 0;
337: int e = 0;
338: int len2 = len - 2;
339: int lineLength = 0;
340: for (; d < len2; d += 3, e += 4) {
341: encode3to4(source, d + off, 3, outBuff, e);
342:
343: lineLength += 4;
344: if (breakLines && lineLength == MAX_LINE_LENGTH) {
345: outBuff[e + 4] = NEW_LINE;
346: e++;
347: lineLength = 0;
348: } // end if: end of line
349: } // en dfor: each piece of array
350:
351: if (d < len) {
352: encode3to4(source, d + off, len - d, outBuff, e);
353: e += 4;
354: } // end if: some padding needed
355:
356: // Return value according to relevant encoding.
357: try {
358: return new String(outBuff, 0, e, PREFERRED_ENCODING);
359: } // end try
360: catch (java.io.UnsupportedEncodingException uue) {
361: return new String(outBuff, 0, e);
362: } // end catch
363:
364: } // end else: don't compress
365:
366: } // end encodeBytes
367:
368: /* ******** D E C O D I N G M E T H O D S ******** */
369:
370: /**
371: * Decodes four bytes from array <var>source</var>
372: * and writes the resulting bytes (up to three of them)
373: * to <var>destination</var>.
374: * The source and destination arrays can be manipulated
375: * anywhere along their length by specifying
376: * <var>srcOffset</var> and <var>destOffset</var>.
377: * This method does not check to make sure your arrays
378: * are large enough to accomodate <var>srcOffset</var> + 4 for
379: * the <var>source</var> array or <var>destOffset</var> + 3 for
380: * the <var>destination</var> array.
381: * This method returns the actual number of bytes that
382: * were converted from the Base64 encoding.
383: *
384: *
385: * @param source the array to convert
386: * @param srcOffset the index where conversion begins
387: * @param destination the array to hold the conversion
388: * @param destOffset the index where output will be put
389: * @return the number of decoded bytes converted
390: * @since 1.3
391: */
392: private static int decode4to3(byte[] source, int srcOffset,
393: byte[] destination, int destOffset) {
394: // Example: Dk==
395: if (source[srcOffset + 2] == EQUALS_SIGN) {
396: // Two ways to do the same thing. Don't know which way I like best.
397: //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
398: // | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
399: int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
400: | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12);
401:
402: destination[destOffset] = (byte) (outBuff >>> 16);
403: return 1;
404: }
405:
406: // Example: DkL=
407: else if (source[srcOffset + 3] == EQUALS_SIGN) {
408: // Two ways to do the same thing. Don't know which way I like best.
409: //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
410: // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
411: // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );
412: int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
413: | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
414: | ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6);
415:
416: destination[destOffset] = (byte) (outBuff >>> 16);
417: destination[destOffset + 1] = (byte) (outBuff >>> 8);
418: return 2;
419: }
420:
421: // Example: DkLE
422: else {
423: try {
424: // Two ways to do the same thing. Don't know which way I like best.
425: //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
426: // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
427: // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )
428: // | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
429: int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
430: | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
431: | ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6)
432: | ((DECODABET[source[srcOffset + 3]] & 0xFF));
433:
434: destination[destOffset] = (byte) (outBuff >> 16);
435: destination[destOffset + 1] = (byte) (outBuff >> 8);
436: destination[destOffset + 2] = (byte) (outBuff);
437:
438: return 3;
439: } catch (Exception e) {
440: System.out.println("" + source[srcOffset] + ": "
441: + (DECODABET[source[srcOffset]]));
442: System.out.println("" + source[srcOffset + 1] + ": "
443: + (DECODABET[source[srcOffset + 1]]));
444: System.out.println("" + source[srcOffset + 2] + ": "
445: + (DECODABET[source[srcOffset + 2]]));
446: System.out.println("" + source[srcOffset + 3] + ": "
447: + (DECODABET[source[srcOffset + 3]]));
448: return -1;
449: } //e nd catch
450: }
451: } // end decodeToBytes
452:
453: /**
454: * Very low-level access to decoding ASCII characters in
455: * the form of a byte array. Does not support automatically
456: * gunzipping or any other "fancy" features.
457: *
458: * @param source The Base64 encoded data
459: * @param off The offset of where to begin decoding
460: * @param len The length of characters to decode
461: * @return decoded data
462: * @since 1.3
463: */
464: public static byte[] decode(byte[] source, int off, int len) {
465: int len34 = len * 3 / 4;
466: byte[] outBuff = new byte[len34]; // Upper limit on size of output
467: int outBuffPosn = 0;
468:
469: byte[] b4 = new byte[4];
470: int b4Posn = 0;
471: int i = 0;
472: byte sbiCrop = 0;
473: byte sbiDecode = 0;
474: for (i = off; i < off + len; i++) {
475: sbiCrop = (byte) (source[i] & 0x7f); // Only the low seven bits
476: sbiDecode = DECODABET[sbiCrop];
477:
478: if (sbiDecode >= WHITE_SPACE_ENC) // White space, Equals sign or better
479: {
480: if (sbiDecode >= EQUALS_SIGN_ENC) {
481: b4[b4Posn++] = sbiCrop;
482: if (b4Posn > 3) {
483: outBuffPosn += decode4to3(b4, 0, outBuff,
484: outBuffPosn);
485: b4Posn = 0;
486:
487: // If that was the equals sign, break out of 'for' loop
488: if (sbiCrop == EQUALS_SIGN)
489: break;
490: } // end if: quartet built
491:
492: } // end if: equals sign or better
493:
494: } // end if: white space, equals sign or better
495: else {
496: System.err.println("Bad Base64 input character at " + i
497: + ": " + source[i] + "(decimal)");
498: return null;
499: } // end else:
500: } // each input character
501:
502: byte[] out = new byte[outBuffPosn];
503: System.arraycopy(outBuff, 0, out, 0, outBuffPosn);
504: return out;
505: } // end decode
506:
507: /**
508: * Decodes data from Base64 notation, automatically
509: * detecting gzip-compressed data and decompressing it.
510: *
511: * @param s the string to decode
512: * @return the decoded data
513: * @since 1.4
514: */
515: public static byte[] decode(String s) {
516: byte[] bytes;
517: try {
518: bytes = s.getBytes(PREFERRED_ENCODING);
519: } // end try
520: catch (java.io.UnsupportedEncodingException uee) {
521: bytes = s.getBytes();
522: } // end catch
523: //</change>
524:
525: // Decode
526: bytes = decode(bytes, 0, bytes.length);
527:
528: return bytes;
529: } // end decode
530:
531: } // end class Base64
|