summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/mime/mailmime_decode.c
Side-by-side diff
Diffstat (limited to 'kmicromail/libetpan/mime/mailmime_decode.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/mime/mailmime_decode.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/kmicromail/libetpan/mime/mailmime_decode.c b/kmicromail/libetpan/mime/mailmime_decode.c
index 3025dcb..e48ec19 100644
--- a/kmicromail/libetpan/mime/mailmime_decode.c
+++ b/kmicromail/libetpan/mime/mailmime_decode.c
@@ -94,202 +94,212 @@ enum {
int mailmime_encoded_phrase_parse(const char * default_fromcode,
const char * message, size_t length,
size_t * index, const char * tocode,
char ** result)
{
MMAPString * gphrase;
struct mailmime_encoded_word * word;
int first;
size_t cur_token;
int r;
int res;
char * str;
char * wordutf8;
int type;
cur_token = * index;
gphrase = mmap_string_new("");
if (gphrase == NULL) {
res = MAILIMF_ERROR_MEMORY;
goto err;
}
first = TRUE;
type = TYPE_ERROR; /* XXX - removes a gcc warning */
while (1) {
r = mailmime_encoded_word_parse(message, length, &cur_token, &word);
if (r == MAILIMF_NO_ERROR) {
if (!first) {
if (type != TYPE_ENCODED_WORD) {
if (mmap_string_append_c(gphrase, ' ') == NULL) {
mailmime_encoded_word_free(word);
res = MAILIMF_ERROR_MEMORY;
goto free;
}
}
}
type = TYPE_ENCODED_WORD;
wordutf8 = NULL;
r = charconv(tocode, word->wd_charset, word->wd_text,
strlen(word->wd_text), &wordutf8);
switch (r) {
case MAIL_CHARCONV_ERROR_MEMORY:
mailmime_encoded_word_free(word);
res = MAILIMF_ERROR_MEMORY;
goto free;
case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET:
case MAIL_CHARCONV_ERROR_CONV:
mailmime_encoded_word_free(word);
res = MAILIMF_ERROR_PARSE;
goto free;
}
if (wordutf8 != NULL) {
if (mmap_string_append(gphrase, wordutf8) == NULL) {
mailmime_encoded_word_free(word);
free(wordutf8);
res = MAILIMF_ERROR_MEMORY;
goto free;
}
free(wordutf8);
}
mailmime_encoded_word_free(word);
first = FALSE;
}
else if (r == MAILIMF_ERROR_PARSE) {
/* do nothing */
}
else {
res = r;
goto free;
}
if (r == MAILIMF_ERROR_PARSE) {
char * raw_word;
r = mailmime_non_encoded_word_parse(message, length,
&cur_token, &raw_word);
if (r == MAILIMF_NO_ERROR) {
if (!first) {
if (mmap_string_append_c(gphrase, ' ') == NULL) {
free(raw_word);
res = MAILIMF_ERROR_MEMORY;
goto free;
}
}
type = TYPE_WORD;
wordutf8 = NULL;
r = charconv(tocode, default_fromcode, raw_word,
strlen(raw_word), &wordutf8);
- if (wordutf8 != NULL) {
- if (mmap_string_append(gphrase, wordutf8) == NULL) {
- free(wordutf8);
- free(raw_word);
- res = MAILIMF_ERROR_MEMORY;
- goto free;
- }
+ switch (r) {
+ case MAIL_CHARCONV_ERROR_MEMORY:
+ free(raw_word);
+ res = MAILIMF_ERROR_MEMORY;
+ goto free;
+ case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET:
+ case MAIL_CHARCONV_ERROR_CONV:
+ free(raw_word);
+ res = MAILIMF_ERROR_PARSE;
+ goto free;
+ }
+
+ if (mmap_string_append(gphrase, wordutf8) == NULL) {
free(wordutf8);
+ free(raw_word);
+ res = MAILIMF_ERROR_MEMORY;
+ goto free;
}
+
free(raw_word);
first = FALSE;
}
else if (r == MAILIMF_ERROR_PARSE) {
break;
}
else {
res = r;
goto free;
}
}
}
if (first) {
res = MAILIMF_ERROR_PARSE;
goto free;
}
str = strdup(gphrase->str);
if (str == NULL) {
res = MAILIMF_ERROR_MEMORY;
goto free;
}
mmap_string_free(gphrase);
* result = str;
* index = cur_token;
return MAILIMF_NO_ERROR;
free:
mmap_string_free(gphrase);
err:
return res;
}
static int
mailmime_non_encoded_word_parse(const char * message, size_t length,
size_t * index,
char ** result)
{
int end;
size_t cur_token;
int res;
char * text;
int r;
size_t begin;
cur_token = * index;
r = mailimf_fws_parse(message, length, &cur_token);
if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) {
res = r;
goto err;
}
begin = cur_token;
end = FALSE;
while (1) {
if (cur_token >= length)
break;
switch (message[cur_token]) {
case ' ':
case '\t':
case '\r':
case '\n':
end = TRUE;
break;
}
if (end)
break;
cur_token ++;
}
if (cur_token - begin == 0) {
res = MAILIMF_ERROR_PARSE;
goto err;
}
text = malloc(cur_token - begin + 1);
if (text == NULL) {
res = MAILIMF_ERROR_MEMORY;
goto err;
}
memcpy(text, message + begin, cur_token - begin);
text[cur_token - begin] = '\0';
* index = cur_token;
* result = text;
return MAILIMF_NO_ERROR;