001: /**
002: * This code is copied from the OpenJDK version 7 java.util.prefs.Base64
003: * implementation, and retains its original license, as shown in full below:
004: *
005: * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
007: *
008: * This code is free software; you can redistribute it and/or modify it
009: * under the terms of the GNU General Public License version 2 only, as
010: * published by the Free Software Foundation. Sun designates this
011: * particular file as subject to the "Classpath" exception as provided
012: * by Sun in the LICENSE file that accompanied this code.
013: *
014: * This code is distributed in the hope that it will be useful, but WITHOUT
015: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
016: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
017: * version 2 for more details (a copy is included in the LICENSE file that
018: * accompanied this code).
019: *
020: * You should have received a copy of the GNU General Public License version
021: * 2 along with this work; if not, write to the Free Software Foundation,
022: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
023: *
024: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
025: * CA 95054 USA or visit www.sun.com if you need additional information or
026: * have any questions.
027: */package org.netbeans.modules.db.util;
028:
029: /**
030: * Static methods for translating Base64 encoded strings to byte arrays
031: * and vice-versa.
032: *
033: * @author Josh Bloch
034: * @version 1.13, 05/05/07
035: * @see Preferences
036: * @since 1.4
037: */
038: public class Base64 {
039: /**
040: * Translates the specified byte array into a Base64 string as per
041: * Preferences.put(byte[]).
042: */
043: public static String byteArrayToBase64(byte[] a) {
044: return byteArrayToBase64(a, false);
045: }
046:
047: /**
048: * Translates the specified byte array into an "alternate representation"
049: * Base64 string. This non-standard variant uses an alphabet that does
050: * not contain the uppercase alphabetic characters, which makes it
051: * suitable for use in situations where case-folding occurs.
052: */
053: public static String byteArrayToAltBase64(byte[] a) {
054: return byteArrayToBase64(a, true);
055: }
056:
057: private static String byteArrayToBase64(byte[] a, boolean alternate) {
058: int aLen = a.length;
059: int numFullGroups = aLen / 3;
060: int numBytesInPartialGroup = aLen - 3 * numFullGroups;
061: int resultLen = 4 * ((aLen + 2) / 3);
062: StringBuffer result = new StringBuffer(resultLen);
063: char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64);
064:
065: // Translate all full groups from byte array elements to Base64
066: int inCursor = 0;
067: for (int i = 0; i < numFullGroups; i++) {
068: int byte0 = a[inCursor++] & 0xff;
069: int byte1 = a[inCursor++] & 0xff;
070: int byte2 = a[inCursor++] & 0xff;
071: result.append(intToAlpha[byte0 >> 2]);
072: result
073: .append(intToAlpha[(byte0 << 4) & 0x3f
074: | (byte1 >> 4)]);
075: result
076: .append(intToAlpha[(byte1 << 2) & 0x3f
077: | (byte2 >> 6)]);
078: result.append(intToAlpha[byte2 & 0x3f]);
079: }
080:
081: // Translate partial group if present
082: if (numBytesInPartialGroup != 0) {
083: int byte0 = a[inCursor++] & 0xff;
084: result.append(intToAlpha[byte0 >> 2]);
085: if (numBytesInPartialGroup == 1) {
086: result.append(intToAlpha[(byte0 << 4) & 0x3f]);
087: result.append("==");
088: } else {
089: // assert numBytesInPartialGroup == 2;
090: int byte1 = a[inCursor++] & 0xff;
091: result.append(intToAlpha[(byte0 << 4) & 0x3f
092: | (byte1 >> 4)]);
093: result.append(intToAlpha[(byte1 << 2) & 0x3f]);
094: result.append('=');
095: }
096: }
097: // assert inCursor == a.length;
098: // assert result.length() == resultLen;
099: return result.toString();
100: }
101:
102: /**
103: * This array is a lookup table that translates 6-bit positive integer
104: * index values into their "Base64 Alphabet" equivalents as specified
105: * in Table 1 of RFC 2045.
106: */
107: private static final char intToBase64[] = { 'A', 'B', 'C', 'D',
108: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
109: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
110: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
111: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
112: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
113:
114: /**
115: * This array is a lookup table that translates 6-bit positive integer
116: * index values into their "Alternate Base64 Alphabet" equivalents.
117: * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
118: * This alternate alphabet does not use the capital letters. It is
119: * designed for use in environments where "case folding" occurs.
120: */
121: private static final char intToAltBase64[] = { '!', '"', '#', '$',
122: '%', '&', '\'', '(', ')', ',', '-', '.', ':', ';', '<',
123: '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~', 'a',
124: 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
125: 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
126: 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
127: '?' };
128:
129: /**
130: * Translates the specified Base64 string (as per Preferences.get(byte[]))
131: * into a byte array.
132: *
133: * @throw IllegalArgumentException if <tt>s</tt> is not a valid Base64
134: * string.
135: */
136: public static byte[] base64ToByteArray(String s) {
137: return base64ToByteArray(s, false);
138: }
139:
140: /**
141: * Translates the specified "alternate representation" Base64 string
142: * into a byte array.
143: *
144: * @throw IllegalArgumentException or ArrayOutOfBoundsException
145: * if <tt>s</tt> is not a valid alternate representation
146: * Base64 string.
147: */
148: public static byte[] altBase64ToByteArray(String s) {
149: return base64ToByteArray(s, true);
150: }
151:
152: private static byte[] base64ToByteArray(String s, boolean alternate) {
153: byte[] alphaToInt = (alternate ? altBase64ToInt : base64ToInt);
154: int sLen = s.length();
155: int numGroups = sLen / 4;
156: if (4 * numGroups != sLen)
157: throw new IllegalArgumentException(
158: "String length must be a multiple of four.");
159: int missingBytesInLastGroup = 0;
160: int numFullGroups = numGroups;
161: if (sLen != 0) {
162: if (s.charAt(sLen - 1) == '=') {
163: missingBytesInLastGroup++;
164: numFullGroups--;
165: }
166: if (s.charAt(sLen - 2) == '=')
167: missingBytesInLastGroup++;
168: }
169: byte[] result = new byte[3 * numGroups
170: - missingBytesInLastGroup];
171:
172: // Translate all full groups from base64 to byte array elements
173: int inCursor = 0, outCursor = 0;
174: for (int i = 0; i < numFullGroups; i++) {
175: int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
176: int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
177: int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
178: int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
179: result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
180: result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
181: result[outCursor++] = (byte) ((ch2 << 6) | ch3);
182: }
183:
184: // Translate partial group, if present
185: if (missingBytesInLastGroup != 0) {
186: int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
187: int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
188: result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
189:
190: if (missingBytesInLastGroup == 1) {
191: int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
192: result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
193: }
194: }
195: // assert inCursor == s.length()-missingBytesInLastGroup;
196: // assert outCursor == result.length;
197: return result;
198: }
199:
200: /**
201: * Translates the specified character, which is assumed to be in the
202: * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
203: *
204: * @throw IllegalArgumentException or ArrayOutOfBoundsException if
205: * c is not in the Base64 Alphabet.
206: */
207: private static int base64toInt(char c, byte[] alphaToInt) {
208: int result = alphaToInt[c];
209: if (result < 0)
210: throw new IllegalArgumentException("Illegal character " + c);
211: return result;
212: }
213:
214: /**
215: * This array is a lookup table that translates unicode characters
216: * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
217: * into their 6-bit positive integer equivalents. Characters that
218: * are not in the Base64 alphabet but fall within the bounds of the
219: * array are translated to -1.
220: */
221: private static final byte base64ToInt[] = { -1, -1, -1, -1, -1, -1,
222: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
223: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
224: -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
225: 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0,
226: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
227: 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26,
228: 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
229: 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
230:
231: /**
232: * This array is the analogue of base64ToInt, but for the nonstandard
233: * variant that avoids the use of uppercase alphabetic characters.
234: */
235: private static final byte altBase64ToInt[] = { -1, -1, -1, -1, -1,
236: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
237: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
238: 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1, 52, 53, 54, 55,
239: 56, 57, 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1,
240: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
241: -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, -1, 18, 19, 21, 20,
242: 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
243: 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 22, 23, 24, 25 };
244: }
|