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
46 #include <netatalk/endian.h>
47 #include <atalk/logger.h>
48 #include <atalk/unicode.h>
49 #include <atalk/util.h>
50 #include "byteorder.h"
56 * @brief Character-set conversion routines built on our iconv.
58 * @note Samba's internal character set (at least in the 3.0 series)
59 * is always the same as the one for the Unix filesystem. It is
60 * <b>not</b> necessarily UTF-8 and may be different on machines that
61 * need i18n filenames to be compatible with Unix software. It does
62 * have to be a superset of ASCII. All multibyte sequences must start
63 * with a byte with the high bit set.
69 #define MAX_CHARSETS 20
71 #define CHECK_FLAGS(a,b) (((a)!=NULL) ? (*(a) & (b)) : 0 )
73 static atalk_iconv_t conv_handles[MAX_CHARSETS][MAX_CHARSETS];
74 static char* charset_names[MAX_CHARSETS];
75 static struct charset_functions* charsets[MAX_CHARSETS];
76 static char hexdig[] = "0123456789abcdef";
77 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
79 static char* read_charsets_from_env(charset_t ch)
85 if (( name = getenv( "ATALK_MAC_CHARSET" )) != NULL )
91 if (( name = getenv( "ATALK_UNIX_CHARSET" )) != NULL )
104 * Return the name of a charset to give to iconv().
106 static const char *charset_name(charset_t ch)
108 const char *ret = NULL;
109 static int first = 1;
110 static char macname[128];
111 static char unixname[128];
114 memset(macname, 0, sizeof(macname));
115 memset(unixname, 0, sizeof(unixname));
119 if (ch == CH_UCS2) ret = "UCS-2";
120 else if (ch == CH_UTF8) ret = "UTF8";
121 else if (ch == CH_UTF8_MAC) ret = "UTF8-MAC";
122 else if (ch == CH_UNIX) {
123 if (unixname[0] == '\0') {
124 ret = read_charsets_from_env(CH_UNIX);
125 strlcpy(unixname, ret, sizeof(unixname));
130 else if (ch == CH_MAC) {
131 if (macname[0] == '\0') {
132 ret = read_charsets_from_env(CH_MAC);
133 strlcpy(macname, ret, sizeof(macname));
140 ret = charset_names[ch];
142 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
143 if (ret && strcasecmp(ret, "LOCALE") == 0) {
144 const char *ln = NULL;
146 #ifdef HAVE_SETLOCALE
147 setlocale(LC_ALL, "");
149 ln = nl_langinfo(CODESET);
151 /* Check whether the charset name is supported
153 atalk_iconv_t handle = atalk_iconv_open(ln, "UCS-2");
154 if (handle == (atalk_iconv_t) -1) {
155 LOG(log_debug, logtype_default, "Locale charset '%s' unsupported, using ASCII instead", ln);
158 atalk_iconv_close(handle);
161 strlcpy(unixname, ln, sizeof(unixname));
165 #else /* system doesn't have LOCALE support */
166 if (ch == CH_UNIX) ret = NULL;
169 if (!ret || !*ret) ret = "ASCII";
173 static struct charset_functions* get_charset_functions (charset_t ch)
175 if (charsets[ch] != NULL)
178 charsets[ch] = find_charset_functions(charset_name(ch));
184 static void lazy_initialize_conv(void)
186 static int initialized = 0;
194 charset_t add_charset(char* name)
196 static charset_t max_charset_t = NUM_CHARSETS-1;
197 charset_t cur_charset_t = max_charset_t+1;
200 lazy_initialize_conv();
202 for (c1=0; c1<=max_charset_t;c1++) {
203 if ( strcasecmp(name, charset_name(c1)) == 0)
207 if ( cur_charset_t >= MAX_CHARSETS ) {
208 LOG (log_debug, logtype_default, "Adding charset %s failed, too many charsets (max. %u allowed)",
210 return (charset_t) -1;
213 /* First try to setup the required conversions */
215 conv_handles[cur_charset_t][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
216 if (conv_handles[cur_charset_t][CH_UCS2] == (atalk_iconv_t)-1) {
217 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
218 name, charset_name(CH_UCS2));
219 conv_handles[cur_charset_t][CH_UCS2] = NULL;
220 return (charset_t) -1;
223 conv_handles[CH_UCS2][cur_charset_t] = atalk_iconv_open( name, charset_name(CH_UCS2));
224 if (conv_handles[CH_UCS2][cur_charset_t] == (atalk_iconv_t)-1) {
225 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
226 charset_name(CH_UCS2), name);
227 conv_handles[CH_UCS2][cur_charset_t] = NULL;
228 return (charset_t) -1;
231 /* register the new charset_t name */
232 charset_names[cur_charset_t] = strdup(name);
234 charsets[cur_charset_t] = get_charset_functions (cur_charset_t);
238 LOG(log_debug9, logtype_default, "Added charset %s with handle %u", name, cur_charset_t);
240 return (cur_charset_t);
244 * Initialize iconv conversion descriptors.
246 * This is called the first time it is needed, and also called again
247 * every time the configuration is reloaded, because the charset or
248 * codepage might have changed.
250 void init_iconv(void)
254 for (c1=0;c1<NUM_CHARSETS;c1++) {
255 const char *name = charset_name((charset_t)c1);
257 conv_handles[c1][CH_UCS2] = atalk_iconv_open( charset_name(CH_UCS2), name);
258 if (conv_handles[c1][CH_UCS2] == (atalk_iconv_t)-1) {
259 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
260 name, charset_name(CH_UCS2));
261 conv_handles[c1][CH_UCS2] = NULL;
264 if (c1 != CH_UCS2) { /* avoid lost memory, make valgrind happy */
265 conv_handles[CH_UCS2][c1] = atalk_iconv_open( name, charset_name(CH_UCS2));
266 if (conv_handles[CH_UCS2][c1] == (atalk_iconv_t)-1) {
267 LOG(log_error, logtype_default, "Required conversion from %s to %s not supported",
268 charset_name(CH_UCS2), name);
269 conv_handles[CH_UCS2][c1] = NULL;
273 charsets[c1] = get_charset_functions (c1);
280 static size_t add_null(charset_t to, char *buf, size_t bytesleft, size_t len)
282 /* Terminate the string */
283 if (to == CH_UCS2 && bytesleft >= 2) {
288 else if ( to != CH_UCS2 && bytesleft > 0 )
300 * Convert string from one encoding to another, making error checking etc
302 * @param src pointer to source string (multibyte or singlebyte)
303 * @param srclen length of the source string in bytes
304 * @param dest pointer to destination string (multibyte or singlebyte)
305 * @param destlen maximal length allowed for string
306 * @returns the number of bytes occupied in the destination
308 static size_t convert_string_internal(charset_t from, charset_t to,
309 void const *src, size_t srclen,
310 void *dest, size_t destlen)
314 const char* inbuf = (const char*)src;
315 char* outbuf = (char*)dest;
316 char* o_save = outbuf;
317 atalk_iconv_t descriptor;
319 /* Fixed based on Samba 3.0.6 */
320 if (srclen == (size_t)-1) {
321 if (from == CH_UCS2) {
322 srclen = (strlen_w((const ucs2_t *)src)) * 2;
324 srclen = strlen((const char *)src);
329 lazy_initialize_conv();
331 descriptor = conv_handles[from][to];
333 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
339 retval = atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
340 if(retval==(size_t)-1) {
341 const char *reason="unknown error";
344 reason="Incomplete multibyte sequence";
347 reason="No more room";
350 reason="Illegal multibyte sequence";
353 LOG(log_debug, logtype_default,"Conversion error: %s",reason);
357 /* Terminate the string */
358 return add_null( to, o_save, o_len, destlen -o_len);
362 size_t convert_string(charset_t from, charset_t to,
363 void const *src, size_t srclen,
364 void *dest, size_t destlen)
368 ucs2_t buffer[MAXPATHLEN];
369 ucs2_t buffer2[MAXPATHLEN];
372 /* convert from_set to UCS2 */
373 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
374 (char*) buffer, sizeof(buffer))) ) {
375 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
379 /* Do pre/decomposition */
380 if ( ((!(charsets[to]) || !(charsets[to]->flags & CHARSET_DECOMPOSED)) &&
381 (!(charsets[from]) || (charsets[from]->flags & CHARSET_DECOMPOSED))))
383 if ((charsets[to] && charsets[to]->flags & CHARSET_DECOMPOSED) )
386 i_len = sizeof(buffer2);
389 switch (composition) {
395 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
399 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
404 /* Convert UCS2 to to_set */
405 if ((size_t)(-1) == ( o_len = convert_string_internal( CH_UCS2, to, (char*) u, i_len, dest, destlen)) ) {
406 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
416 * Convert between character sets, allocating a new buffer for the result.
418 * @param srclen length of source buffer.
419 * @param dest always set at least to NULL
420 * @note -1 is not accepted for srclen.
422 * @returns Size in bytes of the converted string; or -1 in case of error.
425 static size_t convert_string_allocate_internal(charset_t from, charset_t to,
426 void const *src, size_t srclen, char **dest)
428 size_t i_len, o_len, destlen;
430 const char *inbuf = (const char *)src;
431 char *outbuf = NULL, *ob = NULL;
432 atalk_iconv_t descriptor;
436 if (src == NULL || srclen == (size_t)-1)
439 lazy_initialize_conv();
441 descriptor = conv_handles[from][to];
443 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
444 /* conversion not supported, return -1*/
445 LOG(log_debug, logtype_default, "convert_string_allocate: conversion not supported!");
449 destlen = MAX(srclen, 512);
451 destlen = destlen * 2;
452 outbuf = (char *)realloc(ob, destlen);
454 LOG(log_debug, logtype_default,"convert_string_allocate: realloc failed!");
460 inbuf = src; /* this restarts the whole conversion if buffer needed to be increased */
463 retval = atalk_iconv(descriptor,
466 if(retval == (size_t)-1) {
467 const char *reason="unknown error";
470 reason="Incomplete multibyte sequence";
475 reason="Illegal multibyte sequence";
478 LOG(log_debug, logtype_default,"Conversion error: %s(%s)",reason,inbuf);
484 destlen = destlen - o_len;
486 /* Terminate the string */
487 if (to == CH_UCS2 && o_len >= 2) {
490 *dest = (char *)realloc(ob,destlen+2);
492 else if ( to != CH_UCS2 && o_len > 0 ) {
494 *dest = (char *)realloc(ob,destlen+1);
497 goto convert; /* realloc */
500 if (destlen && !*dest) {
501 LOG(log_debug, logtype_default, "convert_string_allocate: out of memory!");
510 size_t convert_string_allocate(charset_t from, charset_t to,
511 void const *src, size_t srclen,
516 ucs2_t buffer[MAXPATHLEN];
517 ucs2_t buffer2[MAXPATHLEN];
522 /* convert from_set to UCS2 */
523 if ((size_t)(-1) == ( o_len = convert_string_internal( from, CH_UCS2, src, srclen,
524 buffer, sizeof(buffer))) ) {
525 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from));
529 /* Do pre/decomposition */
530 if ( ((!(charsets[to]) || !(charsets[to]->flags & CHARSET_DECOMPOSED)) &&
531 (!(charsets[from]) || (charsets[from]->flags & CHARSET_DECOMPOSED))))
533 if ((charsets[to] && charsets[to]->flags & CHARSET_DECOMPOSED) )
536 i_len = sizeof(buffer2);
539 switch (composition) {
545 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
549 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
554 /* Convert UCS2 to to_set */
555 if ((size_t)(-1) == ( o_len = convert_string_allocate_internal( CH_UCS2, to, (char*)u, i_len, dest)) )
556 LOG(log_error, logtype_default, "Conversion failed (CH_UCS2 to %s):%s", charset_name(to), strerror(errno));
562 size_t charset_strupper(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
567 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
569 if (size == (size_t)-1) {
573 if (!strupper_w((ucs2_t *)buffer) && (dest == src)) {
578 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
583 size_t charset_strlower(charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
588 size = convert_string_allocate_internal(ch, CH_UCS2, src, srclen,
590 if (size == (size_t)-1) {
594 if (!strlower_w((ucs2_t *)buffer) && (dest == src)) {
599 size = convert_string_internal(CH_UCS2, ch, buffer, size, dest, destlen);
605 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
607 return charset_strupper( CH_UNIX, src, srclen, dest, destlen);
610 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
612 return charset_strlower( CH_UNIX, src, srclen, dest, destlen);
615 size_t utf8_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
617 return charset_strupper( CH_UTF8, src, srclen, dest, destlen);
620 size_t utf8_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
622 return charset_strlower( CH_UTF8, src, srclen, dest, destlen);
626 * Copy a string from a charset_t char* src to a UCS2 destination, allocating a buffer
628 * @param dest always set at least to NULL
630 * @returns The number of bytes occupied by the string in the destination
631 * or -1 in case of error.
634 size_t charset_to_ucs2_allocate(charset_t ch, ucs2_t **dest, const char *src)
636 size_t src_len = strlen(src);
639 return convert_string_allocate(ch, CH_UCS2, src, src_len, (char**) dest);
642 /** -----------------------------------
643 * Copy a string from a charset_t char* src to a UTF-8 destination, allocating a buffer
645 * @param dest always set at least to NULL
647 * @returns The number of bytes occupied by the string in the destination
650 size_t charset_to_utf8_allocate(charset_t ch, char **dest, const char *src)
652 size_t src_len = strlen(src);
655 return convert_string_allocate(ch, CH_UTF8, src, src_len, dest);
658 /** -----------------------------------
659 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
661 * @param dest always set at least to NULL
663 * @returns The number of bytes occupied by the string in the destination
666 size_t ucs2_to_charset(charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
668 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
669 return convert_string(CH_UCS2, ch, src, src_len, dest, destlen);
672 /* --------------------------------- */
673 size_t ucs2_to_charset_allocate(charset_t ch, char **dest, const ucs2_t *src)
675 size_t src_len = (strlen_w(src)) * sizeof(ucs2_t);
677 return convert_string_allocate(CH_UCS2, ch, src, src_len, dest);
680 /** ---------------------------------
681 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
683 * @param dest always set at least to NULL
685 * @returns The number of bytes occupied by the string in the destination
688 size_t utf8_to_charset_allocate(charset_t ch, char **dest, const char *src)
690 size_t src_len = strlen(src);
692 return convert_string_allocate(CH_UTF8, ch, src, src_len, dest);
695 size_t charset_precompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
698 ucs2_t u[MAXPATHLEN];
702 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
707 if ( (size_t)-1 == (ilen = precompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
712 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
721 size_t charset_decompose ( charset_t ch, char * src, size_t inlen, char * dst, size_t outlen)
724 ucs2_t u[MAXPATHLEN];
728 if ((size_t)(-1) == (len = convert_string_allocate_internal(ch, CH_UCS2, src, inlen, &buffer)) )
733 if ( (size_t)-1 == (ilen = decompose_w((ucs2_t *)buffer, len, u, &ilen)) ) {
738 if ((size_t)(-1) == (len = convert_string_internal( CH_UCS2, ch, (char*)u, ilen, dst, outlen)) ) {
747 size_t utf8_precompose ( char * src, size_t inlen, char * dst, size_t outlen)
749 return charset_precompose ( CH_UTF8, src, inlen, dst, outlen);
752 size_t utf8_decompose ( char * src, size_t inlen, char * dst, size_t outlen)
754 return charset_decompose ( CH_UTF8, src, inlen, dst, outlen);
758 static char debugbuf[ MAXPATHLEN +1 ];
759 char * debug_out ( char * seq, size_t len)
765 p = (unsigned char*) seq;
768 for ( i = 0; i<=(len-1); i++)
770 sprintf(q, "%2.2x.", *p);
781 * Convert from MB to UCS2 charset
783 * CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
784 * CONV_IGNORE: return the first convertable characters.
785 * CONV_FORCE: force convertion
787 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
788 * The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it
789 * for e.g. HFS cdroms.
792 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, u_int16_t *flags)
794 const u_int16_t option = (flags ? *flags : 0);
797 const char* inbuf = (const char*)src;
799 atalk_iconv_t descriptor;
800 atalk_iconv_t descriptor_cap;
802 if (srclen == (size_t)-1)
803 srclen = strlen(src) + 1;
805 descriptor = conv_handles[from_set][CH_UCS2];
806 descriptor_cap = conv_handles[cap_set][CH_UCS2];
808 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
817 if ((option & CONV_UNESCAPEHEX)) {
818 for (j = 0; j < i_len; ++j) {
819 if (inbuf[j] == ':') break;
826 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
827 if (errno == EILSEQ || errno == EINVAL) {
829 if ((option & CONV_IGNORE)) {
830 *flags |= CONV_REQMANGLE;
831 return destlen - o_len;
833 if ((option & CONV__EILSEQ)) {
838 *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
843 /* FIXME reset stat ? */
851 /* we're at the start on an hex encoded ucs2 char */
856 while (i_len >= 3 && inbuf[0] == ':' &&
857 isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
858 h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
863 const char *h_buf = h;
864 if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
867 if (errno == EILSEQ && (option & CONV_IGNORE)) {
868 *flags |= CONV_REQMANGLE;
869 return destlen - o_len;
874 /* We have an invalid :xx sequence */
876 if ((option & CONV_IGNORE)) {
877 *flags |= CONV_REQMANGLE;
878 return destlen - o_len;
885 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
889 * Convert from UCS2 to MB charset
891 * CONV_ESCAPEDOTS: escape leading dots
892 * CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
893 * CONV_IGNORE: return the first convertable characters.
894 * CONV__EILSEQ: unconvertable characters will be replaced with '_'
895 * CONV_FORCE: force convertion
897 * CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ?
898 * This will *not* work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail
899 * The escape scheme is not compatible to the old cap style escape. This is bad, we need it
900 * for e.g. HFS cdroms.
904 static size_t push_charset_flags (charset_t to_set, charset_t cap_set, char* src, size_t srclen, char* dest, size_t destlen, u_int16_t *flags)
906 const u_int16_t option = (flags ? *flags : 0);
907 size_t i_len, o_len, i;
909 const char* inbuf = (const char*)src;
910 char* outbuf = (char*)dest;
911 atalk_iconv_t descriptor;
912 atalk_iconv_t descriptor_cap;
914 descriptor = conv_handles[CH_UCS2][to_set];
915 descriptor_cap = conv_handles[CH_UCS2][cap_set];
917 if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
925 if ((option & CONV_ESCAPEDOTS) &&
926 i_len >= 2 && SVAL(inbuf, 0) == 0x002e) { /* 0x002e = . */
937 *flags |= CONV_REQESCAPE;
941 if ((option & CONV_ESCAPEHEX)) {
942 for (i = 0; i < i_len; i += 2) {
943 ucs2_t c = SVAL(inbuf, i);
944 if (c == 0x002f) { /* 0x002f = / */
948 } else if (c == 0x003a) { /* 0x003a = : */
955 atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
956 if (errno == EILSEQ) {
957 if ((option & CONV_IGNORE)) {
958 *flags |= CONV_REQMANGLE;
959 return destlen - o_len;
961 if ((option & CONV_ESCAPEHEX)) {
962 const size_t bufsiz = o_len / 3 + 1;
963 char *buf = malloc(bufsiz);
969 for (buflen = 1; buflen <= bufsiz; ++buflen) {
972 if (atalk_iconv(descriptor_cap, &inbuf, &i, &b, &o) != (size_t)-1) {
975 } else if (errno != E2BIG) {
978 } else if (o < buflen) {
983 if (o_len < buflen * 3) {
990 for (i = 0; i < buflen; ++i) {
992 *outbuf++ = hexdig[(buf[i] >> 4) & 0x0f];
993 *outbuf++ = hexdig[buf[i] & 0x0f];
996 *flags |= CONV_REQESCAPE;
1017 if (i_len > 0) errno = EINVAL;
1019 return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
1023 * FIXME the size is a mess we really need a malloc/free logic
1024 *`dest size must be dest_len +2
1026 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, u_int16_t *flags)
1028 size_t i_len, o_len;
1030 ucs2_t buffer[MAXPATHLEN +2];
1031 ucs2_t buffer2[MAXPATHLEN +2];
1032 int composition = 0;
1034 lazy_initialize_conv();
1036 /* convert from_set to UCS2 */
1037 if ((size_t)(-1) == ( o_len = pull_charset_flags( from_set, cap_charset, src, src_len,
1038 (char *) buffer, sizeof(buffer) -2, flags)) ) {
1039 LOG(log_error, logtype_default, "Conversion failed ( %s to CH_UCS2 )", charset_name(from_set));
1046 /* Do pre/decomposition */
1047 if (CHECK_FLAGS(flags, CONV_PRECOMPOSE) ||
1048 ((!(charsets[to_set]) || !(charsets[to_set]->flags & CHARSET_DECOMPOSED)) &&
1049 (!(charsets[from_set]) || (charsets[from_set]->flags & CHARSET_DECOMPOSED))))
1051 if (CHECK_FLAGS(flags, CONV_DECOMPOSE) || (charsets[to_set] && charsets[to_set]->flags & CHARSET_DECOMPOSED) )
1054 i_len = sizeof(buffer2) -2;
1057 switch (composition) {
1063 if ( (size_t)-1 == (i_len = precompose_w(buffer, o_len, u, &i_len)) )
1064 return (size_t)(-1);
1067 if ( (size_t)-1 == (i_len = decompose_w(buffer, o_len, u, &i_len)) )
1068 return (size_t)(-1);
1071 /* null terminate */
1075 /* Do case conversions */
1076 if (CHECK_FLAGS(flags, CONV_TOUPPER)) {
1079 if (CHECK_FLAGS(flags, CONV_TOLOWER)) {
1083 /* Convert UCS2 to to_set */
1084 if ((size_t)(-1) == ( o_len = push_charset_flags( to_set, cap_charset, (char *)u, i_len, dest, dest_len, flags )) ) {
1085 LOG(log_error, logtype_default,
1086 "Conversion failed (CH_UCS2 to %s):%s", charset_name(to_set), strerror(errno));
1089 /* null terminate */