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