2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jelmer Vernooij 2002,2003
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/>.
24 * We have to use strcasecmp here as the character conversions
25 * haven't been initialised yet. JRA.
33 * @brief Samba wrapper/stub for iconv character set conversion.
35 * iconv is the XPG2 interface for converting between character
36 * encodings. This file provides a Samba wrapper around it, and also
37 * a simple reimplementation that is used if the system does not
40 * Samba only works with encodings that are supersets of ASCII: ascii
41 * characters like whitespace can be tested for directly, multibyte
42 * sequences start with a byte with the high bit set, and strings are
43 * terminated by a nul byte.
45 * Note that the only function provided by iconv is conversion between
46 * characters. It doesn't directly support operations like
47 * uppercasing or comparison. We have to convert to UCS-2 and compare
50 * @sa Samba Developers Guide
55 static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
56 static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
57 static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
58 static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
59 static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
60 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
61 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
62 static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
63 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
65 static struct charset_functions builtin_functions
[] = {
66 /* windows is really neither UCS-2 not UTF-16 */
67 {"UCS-2LE", iconv_copy
, iconv_copy
},
68 {"UTF-16LE", iconv_copy
, iconv_copy
},
69 {"UCS-2BE", iconv_swab
, iconv_swab
},
70 {"UTF-16BE", iconv_swab
, iconv_swab
},
72 /* we include the UTF-8 alias to cope with differing locale settings */
73 {"UTF8", utf8_pull
, utf8_push
},
74 {"UTF-8", utf8_pull
, utf8_push
},
75 {"ASCII", ascii_pull
, ascii_push
},
76 {"646", ascii_pull
, ascii_push
},
77 {"ISO-8859-1", ascii_pull
, latin1_push
},
78 {"UCS2-HEX", ucs2hex_pull
, ucs2hex_push
},
82 static struct charset_functions
*charsets
= NULL
;
84 static struct charset_functions
*find_charset_functions(const char *name
)
86 struct charset_functions
*c
= charsets
;
89 if (strcasecmp(name
, c
->name
) == 0) {
98 NTSTATUS
smb_register_charset(struct charset_functions
*funcs
)
101 return NT_STATUS_INVALID_PARAMETER
;
104 DEBUG(5, ("Attempting to register new charset %s\n", funcs
->name
));
105 /* Check whether we already have this charset... */
106 if (find_charset_functions(funcs
->name
)) {
107 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs
->name
));
108 return NT_STATUS_OBJECT_NAME_COLLISION
;
111 funcs
->next
= funcs
->prev
= NULL
;
112 DEBUG(5, ("Registered charset %s\n", funcs
->name
));
113 DLIST_ADD(charsets
, funcs
);
117 static void lazy_initialize_iconv(void)
119 static bool initialized
;
124 for(i
= 0; builtin_functions
[i
].name
; i
++)
125 smb_register_charset(&builtin_functions
[i
]);
130 #ifdef HAVE_NATIVE_ICONV
131 /* if there was an error then reset the internal state,
132 this ensures that we don't have a shift state remaining for
133 character sets like SJIS */
134 static size_t sys_iconv(void *cd
,
135 const char **inbuf
, size_t *inbytesleft
,
136 char **outbuf
, size_t *outbytesleft
)
138 size_t ret
= iconv((iconv_t
)cd
,
139 (char **)inbuf
, inbytesleft
,
140 outbuf
, outbytesleft
);
141 if (ret
== (size_t)-1) {
142 int saved_errno
= errno
;
143 iconv(cd
, NULL
, NULL
, NULL
, NULL
);
151 * This is a simple portable iconv() implementaion.
153 * It only knows about a very small number of character sets - just
154 * enough that Samba works on systems that don't have iconv.
156 size_t smb_iconv(smb_iconv_t cd
,
157 const char **inbuf
, size_t *inbytesleft
,
158 char **outbuf
, size_t *outbytesleft
)
164 /* in many cases we can go direct */
166 return cd
->direct(cd
->cd_direct
,
167 inbuf
, inbytesleft
, outbuf
, outbytesleft
);
171 /* otherwise we have to do it chunks at a time */
172 while (*inbytesleft
> 0) {
174 bufsize
= sizeof(cvtbuf
);
176 if (cd
->pull(cd
->cd_pull
,
177 inbuf
, inbytesleft
, &bufp
, &bufsize
) == -1
178 && errno
!= E2BIG
) return -1;
181 bufsize
= sizeof(cvtbuf
) - bufsize
;
183 if (cd
->push(cd
->cd_push
,
184 (const char **)&bufp
, &bufsize
,
185 outbuf
, outbytesleft
) == -1) return -1;
192 static bool is_utf16(const char *name
)
194 return strcasecmp(name
, "UCS-2LE") == 0 ||
195 strcasecmp(name
, "UTF-16LE") == 0;
199 simple iconv_open() wrapper
201 smb_iconv_t
smb_iconv_open(const char *tocode
, const char *fromcode
)
204 struct charset_functions
*from
, *to
;
206 lazy_initialize_iconv();
210 ret
= SMB_MALLOC_P(struct _smb_iconv_t
);
213 return (smb_iconv_t
)-1;
215 memset(ret
, 0, sizeof(struct _smb_iconv_t
));
217 ret
->from_name
= SMB_STRDUP(fromcode
);
218 ret
->to_name
= SMB_STRDUP(tocode
);
220 /* check for the simplest null conversion */
221 if (strcasecmp(fromcode
, tocode
) == 0) {
222 ret
->direct
= iconv_copy
;
226 /* check if we have a builtin function for this conversion */
227 from
= find_charset_functions(fromcode
);
228 if(from
)ret
->pull
= from
->pull
;
230 to
= find_charset_functions(tocode
);
231 if(to
)ret
->push
= to
->push
;
233 /* check if we can use iconv for this conversion */
234 #ifdef HAVE_NATIVE_ICONV
236 ret
->cd_pull
= iconv_open("UTF-16LE", fromcode
);
237 if (ret
->cd_pull
== (iconv_t
)-1)
238 ret
->cd_pull
= iconv_open("UCS-2LE", fromcode
);
239 if (ret
->cd_pull
!= (iconv_t
)-1)
240 ret
->pull
= sys_iconv
;
244 ret
->cd_push
= iconv_open(tocode
, "UTF-16LE");
245 if (ret
->cd_push
== (iconv_t
)-1)
246 ret
->cd_push
= iconv_open(tocode
, "UCS-2LE");
247 if (ret
->cd_push
!= (iconv_t
)-1)
248 ret
->push
= sys_iconv
;
252 /* check if there is a module available that can do this conversion */
253 if (!ret
->pull
&& NT_STATUS_IS_OK(smb_probe_module("charset", fromcode
))) {
254 if(!(from
= find_charset_functions(fromcode
)))
255 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode
, fromcode
));
257 ret
->pull
= from
->pull
;
260 if (!ret
->push
&& NT_STATUS_IS_OK(smb_probe_module("charset", tocode
))) {
261 if(!(to
= find_charset_functions(tocode
)))
262 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode
, tocode
));
264 ret
->push
= to
->push
;
267 if (!ret
->push
|| !ret
->pull
) {
268 SAFE_FREE(ret
->from_name
);
269 SAFE_FREE(ret
->to_name
);
272 return (smb_iconv_t
)-1;
275 /* check for conversion to/from ucs2 */
276 if (is_utf16(fromcode
) && to
) {
277 ret
->direct
= to
->push
;
278 ret
->push
= ret
->pull
= NULL
;
282 if (is_utf16(tocode
) && from
) {
283 ret
->direct
= from
->pull
;
284 ret
->push
= ret
->pull
= NULL
;
288 /* Check if we can do the conversion direct */
289 #ifdef HAVE_NATIVE_ICONV
290 if (is_utf16(fromcode
)) {
291 ret
->direct
= sys_iconv
;
292 ret
->cd_direct
= ret
->cd_push
;
296 if (is_utf16(tocode
)) {
297 ret
->direct
= sys_iconv
;
298 ret
->cd_direct
= ret
->cd_pull
;
308 simple iconv_close() wrapper
310 int smb_iconv_close (smb_iconv_t cd
)
312 #ifdef HAVE_NATIVE_ICONV
313 if (cd
->cd_direct
) iconv_close((iconv_t
)cd
->cd_direct
);
314 if (cd
->cd_pull
) iconv_close((iconv_t
)cd
->cd_pull
);
315 if (cd
->cd_push
) iconv_close((iconv_t
)cd
->cd_push
);
318 SAFE_FREE(cd
->from_name
);
319 SAFE_FREE(cd
->to_name
);
321 memset(cd
, 0, sizeof(*cd
));
327 /**********************************************************************
328 the following functions implement the builtin character sets in Samba
329 and also the "test" character sets that are designed to test
330 multi-byte character set support for english users
331 ***********************************************************************/
333 static size_t ascii_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
334 char **outbuf
, size_t *outbytesleft
)
336 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
337 (*outbuf
)[0] = (*inbuf
)[0];
340 (*outbytesleft
) -= 2;
345 if (*inbytesleft
> 0) {
353 static size_t ascii_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
354 char **outbuf
, size_t *outbytesleft
)
358 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
359 (*outbuf
)[0] = (*inbuf
)[0] & 0x7F;
360 if ((*inbuf
)[1]) ir_count
++;
362 (*outbytesleft
) -= 1;
367 if (*inbytesleft
== 1) {
372 if (*inbytesleft
> 1) {
380 static size_t latin1_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
381 char **outbuf
, size_t *outbytesleft
)
385 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
386 (*outbuf
)[0] = (*inbuf
)[0];
387 if ((*inbuf
)[1]) ir_count
++;
389 (*outbytesleft
) -= 1;
394 if (*inbytesleft
== 1) {
399 if (*inbytesleft
> 1) {
407 static size_t ucs2hex_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
408 char **outbuf
, size_t *outbytesleft
)
410 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
413 if ((*inbuf
)[0] != '@') {
414 /* seven bit ascii case */
415 (*outbuf
)[0] = (*inbuf
)[0];
418 (*outbytesleft
) -= 2;
423 /* it's a hex character */
424 if (*inbytesleft
< 5) {
429 if (sscanf(&(*inbuf
)[1], "%04x", &v
) != 1) {
434 (*outbuf
)[0] = v
&0xff;
437 (*outbytesleft
) -= 2;
442 if (*inbytesleft
> 0) {
450 static size_t ucs2hex_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
451 char **outbuf
, size_t *outbytesleft
)
453 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
456 if ((*inbuf
)[1] == 0 &&
457 ((*inbuf
)[0] & 0x80) == 0 &&
458 (*inbuf
)[0] != '@') {
459 (*outbuf
)[0] = (*inbuf
)[0];
461 (*outbytesleft
) -= 1;
466 if (*outbytesleft
< 5) {
470 snprintf(buf
, 6, "@%04x", SVAL(*inbuf
, 0));
471 memcpy(*outbuf
, buf
, 5);
473 (*outbytesleft
) -= 5;
478 if (*inbytesleft
== 1) {
483 if (*inbytesleft
> 1) {
491 static size_t iconv_swab(void *cd
, const char **inbuf
, size_t *inbytesleft
,
492 char **outbuf
, size_t *outbytesleft
)
496 n
= MIN(*inbytesleft
, *outbytesleft
);
498 swab(*inbuf
, *outbuf
, (n
&~1));
504 (*outbytesleft
) -= n
;
508 if (*inbytesleft
> 0) {
516 static size_t iconv_copy(void *cd
, const char **inbuf
, size_t *inbytesleft
,
517 char **outbuf
, size_t *outbytesleft
)
521 n
= MIN(*inbytesleft
, *outbytesleft
);
523 memmove(*outbuf
, *inbuf
, n
);
526 (*outbytesleft
) -= n
;
530 if (*inbytesleft
> 0) {
538 static size_t utf8_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
539 char **outbuf
, size_t *outbytesleft
)
541 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
542 const uint8
*c
= (const uint8
*)*inbuf
;
543 uint8
*uc
= (uint8
*)*outbuf
;
545 while (in_left
>= 1 && out_left
>= 2) {
546 unsigned int codepoint
;
548 if ((c
[0] & 0x80) == 0) {
558 if ((c
[0] & 0xe0) == 0xc0) {
560 (c
[1] & 0xc0) != 0x80) {
564 codepoint
= (c
[1]&0x3f) | ((c
[0]&0x1f)<<6);
565 if (codepoint
< 0x80) {
566 /* don't accept UTF-8 characters that are not minimally packed */
570 uc
[1] = codepoint
>> 8;
571 uc
[0] = codepoint
& 0xff;
579 if ((c
[0] & 0xf0) == 0xe0) {
581 (c
[1] & 0xc0) != 0x80 ||
582 (c
[2] & 0xc0) != 0x80) {
586 codepoint
= (c
[2]&0x3f) | ((c
[1]&0x3f)<<6) | ((c
[0]&0xf)<<12);
587 if (codepoint
< 0x800) {
588 /* don't accept UTF-8 characters that are not minimally packed */
592 uc
[1] = codepoint
>> 8;
593 uc
[0] = codepoint
& 0xff;
601 if ((c
[0] & 0xf8) == 0xf0) {
603 (c
[1] & 0xc0) != 0x80 ||
604 (c
[2] & 0xc0) != 0x80 ||
605 (c
[3] & 0xc0) != 0x80) {
614 if (codepoint
< 0x10000 || codepoint
> 0x10ffff) {
615 /* don't accept UTF-8 characters that are not minimally packed */
620 codepoint
-= 0x10000;
627 uc
[0] = (codepoint
>>10) & 0xFF;
628 uc
[1] = (codepoint
>>18) | 0xd8;
629 uc
[2] = codepoint
& 0xFF;
630 uc
[3] = ((codepoint
>>8) & 0x3) | 0xdc;
638 /* we don't handle 5 byte sequences */
648 *inbytesleft
= in_left
;
649 *outbytesleft
= out_left
;
651 *outbuf
= (char *)uc
;
655 *inbytesleft
= in_left
;
656 *outbytesleft
= out_left
;
658 *outbuf
= (char *)uc
;
662 static size_t utf8_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
663 char **outbuf
, size_t *outbytesleft
)
665 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
666 uint8
*c
= (uint8
*)*outbuf
;
667 const uint8
*uc
= (const uint8
*)*inbuf
;
669 while (in_left
>= 2 && out_left
>= 1) {
670 unsigned int codepoint
;
672 if (uc
[1] == 0 && !(uc
[0] & 0x80)) {
682 if ((uc
[1]&0xf8) == 0) {
683 /* next simplest case */
688 c
[0] = 0xc0 | (uc
[0]>>6) | (uc
[1]<<2);
689 c
[1] = 0x80 | (uc
[0] & 0x3f);
697 if ((uc
[1] & 0xfc) == 0xdc) {
698 /* its the second part of a 4 byte sequence. Illegal */
707 if ((uc
[1] & 0xfc) != 0xd8) {
708 codepoint
= uc
[0] | (uc
[1]<<8);
713 c
[0] = 0xe0 | (codepoint
>> 12);
714 c
[1] = 0x80 | ((codepoint
>> 6) & 0x3f);
715 c
[2] = 0x80 | (codepoint
& 0x3f);
724 /* its the first part of a 4 byte sequence */
729 if ((uc
[3] & 0xfc) != 0xdc) {
733 codepoint
= 0x10000 + (uc
[2] | ((uc
[3] & 0x3)<<8) |
734 (uc
[0]<<10) | ((uc
[1] & 0x3)<<18));
740 c
[0] = 0xf0 | (codepoint
>> 18);
741 c
[1] = 0x80 | ((codepoint
>> 12) & 0x3f);
742 c
[2] = 0x80 | ((codepoint
>> 6) & 0x3f);
743 c
[3] = 0x80 | (codepoint
& 0x3f);
761 *inbytesleft
= in_left
;
762 *outbytesleft
= out_left
;
769 *inbytesleft
= in_left
;
770 *outbytesleft
= out_left
;