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 void free_charset_names(void)
100 for (int ch = 0; ch < MAX_CHARSETS; ch++) {
101 if (charset_names[ch]) {
102 free(charset_names[ch]);
103 charset_names[ch] = NULL;
108 static struct charset_functions* get_charset_functions (charset_t ch)
110 if (charsets[ch] != NULL)
113 charsets[ch] = find_charset_functions(charset_name(ch));
119 static void lazy_initialize_conv(void)
121 static int initialized = 0;
129 charset_t add_charset(const char* name)
131 static charset_t max_charset_t = NUM_CHARSETS-1;
132 charset_t cur_charset_t = max_charset_t+1;
135 lazy_initialize_conv();
137 for (c1=0; c1<=max_charset_t;c1++) {
138 if ( strcasecmp(name, charset_name(c1)) == 0)
142 if ( cur_charset_t >= MAX_CHARSETS ) {
143 LOG (log_debug, logtype_default, "Adding charset %s failed, too many charsets (max. %u allowed)",
145 return (charset_t) -1;
148 /* First try to setup the required conversions */
150 conv_handles[cur_charset_t][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
151 if (conv_handles[cur_charset_t][CH_UCS2] == (atalk_iconv_t)-1) {
152 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
153 name, charset_name(CH_UCS2));
154 conv_handles[cur_charset_t][CH_UCS2] = NULL;
155 return (charset_t) -1;
158 conv_handles[CH_UCS2][cur_charset_t] = atalk_iconv_open( name, charset_name(CH_UCS2));
159 if (conv_handles[CH_UCS2][cur_charset_t] == (atalk_iconv_t)-1) {
160 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
161 charset_name(CH_UCS2), name);
162 conv_handles[CH_UCS2][cur_charset_t] = NULL;
163 return (charset_t) -1;
166 /* register the new charset_t name */
167 charset_names[cur_charset_t] = strdup(name);
169 charsets[cur_charset_t] = get_charset_functions (cur_charset_t);
173 LOG(log_debug9, logtype_default, "Added charset %s with handle %u", name, cur_charset_t);
175 return (cur_charset_t);
179 * Initialize iconv conversion descriptors.
181 * This is called the first time it is needed, and also called again
182 * every time the configuration is reloaded, because the charset or
183 * codepage might have changed.
185 void init_iconv(void)
189 for (c1=0;c1<NUM_CHARSETS;c1++) {
190 const char *name = charset_name((charset_t)c1);
192 conv_handles[c1][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
193 if (conv_handles[c1][CH_UCS2] == (atalk_iconv_t)-1) {
194 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
195 name, charset_name(CH_UCS2));
196 conv_handles[c1][CH_UCS2] = NULL;
199 if (c1 != CH_UCS2) { /* avoid lost memory, make valgrind happy */
200 conv_handles[CH_UCS2][c1] = atalk_iconv_open( name, charset_name(CH_UCS2));
201 if (conv_handles[CH_UCS2][c1] == (atalk_iconv_t)-1) {
202 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
203 charset_name(CH_UCS2), name);
204 conv_handles[CH_UCS2][c1] = NULL;
208 charsets[c1] = get_charset_functions (c1);
215 static size_t add_null(charset_t to, char *buf, size_t bytesleft, size_t len)
217 /* Terminate the string */
218 if (to == CH_UCS2 && bytesleft >= 2) {
223 else if ( to != CH_UCS2 && bytesleft > 0 )
235 * Convert string from one encoding to another, making error checking etc
237 * @param src pointer to source string (multibyte or singlebyte)
238 * @param srclen length of the source string in bytes
239 * @param dest pointer to destination string (multibyte or singlebyte)
240 * @param destlen maximal length allowed for string
241 * @returns the number of bytes occupied in the destination
243 static size_t convert_string_internal(charset_t from, charset_t to,
244 void const *src, size_t srclen,
245 void *dest, size_t destlen)
249 const char* inbuf = (const char*)src;
250 char* outbuf = (char*)dest;
251 char* o_save = outbuf;
252 atalk_iconv_t descriptor;
254 /* Fixed based on Samba 3.0.6 */
255 if (srclen == (size_t)-1) {
256 if (from == CH_UCS2) {
257 srclen = (strlen_w((const ucs2_t *)src)) * 2;
259 srclen = strlen((const char *)src);
264 lazy_initialize_conv();
266 descriptor = conv_handles[from][to];
268 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
274 retval = atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
275 if(retval==(size_t)-1) {
276 const char *reason="unknown error";
279 reason="Incomplete multibyte sequence";
282 reason="No more room";
285 reason="Illegal multibyte sequence";
288 LOG(log_debug, logtype_default,"Conversion error: %s",reason);
292 /* Terminate the string */
293 return add_null( to, o_save, o_len, destlen -o_len);
297 size_t convert_string(charset_t from, charset_t to,
298 void const *src, size_t srclen,
299 void *dest, size_t destlen)
303 ucs2_t buffer[MAXPATHLEN];
304 ucs2_t buffer2[MAXPATHLEN];
306 /* convert from_set to UCS2 */
307 if ((size_t)-1 == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
308 (char*) buffer, sizeof(buffer))) ) {
309 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
313 /* Do pre/decomposition */
314 i_len = sizeof(buffer2);
316 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
317 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
320 else if (!charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED)) {
321 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
328 /* Convert UCS2 to to_set */
329 if ((size_t)(-1) == ( o_len = convert_string_internal( CH_UCS2, to, (char*) u, i_len, dest, destlen)) ) {
330 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
340 * Convert between character sets, allocating a new buffer for the result.
342 * @param srclen length of source buffer.
343 * @param dest always set at least to NULL
344 * @note -1 is not accepted for srclen.
346 * @returns Size in bytes of the converted string; or -1 in case of error.
349 static size_t convert_string_allocate_internal(charset_t from, charset_t to,
350 void const *src, size_t srclen, char **dest)
352 size_t i_len, o_len, destlen;
354 const char *inbuf = (const char *)src;
355 char *outbuf = NULL, *ob = NULL;
356 atalk_iconv_t descriptor;
360 if (src == NULL || srclen == (size_t)-1)
363 lazy_initialize_conv();
365 descriptor = conv_handles[from][to];
367 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
368 /* conversion not supported, return -1*/
369 LOG(log_debug, logtype_default, "convert_string_allocate: conversion not supported!");
373 destlen = MAX(srclen, 512);
375 destlen = destlen * 2;
376 outbuf = (char *)realloc(ob, destlen);
378 LOG(log_debug, logtype_default,"convert_string_allocate: realloc failed!");
384 inbuf = src; /* this restarts the whole conversion if buffer needed to be increased */
387 retval = atalk_iconv(descriptor,
390 if(retval == (size_t)-1) {
391 const char *reason="unknown error";
394 reason="Incomplete multibyte sequence";
399 reason="Illegal multibyte sequence";
402 LOG(log_debug, logtype_default,"Conversion error: %s(%s)",reason,inbuf);
408 destlen = destlen - o_len;
410 /* Terminate the string */
411 if (to == CH_UCS2 && o_len >= 2) {
414 *dest = (char *)realloc(ob,destlen+2);
416 else if ( to != CH_UCS2 && o_len > 0 ) {
418 *dest = (char *)realloc(ob,destlen+1);
421 goto convert; /* realloc */
424 if (destlen && !*dest) {
425 LOG(log_debug, logtype_default, "convert_string_allocate: out of memory!");
434 size_t convert_string_allocate(charset_t from, charset_t to,
435 void const *src, size_t srclen,
440 ucs2_t buffer[MAXPATHLEN];
441 ucs2_t buffer2[MAXPATHLEN];
445 /* convert from_set to UCS2 */
446 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
447 buffer, sizeof(buffer))) ) {
448 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
452 /* Do pre/decomposition */
453 i_len = sizeof(buffer2);
455 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
456 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
459 else if ( !charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED) ) {
460 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
468 /* Convert UCS2 to to_set */
469 if ((size_t)-1 == ( o_len = convert_string_allocate_internal( CH_UCS2, to, (char*)u, i_len, dest)) )
470 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
476 size_t charset_strupper(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
481 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
483 if (size == (size_t)-1) {
487 if (!strupper_w((ucs2_t *)buffer) && (dest == src)) {
492 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
497 size_t charset_strlower(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
502 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
504 if (size == (size_t)-1) {
508 if (!strlower_w((ucs2_t *)buffer) && (dest == src)) {
513 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
519 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
521 return charset_strupper( CH_UNIX, src, srclen, dest, destlen);
524 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
526 return charset_strlower( CH_UNIX, src, srclen, dest, destlen);
529 size_t utf8_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
531 return charset_strupper( CH_UTF8, src, srclen, dest, destlen);
534 size_t utf8_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
536 return charset_strlower( CH_UTF8, src, srclen, dest, destlen);
540 * Copy a string from a charset_t char* src to a UCS2 destination, allocating a buffer
542 * @param dest always set at least to NULL
544 * @returns The number of bytes occupied by the string in the destination
545 * or -1 in case of error.
548 size_t charset_to_ucs2_allocate(charset_t ch, ucs2_t **dest, const char *src)
550 size_t src_len = strlen(src);
553 return convert_string_allocate(ch, CH_UCS2, src, src_len, (char**) dest);
556 /** -----------------------------------
557 * Copy a string from a charset_t char* src to a UTF-8 destination, allocating a buffer
559 * @param dest always set at least to NULL
561 * @returns The number of bytes occupied by the string in the destination
564 size_t charset_to_utf8_allocate(charset_t ch, char **dest, const char *src)
566 size_t src_len = strlen(src);
569 return convert_string_allocate(ch, CH_UTF8, src, src_len, dest);
572 /** -----------------------------------
573 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
575 * @param dest always set at least to NULL
577 * @returns The number of bytes occupied by the string in the destination
580 size_t ucs2_to_charset(charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
582 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
583 return convert_string(CH_UCS2, ch, src, src_len, dest, destlen);
586 /* --------------------------------- */
587 size_t ucs2_to_charset_allocate(charset_t ch, char **dest, const ucs2_t *src)
589 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
591 return convert_string_allocate(CH_UCS2, ch, src, src_len, dest);
594 /** ---------------------------------
595 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
597 * @param dest always set at least to NULL
599 * @returns The number of bytes occupied by the string in the destination
602 size_t utf8_to_charset_allocate(charset_t ch, char **dest, const char *src)
604 size_t src_len = strlen(src);
606 return convert_string_allocate(CH_UTF8, ch, src, src_len, dest);
609 size_t charset_precompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
612 ucs2_t u[MAXPATHLEN];
616 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
621 if ( (size_t)-1 == (ilen = precompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
626 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
635 size_t charset_decompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
638 ucs2_t u[MAXPATHLEN];
642 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
647 if ( (size_t)-1 == (ilen = decompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
652 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
661 size_t utf8_precompose ( char * src, size_t inlen, char * dst, size_t outlen)
663 return charset_precompose ( CH_UTF8, src, inlen, dst, outlen);
666 size_t utf8_decompose ( char * src, size_t inlen, char * dst, size_t outlen)
668 return charset_decompose ( CH_UTF8, src, inlen, dst, outlen);
672 static char debugbuf[ MAXPATHLEN +1 ];
673 char * debug_out ( char * seq, size_t len)
679 p = (unsigned char*) seq;
682 for ( i = 0; i<=(len-1); i++)
684 sprintf(q, "%2.2x.", *p);
695 * Convert from MB to UCS2 charset
697 * CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
698 * CONV_IGNORE: return the first convertable characters.
699 * CONV_FORCE: force convertion
701 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
702 * The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it
703 * for e.g. HFS cdroms.
706 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)
708 const uint16_t option = (flags ? *flags : 0);
711 const char* inbuf = (const char*)src;
713 atalk_iconv_t descriptor;
714 atalk_iconv_t descriptor_cap;
716 if (srclen == (size_t)-1)
717 srclen = strlen(src) + 1;
719 descriptor = conv_handles[from_set][CH_UCS2];
720 descriptor_cap = conv_handles[cap_set][CH_UCS2];
722 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
731 for (j = 0; j < i_len; ++j)
738 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
739 if (errno == EILSEQ || errno == EINVAL) {
741 if ((option & CONV_IGNORE)) {
742 *flags |= CONV_REQMANGLE;
743 return destlen - o_len;
745 if ((option & CONV__EILSEQ)) {
750 *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
755 /* FIXME reset stat ? */
766 if ((option & CONV_UNESCAPEHEX)) {
767 /* treat it as a CAP hex encoded char */
771 while (i_len >= 3 && inbuf[0] == ':' &&
772 isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
773 h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
778 const char *h_buf = h;
779 if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
782 if (errno == EILSEQ && (option & CONV_IGNORE)) {
783 *flags |= CONV_REQMANGLE;
784 return destlen - o_len;
789 /* We have an invalid :xx sequence */
791 if ((option & CONV_IGNORE)) {
792 *flags |= CONV_REQMANGLE;
793 return destlen - o_len;
798 /* a ':' that we just convert to a '/' */
799 ucs2_t slash = 0x002f;
800 memcpy(outbuf, &slash, sizeof(ucs2_t));
809 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
813 * Convert from UCS2 to MB charset
815 * CONV_ESCAPEDOTS: escape leading dots
816 * CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
817 * CONV_IGNORE: return the first convertable characters.
818 * CONV__EILSEQ: unconvertable characters will be replaced with '_'
819 * CONV_FORCE: force convertion
821 * CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ?
822 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
823 * The escape scheme is not compatible to the old cap style escape. This is bad, we need it
824 * for e.g. HFS cdroms.
828 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)
830 const uint16_t option = (flags ? *flags : 0);
831 size_t i_len, o_len, i;
833 const char* inbuf = (const char*)src;
834 char* outbuf = (char*)dest;
835 atalk_iconv_t descriptor;
836 atalk_iconv_t descriptor_cap;
837 char escch; /* 150210: uninitialized OK, depends on j */
839 descriptor = conv_handles[CH_UCS2][to_set];
840 descriptor_cap = conv_handles[CH_UCS2][cap_set];
842 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
850 if ((option & CONV_ESCAPEDOTS) &&
851 i_len >= 2 && SVAL(inbuf, 0) == 0x002e) { /* 0x002e = . */
862 *flags |= CONV_REQESCAPE;
866 for (i = 0; i < i_len; i += 2) {
867 ucs2_t c = SVAL(inbuf, i);
869 case 0x003a: /* 0x003a = ':' */
870 if ( ! (option & CONV_ALLOW_COLON)) {
878 case 0x002f: /* 0x002f = '/' */
879 if (option & CONV_ALLOW_SLASH) break;
887 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
888 if (errno == EILSEQ) {
889 if ((option & CONV_IGNORE)) {
890 *flags |= CONV_REQMANGLE;
891 return destlen - o_len;
893 if ((option & CONV_ESCAPEHEX)) {
894 const size_t bufsiz = o_len / 3 + 1;
895 char *buf = malloc(bufsiz);
901 for (buflen = 1; buflen <= bufsiz; ++buflen) {
904 if (atalk_iconv(descriptor_cap, &inbuf, &i, &b, &o) != (size_t)-1) {
907 } else if (errno != E2BIG) {
910 } else if (o < buflen) {
915 if (o_len < buflen * 3) {
922 for (i = 0; i < buflen; ++i) {
924 *outbuf++ = hexdig[(buf[i] >> 4) & 0x0f];
925 *outbuf++ = hexdig[buf[i] & 0x0f];
928 *flags |= CONV_REQESCAPE;
936 /* we have a ':' or '/' */
939 if ((option & CONV_ESCAPEHEX)) {
940 /* CAP hex encode it */
958 * THIS SHOULD NEVER BE REACHED !!!
959 * As a safety net I put in a ' ' here
975 default: /* should never be reached */
985 if (i_len > 0) errno = EINVAL;
987 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
991 * FIXME the size is a mess we really need a malloc/free logic
992 *`dest size must be dest_len +2
994 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)
998 ucs2_t buffer[MAXPATHLEN +2];
999 ucs2_t buffer2[MAXPATHLEN +2];
1001 lazy_initialize_conv();
1003 /* convert from_set to UCS2 */
1004 if ((size_t)(-1) == ( o_len = pull_charset_flags( from_set, cap_charset, src, src_len,
1005 (char *) buffer, sizeof(buffer) -2, flags)) ) {
1006 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from_set));
1013 /* Do pre/decomposition */
1014 i_len = sizeof(buffer2) -2;
1016 if (CHECK_FLAGS(flags, CONV_DECOMPOSE) || (charsets[to_set] && (charsets[to_set]->flags & CHARSET_DECOMPOSED)) ) {
1017 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
1018 return (size_t)(-1);
1020 else if (CHECK_FLAGS(flags, CONV_PRECOMPOSE) || !charsets[from_set] || (charsets[from_set]->flags & CHARSET_DECOMPOSED)) {
1021 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
1022 return (size_t)(-1);
1028 /* null terminate */
1032 /* Do case conversions */
1033 if (CHECK_FLAGS(flags, CONV_TOUPPER)) {
1036 else if (CHECK_FLAGS(flags, CONV_TOLOWER)) {
1040 /* Convert UCS2 to to_set */
1041 if ((size_t)(-1) == ( o_len = push_charset_flags( to_set, cap_charset, (char *)u, i_len, dest, dest_len, flags )) ) {
1042 LOG(log_error, logtype_default,
1043 "Conversion failed (CH_UCS2 to %s):%s", charset_name(to_set), strerror(errno));
1046 /* null terminate */