bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen icu_csm = ucasemap_open(NULL, U_FOLD_CASE_DEFAULT, &err);
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainenvoid fts_icu_utf8_to_utf16(ARRAY_TYPE(icu_utf16) *dest_utf16,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen /* try to encode with the current buffer size */
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen avail_uchars = buffer_get_writable_size(dest_buf) / sizeof(UChar);
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen dest_data = buffer_get_space_unsafe(dest_buf, 0,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen retp = u_strFromUTF8Lenient(dest_data, avail_uchars,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen /* try again with a larger buffer */
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen dest_data = buffer_get_space_unsafe(dest_buf, 0,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen retp = u_strFromUTF8Lenient(dest_data, utf16_len,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen i_panic("LibICU u_strFromUTF8Lenient() failed: %s",
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen buffer_set_used_size(dest_buf, utf16_len * sizeof(UChar));
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainenvoid fts_icu_utf16_to_utf8(string_t *dest_utf8, const UChar *src_utf16,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen unsigned int src_len)
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen /* try to encode with the current buffer size */
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen dest_data = buffer_get_space_unsafe(dest_utf8, 0,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen retp = u_strToUTF8WithSub(dest_data, buffer_get_writable_size(dest_utf8),
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen /* try again with a larger buffer */
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen dest_data = buffer_get_space_unsafe(dest_utf8, 0, dest_len);
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen retp = u_strToUTF8WithSub(dest_data, buffer_get_writable_size(dest_utf8), &dest_len,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen i_panic("LibICU u_strToUTF8WithSub() failed: %s",
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainenint fts_icu_translate(ARRAY_TYPE(icu_utf16) *dest_utf16, const UChar *src_utf16,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen unsigned int src_len, UTransliterator *transliterator,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen const char **error_r)
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen /* translation is done in-place in the buffer. try first with the
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen current buffer size. */
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen avail_uchars = (buffer_get_writable_size(dest_buf)-dest_pos) / sizeof(UChar);
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen dest_data = buffer_get_space_unsafe(dest_buf, dest_pos,
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen buffer_get_writable_size(dest_buf) - dest_pos);
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen utrans_transUChars(transliterator, dest_data, &utf16_len,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen /* try again with a larger buffer */
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen dest_data = buffer_get_space_unsafe(dest_buf, dest_pos,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen utrans_transUChars(transliterator, dest_data, &utf16_len,
bf698b98d3a3a1eced66cc682c449f23bf2b67d0Timo Sirainen *error_r = t_strdup_printf("LibICU utrans_transUChars() failed: %s",
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen buffer_set_used_size(dest_buf, utf16_len * sizeof(UChar));
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainenvoid fts_icu_lcase(string_t *dest_utf8, const char *src_utf8)
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen size_t avail_bytes, dest_pos = dest_utf8->used;
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen avail_bytes = buffer_get_writable_size(dest_utf8) - dest_pos;
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen dest_data = buffer_get_space_unsafe(dest_utf8, dest_pos, avail_bytes);
c25abb52b05f4a7e380134ac21e7525d0b46187aTimo Sirainen /* ucasemap_utf8ToLower() may need to be called multiple times, because
c25abb52b05f4a7e380134ac21e7525d0b46187aTimo Sirainen the first return value may not be large enough. */
c25abb52b05f4a7e380134ac21e7525d0b46187aTimo Sirainen for (unsigned int i = 0;; i++) {
c25abb52b05f4a7e380134ac21e7525d0b46187aTimo Sirainen dest_full_len = ucasemap_utf8ToLower(csm, dest_data, avail_bytes,
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen dest_data = buffer_get_space_unsafe(dest_utf8, dest_pos, dest_full_len);
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen i_fatal("LibICU ucasemap_utf8ToLower() failed: %s",
37dfa8907f8216aa73e3880f3f98aba634458e42Timo Sirainen buffer_set_used_size(dest_utf8, dest_full_len);
7c1ce38a29ecdc17480aacb5bac184f42ac05786Teemu Huovilaint fts_icu_transliterator_create(const char *id,
7c1ce38a29ecdc17480aacb5bac184f42ac05786Teemu Huovila const char **error_r)
dcb783533ad1d6944db7c227cde46414a575d81cTimo Sirainen *transliterator_r = utrans_openU(array_idx(&id_utf16, 0),
7c1ce38a29ecdc17480aacb5bac184f42ac05786Teemu Huovila str_printfa(str, "Failed to open transliterator for id '%s': %s",
7c1ce38a29ecdc17480aacb5bac184f42ac05786Teemu Huovila /* we have only one line in our ID */
7c1ce38a29ecdc17480aacb5bac184f42ac05786Teemu Huovila str_printfa(str, " (parse error on offset %u)",