bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen#include "lib.h"
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen#include "charset-utf8-private.h"
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainenstruct charset_translation {
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen normalizer_func_t *normalizer;
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen};
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenstatic int
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenutf8only_charset_to_utf8_begin(const char *charset,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen normalizer_func_t *normalizer,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen struct charset_translation **t_r)
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen{
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen struct charset_translation *t;
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen if (!charset_is_utf8(charset)) {
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen /* no support for charsets that need translation */
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen return -1;
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen }
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen t = i_new(struct charset_translation, 1);
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen t->normalizer = normalizer;
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen *t_r = t;
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen return 0;
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen}
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenstatic void utf8only_charset_to_utf8_end(struct charset_translation *t)
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen{
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen i_free(t);
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen}
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenstatic void
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenutf8only_charset_to_utf8_reset(struct charset_translation *t ATTR_UNUSED)
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen{
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen}
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenstatic enum charset_result
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenutf8only_charset_to_utf8(struct charset_translation *t,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen const unsigned char *src, size_t *src_size,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen buffer_t *dest)
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen{
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen return charset_utf8_to_utf8(t->normalizer, src, src_size, dest);
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen}
5b9bacdb850fd0491c32116f212d6e165aec5127Timo Sirainen
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainenconst struct charset_utf8_vfuncs charset_utf8only = {
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen .to_utf8_begin = utf8only_charset_to_utf8_begin,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen .to_utf8_end = utf8only_charset_to_utf8_end,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen .to_utf8_reset = utf8only_charset_to_utf8_reset,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen .to_utf8 = utf8only_charset_to_utf8,
9c675d5e57363a903e95c6acda3e3971cc7a221fTimo Sirainen};