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"
25 #include "param/param.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 utf8_pull (void *,const char **, size_t *, char **, size_t *);
54 static size_t utf8_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},
70 {"ASCII", ascii_pull, ascii_push},
71 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push}
74 static struct charset_functions *charsets = NULL;
76 bool charset_register_backend(const void *_funcs)
78 struct charset_functions *funcs = (struct charset_functions *)memdup(_funcs,sizeof(struct charset_functions));
79 struct charset_functions *c;
81 /* Check whether we already have this charset... */
82 for (c = charsets; c != NULL; c = c->next) {
83 if(!strcasecmp(c->name, funcs->name)) {
84 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
89 funcs->next = funcs->prev = NULL;
90 DLIST_ADD(charsets, funcs);
94 #ifdef HAVE_NATIVE_ICONV
95 /* if there was an error then reset the internal state,
96 this ensures that we don't have a shift state remaining for
97 character sets like SJIS */
98 static size_t sys_iconv(void *cd,
99 const char **inbuf, size_t *inbytesleft,
100 char **outbuf, size_t *outbytesleft)
102 size_t ret = iconv((iconv_t)cd,
103 discard_const_p(char *, inbuf), inbytesleft,
104 outbuf, outbytesleft);
105 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
111 * This is a simple portable iconv() implementaion.
113 * It only knows about a very small number of character sets - just
114 * enough that Samba works on systems that don't have iconv.
116 size_t smb_iconv(smb_iconv_t cd,
117 const char **inbuf, size_t *inbytesleft,
118 char **outbuf, size_t *outbytesleft)
123 /* in many cases we can go direct */
125 return cd->direct(cd->cd_direct,
126 inbuf, inbytesleft, outbuf, outbytesleft);
130 /* otherwise we have to do it chunks at a time */
131 while (*inbytesleft > 0) {
132 char *bufp1 = cvtbuf;
133 const char *bufp2 = cvtbuf;
135 bufsize = sizeof(cvtbuf);
137 if (cd->pull(cd->cd_pull,
138 inbuf, inbytesleft, &bufp1, &bufsize) == -1
139 && errno != E2BIG) return -1;
141 bufsize = sizeof(cvtbuf) - bufsize;
143 if (cd->push(cd->cd_push,
145 outbuf, outbytesleft) == -1) return -1;
151 static bool is_utf16(const char *name)
153 return strcasecmp(name, "UCS-2LE") == 0 ||
154 strcasecmp(name, "UTF-16LE") == 0;
158 simple iconv_open() wrapper
160 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
163 const struct charset_functions *from=NULL, *to=NULL;
166 ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret),
167 "iconv(%s,%s)", tocode, fromcode);
170 return (smb_iconv_t)-1;
172 memset(ret, 0, sizeof(*ret));
174 /* check for the simplest null conversion */
175 if (strcmp(fromcode, tocode) == 0) {
176 ret->direct = iconv_copy;
180 for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
181 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
182 from = &builtin_functions[i];
184 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
185 to = &builtin_functions[i];
190 for (from=charsets; from; from=from->next) {
191 if (strcasecmp(from->name, fromcode) == 0) break;
196 for (to=charsets; to; to=to->next) {
197 if (strcasecmp(to->name, tocode) == 0) break;
201 #ifdef HAVE_NATIVE_ICONV
202 if ((!from || !to) && !lp_parm_bool(NULL, "iconv", "native", true)) {
206 ret->pull = sys_iconv;
207 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
208 if (ret->cd_pull == (iconv_t)-1)
209 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
210 if (ret->cd_pull == (iconv_t)-1) goto failed;
214 ret->push = sys_iconv;
215 ret->cd_push = iconv_open(tocode, "UTF-16LE");
216 if (ret->cd_push == (iconv_t)-1)
217 ret->cd_push = iconv_open(tocode, "UCS-2LE");
218 if (ret->cd_push == (iconv_t)-1) goto failed;
226 /* check for conversion to/from ucs2 */
227 if (is_utf16(fromcode) && to) {
228 ret->direct = to->push;
231 if (is_utf16(tocode) && from) {
232 ret->direct = from->pull;
236 #ifdef HAVE_NATIVE_ICONV
237 if (is_utf16(fromcode)) {
238 ret->direct = sys_iconv;
239 ret->cd_direct = ret->cd_push;
243 if (is_utf16(tocode)) {
244 ret->direct = sys_iconv;
245 ret->cd_direct = ret->cd_pull;
251 /* the general case has to go via a buffer */
252 if (!ret->pull) ret->pull = from->pull;
253 if (!ret->push) ret->push = to->push;
259 return (smb_iconv_t)-1;
263 simple iconv_close() wrapper
265 int smb_iconv_close(smb_iconv_t cd)
267 #ifdef HAVE_NATIVE_ICONV
268 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
269 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
270 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
278 /**********************************************************************
279 the following functions implement the builtin character sets in Samba
280 and also the "test" character sets that are designed to test
281 multi-byte character set support for english users
282 ***********************************************************************/
283 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
284 char **outbuf, size_t *outbytesleft)
286 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
287 (*outbuf)[0] = (*inbuf)[0];
290 (*outbytesleft) -= 2;
295 if (*inbytesleft > 0) {
303 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
304 char **outbuf, size_t *outbytesleft)
308 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
309 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
310 if ((*inbuf)[1]) ir_count++;
312 (*outbytesleft) -= 1;
317 if (*inbytesleft == 1) {
322 if (*inbytesleft > 1) {
331 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
332 char **outbuf, size_t *outbytesleft)
334 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
337 if ((*inbuf)[0] != '@') {
338 /* seven bit ascii case */
339 (*outbuf)[0] = (*inbuf)[0];
342 (*outbytesleft) -= 2;
347 /* it's a hex character */
348 if (*inbytesleft < 5) {
353 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
358 (*outbuf)[0] = v&0xff;
361 (*outbytesleft) -= 2;
366 if (*inbytesleft > 0) {
374 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
375 char **outbuf, size_t *outbytesleft)
377 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
380 if ((*inbuf)[1] == 0 &&
381 ((*inbuf)[0] & 0x80) == 0 &&
382 (*inbuf)[0] != '@') {
383 (*outbuf)[0] = (*inbuf)[0];
385 (*outbytesleft) -= 1;
390 if (*outbytesleft < 5) {
394 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
395 memcpy(*outbuf, buf, 5);
397 (*outbytesleft) -= 5;
402 if (*inbytesleft == 1) {
407 if (*inbytesleft > 1) {
415 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
416 char **outbuf, size_t *outbytesleft)
420 n = MIN(*inbytesleft, *outbytesleft);
422 swab(*inbuf, *outbuf, (n&~1));
428 (*outbytesleft) -= n;
432 if (*inbytesleft > 0) {
441 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
442 char **outbuf, size_t *outbytesleft)
446 n = MIN(*inbytesleft, *outbytesleft);
448 memmove(*outbuf, *inbuf, n);
451 (*outbytesleft) -= n;
455 if (*inbytesleft > 0) {
463 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
464 char **outbuf, size_t *outbytesleft)
466 size_t in_left=*inbytesleft, out_left=*outbytesleft;
467 const uint8_t *c = (const uint8_t *)*inbuf;
468 uint8_t *uc = (uint8_t *)*outbuf;
470 while (in_left >= 1 && out_left >= 2) {
471 if ((c[0] & 0x80) == 0) {
481 if ((c[0] & 0xe0) == 0xc0) {
483 (c[1] & 0xc0) != 0x80) {
487 uc[1] = (c[0]>>2) & 0x7;
488 uc[0] = (c[0]<<6) | (c[1]&0x3f);
496 if ((c[0] & 0xf0) == 0xe0) {
498 (c[1] & 0xc0) != 0x80 ||
499 (c[2] & 0xc0) != 0x80) {
503 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
504 uc[0] = (c[1]<<6) | (c[2]&0x3f);
512 if ((c[0] & 0xf8) == 0xf0) {
513 unsigned int codepoint;
515 (c[1] & 0xc0) != 0x80 ||
516 (c[2] & 0xc0) != 0x80 ||
517 (c[3] & 0xc0) != 0x80) {
526 if (codepoint < 0x10000) {
527 /* accept UTF-8 characters that are not
528 minimally packed, but pack the result */
529 uc[0] = (codepoint & 0xFF);
530 uc[1] = (codepoint >> 8);
538 codepoint -= 0x10000;
545 uc[0] = (codepoint>>10) & 0xFF;
546 uc[1] = (codepoint>>18) | 0xd8;
547 uc[2] = codepoint & 0xFF;
548 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
556 /* we don't handle 5 byte sequences */
566 *inbytesleft = in_left;
567 *outbytesleft = out_left;
568 *inbuf = (const char *)c;
569 *outbuf = (char *)uc;
573 *inbytesleft = in_left;
574 *outbytesleft = out_left;
575 *inbuf = (const char *)c;
576 *outbuf = (char *)uc;
580 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
581 char **outbuf, size_t *outbytesleft)
583 size_t in_left=*inbytesleft, out_left=*outbytesleft;
584 uint8_t *c = (uint8_t *)*outbuf;
585 const uint8_t *uc = (const uint8_t *)*inbuf;
587 while (in_left >= 2 && out_left >= 1) {
588 unsigned int codepoint;
590 if (uc[1] == 0 && !(uc[0] & 0x80)) {
600 if ((uc[1]&0xf8) == 0) {
601 /* next simplest case */
606 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
607 c[1] = 0x80 | (uc[0] & 0x3f);
615 if ((uc[1] & 0xfc) == 0xdc) {
616 /* its the second part of a 4 byte sequence. Illegal */
625 if ((uc[1] & 0xfc) != 0xd8) {
626 codepoint = uc[0] | (uc[1]<<8);
631 c[0] = 0xe0 | (codepoint >> 12);
632 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
633 c[2] = 0x80 | (codepoint & 0x3f);
642 /* its the first part of a 4 byte sequence */
647 if ((uc[3] & 0xfc) != 0xdc) {
651 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
652 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
658 c[0] = 0xf0 | (codepoint >> 18);
659 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
660 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
661 c[3] = 0x80 | (codepoint & 0x3f);
679 *inbytesleft = in_left;
680 *outbytesleft = out_left;
681 *inbuf = (const char *)uc;
687 *inbytesleft = in_left;
688 *outbytesleft = out_left;
689 *inbuf = (const char *)uc;