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 utf8_munged_push(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 {"UTF8_MUNGED", utf8_pull, utf8_munged_push},
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;
163 _PUBLIC_ smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode,
164 const char *fromcode, bool native_iconv)
167 const struct charset_functions *from=NULL, *to=NULL;
170 ret = (smb_iconv_t)talloc_named(mem_ctx,
172 "iconv(%s,%s)", tocode, fromcode);
175 return (smb_iconv_t)-1;
177 memset(ret, 0, sizeof(*ret));
179 /* check for the simplest null conversion */
180 if (strcmp(fromcode, tocode) == 0) {
181 ret->direct = iconv_copy;
185 for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
186 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
187 from = &builtin_functions[i];
189 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
190 to = &builtin_functions[i];
195 for (from=charsets; from; from=from->next) {
196 if (strcasecmp(from->name, fromcode) == 0) break;
201 for (to=charsets; to; to=to->next) {
202 if (strcasecmp(to->name, tocode) == 0) break;
206 #ifdef HAVE_NATIVE_ICONV
207 if ((!from || !to) && !native_iconv) {
211 ret->pull = sys_iconv;
212 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
213 if (ret->cd_pull == (iconv_t)-1)
214 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
215 if (ret->cd_pull == (iconv_t)-1) goto failed;
219 ret->push = sys_iconv;
220 ret->cd_push = iconv_open(tocode, "UTF-16LE");
221 if (ret->cd_push == (iconv_t)-1)
222 ret->cd_push = iconv_open(tocode, "UCS-2LE");
223 if (ret->cd_push == (iconv_t)-1) goto failed;
231 /* check for conversion to/from ucs2 */
232 if (is_utf16(fromcode) && to) {
233 ret->direct = to->push;
236 if (is_utf16(tocode) && from) {
237 ret->direct = from->pull;
241 #ifdef HAVE_NATIVE_ICONV
242 if (is_utf16(fromcode)) {
243 ret->direct = sys_iconv;
244 ret->cd_direct = ret->cd_push;
248 if (is_utf16(tocode)) {
249 ret->direct = sys_iconv;
250 ret->cd_direct = ret->cd_pull;
256 /* the general case has to go via a buffer */
257 if (!ret->pull) ret->pull = from->pull;
258 if (!ret->push) ret->push = to->push;
264 return (smb_iconv_t)-1;
268 simple iconv_open() wrapper
270 _PUBLIC_ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
272 return smb_iconv_open_ex(NULL, tocode, fromcode, true);
276 simple iconv_close() wrapper
278 _PUBLIC_ int smb_iconv_close(smb_iconv_t cd)
280 #ifdef HAVE_NATIVE_ICONV
281 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
282 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
283 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
291 /**********************************************************************
292 the following functions implement the builtin character sets in Samba
293 and also the "test" character sets that are designed to test
294 multi-byte character set support for english users
295 ***********************************************************************/
296 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
297 char **outbuf, size_t *outbytesleft)
299 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
300 (*outbuf)[0] = (*inbuf)[0];
303 (*outbytesleft) -= 2;
308 if (*inbytesleft > 0) {
316 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
317 char **outbuf, size_t *outbytesleft)
321 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
322 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
323 if ((*inbuf)[1]) ir_count++;
325 (*outbytesleft) -= 1;
330 if (*inbytesleft == 1) {
335 if (*inbytesleft > 1) {
344 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
345 char **outbuf, size_t *outbytesleft)
347 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
350 if ((*inbuf)[0] != '@') {
351 /* seven bit ascii case */
352 (*outbuf)[0] = (*inbuf)[0];
355 (*outbytesleft) -= 2;
360 /* it's a hex character */
361 if (*inbytesleft < 5) {
366 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
371 (*outbuf)[0] = v&0xff;
374 (*outbytesleft) -= 2;
379 if (*inbytesleft > 0) {
387 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
388 char **outbuf, size_t *outbytesleft)
390 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
393 if ((*inbuf)[1] == 0 &&
394 ((*inbuf)[0] & 0x80) == 0 &&
395 (*inbuf)[0] != '@') {
396 (*outbuf)[0] = (*inbuf)[0];
398 (*outbytesleft) -= 1;
403 if (*outbytesleft < 5) {
407 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
408 memcpy(*outbuf, buf, 5);
410 (*outbytesleft) -= 5;
415 if (*inbytesleft == 1) {
420 if (*inbytesleft > 1) {
428 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
429 char **outbuf, size_t *outbytesleft)
433 n = MIN(*inbytesleft, *outbytesleft);
435 swab(*inbuf, *outbuf, (n&~1));
441 (*outbytesleft) -= n;
445 if (*inbytesleft > 0) {
454 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
455 char **outbuf, size_t *outbytesleft)
459 n = MIN(*inbytesleft, *outbytesleft);
461 memmove(*outbuf, *inbuf, n);
464 (*outbytesleft) -= n;
468 if (*inbytesleft > 0) {
477 this takes a UTF8 sequence and produces a UTF16 sequence
479 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
480 char **outbuf, size_t *outbytesleft)
482 size_t in_left=*inbytesleft, out_left=*outbytesleft;
483 const uint8_t *c = (const uint8_t *)*inbuf;
484 uint8_t *uc = (uint8_t *)*outbuf;
486 while (in_left >= 1 && out_left >= 2) {
487 if ((c[0] & 0x80) == 0) {
497 if ((c[0] & 0xe0) == 0xc0) {
499 (c[1] & 0xc0) != 0x80) {
503 uc[1] = (c[0]>>2) & 0x7;
504 uc[0] = (c[0]<<6) | (c[1]&0x3f);
512 if ((c[0] & 0xf0) == 0xe0) {
514 (c[1] & 0xc0) != 0x80 ||
515 (c[2] & 0xc0) != 0x80) {
519 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
520 uc[0] = (c[1]<<6) | (c[2]&0x3f);
528 if ((c[0] & 0xf8) == 0xf0) {
529 unsigned int codepoint;
531 (c[1] & 0xc0) != 0x80 ||
532 (c[2] & 0xc0) != 0x80 ||
533 (c[3] & 0xc0) != 0x80) {
542 if (codepoint < 0x10000) {
543 /* accept UTF-8 characters that are not
544 minimally packed, but pack the result */
545 uc[0] = (codepoint & 0xFF);
546 uc[1] = (codepoint >> 8);
554 codepoint -= 0x10000;
561 uc[0] = (codepoint>>10) & 0xFF;
562 uc[1] = (codepoint>>18) | 0xd8;
563 uc[2] = codepoint & 0xFF;
564 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
572 /* we don't handle 5 byte sequences */
582 *inbytesleft = in_left;
583 *outbytesleft = out_left;
584 *inbuf = (const char *)c;
585 *outbuf = (char *)uc;
589 *inbytesleft = in_left;
590 *outbytesleft = out_left;
591 *inbuf = (const char *)c;
592 *outbuf = (char *)uc;
598 this takes a UTF16 sequence and produces a UTF8 sequence
600 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
601 char **outbuf, size_t *outbytesleft)
603 size_t in_left=*inbytesleft, out_left=*outbytesleft;
604 uint8_t *c = (uint8_t *)*outbuf;
605 const uint8_t *uc = (const uint8_t *)*inbuf;
607 while (in_left >= 2 && out_left >= 1) {
608 unsigned int codepoint;
610 if (uc[1] == 0 && !(uc[0] & 0x80)) {
620 if ((uc[1]&0xf8) == 0) {
621 /* next simplest case */
626 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
627 c[1] = 0x80 | (uc[0] & 0x3f);
635 if ((uc[1] & 0xfc) == 0xdc) {
636 /* its the second part of a 4 byte sequence. Illegal */
645 if ((uc[1] & 0xfc) != 0xd8) {
646 codepoint = uc[0] | (uc[1]<<8);
651 c[0] = 0xe0 | (codepoint >> 12);
652 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
653 c[2] = 0x80 | (codepoint & 0x3f);
662 /* its the first part of a 4 byte sequence */
667 if ((uc[3] & 0xfc) != 0xdc) {
671 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
672 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
678 c[0] = 0xf0 | (codepoint >> 18);
679 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
680 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
681 c[3] = 0x80 | (codepoint & 0x3f);
699 *inbytesleft = in_left;
700 *outbytesleft = out_left;
701 *inbuf = (const char *)uc;
707 *inbytesleft = in_left;
708 *outbytesleft = out_left;
709 *inbuf = (const char *)uc;
716 this takes a UTF16 sequence, munges it according to the string2key
717 rules, and produces a UTF8 sequence
721 1) convert any instance of 0xD800 - 0xDBFF (high surrogate)
722 without an immediately following 0xDC00 - 0x0xDFFF (low surrogate) to
723 U+FFFD (OBJECT REPLACEMENT CHARACTER).
725 2) the same for any low surrogate that was not preceded by a high surrogate.
727 static size_t utf8_munged_push(void *cd, const char **inbuf, size_t *inbytesleft,
728 char **outbuf, size_t *outbytesleft)
730 size_t in_left=*inbytesleft, out_left=*outbytesleft;
731 uint8_t *c = (uint8_t *)*outbuf;
732 const uint8_t *uc = (const uint8_t *)*inbuf;
734 while (in_left >= 2 && out_left >= 1) {
735 unsigned int codepoint;
737 if (uc[1] == 0 && !(uc[0] & 0x80)) {
747 if ((uc[1]&0xf8) == 0) {
748 /* next simplest case */
753 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
754 c[1] = 0x80 | (uc[0] & 0x3f);
762 if ((uc[1] & 0xfc) == 0xdc) {
763 /* low surrogate not preceded by high surrogate
769 if ((uc[1] & 0xfc) != 0xd8) {
770 codepoint = uc[0] | (uc[1]<<8);
774 /* its the first part of a 4 byte sequence */
775 if (in_left < 4 || (uc[3] & 0xfc) != 0xdc) {
776 /* high surrogate not followed by low surrogate
782 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
783 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
789 c[0] = 0xf0 | (codepoint >> 18);
790 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
791 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
792 c[3] = 0x80 | (codepoint & 0x3f);
805 c[0] = 0xe0 | (codepoint >> 12);
806 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
807 c[2] = 0x80 | (codepoint & 0x3f);
826 *inbytesleft = in_left;
827 *outbytesleft = out_left;
828 *inbuf = (const char *)uc;
834 *inbytesleft = in_left;
835 *outbytesleft = out_left;
836 *inbuf = (const char *)uc;