2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 gchar
*unmime_header(const gchar
*encoded_str
)
39 const gchar
*p
= encoded_str
;
40 const gchar
*eword_begin_p
, *encoding_begin_p
, *text_begin_p
,
49 outbuf
= g_string_sized_new(strlen(encoded_str
) * 2);
52 gchar
*decoded_text
= NULL
;
55 eword_begin_p
= strstr(p
, ENCODED_WORD_BEGIN
);
57 g_string_append(outbuf
, p
);
60 encoding_begin_p
= strchr(eword_begin_p
+ 2, '?');
61 if (!encoding_begin_p
) {
62 g_string_append(outbuf
, p
);
65 text_begin_p
= strchr(encoding_begin_p
+ 1, '?');
67 g_string_append(outbuf
, p
);
70 eword_end_p
= strstr(text_begin_p
+ 1, ENCODED_WORD_END
);
72 g_string_append(outbuf
, p
);
76 if (p
== encoded_str
) {
77 g_string_append_len(outbuf
, p
, eword_begin_p
- p
);
80 /* ignore spaces between encoded words */
83 for (sp
= p
; sp
< eword_begin_p
; sp
++) {
84 if (!g_ascii_isspace(*sp
)) {
86 (outbuf
, p
, eword_begin_p
- p
);
93 len
= MIN(sizeof(charset
) - 1,
94 encoding_begin_p
- (eword_begin_p
+ 2));
95 memcpy(charset
, eword_begin_p
+ 2, len
);
97 encoding
= g_ascii_toupper(*(encoding_begin_p
+ 1));
99 if (encoding
== 'B') {
100 decoded_text
= g_malloc
101 (eword_end_p
- (text_begin_p
+ 1) + 1);
102 len
= base64_decode(decoded_text
, text_begin_p
+ 1,
103 eword_end_p
- (text_begin_p
+ 1));
104 decoded_text
[len
] = '\0';
105 } else if (encoding
== 'Q') {
106 decoded_text
= g_malloc
107 (eword_end_p
- (text_begin_p
+ 1) + 1);
108 len
= qp_decode_q_encoding
109 (decoded_text
, text_begin_p
+ 1,
110 eword_end_p
- (text_begin_p
+ 1));
112 g_string_append_len(outbuf
, p
, eword_end_p
+ 2 - p
);
117 /* convert to UTF-8 */
118 conv_str
= conv_codeset_strdup(decoded_text
, charset
, NULL
);
119 if (!conv_str
|| !g_utf8_validate(conv_str
, -1, NULL
)) {
121 conv_str
= g_malloc(len
+ 1);
122 conv_utf8todisp(conv_str
, len
+ 1, decoded_text
);
124 g_string_append(outbuf
, conv_str
);
127 g_free(decoded_text
);
132 out_str
= outbuf
->str
;
133 out_len
= outbuf
->len
;
134 g_string_free(outbuf
, FALSE
);
136 return g_realloc(out_str
, out_len
+ 1);