dovecot: Added message_header_decode_utf8(). Use it in message d...

dovecot at dovecot.org dovecot at dovecot.org
Fri Jul 20 12:02:49 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/841b052e269e
changeset: 6118:841b052e269e
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Jul 20 12:02:45 2007 +0300
description:
Added message_header_decode_utf8(). Use it in message decoder instead of
implementing the same thing.

diffstat:

3 files changed, 54 insertions(+), 33 deletions(-)
src/lib-mail/message-decoder.c       |   35 +------------------------
src/lib-mail/message-header-decode.c |   47 ++++++++++++++++++++++++++++++++++
src/lib-mail/message-header-decode.h |    5 +++

diffs (126 lines):

diff -r 9214044ce1f1 -r 841b052e269e src/lib-mail/message-decoder.c
--- a/src/lib-mail/message-decoder.c	Fri Jul 20 09:42:25 2007 +0300
+++ b/src/lib-mail/message-decoder.c	Fri Jul 20 12:02:45 2007 +0300
@@ -71,37 +71,6 @@ void message_decoder_deinit(struct messa
 	i_free(ctx);
 }
 
-static bool
-message_decode_header_callback(const unsigned char *data, size_t size,
-			       const char *charset, void *context)
-{
-	struct message_decoder_context *ctx = context;
-	struct charset_translation *t;
-	bool unknown_charset;
-
-	if (charset == NULL || charset_is_utf8(charset)) {
-		/* ASCII / UTF-8 */
-		if (ctx->ucase) {
-			charset_utf8_ucase_write(ctx->buf, ctx->buf->used,
-						 data, size);
-		} else {
-			buffer_append(ctx->buf, data, size);
-		}
-		return TRUE;
-	}
-
-	t = charset_to_utf8_begin(charset, ctx->ucase, &unknown_charset);
-	if (unknown_charset) {
-		/* let's just ignore this part */
-		return TRUE;
-	}
-
-	/* ignore any errors */
-	(void)charset_to_utf8_full(t, data, &size, ctx->buf);
-	charset_to_utf8_end(&t);
-	return TRUE;
-}
-
 static void
 parse_content_transfer_encoding(struct message_decoder_context *ctx,
 				struct message_header_line *hdr)
@@ -185,8 +154,8 @@ static bool message_decode_header(struct
 		parse_content_transfer_encoding(ctx, hdr);
 
 	buffer_set_used_size(ctx->buf, 0);
-	message_header_decode(hdr->full_value, hdr->full_value_len,
-			      message_decode_header_callback, ctx);
+	message_header_decode_utf8(hdr->full_value, hdr->full_value_len,
+				   ctx->buf, ctx->ucase);
 	value_len = ctx->buf->used;
 
 	if (ctx->ucase) {
diff -r 9214044ce1f1 -r 841b052e269e src/lib-mail/message-header-decode.c
--- a/src/lib-mail/message-header-decode.c	Fri Jul 20 09:42:25 2007 +0300
+++ b/src/lib-mail/message-header-decode.c	Fri Jul 20 12:02:45 2007 +0300
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "base64.h"
 #include "buffer.h"
+#include "charset-utf8.h"
 #include "quoted-printable.h"
 #include "message-header-decode.h"
 
@@ -113,3 +114,49 @@ void message_header_decode(const unsigne
 	}
 	t_pop();
 }
+
+struct decode_utf8_context {
+	buffer_t *dest;
+	bool ucase;
+};
+
+static bool
+decode_utf8_callback(const unsigned char *data, size_t size,
+		     const char *charset, void *context)
+{
+	struct decode_utf8_context *ctx = context;
+	struct charset_translation *t;
+	bool unknown_charset;
+
+	if (charset == NULL || charset_is_utf8(charset)) {
+		/* ASCII / UTF-8 */
+		if (ctx->ucase) {
+			charset_utf8_ucase_write(ctx->dest, ctx->dest->used,
+						 data, size);
+		} else {
+			buffer_append(ctx->dest, data, size);
+		}
+		return TRUE;
+	}
+
+	t = charset_to_utf8_begin(charset, ctx->ucase, &unknown_charset);
+	if (unknown_charset) {
+		/* let's just ignore this part */
+		return TRUE;
+	}
+
+	/* ignore any errors */
+	(void)charset_to_utf8_full(t, data, &size, ctx->dest);
+	charset_to_utf8_end(&t);
+	return TRUE;
+}
+
+void message_header_decode_utf8(const unsigned char *data, size_t size,
+				buffer_t *dest, bool ucase)
+{
+	struct decode_utf8_context ctx;
+
+	ctx.dest = dest;
+	ctx.ucase = ucase;
+	message_header_decode(data, size, decode_utf8_callback, &ctx);
+}
diff -r 9214044ce1f1 -r 841b052e269e src/lib-mail/message-header-decode.h
--- a/src/lib-mail/message-header-decode.h	Fri Jul 20 09:42:25 2007 +0300
+++ b/src/lib-mail/message-header-decode.h	Fri Jul 20 12:02:45 2007 +0300
@@ -13,4 +13,9 @@ void message_header_decode(const unsigne
 			   message_header_decode_callback_t *callback,
 			   void *context);
 
+/* Append decoded RFC2047 header as UTF-8 to given buffer. If ucase=TRUE,
+   the header is appended uppercased. */
+void message_header_decode_utf8(const unsigned char *data, size_t size,
+				buffer_t *dest, bool ucase);
+
 #endif


More information about the dovecot-cvs mailing list