001: /*
002: *
003: * The contents of this file are subject to the Netscape Public
004: * License Version 1.1 (the "License"); you may not use this file
005: * except in compliance with the License. You may obtain a copy of
006: * the License at http://www.mozilla.org/NPL/
007: *
008: * Software distributed under the License is distributed on an "AS
009: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
010: * implied. See the License for the specific language governing
011: * rights and limitations under the License.
012: *
013: * The Original Code is mozilla.org code.
014: *
015: * The Initial Developer of the Original Code is Netscape
016: * Communications Corporation. Portions created by Netscape are
017: * Copyright (C) 1999 Netscape Communications Corporation. All
018: * Rights Reserved.
019: *
020: * Contributor(s):
021: */
022:
023: package org.ozoneDB.util;
024:
025: import java.io.ByteArrayOutputStream;
026:
027: /**
028: * Base 64 text to byte decoded. To produce the binary array from
029: * base 64 encoding call {@link #translate} for each sequence of
030: * characters and {@link #getByteArray} to mark closure of the
031: * character stream and retrieve the binary contents.
032: *
033: * @author Based on code from the Mozilla Directory SDK
034: */
035: public final class MimeBase64Decoder {
036:
037: private ByteArrayOutputStream out = new ByteArrayOutputStream();
038:
039: private byte[] token = new byte[4];
040: private byte[] bytes = new byte[3];
041: private int token_length = 0;
042:
043: private final static byte NUL = 127;
044: private final static byte EOF = 126;
045:
046: private final static byte[] map =
047:
048: { NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
049: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
050: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
051: NUL, NUL, NUL, NUL, NUL, NUL, 62, NUL, NUL, NUL, 63, 52,
052: 53, 54, 55, 56, 57, 58, 59, 60, 61, NUL, NUL, NUL, EOF,
053: NUL, NUL, NUL, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
054: 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, NUL,
055: NUL, NUL, NUL, NUL, NUL, 26, 27, 28, 29, 30, 31, 32, 33,
056: 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
057: 49, 50, 51, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
058: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
059: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
060: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
061: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
062: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
063: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
064: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
065: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
066: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
067: NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
068: NUL, NUL, NUL, NUL };
069:
070: // 000-007
071: // 010-017
072: // 020-027
073: // 030-037
074: // 040-047 !"#$%&'
075: // 050-057 ()*+,-./
076: // 060-067 01234567
077: // 070-077 89:;<=>?
078:
079: // 100-107 @ABCDEFG
080: // 110-117 HIJKLMNO
081: // 120-127 PQRSTUVW
082: // 130-137 XYZ[\]^_
083: // 140-147 `abcdefg
084: // 150-157 hijklmno
085: // 160-167 pqrstuvw
086: // 170-177 xyz{|}~
087:
088: // 200-207
089: // 210-217
090: // 220-227
091: // 230-237
092: // 240-247
093: // 250-257
094: // 260-267
095: // 270-277
096:
097: // 300-307
098: // 310-317
099: // 320-327
100: // 330-337
101: // 340-347
102: // 350-357
103: // 360-367
104: // 370-377
105:
106: // Fast routine that assumes full 4-char tokens with no '=' in them.
107: //
108: private void decode_token() {
109: int num = token[0] << 18 | token[1] << 12 | token[2] << 6
110: | token[3];
111:
112: bytes[0] = (byte) (0xFF & num >> 16);
113: bytes[1] = (byte) (0xFF & num >> 8);
114: bytes[2] = (byte) (0xFF & num);
115:
116: out.write(bytes, 0, 3);
117: }
118:
119: // Hairier routine that deals with the final token, which can have fewer
120: // than four characters, and that might be padded with '='.
121: //
122: private final void decode_final_token() {
123:
124: byte b0 = token[0];
125: byte b1 = token[1];
126: byte b2 = token[2];
127: byte b3 = token[3];
128:
129: int eq_count = 0;
130:
131: if (b0 == EOF) {
132: b0 = 0;
133: eq_count++;
134: }
135: if (b1 == EOF) {
136: b1 = 0;
137: eq_count++;
138: }
139: if (b2 == EOF) {
140: b2 = 0;
141: eq_count++;
142: }
143: if (b3 == EOF) {
144: b3 = 0;
145: eq_count++;
146: }
147:
148: int num = b0 << 18 | b1 << 12 | b2 << 6 | b3;
149:
150: // eq_count will be 0, 1, or 2.
151: // No "=" padding means 4 bytes mapped to 3, the normal case,
152: // not handled in this routine.
153: // "xxx=" means 3 bytes mapped to 2.
154: // "xx==" means 2 bytes mapped to 1.
155: // "x===" can't happen, because "x" would then be encoding
156: // only 6 bits, not 8, the minimum possible.
157:
158: out.write((byte) (num >> 16));
159: if (eq_count <= 1) {
160: out.write((byte) (num >> 8 & 0xFF));
161: if (eq_count == 0) {
162: out.write((byte) (num & 0xFF));
163: }
164: }
165: }
166:
167: public final void translate(char[] ch) {
168: translate(ch, 0, ch.length);
169: }
170:
171: public final void translate(char[] ch, int offset, int length) {
172:
173: if (token == null) {
174: return;
175: }
176:
177: for (int i = offset; i < offset + length; i++) {
178: byte t = map[ch[i] & 0xff];
179:
180: if (t == EOF) {
181: eof();
182: } else {
183: if (t != NUL) {
184: token[token_length++] = t;
185: }
186: }
187: if (token_length == 4) {
188: decode_token();
189: token_length = 0;
190: }
191: }
192: }
193:
194: private void eof() {
195: if (token != null && token_length != 0) {
196: while (token_length < 4) {
197: token[token_length++] = EOF;
198: }
199: decode_final_token();
200: }
201: token_length = 0;
202: token = new byte[4];
203: bytes = new byte[3];
204: }
205:
206: public byte[] getByteArray() {
207: eof();
208: return out.toByteArray();
209: }
210: }
|