001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012: /**
013: * Base64 is a helper class for converting byte arrays to and
014: * from base 64 encoded Strings.
015: *
016: */
017: class Base64 {
018:
019: private static final byte equalSign = (byte) '=';
020:
021: static char digits[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
022: 'I', 'J', 'K', 'L', 'M',
023: 'N',
024: 'O',
025: 'P', //
026: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
027: 'c', 'd',
028: 'e',
029: 'f', //
030: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
031: 's', 't', 'u',
032: 'v', //
033: 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
034: '8', '9', '+', '/' };
035:
036: /**
037: * This method decodes the byte array in base 64 encoding into a char array
038: * Base 64 encoding has to be according to the specification given by the
039: * RFC 1521 (5.2).
040: *
041: * @param data the encoded byte array
042: * @return the decoded byte array
043: */
044: public static byte[] decode(byte[] data) {
045: if (data.length == 0) {
046: return data;
047: }
048: int lastRealDataIndex = data.length - 1;
049: while (data[lastRealDataIndex] == equalSign) {
050: lastRealDataIndex--;
051: }
052: // original data digit is 8 bits long, but base64 digit is 6 bits long
053: int padBytes = data.length - 1 - lastRealDataIndex;
054: int byteLength = data.length * 6 / 8 - padBytes;
055: byte[] result = new byte[byteLength];
056: // Each 4 bytes of input (encoded) we end up with 3 bytes of output
057: int dataIndex = 0;
058: int resultIndex = 0;
059: int allBits = 0;
060: // how many result chunks we can process before getting to pad bytes
061: int resultChunks = (lastRealDataIndex + 1) / 4;
062: for (int i = 0; i < resultChunks; i++) {
063: allBits = 0;
064: // Loop 4 times gathering input bits (4 * 6 = 24)
065: for (int j = 0; j < 4; j++) {
066: allBits = (allBits << 6)
067: | decodeDigit(data[dataIndex++]);
068: }
069: // Loop 3 times generating output bits (3 * 8 = 24)
070: for (int j = resultIndex + 2; j >= resultIndex; j--) {
071: result[j] = (byte) (allBits & 0xff); // Bottom 8 bits
072: allBits = allBits >>> 8;
073: }
074: resultIndex += 3; // processed 3 result bytes
075: }
076: // Now we do the extra bytes in case the original (non-encoded) data
077: // was not multiple of 3 bytes
078: switch (padBytes) {
079: case 1:
080: // 1 pad byte means 3 (4-1) extra Base64 bytes of input, 18
081: // bits, of which only 16 are meaningful
082: // Or: 2 bytes of result data
083: allBits = 0;
084: // Loop 3 times gathering input bits
085: for (int j = 0; j < 3; j++) {
086: allBits = (allBits << 6)
087: | decodeDigit(data[dataIndex++]);
088: }
089: // NOTE - The code below ends up being equivalent to allBits =
090: // allBits>>>2
091: // But we code it in a non-optimized way for clarity
092: // The 4th, missing 6 bits are all 0
093: allBits = allBits << 6;
094: // The 3rd, missing 8 bits are all 0
095: allBits = allBits >>> 8;
096: // Loop 2 times generating output bits
097: for (int j = resultIndex + 1; j >= resultIndex; j--) {
098: result[j] = (byte) (allBits & 0xff); // Bottom 8
099: // bits
100: allBits = allBits >>> 8;
101: }
102: break;
103: case 2:
104: // 2 pad bytes mean 2 (4-2) extra Base64 bytes of input, 12 bits
105: // of data, of which only 8 are meaningful
106: // Or: 1 byte of result data
107: allBits = 0;
108: // Loop 2 times gathering input bits
109: for (int j = 0; j < 2; j++) {
110: allBits = (allBits << 6)
111: | decodeDigit(data[dataIndex++]);
112: }
113: // NOTE - The code below ends up being equivalent to allBits =
114: // allBits>>>4
115: // But we code it in a non-optimized way for clarity
116: // The 3rd and 4th, missing 6 bits are all 0
117: allBits = allBits << 6;
118: allBits = allBits << 6;
119: // The 3rd and 4th, missing 8 bits are all 0
120: allBits = allBits >>> 8;
121: allBits = allBits >>> 8;
122: result[resultIndex] = (byte) (allBits & 0xff); // Bottom
123: // 8
124: // bits
125: break;
126: }
127: return result;
128: }
129:
130: /**
131: * This method converts a Base 64 digit to its numeric value.
132: *
133: * @param data digit (character) to convert
134: * @return value for the digit
135: */
136: static int decodeDigit(byte data) {
137: char charData = (char) data;
138: if (charData <= 'Z' && charData >= 'A') {
139: return charData - 'A';
140: }
141: if (charData <= 'z' && charData >= 'a') {
142: return charData - 'a' + 26;
143: }
144: if (charData <= '9' && charData >= '0') {
145: return charData - '0' + 52;
146: }
147: switch (charData) {
148: case '+':
149: return 62;
150: case '/':
151: return 63;
152: default:
153: throw new IllegalArgumentException(
154: "Invalid char to decode: " + data); //$NON-NLS-1$
155: }
156: }
157:
158: /**
159: * This method encodes the byte array into a char array in base 64 according
160: * to the specification given by the RFC 1521 (5.2).
161: *
162: * @param data the encoded char array
163: * @return the byte array that needs to be encoded
164: */
165: public static byte[] encode(byte[] data) {
166: int sourceChunks = data.length / 3;
167: int len = ((data.length + 2) / 3) * 4;
168: byte[] result = new byte[len];
169: int extraBytes = data.length - (sourceChunks * 3);
170: // Each 4 bytes of input (encoded) we end up with 3 bytes of output
171: int dataIndex = 0;
172: int resultIndex = 0;
173: int allBits = 0;
174: for (int i = 0; i < sourceChunks; i++) {
175: allBits = 0;
176: // Loop 3 times gathering input bits (3 * 8 = 24)
177: for (int j = 0; j < 3; j++) {
178: allBits = (allBits << 8) | (data[dataIndex++] & 0xff);
179: }
180: // Loop 4 times generating output bits (4 * 6 = 24)
181: for (int j = resultIndex + 3; j >= resultIndex; j--) {
182: result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
183: // 6
184: // bits
185: allBits = allBits >>> 6;
186: }
187: resultIndex += 4; // processed 4 result bytes
188: }
189: // Now we do the extra bytes in case the original (non-encoded) data
190: // is not multiple of 4 bytes
191: switch (extraBytes) {
192: case 1:
193: allBits = data[dataIndex++]; // actual byte
194: allBits = allBits << 8; // 8 bits of zeroes
195: allBits = allBits << 8; // 8 bits of zeroes
196: // Loop 4 times generating output bits (4 * 6 = 24)
197: for (int j = resultIndex + 3; j >= resultIndex; j--) {
198: result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
199: // 6
200: // bits
201: allBits = allBits >>> 6;
202: }
203: // 2 pad tags
204: result[result.length - 1] = (byte) '=';
205: result[result.length - 2] = (byte) '=';
206: break;
207: case 2:
208: allBits = data[dataIndex++]; // actual byte
209: allBits = (allBits << 8) | (data[dataIndex++] & 0xff); // actual
210: // byte
211: allBits = allBits << 8; // 8 bits of zeroes
212: // Loop 4 times generating output bits (4 * 6 = 24)
213: for (int j = resultIndex + 3; j >= resultIndex; j--) {
214: result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
215: // 6
216: // bits
217: allBits = allBits >>> 6;
218: }
219: // 1 pad tag
220: result[result.length - 1] = (byte) '=';
221: break;
222: }
223: return result;
224: }
225: }
|