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
54 static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
55 static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
56 static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
57 static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
58 static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
59 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
60 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
61 static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
62 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
64 static struct charset_functions builtin_functions
[] = {
65 /* windows is really neither UCS-2 not UTF-16 */
66 {"UCS-2LE", iconv_copy
, iconv_copy
},
67 {"UTF-16LE", iconv_copy
, iconv_copy
},
68 {"UCS-2BE", iconv_swab
, iconv_swab
},
69 {"UTF-16BE", iconv_swab
, iconv_swab
},
71 /* we include the UTF-8 alias to cope with differing locale settings */
72 {"UTF8", utf8_pull
, utf8_push
},
73 {"UTF-8", utf8_pull
, utf8_push
},
74 {"ASCII", ascii_pull
, ascii_push
},
75 {"646", ascii_pull
, ascii_push
},
76 {"ISO-8859-1", ascii_pull
, latin1_push
},
77 {"UCS2-HEX", ucs2hex_pull
, ucs2hex_push
},
81 static struct charset_functions
*charsets
= NULL
;
83 static struct charset_functions
*find_charset_functions(const char *name
)
85 struct charset_functions
*c
= charsets
;
88 if (strcasecmp(name
, c
->name
) == 0) {
97 NTSTATUS
smb_register_charset(struct charset_functions
*funcs
)
100 return NT_STATUS_INVALID_PARAMETER
;
103 DEBUG(5, ("Attempting to register new charset %s\n", funcs
->name
));
104 /* Check whether we already have this charset... */
105 if (find_charset_functions(funcs
->name
)) {
106 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs
->name
));
107 return NT_STATUS_OBJECT_NAME_COLLISION
;
110 funcs
->next
= funcs
->prev
= NULL
;
111 DEBUG(5, ("Registered charset %s\n", funcs
->name
));
112 DLIST_ADD(charsets
, funcs
);
116 static void lazy_initialize_iconv(void)
118 static BOOL initialized
;
123 for(i
= 0; builtin_functions
[i
].name
; i
++)
124 smb_register_charset(&builtin_functions
[i
]);
129 /* if there was an error then reset the internal state,
130 this ensures that we don't have a shift state remaining for
131 character sets like SJIS */
132 static size_t sys_iconv(void *cd
,
133 const char **inbuf
, size_t *inbytesleft
,
134 char **outbuf
, size_t *outbytesleft
)
136 #ifdef HAVE_NATIVE_ICONV
137 size_t ret
= iconv((iconv_t
)cd
,
138 (char **)inbuf
, inbytesleft
,
139 outbuf
, outbytesleft
);
140 if (ret
== (size_t)-1) {
141 int saved_errno
= errno
;
142 iconv(cd
, NULL
, NULL
, NULL
, NULL
);
153 * This is a simple portable iconv() implementaion.
155 * It only knows about a very small number of character sets - just
156 * enough that Samba works on systems that don't have iconv.
158 size_t smb_iconv(smb_iconv_t cd
,
159 const char **inbuf
, size_t *inbytesleft
,
160 char **outbuf
, size_t *outbytesleft
)
166 /* in many cases we can go direct */
168 return cd
->direct(cd
->cd_direct
,
169 inbuf
, inbytesleft
, outbuf
, outbytesleft
);
173 /* otherwise we have to do it chunks at a time */
174 while (*inbytesleft
> 0) {
176 bufsize
= sizeof(cvtbuf
);
178 if (cd
->pull(cd
->cd_pull
,
179 inbuf
, inbytesleft
, &bufp
, &bufsize
) == -1
180 && errno
!= E2BIG
) return -1;
183 bufsize
= sizeof(cvtbuf
) - bufsize
;
185 if (cd
->push(cd
->cd_push
,
186 (const char **)&bufp
, &bufsize
,
187 outbuf
, outbytesleft
) == -1) return -1;
194 static BOOL
is_utf16(const char *name
)
196 return strcasecmp(name
, "UCS-2LE") == 0 ||
197 strcasecmp(name
, "UTF-16LE") == 0;
201 simple iconv_open() wrapper
203 smb_iconv_t
smb_iconv_open(const char *tocode
, const char *fromcode
)
206 struct charset_functions
*from
, *to
;
208 lazy_initialize_iconv();
212 ret
= SMB_MALLOC_P(struct _smb_iconv_t
);
215 return (smb_iconv_t
)-1;
217 memset(ret
, 0, sizeof(struct _smb_iconv_t
));
219 ret
->from_name
= SMB_STRDUP(fromcode
);
220 ret
->to_name
= SMB_STRDUP(tocode
);
222 /* check for the simplest null conversion */
223 if (strcasecmp(fromcode
, tocode
) == 0) {
224 ret
->direct
= iconv_copy
;
228 /* check if we have a builtin function for this conversion */
229 from
= find_charset_functions(fromcode
);
230 if(from
)ret
->pull
= from
->pull
;
232 to
= find_charset_functions(tocode
);
233 if(to
)ret
->push
= to
->push
;
235 /* check if we can use iconv for this conversion */
236 #ifdef HAVE_NATIVE_ICONV
238 ret
->cd_pull
= iconv_open("UTF-16LE", fromcode
);
239 if (ret
->cd_pull
== (iconv_t
)-1)
240 ret
->cd_pull
= iconv_open("UCS-2LE", fromcode
);
241 if (ret
->cd_pull
!= (iconv_t
)-1)
242 ret
->pull
= sys_iconv
;
246 ret
->cd_push
= iconv_open(tocode
, "UTF-16LE");
247 if (ret
->cd_push
== (iconv_t
)-1)
248 ret
->cd_push
= iconv_open(tocode
, "UCS-2LE");
249 if (ret
->cd_push
!= (iconv_t
)-1)
250 ret
->push
= sys_iconv
;
254 /* check if there is a module available that can do this conversion */
255 if (!ret
->pull
&& NT_STATUS_IS_OK(smb_probe_module("charset", fromcode
))) {
256 if(!(from
= find_charset_functions(fromcode
)))
257 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode
, fromcode
));
259 ret
->pull
= from
->pull
;
262 if (!ret
->push
&& NT_STATUS_IS_OK(smb_probe_module("charset", tocode
))) {
263 if(!(to
= find_charset_functions(tocode
)))
264 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode
, tocode
));
266 ret
->push
= to
->push
;
269 if (!ret
->push
|| !ret
->pull
) {
270 SAFE_FREE(ret
->from_name
);
271 SAFE_FREE(ret
->to_name
);
274 return (smb_iconv_t
)-1;
277 /* check for conversion to/from ucs2 */
278 if (is_utf16(fromcode
) && to
) {
279 ret
->direct
= to
->push
;
280 ret
->push
= ret
->pull
= NULL
;
284 if (is_utf16(tocode
) && from
) {
285 ret
->direct
= from
->pull
;
286 ret
->push
= ret
->pull
= NULL
;
290 /* Check if we can do the conversion direct */
291 #ifdef HAVE_NATIVE_ICONV
292 if (is_utf16(fromcode
)) {
293 ret
->direct
= sys_iconv
;
294 ret
->cd_direct
= ret
->cd_push
;
298 if (is_utf16(tocode
)) {
299 ret
->direct
= sys_iconv
;
300 ret
->cd_direct
= ret
->cd_pull
;
310 simple iconv_close() wrapper
312 int smb_iconv_close (smb_iconv_t cd
)
314 #ifdef HAVE_NATIVE_ICONV
315 if (cd
->cd_direct
) iconv_close((iconv_t
)cd
->cd_direct
);
316 if (cd
->cd_pull
) iconv_close((iconv_t
)cd
->cd_pull
);
317 if (cd
->cd_push
) iconv_close((iconv_t
)cd
->cd_push
);
320 SAFE_FREE(cd
->from_name
);
321 SAFE_FREE(cd
->to_name
);
323 memset(cd
, 0, sizeof(*cd
));
329 /**********************************************************************
330 the following functions implement the builtin character sets in Samba
331 and also the "test" character sets that are designed to test
332 multi-byte character set support for english users
333 ***********************************************************************/
335 static size_t ascii_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
336 char **outbuf
, size_t *outbytesleft
)
338 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
339 (*outbuf
)[0] = (*inbuf
)[0];
342 (*outbytesleft
) -= 2;
347 if (*inbytesleft
> 0) {
355 static size_t ascii_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
356 char **outbuf
, size_t *outbytesleft
)
360 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
361 (*outbuf
)[0] = (*inbuf
)[0] & 0x7F;
362 if ((*inbuf
)[1]) ir_count
++;
364 (*outbytesleft
) -= 1;
369 if (*inbytesleft
== 1) {
374 if (*inbytesleft
> 1) {
382 static size_t latin1_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
383 char **outbuf
, size_t *outbytesleft
)
387 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
388 (*outbuf
)[0] = (*inbuf
)[0];
389 if ((*inbuf
)[1]) ir_count
++;
391 (*outbytesleft
) -= 1;
396 if (*inbytesleft
== 1) {
401 if (*inbytesleft
> 1) {
409 static size_t ucs2hex_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
410 char **outbuf
, size_t *outbytesleft
)
412 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
415 if ((*inbuf
)[0] != '@') {
416 /* seven bit ascii case */
417 (*outbuf
)[0] = (*inbuf
)[0];
420 (*outbytesleft
) -= 2;
425 /* it's a hex character */
426 if (*inbytesleft
< 5) {
431 if (sscanf(&(*inbuf
)[1], "%04x", &v
) != 1) {
436 (*outbuf
)[0] = v
&0xff;
439 (*outbytesleft
) -= 2;
444 if (*inbytesleft
> 0) {
452 static size_t ucs2hex_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
453 char **outbuf
, size_t *outbytesleft
)
455 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
458 if ((*inbuf
)[1] == 0 &&
459 ((*inbuf
)[0] & 0x80) == 0 &&
460 (*inbuf
)[0] != '@') {
461 (*outbuf
)[0] = (*inbuf
)[0];
463 (*outbytesleft
) -= 1;
468 if (*outbytesleft
< 5) {
472 snprintf(buf
, 6, "@%04x", SVAL(*inbuf
, 0));
473 memcpy(*outbuf
, buf
, 5);
475 (*outbytesleft
) -= 5;
480 if (*inbytesleft
== 1) {
485 if (*inbytesleft
> 1) {
493 static size_t iconv_swab(void *cd
, const char **inbuf
, size_t *inbytesleft
,
494 char **outbuf
, size_t *outbytesleft
)
498 n
= MIN(*inbytesleft
, *outbytesleft
);
500 swab(*inbuf
, *outbuf
, (n
&~1));
506 (*outbytesleft
) -= n
;
510 if (*inbytesleft
> 0) {
518 static size_t iconv_copy(void *cd
, const char **inbuf
, size_t *inbytesleft
,
519 char **outbuf
, size_t *outbytesleft
)
523 n
= MIN(*inbytesleft
, *outbytesleft
);
525 memmove(*outbuf
, *inbuf
, n
);
528 (*outbytesleft
) -= n
;
532 if (*inbytesleft
> 0) {
540 static size_t utf8_pull(void *cd
, const char **inbuf
, size_t *inbytesleft
,
541 char **outbuf
, size_t *outbytesleft
)
543 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
544 const uint8
*c
= (const uint8
*)*inbuf
;
545 uint8
*uc
= (uint8
*)*outbuf
;
547 while (in_left
>= 1 && out_left
>= 2) {
548 if ((c
[0] & 0x80) == 0) {
558 if ((c
[0] & 0xe0) == 0xc0) {
560 (c
[1] & 0xc0) != 0x80) {
564 uc
[1] = (c
[0]>>2) & 0x7;
565 uc
[0] = (c
[0]<<6) | (c
[1]&0x3f);
573 if ((c
[0] & 0xf0) == 0xe0) {
575 (c
[1] & 0xc0) != 0x80 ||
576 (c
[2] & 0xc0) != 0x80) {
580 uc
[1] = ((c
[0]&0xF)<<4) | ((c
[1]>>2)&0xF);
581 uc
[0] = (c
[1]<<6) | (c
[2]&0x3f);
589 if ((c
[0] & 0xf8) == 0xf0) {
590 unsigned int codepoint
;
592 (c
[1] & 0xc0) != 0x80 ||
593 (c
[2] & 0xc0) != 0x80 ||
594 (c
[3] & 0xc0) != 0x80) {
603 if (codepoint
< 0x10000) {
604 /* accept UTF-8 characters that are not
605 minimally packed, but pack the result */
606 uc
[0] = (codepoint
& 0xFF);
607 uc
[1] = (codepoint
>> 8);
615 codepoint
-= 0x10000;
622 uc
[0] = (codepoint
>>10) & 0xFF;
623 uc
[1] = (codepoint
>>18) | 0xd8;
624 uc
[2] = codepoint
& 0xFF;
625 uc
[3] = ((codepoint
>>8) & 0x3) | 0xdc;
633 /* we don't handle 5 byte sequences */
643 *inbytesleft
= in_left
;
644 *outbytesleft
= out_left
;
646 *outbuf
= (char *)uc
;
650 *inbytesleft
= in_left
;
651 *outbytesleft
= out_left
;
653 *outbuf
= (char *)uc
;
657 static size_t utf8_push(void *cd
, const char **inbuf
, size_t *inbytesleft
,
658 char **outbuf
, size_t *outbytesleft
)
660 size_t in_left
=*inbytesleft
, out_left
=*outbytesleft
;
661 uint8
*c
= (uint8
*)*outbuf
;
662 const uint8
*uc
= (const uint8
*)*inbuf
;
664 while (in_left
>= 2 && out_left
>= 1) {
665 unsigned int codepoint
;
667 if (uc
[1] == 0 && !(uc
[0] & 0x80)) {
677 if ((uc
[1]&0xf8) == 0) {
678 /* next simplest case */
683 c
[0] = 0xc0 | (uc
[0]>>6) | (uc
[1]<<2);
684 c
[1] = 0x80 | (uc
[0] & 0x3f);
692 if ((uc
[1] & 0xfc) == 0xdc) {
693 /* its the second part of a 4 byte sequence. Illegal */
702 if ((uc
[1] & 0xfc) != 0xd8) {
703 codepoint
= uc
[0] | (uc
[1]<<8);
708 c
[0] = 0xe0 | (codepoint
>> 12);
709 c
[1] = 0x80 | ((codepoint
>> 6) & 0x3f);
710 c
[2] = 0x80 | (codepoint
& 0x3f);
719 /* its the first part of a 4 byte sequence */
724 if ((uc
[3] & 0xfc) != 0xdc) {
728 codepoint
= 0x10000 + (uc
[2] | ((uc
[3] & 0x3)<<8) |
729 (uc
[0]<<10) | ((uc
[1] & 0x3)<<18));
735 c
[0] = 0xf0 | (codepoint
>> 18);
736 c
[1] = 0x80 | ((codepoint
>> 12) & 0x3f);
737 c
[2] = 0x80 | ((codepoint
>> 6) & 0x3f);
738 c
[3] = 0x80 | (codepoint
& 0x3f);
756 *inbytesleft
= in_left
;
757 *outbytesleft
= out_left
;
764 *inbytesleft
= in_left
;
765 *outbytesleft
= out_left
;