From: Pietro Cerutti Date: Fri, 19 Jan 2018 15:34:03 +0000 (+0000) Subject: Move mutt_rfc2047_choose_charset to mutt_ch_choose X-Git-Tag: neomutt-20180223~37^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7738105cf5fecd1c2a4c6afc443b3a83652095b7;p=neomutt Move mutt_rfc2047_choose_charset to mutt_ch_choose Issue #1015 --- diff --git a/mutt/charset.c b/mutt/charset.c index 1b35fd01b..a867d5e15 100644 --- a/mutt/charset.c +++ b/mutt/charset.c @@ -53,6 +53,7 @@ * | mutt_ch_lookup_remove() | Remove all the character set lookups * | mutt_ch_set_charset() | Update the records for a new character set * | mutt_ch_set_langinfo_charset() | Set the user's choice of character set + * | mutt_ch_choose() | Figure the best charset to encode a string */ #include "config.h" @@ -950,3 +951,80 @@ void mutt_ch_set_charset(char *charset) bind_textdomain_codeset(PACKAGE, buffer); #endif } + +/** + * mutt_ch_choose - Figure the best charset to encode a string + * @param[in] fromcode Original charset of the string + * @param[in] charsets Colon-separated list of potential charsets to use + * @param[in] u String to encode + * @param[in] ulen Length of the string to encode + * @param[out] d If not NULL, point it to the converted string + * @param[out] dlen If not NULL, point it to the length of the d string + * @retval ptr Best performing charset + * @retval NULL None could be found + */ +char *mutt_ch_choose(const char *fromcode, const char *charsets, + char *u, size_t ulen, char **d, size_t *dlen) +{ + char canonical_buf[LONG_STRING]; + char *e = NULL, *tocode = NULL; + size_t elen = 0, bestn = 0; + const char *q = NULL; + + for (const char *p = charsets; p; p = q ? q + 1 : 0) + { + char *s = NULL, *t = NULL; + size_t slen, n; + + q = strchr(p, ':'); + + n = q ? q - p : strlen(p); + if (!n) + continue; + + t = mutt_mem_malloc(n + 1); + memcpy(t, p, n); + t[n] = '\0'; + + s = mutt_str_substr_dup(u, u + ulen); + if (mutt_ch_convert_string(&s, fromcode, t, 0)) + { + FREE(&t); + FREE(&s); + continue; + } + slen = mutt_str_strlen(s); + + if (!tocode || n < bestn) + { + bestn = n; + FREE(&tocode); + tocode = t; + if (d) + { + FREE(&e); + e = s; + } + else + FREE(&s); + elen = slen; + } + else + { + FREE(&t); + FREE(&s); + } + } + if (tocode) + { + if (d) + *d = e; + if (dlen) + *dlen = elen; + + mutt_ch_canonical_charset(canonical_buf, sizeof(canonical_buf), tocode); + mutt_str_replace(&tocode, canonical_buf); + } + return tocode; +} + diff --git a/mutt/charset.h b/mutt/charset.h index 6b3b07030..3447e6d5c 100644 --- a/mutt/charset.h +++ b/mutt/charset.h @@ -104,6 +104,9 @@ void mutt_ch_fgetconv_close(struct FgetConv **fc); int mutt_ch_fgetconv(struct FgetConv *fc); char * mutt_ch_fgetconvs(char *buf, size_t buflen, struct FgetConv *fc); +char * mutt_ch_choose(const char *fromcode, const char *charsets, + char *u, size_t ulen, char **d, size_t *dlen); + #define mutt_ch_is_utf8(a) mutt_ch_chscmp(a, "utf-8") #define mutt_ch_is_us_ascii(a) mutt_ch_chscmp(a, "us-ascii") diff --git a/mutt/rfc2047.c b/mutt/rfc2047.c index 3213f4bda..f70ab00cb 100644 --- a/mutt/rfc2047.c +++ b/mutt/rfc2047.c @@ -29,7 +29,6 @@ * * | Function | Description * | :---------------------------- | :----------------------------------------- - * | mutt_rfc2047_choose_charset() | Figure the best charset to encode a string * | mutt_rfc2047_decode() | Decode any RFC2047-encoded header fields * | mutt_rfc2047_encode() | RFC-2047-encode a string */ @@ -493,7 +492,7 @@ static int rfc2047_encode(const char *d, size_t dlen, int col, const char *fromc tocode = fromcode; if (icode) { - tocode1 = mutt_rfc2047_choose_charset(icode, charsets, u, ulen, 0, 0); + tocode1 = mutt_ch_choose(icode, charsets, u, ulen, 0, 0); if (tocode1) tocode = tocode1; else @@ -624,82 +623,6 @@ static int rfc2047_encode(const char *d, size_t dlen, int col, const char *fromc return rc; } -/** - * mutt_rfc2047_choose_charset - Figure the best charset to encode a string - * @param[in] fromcode Original charset of the string - * @param[in] charsets Colon-separated list of potential charsets to use - * @param[in] u String to encode - * @param[in] ulen Length of the string to encode - * @param[out] d If not NULL, point it to the converted string - * @param[out] dlen If not NULL, point it to the length of the d string - * @retval ptr Best performing charset - * @retval NULL None could be found - */ -char *mutt_rfc2047_choose_charset(const char *fromcode, const char *charsets, - char *u, size_t ulen, char **d, size_t *dlen) -{ - char canonical_buf[LONG_STRING]; - char *e = NULL, *tocode = NULL; - size_t elen = 0, bestn = 0; - const char *q = NULL; - - for (const char *p = charsets; p; p = q ? q + 1 : 0) - { - char *s = NULL, *t = NULL; - size_t slen, n; - - q = strchr(p, ':'); - - n = q ? q - p : strlen(p); - if (!n) - continue; - - t = mutt_mem_malloc(n + 1); - memcpy(t, p, n); - t[n] = '\0'; - - s = mutt_str_substr_dup(u, u + ulen); - if (mutt_ch_convert_string(&s, fromcode, t, 0)) - { - FREE(&t); - FREE(&s); - continue; - } - slen = mutt_str_strlen(s); - - if (!tocode || n < bestn) - { - bestn = n; - FREE(&tocode); - tocode = t; - if (d) - { - FREE(&e); - e = s; - } - else - FREE(&s); - elen = slen; - } - else - { - FREE(&t); - FREE(&s); - } - } - if (tocode) - { - if (d) - *d = e; - if (dlen) - *dlen = elen; - - mutt_ch_canonical_charset(canonical_buf, sizeof(canonical_buf), tocode); - mutt_str_replace(&tocode, canonical_buf); - } - return tocode; -} - /** * mutt_rfc2047_encode - RFC-2047-encode a string * @param[in,out] pd String to be encoded, and resulting encoded string diff --git a/mutt/rfc2047.h b/mutt/rfc2047.h index 54f8ceef0..7e031b5b2 100644 --- a/mutt/rfc2047.h +++ b/mutt/rfc2047.h @@ -29,7 +29,5 @@ void mutt_rfc2047_encode(char **pd, const char *specials, int col, const char *charsets); void mutt_rfc2047_decode(char **pd); -char *mutt_rfc2047_choose_charset(const char *fromcode, const char *charsets, - char *u, size_t ulen, char **d, size_t *dlen); #endif /* _MUTT_LIB_RFC2047_H */ diff --git a/rfc2231.c b/rfc2231.c index ae582507c..34da9df5a 100644 --- a/rfc2231.c +++ b/rfc2231.c @@ -325,7 +325,7 @@ int rfc2231_encode_string(char **pd) return 0; if (!Charset || !SendCharset || - !(charset = mutt_rfc2047_choose_charset(Charset, SendCharset, *pd, strlen(*pd), &d, &dlen))) + !(charset = mutt_ch_choose(Charset, SendCharset, *pd, strlen(*pd), &d, &dlen))) { charset = mutt_str_strdup(Charset ? Charset : "unknown-8bit"); FREE(&d);