2 Unix SMB/CIFS implementation.
3 Character set conversion Extensions
4 Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
5 Copyright (C) Andrew Tridgell 2001
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Martin Pool 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #endif /* HAVE_CONFIG_H */
35 #include <sys/param.h>
36 #ifdef HAVE_USABLE_ICONV
39 #include <arpa/inet.h>
41 #include <atalk/logger.h>
42 #include <atalk/unicode.h>
43 #include <atalk/util.h>
44 #include <atalk/compat.h>
46 #include "byteorder.h"
52 * @brief Character-set conversion routines built on our iconv.
54 * @note Samba's internal character set (at least in the 3.0 series)
55 * is always the same as the one for the Unix filesystem. It is
56 * <b>not</b> necessarily UTF-8 and may be different on machines that
57 * need i18n filenames to be compatible with Unix software. It does
58 * have to be a superset of ASCII. All multibyte sequences must start
59 * with a byte with the high bit set.
65 #define MAX_CHARSETS 20
67 #define CHECK_FLAGS(a,b) (((a)!=NULL) ? (*(a) & (b)) : 0 )
69 static atalk_iconv_t conv_handles[MAX_CHARSETS][MAX_CHARSETS];
70 static char* charset_names[MAX_CHARSETS];
71 static struct charset_functions* charsets[MAX_CHARSETS];
72 static char hexdig[] = "0123456789abcdef";
73 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
77 * Return the name of a charset to give to iconv().
79 static const char *charset_name(charset_t ch)
81 const char *ret = NULL;
83 if (ch == CH_UCS2) ret = "UCS-2";
84 else if (ch == CH_UTF8) ret = "UTF8";
85 else if (ch == CH_UTF8_MAC) ret = "UTF8-MAC";
86 else ret = charset_names[ch];
90 int set_charset_name(charset_t ch, const char *name)
92 if (ch >= NUM_CHARSETS)
94 charset_names[ch] = strdup(name);
98 static struct charset_functions* get_charset_functions (charset_t ch)
100 if (charsets[ch] != NULL)
103 charsets[ch] = find_charset_functions(charset_name(ch));
109 static void lazy_initialize_conv(void)
111 static int initialized = 0;
119 charset_t add_charset(const char* name)
121 static charset_t max_charset_t = NUM_CHARSETS-1;
122 charset_t cur_charset_t = max_charset_t+1;
125 lazy_initialize_conv();
127 for (c1=0; c1<=max_charset_t;c1++) {
128 if ( strcasecmp(name, charset_name(c1)) == 0)
132 if ( cur_charset_t >= MAX_CHARSETS ) {
133 LOG (log_debug, logtype_default, "Adding charset %s failed, too many charsets (max. %u allowed)",
135 return (charset_t) -1;
138 /* First try to setup the required conversions */
140 conv_handles[cur_charset_t][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
141 if (conv_handles[cur_charset_t][CH_UCS2] == (atalk_iconv_t)-1) {
142 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
143 name, charset_name(CH_UCS2));
144 conv_handles[cur_charset_t][CH_UCS2] = NULL;
145 return (charset_t) -1;
148 conv_handles[CH_UCS2][cur_charset_t] = atalk_iconv_open( name, charset_name(CH_UCS2));
149 if (conv_handles[CH_UCS2][cur_charset_t] == (atalk_iconv_t)-1) {
150 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
151 charset_name(CH_UCS2), name);
152 conv_handles[CH_UCS2][cur_charset_t] = NULL;
153 return (charset_t) -1;
156 /* register the new charset_t name */
157 charset_names[cur_charset_t] = strdup(name);
159 charsets[cur_charset_t] = get_charset_functions (cur_charset_t);
163 LOG(log_debug9, logtype_default, "Added charset %s with handle %u", name, cur_charset_t);
165 return (cur_charset_t);
169 * Initialize iconv conversion descriptors.
171 * This is called the first time it is needed, and also called again
172 * every time the configuration is reloaded, because the charset or
173 * codepage might have changed.
175 void init_iconv(void)
179 for (c1=0;c1<NUM_CHARSETS;c1++) {
180 const char *name = charset_name((charset_t)c1);
182 conv_handles[c1][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
183 if (conv_handles[c1][CH_UCS2] == (atalk_iconv_t)-1) {
184 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
185 name, charset_name(CH_UCS2));
186 conv_handles[c1][CH_UCS2] = NULL;
189 if (c1 != CH_UCS2) { /* avoid lost memory, make valgrind happy */
190 conv_handles[CH_UCS2][c1] = atalk_iconv_open( name, charset_name(CH_UCS2));
191 if (conv_handles[CH_UCS2][c1] == (atalk_iconv_t)-1) {
192 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
193 charset_name(CH_UCS2), name);
194 conv_handles[CH_UCS2][c1] = NULL;
198 charsets[c1] = get_charset_functions (c1);
205 static size_t add_null(charset_t to, char *buf, size_t bytesleft, size_t len)
207 /* Terminate the string */
208 if (to == CH_UCS2 && bytesleft >= 2) {
213 else if ( to != CH_UCS2 && bytesleft > 0 )
225 * Convert string from one encoding to another, making error checking etc
227 * @param src pointer to source string (multibyte or singlebyte)
228 * @param srclen length of the source string in bytes
229 * @param dest pointer to destination string (multibyte or singlebyte)
230 * @param destlen maximal length allowed for string
231 * @returns the number of bytes occupied in the destination
233 static size_t convert_string_internal(charset_t from, charset_t to,
234 void const *src, size_t srclen,
235 void *dest, size_t destlen)
239 const char* inbuf = (const char*)src;
240 char* outbuf = (char*)dest;
241 char* o_save = outbuf;
242 atalk_iconv_t descriptor;
244 /* Fixed based on Samba 3.0.6 */
245 if (srclen == (size_t)-1) {
246 if (from == CH_UCS2) {
247 srclen = (strlen_w((const ucs2_t *)src)) * 2;
249 srclen = strlen((const char *)src);
254 lazy_initialize_conv();
256 descriptor = conv_handles[from][to];
258 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
264 retval = atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
265 if(retval==(size_t)-1) {
266 const char *reason="unknown error";
269 reason="Incomplete multibyte sequence";
272 reason="No more room";
275 reason="Illegal multibyte sequence";
278 LOG(log_debug, logtype_default,"Conversion error: %s",reason);
282 /* Terminate the string */
283 return add_null( to, o_save, o_len, destlen -o_len);
287 size_t convert_string(charset_t from, charset_t to,
288 void const *src, size_t srclen,
289 void *dest, size_t destlen)
293 ucs2_t buffer[MAXPATHLEN];
294 ucs2_t buffer2[MAXPATHLEN];
296 /* convert from_set to UCS2 */
297 if ((size_t)-1 == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
298 (char*) buffer, sizeof(buffer))) ) {
299 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
303 /* Do pre/decomposition */
304 i_len = sizeof(buffer2);
306 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
307 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
310 else if (!charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED)) {
311 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
318 /* Convert UCS2 to to_set */
319 if ((size_t)(-1) == ( o_len = convert_string_internal( CH_UCS2, to, (char*) u, i_len, dest, destlen)) ) {
320 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
330 * Convert between character sets, allocating a new buffer for the result.
332 * @param srclen length of source buffer.
333 * @param dest always set at least to NULL
334 * @note -1 is not accepted for srclen.
336 * @returns Size in bytes of the converted string; or -1 in case of error.
339 static size_t convert_string_allocate_internal(charset_t from, charset_t to,
340 void const *src, size_t srclen, char **dest)
342 size_t i_len, o_len, destlen;
344 const char *inbuf = (const char *)src;
345 char *outbuf = NULL, *ob = NULL;
346 atalk_iconv_t descriptor;
350 if (src == NULL || srclen == (size_t)-1)
353 lazy_initialize_conv();
355 descriptor = conv_handles[from][to];
357 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
358 /* conversion not supported, return -1*/
359 LOG(log_debug, logtype_default, "convert_string_allocate: conversion not supported!");
363 destlen = MAX(srclen, 512);
365 destlen = destlen * 2;
366 outbuf = (char *)realloc(ob, destlen);
368 LOG(log_debug, logtype_default,"convert_string_allocate: realloc failed!");
374 inbuf = src; /* this restarts the whole conversion if buffer needed to be increased */
377 retval = atalk_iconv(descriptor,
380 if(retval == (size_t)-1) {
381 const char *reason="unknown error";
384 reason="Incomplete multibyte sequence";
389 reason="Illegal multibyte sequence";
392 LOG(log_debug, logtype_default,"Conversion error: %s(%s)",reason,inbuf);
398 destlen = destlen - o_len;
400 /* Terminate the string */
401 if (to == CH_UCS2 && o_len >= 2) {
404 *dest = (char *)realloc(ob,destlen+2);
406 else if ( to != CH_UCS2 && o_len > 0 ) {
408 *dest = (char *)realloc(ob,destlen+1);
411 goto convert; /* realloc */
414 if (destlen && !*dest) {
415 LOG(log_debug, logtype_default, "convert_string_allocate: out of memory!");
424 size_t convert_string_allocate(charset_t from, charset_t to,
425 void const *src, size_t srclen,
430 ucs2_t buffer[MAXPATHLEN];
431 ucs2_t buffer2[MAXPATHLEN];
435 /* convert from_set to UCS2 */
436 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
437 buffer, sizeof(buffer))) ) {
438 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
442 /* Do pre/decomposition */
443 i_len = sizeof(buffer2);
445 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
446 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
449 else if ( !charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED) ) {
450 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
458 /* Convert UCS2 to to_set */
459 if ((size_t)-1 == ( o_len = convert_string_allocate_internal( CH_UCS2, to, (char*)u, i_len, dest)) )
460 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
466 size_t charset_strupper(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
471 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
473 if (size == (size_t)-1) {
477 if (!strupper_w((ucs2_t *)buffer) && (dest == src)) {
482 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
487 size_t charset_strlower(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
492 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
494 if (size == (size_t)-1) {
498 if (!strlower_w((ucs2_t *)buffer) && (dest == src)) {
503 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
509 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
511 return charset_strupper( CH_UNIX, src, srclen, dest, destlen);
514 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
516 return charset_strlower( CH_UNIX, src, srclen, dest, destlen);
519 size_t utf8_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
521 return charset_strupper( CH_UTF8, src, srclen, dest, destlen);
524 size_t utf8_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
526 return charset_strlower( CH_UTF8, src, srclen, dest, destlen);
530 * Copy a string from a charset_t char* src to a UCS2 destination, allocating a buffer
532 * @param dest always set at least to NULL
534 * @returns The number of bytes occupied by the string in the destination
535 * or -1 in case of error.
538 size_t charset_to_ucs2_allocate(charset_t ch, ucs2_t **dest, const char *src)
540 size_t src_len = strlen(src);
543 return convert_string_allocate(ch, CH_UCS2, src, src_len, (char**) dest);
546 /** -----------------------------------
547 * Copy a string from a charset_t char* src to a UTF-8 destination, allocating a buffer
549 * @param dest always set at least to NULL
551 * @returns The number of bytes occupied by the string in the destination
554 size_t charset_to_utf8_allocate(charset_t ch, char **dest, const char *src)
556 size_t src_len = strlen(src);
559 return convert_string_allocate(ch, CH_UTF8, src, src_len, dest);
562 /** -----------------------------------
563 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
565 * @param dest always set at least to NULL
567 * @returns The number of bytes occupied by the string in the destination
570 size_t ucs2_to_charset(charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
572 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
573 return convert_string(CH_UCS2, ch, src, src_len, dest, destlen);
576 /* --------------------------------- */
577 size_t ucs2_to_charset_allocate(charset_t ch, char **dest, const ucs2_t *src)
579 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
581 return convert_string_allocate(CH_UCS2, ch, src, src_len, dest);
584 /** ---------------------------------
585 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
587 * @param dest always set at least to NULL
589 * @returns The number of bytes occupied by the string in the destination
592 size_t utf8_to_charset_allocate(charset_t ch, char **dest, const char *src)
594 size_t src_len = strlen(src);
596 return convert_string_allocate(CH_UTF8, ch, src, src_len, dest);
599 size_t charset_precompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
602 ucs2_t u[MAXPATHLEN];
606 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
611 if ( (size_t)-1 == (ilen = precompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
616 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
625 size_t charset_decompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
628 ucs2_t u[MAXPATHLEN];
632 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
637 if ( (size_t)-1 == (ilen = decompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
642 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
651 size_t utf8_precompose ( char * src, size_t inlen, char * dst, size_t outlen)
653 return charset_precompose ( CH_UTF8, src, inlen, dst, outlen);
656 size_t utf8_decompose ( char * src, size_t inlen, char * dst, size_t outlen)
658 return charset_decompose ( CH_UTF8, src, inlen, dst, outlen);
662 static char debugbuf[ MAXPATHLEN +1 ];
663 char * debug_out ( char * seq, size_t len)
669 p = (unsigned char*) seq;
672 for ( i = 0; i<=(len-1); i++)
674 sprintf(q, "%2.2x.", *p);
685 * Convert from MB to UCS2 charset
687 * CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
688 * CONV_IGNORE: return the first convertable characters.
689 * CONV_FORCE: force convertion
691 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
692 * The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it
693 * for e.g. HFS cdroms.
696 static size_t pull_charset_flags (charset_t from_set, charset_t cap_set, const char *src, size_t srclen, char* dest, size_t destlen, uint16_t *flags)
698 const uint16_t option = (flags ? *flags : 0);
701 const char* inbuf = (const char*)src;
703 atalk_iconv_t descriptor;
704 atalk_iconv_t descriptor_cap;
706 if (srclen == (size_t)-1)
707 srclen = strlen(src) + 1;
709 descriptor = conv_handles[from_set][CH_UCS2];
710 descriptor_cap = conv_handles[cap_set][CH_UCS2];
712 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
721 for (j = 0; j < i_len; ++j)
728 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
729 if (errno == EILSEQ || errno == EINVAL) {
731 if ((option & CONV_IGNORE)) {
732 *flags |= CONV_REQMANGLE;
733 return destlen - o_len;
735 if ((option & CONV__EILSEQ)) {
740 *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
745 /* FIXME reset stat ? */
756 if ((option & CONV_UNESCAPEHEX)) {
757 /* treat it as a CAP hex encoded char */
761 while (i_len >= 3 && inbuf[0] == ':' &&
762 isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
763 h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
768 const char *h_buf = h;
769 if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
772 if (errno == EILSEQ && (option & CONV_IGNORE)) {
773 *flags |= CONV_REQMANGLE;
774 return destlen - o_len;
779 /* We have an invalid :xx sequence */
781 if ((option & CONV_IGNORE)) {
782 *flags |= CONV_REQMANGLE;
783 return destlen - o_len;
788 /* a ':' that we just convert to a '/' */
789 ucs2_t slash = 0x002f;
790 memcpy(outbuf, &slash, sizeof(ucs2_t));
799 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
803 * Convert from UCS2 to MB charset
805 * CONV_ESCAPEDOTS: escape leading dots
806 * CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
807 * CONV_IGNORE: return the first convertable characters.
808 * CONV__EILSEQ: unconvertable characters will be replaced with '_'
809 * CONV_FORCE: force convertion
811 * CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ?
812 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
813 * The escape scheme is not compatible to the old cap style escape. This is bad, we need it
814 * for e.g. HFS cdroms.
818 static size_t push_charset_flags (charset_t to_set, charset_t cap_set, char* src, size_t srclen, char* dest, size_t destlen, uint16_t *flags)
820 const uint16_t option = (flags ? *flags : 0);
821 size_t i_len, o_len, i;
823 const char* inbuf = (const char*)src;
824 char* outbuf = (char*)dest;
825 atalk_iconv_t descriptor;
826 atalk_iconv_t descriptor_cap;
827 char escch; /* 150210: uninitialized OK, depends on j */
829 descriptor = conv_handles[CH_UCS2][to_set];
830 descriptor_cap = conv_handles[CH_UCS2][cap_set];
832 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
840 if ((option & CONV_ESCAPEDOTS) &&
841 i_len >= 2 && SVAL(inbuf, 0) == 0x002e) { /* 0x002e = . */
852 *flags |= CONV_REQESCAPE;
856 for (i = 0; i < i_len; i += 2) {
857 ucs2_t c = SVAL(inbuf, i);
859 case 0x003a: /* 0x003a = ':' */
860 if ( ! (option & CONV_ALLOW_COLON)) {
868 case 0x002f: /* 0x002f = '/' */
876 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
877 if (errno == EILSEQ) {
878 if ((option & CONV_IGNORE)) {
879 *flags |= CONV_REQMANGLE;
880 return destlen - o_len;
882 if ((option & CONV_ESCAPEHEX)) {
883 const size_t bufsiz = o_len / 3 + 1;
884 char *buf = malloc(bufsiz);
890 for (buflen = 1; buflen <= bufsiz; ++buflen) {
893 if (atalk_iconv(descriptor_cap, &inbuf, &i, &b, &o) != (size_t)-1) {
896 } else if (errno != E2BIG) {
899 } else if (o < buflen) {
904 if (o_len < buflen * 3) {
911 for (i = 0; i < buflen; ++i) {
913 *outbuf++ = hexdig[(buf[i] >> 4) & 0x0f];
914 *outbuf++ = hexdig[buf[i] & 0x0f];
917 *flags |= CONV_REQESCAPE;
925 /* we have a ':' or '/' */
928 if ((option & CONV_ESCAPEHEX)) {
929 /* CAP hex encode it */
947 * THIS SHOULD NEVER BE REACHED !!!
948 * As a safety net I put in a ' ' here
964 default: /* should never be reached */
974 if (i_len > 0) errno = EINVAL;
976 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
980 * FIXME the size is a mess we really need a malloc/free logic
981 *`dest size must be dest_len +2
983 size_t convert_charset ( charset_t from_set, charset_t to_set, charset_t cap_charset, const char *src, size_t src_len, char *dest, size_t dest_len, uint16_t *flags)
987 ucs2_t buffer[MAXPATHLEN +2];
988 ucs2_t buffer2[MAXPATHLEN +2];
990 lazy_initialize_conv();
992 /* convert from_set to UCS2 */
993 if ((size_t)(-1) == ( o_len = pull_charset_flags( from_set, cap_charset, src, src_len,
994 (char *) buffer, sizeof(buffer) -2, flags)) ) {
995 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from_set));
1002 /* Do pre/decomposition */
1003 i_len = sizeof(buffer2) -2;
1005 if (CHECK_FLAGS(flags, CONV_DECOMPOSE) || (charsets[to_set] && (charsets[to_set]->flags & CHARSET_DECOMPOSED)) ) {
1006 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
1007 return (size_t)(-1);
1009 else if (CHECK_FLAGS(flags, CONV_PRECOMPOSE) || !charsets[from_set] || (charsets[from_set]->flags & CHARSET_DECOMPOSED)) {
1010 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
1011 return (size_t)(-1);
1017 /* null terminate */
1021 /* Do case conversions */
1022 if (CHECK_FLAGS(flags, CONV_TOUPPER)) {
1025 else if (CHECK_FLAGS(flags, CONV_TOLOWER)) {
1029 /* Convert UCS2 to to_set */
1030 if ((size_t)(-1) == ( o_len = push_charset_flags( to_set, cap_charset, (char *)u, i_len, dest, dest_len, flags )) ) {
1031 LOG(log_error, logtype_default,
1032 "Conversion failed (CH_UCS2 to %s):%s", charset_name(to_set), strerror(errno));
1035 /* null terminate */