]> granicus.if.org Git - neomutt/commitdiff
Move mutt_rfc2047_choose_charset to mutt_ch_choose
authorPietro Cerutti <gahr@gahr.ch>
Fri, 19 Jan 2018 15:34:03 +0000 (15:34 +0000)
committerPietro Cerutti <gahr@gahr.ch>
Fri, 19 Jan 2018 15:34:03 +0000 (15:34 +0000)
Issue #1015

mutt/charset.c
mutt/charset.h
mutt/rfc2047.c
mutt/rfc2047.h
rfc2231.c

index 1b35fd01b5cb0e5f96bca83c9166d52cebc3133c..a867d5e1572a29eb2575b19283acaa1505f9fe7d 100644 (file)
@@ -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;
+}
+
index 6b3b070307cbb0203c20aa06092597fcb270ac1b..3447e6d5c2ab044a51e77aaae831bb136416a0c7 100644 (file)
@@ -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")
 
index 3213f4bda7f69817fa98bdb402d8114b807218ce..f70ab00cbf8b7d09d1df2e8b492cd59f3cca2e87 100644 (file)
@@ -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
index 54f8ceef0d9c8609d3bacea2ff4d9d78c2e6a650..7e031b5b2e43a1b1dd89f3367d878f567840f6d1 100644 (file)
@@ -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 */
index ae582507cc038bdabd13bb9db39de422cf183684..34da9df5a79340ec999bc81a25dcc42bea9a1254 100644 (file)
--- 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);