001: package org.bouncycastle.util.encoders;
002:
003: import java.io.IOException;
004: import java.io.OutputStream;
005:
006: public class Base64Encoder implements Encoder {
007: protected final byte[] encodingTable = { (byte) 'A', (byte) 'B',
008: (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',
009: (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
010: (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',
011: (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
012: (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',
013: (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
014: (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',
015: (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
016: (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',
017: (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
018: (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
019: (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
020: (byte) '+', (byte) '/' };
021:
022: protected byte padding = (byte) '=';
023:
024: /*
025: * set up the decoding table.
026: */
027: protected final byte[] decodingTable = new byte[128];
028:
029: protected void initialiseDecodingTable() {
030: for (int i = 0; i < encodingTable.length; i++) {
031: decodingTable[encodingTable[i]] = (byte) i;
032: }
033: }
034:
035: public Base64Encoder() {
036: initialiseDecodingTable();
037: }
038:
039: /**
040: * encode the input data producing a base 64 output stream.
041: *
042: * @return the number of bytes produced.
043: */
044: public int encode(byte[] data, int off, int length, OutputStream out)
045: throws IOException {
046: int modulus = length % 3;
047: int dataLength = (length - modulus);
048: int a1, a2, a3;
049:
050: for (int i = off; i < off + dataLength; i += 3) {
051: a1 = data[i] & 0xff;
052: a2 = data[i + 1] & 0xff;
053: a3 = data[i + 2] & 0xff;
054:
055: out.write(encodingTable[(a1 >>> 2) & 0x3f]);
056: out.write(encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]);
057: out.write(encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]);
058: out.write(encodingTable[a3 & 0x3f]);
059: }
060:
061: /*
062: * process the tail end.
063: */
064: int b1, b2, b3;
065: int d1, d2;
066:
067: switch (modulus) {
068: case 0: /* nothing left to do */
069: break;
070: case 1:
071: d1 = data[off + dataLength] & 0xff;
072: b1 = (d1 >>> 2) & 0x3f;
073: b2 = (d1 << 4) & 0x3f;
074:
075: out.write(encodingTable[b1]);
076: out.write(encodingTable[b2]);
077: out.write(padding);
078: out.write(padding);
079: break;
080: case 2:
081: d1 = data[off + dataLength] & 0xff;
082: d2 = data[off + dataLength + 1] & 0xff;
083:
084: b1 = (d1 >>> 2) & 0x3f;
085: b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
086: b3 = (d2 << 2) & 0x3f;
087:
088: out.write(encodingTable[b1]);
089: out.write(encodingTable[b2]);
090: out.write(encodingTable[b3]);
091: out.write(padding);
092: break;
093: }
094:
095: return (dataLength / 3) * 4 + ((modulus == 0) ? 0 : 4);
096: }
097:
098: private boolean ignore(char c) {
099: return (c == '\n' || c == '\r' || c == '\t' || c == ' ');
100: }
101:
102: /**
103: * decode the base 64 encoded byte data writing it to the given output stream,
104: * whitespace characters will be ignored.
105: *
106: * @return the number of bytes produced.
107: */
108: public int decode(byte[] data, int off, int length, OutputStream out)
109: throws IOException {
110: byte b1, b2, b3, b4;
111: int outLen = 0;
112:
113: int end = off + length;
114:
115: while (end > off) {
116: if (!ignore((char) data[end - 1])) {
117: break;
118: }
119:
120: end--;
121: }
122:
123: int i = off;
124: int finish = end - 4;
125:
126: i = nextI(data, i, finish);
127:
128: while (i < finish) {
129: b1 = decodingTable[data[i++]];
130:
131: i = nextI(data, i, finish);
132:
133: b2 = decodingTable[data[i++]];
134:
135: i = nextI(data, i, finish);
136:
137: b3 = decodingTable[data[i++]];
138:
139: i = nextI(data, i, finish);
140:
141: b4 = decodingTable[data[i++]];
142:
143: out.write((b1 << 2) | (b2 >> 4));
144: out.write((b2 << 4) | (b3 >> 2));
145: out.write((b3 << 6) | b4);
146:
147: outLen += 3;
148:
149: i = nextI(data, i, finish);
150: }
151:
152: outLen += decodeLastBlock(out, (char) data[end - 4],
153: (char) data[end - 3], (char) data[end - 2],
154: (char) data[end - 1]);
155:
156: return outLen;
157: }
158:
159: private int nextI(byte[] data, int i, int finish) {
160: while ((i < finish) && ignore((char) data[i])) {
161: i++;
162: }
163: return i;
164: }
165:
166: /**
167: * decode the base 64 encoded String data writing it to the given output stream,
168: * whitespace characters will be ignored.
169: *
170: * @return the number of bytes produced.
171: */
172: public int decode(String data, OutputStream out) throws IOException {
173: byte b1, b2, b3, b4;
174: int length = 0;
175:
176: int end = data.length();
177:
178: while (end > 0) {
179: if (!ignore(data.charAt(end - 1))) {
180: break;
181: }
182:
183: end--;
184: }
185:
186: int i = 0;
187: int finish = end - 4;
188:
189: i = nextI(data, i, finish);
190:
191: while (i < finish) {
192: b1 = decodingTable[data.charAt(i++)];
193:
194: i = nextI(data, i, finish);
195:
196: b2 = decodingTable[data.charAt(i++)];
197:
198: i = nextI(data, i, finish);
199:
200: b3 = decodingTable[data.charAt(i++)];
201:
202: i = nextI(data, i, finish);
203:
204: b4 = decodingTable[data.charAt(i++)];
205:
206: out.write((b1 << 2) | (b2 >> 4));
207: out.write((b2 << 4) | (b3 >> 2));
208: out.write((b3 << 6) | b4);
209:
210: length += 3;
211:
212: i = nextI(data, i, finish);
213: }
214:
215: length += decodeLastBlock(out, data.charAt(end - 4), data
216: .charAt(end - 3), data.charAt(end - 2), data
217: .charAt(end - 1));
218:
219: return length;
220: }
221:
222: private int decodeLastBlock(OutputStream out, char c1, char c2,
223: char c3, char c4) throws IOException {
224: byte b1, b2, b3, b4;
225:
226: if (c3 == padding) {
227: b1 = decodingTable[c1];
228: b2 = decodingTable[c2];
229:
230: out.write((b1 << 2) | (b2 >> 4));
231:
232: return 1;
233: } else if (c4 == padding) {
234: b1 = decodingTable[c1];
235: b2 = decodingTable[c2];
236: b3 = decodingTable[c3];
237:
238: out.write((b1 << 2) | (b2 >> 4));
239: out.write((b2 << 4) | (b3 >> 2));
240:
241: return 2;
242: } else {
243: b1 = decodingTable[c1];
244: b2 = decodingTable[c2];
245: b3 = decodingTable[c3];
246: b4 = decodingTable[c4];
247:
248: out.write((b1 << 2) | (b2 >> 4));
249: out.write((b2 << 4) | (b3 >> 2));
250: out.write((b3 << 6) | b4);
251:
252: return 3;
253: }
254: }
255:
256: private int nextI(String data, int i, int finish) {
257: while ((i < finish) && ignore(data.charAt(i))) {
258: i++;
259: }
260: return i;
261: }
262: }
|