001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/misc/Base64.java,v 1.11 2007/01/15 10:31:17 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.11 $
005: * $Date: 2007/01/15 10:31:17 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: */
032: /*
033: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/misc/Base64.java,v 1.11 2007/01/15 10:31:17 dungbtm Exp $
034: * $Revision: 1.11 $
035: * $Date: 2007/01/15 10:31:17 $
036: *
037: * ====================================================================
038: *
039: * The Apache Software License, Version 1.1
040: *
041: * Copyright (c) 1999 The Apache Software Foundation. All rights
042: * reserved.
043: *
044: * Redistribution and use in source and binary forms, with or without
045: * modification, are permitted provided that the following conditions
046: * are met:
047: *
048: * 1. Redistributions of source code must retain the above copyright
049: * notice, this list of conditions and the following disclaimer.
050: *
051: * 2. Redistributions in binary form must reproduce the above copyright
052: * notice, this list of conditions and the following disclaimer in
053: * the documentation and/or other materials provided with the
054: * distribution.
055: *
056: * 3. The end-user documentation included with the redistribution, if
057: * any, must include the following acknowlegement:
058: * "This product includes software developed by the
059: * Apache Software Foundation (http://www.apache.org/)."
060: * Alternately, this acknowlegement may appear in the software itself,
061: * if and wherever such third-party acknowlegements normally appear.
062: *
063: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
064: * Foundation" must not be used to endorse or promote products derived
065: * from this software without prior written permission. For written
066: * permission, please contact apache@apache.org.
067: *
068: * 5. Products derived from this software may not be called "Apache"
069: * nor may "Apache" appear in their names without prior written
070: * permission of the Apache Group.
071: *
072: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
073: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
074: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
075: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
076: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
077: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
078: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
079: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
080: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
081: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
082: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
083: * SUCH DAMAGE.
084: * ====================================================================
085: *
086: * This software consists of voluntary contributions made by many
087: * individuals on behalf of the Apache Software Foundation. For more
088: * information on the Apache Software Foundation, please see
089: * <http://www.apache.org/>.
090: *
091: * [Additional notices, if required by prior licensing conditions]
092: *
093: */
094:
095: package net.myvietnam.mvncore.misc;
096:
097: /**
098: * This class provides encode/decode for RFC 2045 Base64 as defined by
099: * RFC 2045, N. Freed and N. Borenstein. <a
100: * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>:
101: * Multipurpose Internet Mail Extensions (MIME) Part One: Format of
102: * Internet Message Bodies. Reference 1996
103: *
104: * @author Jeffrey Rodriguez
105: * @version $Id: Base64.java,v 1.11 2007/01/15 10:31:17 dungbtm Exp $
106: */
107: public final class Base64 {
108: static private final int BASELENGTH = 255;
109: static private final int LOOKUPLENGTH = 64;
110: static private final int TWENTYFOURBITGROUP = 24;
111: static private final int EIGHTBIT = 8;
112: static private final int SIXTEENBIT = 16;
113: //static private final int SIXBIT = 6;
114: static private final int FOURBYTE = 4;
115: static private final int SIGN = -128;
116: static private final byte PAD = (byte) '=';
117: static private byte[] base64Alphabet = new byte[BASELENGTH];
118: static private byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
119: //static private final Log log = LogSource.getInstance("org.apache.commons.util.Base64");
120:
121: static {
122: for (int i = 0; i < BASELENGTH; i++) {
123: base64Alphabet[i] = -1;
124: }
125: for (int i = 'Z'; i >= 'A'; i--) {
126: base64Alphabet[i] = (byte) (i - 'A');
127: }
128: for (int i = 'z'; i >= 'a'; i--) {
129: base64Alphabet[i] = (byte) (i - 'a' + 26);
130: }
131: for (int i = '9'; i >= '0'; i--) {
132: base64Alphabet[i] = (byte) (i - '0' + 52);
133: }
134:
135: base64Alphabet['+'] = 62;
136: base64Alphabet['/'] = 63;
137:
138: for (int i = 0; i <= 25; i++)
139: lookUpBase64Alphabet[i] = (byte) ('A' + i);
140:
141: for (int i = 26, j = 0; i <= 51; i++, j++)
142: lookUpBase64Alphabet[i] = (byte) ('a' + j);
143:
144: for (int i = 52, j = 0; i <= 61; i++, j++)
145: lookUpBase64Alphabet[i] = (byte) ('0' + j);
146:
147: lookUpBase64Alphabet[62] = (byte) '+';
148: lookUpBase64Alphabet[63] = (byte) '/';
149: }
150:
151: public static boolean isBase64(String isValidString) {
152: return isArrayByteBase64(isValidString.getBytes());
153: }
154:
155: public static boolean isBase64(byte octect) {
156: //shall we ignore white space? JEFF??
157: return (octect == PAD || base64Alphabet[octect] != -1);
158: }
159:
160: public static boolean isArrayByteBase64(byte[] arrayOctect) {
161: int length = arrayOctect.length;
162: if (length == 0) {
163: // shouldn't a 0 length array be valid base64 data?
164: // return false;
165: return true;
166: }
167: for (int i = 0; i < length; i++) {
168: if (!Base64.isBase64(arrayOctect[i]))
169: return false;
170: }
171: return true;
172: }
173:
174: /**
175: * Encodes hex octects into Base64.
176: *
177: * @param binaryData Array containing binary data to encode.
178: * @return Base64-encoded data.
179: */
180: public static byte[] encode(byte[] binaryData) {
181: int lengthDataBits = binaryData.length * EIGHTBIT;
182: int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
183: int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
184: byte encodedData[] = null;
185:
186: if (fewerThan24bits != 0) {
187: //data not divisible by 24 bit
188: encodedData = new byte[(numberTriplets + 1) * 4];
189: } else {
190: // 16 or 8 bit
191: encodedData = new byte[numberTriplets * 4];
192: }
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: //log.debug("number of triplets = " + numberTriplets);
200: for (i = 0; i < numberTriplets; i++) {
201: dataIndex = i * 3;
202: b1 = binaryData[dataIndex];
203: b2 = binaryData[dataIndex + 1];
204: b3 = binaryData[dataIndex + 2];
205:
206: //log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);
207:
208: l = (byte) (b2 & 0x0f);
209: k = (byte) (b1 & 0x03);
210:
211: encodedIndex = i * 4;
212: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
213: : (byte) ((b1) >> 2 ^ 0xc0);
214: byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
215: : (byte) ((b2) >> 4 ^ 0xf0);
216: byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
217: : (byte) ((b3) >> 6 ^ 0xfc);
218:
219: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
220: //log.debug( "val2 = " + val2 );
221: //log.debug( "k4 = " + (k<<4) );
222: //log.debug( "vak = " + (val2 | (k<<4)) );
223: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
224: | (k << 4)];
225: encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2)
226: | val3];
227: encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
228: }
229:
230: // form integral number of 6-bit groups
231: dataIndex = i * 3;
232: encodedIndex = i * 4;
233: if (fewerThan24bits == EIGHTBIT) {
234: b1 = binaryData[dataIndex];
235: k = (byte) (b1 & 0x03);
236: //log.debug("b1=" + b1);
237: //log.debug("b1<<2 = " + (b1>>2) );
238: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
239: : (byte) ((b1) >> 2 ^ 0xc0);
240: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
241: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
242: encodedData[encodedIndex + 2] = PAD;
243: encodedData[encodedIndex + 3] = PAD;
244: } else if (fewerThan24bits == SIXTEENBIT) {
245:
246: b1 = binaryData[dataIndex];
247: b2 = binaryData[dataIndex + 1];
248: l = (byte) (b2 & 0x0f);
249: k = (byte) (b1 & 0x03);
250:
251: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
252: : (byte) ((b1) >> 2 ^ 0xc0);
253: byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
254: : (byte) ((b2) >> 4 ^ 0xf0);
255:
256: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
257: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
258: | (k << 4)];
259: encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
260: encodedData[encodedIndex + 3] = PAD;
261: }
262:
263: return encodedData;
264: }
265:
266: /**
267: * Decodes Base64 data into octects
268: *
269: * @param base64Data Byte array containing Base64 data
270: * @return Array containing decoded data.
271: */
272: public static byte[] decode(byte[] base64Data) {
273: // handle the edge case, so we don't have to worry about it later
274: if (base64Data.length == 0) {
275: return new byte[0];
276: }
277:
278: int numberQuadruple = base64Data.length / FOURBYTE;
279: byte decodedData[] = null;
280: byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0;
281:
282: // Throw away anything not in base64Data
283:
284: int encodedIndex = 0;
285: int dataIndex = 0;
286: {
287: // this sizes the output array properly - rlw
288: int lastData = base64Data.length;
289: // ignore the '=' padding
290: while (base64Data[lastData - 1] == PAD) {
291: if (--lastData == 0) {
292: return new byte[0];
293: }
294: }
295: decodedData = new byte[lastData - numberQuadruple];
296: }
297:
298: for (int i = 0; i < numberQuadruple; i++) {
299: dataIndex = i * 4;
300: marker0 = base64Data[dataIndex + 2];
301: marker1 = base64Data[dataIndex + 3];
302:
303: b1 = base64Alphabet[base64Data[dataIndex]];
304: b2 = base64Alphabet[base64Data[dataIndex + 1]];
305:
306: if (marker0 != PAD && marker1 != PAD) {
307: //No PAD e.g 3cQl
308: b3 = base64Alphabet[marker0];
309: b4 = base64Alphabet[marker1];
310:
311: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
312: decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
313: decodedData[encodedIndex + 2] = (byte) (b3 << 6 | b4);
314: } else if (marker0 == PAD) {
315: //Two PAD e.g. 3c[Pad][Pad]
316: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
317: } else if (marker1 == PAD) {
318: //One PAD e.g. 3cQ[Pad]
319: b3 = base64Alphabet[marker0];
320:
321: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
322: decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
323: }
324: encodedIndex += 3;
325: }
326: return decodedData;
327: }
328:
329: }
|