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 ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
55 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
56 static size_t iconv_copy (void *,const char **, size_t *, char **, size_t *);
57 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
59 static const struct charset_functions builtin_functions[] = {
60 /* windows is closest to UTF-16 */
61 {"UCS-2LE", iconv_copy, iconv_copy},
62 {"UTF-16LE", iconv_copy, iconv_copy},
63 {"UCS-2BE", iconv_swab, iconv_swab},
64 {"UTF-16BE", iconv_swab, iconv_swab},
66 /* we include the UTF-8 alias to cope with differing locale settings */
67 {"UTF8", utf8_pull, utf8_push},
68 {"UTF-8", utf8_pull, utf8_push},
69 {"ASCII", ascii_pull, ascii_push},
70 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push}
73 static struct charset_functions *charsets = NULL;
75 bool charset_register_backend(const void *_funcs)
77 struct charset_functions *funcs = memdup(_funcs,sizeof(struct charset_functions));
78 struct charset_functions *c;
80 /* Check whether we already have this charset... */
81 for (c = charsets; c != NULL; c = c->next) {
82 if(!strcasecmp(c->name, funcs->name)) {
83 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
88 funcs->next = funcs->prev = NULL;
89 DLIST_ADD(charsets, funcs);
93 #ifdef HAVE_NATIVE_ICONV
94 /* if there was an error then reset the internal state,
95 this ensures that we don't have a shift state remaining for
96 character sets like SJIS */
97 static size_t sys_iconv(void *cd,
98 const char **inbuf, size_t *inbytesleft,
99 char **outbuf, size_t *outbytesleft)
101 size_t ret = iconv((iconv_t)cd,
102 discard_const_p(char *, inbuf), inbytesleft,
103 outbuf, outbytesleft);
104 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
110 * This is a simple portable iconv() implementaion.
112 * It only knows about a very small number of character sets - just
113 * enough that Samba works on systems that don't have iconv.
115 size_t smb_iconv(smb_iconv_t cd,
116 const char **inbuf, size_t *inbytesleft,
117 char **outbuf, size_t *outbytesleft)
122 /* in many cases we can go direct */
124 return cd->direct(cd->cd_direct,
125 inbuf, inbytesleft, outbuf, outbytesleft);
129 /* otherwise we have to do it chunks at a time */
130 while (*inbytesleft > 0) {
131 char *bufp1 = cvtbuf;
132 const char *bufp2 = cvtbuf;
134 bufsize = sizeof(cvtbuf);
136 if (cd->pull(cd->cd_pull,
137 inbuf, inbytesleft, &bufp1, &bufsize) == -1
138 && errno != E2BIG) return -1;
140 bufsize = sizeof(cvtbuf) - bufsize;
142 if (cd->push(cd->cd_push,
144 outbuf, outbytesleft) == -1) return -1;
150 static bool is_utf16(const char *name)
152 return strcasecmp(name, "UCS-2LE") == 0 ||
153 strcasecmp(name, "UTF-16LE") == 0;
157 simple iconv_open() wrapper
159 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
162 const struct charset_functions *from=NULL, *to=NULL;
165 ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret),
166 "iconv(%s,%s)", tocode, fromcode);
169 return (smb_iconv_t)-1;
171 memset(ret, 0, sizeof(*ret));
173 /* check for the simplest null conversion */
174 if (strcmp(fromcode, tocode) == 0) {
175 ret->direct = iconv_copy;
179 for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
180 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
181 from = &builtin_functions[i];
183 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
184 to = &builtin_functions[i];
189 for (from=charsets; from; from=from->next) {
190 if (strcasecmp(from->name, fromcode) == 0) break;
195 for (to=charsets; to; to=to->next) {
196 if (strcasecmp(to->name, tocode) == 0) break;
200 #ifdef HAVE_NATIVE_ICONV
201 if ((!from || !to) && !lp_parm_bool(-1, "iconv", "native", true)) {
205 ret->pull = sys_iconv;
206 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
207 if (ret->cd_pull == (iconv_t)-1)
208 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
209 if (ret->cd_pull == (iconv_t)-1) goto failed;
213 ret->push = sys_iconv;
214 ret->cd_push = iconv_open(tocode, "UTF-16LE");
215 if (ret->cd_push == (iconv_t)-1)
216 ret->cd_push = iconv_open(tocode, "UCS-2LE");
217 if (ret->cd_push == (iconv_t)-1) goto failed;
225 /* check for conversion to/from ucs2 */
226 if (is_utf16(fromcode) && to) {
227 ret->direct = to->push;
230 if (is_utf16(tocode) && from) {
231 ret->direct = from->pull;
235 #ifdef HAVE_NATIVE_ICONV
236 if (is_utf16(fromcode)) {
237 ret->direct = sys_iconv;
238 ret->cd_direct = ret->cd_push;
242 if (is_utf16(tocode)) {
243 ret->direct = sys_iconv;
244 ret->cd_direct = ret->cd_pull;
250 /* the general case has to go via a buffer */
251 if (!ret->pull) ret->pull = from->pull;
252 if (!ret->push) ret->push = to->push;
258 return (smb_iconv_t)-1;
262 simple iconv_close() wrapper
264 int smb_iconv_close(smb_iconv_t cd)
266 #ifdef HAVE_NATIVE_ICONV
267 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
268 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
269 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
277 /**********************************************************************
278 the following functions implement the builtin character sets in Samba
279 and also the "test" character sets that are designed to test
280 multi-byte character set support for english users
281 ***********************************************************************/
282 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
283 char **outbuf, size_t *outbytesleft)
285 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
286 (*outbuf)[0] = (*inbuf)[0];
289 (*outbytesleft) -= 2;
294 if (*inbytesleft > 0) {
302 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
303 char **outbuf, size_t *outbytesleft)
307 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
308 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
309 if ((*inbuf)[1]) ir_count++;
311 (*outbytesleft) -= 1;
316 if (*inbytesleft == 1) {
321 if (*inbytesleft > 1) {
330 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
331 char **outbuf, size_t *outbytesleft)
333 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
336 if ((*inbuf)[0] != '@') {
337 /* seven bit ascii case */
338 (*outbuf)[0] = (*inbuf)[0];
341 (*outbytesleft) -= 2;
346 /* it's a hex character */
347 if (*inbytesleft < 5) {
352 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
357 (*outbuf)[0] = v&0xff;
360 (*outbytesleft) -= 2;
365 if (*inbytesleft > 0) {
373 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
374 char **outbuf, size_t *outbytesleft)
376 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
379 if ((*inbuf)[1] == 0 &&
380 ((*inbuf)[0] & 0x80) == 0 &&
381 (*inbuf)[0] != '@') {
382 (*outbuf)[0] = (*inbuf)[0];
384 (*outbytesleft) -= 1;
389 if (*outbytesleft < 5) {
393 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
394 memcpy(*outbuf, buf, 5);
396 (*outbytesleft) -= 5;
401 if (*inbytesleft == 1) {
406 if (*inbytesleft > 1) {
414 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
415 char **outbuf, size_t *outbytesleft)
419 n = MIN(*inbytesleft, *outbytesleft);
421 swab(*inbuf, *outbuf, (n&~1));
427 (*outbytesleft) -= n;
431 if (*inbytesleft > 0) {
440 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
441 char **outbuf, size_t *outbytesleft)
445 n = MIN(*inbytesleft, *outbytesleft);
447 memmove(*outbuf, *inbuf, n);
450 (*outbytesleft) -= n;
454 if (*inbytesleft > 0) {
462 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
463 char **outbuf, size_t *outbytesleft)
465 size_t in_left=*inbytesleft, out_left=*outbytesleft;
466 const uint8_t *c = (const uint8_t *)*inbuf;
467 uint8_t *uc = (uint8_t *)*outbuf;
469 while (in_left >= 1 && out_left >= 2) {
470 if ((c[0] & 0x80) == 0) {
480 if ((c[0] & 0xe0) == 0xc0) {
482 (c[1] & 0xc0) != 0x80) {
486 uc[1] = (c[0]>>2) & 0x7;
487 uc[0] = (c[0]<<6) | (c[1]&0x3f);
495 if ((c[0] & 0xf0) == 0xe0) {
497 (c[1] & 0xc0) != 0x80 ||
498 (c[2] & 0xc0) != 0x80) {
502 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
503 uc[0] = (c[1]<<6) | (c[2]&0x3f);
511 if ((c[0] & 0xf8) == 0xf0) {
512 unsigned int codepoint;
514 (c[1] & 0xc0) != 0x80 ||
515 (c[2] & 0xc0) != 0x80 ||
516 (c[3] & 0xc0) != 0x80) {
525 if (codepoint < 0x10000) {
526 /* accept UTF-8 characters that are not
527 minimally packed, but pack the result */
528 uc[0] = (codepoint & 0xFF);
529 uc[1] = (codepoint >> 8);
537 codepoint -= 0x10000;
544 uc[0] = (codepoint>>10) & 0xFF;
545 uc[1] = (codepoint>>18) | 0xd8;
546 uc[2] = codepoint & 0xFF;
547 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
555 /* we don't handle 5 byte sequences */
565 *inbytesleft = in_left;
566 *outbytesleft = out_left;
567 *inbuf = (const char *)c;
568 *outbuf = (char *)uc;
572 *inbytesleft = in_left;
573 *outbytesleft = out_left;
574 *inbuf = (const char *)c;
575 *outbuf = (char *)uc;
579 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
580 char **outbuf, size_t *outbytesleft)
582 size_t in_left=*inbytesleft, out_left=*outbytesleft;
583 uint8_t *c = (uint8_t *)*outbuf;
584 const uint8_t *uc = (const uint8_t *)*inbuf;
586 while (in_left >= 2 && out_left >= 1) {
587 unsigned int codepoint;
589 if (uc[1] == 0 && !(uc[0] & 0x80)) {
599 if ((uc[1]&0xf8) == 0) {
600 /* next simplest case */
605 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
606 c[1] = 0x80 | (uc[0] & 0x3f);
614 if ((uc[1] & 0xfc) == 0xdc) {
615 /* its the second part of a 4 byte sequence. Illegal */
624 if ((uc[1] & 0xfc) != 0xd8) {
625 codepoint = uc[0] | (uc[1]<<8);
630 c[0] = 0xe0 | (codepoint >> 12);
631 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
632 c[2] = 0x80 | (codepoint & 0x3f);
641 /* its the first part of a 4 byte sequence */
646 if ((uc[3] & 0xfc) != 0xdc) {
650 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
651 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
657 c[0] = 0xf0 | (codepoint >> 18);
658 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
659 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
660 c[3] = 0x80 | (codepoint & 0x3f);
678 *inbytesleft = in_left;
679 *outbytesleft = out_left;
680 *inbuf = (const char *)uc;
686 *inbytesleft = in_left;
687 *outbytesleft = out_left;
688 *inbuf = (const char *)uc;