2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #define ENCODED_WORD_BEGIN "=?"
32 #define ENCODED_WORD_END "?="
34 static gboolean get_hex_value(gchar *out, gchar c1, gchar c2);
36 /* Decodes headers based on RFC2045 and RFC2047. */
38 void unmime_header(gchar *out, const gchar *str)
43 const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
51 gchar *decoded_text = NULL;
53 eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
58 encoding_begin_p = strchr(eword_begin_p + 2, '?');
59 if (!encoding_begin_p) {
63 text_begin_p = strchr(encoding_begin_p + 1, '?');
68 eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
75 memcpy(outp, p, eword_begin_p - p);
76 outp += eword_begin_p - p;
79 /* ignore spaces between encoded words */
80 for (sp = p; sp < eword_begin_p; sp++) {
82 memcpy(outp, p, eword_begin_p - p);
83 outp += eword_begin_p - p;
90 charset = g_strndup(eword_begin_p + 2,
91 encoding_begin_p - (eword_begin_p + 2));
92 encoding = toupper(*(encoding_begin_p + 1));
94 if (encoding == 'B') {
98 g_strndup(text_begin_p + 1,
99 eword_end_p - (text_begin_p + 1));
100 decoded_text = g_malloc
101 (eword_end_p - (text_begin_p + 1) + 1);
103 len = from64tobits(decoded_text, encoded_text); //
104 decoded_text[len] = '\0';
106 g_free(encoded_text);
107 } else if (encoding == 'Q') {
108 const gchar *ep = text_begin_p + 1;
111 dp = decoded_text = g_malloc(eword_end_p - ep + 1);
113 while (ep < eword_end_p) {
114 if (*ep == '=' && ep + 3 <= eword_end_p) {
115 if (get_hex_value(dp, ep[1], ep[2])
121 } else if (*ep == '_') {
132 memcpy(outp, p, eword_end_p + 2 - p);
133 outp += eword_end_p + 2 - p;
139 /* convert to locale encoding */
140 conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
142 len = strlen(conv_str);
143 memcpy(outp, conv_str, len);
146 len = strlen(decoded_text);
147 memcpy(outp, decoded_text, len);
151 g_free(decoded_text);
160 gint unmime_quoted_printable_line(gchar *str)
162 gchar *inp = str, *outp = str;
164 while (*inp != '\0') {
166 if (inp[1] && inp[2] &&
167 get_hex_value(outp, inp[1], inp[2]) == TRUE) {
169 } else if (inp[1] == '\0' || isspace(inp[1])) {
170 /* soft line break */
173 /* broken QP string */
187 #define HEX_TO_INT(val, hex) \
191 if ('0' <= c && c <= '9') { \
193 } else if ('a' <= c && c <= 'f') { \
194 val = c - 'a' + 10; \
195 } else if ('A' <= c && c <= 'F') { \
196 val = c - 'A' + 10; \
202 static gboolean get_hex_value(gchar *out, gchar c1, gchar c2)
209 if (hi == -1 || lo == -1)
212 *out = (hi << 4) + lo;