2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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.
30 #include "quoted-printable.h"
32 #define ENCODED_WORD_BEGIN "=?"
33 #define ENCODED_WORD_END "?="
35 /* Decodes headers based on RFC2045 and RFC2047. */
37 void unmime_header(gchar *out, const gchar *str)
42 const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
50 gchar *decoded_text = NULL;
52 eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
57 encoding_begin_p = strchr(eword_begin_p + 2, '?');
58 if (!encoding_begin_p) {
62 text_begin_p = strchr(encoding_begin_p + 1, '?');
67 eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
74 memcpy(outp, p, eword_begin_p - p);
75 outp += eword_begin_p - p;
78 /* ignore spaces between encoded words */
79 for (sp = p; sp < eword_begin_p; sp++) {
81 memcpy(outp, p, eword_begin_p - p);
82 outp += eword_begin_p - p;
89 len = MIN(sizeof(charset) - 1,
90 encoding_begin_p - (eword_begin_p + 2));
91 memcpy(charset, eword_begin_p + 2, len);
93 encoding = toupper(*(encoding_begin_p + 1));
95 if (encoding == 'B') {
96 decoded_text = g_malloc
97 (eword_end_p - (text_begin_p + 1) + 1);
98 len = base64_decode(decoded_text, text_begin_p + 1,
99 eword_end_p - (text_begin_p + 1));
100 decoded_text[len] = '\0';
101 } else if (encoding == 'Q') {
102 decoded_text = g_malloc
103 (eword_end_p - (text_begin_p + 1) + 1);
104 len = qp_decode_q_encoding
105 (decoded_text, text_begin_p + 1,
106 eword_end_p - (text_begin_p + 1));
108 memcpy(outp, p, eword_end_p + 2 - p);
109 outp += eword_end_p + 2 - p;
114 /* convert to locale encoding */
115 conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
117 len = strlen(conv_str);
118 memcpy(outp, conv_str, len);
121 len = strlen(decoded_text);
122 conv_localetodisp(outp, len + 1, decoded_text);
126 g_free(decoded_text);