01: /*
02: * RFC2047.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: RFC2047.java,v 1.1.1.1 2002/09/24 16:10:04 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j.mail;
23:
24: import gnu.regexp.RE;
25: import gnu.regexp.REMatch;
26: import gnu.regexp.UncheckedRE;
27: import java.io.UnsupportedEncodingException;
28: import org.armedbear.j.FastStringBuffer;
29: import org.armedbear.j.Log;
30: import org.armedbear.j.Utilities;
31:
32: public final class RFC2047 {
33: private static final RE prefixRE = new UncheckedRE(
34: "=\\?[^?]+\\?[bq]\\?");
35:
36: public static String decode(String encoded) {
37: if (encoded == null)
38: return null;
39: // Fail fast.
40: if (encoded.indexOf("=?") < 0)
41: return encoded;
42: REMatch match = prefixRE.getMatch(encoded.toLowerCase());
43: if (match == null) {
44: Log.error("RFC2047.decode prefix is null");
45: Log.error("encoded = |" + encoded + "|");
46: return encoded;
47: }
48: String prefix = match.toString();
49: int index = match.getStartIndex();
50: String charset = prefix.substring(2, prefix.length() - 3);
51: String encoding = Utilities.getEncodingFromCharset(charset);
52: FastStringBuffer sb = new FastStringBuffer();
53: sb.append(encoded.substring(0, index));
54: String remaining = encoded.substring(index);
55: while (true) {
56: int begin = prefix.length();
57: int end = remaining.indexOf("?=", begin);
58: if (end < 0) {
59: // Error.
60: sb.append(remaining);
61: Log.error("RFC2047.decode error");
62: Log.error("encoded = |" + encoded + "|");
63: Log.error("remaining = |" + remaining + "|");
64: return sb.toString();
65: }
66: byte[] bytes = null;
67: if (prefix.endsWith("?b?"))
68: bytes = Base64Decoder.decode(remaining.substring(begin,
69: end));
70: else if (prefix.endsWith("?q?"))
71: bytes = QuotedPrintableDecoder.decode(remaining
72: .substring(begin, end));
73: if (bytes == null) {
74: Log.error("RFC2047.decode error");
75: Log.error("encoded = |" + encoded + "|");
76: return encoded;
77: }
78: try {
79: sb.append(new String(bytes, encoding));
80: } catch (UnsupportedEncodingException e) {
81: Log.error(e);
82: return encoded;
83: }
84: remaining = remaining.substring(end + 2);
85: index = remaining.toLowerCase().indexOf(prefix);
86: if (index < 0) {
87: sb.append(remaining);
88: break;
89: }
90: sb.append(remaining.substring(0, index));
91: remaining = remaining.substring(index);
92: }
93: return sb.toString();
94: }
95: }
|