git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/4] mailinfo: refactor to support utf8 decode attempts

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Sep 19, 2016, 21:08 UTC
Message-ID
<e882f6395d1079037843e6e5b632c0010e1f4cdf.1474319086.git.jonathantanmy@google.com>
In-Reply-To
<cover.1474319086.git.jonathantanmy@google.com>

mailinfo.c currently has a convert_to_utf8 function that overrides its argument and prints an error message when the decoding fails. Refactor it, creating another function that does not override anything and does not print an error message when the decoding fails. This is to be used by a subsequent patch.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 mailinfo.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
diff --git a/mailinfo.c b/mailinfo.c
index 0c4738a..aadad09 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -340,23 +340,47 @@ static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
 	return out;
 }
 
+/*
+ * Attempts to convert line into UTF-8.
+ *
+ * This differs from convert_to_utf8 in that no inputs are overridden, and that
+ * conversion non-success is not considered an error case - mi->input_error is
+ * not set, and no error message is printed.
+ *
+ * If the conversion is unnecessary, returns 1 and stores NULL in result.
+ *
+ * If the conversion is successful, returns 1 and stores the converted string
+ * in result.
+ *
+ * If the conversion is unsuccessful, returns 0 and stores NULL in result.
+ */
+static int try_convert_to_utf8(const struct mailinfo *mi, const char *line,
+			       const char *charset, char **result)
+{
+	if (!mi->metainfo_charset || !charset || !*charset ||
+	    same_encoding(mi->metainfo_charset, charset)) {
+		*result = NULL;
+		return 1;
+	}
+
+	*result = reencode_string(line, mi->metainfo_charset, charset);
+	return !!*result;
+}
+
+/*
+ * Converts line into UTF-8, setting mi->input_error to -1 upon failure.
+ */
 static int convert_to_utf8(struct mailinfo *mi,
 			   struct strbuf *line, const char *charset)
 {
 	char *out;
-
-	if (!mi->metainfo_charset || !charset || !*charset)
-		return 0;
-
-	if (same_encoding(mi->metainfo_charset, charset))
-		return 0;
-	out = reencode_string(line->buf, mi->metainfo_charset, charset);
-	if (!out) {
+	if (!try_convert_to_utf8(mi, line->buf, charset, &out)) {
 		mi->input_error = -1;
 		return error("cannot convert from %s to %s",
 			     charset, mi->metainfo_charset);
 	}
-	strbuf_attach(line, out, strlen(out), strlen(out));
+	if (out)
+		strbuf_attach(line, out, strlen(out), strlen(out));
 	return 0;
 }
 
-- 
2.10.0.rc2.20.g5b18e70
Previous: Jonathan TanNext: Jonathan Tan
Message 4 of 12 in “handle multiline in-body headers”
  1. 0/4 handle multiline in-body headersJonathan Tan, Sep 19, 2016
  2. 1/4 mailinfo: separate in-body header processingJonathan Tan, Sep 19, 2016
  3. 3/4 mailinfo: make is_scissors_line take plain char *Jonathan Tan, Sep 19, 2016
  4. 2/4 mailinfo: refactor to support utf8 decode attemptsJonathan Tan, Sep 19, 2016
  5. 4/4 mailinfo: handle in-body header continuationsJonathan Tan, Sep 19, 2016
  6. Junio C HamanoSep 19, 2016
  7. 0/3 handle multiline in-body headersJonathan Tan, Sep 20, 2016
  8. 1/3 mailinfo: separate in-body header processingJonathan Tan, Sep 20, 2016
  9. 2/3 mailinfo: make is_scissors_line take plain char *Jonathan Tan, Sep 20, 2016
  10. 3/3 mailinfo: handle in-body header continuationsJonathan Tan, Sep 20, 2016
  11. Jeff KingSep 20, 2016
  12. Junio C HamanoSep 21, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.