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
45 #include <arpa/inet.h>
47 #include <atalk/logger.h>
48 #include <atalk/unicode.h>
49 #include <atalk/util.h>
50 #include <atalk/compat.h>
52 #include "byteorder.h"
58 * @brief Character-set conversion routines built on our iconv.
60 * @note Samba's internal character set (at least in the 3.0 series)
61 * is always the same as the one for the Unix filesystem. It is
62 * <b>not</b> necessarily UTF-8 and may be different on machines that
63 * need i18n filenames to be compatible with Unix software. It does
64 * have to be a superset of ASCII. All multibyte sequences must start
65 * with a byte with the high bit set.
71 #define MAX_CHARSETS 20
73 #define CHECK_FLAGS(a,b) (((a)!=NULL) ? (*(a) & (b)) : 0 )
75 static atalk_iconv_t conv_handles[MAX_CHARSETS][MAX_CHARSETS];
76 static char* charset_names[MAX_CHARSETS];
77 static struct charset_functions* charsets[MAX_CHARSETS];
78 static char hexdig[] = "0123456789abcdef";
79 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
83 * Return the name of a charset to give to iconv().
85 static const char *charset_name(charset_t ch)
87 const char *ret = NULL;
89 static char macname[128];
90 static char unixname[128];
93 memset(macname, 0, sizeof(macname));
94 memset(unixname, 0, sizeof(unixname));
98 if (ch == CH_UCS2) ret = "UCS-2";
99 else if (ch == CH_UTF8) ret = "UTF8";
100 else if (ch == CH_UTF8_MAC) ret = "UTF8-MAC";
101 else if (ch == CH_UNIX) {
102 if (unixname[0] == '\0') {
104 strlcpy(unixname, ret, sizeof(unixname));
109 else if (ch == CH_MAC) {
110 if (macname[0] == '\0') {
112 strlcpy(macname, ret, sizeof(macname));
119 ret = charset_names[ch];
122 if (ret && strcasecmp(ret, "LOCALE") == 0) {
123 const char *ln = NULL;
125 setlocale(LC_ALL, "");
126 ln = nl_langinfo(CODESET);
128 /* Check whether the charset name is supported
130 atalk_iconv_t handle = atalk_iconv_open(ln, "UCS-2");
131 if (handle == (atalk_iconv_t) -1) {
132 LOG(log_debug, logtype_default, "Locale charset '%s' unsupported, using ASCII instead", ln);
135 atalk_iconv_close(handle);
138 strlcpy(unixname, ln, sizeof(unixname));
142 #else /* system doesn't have LOCALE support */
143 if (ch == CH_UNIX) ret = NULL;
146 if (!ret || !*ret) ret = "ASCII";
150 static struct charset_functions* get_charset_functions (charset_t ch)
152 if (charsets[ch] != NULL)
155 charsets[ch] = find_charset_functions(charset_name(ch));
161 static void lazy_initialize_conv(void)
163 static int initialized = 0;
171 charset_t add_charset(const char* name)
173 static charset_t max_charset_t = NUM_CHARSETS-1;
174 charset_t cur_charset_t = max_charset_t+1;
177 lazy_initialize_conv();
179 for (c1=0; c1<=max_charset_t;c1++) {
180 if ( strcasecmp(name, charset_name(c1)) == 0)
184 if ( cur_charset_t >= MAX_CHARSETS ) {
185 LOG (log_debug, logtype_default, "Adding charset %s failed, too many charsets (max. %u allowed)",
187 return (charset_t) -1;
190 /* First try to setup the required conversions */
192 conv_handles[cur_charset_t][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
193 if (conv_handles[cur_charset_t][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[cur_charset_t][CH_UCS2] = NULL;
197 return (charset_t) -1;
200 conv_handles[CH_UCS2][cur_charset_t] = atalk_iconv_open( name, charset_name(CH_UCS2));
201 if (conv_handles[CH_UCS2][cur_charset_t] == (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][cur_charset_t] = NULL;
205 return (charset_t) -1;
208 /* register the new charset_t name */
209 charset_names[cur_charset_t] = strdup(name);
211 charsets[cur_charset_t] = get_charset_functions (cur_charset_t);
215 LOG(log_debug9, logtype_default, "Added charset %s with handle %u", name, cur_charset_t);
217 return (cur_charset_t);
221 * Initialize iconv conversion descriptors.
223 * This is called the first time it is needed, and also called again
224 * every time the configuration is reloaded, because the charset or
225 * codepage might have changed.
227 void init_iconv(void)
231 for (c1=0;c1<NUM_CHARSETS;c1++) {
232 const char *name = charset_name((charset_t)c1);
234 conv_handles[c1][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
235 if (conv_handles[c1][CH_UCS2] == (atalk_iconv_t)-1) {
236 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
237 name, charset_name(CH_UCS2));
238 conv_handles[c1][CH_UCS2] = NULL;
241 if (c1 != CH_UCS2) { /* avoid lost memory, make valgrind happy */
242 conv_handles[CH_UCS2][c1] = atalk_iconv_open( name, charset_name(CH_UCS2));
243 if (conv_handles[CH_UCS2][c1] == (atalk_iconv_t)-1) {
244 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
245 charset_name(CH_UCS2), name);
246 conv_handles[CH_UCS2][c1] = NULL;
250 charsets[c1] = get_charset_functions (c1);
257 static size_t add_null(charset_t to, char *buf, size_t bytesleft, size_t len)
259 /* Terminate the string */
260 if (to == CH_UCS2 && bytesleft >= 2) {
265 else if ( to != CH_UCS2 && bytesleft > 0 )
277 * Convert string from one encoding to another, making error checking etc
279 * @param src pointer to source string (multibyte or singlebyte)
280 * @param srclen length of the source string in bytes
281 * @param dest pointer to destination string (multibyte or singlebyte)
282 * @param destlen maximal length allowed for string
283 * @returns the number of bytes occupied in the destination
285 static size_t convert_string_internal(charset_t from, charset_t to,
286 void const *src, size_t srclen,
287 void *dest, size_t destlen)
291 const char* inbuf = (const char*)src;
292 char* outbuf = (char*)dest;
293 char* o_save = outbuf;
294 atalk_iconv_t descriptor;
296 /* Fixed based on Samba 3.0.6 */
297 if (srclen == (size_t)-1) {
298 if (from == CH_UCS2) {
299 srclen = (strlen_w((const ucs2_t *)src)) * 2;
301 srclen = strlen((const char *)src);
306 lazy_initialize_conv();
308 descriptor = conv_handles[from][to];
310 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
316 retval = atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
317 if(retval==(size_t)-1) {
318 const char *reason="unknown error";
321 reason="Incomplete multibyte sequence";
324 reason="No more room";
327 reason="Illegal multibyte sequence";
330 LOG(log_debug, logtype_default,"Conversion error: %s",reason);
334 /* Terminate the string */
335 return add_null( to, o_save, o_len, destlen -o_len);
339 size_t convert_string(charset_t from, charset_t to,
340 void const *src, size_t srclen,
341 void *dest, size_t destlen)
345 ucs2_t buffer[MAXPATHLEN];
346 ucs2_t buffer2[MAXPATHLEN];
348 /* convert from_set to UCS2 */
349 if ((size_t)-1 == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
350 (char*) buffer, sizeof(buffer))) ) {
351 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
355 /* Do pre/decomposition */
356 i_len = sizeof(buffer2);
358 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
359 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
362 else if (!charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED)) {
363 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
370 /* Convert UCS2 to to_set */
371 if ((size_t)(-1) == ( o_len = convert_string_internal( CH_UCS2, to, (char*) u, i_len, dest, destlen)) ) {
372 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
382 * Convert between character sets, allocating a new buffer for the result.
384 * @param srclen length of source buffer.
385 * @param dest always set at least to NULL
386 * @note -1 is not accepted for srclen.
388 * @returns Size in bytes of the converted string; or -1 in case of error.
391 static size_t convert_string_allocate_internal(charset_t from, charset_t to,
392 void const *src, size_t srclen, char **dest)
394 size_t i_len, o_len, destlen;
396 const char *inbuf = (const char *)src;
397 char *outbuf = NULL, *ob = NULL;
398 atalk_iconv_t descriptor;
402 if (src == NULL || srclen == (size_t)-1)
405 lazy_initialize_conv();
407 descriptor = conv_handles[from][to];
409 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
410 /* conversion not supported, return -1*/
411 LOG(log_debug, logtype_default, "convert_string_allocate: conversion not supported!");
415 destlen = MAX(srclen, 512);
417 destlen = destlen * 2;
418 outbuf = (char *)realloc(ob, destlen);
420 LOG(log_debug, logtype_default,"convert_string_allocate: realloc failed!");
426 inbuf = src; /* this restarts the whole conversion if buffer needed to be increased */
429 retval = atalk_iconv(descriptor,
432 if(retval == (size_t)-1) {
433 const char *reason="unknown error";
436 reason="Incomplete multibyte sequence";
441 reason="Illegal multibyte sequence";
444 LOG(log_debug, logtype_default,"Conversion error: %s(%s)",reason,inbuf);
450 destlen = destlen - o_len;
452 /* Terminate the string */
453 if (to == CH_UCS2 && o_len >= 2) {
456 *dest = (char *)realloc(ob,destlen+2);
458 else if ( to != CH_UCS2 && o_len > 0 ) {
460 *dest = (char *)realloc(ob,destlen+1);
463 goto convert; /* realloc */
466 if (destlen && !*dest) {
467 LOG(log_debug, logtype_default, "convert_string_allocate: out of memory!");
476 size_t convert_string_allocate(charset_t from, charset_t to,
477 void const *src, size_t srclen,
482 ucs2_t buffer[MAXPATHLEN];
483 ucs2_t buffer2[MAXPATHLEN];
487 /* convert from_set to UCS2 */
488 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
489 buffer, sizeof(buffer))) ) {
490 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
494 /* Do pre/decomposition */
495 i_len = sizeof(buffer2);
497 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
498 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
501 else if ( !charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED) ) {
502 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
510 /* Convert UCS2 to to_set */
511 if ((size_t)-1 == ( o_len = convert_string_allocate_internal( CH_UCS2, to, (char*)u, i_len, dest)) )
512 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
518 size_t charset_strupper(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
523 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
525 if (size == (size_t)-1) {
529 if (!strupper_w((ucs2_t *)buffer) && (dest == src)) {
534 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
539 size_t charset_strlower(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
544 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
546 if (size == (size_t)-1) {
550 if (!strlower_w((ucs2_t *)buffer) && (dest == src)) {
555 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
561 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
563 return charset_strupper( CH_UNIX, src, srclen, dest, destlen);
566 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
568 return charset_strlower( CH_UNIX, src, srclen, dest, destlen);
571 size_t utf8_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
573 return charset_strupper( CH_UTF8, src, srclen, dest, destlen);
576 size_t utf8_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
578 return charset_strlower( CH_UTF8, src, srclen, dest, destlen);
582 * Copy a string from a charset_t char* src to a UCS2 destination, allocating a buffer
584 * @param dest always set at least to NULL
586 * @returns The number of bytes occupied by the string in the destination
587 * or -1 in case of error.
590 size_t charset_to_ucs2_allocate(charset_t ch, ucs2_t **dest, const char *src)
592 size_t src_len = strlen(src);
595 return convert_string_allocate(ch, CH_UCS2, src, src_len, (char**) dest);
598 /** -----------------------------------
599 * Copy a string from a charset_t char* src to a UTF-8 destination, allocating a buffer
601 * @param dest always set at least to NULL
603 * @returns The number of bytes occupied by the string in the destination
606 size_t charset_to_utf8_allocate(charset_t ch, char **dest, const char *src)
608 size_t src_len = strlen(src);
611 return convert_string_allocate(ch, CH_UTF8, src, src_len, dest);
614 /** -----------------------------------
615 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
617 * @param dest always set at least to NULL
619 * @returns The number of bytes occupied by the string in the destination
622 size_t ucs2_to_charset(charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
624 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
625 return convert_string(CH_UCS2, ch, src, src_len, dest, destlen);
628 /* --------------------------------- */
629 size_t ucs2_to_charset_allocate(charset_t ch, char **dest, const ucs2_t *src)
631 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
633 return convert_string_allocate(CH_UCS2, ch, src, src_len, dest);
636 /** ---------------------------------
637 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
639 * @param dest always set at least to NULL
641 * @returns The number of bytes occupied by the string in the destination
644 size_t utf8_to_charset_allocate(charset_t ch, char **dest, const char *src)
646 size_t src_len = strlen(src);
648 return convert_string_allocate(CH_UTF8, ch, src, src_len, dest);
651 size_t charset_precompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
654 ucs2_t u[MAXPATHLEN];
658 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
663 if ( (size_t)-1 == (ilen = precompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
668 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
677 size_t charset_decompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
680 ucs2_t u[MAXPATHLEN];
684 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
689 if ( (size_t)-1 == (ilen = decompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
694 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
703 size_t utf8_precompose ( char * src, size_t inlen, char * dst, size_t outlen)
705 return charset_precompose ( CH_UTF8, src, inlen, dst, outlen);
708 size_t utf8_decompose ( char * src, size_t inlen, char * dst, size_t outlen)
710 return charset_decompose ( CH_UTF8, src, inlen, dst, outlen);
714 static char debugbuf[ MAXPATHLEN +1 ];
715 char * debug_out ( char * seq, size_t len)
721 p = (unsigned char*) seq;
724 for ( i = 0; i<=(len-1); i++)
726 sprintf(q, "%2.2x.", *p);
737 * Convert from MB to UCS2 charset
739 * CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
740 * CONV_IGNORE: return the first convertable characters.
741 * CONV_FORCE: force convertion
743 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
744 * The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it
745 * for e.g. HFS cdroms.
748 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)
750 const uint16_t option = (flags ? *flags : 0);
753 const char* inbuf = (const char*)src;
755 atalk_iconv_t descriptor;
756 atalk_iconv_t descriptor_cap;
758 if (srclen == (size_t)-1)
759 srclen = strlen(src) + 1;
761 descriptor = conv_handles[from_set][CH_UCS2];
762 descriptor_cap = conv_handles[cap_set][CH_UCS2];
764 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
773 if ((option & CONV_UNESCAPEHEX)) {
774 for (j = 0; j < i_len; ++j) {
775 if (inbuf[j] == ':') break;
782 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
783 if (errno == EILSEQ || errno == EINVAL) {
785 if ((option & CONV_IGNORE)) {
786 *flags |= CONV_REQMANGLE;
787 return destlen - o_len;
789 if ((option & CONV__EILSEQ)) {
794 *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
799 /* FIXME reset stat ? */
807 /* we're at the start on an hex encoded ucs2 char */
812 while (i_len >= 3 && inbuf[0] == ':' &&
813 isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
814 h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
819 const char *h_buf = h;
820 if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
823 if (errno == EILSEQ && (option & CONV_IGNORE)) {
824 *flags |= CONV_REQMANGLE;
825 return destlen - o_len;
830 /* We have an invalid :xx sequence */
832 if ((option & CONV_IGNORE)) {
833 *flags |= CONV_REQMANGLE;
834 return destlen - o_len;
841 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
845 * Convert from UCS2 to MB charset
847 * CONV_ESCAPEDOTS: escape leading dots
848 * CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
849 * CONV_IGNORE: return the first convertable characters.
850 * CONV__EILSEQ: unconvertable characters will be replaced with '_'
851 * CONV_FORCE: force convertion
853 * CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ?
854 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
855 * The escape scheme is not compatible to the old cap style escape. This is bad, we need it
856 * for e.g. HFS cdroms.
860 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)
862 const uint16_t option = (flags ? *flags : 0);
863 size_t i_len, o_len, i;
865 const char* inbuf = (const char*)src;
866 char* outbuf = (char*)dest;
867 atalk_iconv_t descriptor;
868 atalk_iconv_t descriptor_cap;
869 char escch; /* 150210: uninitialized OK, depends on j */
871 descriptor = conv_handles[CH_UCS2][to_set];
872 descriptor_cap = conv_handles[CH_UCS2][cap_set];
874 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
882 if ((option & CONV_ESCAPEDOTS) &&
883 i_len >= 2 && SVAL(inbuf, 0) == 0x002e) { /* 0x002e = . */
894 *flags |= CONV_REQESCAPE;
898 if ((option & CONV_ESCAPEHEX)) {
899 for (i = 0; i < i_len; i += 2) {
900 ucs2_t c = SVAL(inbuf, i);
902 case 0x003a: /* 0x003a = ':' */
903 if ( ! (option & CONV_ALLOW_COLON)) {
911 case 0x002f: /* 0x002f = '/' */
920 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
921 if (errno == EILSEQ) {
922 if ((option & CONV_IGNORE)) {
923 *flags |= CONV_REQMANGLE;
924 return destlen - o_len;
926 if ((option & CONV_ESCAPEHEX)) {
927 const size_t bufsiz = o_len / 3 + 1;
928 char *buf = malloc(bufsiz);
934 for (buflen = 1; buflen <= bufsiz; ++buflen) {
937 if (atalk_iconv(descriptor_cap, &inbuf, &i, &b, &o) != (size_t)-1) {
940 } else if (errno != E2BIG) {
943 } else if (o < buflen) {
948 if (o_len < buflen * 3) {
955 for (i = 0; i < buflen; ++i) {
957 *outbuf++ = hexdig[(buf[i] >> 4) & 0x0f];
958 *outbuf++ = hexdig[buf[i] & 0x0f];
961 *flags |= CONV_REQESCAPE;
987 * THIS SHOULD NEVER BE REACHED !!!
988 * As a safety net I put in a ' ' here
1000 if (i_len > 0) errno = EINVAL;
1002 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
1006 * FIXME the size is a mess we really need a malloc/free logic
1007 *`dest size must be dest_len +2
1009 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)
1011 size_t i_len, o_len;
1013 ucs2_t buffer[MAXPATHLEN +2];
1014 ucs2_t buffer2[MAXPATHLEN +2];
1016 lazy_initialize_conv();
1018 /* convert from_set to UCS2 */
1019 if ((size_t)(-1) == ( o_len = pull_charset_flags( from_set, cap_charset, src, src_len,
1020 (char *) buffer, sizeof(buffer) -2, flags)) ) {
1021 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from_set));
1028 /* Do pre/decomposition */
1029 i_len = sizeof(buffer2) -2;
1031 if (CHECK_FLAGS(flags, CONV_DECOMPOSE) || (charsets[to_set] && (charsets[to_set]->flags & CHARSET_DECOMPOSED)) ) {
1032 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
1033 return (size_t)(-1);
1035 else if (CHECK_FLAGS(flags, CONV_PRECOMPOSE) || !charsets[from_set] || (charsets[from_set]->flags & CHARSET_DECOMPOSED)) {
1036 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
1037 return (size_t)(-1);
1043 /* null terminate */
1047 /* Do case conversions */
1048 if (CHECK_FLAGS(flags, CONV_TOUPPER)) {
1051 else if (CHECK_FLAGS(flags, CONV_TOLOWER)) {
1055 /* Convert UCS2 to to_set */
1056 if ((size_t)(-1) == ( o_len = push_charset_flags( to_set, cap_charset, (char *)u, i_len, dest, dest_len, flags )) ) {
1057 LOG(log_error, logtype_default,
1058 "Conversion failed (CH_UCS2 to %s):%s", charset_name(to_set), strerror(errno));
1061 /* null terminate */