diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-11-19 10:05:14 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-11-19 10:05:14 +0000 |
commit | 7428ea06d530334f715f7e33a3da6ed0183c4967 (patch) | |
tree | 22ac5dd34c1580a6fa3700616f61dbd29c3b1401 /src/libmime/mime_encoding.c | |
parent | bd455692ef8f174210fc48f0048feed392008782 (diff) | |
download | rspamd-7428ea06d530334f715f7e33a3da6ed0183c4967.tar.gz rspamd-7428ea06d530334f715f7e33a3da6ed0183c4967.zip |
[Minor] Unify converters usage
Diffstat (limited to 'src/libmime/mime_encoding.c')
-rw-r--r-- | src/libmime/mime_encoding.c | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/src/libmime/mime_encoding.c b/src/libmime/mime_encoding.c index 7872fa330..7ef492f9d 100644 --- a/src/libmime/mime_encoding.c +++ b/src/libmime/mime_encoding.c @@ -135,7 +135,10 @@ rspamd_converter_to_uchars (struct rspamd_charset_converter *cnv, struct rspamd_charset_converter * -rspamd_mime_get_converter_cached (const gchar *enc, UErrorCode *err) +rspamd_mime_get_converter_cached (const gchar *enc, + rspamd_mempool_t *pool, + gboolean is_canon, + UErrorCode *err) { const gchar *canon_name; static rspamd_lru_hash_t *cache; @@ -147,7 +150,19 @@ rspamd_mime_get_converter_cached (const gchar *enc, UErrorCode *err) rspamd_str_equal); } - canon_name = ucnv_getStandardName (enc, "IANA", err); + if (enc == NULL) { + return NULL; + } + + if (!is_canon) { + rspamd_ftok_t cset_tok; + + RSPAMD_FTOK_FROM_STR (&cset_tok, enc); + canon_name = rspamd_mime_detect_charset (&cset_tok, pool); + } + else { + canon_name = enc; + } if (canon_name == NULL) { return NULL; @@ -306,7 +321,7 @@ rspamd_mime_text_to_utf8 (rspamd_mempool_t *pool, UConverter *utf8_converter; struct rspamd_charset_converter *conv; - conv = rspamd_mime_get_converter_cached (in_enc, &uc_err); + conv = rspamd_mime_get_converter_cached (in_enc, pool, TRUE, &uc_err); utf8_converter = rspamd_get_utf8_converter (); if (conv == NULL) { @@ -370,7 +385,8 @@ rspamd_mime_text_part_utf8_convert (struct rspamd_task *task, UConverter *utf8_converter; struct rspamd_charset_converter *conv; - conv = rspamd_mime_get_converter_cached (charset, &uc_err); + conv = rspamd_mime_get_converter_cached (charset, task->task_pool, + TRUE, &uc_err); utf8_converter = rspamd_get_utf8_converter (); if (conv == NULL) { @@ -429,6 +445,7 @@ rspamd_mime_text_part_utf8_convert (struct rspamd_task *task, gboolean rspamd_mime_to_utf8_byte_array (GByteArray *in, GByteArray *out, + rspamd_mempool_t *pool, const gchar *enc) { gint32 r, clen, dlen; @@ -438,6 +455,24 @@ rspamd_mime_to_utf8_byte_array (GByteArray *in, struct rspamd_charset_converter *conv; rspamd_ftok_t charset_tok; + if (in == NULL || in->len == 0) { + return FALSE; + } + + if (enc == NULL) { + /* Assume utf ? */ + if (rspamd_fast_utf8_validate (in->data, in->len) == 0) { + g_byte_array_set_size (out, in->len); + memcpy (out->data, in->data, out->len); + + return TRUE; + } + else { + /* Bad stuff, keep out */ + return FALSE; + } + } + RSPAMD_FTOK_FROM_STR (&charset_tok, enc); if (rspamd_mime_charset_utf_check (&charset_tok, (gchar *)in->data, in->len, @@ -449,7 +484,7 @@ rspamd_mime_to_utf8_byte_array (GByteArray *in, } utf8_converter = rspamd_get_utf8_converter (); - conv = rspamd_mime_get_converter_cached (enc, &uc_err); + conv = rspamd_mime_get_converter_cached (enc, pool, TRUE, &uc_err); if (conv == NULL) { return FALSE; |