dovecot: charset_to_utf8_begin() API change.
dovecot at dovecot.org
dovecot at dovecot.org
Fri Jul 20 17:39:17 EEST 2007
details: http://hg.dovecot.org/dovecot/rev/d01522d276f6
changeset: 6132:d01522d276f6
user: Timo Sirainen <tss at iki.fi>
date: Fri Jul 20 17:37:05 2007 +0300
description:
charset_to_utf8_begin() API change.
diffstat:
6 files changed, 48 insertions(+), 56 deletions(-)
src/lib-charset/charset-iconv.c | 28 +++++++++------------
src/lib-charset/charset-utf8.c | 44 +++++++++++++++-------------------
src/lib-charset/charset-utf8.h | 15 +++++++----
src/lib-mail/message-decoder.c | 10 +++----
src/lib-mail/message-header-decode.c | 4 ---
src/lib-mail/message-search.c | 3 --
diffs (244 lines):
diff -r 5f56b2eb32b3 -r d01522d276f6 src/lib-charset/charset-iconv.c
--- a/src/lib-charset/charset-iconv.c Fri Jul 20 17:27:02 2007 +0300
+++ b/src/lib-charset/charset-iconv.c Fri Jul 20 17:37:05 2007 +0300
@@ -12,33 +12,28 @@
struct charset_translation {
iconv_t cd;
- unsigned int ucase:1;
+ enum charset_flags flags;
};
-struct charset_translation *
-charset_to_utf8_begin(const char *charset, bool ucase, bool *unknown_charset_r)
+int charset_to_utf8_begin(const char *charset, enum charset_flags flags,
+ struct charset_translation **t_r)
{
struct charset_translation *t;
iconv_t cd;
-
- if (unknown_charset_r != NULL)
- *unknown_charset_r = FALSE;
if (charset_is_utf8(charset))
cd = (iconv_t)-1;
else {
cd = iconv_open("UTF-8", charset);
- if (cd == (iconv_t)-1) {
- if (unknown_charset_r != NULL)
- *unknown_charset_r = TRUE;
- return NULL;
- }
+ if (cd == (iconv_t)-1)
+ return -1;
}
t = i_new(struct charset_translation, 1);
t->cd = cd;
- t->ucase = ucase;
- return t;
+ t->flags = flags;
+ *t_r = t;
+ return 0;
}
void charset_to_utf8_end(struct charset_translation **_t)
@@ -66,12 +61,13 @@ charset_to_utf8_try(struct charset_trans
ICONV_CONST char *ic_srcbuf;
char tmpbuf[8192], *ic_destbuf;
size_t srcleft, destleft;
+ bool dtcase = (t->flags & CHARSET_FLAG_DECOMP_TITLECASE) != 0;
bool ret = TRUE;
if (t->cd == (iconv_t)-1) {
/* no translation needed - just copy it to outbuf uppercased */
*result = CHARSET_RET_OK;
- if (!t->ucase) {
+ if (!dtcase) {
buffer_append(dest, src, *src_size);
return TRUE;
}
@@ -80,7 +76,7 @@ charset_to_utf8_try(struct charset_trans
*result = CHARSET_RET_INVALID_INPUT;
return TRUE;
}
- if (!t->ucase) {
+ if (!dtcase) {
destleft = buffer_get_size(dest) - dest->used;
if (destleft < *src_size) {
/* The buffer is most likely too small to hold the
@@ -112,7 +108,7 @@ charset_to_utf8_try(struct charset_trans
}
*src_size -= srcleft;
- if (!t->ucase) {
+ if (!dtcase) {
/* give back the memory we didn't use */
buffer_set_used_size(dest, dest->used - destleft);
} else {
diff -r 5f56b2eb32b3 -r d01522d276f6 src/lib-charset/charset-utf8.c
--- a/src/lib-charset/charset-utf8.c Fri Jul 20 17:27:02 2007 +0300
+++ b/src/lib-charset/charset-utf8.c Fri Jul 20 17:37:05 2007 +0300
@@ -2,6 +2,7 @@
#include "lib.h"
#include "buffer.h"
+#include "unichar.h"
#include "charset-utf8.h"
#include <ctype.h>
@@ -16,33 +17,28 @@ bool charset_is_utf8(const char *charset
#ifndef HAVE_ICONV
-#include <ctype.h>
-
struct charset_translation {
- int dummy;
+ enum charset_flags flags;
};
-static struct charset_translation ascii_translation, utf8_translation;
-static struct charset_translation ascii_translation_uc, utf8_translation_uc;
+static struct charset_translation raw_translation = { 0 };
+static struct charset_translation tc_translation = {
+ CHARSET_FLAG_DECOMP_TITLECASE
+};
-struct charset_translation *
-charset_to_utf8_begin(const char *charset, bool ucase, bool *unknown_charset_r)
+int charset_to_utf8_begin(const char *charset, enum charset_flags flags,
+ struct charset_translation **t_r)
{
- if (unknown_charset_r != NULL)
- *unknown_charset_r = FALSE;
-
- if (strcasecmp(charset, "us-ascii") == 0 ||
- strcasecmp(charset, "ascii") == 0)
- return ucase ? &ascii_translation_uc : &ascii_translation;
-
- if (strcasecmp(charset, "UTF-8") == 0 ||
- strcasecmp(charset, "UTF8") == 0)
- return ucase ? &utf8_translation_uc : &utf8_translation;
+ if (charset_is_utf8(charset)) {
+ if ((flags & CHARSET_FLAG_DECOMP_TITLECASE) != 0)
+ *t_r = &tc_translation;
+ else
+ *t_r = &raw_translation;
+ return 0;
+ }
/* no support for charsets that need translation */
- if (unknown_charset_r != NULL)
- *unknown_charset_r = TRUE;
- return NULL;
+ return -1;
}
void charset_to_utf8_end(struct charset_translation **t __attr_unused__)
@@ -57,12 +53,12 @@ charset_to_utf8(struct charset_translati
charset_to_utf8(struct charset_translation *t,
const unsigned char *src, size_t *src_size, buffer_t *dest)
{
- if (t != &utf8_translation_uc && t != &ascii_translation_uc) {
+ if ((t->flags & CHARSET_FLAG_DECOMP_TITLECASE) == 0)
buffer_append(dest, src, *src_size);
- return CHARSET_RET_OK;
+ else {
+ if (uni_utf8_to_decomposed_titlecase(src, *src_size, dest) < 0)
+ return CHARSET_RET_INVALID_INPUT;
}
- if (uni_utf8_to_decomposed_titlecase(src, *src_size, dest) < 0)
- return CHARSET_RET_INVALID_INPUT;
return CHARSET_RET_OK;
}
diff -r 5f56b2eb32b3 -r d01522d276f6 src/lib-charset/charset-utf8.h
--- a/src/lib-charset/charset-utf8.h Fri Jul 20 17:27:02 2007 +0300
+++ b/src/lib-charset/charset-utf8.h Fri Jul 20 17:37:05 2007 +0300
@@ -1,5 +1,12 @@
#ifndef __CHARSET_UTF8_H
#define __CHARSET_UTF8_H
+
+struct charset_translation;
+
+enum charset_flags {
+ /* Translate the output to decomposed titlecase */
+ CHARSET_FLAG_DECOMP_TITLECASE = 0x01
+};
enum charset_result {
CHARSET_RET_OK = 1,
@@ -7,12 +14,10 @@ enum charset_result {
CHARSET_RET_INVALID_INPUT = -2
};
-/* Begin translation to UTF-8. If ucase=TRUE, returns data uppercased. */
-struct charset_translation *
-charset_to_utf8_begin(const char *charset, bool ucase, bool *unknown_charset_r);
-
+/* Begin translation to UTF-8. Returns -1 if charset is unknown. */
+int charset_to_utf8_begin(const char *charset, enum charset_flags flags,
+ struct charset_translation **t_r);
void charset_to_utf8_end(struct charset_translation **t);
-
void charset_to_utf8_reset(struct charset_translation *t);
/* Returns TRUE if charset is UTF-8 or ASCII */
diff -r 5f56b2eb32b3 -r d01522d276f6 src/lib-mail/message-decoder.c
--- a/src/lib-mail/message-decoder.c Fri Jul 20 17:27:02 2007 +0300
+++ b/src/lib-mail/message-decoder.c Fri Jul 20 17:37:05 2007 +0300
@@ -212,15 +212,13 @@ static bool message_decode_body(struct m
unsigned char new_buf[MAX_ENCODING_BUF_SIZE+1];
const unsigned char *data = NULL;
size_t pos, size = 0, skip = 0;
- bool unknown_charset;
int ret;
if (ctx->charset_trans == NULL && !ctx->charset_utf8) {
- ctx->charset_trans =
- charset_to_utf8_begin(ctx->content_charset != NULL ?
- ctx->content_charset : "UTF-8",
- ctx->ucase,
- &unknown_charset);
+ if (charset_to_utf8_begin(ctx->content_charset != NULL ?
+ ctx->content_charset : "UTF-8",
+ ctx->ucase, &ctx->charset_trans) < 0)
+ ctx->charset_trans = NULL;
}
if (ctx->encoding_size != 0) {
diff -r 5f56b2eb32b3 -r d01522d276f6 src/lib-mail/message-header-decode.c
--- a/src/lib-mail/message-header-decode.c Fri Jul 20 17:27:02 2007 +0300
+++ b/src/lib-mail/message-header-decode.c Fri Jul 20 17:37:05 2007 +0300
@@ -129,7 +129,6 @@ decode_utf8_callback(const unsigned char
{
struct decode_utf8_context *ctx = context;
struct charset_translation *t;
- bool unknown_charset;
/* one call with charset=NULL means nothing changed */
if (!ctx->called && charset == NULL)
@@ -148,8 +147,7 @@ decode_utf8_callback(const unsigned char
return TRUE;
}
- t = charset_to_utf8_begin(charset, ctx->ucase, &unknown_charset);
- if (unknown_charset) {
+ if (charset_to_utf8_begin(charset, ctx->ucase, &t) < 0) {
/* let's just ignore this part */
return TRUE;
}
diff -r 5f56b2eb32b3 -r d01522d276f6 src/lib-mail/message-search.c
--- a/src/lib-mail/message-search.c Fri Jul 20 17:27:02 2007 +0300
+++ b/src/lib-mail/message-search.c Fri Jul 20 17:37:05 2007 +0300
@@ -35,8 +35,7 @@ int message_search_init(pool_t pool, con
string_t *key_utf8;
size_t key_len;
- t = charset_to_utf8_begin(charset, TRUE, NULL);
- if (t == NULL)
+ if (charset_to_utf8_begin(charset, TRUE, &t) < 0)
return 0;
t_push();
More information about the dovecot-cvs
mailing list