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 LOG(log_info, logtype_default, "Locale charset is '%s'", ln);
131 atalk_iconv_t handle = atalk_iconv_open(ln, "UCS-2");
132 if (handle == (atalk_iconv_t) -1) {
133 LOG(log_warning, logtype_default, "Locale charset '%s' unsupported, using ASCII instead", ln);
136 atalk_iconv_close(handle);
139 strlcpy(unixname, ln, sizeof(unixname));
143 #else /* system doesn't have LOCALE support */
144 LOG(log_warning, logtype_default, "system doesn't have LOCALE support");
145 if (ch == CH_UNIX) ret = NULL;
148 if (!ret || !*ret) ret = "ASCII";
152 static struct charset_functions* get_charset_functions (charset_t ch)
154 if (charsets[ch] != NULL)
157 charsets[ch] = find_charset_functions(charset_name(ch));
163 static void lazy_initialize_conv(void)
165 static int initialized = 0;
173 charset_t add_charset(const char* name)
175 static charset_t max_charset_t = NUM_CHARSETS-1;
176 charset_t cur_charset_t = max_charset_t+1;
179 lazy_initialize_conv();
181 for (c1=0; c1<=max_charset_t;c1++) {
182 if ( strcasecmp(name, charset_name(c1)) == 0)
186 if ( cur_charset_t >= MAX_CHARSETS ) {
187 LOG (log_debug, logtype_default, "Adding charset %s failed, too many charsets (max. %u allowed)",
189 return (charset_t) -1;
192 /* First try to setup the required conversions */
194 conv_handles[cur_charset_t][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
195 if (conv_handles[cur_charset_t][CH_UCS2] == (atalk_iconv_t)-1) {
196 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
197 name, charset_name(CH_UCS2));
198 conv_handles[cur_charset_t][CH_UCS2] = NULL;
199 return (charset_t) -1;
202 conv_handles[CH_UCS2][cur_charset_t] = atalk_iconv_open( name, charset_name(CH_UCS2));
203 if (conv_handles[CH_UCS2][cur_charset_t] == (atalk_iconv_t)-1) {
204 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
205 charset_name(CH_UCS2), name);
206 conv_handles[CH_UCS2][cur_charset_t] = NULL;
207 return (charset_t) -1;
210 /* register the new charset_t name */
211 charset_names[cur_charset_t] = strdup(name);
213 charsets[cur_charset_t] = get_charset_functions (cur_charset_t);
217 LOG(log_debug9, logtype_default, "Added charset %s with handle %u", name, cur_charset_t);
219 return (cur_charset_t);
223 * Initialize iconv conversion descriptors.
225 * This is called the first time it is needed, and also called again
226 * every time the configuration is reloaded, because the charset or
227 * codepage might have changed.
229 void init_iconv(void)
233 for (c1=0;c1<NUM_CHARSETS;c1++) {
234 const char *name = charset_name((charset_t)c1);
236 conv_handles[c1][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
237 if (conv_handles[c1][CH_UCS2] == (atalk_iconv_t)-1) {
238 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
239 name, charset_name(CH_UCS2));
240 conv_handles[c1][CH_UCS2] = NULL;
243 if (c1 != CH_UCS2) { /* avoid lost memory, make valgrind happy */
244 conv_handles[CH_UCS2][c1] = atalk_iconv_open( name, charset_name(CH_UCS2));
245 if (conv_handles[CH_UCS2][c1] == (atalk_iconv_t)-1) {
246 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
247 charset_name(CH_UCS2), name);
248 conv_handles[CH_UCS2][c1] = NULL;
252 charsets[c1] = get_charset_functions (c1);
259 static size_t add_null(charset_t to, char *buf, size_t bytesleft, size_t len)
261 /* Terminate the string */
262 if (to == CH_UCS2 && bytesleft >= 2) {
267 else if ( to != CH_UCS2 && bytesleft > 0 )
279 * Convert string from one encoding to another, making error checking etc
281 * @param src pointer to source string (multibyte or singlebyte)
282 * @param srclen length of the source string in bytes
283 * @param dest pointer to destination string (multibyte or singlebyte)
284 * @param destlen maximal length allowed for string
285 * @returns the number of bytes occupied in the destination
287 static size_t convert_string_internal(charset_t from, charset_t to,
288 void const *src, size_t srclen,
289 void *dest, size_t destlen)
293 const char* inbuf = (const char*)src;
294 char* outbuf = (char*)dest;
295 char* o_save = outbuf;
296 atalk_iconv_t descriptor;
298 /* Fixed based on Samba 3.0.6 */
299 if (srclen == (size_t)-1) {
300 if (from == CH_UCS2) {
301 srclen = (strlen_w((const ucs2_t *)src)) * 2;
303 srclen = strlen((const char *)src);
308 lazy_initialize_conv();
310 descriptor = conv_handles[from][to];
312 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
318 retval = atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
319 if(retval==(size_t)-1) {
320 const char *reason="unknown error";
323 reason="Incomplete multibyte sequence";
326 reason="No more room";
329 reason="Illegal multibyte sequence";
332 LOG(log_debug, logtype_default,"Conversion error: %s",reason);
336 /* Terminate the string */
337 return add_null( to, o_save, o_len, destlen -o_len);
341 size_t convert_string(charset_t from, charset_t to,
342 void const *src, size_t srclen,
343 void *dest, size_t destlen)
347 ucs2_t buffer[MAXPATHLEN];
348 ucs2_t buffer2[MAXPATHLEN];
350 /* convert from_set to UCS2 */
351 if ((size_t)-1 == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
352 (char*) buffer, sizeof(buffer))) ) {
353 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
357 /* Do pre/decomposition */
358 i_len = sizeof(buffer2);
360 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
361 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
364 else if (!charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED)) {
365 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
372 /* Convert UCS2 to to_set */
373 if ((size_t)(-1) == ( o_len = convert_string_internal( CH_UCS2, to, (char*) u, i_len, dest, destlen)) ) {
374 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
384 * Convert between character sets, allocating a new buffer for the result.
386 * @param srclen length of source buffer.
387 * @param dest always set at least to NULL
388 * @note -1 is not accepted for srclen.
390 * @returns Size in bytes of the converted string; or -1 in case of error.
393 static size_t convert_string_allocate_internal(charset_t from, charset_t to,
394 void const *src, size_t srclen, char **dest)
396 size_t i_len, o_len, destlen;
398 const char *inbuf = (const char *)src;
399 char *outbuf = NULL, *ob = NULL;
400 atalk_iconv_t descriptor;
404 if (src == NULL || srclen == (size_t)-1)
407 lazy_initialize_conv();
409 descriptor = conv_handles[from][to];
411 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
412 /* conversion not supported, return -1*/
413 LOG(log_debug, logtype_default, "convert_string_allocate: conversion not supported!");
417 destlen = MAX(srclen, 512);
419 destlen = destlen * 2;
420 outbuf = (char *)realloc(ob, destlen);
422 LOG(log_debug, logtype_default,"convert_string_allocate: realloc failed!");
428 inbuf = src; /* this restarts the whole conversion if buffer needed to be increased */
431 retval = atalk_iconv(descriptor,
434 if(retval == (size_t)-1) {
435 const char *reason="unknown error";
438 reason="Incomplete multibyte sequence";
443 reason="Illegal multibyte sequence";
446 LOG(log_debug, logtype_default,"Conversion error: %s(%s)",reason,inbuf);
452 destlen = destlen - o_len;
454 /* Terminate the string */
455 if (to == CH_UCS2 && o_len >= 2) {
458 *dest = (char *)realloc(ob,destlen+2);
460 else if ( to != CH_UCS2 && o_len > 0 ) {
462 *dest = (char *)realloc(ob,destlen+1);
465 goto convert; /* realloc */
468 if (destlen && !*dest) {
469 LOG(log_debug, logtype_default, "convert_string_allocate: out of memory!");
478 size_t convert_string_allocate(charset_t from, charset_t to,
479 void const *src, size_t srclen,
484 ucs2_t buffer[MAXPATHLEN];
485 ucs2_t buffer2[MAXPATHLEN];
489 /* convert from_set to UCS2 */
490 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
491 buffer, sizeof(buffer))) ) {
492 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
496 /* Do pre/decomposition */
497 i_len = sizeof(buffer2);
499 if (charsets[to] && (charsets[to]->flags & CHARSET_DECOMPOSED) ) {
500 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
503 else if ( !charsets[from] || (charsets[from]->flags & CHARSET_DECOMPOSED) ) {
504 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
512 /* Convert UCS2 to to_set */
513 if ((size_t)-1 == ( o_len = convert_string_allocate_internal( CH_UCS2, to, (char*)u, i_len, dest)) )
514 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
520 size_t charset_strupper(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
525 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
527 if (size == (size_t)-1) {
531 if (!strupper_w((ucs2_t *)buffer) && (dest == src)) {
536 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
541 size_t charset_strlower(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
546 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
548 if (size == (size_t)-1) {
552 if (!strlower_w((ucs2_t *)buffer) && (dest == src)) {
557 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
563 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
565 return charset_strupper( CH_UNIX, src, srclen, dest, destlen);
568 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
570 return charset_strlower( CH_UNIX, src, srclen, dest, destlen);
573 size_t utf8_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
575 return charset_strupper( CH_UTF8, src, srclen, dest, destlen);
578 size_t utf8_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
580 return charset_strlower( CH_UTF8, src, srclen, dest, destlen);
584 * Copy a string from a charset_t char* src to a UCS2 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
589 * or -1 in case of error.
592 size_t charset_to_ucs2_allocate(charset_t ch, ucs2_t **dest, const char *src)
594 size_t src_len = strlen(src);
597 return convert_string_allocate(ch, CH_UCS2, src, src_len, (char**) dest);
600 /** -----------------------------------
601 * Copy a string from a charset_t char* src to a UTF-8 destination, allocating a buffer
603 * @param dest always set at least to NULL
605 * @returns The number of bytes occupied by the string in the destination
608 size_t charset_to_utf8_allocate(charset_t ch, char **dest, const char *src)
610 size_t src_len = strlen(src);
613 return convert_string_allocate(ch, CH_UTF8, src, src_len, dest);
616 /** -----------------------------------
617 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
619 * @param dest always set at least to NULL
621 * @returns The number of bytes occupied by the string in the destination
624 size_t ucs2_to_charset(charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
626 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
627 return convert_string(CH_UCS2, ch, src, src_len, dest, destlen);
630 /* --------------------------------- */
631 size_t ucs2_to_charset_allocate(charset_t ch, char **dest, const ucs2_t *src)
633 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
635 return convert_string_allocate(CH_UCS2, ch, src, src_len, dest);
638 /** ---------------------------------
639 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
641 * @param dest always set at least to NULL
643 * @returns The number of bytes occupied by the string in the destination
646 size_t utf8_to_charset_allocate(charset_t ch, char **dest, const char *src)
648 size_t src_len = strlen(src);
650 return convert_string_allocate(CH_UTF8, ch, src, src_len, dest);
653 size_t charset_precompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
656 ucs2_t u[MAXPATHLEN];
660 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
665 if ( (size_t)-1 == (ilen = precompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
670 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
679 size_t charset_decompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
682 ucs2_t u[MAXPATHLEN];
686 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
691 if ( (size_t)-1 == (ilen = decompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
696 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
705 size_t utf8_precompose ( char * src, size_t inlen, char * dst, size_t outlen)
707 return charset_precompose ( CH_UTF8, src, inlen, dst, outlen);
710 size_t utf8_decompose ( char * src, size_t inlen, char * dst, size_t outlen)
712 return charset_decompose ( CH_UTF8, src, inlen, dst, outlen);
716 static char debugbuf[ MAXPATHLEN +1 ];
717 char * debug_out ( char * seq, size_t len)
723 p = (unsigned char*) seq;
726 for ( i = 0; i<=(len-1); i++)
728 sprintf(q, "%2.2x.", *p);
739 * Convert from MB to UCS2 charset
741 * CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
742 * CONV_IGNORE: return the first convertable characters.
743 * CONV_FORCE: force convertion
745 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
746 * The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it
747 * for e.g. HFS cdroms.
750 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)
752 const uint16_t option = (flags ? *flags : 0);
755 const char* inbuf = (const char*)src;
757 atalk_iconv_t descriptor;
758 atalk_iconv_t descriptor_cap;
760 if (srclen == (size_t)-1)
761 srclen = strlen(src) + 1;
763 descriptor = conv_handles[from_set][CH_UCS2];
764 descriptor_cap = conv_handles[cap_set][CH_UCS2];
766 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
775 if ((option & CONV_UNESCAPEHEX)) {
776 for (j = 0; j < i_len; ++j) {
777 if (inbuf[j] == ':') break;
784 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
785 if (errno == EILSEQ || errno == EINVAL) {
787 if ((option & CONV_IGNORE)) {
788 *flags |= CONV_REQMANGLE;
789 return destlen - o_len;
791 if ((option & CONV__EILSEQ)) {
796 *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
801 /* FIXME reset stat ? */
809 /* we're at the start on an hex encoded ucs2 char */
814 while (i_len >= 3 && inbuf[0] == ':' &&
815 isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
816 h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
821 const char *h_buf = h;
822 if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
825 if (errno == EILSEQ && (option & CONV_IGNORE)) {
826 *flags |= CONV_REQMANGLE;
827 return destlen - o_len;
832 /* We have an invalid :xx sequence */
834 if ((option & CONV_IGNORE)) {
835 *flags |= CONV_REQMANGLE;
836 return destlen - o_len;
843 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
847 * Convert from UCS2 to MB charset
849 * CONV_ESCAPEDOTS: escape leading dots
850 * CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
851 * CONV_IGNORE: return the first convertable characters.
852 * CONV__EILSEQ: unconvertable characters will be replaced with '_'
853 * CONV_FORCE: force convertion
855 * CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ?
856 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
857 * The escape scheme is not compatible to the old cap style escape. This is bad, we need it
858 * for e.g. HFS cdroms.
862 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)
864 const uint16_t option = (flags ? *flags : 0);
865 size_t i_len, o_len, i;
867 const char* inbuf = (const char*)src;
868 char* outbuf = (char*)dest;
869 atalk_iconv_t descriptor;
870 atalk_iconv_t descriptor_cap;
871 char escch; /* 150210: uninitialized OK, depends on j */
873 descriptor = conv_handles[CH_UCS2][to_set];
874 descriptor_cap = conv_handles[CH_UCS2][cap_set];
876 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
884 if ((option & CONV_ESCAPEDOTS) &&
885 i_len >= 2 && SVAL(inbuf, 0) == 0x002e) { /* 0x002e = . */
896 *flags |= CONV_REQESCAPE;
900 if ((option & CONV_ESCAPEHEX)) {
901 for (i = 0; i < i_len; i += 2) {
902 ucs2_t c = SVAL(inbuf, i);
904 case 0x003a: /* 0x003a = ':' */
905 if ( ! (option & CONV_ALLOW_COLON)) {
913 case 0x002f: /* 0x002f = '/' */
922 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
923 if (errno == EILSEQ) {
924 if ((option & CONV_IGNORE)) {
925 *flags |= CONV_REQMANGLE;
926 return destlen - o_len;
928 if ((option & CONV_ESCAPEHEX)) {
929 const size_t bufsiz = o_len / 3 + 1;
930 char *buf = malloc(bufsiz);
936 for (buflen = 1; buflen <= bufsiz; ++buflen) {
939 if (atalk_iconv(descriptor_cap, &inbuf, &i, &b, &o) != (size_t)-1) {
942 } else if (errno != E2BIG) {
945 } else if (o < buflen) {
950 if (o_len < buflen * 3) {
957 for (i = 0; i < buflen; ++i) {
959 *outbuf++ = hexdig[(buf[i] >> 4) & 0x0f];
960 *outbuf++ = hexdig[buf[i] & 0x0f];
963 *flags |= CONV_REQESCAPE;
989 * THIS SHOULD NEVER BE REACHED !!!
990 * As a safety net I put in a ' ' here
1002 if (i_len > 0) errno = EINVAL;
1004 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
1008 * FIXME the size is a mess we really need a malloc/free logic
1009 *`dest size must be dest_len +2
1011 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)
1013 size_t i_len, o_len;
1015 ucs2_t buffer[MAXPATHLEN +2];
1016 ucs2_t buffer2[MAXPATHLEN +2];
1018 lazy_initialize_conv();
1020 /* convert from_set to UCS2 */
1021 if ((size_t)(-1) == ( o_len = pull_charset_flags( from_set, cap_charset, src, src_len,
1022 (char *) buffer, sizeof(buffer) -2, flags)) ) {
1023 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from_set));
1030 /* Do pre/decomposition */
1031 i_len = sizeof(buffer2) -2;
1033 if (CHECK_FLAGS(flags, CONV_DECOMPOSE) || (charsets[to_set] && (charsets[to_set]->flags & CHARSET_DECOMPOSED)) ) {
1034 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
1035 return (size_t)(-1);
1037 else if (CHECK_FLAGS(flags, CONV_PRECOMPOSE) || !charsets[from_set] || (charsets[from_set]->flags & CHARSET_DECOMPOSED)) {
1038 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
1039 return (size_t)(-1);
1045 /* null terminate */
1049 /* Do case conversions */
1050 if (CHECK_FLAGS(flags, CONV_TOUPPER)) {
1053 else if (CHECK_FLAGS(flags, CONV_TOLOWER)) {
1057 /* Convert UCS2 to to_set */
1058 if ((size_t)(-1) == ( o_len = push_charset_flags( to_set, cap_charset, (char *)u, i_len, dest, dest_len, flags )) ) {
1059 LOG(log_error, logtype_default,
1060 "Conversion failed (CH_UCS2 to %s):%s", charset_name(to_set), strerror(errno));
1063 /* null terminate */