fix bug 3235, 'Extraneous double quotes inside base64-encoded From header confuse...
[claws.git] / src / unmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
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 3 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "codeconv.h"
30 #include "base64.h"
31 #include "quoted-printable.h"
32
33 #define ENCODED_WORD_BEGIN      "=?"
34 #define ENCODED_WORD_END        "?="
35
36 /* Decodes headers based on RFC2045 and RFC2047. */
37
38 gchar *unmime_header(const gchar *encoded_str, gboolean addr_field)
39 {
40         const gchar *p = encoded_str;
41         const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
42                     *eword_end_p;
43         gchar charset[32];
44         gchar encoding;
45         gchar *conv_str;
46         GString *outbuf;
47         gchar *out_str;
48         gsize out_len;
49         int in_quote = FALSE;
50
51         outbuf = g_string_sized_new(strlen(encoded_str) * 2);
52
53         while (*p != '\0') {
54                 gchar *decoded_text = NULL;
55                 const gchar *quote_p;
56                 gint len;
57
58                 eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
59                 if (!eword_begin_p) {
60                         g_string_append(outbuf, p);
61                         break;
62                 }
63                 
64                 quote_p = p;
65                 while ((quote_p = strchr(quote_p, '"')) != NULL) {
66                         if (quote_p && quote_p < eword_begin_p) {
67                                 /* Found a quote before the encoded word. */
68                                 in_quote = !in_quote;
69                                 quote_p++;
70                         }
71                         if (quote_p >= eword_begin_p)
72                                 break;
73                 }
74
75                 encoding_begin_p = strchr(eword_begin_p + 2, '?');
76                 if (!encoding_begin_p) {
77                         g_string_append(outbuf, p);
78                         break;
79                 }
80                 text_begin_p = strchr(encoding_begin_p + 1, '?');
81                 if (!text_begin_p) {
82                         g_string_append(outbuf, p);
83                         break;
84                 }
85                 eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END);
86                 if (!eword_end_p) {
87                         g_string_append(outbuf, p);
88                         break;
89                 }
90
91                 if (p == encoded_str) {
92                         g_string_append_len(outbuf, p, eword_begin_p - p);
93                         p = eword_begin_p;
94                 } else {
95                         /* ignore spaces between encoded words */
96                         const gchar *sp;
97
98                         for (sp = p; sp < eword_begin_p; sp++) {
99                                 if (!g_ascii_isspace(*sp)) {
100                                         g_string_append_len
101                                                 (outbuf, p, eword_begin_p - p);
102                                         p = eword_begin_p;
103                                         break;
104                                 }
105                         }
106                 }
107
108                 len = MIN(sizeof(charset) - 1,
109                           encoding_begin_p - (eword_begin_p + 2));
110                 memcpy(charset, eword_begin_p + 2, len);
111                 charset[len] = '\0';
112                 encoding = g_ascii_toupper(*(encoding_begin_p + 1));
113
114                 if (encoding == 'B') {
115                         decoded_text = g_malloc
116                                 (eword_end_p - (text_begin_p + 1) + 1);
117                         len = base64_decode(decoded_text, text_begin_p + 1,
118                                             eword_end_p - (text_begin_p + 1));
119                         decoded_text[len] = '\0';
120                 } else if (encoding == 'Q') {
121                         decoded_text = g_malloc
122                                 (eword_end_p - (text_begin_p + 1) + 1);
123                         len = qp_decode_q_encoding
124                                 (decoded_text, text_begin_p + 1,
125                                  eword_end_p - (text_begin_p + 1));
126                 } else {
127                         g_string_append_len(outbuf, p, eword_end_p + 2 - p);
128                         p = eword_end_p + 2;
129                         continue;
130                 }
131
132                 /* An encoded word MUST not appear within a quoted string,
133                  * so quoting that word after decoding should be safe.
134                  * We check there are no quotes just to be sure. If there
135                  * are, well, the comma won't pose a problem, probably.
136                  */
137                 if (addr_field && strchr(decoded_text, ',') && !in_quote &&
138                     !strchr(decoded_text, '"')) {
139                         gchar *tmp = g_strdup_printf("\"%s\"", decoded_text);
140                         g_free(decoded_text);
141                         decoded_text = tmp;
142                 }
143
144                 /* convert to UTF-8 */
145                 conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
146                 if (!conv_str || !g_utf8_validate(conv_str, -1, NULL)) {
147                         g_free(conv_str);
148                         conv_str = g_malloc(len + 1);
149                         conv_utf8todisp(conv_str, len + 1, decoded_text);
150                 }
151                 g_string_append(outbuf, conv_str);
152                 g_free(conv_str);
153
154                 g_free(decoded_text);
155
156                 p = eword_end_p + 2;
157         }
158         
159         out_str = outbuf->str;
160         out_len = outbuf->len;
161         g_string_free(outbuf, FALSE);
162
163         return g_realloc(out_str, out_len + 1);
164 }