001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.util;
010:
011: /**
012: * This class is copy/paste of Jakarta's Commons-Codec v1.1 <code>org.apache.commons.codec.binary.Base64</code>
013: * implementation.
014: * It is reproduced here because we don't want to require a new jar just to perform Base64 code/decoding.
015: *
016: * @version $Revision: 1.3 $
017: */
018: public class Base64Codec {
019: static final int CHUNK_SIZE = 76;
020: static final byte[] CHUNK_SEPARATOR = "\n".getBytes();
021:
022: static final int BASELENGTH = 255;
023: static final int LOOKUPLENGTH = 64;
024: static final int TWENTYFOURBITGROUP = 24;
025: static final int EIGHTBIT = 8;
026: static final int SIXTEENBIT = 16;
027: static final int SIXBIT = 6;
028: static final int FOURBYTE = 4;
029: static final int SIGN = -128;
030: static final byte PAD = (byte) '=';
031:
032: private static byte[] base64Alphabet = new byte[BASELENGTH];
033: private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
034:
035: static {
036: for (int i = 0; i < BASELENGTH; i++) {
037: base64Alphabet[i] = (byte) -1;
038: }
039: for (int i = 'Z'; i >= 'A'; i--) {
040: base64Alphabet[i] = (byte) (i - 'A');
041: }
042: for (int i = 'z'; i >= 'a'; i--) {
043: base64Alphabet[i] = (byte) (i - 'a' + 26);
044: }
045: for (int i = '9'; i >= '0'; i--) {
046: base64Alphabet[i] = (byte) (i - '0' + 52);
047: }
048:
049: base64Alphabet['+'] = 62;
050: base64Alphabet['/'] = 63;
051:
052: for (int i = 0; i <= 25; i++) {
053: lookUpBase64Alphabet[i] = (byte) ('A' + i);
054: }
055:
056: for (int i = 26, j = 0; i <= 51; i++, j++) {
057: lookUpBase64Alphabet[i] = (byte) ('a' + j);
058: }
059:
060: for (int i = 52, j = 0; i <= 61; i++, j++) {
061: lookUpBase64Alphabet[i] = (byte) ('0' + j);
062: }
063:
064: lookUpBase64Alphabet[62] = (byte) '+';
065: lookUpBase64Alphabet[63] = (byte) '/';
066: }
067:
068: private Base64Codec() {
069: }
070:
071: public static boolean isArrayByteBase64(byte[] arrayOctect) {
072: arrayOctect = discardWhitespace(arrayOctect);
073:
074: int length = arrayOctect.length;
075: if (length == 0) {
076: return true;
077: }
078: for (int i = 0; i < length; i++) {
079: if (!isBase64(arrayOctect[i])) {
080: return false;
081: }
082: }
083: return true;
084: }
085:
086: public static byte[] encodeBase64(byte[] binaryData) {
087: return (encodeBase64(binaryData, false));
088: }
089:
090: public static byte[] decodeBase64(byte[] base64Data) {
091: // RFC 2045 suggests line wrapping at (no more than) 76
092: // characters -- we may have embedded whitespace.
093: base64Data = discardWhitespace(base64Data);
094:
095: // handle the edge case, so we don't have to worry about it later
096: if (base64Data.length == 0) {
097: return new byte[0];
098: }
099:
100: int numberQuadruple = base64Data.length / FOURBYTE;
101: byte decodedData[] = null;
102: byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0;
103:
104: // Throw away anything not in base64Data
105:
106: int encodedIndex = 0;
107: int dataIndex = 0;
108: {
109: // this sizes the output array properly - rlw
110: int lastData = base64Data.length;
111: // ignore the '=' padding
112: while (base64Data[lastData - 1] == PAD) {
113: if (--lastData == 0) {
114: return new byte[0];
115: }
116: }
117: decodedData = new byte[lastData - numberQuadruple];
118: }
119:
120: for (int i = 0; i < numberQuadruple; i++) {
121: dataIndex = i * 4;
122: marker0 = base64Data[dataIndex + 2];
123: marker1 = base64Data[dataIndex + 3];
124:
125: b1 = base64Alphabet[base64Data[dataIndex]];
126: b2 = base64Alphabet[base64Data[dataIndex + 1]];
127:
128: if (marker0 != PAD && marker1 != PAD) {
129: //No PAD e.g 3cQl
130: b3 = base64Alphabet[marker0];
131: b4 = base64Alphabet[marker1];
132:
133: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
134: decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
135: decodedData[encodedIndex + 2] = (byte) (b3 << 6 | b4);
136: } else if (marker0 == PAD) {
137: //Two PAD e.g. 3c[Pad][Pad]
138: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
139: } else if (marker1 == PAD) {
140: //One PAD e.g. 3cQ[Pad]
141: b3 = base64Alphabet[marker0];
142:
143: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
144: decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
145: }
146: encodedIndex += 3;
147: }
148: return decodedData;
149: }
150:
151: private static byte[] encodeBase64Chunked(byte[] binaryData) {
152: return (encodeBase64(binaryData, true));
153: }
154:
155: private static boolean isBase64(byte octect) {
156: if (octect == PAD) {
157: return true;
158: } else if (base64Alphabet[octect] == -1) {
159: return false;
160: } else {
161: return true;
162: }
163: }
164:
165: private static byte[] encodeBase64(byte[] binaryData,
166: boolean isChunked) {
167: int lengthDataBits = binaryData.length * EIGHTBIT;
168: int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
169: int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
170: byte encodedData[] = null;
171: int encodedDataLength = 0;
172: int nbrChunks = 0;
173:
174: if (fewerThan24bits != 0) {
175: //data not divisible by 24 bit
176: encodedDataLength = (numberTriplets + 1) * 4;
177: } else {
178: // 16 or 8 bit
179: encodedDataLength = numberTriplets * 4;
180: }
181:
182: // If the output is to be "chunked" into 76 character sections,
183: // for compliance with RFC 2045 MIME, then it is important to
184: // allow for extra length to account for the separator(s)
185: if (isChunked) {
186:
187: nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 : (int) Math
188: .ceil((float) encodedDataLength / CHUNK_SIZE));
189: encodedDataLength += nbrChunks * CHUNK_SEPARATOR.length;
190: }
191:
192: encodedData = new byte[encodedDataLength];
193:
194: byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
195:
196: int encodedIndex = 0;
197: int dataIndex = 0;
198: int i = 0;
199: int nextSeparatorIndex = CHUNK_SIZE;
200: int chunksSoFar = 0;
201:
202: //log.debug("number of triplets = " + numberTriplets);
203: for (i = 0; i < numberTriplets; i++) {
204: dataIndex = i * 3;
205: b1 = binaryData[dataIndex];
206: b2 = binaryData[dataIndex + 1];
207: b3 = binaryData[dataIndex + 2];
208:
209: //log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);
210:
211: l = (byte) (b2 & 0x0f);
212: k = (byte) (b1 & 0x03);
213:
214: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
215: : (byte) ((b1) >> 2 ^ 0xc0);
216: byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
217: : (byte) ((b2) >> 4 ^ 0xf0);
218: byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
219: : (byte) ((b3) >> 6 ^ 0xfc);
220:
221: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
222: //log.debug( "val2 = " + val2 );
223: //log.debug( "k4 = " + (k<<4) );
224: //log.debug( "vak = " + (val2 | (k<<4)) );
225: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
226: | (k << 4)];
227: encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2)
228: | val3];
229: encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
230:
231: encodedIndex += 4;
232:
233: // If we are chunking, let's put a chunk separator down.
234: if (isChunked) {
235: // this assumes that CHUNK_SIZE % 4 == 0
236: if (encodedIndex == nextSeparatorIndex) {
237: System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
238: encodedIndex, CHUNK_SEPARATOR.length);
239: chunksSoFar++;
240: nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1))
241: + (chunksSoFar * CHUNK_SEPARATOR.length);
242: encodedIndex += CHUNK_SEPARATOR.length;
243: }
244: }
245: }
246:
247: // form integral number of 6-bit groups
248: dataIndex = i * 3;
249:
250: if (fewerThan24bits == EIGHTBIT) {
251: b1 = binaryData[dataIndex];
252: k = (byte) (b1 & 0x03);
253: //log.debug("b1=" + b1);
254: //log.debug("b1<<2 = " + (b1>>2) );
255: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
256: : (byte) ((b1) >> 2 ^ 0xc0);
257: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
258: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
259: encodedData[encodedIndex + 2] = PAD;
260: encodedData[encodedIndex + 3] = PAD;
261: } else if (fewerThan24bits == SIXTEENBIT) {
262:
263: b1 = binaryData[dataIndex];
264: b2 = binaryData[dataIndex + 1];
265: l = (byte) (b2 & 0x0f);
266: k = (byte) (b1 & 0x03);
267:
268: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
269: : (byte) ((b1) >> 2 ^ 0xc0);
270: byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
271: : (byte) ((b2) >> 4 ^ 0xf0);
272:
273: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
274: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
275: | (k << 4)];
276: encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
277: encodedData[encodedIndex + 3] = PAD;
278: }
279:
280: if (isChunked) {
281: // we also add a separator to the end of the final chunk.
282: if (chunksSoFar < nbrChunks) {
283: System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
284: encodedDataLength - CHUNK_SEPARATOR.length,
285: CHUNK_SEPARATOR.length);
286: }
287: }
288:
289: return encodedData;
290: }
291:
292: private static byte[] discardWhitespace(byte[] data) {
293: byte groomedData[] = new byte[data.length];
294: int bytesCopied = 0;
295:
296: for (int i = 0; i < data.length; i++) {
297: switch (data[i]) {
298: case (byte) ' ':
299: case (byte) '\n':
300: case (byte) '\r':
301: case (byte) '\t':
302: break;
303: default:
304: groomedData[bytesCopied++] = data[i];
305: }
306: }
307:
308: byte packedData[] = new byte[bytesCopied];
309:
310: System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
311:
312: return packedData;
313: }
314: }
|