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"
31 * @brief Samba wrapper/stub for iconv character set conversion.
33 * iconv is the XPG2 interface for converting between character
34 * encodings. This file provides a Samba wrapper around it, and also
35 * a simple reimplementation that is used if the system does not
38 * Samba only works with encodings that are supersets of ASCII: ascii
39 * characters like whitespace can be tested for directly, multibyte
40 * sequences start with a byte with the high bit set, and strings are
41 * terminated by a nul byte.
43 * Note that the only function provided by iconv is conversion between
44 * characters. It doesn't directly support operations like
45 * uppercasing or comparison. We have to convert to UTF-16LE and
48 * @sa Samba Developers Guide
51 static size_t ascii_pull (void *,const char **, size_t *, char **, size_t *);
52 static size_t ascii_push (void *,const char **, size_t *, char **, size_t *);
53 static size_t latin1_push (void *,const char **, size_t *, char **, size_t *);
54 static size_t utf8_pull (void *,const char **, size_t *, char **, size_t *);
55 static size_t utf8_push (void *,const char **, size_t *, char **, size_t *);
56 static size_t utf16_munged_pull(void *,const char **, size_t *, char **, size_t *);
57 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
58 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
59 static size_t iconv_copy (void *,const char **, size_t *, char **, size_t *);
60 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
62 static const struct charset_functions builtin_functions[] = {
63 /* windows is closest to UTF-16 */
64 {"UCS-2LE", iconv_copy, iconv_copy},
65 {"UTF-16LE", iconv_copy, iconv_copy},
66 {"UCS-2BE", iconv_swab, iconv_swab},
67 {"UTF-16BE", iconv_swab, iconv_swab},
69 /* we include the UTF-8 alias to cope with differing locale settings */
70 {"UTF8", utf8_pull, utf8_push},
71 {"UTF-8", utf8_pull, utf8_push},
73 /* this handles the munging needed for String2Key */
74 {"UTF16_MUNGED", utf16_munged_pull, iconv_copy},
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}
82 static struct charset_functions *charsets = NULL;
84 bool charset_register_backend(const void *_funcs)
86 struct charset_functions *funcs = (struct charset_functions *)memdup(_funcs,sizeof(struct charset_functions));
87 struct charset_functions *c;
89 /* Check whether we already have this charset... */
90 for (c = charsets; c != NULL; c = c->next) {
91 if(!strcasecmp(c->name, funcs->name)) {
92 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
97 funcs->next = funcs->prev = NULL;
98 DLIST_ADD(charsets, funcs);
102 #ifdef HAVE_NATIVE_ICONV
103 /* if there was an error then reset the internal state,
104 this ensures that we don't have a shift state remaining for
105 character sets like SJIS */
106 static size_t sys_iconv(void *cd,
107 const char **inbuf, size_t *inbytesleft,
108 char **outbuf, size_t *outbytesleft)
110 size_t ret = iconv((iconv_t)cd,
111 discard_const_p(char *, inbuf), inbytesleft,
112 outbuf, outbytesleft);
113 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
119 * This is a simple portable iconv() implementaion.
121 * It only knows about a very small number of character sets - just
122 * enough that Samba works on systems that don't have iconv.
124 _PUBLIC_ size_t smb_iconv(smb_iconv_t cd,
125 const char **inbuf, size_t *inbytesleft,
126 char **outbuf, size_t *outbytesleft)
131 /* in many cases we can go direct */
133 return cd->direct(cd->cd_direct,
134 inbuf, inbytesleft, outbuf, outbytesleft);
138 /* otherwise we have to do it chunks at a time */
139 while (*inbytesleft > 0) {
140 char *bufp1 = cvtbuf;
141 const char *bufp2 = cvtbuf;
143 bufsize = sizeof(cvtbuf);
145 if (cd->pull(cd->cd_pull,
146 inbuf, inbytesleft, &bufp1, &bufsize) == -1
147 && errno != E2BIG) return -1;
149 bufsize = sizeof(cvtbuf) - bufsize;
151 if (cd->push(cd->cd_push,
153 outbuf, outbytesleft) == -1) return -1;
159 static bool is_utf16(const char *name)
161 return strcasecmp(name, "UCS-2LE") == 0 ||
162 strcasecmp(name, "UTF-16LE") == 0;
167 _PUBLIC_ smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode,
168 const char *fromcode, bool native_iconv)
171 const struct charset_functions *from=NULL, *to=NULL;
174 ret = (smb_iconv_t)talloc_named(mem_ctx,
176 "iconv(%s,%s)", tocode, fromcode);
179 return (smb_iconv_t)-1;
181 memset(ret, 0, sizeof(*ret));
183 /* check for the simplest null conversion */
184 if (strcmp(fromcode, tocode) == 0) {
185 ret->direct = iconv_copy;
189 for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
190 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
191 from = &builtin_functions[i];
193 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
194 to = &builtin_functions[i];
199 for (from=charsets; from; from=from->next) {
200 if (strcasecmp(from->name, fromcode) == 0) break;
205 for (to=charsets; to; to=to->next) {
206 if (strcasecmp(to->name, tocode) == 0) break;
210 #ifdef HAVE_NATIVE_ICONV
211 if ((!from || !to) && !native_iconv) {
215 ret->pull = sys_iconv;
216 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
217 if (ret->cd_pull == (iconv_t)-1)
218 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
219 if (ret->cd_pull == (iconv_t)-1) goto failed;
223 ret->push = sys_iconv;
224 ret->cd_push = iconv_open(tocode, "UTF-16LE");
225 if (ret->cd_push == (iconv_t)-1)
226 ret->cd_push = iconv_open(tocode, "UCS-2LE");
227 if (ret->cd_push == (iconv_t)-1) goto failed;
235 /* check for conversion to/from ucs2 */
236 if (is_utf16(fromcode) && to) {
237 ret->direct = to->push;
240 if (is_utf16(tocode) && from) {
241 ret->direct = from->pull;
245 #ifdef HAVE_NATIVE_ICONV
246 if (is_utf16(fromcode)) {
247 ret->direct = sys_iconv;
248 ret->cd_direct = ret->cd_push;
252 if (is_utf16(tocode)) {
253 ret->direct = sys_iconv;
254 ret->cd_direct = ret->cd_pull;
260 /* the general case has to go via a buffer */
261 if (!ret->pull) ret->pull = from->pull;
262 if (!ret->push) ret->push = to->push;
268 return (smb_iconv_t)-1;
272 simple iconv_open() wrapper
274 _PUBLIC_ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
276 return smb_iconv_open_ex(NULL, tocode, fromcode, true);
280 simple iconv_close() wrapper
282 _PUBLIC_ int smb_iconv_close(smb_iconv_t cd)
284 #ifdef HAVE_NATIVE_ICONV
285 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
286 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
287 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
295 /**********************************************************************
296 the following functions implement the builtin character sets in Samba
297 and also the "test" character sets that are designed to test
298 multi-byte character set support for english users
299 ***********************************************************************/
300 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
301 char **outbuf, size_t *outbytesleft)
303 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
304 (*outbuf)[0] = (*inbuf)[0];
307 (*outbytesleft) -= 2;
312 if (*inbytesleft > 0) {
320 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
321 char **outbuf, size_t *outbytesleft)
325 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
326 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
327 if ((*inbuf)[1]) ir_count++;
329 (*outbytesleft) -= 1;
334 if (*inbytesleft == 1) {
339 if (*inbytesleft > 1) {
347 static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
348 char **outbuf, size_t *outbytesleft)
352 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
353 (*outbuf)[0] = (*inbuf)[0];
354 if ((*inbuf)[1]) ir_count++;
356 (*outbytesleft) -= 1;
361 if (*inbytesleft == 1) {
366 if (*inbytesleft > 1) {
374 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
375 char **outbuf, size_t *outbytesleft)
377 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
380 if ((*inbuf)[0] != '@') {
381 /* seven bit ascii case */
382 (*outbuf)[0] = (*inbuf)[0];
385 (*outbytesleft) -= 2;
390 /* it's a hex character */
391 if (*inbytesleft < 5) {
396 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
401 (*outbuf)[0] = v&0xff;
404 (*outbytesleft) -= 2;
409 if (*inbytesleft > 0) {
417 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
418 char **outbuf, size_t *outbytesleft)
420 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
423 if ((*inbuf)[1] == 0 &&
424 ((*inbuf)[0] & 0x80) == 0 &&
425 (*inbuf)[0] != '@') {
426 (*outbuf)[0] = (*inbuf)[0];
428 (*outbytesleft) -= 1;
433 if (*outbytesleft < 5) {
437 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
438 memcpy(*outbuf, buf, 5);
440 (*outbytesleft) -= 5;
445 if (*inbytesleft == 1) {
450 if (*inbytesleft > 1) {
458 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
459 char **outbuf, size_t *outbytesleft)
463 n = MIN(*inbytesleft, *outbytesleft);
465 swab(*inbuf, *outbuf, (n&~1));
471 (*outbytesleft) -= n;
475 if (*inbytesleft > 0) {
484 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
485 char **outbuf, size_t *outbytesleft)
489 n = MIN(*inbytesleft, *outbytesleft);
491 memmove(*outbuf, *inbuf, n);
494 (*outbytesleft) -= n;
498 if (*inbytesleft > 0) {
507 this takes a UTF8 sequence and produces a UTF16 sequence
509 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
510 char **outbuf, size_t *outbytesleft)
512 size_t in_left=*inbytesleft, out_left=*outbytesleft;
513 const uint8_t *c = (const uint8_t *)*inbuf;
514 uint8_t *uc = (uint8_t *)*outbuf;
516 while (in_left >= 1 && out_left >= 2) {
517 if ((c[0] & 0x80) == 0) {
527 if ((c[0] & 0xe0) == 0xc0) {
529 (c[1] & 0xc0) != 0x80) {
533 uc[1] = (c[0]>>2) & 0x7;
534 uc[0] = (c[0]<<6) | (c[1]&0x3f);
542 if ((c[0] & 0xf0) == 0xe0) {
544 (c[1] & 0xc0) != 0x80 ||
545 (c[2] & 0xc0) != 0x80) {
549 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
550 uc[0] = (c[1]<<6) | (c[2]&0x3f);
558 if ((c[0] & 0xf8) == 0xf0) {
559 unsigned int codepoint;
561 (c[1] & 0xc0) != 0x80 ||
562 (c[2] & 0xc0) != 0x80 ||
563 (c[3] & 0xc0) != 0x80) {
572 if (codepoint < 0x10000) {
573 /* accept UTF-8 characters that are not
574 minimally packed, but pack the result */
575 uc[0] = (codepoint & 0xFF);
576 uc[1] = (codepoint >> 8);
584 codepoint -= 0x10000;
591 uc[0] = (codepoint>>10) & 0xFF;
592 uc[1] = (codepoint>>18) | 0xd8;
593 uc[2] = codepoint & 0xFF;
594 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
602 /* we don't handle 5 byte sequences */
612 *inbytesleft = in_left;
613 *outbytesleft = out_left;
614 *inbuf = (const char *)c;
615 *outbuf = (char *)uc;
619 *inbytesleft = in_left;
620 *outbytesleft = out_left;
621 *inbuf = (const char *)c;
622 *outbuf = (char *)uc;
628 this takes a UTF16 sequence and produces a UTF8 sequence
630 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
631 char **outbuf, size_t *outbytesleft)
633 size_t in_left=*inbytesleft, out_left=*outbytesleft;
634 uint8_t *c = (uint8_t *)*outbuf;
635 const uint8_t *uc = (const uint8_t *)*inbuf;
637 while (in_left >= 2 && out_left >= 1) {
638 unsigned int codepoint;
640 if (uc[1] == 0 && !(uc[0] & 0x80)) {
650 if ((uc[1]&0xf8) == 0) {
651 /* next simplest case */
656 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
657 c[1] = 0x80 | (uc[0] & 0x3f);
665 if ((uc[1] & 0xfc) == 0xdc) {
666 /* its the second part of a 4 byte sequence. Illegal */
675 if ((uc[1] & 0xfc) != 0xd8) {
676 codepoint = uc[0] | (uc[1]<<8);
681 c[0] = 0xe0 | (codepoint >> 12);
682 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
683 c[2] = 0x80 | (codepoint & 0x3f);
692 /* its the first part of a 4 byte sequence */
697 if ((uc[3] & 0xfc) != 0xdc) {
701 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
702 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
708 c[0] = 0xf0 | (codepoint >> 18);
709 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
710 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
711 c[3] = 0x80 | (codepoint & 0x3f);
729 *inbytesleft = in_left;
730 *outbytesleft = out_left;
731 *inbuf = (const char *)uc;
737 *inbytesleft = in_left;
738 *outbytesleft = out_left;
739 *inbuf = (const char *)uc;
746 this takes a UTF16 munged sequence, modifies it according to the
747 string2key rules, and produces a UTF16 sequence
751 1) any 0x0000 characters are mapped to 0x0001
753 2) convert any instance of 0xD800 - 0xDBFF (high surrogate)
754 without an immediately following 0xDC00 - 0x0xDFFF (low surrogate) to
755 U+FFFD (OBJECT REPLACEMENT CHARACTER).
757 3) the same for any low surrogate that was not preceded by a high surrogate.
760 static size_t utf16_munged_pull(void *cd, const char **inbuf, size_t *inbytesleft,
761 char **outbuf, size_t *outbytesleft)
763 size_t in_left=*inbytesleft, out_left=*outbytesleft;
764 uint8_t *c = (uint8_t *)*outbuf;
765 const uint8_t *uc = (const uint8_t *)*inbuf;
767 while (in_left >= 2 && out_left >= 2) {
768 unsigned int codepoint = uc[0] | (uc[1]<<8);
770 if (codepoint == 0) {
774 if ((codepoint & 0xfc00) == 0xd800) {
775 /* a high surrogate */
776 unsigned int codepoint2;
781 codepoint2 = uc[2] | (uc[3]<<8);
782 if ((codepoint2 & 0xfc00) != 0xdc00) {
783 /* high surrogate not followed by low
784 surrogate: convert to 0xfffd */
800 if ((codepoint & 0xfc00) == 0xdc00) {
801 /* low surrogate not preceded by high
802 surrogate: convert to 0xfffd */
807 c[0] = codepoint & 0xFF;
808 c[1] = (codepoint>>8) & 0xFF;
827 *inbytesleft = in_left;
828 *outbytesleft = out_left;
829 *inbuf = (const char *)uc;
835 *inbytesleft = in_left;
836 *outbytesleft = out_left;
837 *inbuf = (const char *)uc;