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