Skip to content

Commit

Permalink
lib-charset: Allow plugins to replace charset_* functions
Browse files Browse the repository at this point in the history
They just need to point charset_utf8_vfuncs to their own implementation.
  • Loading branch information
sirainen committed Feb 19, 2018
1 parent 59fcdd2 commit 0b88a23
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/lib-charset/Makefile.am
Expand Up @@ -11,7 +11,8 @@ libcharset_la_SOURCES = \
charset-utf8-only.c

headers = \
charset-utf8.h
charset-utf8.h \
charset-utf8-private.h

pkginc_libdir=$(pkgincludedir)
pkginc_lib_HEADERS = $(headers)
Expand Down
30 changes: 17 additions & 13 deletions src/lib-charset/charset-iconv.c
Expand Up @@ -2,8 +2,7 @@

#include "lib.h"
#include "buffer.h"
#include "unichar.h"
#include "charset-utf8.h"
#include "charset-utf8-private.h"

#ifdef HAVE_ICONV

Expand All @@ -15,8 +14,9 @@ struct charset_translation {
normalizer_func_t *normalizer;
};

int charset_to_utf8_begin(const char *charset, normalizer_func_t *normalizer,
struct charset_translation **t_r)
static int
iconv_charset_to_utf8_begin(const char *charset, normalizer_func_t *normalizer,
struct charset_translation **t_r)
{
struct charset_translation *t;
iconv_t cd;
Expand All @@ -38,18 +38,14 @@ int charset_to_utf8_begin(const char *charset, normalizer_func_t *normalizer,
return 0;
}

void charset_to_utf8_end(struct charset_translation **_t)
static void iconv_charset_to_utf8_end(struct charset_translation *t)
{
struct charset_translation *t = *_t;

*_t = NULL;

if (t->cd != (iconv_t)-1)
iconv_close(t->cd);
i_free(t);
}

void charset_to_utf8_reset(struct charset_translation *t)
static void iconv_charset_to_utf8_reset(struct charset_translation *t)
{
if (t->cd != (iconv_t)-1)
(void)iconv(t->cd, NULL, NULL, NULL, NULL);
Expand Down Expand Up @@ -103,9 +99,10 @@ charset_to_utf8_try(struct charset_translation *t,
return ret;
}

enum charset_result
charset_to_utf8(struct charset_translation *t,
const unsigned char *src, size_t *src_size, buffer_t *dest)
static enum charset_result
iconv_charset_to_utf8(struct charset_translation *t,
const unsigned char *src, size_t *src_size,
buffer_t *dest)
{
enum charset_result result;
size_t pos, size;
Expand Down Expand Up @@ -140,4 +137,11 @@ charset_to_utf8(struct charset_translation *t,
return result;
}

const struct charset_utf8_vfuncs charset_iconv = {
.to_utf8_begin = iconv_charset_to_utf8_begin,
.to_utf8_end = iconv_charset_to_utf8_end,
.to_utf8_reset = iconv_charset_to_utf8_reset,
.to_utf8 = iconv_charset_to_utf8,
};

#endif
30 changes: 19 additions & 11 deletions src/lib-charset/charset-utf8-only.c
@@ -1,16 +1,18 @@
/* Copyright (c) 2002-2017 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "charset-utf8.h"
#include "charset-utf8-private.h"

#ifndef HAVE_ICONV

struct charset_translation {
normalizer_func_t *normalizer;
};

int charset_to_utf8_begin(const char *charset, normalizer_func_t *normalizer,
struct charset_translation **t_r)
static int
utf8only_charset_to_utf8_begin(const char *charset,
normalizer_func_t *normalizer,
struct charset_translation **t_r)
{
struct charset_translation *t;

Expand All @@ -25,23 +27,29 @@ int charset_to_utf8_begin(const char *charset, normalizer_func_t *normalizer,
return 0;
}

void charset_to_utf8_end(struct charset_translation **_t)
static void utf8only_charset_to_utf8_end(struct charset_translation *t)
{
struct charset_translation *t = *_t;

*_t = NULL;
i_free(t);
}

void charset_to_utf8_reset(struct charset_translation *t ATTR_UNUSED)
static void
utf8only_charset_to_utf8_reset(struct charset_translation *t ATTR_UNUSED)
{
}

enum charset_result
charset_to_utf8(struct charset_translation *t,
const unsigned char *src, size_t *src_size, buffer_t *dest)
static enum charset_result
utf8only_charset_to_utf8(struct charset_translation *t,
const unsigned char *src, size_t *src_size,
buffer_t *dest)
{
return charset_utf8_to_utf8(t->normalizer, src, src_size, dest);
}

const struct charset_utf8_vfuncs charset_utf8only = {
.to_utf8_begin = utf8only_charset_to_utf8_begin,
.to_utf8_end = utf8only_charset_to_utf8_end,
.to_utf8_reset = utf8only_charset_to_utf8_reset,
.to_utf8 = utf8only_charset_to_utf8,
};

#endif
21 changes: 21 additions & 0 deletions src/lib-charset/charset-utf8-private.h
@@ -0,0 +1,21 @@
#ifndef CHARSET_UTF8_PRIVATE_H
#define CHARSET_UTF8_PRIVATE_H

#include "unichar.h"
#include "charset-utf8.h"

struct charset_utf8_vfuncs {
int (*to_utf8_begin)(const char *charset, normalizer_func_t *normalizer,
struct charset_translation **t_r);
void (*to_utf8_end)(struct charset_translation *t);
void (*to_utf8_reset)(struct charset_translation *t);

enum charset_result (*to_utf8)(struct charset_translation *t,
const unsigned char *src,
size_t *src_size, buffer_t *dest);
};

extern const struct charset_utf8_vfuncs charset_utf8only;
extern const struct charset_utf8_vfuncs charset_iconv;

#endif
35 changes: 33 additions & 2 deletions src/lib-charset/charset-utf8.c
Expand Up @@ -3,11 +3,16 @@
#include "lib.h"
#include "buffer.h"
#include "str.h"
#include "unichar.h"
#include "charset-utf8.h"
#include "charset-utf8-private.h"

#include <ctype.h>

#ifdef HAVE_ICONV
const struct charset_utf8_vfuncs *charset_utf8_vfuncs = &charset_iconv;
#else
const struct charset_utf8_vfuncs *charset_utf8_vfuncs = &charset_utf8only;
#endif

bool charset_is_utf8(const char *charset)
{
return strcasecmp(charset, "us-ascii") == 0 ||
Expand Down Expand Up @@ -66,3 +71,29 @@ charset_utf8_to_utf8(normalizer_func_t *normalizer,
}
return res;
}

int charset_to_utf8_begin(const char *charset, normalizer_func_t *normalizer,
struct charset_translation **t_r)
{
return charset_utf8_vfuncs->to_utf8_begin(charset, normalizer, t_r);
}

void charset_to_utf8_end(struct charset_translation **_t)
{
struct charset_translation *t = *_t;

*_t = NULL;
charset_utf8_vfuncs->to_utf8_end(t);
}

void charset_to_utf8_reset(struct charset_translation *t)
{
charset_utf8_vfuncs->to_utf8_reset(t);
}

enum charset_result
charset_to_utf8(struct charset_translation *t,
const unsigned char *src, size_t *src_size, buffer_t *dest)
{
return charset_utf8_vfuncs->to_utf8(t, src, src_size, dest);
}

0 comments on commit 0b88a23

Please sign in to comment.