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>
45 #include <atalk/byteorder.h>
51 * @brief Character-set conversion routines built on our iconv.
53 * @note Samba's internal character set (at least in the 3.0 series)
54 * is always the same as the one for the Unix filesystem. It is
55 * <b>not</b> necessarily UTF-8 and may be different on machines that
56 * need i18n filenames to be compatible with Unix software. It does
57 * have to be a superset of ASCII. All multibyte sequences must start
58 * with a byte with the high bit set.
64 #define MAX_CHARSETS 20
66 #define CHECK_FLAGS(a,b) (((a)!=NULL) ? (*(a) & (b)) : 0 )
68 static atalk_iconv_t conv_handles[MAX_CHARSETS][MAX_CHARSETS];
69 static char* charset_names[MAX_CHARSETS];
70 static struct charset_functions* charsets[MAX_CHARSETS];
71 static char hexdig[] = "0123456789abcdef";
72 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
76 * Return the name of a charset to give to iconv().
78 static const char *charset_name(charset_t ch)
80 const char *ret = NULL;
82 if (ch == CH_UCS2) ret = "UCS-2";
83 else if (ch == CH_UTF8) ret = "UTF8";
84 else if (ch == CH_UTF8_MAC) ret = "UTF8-MAC";
85 else ret = charset_names[ch];
89 int set_charset_name(charset_t ch, const char *name)
91 if (ch >= NUM_CHARSETS)
93 charset_names[ch] = strdup(name);
97 static struct charset_functions* get_charset_functions (charset_t ch)
99 if (charsets[ch] != NULL)
102 charsets[ch] = find_charset_functions(charset_name(ch));
108 static void lazy_initialize_conv(void)
110 static int initialized = 0;
118 charset_t add_charset(const char* name)
120 static charset_t max_charset_t = NUM_CHARSETS-1;
121 charset_t cur_charset_t = max_charset_t+1;
124 lazy_initialize_conv();
126 for (c1=0; c1<=max_charset_t;c1++) {
127 if ( strcasecmp(name, charset_name(c1)) == 0)
131 if ( cur_charset_t >= MAX_CHARSETS ) {
132 LOG (log_debug, logtype_default, "Adding charset %s failed, too many charsets (max. %u allowed)",
134 return (charset_t) -1;
137 /* First try to setup the required conversions */
139 conv_handles[cur_charset_t][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
140 if (conv_handles[cur_charset_t][CH_UCS2] == (atalk_iconv_t)-1) {
141 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
142 name, charset_name(CH_UCS2));
143 conv_handles[cur_charset_t][CH_UCS2] = NULL;
144 return (charset_t) -1;
147 conv_handles[CH_UCS2][cur_charset_t] = atalk_iconv_open( name, charset_name(CH_UCS2));
148 if (conv_handles[CH_UCS2][cur_charset_t] == (atalk_iconv_t)-1) {
149 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
150 charset_name(CH_UCS2), name);
151 conv_handles[CH_UCS2][cur_charset_t] = NULL;
152 return (charset_t) -1;
155 /* register the new charset_t name */
156 charset_names[cur_charset_t] = strdup(name);
158 charsets[cur_charset_t] = get_charset_functions (cur_charset_t);
162 LOG(log_debug9, logtype_default, "Added charset %s with handle %u", name, cur_charset_t);
164 return (cur_charset_t);
168 * Initialize iconv conversion descriptors.
170 * This is called the first time it is needed, and also called again
171 * every time the configuration is reloaded, because the charset or
172 * codepage might have changed.
174 void init_iconv(void)
178 for (c1=0;c1<NUM_CHARSETS;c1++) {
179 const char *name = charset_name((charset_t)c1);
181 conv_handles[c1][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
182 if (conv_handles[c1][CH_UCS2] == (atalk_iconv_t)-1) {
183 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
184 name, charset_name(CH_UCS2));
185 conv_handles[c1][CH_UCS2] = NULL;
188 if (c1 != CH_UCS2) { /* avoid lost memory, make valgrind happy */
189 conv_handles[CH_UCS2][c1] = atalk_iconv_open( name, charset_name(CH_UCS2));
190 if (conv_handles[CH_UCS2][c1] == (atalk_iconv_t)-1) {
191 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
192 charset_name(CH_UCS2), name);
193 conv_handles[CH_UCS2][c1] = NULL;
197 charsets[c1] = get_charset_functions (c1);
204 static size_t add_null(charset_t to, char *buf, size_t bytesleft, size_t len)
206 /* Terminate the string */
207 if (to == CH_UCS2 && bytesleft >= 2) {
212 else if ( to != CH_UCS2 && bytesleft > 0 )
224 * Convert string from one encoding to another, making error checking etc
226 * @param src pointer to source string (multibyte or singlebyte)
227 * @param srclen length of the source string in bytes
228 * @param dest pointer to destination string (multibyte or singlebyte)
229 * @param destlen maximal length allowed for string
230 * @returns the number of bytes occupied in the destination
232 static size_t convert_string_internal(charset_t from, charset_t to,
233 void const *src, size_t srclen,
234 void *dest, size_t destlen)
238 const char* inbuf = (const char*)src;
239 char* outbuf = (char*)dest;
240 char* o_save = outbuf;
241 atalk_iconv_t descriptor;
243 /* Fixed based on Samba 3.0.6 */
244 if (srclen == (size_t)-1) {
245 if (from == CH_UCS2) {
246 srclen = (strlen_w((const ucs2_t *)src)) * 2;
248 srclen = strlen((const char *)src);
253 lazy_initialize_conv();
255 descriptor = conv_handles[from][to];
257 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
263 retval = atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
264 if(retval==(size_t)-1) {
265 const char *reason="unknown error";
268 reason="Incomplete multibyte sequence";
271 reason="No more room";
274 reason="Illegal multibyte sequence";
277 LOG(log_debug, logtype_default,"Conversion error: %s",reason);
281 /* Terminate the string */
282 return add_null( to, o_save, o_len, destlen -o_len);
286 size_t convert_string(charset_t from, charset_t to,
287 void const *src, size_t srclen,
288 void *dest, size_t destlen)
292 ucs2_t buffer[MAXPATHLEN];
293 ucs2_t buffer2[MAXPATHLEN];
295 /* convert from_set to UCS2 */
296 if ((size_t)-1 == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
297 (char*) buffer, sizeof(buffer))) ) {
298 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
302 /* Do pre/decomposition */
303 i_len = sizeof(buffer2);
305 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
306 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
309 else if (!charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED)) {
310 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
317 /* Convert UCS2 to to_set */
318 if ((size_t)(-1) == ( o_len = convert_string_internal( CH_UCS2, to, (char*) u, i_len, dest, destlen)) ) {
319 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
329 * Convert between character sets, allocating a new buffer for the result.
331 * @param srclen length of source buffer.
332 * @param dest always set at least to NULL
333 * @note -1 is not accepted for srclen.
335 * @returns Size in bytes of the converted string; or -1 in case of error.
338 static size_t convert_string_allocate_internal(charset_t from, charset_t to,
339 void const *src, size_t srclen, char **dest)
341 size_t i_len, o_len, destlen;
343 const char *inbuf = (const char *)src;
344 char *outbuf = NULL, *ob = NULL;
345 atalk_iconv_t descriptor;
349 if (src == NULL || srclen == (size_t)-1)
352 lazy_initialize_conv();
354 descriptor = conv_handles[from][to];
356 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
357 /* conversion not supported, return -1*/
358 LOG(log_debug, logtype_default, "convert_string_allocate: conversion not supported!");
362 destlen = MAX(srclen, 512);
364 destlen = destlen * 2;
365 outbuf = (char *)realloc(ob, destlen);
367 LOG(log_debug, logtype_default,"convert_string_allocate: realloc failed!");
373 inbuf = src; /* this restarts the whole conversion if buffer needed to be increased */
376 retval = atalk_iconv(descriptor,
379 if(retval == (size_t)-1) {
380 const char *reason="unknown error";
383 reason="Incomplete multibyte sequence";
388 reason="Illegal multibyte sequence";
391 LOG(log_debug, logtype_default,"Conversion error: %s(%s)",reason,inbuf);
397 destlen = destlen - o_len;
399 /* Terminate the string */
400 if (to == CH_UCS2 && o_len >= 2) {
403 *dest = (char *)realloc(ob,destlen+2);
405 else if ( to != CH_UCS2 && o_len > 0 ) {
407 *dest = (char *)realloc(ob,destlen+1);
410 goto convert; /* realloc */
413 if (destlen && !*dest) {
414 LOG(log_debug, logtype_default, "convert_string_allocate: out of memory!");
423 size_t convert_string_allocate(charset_t from, charset_t to,
424 void const *src, size_t srclen,
429 ucs2_t buffer[MAXPATHLEN];
430 ucs2_t buffer2[MAXPATHLEN];
434 /* convert from_set to UCS2 */
435 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
436 buffer, sizeof(buffer))) ) {
437 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
441 /* Do pre/decomposition */
442 i_len = sizeof(buffer2);
444 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
445 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
448 else if ( !charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED) ) {
449 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
457 /* Convert UCS2 to to_set */
458 if ((size_t)-1 == ( o_len = convert_string_allocate_internal( CH_UCS2, to, (char*)u, i_len, dest)) )
459 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
465 size_t charset_strupper(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
470 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
472 if (size == (size_t)-1) {
476 if (!strupper_w((ucs2_t *)buffer) && (dest == src)) {
481 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
486 size_t charset_strlower(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
491 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
493 if (size == (size_t)-1) {
497 if (!strlower_w((ucs2_t *)buffer) && (dest == src)) {
502 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
508 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
510 return charset_strupper( CH_UNIX, src, srclen, dest, destlen);
513 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
515 return charset_strlower( CH_UNIX, src, srclen, dest, destlen);
518 size_t utf8_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
520 return charset_strupper( CH_UTF8, src, srclen, dest, destlen);
523 size_t utf8_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
525 return charset_strlower( CH_UTF8, src, srclen, dest, destlen);
529 * Copy a string from a charset_t char* src to a UCS2 destination, allocating a buffer
531 * @param dest always set at least to NULL
533 * @returns The number of bytes occupied by the string in the destination
534 * or -1 in case of error.
537 size_t charset_to_ucs2_allocate(charset_t ch, ucs2_t **dest, const char *src)
539 size_t src_len = strlen(src);
542 return convert_string_allocate(ch, CH_UCS2, src, src_len, (char**) dest);
545 /** -----------------------------------
546 * Copy a string from a charset_t char* src to a UTF-8 destination, allocating a buffer
548 * @param dest always set at least to NULL
550 * @returns The number of bytes occupied by the string in the destination
553 size_t charset_to_utf8_allocate(charset_t ch, char **dest, const char *src)
555 size_t src_len = strlen(src);
558 return convert_string_allocate(ch, CH_UTF8, src, src_len, dest);
561 /** -----------------------------------
562 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
564 * @param dest always set at least to NULL
566 * @returns The number of bytes occupied by the string in the destination
569 size_t ucs2_to_charset(charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
571 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
572 return convert_string(CH_UCS2, ch, src, src_len, dest, destlen);
575 /* --------------------------------- */
576 size_t ucs2_to_charset_allocate(charset_t ch, char **dest, const ucs2_t *src)
578 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
580 return convert_string_allocate(CH_UCS2, ch, src, src_len, dest);
583 /** ---------------------------------
584 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
586 * @param dest always set at least to NULL
588 * @returns The number of bytes occupied by the string in the destination
591 size_t utf8_to_charset_allocate(charset_t ch, char **dest, const char *src)
593 size_t src_len = strlen(src);
595 return convert_string_allocate(CH_UTF8, ch, src, src_len, dest);
598 size_t charset_precompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
601 ucs2_t u[MAXPATHLEN];
605 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
610 if ( (size_t)-1 == (ilen = precompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
615 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
624 size_t charset_decompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
627 ucs2_t u[MAXPATHLEN];
631 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
636 if ( (size_t)-1 == (ilen = decompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
641 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
650 size_t utf8_precompose ( char * src, size_t inlen, char * dst, size_t outlen)
652 return charset_precompose ( CH_UTF8, src, inlen, dst, outlen);
655 size_t utf8_decompose ( char * src, size_t inlen, char * dst, size_t outlen)
657 return charset_decompose ( CH_UTF8, src, inlen, dst, outlen);
661 static char debugbuf[ MAXPATHLEN +1 ];
662 char * debug_out ( char * seq, size_t len)
668 p = (unsigned char*) seq;
671 for ( i = 0; i<=(len-1); i++)
673 sprintf(q, "%2.2x.", *p);
684 * Convert from MB to UCS2 charset
686 * CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
687 * CONV_IGNORE: return the first convertable characters.
688 * CONV_FORCE: force convertion
690 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
691 * The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it
692 * for e.g. HFS cdroms.
695 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)
697 const uint16_t option = (flags ? *flags : 0);
700 const char* inbuf = (const char*)src;
702 atalk_iconv_t descriptor;
703 atalk_iconv_t descriptor_cap;
705 if (srclen == (size_t)-1)
706 srclen = strlen(src) + 1;
708 descriptor = conv_handles[from_set][CH_UCS2];
709 descriptor_cap = conv_handles[cap_set][CH_UCS2];
711 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
720 for (j = 0; j < i_len; ++j)
727 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
728 if (errno == EILSEQ || errno == EINVAL) {
730 if ((option & CONV_IGNORE)) {
731 *flags |= CONV_REQMANGLE;
732 return destlen - o_len;
734 if ((option & CONV__EILSEQ)) {
739 *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
744 /* FIXME reset stat ? */
755 if ((option & CONV_UNESCAPEHEX)) {
756 /* treat it as a CAP hex encoded char */
760 while (i_len >= 3 && inbuf[0] == ':' &&
761 isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
762 h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
767 const char *h_buf = h;
768 if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
771 if (errno == EILSEQ && (option & CONV_IGNORE)) {
772 *flags |= CONV_REQMANGLE;
773 return destlen - o_len;
778 /* We have an invalid :xx sequence */
780 if ((option & CONV_IGNORE)) {
781 *flags |= CONV_REQMANGLE;
782 return destlen - o_len;
787 /* a ':' that we just convert to a '/' */
788 ucs2_t slash = 0x002f;
789 memcpy(outbuf, &slash, sizeof(ucs2_t));
798 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
802 * Convert from UCS2 to MB charset
804 * CONV_ESCAPEDOTS: escape leading dots
805 * CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
806 * CONV_IGNORE: return the first convertable characters.
807 * CONV__EILSEQ: unconvertable characters will be replaced with '_'
808 * CONV_FORCE: force convertion
810 * CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ?
811 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
812 * The escape scheme is not compatible to the old cap style escape. This is bad, we need it
813 * for e.g. HFS cdroms.
817 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)
819 const uint16_t option = (flags ? *flags : 0);
820 size_t i_len, o_len, i;
822 const char* inbuf = (const char*)src;
823 char* outbuf = (char*)dest;
824 atalk_iconv_t descriptor;
825 atalk_iconv_t descriptor_cap;
826 char escch; /* 150210: uninitialized OK, depends on j */
828 descriptor = conv_handles[CH_UCS2][to_set];
829 descriptor_cap = conv_handles[CH_UCS2][cap_set];
831 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
839 if ((option & CONV_ESCAPEDOTS) &&
840 i_len >= 2 && SVAL(inbuf, 0) == 0x002e) { /* 0x002e = . */
851 *flags |= CONV_REQESCAPE;
855 for (i = 0; i < i_len; i += 2) {
856 ucs2_t c = SVAL(inbuf, i);
858 case 0x003a: /* 0x003a = ':' */
859 if ( ! (option & CONV_ALLOW_COLON)) {
867 case 0x002f: /* 0x002f = '/' */
868 if (option & CONV_ALLOW_SLASH) break;
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 */