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)
41 const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
49 gchar *decoded_text = NULL;
51 eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
56 encoding_begin_p = strchr(eword_begin_p + 2, '?');
57 if (!encoding_begin_p) {
61 text_begin_p = strchr(encoding_begin_p + 1, '?');
66 eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
73 memcpy(outp, p, eword_begin_p - p);
74 outp += eword_begin_p - p;
77 /* ignore spaces between encoded words */
80 for (sp = p; sp < eword_begin_p; sp++) {
81 if (!isspace(*(const guchar *)sp)) {
82 memcpy(outp, p, eword_begin_p - p);
83 outp += eword_begin_p - p;
90 len = MIN(sizeof(charset) - 1,
91 encoding_begin_p - (eword_begin_p + 2));
92 memcpy(charset, eword_begin_p + 2, len);
94 encoding = toupper(*(encoding_begin_p + 1));
96 if (encoding == 'B') {
97 decoded_text = g_malloc
98 (eword_end_p - (text_begin_p + 1) + 1);
99 len = base64_decode(decoded_text, text_begin_p + 1,
100 eword_end_p - (text_begin_p + 1));
101 decoded_text[len] = '\0';
102 } else if (encoding == 'Q') {
103 decoded_text = g_malloc
104 (eword_end_p - (text_begin_p + 1) + 1);
105 len = qp_decode_q_encoding
106 (decoded_text, text_begin_p + 1,
107 eword_end_p - (text_begin_p + 1));
109 memcpy(outp, p, eword_end_p + 2 - p);
110 outp += eword_end_p + 2 - p;
115 /* convert to UTF-8 */
116 conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
118 len = strlen(conv_str);
119 memcpy(outp, conv_str, len);
122 len = strlen(decoded_text);
123 conv_utf8todisp(outp, len + 1, decoded_text);
127 g_free(decoded_text);