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: * @author Alexander Y. Kleymenov
019: * @version $Revision: 1.1.6.3 $
020: */package org.apache.harmony.misc;
021:
022: /**
023: * This class implements Base64 encoding/decoding functionality
024: * as specified in RFC 2045 (http://www.ietf.org/rfc/rfc2045.txt).
025: */
026: public class Base64 {
027:
028: public static byte[] decode(byte[] in) {
029: return decode(in, in.length);
030: }
031:
032: public static byte[] decode(byte[] in, int len) {
033: int length = len / 4 * 3;
034: byte[] out = new byte[length];
035: int pad = 0, index = 0, j = 0, bits = 0;
036: byte[] bytes = new byte[4];
037: byte chr;
038: for (int i = 0; i < len; i++) {
039: if (in[i] == '\n' || in[i] == '\r') {
040: continue;
041: }
042: chr = in[i];
043: // char ASCII value
044: // + 43 62
045: // / 47 63
046: // 0 48 52
047: // . . .
048: // 9 57 61 (ASCII + 4)
049: // = 61 pad
050: // A 65 0
051: // . . .
052: // Z 90 25 (ASCII - 65)
053: // a 97 26
054: // . . .
055: // z 122 51 (ASCII - 71)
056: if (chr == '+') {
057: bits = 62;
058: } else if (chr == '/') {
059: bits = 63;
060: } else if ((chr >= '0') && (chr <= '9')) {
061: bits = chr + 4;
062: } else if (chr == '=') {
063: bits = 0;
064: pad++;
065: } else if ((chr >= 'A') && (chr <= 'Z')) {
066: bits = chr - 65;
067: } else if ((chr >= 'a') && (chr <= 'z')) {
068: bits = chr - 71;
069: } else {
070: return null;
071: }
072: bytes[j % 4] = (byte) bits;
073: if (j % 4 == 3) {
074: out[index++] = (byte) (bytes[0] << 2 | bytes[1] >> 4);
075: if (pad != 2) {
076: out[index++] = (byte) (bytes[1] << 4 | bytes[2] >> 2);
077: if (pad != 1) {
078: out[index++] = (byte) (bytes[2] << 6 | bytes[3]);
079: }
080: }
081: }
082: j++;
083: }
084: byte[] result = new byte[index];
085: System.arraycopy(out, 0, result, 0, index);
086: return result;
087: }
088:
089: private static final byte[] map = new byte[] { 'A', 'B', 'C', 'D',
090: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
091: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
092: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
093: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
094: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
095:
096: public static String encode(byte[] in) {
097: int length = in.length * 4 / 3;
098: length += length / 76 + 3; // for crlr
099: byte[] out = new byte[length];
100: int index = 0, i, crlr = 0, end = in.length - in.length % 3;
101: for (i = 0; i < end; i += 3) {
102: out[index++] = map[(in[i] & 0xff) >> 2];
103: out[index++] = map[((in[i] & 0x03) << 4)
104: | ((in[i + 1] & 0xff) >> 4)];
105: out[index++] = map[((in[i + 1] & 0x0f) << 2)
106: | ((in[i + 2] & 0xff) >> 6)];
107: out[index++] = map[(in[i + 2] & 0x3f)];
108: if (((index - crlr) % 76 == 0) && (index != 0)) {
109: out[index++] = '\n';
110: crlr++;
111: //out[index++] = '\r';
112: //crlr++;
113: }
114: }
115: switch (in.length % 3) {
116: case 1:
117: out[index++] = map[(in[end] & 0xff) >> 2];
118: out[index++] = map[(in[end] & 0x03) << 4];
119: out[index++] = '=';
120: out[index++] = '=';
121: break;
122: case 2:
123: out[index++] = map[(in[end] & 0xff) >> 2];
124: out[index++] = map[((in[end] & 0x03) << 4)
125: | ((in[end + 1] & 0xff) >> 4)];
126: out[index++] = map[((in[end + 1] & 0x0f) << 2)];
127: out[index++] = '=';
128: break;
129: }
130: return new String(out, 0, index);
131: }
132: }
|