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 /* 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 #ifdef HAVE_NATIVE_ICONV
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);
155 * This is a simple portable iconv() implementaion.
157 * It only knows about a very small number of character sets - just
158 * enough that Samba works on systems that don't have iconv.
160 size_t smb_iconv(smb_iconv_t cd,
161 const char **inbuf, size_t *inbytesleft,
162 char **outbuf, size_t *outbytesleft)
168 /* in many cases we can go direct */
170 return cd->direct(cd->cd_direct,
171 inbuf, inbytesleft, outbuf, outbytesleft);
175 /* otherwise we have to do it chunks at a time */
176 while (*inbytesleft > 0) {
178 bufsize = sizeof(cvtbuf);
180 if (cd->pull(cd->cd_pull,
181 inbuf, inbytesleft, &bufp, &bufsize) == -1
182 && errno != E2BIG) return -1;
185 bufsize = sizeof(cvtbuf) - bufsize;
187 if (cd->push(cd->cd_push,
188 (const char **)&bufp, &bufsize,
189 outbuf, outbytesleft) == -1) return -1;
196 static BOOL is_utf16(const char *name)
198 return strcasecmp(name, "UCS-2LE") == 0 ||
199 strcasecmp(name, "UTF-16LE") == 0;
203 simple iconv_open() wrapper
205 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
208 struct charset_functions *from, *to;
210 lazy_initialize_iconv();
214 ret = SMB_MALLOC_P(struct _smb_iconv_t);
217 return (smb_iconv_t)-1;
219 memset(ret, 0, sizeof(struct _smb_iconv_t));
221 ret->from_name = SMB_STRDUP(fromcode);
222 ret->to_name = SMB_STRDUP(tocode);
224 /* check for the simplest null conversion */
225 if (strcasecmp(fromcode, tocode) == 0) {
226 ret->direct = iconv_copy;
230 /* check if we have a builtin function for this conversion */
231 from = find_charset_functions(fromcode);
232 if(from)ret->pull = from->pull;
234 to = find_charset_functions(tocode);
235 if(to)ret->push = to->push;
237 /* check if we can use iconv for this conversion */
238 #ifdef HAVE_NATIVE_ICONV
240 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
241 if (ret->cd_pull == (iconv_t)-1)
242 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
243 if (ret->cd_pull != (iconv_t)-1)
244 ret->pull = sys_iconv;
248 ret->cd_push = iconv_open(tocode, "UTF-16LE");
249 if (ret->cd_push == (iconv_t)-1)
250 ret->cd_push = iconv_open(tocode, "UCS-2LE");
251 if (ret->cd_push != (iconv_t)-1)
252 ret->push = sys_iconv;
256 /* check if there is a module available that can do this conversion */
257 if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
258 if(!(from = find_charset_functions(fromcode)))
259 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
261 ret->pull = from->pull;
264 if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
265 if(!(to = find_charset_functions(tocode)))
266 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
268 ret->push = to->push;
271 if (!ret->push || !ret->pull) {
272 SAFE_FREE(ret->from_name);
273 SAFE_FREE(ret->to_name);
276 return (smb_iconv_t)-1;
279 /* check for conversion to/from ucs2 */
280 if (is_utf16(fromcode) && to) {
281 ret->direct = to->push;
282 ret->push = ret->pull = NULL;
286 if (is_utf16(tocode) && from) {
287 ret->direct = from->pull;
288 ret->push = ret->pull = NULL;
292 /* Check if we can do the conversion direct */
293 #ifdef HAVE_NATIVE_ICONV
294 if (is_utf16(fromcode)) {
295 ret->direct = sys_iconv;
296 ret->cd_direct = ret->cd_push;
300 if (is_utf16(tocode)) {
301 ret->direct = sys_iconv;
302 ret->cd_direct = ret->cd_pull;
312 simple iconv_close() wrapper
314 int smb_iconv_close (smb_iconv_t cd)
316 #ifdef HAVE_NATIVE_ICONV
317 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
318 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
319 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
322 SAFE_FREE(cd->from_name);
323 SAFE_FREE(cd->to_name);
325 memset(cd, 0, sizeof(*cd));
331 /**********************************************************************
332 the following functions implement the builtin character sets in Samba
333 and also the "test" character sets that are designed to test
334 multi-byte character set support for english users
335 ***********************************************************************/
337 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
338 char **outbuf, size_t *outbytesleft)
340 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
341 (*outbuf)[0] = (*inbuf)[0];
344 (*outbytesleft) -= 2;
349 if (*inbytesleft > 0) {
357 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
358 char **outbuf, size_t *outbytesleft)
362 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
363 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
364 if ((*inbuf)[1]) ir_count++;
366 (*outbytesleft) -= 1;
371 if (*inbytesleft == 1) {
376 if (*inbytesleft > 1) {
384 static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
385 char **outbuf, size_t *outbytesleft)
389 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
390 (*outbuf)[0] = (*inbuf)[0];
391 if ((*inbuf)[1]) ir_count++;
393 (*outbytesleft) -= 1;
398 if (*inbytesleft == 1) {
403 if (*inbytesleft > 1) {
411 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
412 char **outbuf, size_t *outbytesleft)
414 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
417 if ((*inbuf)[0] != '@') {
418 /* seven bit ascii case */
419 (*outbuf)[0] = (*inbuf)[0];
422 (*outbytesleft) -= 2;
427 /* it's a hex character */
428 if (*inbytesleft < 5) {
433 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
438 (*outbuf)[0] = v&0xff;
441 (*outbytesleft) -= 2;
446 if (*inbytesleft > 0) {
454 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
455 char **outbuf, size_t *outbytesleft)
457 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
460 if ((*inbuf)[1] == 0 &&
461 ((*inbuf)[0] & 0x80) == 0 &&
462 (*inbuf)[0] != '@') {
463 (*outbuf)[0] = (*inbuf)[0];
465 (*outbytesleft) -= 1;
470 if (*outbytesleft < 5) {
474 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
475 memcpy(*outbuf, buf, 5);
477 (*outbytesleft) -= 5;
482 if (*inbytesleft == 1) {
487 if (*inbytesleft > 1) {
495 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
496 char **outbuf, size_t *outbytesleft)
500 n = MIN(*inbytesleft, *outbytesleft);
502 swab(*inbuf, *outbuf, (n&~1));
508 (*outbytesleft) -= n;
512 if (*inbytesleft > 0) {
520 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
521 char **outbuf, size_t *outbytesleft)
525 n = MIN(*inbytesleft, *outbytesleft);
527 memmove(*outbuf, *inbuf, n);
530 (*outbytesleft) -= n;
534 if (*inbytesleft > 0) {
542 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
543 char **outbuf, size_t *outbytesleft)
545 size_t in_left=*inbytesleft, out_left=*outbytesleft;
546 const uint8 *c = (const uint8 *)*inbuf;
547 uint8 *uc = (uint8 *)*outbuf;
549 while (in_left >= 1 && out_left >= 2) {
550 if ((c[0] & 0x80) == 0) {
560 if ((c[0] & 0xe0) == 0xc0) {
562 (c[1] & 0xc0) != 0x80) {
566 uc[1] = (c[0]>>2) & 0x7;
567 uc[0] = (c[0]<<6) | (c[1]&0x3f);
575 if ((c[0] & 0xf0) == 0xe0) {
577 (c[1] & 0xc0) != 0x80 ||
578 (c[2] & 0xc0) != 0x80) {
582 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
583 uc[0] = (c[1]<<6) | (c[2]&0x3f);
591 if ((c[0] & 0xf8) == 0xf0) {
592 unsigned int codepoint;
594 (c[1] & 0xc0) != 0x80 ||
595 (c[2] & 0xc0) != 0x80 ||
596 (c[3] & 0xc0) != 0x80) {
605 if (codepoint < 0x10000) {
606 /* accept UTF-8 characters that are not
607 minimally packed, but pack the result */
608 uc[0] = (codepoint & 0xFF);
609 uc[1] = (codepoint >> 8);
617 codepoint -= 0x10000;
624 uc[0] = (codepoint>>10) & 0xFF;
625 uc[1] = (codepoint>>18) | 0xd8;
626 uc[2] = codepoint & 0xFF;
627 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
635 /* we don't handle 5 byte sequences */
645 *inbytesleft = in_left;
646 *outbytesleft = out_left;
648 *outbuf = (char *)uc;
652 *inbytesleft = in_left;
653 *outbytesleft = out_left;
655 *outbuf = (char *)uc;
659 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
660 char **outbuf, size_t *outbytesleft)
662 size_t in_left=*inbytesleft, out_left=*outbytesleft;
663 uint8 *c = (uint8 *)*outbuf;
664 const uint8 *uc = (const uint8 *)*inbuf;
666 while (in_left >= 2 && out_left >= 1) {
667 unsigned int codepoint;
669 if (uc[1] == 0 && !(uc[0] & 0x80)) {
679 if ((uc[1]&0xf8) == 0) {
680 /* next simplest case */
685 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
686 c[1] = 0x80 | (uc[0] & 0x3f);
694 if ((uc[1] & 0xfc) == 0xdc) {
695 /* its the second part of a 4 byte sequence. Illegal */
704 if ((uc[1] & 0xfc) != 0xd8) {
705 codepoint = uc[0] | (uc[1]<<8);
710 c[0] = 0xe0 | (codepoint >> 12);
711 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
712 c[2] = 0x80 | (codepoint & 0x3f);
721 /* its the first part of a 4 byte sequence */
726 if ((uc[3] & 0xfc) != 0xdc) {
730 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
731 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
737 c[0] = 0xf0 | (codepoint >> 18);
738 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
739 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
740 c[3] = 0x80 | (codepoint & 0x3f);
758 *inbytesleft = in_left;
759 *outbytesleft = out_left;
766 *inbytesleft = in_left;
767 *outbytesleft = out_left;