summaryrefslogtreecommitdiffstats
path: root/src/lib-fts/fts-icu.c
blob: ca652c36fb9b2bcc79fe1997baedd48d9770bfa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "mempool.h"
#include "array.h"
#include "str.h"
#include "unichar.h"
#include "fts-icu.h"

#include <unicode/uchar.h>
#include <unicode/ucasemap.h>
#include <unicode/uclean.h>

static struct UCaseMap *icu_csm = NULL;

static struct UCaseMap *fts_icu_csm(void)
{
	UErrorCode err = U_ZERO_ERROR;

	if (icu_csm != NULL)
		return icu_csm;
	icu_csm = ucasemap_open(NULL, U_FOLD_CASE_DEFAULT, &err);
	if (U_FAILURE(err)) {
		i_fatal("LibICU ucasemap_open() failed: %s",
			u_errorName(err));
	}
	return icu_csm;
}

void fts_icu_utf8_to_utf16(ARRAY_TYPE(icu_utf16) *dest_utf16,
			   const char *src_utf8)
{
	buffer_t *dest_buf = dest_utf16->arr.buffer;
	UErrorCode err = U_ZERO_ERROR;
	size_t src_bytes = strlen(src_utf8);
	int32_t utf16_len;
	UChar *dest_data, *retp = NULL;
	int32_t avail_uchars = 0;

	/* try to encode with the current buffer size */
	avail_uchars = buffer_get_writable_size(dest_buf) / sizeof(UChar);
	dest_data = buffer_get_space_unsafe(dest_buf, 0,
				buffer_get_writable_size(dest_buf));
	retp = u_strFromUTF8Lenient(dest_data, avail_uchars,
				    &utf16_len, src_utf8, src_bytes, &err);
	if (err == U_BUFFER_OVERFLOW_ERROR) {
		/* try again with a larger buffer */
		dest_data = buffer_get_space_unsafe(dest_buf, 0,
						    utf16_len * sizeof(UChar));
		err = U_ZERO_ERROR;
		retp = u_strFromUTF8Lenient(dest_data, utf16_len,
					    &utf16_len, src_utf8,
					    src_bytes, &err);
	}
	if (U_FAILURE(err)) {
		i_panic("LibICU u_strFromUTF8Lenient() failed: %s",
			u_errorName(err));
	}
	buffer_set_used_size(dest_buf, utf16_len * sizeof(UChar));
	i_assert(retp == dest_data);
}

void fts_icu_utf16_to_utf8(string_t *dest_utf8, const UChar *src_utf16,
			   unsigned int src_len)
{
	int32_t dest_len = 0;
	int32_t sub_num = 0;
	char *dest_data, *retp = NULL;
	UErrorCode err = U_ZERO_ERROR;

	/* try to encode with the current buffer size */
	dest_data = buffer_get_space_unsafe(dest_utf8, 0,
					    buffer_get_writable_size(dest_utf8));
	retp = u_strToUTF8WithSub(dest_data, buffer_get_writable_size(dest_utf8),
				  &dest_len, src_utf16, src_len,
				  UNICODE_REPLACEMENT_CHAR, &sub_num, &err);
	if (err == U_BUFFER_OVERFLOW_ERROR) {
		/* try again with a larger buffer */
		dest_data = buffer_get_space_unsafe(dest_utf8, 0, dest_len);
		err = U_ZERO_ERROR;
		retp = u_strToUTF8WithSub(dest_data, buffer_get_writable_size(dest_utf8), &dest_len,
					  src_utf16, src_len,
					  UNICODE_REPLACEMENT_CHAR,
					  &sub_num, &err);
	}
	if (U_FAILURE(err)) {
		i_panic("LibICU u_strToUTF8WithSub() failed: %s",
			u_errorName(err));
	}
	buffer_set_used_size(dest_utf8, dest_len);
	i_assert(retp == dest_data);
}

