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