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: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.luni.util;
022:
023: import java.io.UnsupportedEncodingException;
024:
025: /**
026: * This class implements Base64 encoding/decoding functionality
027: * as specified in RFC 2045 (http://www.ietf.org/rfc/rfc2045.txt).
028: */
029: public class Base64 {
030:
031: public static byte[] decode(byte[] in) {
032: return decode(in, in.length);
033: }
034:
035: public static byte[] decode(byte[] in, int len) {
036: // approximate output length
037: int length = len / 4 * 3;
038: // return an empty array on emtpy or short input without padding
039: if (length == 0) {
040: return new byte[0];
041: }
042: // temporary array
043: byte[] out = new byte[length];
044: // number of padding characters ('=')
045: int pad = 0;
046: byte chr;
047: // compute the number of the padding characters
048: // and adjust the length of the input
049: for (;; len--) {
050: chr = in[len - 1];
051: // skip the neutral characters
052: if ((chr == '\n') || (chr == '\r') || (chr == ' ')
053: || (chr == '\t')) {
054: continue;
055: }
056: if (chr == '=') {
057: pad++;
058: } else {
059: break;
060: }
061: }
062: // index in the output array
063: int out_index = 0;
064: // index in the input array
065: int in_index = 0;
066: // holds the value of the input character
067: int bits = 0;
068: // holds the value of the input quantum
069: int quantum = 0;
070: for (int i = 0; i < len; i++) {
071: chr = in[i];
072: // skip the neutral characters
073: if ((chr == '\n') || (chr == '\r') || (chr == ' ')
074: || (chr == '\t')) {
075: continue;
076: }
077: if ((chr >= 'A') && (chr <= 'Z')) {
078: // char ASCII value
079: // A 65 0
080: // Z 90 25 (ASCII - 65)
081: bits = chr - 65;
082: } else if ((chr >= 'a') && (chr <= 'z')) {
083: // char ASCII value
084: // a 97 26
085: // z 122 51 (ASCII - 71)
086: bits = chr - 71;
087: } else if ((chr >= '0') && (chr <= '9')) {
088: // char ASCII value
089: // 0 48 52
090: // 9 57 61 (ASCII + 4)
091: bits = chr + 4;
092: } else if (chr == '+') {
093: bits = 62;
094: } else if (chr == '/') {
095: bits = 63;
096: } else {
097: return null;
098: }
099: // append the value to the quantum
100: quantum = (quantum << 6) | (byte) bits;
101: if (in_index % 4 == 3) {
102: // 4 characters were read, so make the output:
103: out[out_index++] = (byte) ((quantum & 0x00FF0000) >> 16);
104: out[out_index++] = (byte) ((quantum & 0x0000FF00) >> 8);
105: out[out_index++] = (byte) (quantum & 0x000000FF);
106: }
107: in_index++;
108: }
109: if (pad > 0) {
110: // adjust the quantum value according to the padding
111: quantum = quantum << (6 * pad);
112: // make output
113: out[out_index++] = (byte) ((quantum & 0x00FF0000) >> 16);
114: if (pad == 1) {
115: out[out_index++] = (byte) ((quantum & 0x0000FF00) >> 8);
116: }
117: }
118: // create the resulting array
119: byte[] result = new byte[out_index];
120: System.arraycopy(out, 0, result, 0, out_index);
121: return result;
122: }
123:
124: private static final byte[] map = new byte[] { 'A', 'B', 'C', 'D',
125: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
126: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
127: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
128: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
129: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
130:
131: public static String encode(byte[] in, String charsetName)
132: throws UnsupportedEncodingException {
133: int length = in.length * 4 / 3;
134: length += length / 76 + 3; // for crlr
135: byte[] out = new byte[length];
136: int index = 0, i, crlr = 0, end = in.length - in.length % 3;
137: for (i = 0; i < end; i += 3) {
138: out[index++] = map[(in[i] & 0xff) >> 2];
139: out[index++] = map[((in[i] & 0x03) << 4)
140: | ((in[i + 1] & 0xff) >> 4)];
141: out[index++] = map[((in[i + 1] & 0x0f) << 2)
142: | ((in[i + 2] & 0xff) >> 6)];
143: out[index++] = map[(in[i + 2] & 0x3f)];
144: if (((index - crlr) % 76 == 0) && (index != 0)) {
145: out[index++] = '\n';
146: crlr++;
147: //out[index++] = '\r';
148: //crlr++;
149: }
150: }
151: switch (in.length % 3) {
152: case 1:
153: out[index++] = map[(in[end] & 0xff) >> 2];
154: out[index++] = map[(in[end] & 0x03) << 4];
155: out[index++] = '=';
156: out[index++] = '=';
157: break;
158: case 2:
159: out[index++] = map[(in[end] & 0xff) >> 2];
160: out[index++] = map[((in[end] & 0x03) << 4)
161: | ((in[end + 1] & 0xff) >> 4)];
162: out[index++] = map[((in[end + 1] & 0x0f) << 2)];
163: out[index++] = '=';
164: break;
165: }
166: return new String(out, 0, index, charsetName);
167: }
168: }
|