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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * We have to use strcasecmp here as the character conversions
26 * haven't been initialised yet. JRA.
34 * @brief Samba wrapper/stub for iconv character set conversion.
36 * iconv is the XPG2 interface for converting between character
37 * encodings. This file provides a Samba wrapper around it, and also
38 * a simple reimplementation that is used if the system does not
41 * Samba only works with encodings that are supersets of ASCII: ascii
42 * characters like whitespace can be tested for directly, multibyte
43 * sequences start with a byte with the high bit set, and strings are
44 * terminated by a nul byte.
46 * Note that the only function provided by iconv is conversion between
47 * characters. It doesn't directly support operations like
48 * uppercasing or comparison. We have to convert to UCS-2 and compare
51 * @sa Samba Developers Guide
56 static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
57 static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
58 static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
59 static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
60 static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
61 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
62 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
63 static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
64 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
66 static struct charset_functions builtin_functions
[] = {
67 /* windows is really neither UCS-2 not UTF-16 */
68 {"UCS-2LE", iconv_copy
, iconv_copy
},
69 {"UTF-16LE", iconv_copy
, iconv_copy
},
70 {"UCS-2BE", iconv_swab
, iconv_swab
},
71 {"UTF-16BE", iconv_swab
, iconv_swab
},
73 /* we include the UTF-8 alias to cope with differing locale settings */
74 {"UTF8", utf8_pull
, utf8_push
},
75 {"UTF-8", utf8_pull
, utf8_push
},
76 {"ASCII", ascii_pull
, ascii_push
},
77 {"646", ascii_pull
, ascii_push
},
78 {"ISO-8859-1", ascii_pull
, latin1_push
},
79 {"UCS2-HEX", ucs2hex_pull
, ucs2hex_push
},
83 static struct charset_functions
*charsets
= NULL
;
85 static struct charset_functions
*find_charset_functions(const char *name
)
87 struct charset_functions
*c
= charsets
;
90 if (strcasecmp(name
, c
->name
) == 0) {
99 NTSTATUS
smb_register_charset(struct charset_functions
*funcs
)
102 return NT_STATUS_INVALID_PARAMETER
;
105 DEBUG(5, ("Attempting to register new charset %s\n", funcs
->name
));
106 /* Check whether we already have this charset... */
107 if (find_charset_functions(funcs
->name
)) {
108 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs
->name
));
109 return NT_STATUS_OBJECT_NAME_COLLISION
;
112 funcs
->next
= funcs
->prev
= NULL
;
113 DEBUG(5, ("Registered charset %s\n", funcs
->name
));
114 DLIST_ADD(charsets
, funcs
);
118 static void lazy_initialize_iconv(void)
120 static BOOL initialized
;
125 for(i
= 0; builtin_functions
[i
].name
; i
++)
126 smb_register_charset(&builtin_functions
[i
]);
131 #ifdef HAVE_NATIVE_ICONV
132 /* if there was an error then reset the internal state,
133 this ensures that we don't have a shift state remaining for
134 character sets like SJIS */
135 static size_t sys_iconv(void *cd
,
136 const char **inbuf
, size_t *inbytesleft
,
137 char **outbuf
, size_t *outbytesleft
)
139 size_t ret
= iconv((iconv_t
)cd
,
140 (char **)inbuf
, inbytesleft
,
141 outbuf
, outbytesleft
);
142 if (ret
== (size_t)-1) {
143 int saved_errno
= errno
;
144 iconv(cd
, NULL
, NULL
, NULL
, NULL
);
152 * This is a simple portable iconv() implementaion.
154 * It only knows about a very small number of character sets - just
155 * enough that Samba works on systems that don't have iconv.
157 size_t smb_iconv(smb_iconv_t cd
,
158 const char **inbuf
, size_t *inbytesleft
,
159 char **outbuf
, size_t *outbytesleft
)
165 /* in many cases we can go direct */
167 return cd
->direct(cd
->cd_direct
,
168 inbuf
, inbytesleft
, outbuf
, outbytesleft
);
172 /* otherwise we have to do it chunks at a time */
173 while (*inbytesleft
> 0) {
175 bufsize
= sizeof(cvtbuf
);
177 if (cd
->pull(cd
->cd_pull
,
178 inbuf
, inbytesleft
, &bufp
, &bufsize
) == -1
179 && errno
!= E2BIG
) return -1;
182 bufsize
= sizeof(cvtbuf
) - bufsize
;
184 if (cd
->push(cd
->cd_push
,
185 (const char **)&bufp
, &bufsize
,
186 outbuf
, outbytesleft
) == -1) return -1;
193 static BOOL
is_utf16(const char *name
)
195 return strcasecmp(name
, "UCS-2LE") == 0 ||
196 strcasecmp(name
, "UTF-16LE") == 0;
200 simple iconv_open() wrapper
202 smb_iconv_t
smb_iconv_open(const char *tocode
, const char *fromcode
)
205 struct charset_functions
*from
, *to
;
207 lazy_initialize_iconv();
211 ret
= SMB_MALLOC_P(struct _smb_iconv_t
);
214 return (smb_iconv_t
)-1;
216 memset(ret
, 0, sizeof(struct _smb_iconv_t
));
218 ret
->from_name
= SMB_STRDUP(fromcode
);
219 ret
->to_name
= SMB_STRDUP(tocode
);
221 /* check for the simplest null conversion */
222 if (strcasecmp(fromcode
, tocode
) == 0) {
223 ret
->direct
= iconv_copy
;
227 /* check if we have a builtin function for this conversion */
228 from
= find_charset_functions(fromcode
);
229 if(from
)ret
->pull
= from
->pull
;
231 to
= find_charset_functions(tocode
);
232 if(to
)ret
->push
= to
->push
;
234 /* check if we can use iconv for this conversion */
235 #ifdef HAVE_NATIVE_ICONV
237 ret
->cd_pull
= iconv_open("UTF-16LE", fromcode
);
238 if (ret
->cd_pull
== (iconv_t
)-1)
239 ret
->cd_pull
= iconv_open("UCS-2LE", fromcode
);
240 if (ret
->cd_pull
!= (iconv_t
)-1)
241 ret
->pull
= sys_iconv
;
245 ret
->cd_push
= iconv_open(tocode
, "UTF-16LE");
246 if (ret
->cd_push
== (iconv_t
)-1)
247 ret
->cd_push
= iconv_open(tocode
, "UCS-2LE");
248 if (ret
->cd_push
!= (iconv_t
)-1)
249 ret
->push
= sys_iconv
;
253 /* check if there is a module available that can do this conversion */
254 if (!ret
->pull
&& NT_STATUS_IS_OK(smb_probe_module("charset", fromcode
))) {
255 if(!(from
= find_charset_functions(fromcode
)))
256 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode
, fromcode
));
258 ret
->pull
= from
->pull
;
261 if (!ret
->push
&& NT_STATUS_IS_OK(smb_probe_module("charset", tocode
))) {
262 if(!(to
= find_charset_functions(tocode
)))
263 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode
, tocode
));
265 ret
->push
= to
->push
;
268 if (!ret
->push
|| !ret
->pull
) {
269 SAFE_FREE(ret
->from_name
);
270 SAFE_FREE(ret
->to_name
);
273 return (smb_iconv_t
)-1;
276 /* check for conversion to/from ucs2 */
277 if (is_utf16(fromcode
) && to
) {
278 ret
->direct
= to
->push
;
279 ret
->push
= ret
->pull
= NULL
;
283 if (is_utf16(tocode
) && from
) {
284 ret
->direct
= from
->pull
;
285 ret
->push
= ret
->pull
= NULL
;
289 /* Check if we can do the conversion direct */
290 #ifdef HAVE_NATIVE_ICONV
291 if (is_utf16(fromcode
)) {
292 ret
->direct
= sys_iconv
;
293 ret
->cd_direct
= ret
->cd_push
;
297 if (is_utf16(tocode
)) {
298 ret
->direct
= sys_iconv
;
299 ret
->cd_direct
= ret
->cd_pull
;
309 simple iconv_close() wrapper
311 int smb_iconv_close (smb_iconv_t cd
)
313 #ifdef HAVE_NATIVE_ICONV
314 if (cd
->cd_direct
) iconv_close((iconv_t
)cd
->cd_direct
);
315 if (cd
->cd_pull
) iconv_close((iconv_t
)cd
->cd_pull
);
316 if (cd
->cd_push
) iconv_close((iconv_t
)cd
->cd_push
);
319 SAFE_FREE(cd
->from_name
);
320 SAFE_FREE(cd
->to_name
);
322 memset(cd
, 0, sizeof(*cd
));
328 /**********************************************************************
329 the following functions implement the builtin character sets in Samba
330 and also the "test" character sets that are designed to test
331 multi-byte character set support for english users
332 ***********************************************************************/
334 static size_t ascii_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
335 char **outbuf
, size_t *outbytesleft
)
337 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
338 (*outbuf
)[0] = (*inbuf
)[0];
341 (*outbytesleft
) -= 2;
346 if (*inbytesleft
> 0) {
354 static size_t ascii_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
355 char **outbuf
, size_t *outbytesleft
)
359 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
360 (*outbuf
)[0] = (*inbuf
)[0] & 0x7F;
361 if ((*inbuf
)[1]) ir_count
++;
363 (*outbytesleft
) -= 1;
368 if (*inbytesleft
== 1) {
373 if (*inbytesleft
> 1) {
381 static size_t latin1_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
382 char **outbuf
, size_t *outbytesleft
)
386 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
387 (*outbuf
)[0] = (*inbuf
)[0];
388 if ((*inbuf
)[1]) ir_count
++;
390 (*outbytesleft
) -= 1;
395 if (*inbytesleft
== 1) {
400 if (*inbytesleft
> 1) {
408 static size_t ucs2hex_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
409 char **outbuf
, size_t *outbytesleft
)
411 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
414 if ((*inbuf
)[0] != '@') {
415 /* seven bit ascii case */
416 (*outbuf
)[0] = (*inbuf
)[0];
419 (*outbytesleft
) -= 2;
424 /* it's a hex character */
425 if (*inbytesleft
< 5) {
430 if (sscanf(&(*inbuf
)[1], "%04x", &v
) != 1) {
435 (*outbuf
)[0] = v
&0xff;
438 (*outbytesleft
) -= 2;
443 if (*inbytesleft
> 0) {
451 static size_t ucs2hex_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
452 char **outbuf
, size_t *outbytesleft
)
454 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
457 if ((*inbuf
)[1] == 0 &&
458 ((*inbuf
)[0] & 0x80) == 0 &&
459 (*inbuf
)[0] != '@') {
460 (*outbuf
)[0] = (*inbuf
)[0];
462 (*outbytesleft
) -= 1;
467 if (*outbytesleft
< 5) {
471 snprintf(buf
, 6, "@%04x", SVAL(*inbuf
, 0));
472 memcpy(*outbuf
, buf
, 5);
474 (*outbytesleft
) -= 5;
479 if (*inbytesleft
== 1) {
484 if (*inbytesleft
> 1) {
492 static size_t iconv_swab(void *cd
, const char **inbuf
, size_t *inbytesleft
,
493 char **outbuf
, size_t *outbytesleft
)
497 n
= MIN(*inbytesleft
, *outbytesleft
);
499 swab(*inbuf
, *outbuf
, (n
&~1));
505 (*outbytesleft
) -= n
;
509 if (*inbytesleft
> 0) {
517 static size_t iconv_copy(void *cd
, const char **inbuf
, size_t *inbytesleft
,
518 char **outbuf
, size_t *outbytesleft
)
522 n
= MIN(*inbytesleft
, *outbytesleft
);
524 memmove(*outbuf
, *inbuf
, n
);
527 (*outbytesleft
) -= n
;
531 if (*inbytesleft
> 0) {
539 static size_t utf8_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
540 char **outbuf
, size_t *outbytesleft
)
542 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
543 const uint8
*c
= (const uint8
*)*inbuf
;
544 uint8
*uc
= (uint8
*)*outbuf
;
546 while (in_left
>= 1 && out_left
>= 2) {
547 unsigned int codepoint
;
549 if ((c
[0] & 0x80) == 0) {
559 if ((c
[0] & 0xe0) == 0xc0) {
561 (c
[1] & 0xc0) != 0x80) {
565 codepoint
= (c
[1]&0x3f) | ((c
[0]&0x1f)<<6);
566 if (codepoint
< 0x80) {
567 /* don't accept UTF-8 characters that are not minimally packed */
571 uc
[1] = codepoint
>> 8;
572 uc
[0] = codepoint
& 0xff;
580 if ((c
[0] & 0xf0) == 0xe0) {
582 (c
[1] & 0xc0) != 0x80 ||
583 (c
[2] & 0xc0) != 0x80) {
587 codepoint
= (c
[2]&0x3f) | ((c
[1]&0x3f)<<6) | ((c
[0]&0xf)<<12);
588 if (codepoint
< 0x800) {
589 /* don't accept UTF-8 characters that are not minimally packed */
593 uc
[1] = codepoint
>> 8;
594 uc
[0] = codepoint
& 0xff;
602 if ((c
[0] & 0xf8) == 0xf0) {
604 (c
[1] & 0xc0) != 0x80 ||
605 (c
[2] & 0xc0) != 0x80 ||
606 (c
[3] & 0xc0) != 0x80) {
615 if (codepoint
< 0x10000 || codepoint
> 0x10ffff) {
616 /* don't accept UTF-8 characters that are not minimally packed */
621 codepoint
-= 0x10000;
628 uc
[0] = (codepoint
>>10) & 0xFF;
629 uc
[1] = (codepoint
>>18) | 0xd8;
630 uc
[2] = codepoint
& 0xFF;
631 uc
[3] = ((codepoint
>>8) & 0x3) | 0xdc;
639 /* we don't handle 5 byte sequences */
649 *inbytesleft
= in_left
;
650 *outbytesleft
= out_left
;
652 *outbuf
= (char *)uc
;
656 *inbytesleft
= in_left
;
657 *outbytesleft
= out_left
;
659 *outbuf
= (char *)uc
;
663 static size_t utf8_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
664 char **outbuf
, size_t *outbytesleft
)
666 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
667 uint8
*c
= (uint8
*)*outbuf
;
668 const uint8
*uc
= (const uint8
*)*inbuf
;
670 while (in_left
>= 2 && out_left
>= 1) {
671 unsigned int codepoint
;
673 if (uc
[1] == 0 && !(uc
[0] & 0x80)) {
683 if ((uc
[1]&0xf8) == 0) {
684 /* next simplest case */
689 c
[0] = 0xc0 | (uc
[0]>>6) | (uc
[1]<<2);
690 c
[1] = 0x80 | (uc
[0] & 0x3f);
698 if ((uc
[1] & 0xfc) == 0xdc) {
699 /* its the second part of a 4 byte sequence. Illegal */
708 if ((uc
[1] & 0xfc) != 0xd8) {
709 codepoint
= uc
[0] | (uc
[1]<<8);
714 c
[0] = 0xe0 | (codepoint
>> 12);
715 c
[1] = 0x80 | ((codepoint
>> 6) & 0x3f);
716 c
[2] = 0x80 | (codepoint
& 0x3f);
725 /* its the first part of a 4 byte sequence */
730 if ((uc
[3] & 0xfc) != 0xdc) {
734 codepoint
= 0x10000 + (uc
[2] | ((uc
[3] & 0x3)<<8) |
735 (uc
[0]<<10) | ((uc
[1] & 0x3)<<18));
741 c
[0] = 0xf0 | (codepoint
>> 18);
742 c
[1] = 0x80 | ((codepoint
>> 12) & 0x3f);
743 c
[2] = 0x80 | ((codepoint
>> 6) & 0x3f);
744 c
[3] = 0x80 | (codepoint
& 0x3f);
762 *inbytesleft
= in_left
;
763 *outbytesleft
= out_left
;
770 *inbytesleft
= in_left
;
771 *outbytesleft
= out_left
;