2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jelmer Vernooij 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../lib/util/dlinklist.h"
23 #include "system/iconv.h"
24 #include "system/filesys.h"
30 * @brief Samba wrapper/stub for iconv character set conversion.
32 * iconv is the XPG2 interface for converting between character
33 * encodings. This file provides a Samba wrapper around it, and also
34 * a simple reimplementation that is used if the system does not
37 * Samba only works with encodings that are supersets of ASCII: ascii
38 * characters like whitespace can be tested for directly, multibyte
39 * sequences start with a byte with the high bit set, and strings are
40 * terminated by a nul byte.
42 * Note that the only function provided by iconv is conversion between
43 * characters. It doesn't directly support operations like
44 * uppercasing or comparison. We have to convert to UTF-16LE and
47 * @sa Samba Developers Guide
50 static size_t ascii_pull (void *,const char **, size_t *, char **, size_t *);
51 static size_t ascii_push (void *,const char **, size_t *, char **, size_t *);
52 static size_t utf8_pull (void *,const char **, size_t *, char **, size_t *);
53 static size_t utf8_push (void *,const char **, size_t *, char **, size_t *);
54 static size_t utf16_munged_pull(void *,const char **, size_t *, char **, size_t *);
55 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
56 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
57 static size_t iconv_copy (void *,const char **, size_t *, char **, size_t *);
58 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
60 static const struct charset_functions builtin_functions
[] = {
61 /* windows is closest to UTF-16 */
62 {"UCS-2LE", iconv_copy
, iconv_copy
},
63 {"UTF-16LE", iconv_copy
, iconv_copy
},
64 {"UCS-2BE", iconv_swab
, iconv_swab
},
65 {"UTF-16BE", iconv_swab
, iconv_swab
},
67 /* we include the UTF-8 alias to cope with differing locale settings */
68 {"UTF8", utf8_pull
, utf8_push
},
69 {"UTF-8", utf8_pull
, utf8_push
},
71 /* this handles the munging needed for String2Key */
72 {"UTF16_MUNGED", utf16_munged_pull
, iconv_copy
},
74 {"ASCII", ascii_pull
, ascii_push
},
75 {"UCS2-HEX", ucs2hex_pull
, ucs2hex_push
}
78 static struct charset_functions
*charsets
= NULL
;
80 bool charset_register_backend(const void *_funcs
)
82 struct charset_functions
*funcs
= (struct charset_functions
*)memdup(_funcs
,sizeof(struct charset_functions
));
83 struct charset_functions
*c
;
85 /* Check whether we already have this charset... */
86 for (c
= charsets
; c
!= NULL
; c
= c
->next
) {
87 if(!strcasecmp(c
->name
, funcs
->name
)) {
88 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs
->name
));
93 funcs
->next
= funcs
->prev
= NULL
;
94 DLIST_ADD(charsets
, funcs
);
98 #ifdef HAVE_NATIVE_ICONV
99 /* if there was an error then reset the internal state,
100 this ensures that we don't have a shift state remaining for
101 character sets like SJIS */
102 static size_t sys_iconv(void *cd
,
103 const char **inbuf
, size_t *inbytesleft
,
104 char **outbuf
, size_t *outbytesleft
)
106 size_t ret
= iconv((iconv_t
)cd
,
107 discard_const_p(char *, inbuf
), inbytesleft
,
108 outbuf
, outbytesleft
);
109 if (ret
== (size_t)-1) iconv(cd
, NULL
, NULL
, NULL
, NULL
);
115 * This is a simple portable iconv() implementaion.
117 * It only knows about a very small number of character sets - just
118 * enough that Samba works on systems that don't have iconv.
120 _PUBLIC_
size_t smb_iconv(smb_iconv_t cd
,
121 const char **inbuf
, size_t *inbytesleft
,
122 char **outbuf
, size_t *outbytesleft
)
127 /* in many cases we can go direct */
129 return cd
->direct(cd
->cd_direct
,
130 inbuf
, inbytesleft
, outbuf
, outbytesleft
);
134 /* otherwise we have to do it chunks at a time */
135 while (*inbytesleft
> 0) {
136 char *bufp1
= cvtbuf
;
137 const char *bufp2
= cvtbuf
;
139 bufsize
= sizeof(cvtbuf
);
141 if (cd
->pull(cd
->cd_pull
,
142 inbuf
, inbytesleft
, &bufp1
, &bufsize
) == -1
143 && errno
!= E2BIG
) return -1;
145 bufsize
= sizeof(cvtbuf
) - bufsize
;
147 if (cd
->push(cd
->cd_push
,
149 outbuf
, outbytesleft
) == -1) return -1;
155 static bool is_utf16(const char *name
)
157 return strcasecmp(name
, "UCS-2LE") == 0 ||
158 strcasecmp(name
, "UTF-16LE") == 0;
161 int smb_iconv_t_destructor(smb_iconv_t hwd
)
163 #ifdef HAVE_NATIVE_ICONV
164 if (hwd
->cd_pull
!= NULL
&& hwd
->cd_pull
!= (iconv_t
)-1)
165 iconv_close(hwd
->cd_pull
);
166 if (hwd
->cd_push
!= NULL
&& hwd
->cd_push
!= (iconv_t
)-1)
167 iconv_close(hwd
->cd_push
);
168 if (hwd
->cd_direct
!= NULL
&& hwd
->cd_direct
!= (iconv_t
)-1)
169 iconv_close(hwd
->cd_direct
);
175 _PUBLIC_ smb_iconv_t
smb_iconv_open_ex(TALLOC_CTX
*mem_ctx
, const char *tocode
,
176 const char *fromcode
, bool native_iconv
)
179 const struct charset_functions
*from
=NULL
, *to
=NULL
;
182 ret
= (smb_iconv_t
)talloc_named(mem_ctx
,
184 "iconv(%s,%s)", tocode
, fromcode
);
187 return (smb_iconv_t
)-1;
189 memset(ret
, 0, sizeof(*ret
));
190 talloc_set_destructor(ret
, smb_iconv_t_destructor
);
192 /* check for the simplest null conversion */
193 if (strcmp(fromcode
, tocode
) == 0) {
194 ret
->direct
= iconv_copy
;
198 for (i
=0;i
<ARRAY_SIZE(builtin_functions
);i
++) {
199 if (strcasecmp(fromcode
, builtin_functions
[i
].name
) == 0) {
200 from
= &builtin_functions
[i
];
202 if (strcasecmp(tocode
, builtin_functions
[i
].name
) == 0) {
203 to
= &builtin_functions
[i
];
208 for (from
=charsets
; from
; from
=from
->next
) {
209 if (strcasecmp(from
->name
, fromcode
) == 0) break;
214 for (to
=charsets
; to
; to
=to
->next
) {
215 if (strcasecmp(to
->name
, tocode
) == 0) break;
219 #ifdef HAVE_NATIVE_ICONV
220 if ((!from
|| !to
) && !native_iconv
) {
224 ret
->pull
= sys_iconv
;
225 ret
->cd_pull
= iconv_open("UTF-16LE", fromcode
);
226 if (ret
->cd_pull
== (iconv_t
)-1)
227 ret
->cd_pull
= iconv_open("UCS-2LE", fromcode
);
228 if (ret
->cd_pull
== (iconv_t
)-1) goto failed
;
232 ret
->push
= sys_iconv
;
233 ret
->cd_push
= iconv_open(tocode
, "UTF-16LE");
234 if (ret
->cd_push
== (iconv_t
)-1)
235 ret
->cd_push
= iconv_open(tocode
, "UCS-2LE");
236 if (ret
->cd_push
== (iconv_t
)-1) goto failed
;
244 /* check for conversion to/from ucs2 */
245 if (is_utf16(fromcode
) && to
) {
246 ret
->direct
= to
->push
;
249 if (is_utf16(tocode
) && from
) {
250 ret
->direct
= from
->pull
;
254 #ifdef HAVE_NATIVE_ICONV
255 if (is_utf16(fromcode
)) {
256 ret
->direct
= sys_iconv
;
257 ret
->cd_direct
= ret
->cd_push
;
261 if (is_utf16(tocode
)) {
262 ret
->direct
= sys_iconv
;
263 /* could be set just above - so we need to close iconv */
264 if (ret
->cd_direct
!= NULL
&& ret
->cd_direct
!= (iconv_t
)-1)
265 iconv_close(ret
->cd_direct
);
266 ret
->cd_direct
= ret
->cd_pull
;
272 /* the general case has to go via a buffer */
273 if (!ret
->pull
) ret
->pull
= from
->pull
;
274 if (!ret
->push
) ret
->push
= to
->push
;
280 return (smb_iconv_t
)-1;
284 simple iconv_open() wrapper
286 _PUBLIC_ smb_iconv_t
smb_iconv_open(const char *tocode
, const char *fromcode
)
288 return smb_iconv_open_ex(talloc_autofree_context(), tocode
, fromcode
, true);
292 simple iconv_close() wrapper
294 _PUBLIC_
int smb_iconv_close(smb_iconv_t cd
)
301 /**********************************************************************
302 the following functions implement the builtin character sets in Samba
303 and also the "test" character sets that are designed to test
304 multi-byte character set support for english users
305 ***********************************************************************/
306 static size_t ascii_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
307 char **outbuf
, size_t *outbytesleft
)
309 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
310 (*outbuf
)[0] = (*inbuf
)[0];
313 (*outbytesleft
) -= 2;
318 if (*inbytesleft
> 0) {
326 static size_t ascii_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
327 char **outbuf
, size_t *outbytesleft
)
331 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
332 (*outbuf
)[0] = (*inbuf
)[0] & 0x7F;
333 if ((*inbuf
)[1]) ir_count
++;
335 (*outbytesleft
) -= 1;
340 if (*inbytesleft
== 1) {
345 if (*inbytesleft
> 1) {
354 static size_t ucs2hex_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
355 char **outbuf
, size_t *outbytesleft
)
357 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
360 if ((*inbuf
)[0] != '@') {
361 /* seven bit ascii case */
362 (*outbuf
)[0] = (*inbuf
)[0];
365 (*outbytesleft
) -= 2;
370 /* it's a hex character */
371 if (*inbytesleft
< 5) {
376 if (sscanf(&(*inbuf
)[1], "%04x", &v
) != 1) {
381 (*outbuf
)[0] = v
&0xff;
384 (*outbytesleft
) -= 2;
389 if (*inbytesleft
> 0) {
397 static size_t ucs2hex_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
398 char **outbuf
, size_t *outbytesleft
)
400 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
403 if ((*inbuf
)[1] == 0 &&
404 ((*inbuf
)[0] & 0x80) == 0 &&
405 (*inbuf
)[0] != '@') {
406 (*outbuf
)[0] = (*inbuf
)[0];
408 (*outbytesleft
) -= 1;
413 if (*outbytesleft
< 5) {
417 snprintf(buf
, 6, "@%04x", SVAL(*inbuf
, 0));
418 memcpy(*outbuf
, buf
, 5);
420 (*outbytesleft
) -= 5;
425 if (*inbytesleft
== 1) {
430 if (*inbytesleft
> 1) {
438 static size_t iconv_swab(void *cd
, const char **inbuf
, size_t *inbytesleft
,
439 char **outbuf
, size_t *outbytesleft
)
443 n
= MIN(*inbytesleft
, *outbytesleft
);
445 swab(*inbuf
, *outbuf
, (n
&~1));
451 (*outbytesleft
) -= n
;
455 if (*inbytesleft
> 0) {
464 static size_t iconv_copy(void *cd
, const char **inbuf
, size_t *inbytesleft
,
465 char **outbuf
, size_t *outbytesleft
)
469 n
= MIN(*inbytesleft
, *outbytesleft
);
471 memmove(*outbuf
, *inbuf
, n
);
474 (*outbytesleft
) -= n
;
478 if (*inbytesleft
> 0) {
487 this takes a UTF8 sequence and produces a UTF16 sequence
489 static size_t utf8_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
490 char **outbuf
, size_t *outbytesleft
)
492 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
493 const uint8_t *c
= (const uint8_t *)*inbuf
;
494 uint8_t *uc
= (uint8_t *)*outbuf
;
496 while (in_left
>= 1 && out_left
>= 2) {
497 if ((c
[0] & 0x80) == 0) {
507 if ((c
[0] & 0xe0) == 0xc0) {
509 (c
[1] & 0xc0) != 0x80) {
513 uc
[1] = (c
[0]>>2) & 0x7;
514 uc
[0] = (c
[0]<<6) | (c
[1]&0x3f);
522 if ((c
[0] & 0xf0) == 0xe0) {
524 (c
[1] & 0xc0) != 0x80 ||
525 (c
[2] & 0xc0) != 0x80) {
529 uc
[1] = ((c
[0]&0xF)<<4) | ((c
[1]>>2)&0xF);
530 uc
[0] = (c
[1]<<6) | (c
[2]&0x3f);
538 if ((c
[0] & 0xf8) == 0xf0) {
539 unsigned int codepoint
;
541 (c
[1] & 0xc0) != 0x80 ||
542 (c
[2] & 0xc0) != 0x80 ||
543 (c
[3] & 0xc0) != 0x80) {
552 if (codepoint
< 0x10000) {
553 /* accept UTF-8 characters that are not
554 minimally packed, but pack the result */
555 uc
[0] = (codepoint
& 0xFF);
556 uc
[1] = (codepoint
>> 8);
564 codepoint
-= 0x10000;
571 uc
[0] = (codepoint
>>10) & 0xFF;
572 uc
[1] = (codepoint
>>18) | 0xd8;
573 uc
[2] = codepoint
& 0xFF;
574 uc
[3] = ((codepoint
>>8) & 0x3) | 0xdc;
582 /* we don't handle 5 byte sequences */
592 *inbytesleft
= in_left
;
593 *outbytesleft
= out_left
;
594 *inbuf
= (const char *)c
;
595 *outbuf
= (char *)uc
;
599 *inbytesleft
= in_left
;
600 *outbytesleft
= out_left
;
601 *inbuf
= (const char *)c
;
602 *outbuf
= (char *)uc
;
608 this takes a UTF16 sequence and produces a UTF8 sequence
610 static size_t utf8_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
611 char **outbuf
, size_t *outbytesleft
)
613 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
614 uint8_t *c
= (uint8_t *)*outbuf
;
615 const uint8_t *uc
= (const uint8_t *)*inbuf
;
617 while (in_left
>= 2 && out_left
>= 1) {
618 unsigned int codepoint
;
620 if (uc
[1] == 0 && !(uc
[0] & 0x80)) {
630 if ((uc
[1]&0xf8) == 0) {
631 /* next simplest case */
636 c
[0] = 0xc0 | (uc
[0]>>6) | (uc
[1]<<2);
637 c
[1] = 0x80 | (uc
[0] & 0x3f);
645 if ((uc
[1] & 0xfc) == 0xdc) {
646 /* its the second part of a 4 byte sequence. Illegal */
655 if ((uc
[1] & 0xfc) != 0xd8) {
656 codepoint
= uc
[0] | (uc
[1]<<8);
661 c
[0] = 0xe0 | (codepoint
>> 12);
662 c
[1] = 0x80 | ((codepoint
>> 6) & 0x3f);
663 c
[2] = 0x80 | (codepoint
& 0x3f);
672 /* its the first part of a 4 byte sequence */
677 if ((uc
[3] & 0xfc) != 0xdc) {
681 codepoint
= 0x10000 + (uc
[2] | ((uc
[3] & 0x3)<<8) |
682 (uc
[0]<<10) | ((uc
[1] & 0x3)<<18));
688 c
[0] = 0xf0 | (codepoint
>> 18);
689 c
[1] = 0x80 | ((codepoint
>> 12) & 0x3f);
690 c
[2] = 0x80 | ((codepoint
>> 6) & 0x3f);
691 c
[3] = 0x80 | (codepoint
& 0x3f);
709 *inbytesleft
= in_left
;
710 *outbytesleft
= out_left
;
711 *inbuf
= (const char *)uc
;
717 *inbytesleft
= in_left
;
718 *outbytesleft
= out_left
;
719 *inbuf
= (const char *)uc
;
726 this takes a UTF16 munged sequence, modifies it according to the
727 string2key rules, and produces a UTF16 sequence
731 1) any 0x0000 characters are mapped to 0x0001
733 2) convert any instance of 0xD800 - 0xDBFF (high surrogate)
734 without an immediately following 0xDC00 - 0x0xDFFF (low surrogate) to
735 U+FFFD (OBJECT REPLACEMENT CHARACTER).
737 3) the same for any low surrogate that was not preceded by a high surrogate.
740 static size_t utf16_munged_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
741 char **outbuf
, size_t *outbytesleft
)
743 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
744 uint8_t *c
= (uint8_t *)*outbuf
;
745 const uint8_t *uc
= (const uint8_t *)*inbuf
;
747 while (in_left
>= 2 && out_left
>= 2) {
748 unsigned int codepoint
= uc
[0] | (uc
[1]<<8);
750 if (codepoint
== 0) {
754 if ((codepoint
& 0xfc00) == 0xd800) {
755 /* a high surrogate */
756 unsigned int codepoint2
;
761 codepoint2
= uc
[2] | (uc
[3]<<8);
762 if ((codepoint2
& 0xfc00) != 0xdc00) {
763 /* high surrogate not followed by low
764 surrogate: convert to 0xfffd */
780 if ((codepoint
& 0xfc00) == 0xdc00) {
781 /* low surrogate not preceded by high
782 surrogate: convert to 0xfffd */
787 c
[0] = codepoint
& 0xFF;
788 c
[1] = (codepoint
>>8) & 0xFF;
807 *inbytesleft
= in_left
;
808 *outbytesleft
= out_left
;
809 *inbuf
= (const char *)uc
;
815 *inbytesleft
= in_left
;
816 *outbytesleft
= out_left
;
817 *inbuf
= (const char *)uc
;