001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.util;
018:
019: /**
020: * This class provides encode/decode for RFC 2045 Base64 as defined by
021: * RFC 2045, N. Freed and N. Borenstein. <a
022: * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>:
023: * Multipurpose Internet Mail Extensions (MIME) Part One: Format of
024: * Internet Message Bodies. Reference 1996
025: *
026: * @author Jeffrey Rodriguez
027: * @version $Id: Base64.java,v 1.3 2004/05/26 16:20:50 yoavs Exp $
028: */
029: public final class Base64 {
030: static private final int BASELENGTH = 255;
031: static private final int LOOKUPLENGTH = 64;
032: static private final int TWENTYFOURBITGROUP = 24;
033: static private final int EIGHTBIT = 8;
034: static private final int SIXTEENBIT = 16;
035: static private final int SIXBIT = 6;
036: static private final int FOURBYTE = 4;
037: static private final int SIGN = -128;
038: static private final byte PAD = (byte) '=';
039: static private byte[] base64Alphabet = new byte[BASELENGTH];
040: static private byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
041: //static private final Log log = LogSource.getInstance("org.apache.commons.util.Base64");
042:
043: static {
044: for (int i = 0; i < BASELENGTH; i++) {
045: base64Alphabet[i] = -1;
046: }
047: for (int i = 'Z'; i >= 'A'; i--) {
048: base64Alphabet[i] = (byte) (i - 'A');
049: }
050: for (int i = 'z'; i >= 'a'; i--) {
051: base64Alphabet[i] = (byte) (i - 'a' + 26);
052: }
053: for (int i = '9'; i >= '0'; i--) {
054: base64Alphabet[i] = (byte) (i - '0' + 52);
055: }
056:
057: base64Alphabet['+'] = 62;
058: base64Alphabet['/'] = 63;
059:
060: for (int i = 0; i <= 25; i++)
061: lookUpBase64Alphabet[i] = (byte) ('A' + i);
062:
063: for (int i = 26, j = 0; i <= 51; i++, j++)
064: lookUpBase64Alphabet[i] = (byte) ('a' + j);
065:
066: for (int i = 52, j = 0; i <= 61; i++, j++)
067: lookUpBase64Alphabet[i] = (byte) ('0' + j);
068:
069: lookUpBase64Alphabet[62] = (byte) '+';
070: lookUpBase64Alphabet[63] = (byte) '/';
071: }
072:
073: public static boolean isBase64(String isValidString) {
074: return isArrayByteBase64(isValidString.getBytes());
075: }
076:
077: public static boolean isBase64(byte octect) {
078: //shall we ignore white space? JEFF??
079: return (octect == PAD || base64Alphabet[octect] != -1);
080: }
081:
082: public static boolean isArrayByteBase64(byte[] arrayOctect) {
083: int length = arrayOctect.length;
084: if (length == 0) {
085: // shouldn't a 0 length array be valid base64 data?
086: // return false;
087: return true;
088: }
089: for (int i = 0; i < length; i++) {
090: if (!Base64.isBase64(arrayOctect[i]))
091: return false;
092: }
093: return true;
094: }
095:
096: /**
097: * Encodes hex octects into Base64.
098: *
099: * @param binaryData Array containing binary data to encode.
100: * @return Base64-encoded data.
101: */
102: public static byte[] encode(byte[] binaryData) {
103: int lengthDataBits = binaryData.length * EIGHTBIT;
104: int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
105: int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
106: byte encodedData[] = null;
107:
108: if (fewerThan24bits != 0) {
109: //data not divisible by 24 bit
110: encodedData = new byte[(numberTriplets + 1) * 4];
111: } else {
112: // 16 or 8 bit
113: encodedData = new byte[numberTriplets * 4];
114: }
115:
116: byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
117:
118: int encodedIndex = 0;
119: int dataIndex = 0;
120: int i = 0;
121: //log.debug("number of triplets = " + numberTriplets);
122: for (i = 0; i < numberTriplets; i++) {
123: dataIndex = i * 3;
124: b1 = binaryData[dataIndex];
125: b2 = binaryData[dataIndex + 1];
126: b3 = binaryData[dataIndex + 2];
127:
128: //log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);
129:
130: l = (byte) (b2 & 0x0f);
131: k = (byte) (b1 & 0x03);
132:
133: encodedIndex = i * 4;
134: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
135: : (byte) ((b1) >> 2 ^ 0xc0);
136: byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
137: : (byte) ((b2) >> 4 ^ 0xf0);
138: byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
139: : (byte) ((b3) >> 6 ^ 0xfc);
140:
141: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
142: //log.debug( "val2 = " + val2 );
143: //log.debug( "k4 = " + (k<<4) );
144: //log.debug( "vak = " + (val2 | (k<<4)) );
145: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
146: | (k << 4)];
147: encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2)
148: | val3];
149: encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
150: }
151:
152: // form integral number of 6-bit groups
153: dataIndex = i * 3;
154: encodedIndex = i * 4;
155: if (fewerThan24bits == EIGHTBIT) {
156: b1 = binaryData[dataIndex];
157: k = (byte) (b1 & 0x03);
158: //log.debug("b1=" + b1);
159: //log.debug("b1<<2 = " + (b1>>2) );
160: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
161: : (byte) ((b1) >> 2 ^ 0xc0);
162: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
163: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
164: encodedData[encodedIndex + 2] = PAD;
165: encodedData[encodedIndex + 3] = PAD;
166: } else if (fewerThan24bits == SIXTEENBIT) {
167:
168: b1 = binaryData[dataIndex];
169: b2 = binaryData[dataIndex + 1];
170: l = (byte) (b2 & 0x0f);
171: k = (byte) (b1 & 0x03);
172:
173: byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
174: : (byte) ((b1) >> 2 ^ 0xc0);
175: byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
176: : (byte) ((b2) >> 4 ^ 0xf0);
177:
178: encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
179: encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
180: | (k << 4)];
181: encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
182: encodedData[encodedIndex + 3] = PAD;
183: }
184:
185: return encodedData;
186: }
187:
188: /**
189: * Decodes Base64 data into octects
190: *
191: * @param base64Data Byte array containing Base64 data
192: * @return Array containing decoded data.
193: */
194: public static byte[] decode(byte[] base64Data) {
195: // handle the edge case, so we don't have to worry about it later
196: if (base64Data.length == 0) {
197: return new byte[0];
198: }
199:
200: int numberQuadruple = base64Data.length / FOURBYTE;
201: byte decodedData[] = null;
202: byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0;
203:
204: // Throw away anything not in base64Data
205:
206: int encodedIndex = 0;
207: int dataIndex = 0;
208: {
209: // this sizes the output array properly - rlw
210: int lastData = base64Data.length;
211: // ignore the '=' padding
212: while (base64Data[lastData - 1] == PAD) {
213: if (--lastData == 0) {
214: return new byte[0];
215: }
216: }
217: decodedData = new byte[lastData - numberQuadruple];
218: }
219:
220: for (int i = 0; i < numberQuadruple; i++) {
221: dataIndex = i * 4;
222: marker0 = base64Data[dataIndex + 2];
223: marker1 = base64Data[dataIndex + 3];
224:
225: b1 = base64Alphabet[base64Data[dataIndex]];
226: b2 = base64Alphabet[base64Data[dataIndex + 1]];
227:
228: if (marker0 != PAD && marker1 != PAD) {
229: //No PAD e.g 3cQl
230: b3 = base64Alphabet[marker0];
231: b4 = base64Alphabet[marker1];
232:
233: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
234: decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
235: decodedData[encodedIndex + 2] = (byte) (b3 << 6 | b4);
236: } else if (marker0 == PAD) {
237: //Two PAD e.g. 3c[Pad][Pad]
238: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
239: } else if (marker1 == PAD) {
240: //One PAD e.g. 3cQ[Pad]
241: b3 = base64Alphabet[marker0];
242:
243: decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
244: decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
245: }
246: encodedIndex += 3;
247: }
248: return decodedData;
249: }
250:
251: }
|