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 ret->cd_direct = ret->cd_pull;
269 /* the general case has to go via a buffer */
270 if (!ret->pull) ret->pull = from->pull;
271 if (!ret->push) ret->push = to->push;
277 return (smb_iconv_t)-1;
281 simple iconv_open() wrapper
283 _PUBLIC_ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
285 return smb_iconv_open_ex(talloc_autofree_context(), tocode, fromcode, true);
289 simple iconv_close() wrapper
291 _PUBLIC_ int smb_iconv_close(smb_iconv_t cd)
298 /**********************************************************************
299 the following functions implement the builtin character sets in Samba
300 and also the "test" character sets that are designed to test
301 multi-byte character set support for english users
302 ***********************************************************************/
303 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
304 char **outbuf, size_t *outbytesleft)
306 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
307 (*outbuf)[0] = (*inbuf)[0];
310 (*outbytesleft) -= 2;
315 if (*inbytesleft > 0) {
323 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
324 char **outbuf, size_t *outbytesleft)
328 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
329 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
330 if ((*inbuf)[1]) ir_count++;
332 (*outbytesleft) -= 1;
337 if (*inbytesleft == 1) {
342 if (*inbytesleft > 1) {
351 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
352 char **outbuf, size_t *outbytesleft)
354 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
357 if ((*inbuf)[0] != '@') {
358 /* seven bit ascii case */
359 (*outbuf)[0] = (*inbuf)[0];
362 (*outbytesleft) -= 2;
367 /* it's a hex character */
368 if (*inbytesleft < 5) {
373 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
378 (*outbuf)[0] = v&0xff;
381 (*outbytesleft) -= 2;
386 if (*inbytesleft > 0) {
394 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
395 char **outbuf, size_t *outbytesleft)
397 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
400 if ((*inbuf)[1] == 0 &&
401 ((*inbuf)[0] & 0x80) == 0 &&
402 (*inbuf)[0] != '@') {
403 (*outbuf)[0] = (*inbuf)[0];
405 (*outbytesleft) -= 1;
410 if (*outbytesleft < 5) {
414 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
415 memcpy(*outbuf, buf, 5);
417 (*outbytesleft) -= 5;
422 if (*inbytesleft == 1) {
427 if (*inbytesleft > 1) {
435 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
436 char **outbuf, size_t *outbytesleft)
440 n = MIN(*inbytesleft, *outbytesleft);
442 swab(*inbuf, *outbuf, (n&~1));
448 (*outbytesleft) -= n;
452 if (*inbytesleft > 0) {
461 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
462 char **outbuf, size_t *outbytesleft)
466 n = MIN(*inbytesleft, *outbytesleft);
468 memmove(*outbuf, *inbuf, n);
471 (*outbytesleft) -= n;
475 if (*inbytesleft > 0) {
484 this takes a UTF8 sequence and produces a UTF16 sequence
486 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
487 char **outbuf, size_t *outbytesleft)
489 size_t in_left=*inbytesleft, out_left=*outbytesleft;
490 const uint8_t *c = (const uint8_t *)*inbuf;
491 uint8_t *uc = (uint8_t *)*outbuf;
493 while (in_left >= 1 && out_left >= 2) {
494 if ((c[0] & 0x80) == 0) {
504 if ((c[0] & 0xe0) == 0xc0) {
506 (c[1] & 0xc0) != 0x80) {
510 uc[1] = (c[0]>>2) & 0x7;
511 uc[0] = (c[0]<<6) | (c[1]&0x3f);
519 if ((c[0] & 0xf0) == 0xe0) {
521 (c[1] & 0xc0) != 0x80 ||
522 (c[2] & 0xc0) != 0x80) {
526 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
527 uc[0] = (c[1]<<6) | (c[2]&0x3f);
535 if ((c[0] & 0xf8) == 0xf0) {
536 unsigned int codepoint;
538 (c[1] & 0xc0) != 0x80 ||
539 (c[2] & 0xc0) != 0x80 ||
540 (c[3] & 0xc0) != 0x80) {
549 if (codepoint < 0x10000) {
550 /* accept UTF-8 characters that are not
551 minimally packed, but pack the result */
552 uc[0] = (codepoint & 0xFF);
553 uc[1] = (codepoint >> 8);
561 codepoint -= 0x10000;
568 uc[0] = (codepoint>>10) & 0xFF;
569 uc[1] = (codepoint>>18) | 0xd8;
570 uc[2] = codepoint & 0xFF;
571 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
579 /* we don't handle 5 byte sequences */
589 *inbytesleft = in_left;
590 *outbytesleft = out_left;
591 *inbuf = (const char *)c;
592 *outbuf = (char *)uc;
596 *inbytesleft = in_left;
597 *outbytesleft = out_left;
598 *inbuf = (const char *)c;
599 *outbuf = (char *)uc;
605 this takes a UTF16 sequence and produces a UTF8 sequence
607 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
608 char **outbuf, size_t *outbytesleft)
610 size_t in_left=*inbytesleft, out_left=*outbytesleft;
611 uint8_t *c = (uint8_t *)*outbuf;
612 const uint8_t *uc = (const uint8_t *)*inbuf;
614 while (in_left >= 2 && out_left >= 1) {
615 unsigned int codepoint;
617 if (uc[1] == 0 && !(uc[0] & 0x80)) {
627 if ((uc[1]&0xf8) == 0) {
628 /* next simplest case */
633 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
634 c[1] = 0x80 | (uc[0] & 0x3f);
642 if ((uc[1] & 0xfc) == 0xdc) {
643 /* its the second part of a 4 byte sequence. Illegal */
652 if ((uc[1] & 0xfc) != 0xd8) {
653 codepoint = uc[0] | (uc[1]<<8);
658 c[0] = 0xe0 | (codepoint >> 12);
659 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
660 c[2] = 0x80 | (codepoint & 0x3f);
669 /* its the first part of a 4 byte sequence */
674 if ((uc[3] & 0xfc) != 0xdc) {
678 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
679 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
685 c[0] = 0xf0 | (codepoint >> 18);
686 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
687 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
688 c[3] = 0x80 | (codepoint & 0x3f);
706 *inbytesleft = in_left;
707 *outbytesleft = out_left;
708 *inbuf = (const char *)uc;
714 *inbytesleft = in_left;
715 *outbytesleft = out_left;
716 *inbuf = (const char *)uc;
723 this takes a UTF16 munged sequence, modifies it according to the
724 string2key rules, and produces a UTF16 sequence
728 1) any 0x0000 characters are mapped to 0x0001
730 2) convert any instance of 0xD800 - 0xDBFF (high surrogate)
731 without an immediately following 0xDC00 - 0x0xDFFF (low surrogate) to
732 U+FFFD (OBJECT REPLACEMENT CHARACTER).
734 3) the same for any low surrogate that was not preceded by a high surrogate.
737 static size_t utf16_munged_pull(void *cd, const char **inbuf, size_t *inbytesleft,
738 char **outbuf, size_t *outbytesleft)
740 size_t in_left=*inbytesleft, out_left=*outbytesleft;
741 uint8_t *c = (uint8_t *)*outbuf;
742 const uint8_t *uc = (const uint8_t *)*inbuf;
744 while (in_left >= 2 && out_left >= 2) {
745 unsigned int codepoint = uc[0] | (uc[1]<<8);
747 if (codepoint == 0) {
751 if ((codepoint & 0xfc00) == 0xd800) {
752 /* a high surrogate */
753 unsigned int codepoint2;
758 codepoint2 = uc[2] | (uc[3]<<8);
759 if ((codepoint2 & 0xfc00) != 0xdc00) {
760 /* high surrogate not followed by low
761 surrogate: convert to 0xfffd */
777 if ((codepoint & 0xfc00) == 0xdc00) {
778 /* low surrogate not preceded by high
779 surrogate: convert to 0xfffd */
784 c[0] = codepoint & 0xFF;
785 c[1] = (codepoint>>8) & 0xFF;
804 *inbytesleft = in_left;
805 *outbytesleft = out_left;
806 *inbuf = (const char *)uc;
812 *inbytesleft = in_left;
813 *outbytesleft = out_left;
814 *inbuf = (const char *)uc;