-rw-r--r-- | kmicromail/libetpan/mime/mailmime_decode.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/kmicromail/libetpan/mime/mailmime_decode.c b/kmicromail/libetpan/mime/mailmime_decode.c index b2ab0f7..bb7638e 100644 --- a/kmicromail/libetpan/mime/mailmime_decode.c +++ b/kmicromail/libetpan/mime/mailmime_decode.c | |||
@@ -1,250 +1,250 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 15 | * 3. Neither the name of the libEtPan! project nor the names of its |
16 | * contributors may be used to endorse or promote products derived | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | /* | 36 | /* |
37 | RFC 2047 : MIME (Multipurpose Internet Mail Extensions) Part Three: | 37 | RFC 2047 : MIME (Multipurpose Internet Mail Extensions) Part Three: |
38 | Message Header Extensions for Non-ASCII Text | 38 | Message Header Extensions for Non-ASCII Text |
39 | */ | 39 | */ |
40 | 40 | ||
41 | #include "mailmime_decode.h" | 41 | #include "mailmime_decode.h" |
42 | 42 | ||
43 | #include <ctype.h> | 43 | #include <ctype.h> |
44 | #include <unistd.h> | 44 | #include <unistd.h> |
45 | #include <sys/mman.h> | 45 | #include <sys/mman.h> |
46 | #include <string.h> | 46 | #include <string.h> |
47 | #include <stdlib.h> | 47 | #include <stdlib.h> |
48 | 48 | ||
49 | #include "mailmime_content.h" | 49 | #include "mailmime_content.h" |
50 | 50 | ||
51 | #include "charconv.h" | 51 | #include "charconv.h" |
52 | #include "mmapstring.h" | 52 | #include "mmapstring.h" |
53 | #include "mailimf.h" | 53 | #include "mailimf.h" |
54 | 54 | ||
55 | #ifndef TRUE | 55 | #ifndef TRUE |
56 | #define TRUE 1 | 56 | #define TRUE 1 |
57 | #endif | 57 | #endif |
58 | 58 | ||
59 | #ifndef FALSE | 59 | #ifndef FALSE |
60 | #define FALSE 0 | 60 | #define FALSE 0 |
61 | #endif | 61 | #endif |
62 | 62 | ||
63 | static int mailmime_charset_parse(const char * message, size_t length, | 63 | static int mailmime_charset_parse(const char * message, size_t length, |
64 | size_t * index, char ** charset); | 64 | size_t * index, char ** charset); |
65 | 65 | ||
66 | enum { | 66 | enum { |
67 | MAILMIME_ENCODING_B, | 67 | MAILMIME_ENCODING_B, |
68 | MAILMIME_ENCODING_Q | 68 | MAILMIME_ENCODING_Q |
69 | }; | 69 | }; |
70 | 70 | ||
71 | static int mailmime_encoding_parse(const char * message, size_t length, | 71 | static int mailmime_encoding_parse(const char * message, size_t length, |
72 | size_t * index, int * result); | 72 | size_t * index, int * result); |
73 | 73 | ||
74 | static int mailmime_etoken_parse(const char * message, size_t length, | 74 | static int mailmime_etoken_parse(const char * message, size_t length, |
75 | size_t * index, char ** result); | 75 | size_t * index, char ** result); |
76 | 76 | ||
77 | static int | 77 | static int |
78 | mailmime_non_encoded_word_parse(const char * message, size_t length, | 78 | mailmime_non_encoded_word_parse(const char * message, size_t length, |
79 | size_t * index, | 79 | size_t * index, |
80 | char ** result); | 80 | char ** result); |
81 | 81 | ||
82 | static int | 82 | static int |
83 | mailmime_encoded_word_parse(const char * message, size_t length, | 83 | mailmime_encoded_word_parse(const char * message, size_t length, |
84 | size_t * index, | 84 | size_t * index, |
85 | struct mailmime_encoded_word ** result); | 85 | struct mailmime_encoded_word ** result); |
86 | 86 | ||
87 | 87 | ||
88 | enum { | 88 | enum { |
89 | TYPE_ERROR, | 89 | TYPE_ERROR, |
90 | TYPE_WORD, | 90 | TYPE_WORD, |
91 | TYPE_ENCODED_WORD, | 91 | TYPE_ENCODED_WORD, |
92 | }; | 92 | }; |
93 | 93 | ||
94 | int mailmime_encoded_phrase_parse(const char * default_fromcode, | 94 | int mailmime_encoded_phrase_parse(const char * default_fromcode, |
95 | const char * message, size_t length, | 95 | const char * message, size_t length, |
96 | size_t * index, const char * tocode, | 96 | size_t * index, const char * tocode, |
97 | char ** result) | 97 | char ** result) |
98 | { | 98 | { |
99 | MMAPString * gphrase; | 99 | MMAPString * gphrase; |
100 | struct mailmime_encoded_word * word; | 100 | struct mailmime_encoded_word * word; |
101 | int first; | 101 | int first; |
102 | size_t cur_token; | 102 | size_t cur_token; |
103 | int r; | 103 | int r; |
104 | int res; | 104 | int res; |
105 | char * str; | 105 | char * str; |
106 | char * wordutf8; | 106 | char * wordutf8; |
107 | int type; | 107 | int type; |
108 | 108 | int appendNewLine; | |
109 | cur_token = * index; | 109 | cur_token = * index; |
110 | 110 | ||
111 | gphrase = mmap_string_new(""); | 111 | gphrase = mmap_string_new(""); |
112 | if (gphrase == NULL) { | 112 | if (gphrase == NULL) { |
113 | res = MAILIMF_ERROR_MEMORY; | 113 | res = MAILIMF_ERROR_MEMORY; |
114 | goto err; | 114 | goto err; |
115 | } | 115 | } |
116 | 116 | ||
117 | first = TRUE; | 117 | first = TRUE; |
118 | 118 | ||
119 | type = TYPE_ERROR; /* XXX - removes a gcc warning */ | 119 | type = TYPE_ERROR; /* XXX - removes a gcc warning */ |
120 | // LUTZ add | 120 | /* LUTZ add*/ |
121 | int appendNewLine = FALSE; | 121 | appendNewLine = FALSE; |
122 | while (1) { //while | 122 | while (1) { |
123 | 123 | ||
124 | r = mailmime_encoded_word_parse(message, length, &cur_token, &word); | 124 | r = mailmime_encoded_word_parse(message, length, &cur_token, &word); |
125 | if (r == MAILIMF_NO_ERROR) { | 125 | if (r == MAILIMF_NO_ERROR) { |
126 | if (!first) { | 126 | if (!first) { |
127 | if (type != TYPE_ENCODED_WORD) { | 127 | if (type != TYPE_ENCODED_WORD) { |
128 | if (mmap_string_append_c(gphrase, ' ') == NULL) { | 128 | if (mmap_string_append_c(gphrase, ' ') == NULL) { |
129 | mailmime_encoded_word_free(word); | 129 | mailmime_encoded_word_free(word); |
130 | res = MAILIMF_ERROR_MEMORY; | 130 | res = MAILIMF_ERROR_MEMORY; |
131 | goto free; | 131 | goto free; |
132 | } | 132 | } |
133 | } | 133 | } |
134 | } | 134 | } |
135 | type = TYPE_ENCODED_WORD; | 135 | type = TYPE_ENCODED_WORD; |
136 | wordutf8 = NULL; | 136 | wordutf8 = NULL; |
137 | r = charconv(tocode, word->wd_charset, word->wd_text, | 137 | r = charconv(tocode, word->wd_charset, word->wd_text, |
138 | strlen(word->wd_text), &wordutf8); | 138 | strlen(word->wd_text), &wordutf8); |
139 | switch (r) { | 139 | switch (r) { |
140 | case MAIL_CHARCONV_ERROR_MEMORY: | 140 | case MAIL_CHARCONV_ERROR_MEMORY: |
141 | mailmime_encoded_word_free(word); | 141 | mailmime_encoded_word_free(word); |
142 | res = MAILIMF_ERROR_MEMORY; | 142 | res = MAILIMF_ERROR_MEMORY; |
143 | goto free; | 143 | goto free; |
144 | 144 | ||
145 | case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: | 145 | case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: |
146 | case MAIL_CHARCONV_ERROR_CONV: | 146 | case MAIL_CHARCONV_ERROR_CONV: |
147 | mailmime_encoded_word_free(word); | 147 | mailmime_encoded_word_free(word); |
148 | res = MAILIMF_ERROR_PARSE; | 148 | res = MAILIMF_ERROR_PARSE; |
149 | goto free; | 149 | goto free; |
150 | } | 150 | } |
151 | 151 | ||
152 | if (wordutf8 != NULL) { | 152 | if (wordutf8 != NULL) { |
153 | if (mmap_string_append(gphrase, wordutf8) == NULL) { | 153 | if (mmap_string_append(gphrase, wordutf8) == NULL) { |
154 | mailmime_encoded_word_free(word); | 154 | mailmime_encoded_word_free(word); |
155 | free(wordutf8); | 155 | free(wordutf8); |
156 | res = MAILIMF_ERROR_MEMORY; | 156 | res = MAILIMF_ERROR_MEMORY; |
157 | goto free; | 157 | goto free; |
158 | } | 158 | } |
159 | free(wordutf8); | 159 | free(wordutf8); |
160 | } | 160 | } |
161 | mailmime_encoded_word_free(word); | 161 | mailmime_encoded_word_free(word); |
162 | first = FALSE; | 162 | first = FALSE; |
163 | } | 163 | } |
164 | else if (r == MAILIMF_ERROR_PARSE) { | 164 | else if (r == MAILIMF_ERROR_PARSE) { |
165 | /* do nothing */ | 165 | /* do nothing */ |
166 | } | 166 | } |
167 | else { | 167 | else { |
168 | res = r; | 168 | res = r; |
169 | goto free; | 169 | goto free; |
170 | } | 170 | } |
171 | 171 | ||
172 | if (r == MAILIMF_ERROR_PARSE) { | 172 | if (r == MAILIMF_ERROR_PARSE) { |
173 | char * raw_word; | 173 | char * raw_word; |
174 | 174 | ||
175 | r = mailmime_non_encoded_word_parse(message, length, | 175 | r = mailmime_non_encoded_word_parse(message, length, |
176 | &cur_token, &raw_word); | 176 | &cur_token, &raw_word); |
177 | if (r == MAILIMF_NO_ERROR) { | 177 | if (r == MAILIMF_NO_ERROR) { |
178 | if (!first) { | 178 | if (!first) { |
179 | if (mmap_string_append_c(gphrase, ' ') == NULL) { | 179 | if (mmap_string_append_c(gphrase, ' ') == NULL) { |
180 | free(raw_word); | 180 | free(raw_word); |
181 | res = MAILIMF_ERROR_MEMORY; | 181 | res = MAILIMF_ERROR_MEMORY; |
182 | goto free; | 182 | goto free; |
183 | } | 183 | } |
184 | } | 184 | } |
185 | type = TYPE_WORD; | 185 | type = TYPE_WORD; |
186 | 186 | ||
187 | wordutf8 = NULL; | 187 | wordutf8 = NULL; |
188 | r = charconv(tocode, default_fromcode, raw_word, | 188 | r = charconv(tocode, default_fromcode, raw_word, |
189 | strlen(raw_word), &wordutf8); | 189 | strlen(raw_word), &wordutf8); |
190 | 190 | ||
191 | switch (r) { | 191 | switch (r) { |
192 | case MAIL_CHARCONV_ERROR_MEMORY: | 192 | case MAIL_CHARCONV_ERROR_MEMORY: |
193 | free(raw_word); | 193 | free(raw_word); |
194 | res = MAILIMF_ERROR_MEMORY; | 194 | res = MAILIMF_ERROR_MEMORY; |
195 | goto free; | 195 | goto free; |
196 | 196 | ||
197 | case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: | 197 | case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: |
198 | case MAIL_CHARCONV_ERROR_CONV: | 198 | case MAIL_CHARCONV_ERROR_CONV: |
199 | free(raw_word); | 199 | free(raw_word); |
200 | res = MAILIMF_ERROR_PARSE; | 200 | res = MAILIMF_ERROR_PARSE; |
201 | goto free; | 201 | goto free; |
202 | } | 202 | } |
203 | // LUTZ add | 203 | // LUTZ add |
204 | if ( appendNewLine ) { | 204 | if ( appendNewLine ) { |
205 | appendNewLine = FALSE; | 205 | appendNewLine = FALSE; |
206 | if (mmap_string_append(gphrase, "\n") == NULL) { | 206 | if (mmap_string_append(gphrase, "\n") == NULL) { |
207 | free(wordutf8); | 207 | free(wordutf8); |
208 | free(raw_word); | 208 | free(raw_word); |
209 | res = MAILIMF_ERROR_MEMORY; | 209 | res = MAILIMF_ERROR_MEMORY; |
210 | goto free; | 210 | goto free; |
211 | } | 211 | } |
212 | } | 212 | } |
213 | //fprintf(stderr,"append *%s* \n",wordutf8 ); | 213 | //fprintf(stderr,"append *%s* \n",wordutf8 ); |
214 | if (mmap_string_append(gphrase, wordutf8) == NULL) { | 214 | if (mmap_string_append(gphrase, wordutf8) == NULL) { |
215 | free(wordutf8); | 215 | free(wordutf8); |
216 | free(raw_word); | 216 | free(raw_word); |
217 | res = MAILIMF_ERROR_MEMORY; | 217 | res = MAILIMF_ERROR_MEMORY; |
218 | goto free; | 218 | goto free; |
219 | } | 219 | } |
220 | // LUTZ fix | 220 | // LUTZ fix |
221 | free(wordutf8); | 221 | free(wordutf8); |
222 | free(raw_word); | 222 | free(raw_word); |
223 | first = FALSE; | 223 | first = FALSE; |
224 | } | 224 | } |
225 | else if (r == MAILIMF_ERROR_PARSE) { | 225 | else if (r == MAILIMF_ERROR_PARSE) { |
226 | // LUTZ add | 226 | // LUTZ add |
227 | if ( cur_token >= length ) | 227 | if ( cur_token >= length ) |
228 | break; | 228 | break; |
229 | ++cur_token; | 229 | ++cur_token; |
230 | appendNewLine = TRUE; | 230 | appendNewLine = TRUE; |
231 | } | 231 | } |
232 | else { | 232 | else { |
233 | res = r; | 233 | res = r; |
234 | goto free; | 234 | goto free; |
235 | } | 235 | } |
236 | } | 236 | } |
237 | } | 237 | } |
238 | 238 | ||
239 | if (first) { | 239 | if (first) { |
240 | res = MAILIMF_ERROR_PARSE; | 240 | res = MAILIMF_ERROR_PARSE; |
241 | goto free; | 241 | goto free; |
242 | } | 242 | } |
243 | 243 | ||
244 | str = strdup(gphrase->str); | 244 | str = strdup(gphrase->str); |
245 | if (str == NULL) { | 245 | if (str == NULL) { |
246 | res = MAILIMF_ERROR_MEMORY; | 246 | res = MAILIMF_ERROR_MEMORY; |
247 | goto free; | 247 | goto free; |
248 | } | 248 | } |
249 | mmap_string_free(gphrase); | 249 | mmap_string_free(gphrase); |
250 | 250 | ||