int fts_icu_translate(ARRAY_TYPE(icu_utf16) *dest_utf16, const UChar *src_utf16,
		      unsigned int src_len, UTransliterator *transliterator,
		      const char **error_r)
{
	buffer_t *dest_buf = dest_utf16->arr.buffer;
	UErrorCode err = U_ZERO_ERROR;
	int32_t utf16_len = src_len;
	UChar *dest_data;
	int32_t avail_uchars, limit = src_len;
	size_t dest_pos = dest_buf->used;

	/* translation is done in-place in the buffer. try first with the
	   current buffer size. */
	array_append(dest_utf16, src_utf16, src_len);

	avail_uchars = (buffer_get_writable_size(dest_buf)-dest_pos) / sizeof(UChar);
	dest_data = buffer_get_space_unsafe(dest_buf, dest_pos,
			buffer_get_writable_size(dest_buf) - dest_pos);
	utrans_transUChars(transliterator, dest_data, &utf16_len,
			   avail_uchars, 0, &limit, &err);
	if (err == U_BUFFER_OVERFLOW_ERROR) {
		/* try again with a larger buffer */
		err = U_ZERO_ERROR;
		avail_uchars = utf16_len;
		limit = utf16_len = src_len;
		buffer_write(dest_buf, dest_pos,
			     src_utf16, src_len*sizeof(UChar));
		dest_data = buffer_get_space_unsafe(dest_buf, dest_pos,
						    avail_uchars * sizeof(UChar));
		utrans_transUChars(transliterator, dest_data, &utf16_len,
				   avail_uchars, 0, &limit, &err);
		i_assert(err != U_BUFFER_OVERFLOW_ERROR);
	}
	if (U_FAILURE(err)) {
		*error_r = t_strdup_printf("LibICU utrans_transUChars() failed: %s",
					   u_errorName(err));
		buffer_set_used_size(dest_buf, dest_pos);
		return -1;
	}
	buffer_set_used_size(dest_buf, utf16_len * sizeof(UChar));
	return 0;
}

void fts_icu_lcase(string_t *dest_utf8, const char *src_utf8)
{
	struct UCaseMap *csm = fts_icu_csm();
	size_t avail_bytes, dest_pos = dest_utf8->used;
	char *dest_data;
	int dest_full_len;
	UErrorCode err = U_ZERO_ERROR;

	avail_bytes = buffer_get_writable_size(dest_utf8) - dest_pos;
	dest_data = buffer_get_space_unsafe(dest_utf8, dest_pos, avail_bytes);

	/* ucasemap_utf8ToLower() may need to be called multiple times, because
	   the first return value may not be large enough. */
	for (unsigned int i = 0;; i++) {
		dest_full_len = ucasemap_utf8ToLower(csm, dest_data, avail_bytes,
						     src_utf8, -1, &err);
		if (err != U_BUFFER_OVERFLOW_ERROR || i == 2)
			break;

		err = U_ZERO_ERROR;
		dest_data = buffer_get_space_unsafe(dest_utf8, dest_pos, dest_full_len);
		avail_bytes = dest_full_len;
	}
	if (U_FAILURE(err)) {
		i_fatal("LibICU ucasemap_utf8ToLower() failed: %s",
			u_errorName(err));
	}
	buffer_set_used_size(dest_utf8, dest_full_len);
}

void fts_icu_deinit(void)
{
	if (icu_csm != NULL) {
		ucasemap_close(icu_csm);
		icu_csm = NULL;
	}
	u_cleanup();
}

int fts_icu_transliterator_create(const char *id,
                                  UTransliterator **transliterator_r,
                                  const char **error_r)
{
	UErrorCode err = U_ZERO_ERROR;
	UParseError perr;
	ARRAY_TYPE(icu_utf16) id_utf16;
	i_zero(&perr);

	t_array_init(&id_utf16, strlen(id));
	fts_icu_utf8_to_utf16(&id_utf16, id);
	*transliterator_r = utrans_openU(array_front(&id_utf16),
					 array_count(&id_utf16),
					 UTRANS_FORWARD, NULL, 0, &perr, &err);
	if (U_FAILURE(err)) {
		string_t *str = t_str_new(128);

		str_printfa(str, "Failed to open transliterator for id '%s': %s",
			    id, u_errorName(err));
		if (perr.line >= 1) {
			/* we have only one line in our ID */
			str_printfa(str, " (parse error on offset %u)",
				    perr.offset);
		}
		*error_r = str_c(str);
		return -1;
	}
	return 0;
}