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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "dlinklist.h"
24 #include "system/iconv.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 const 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},
74 static struct charset_functions *charsets = NULL;
76 static NTSTATUS charset_register_backend(const void *_funcs)
78 struct charset_functions *funcs = memdup(_funcs,sizeof(struct charset_functions));
79 struct charset_functions *c = charsets;
81 /* Check whether we already have this charset... */
83 if(!strcasecmp(c->name, funcs->name)){
84 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
85 return NT_STATUS_OBJECT_NAME_COLLISION;
90 funcs->next = funcs->prev = NULL;
91 DLIST_ADD(charsets, funcs);
95 static void lazy_initialize_iconv(void)
97 static BOOL initialized = False;
102 register_subsystem("charset", charset_register_backend);
104 for(i = 0; builtin_functions[i].name; i++)
105 register_backend("charset", &builtin_functions[i]);
109 #ifdef HAVE_NATIVE_ICONV
110 /* if there was an error then reset the internal state,
111 this ensures that we don't have a shift state remaining for
112 character sets like SJIS */
113 static size_t sys_iconv(void *cd,
114 const char **inbuf, size_t *inbytesleft,
115 char **outbuf, size_t *outbytesleft)
117 size_t ret = iconv((iconv_t)cd,
118 discard_const_p(char *, inbuf), inbytesleft,
119 outbuf, outbytesleft);
120 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
126 * This is a simple portable iconv() implementaion.
128 * It only knows about a very small number of character sets - just
129 * enough that Samba works on systems that don't have iconv.
131 size_t smb_iconv(smb_iconv_t cd,
132 const char **inbuf, size_t *inbytesleft,
133 char **outbuf, size_t *outbytesleft)
138 /* in many cases we can go direct */
140 return cd->direct(cd->cd_direct,
141 inbuf, inbytesleft, outbuf, outbytesleft);
145 /* otherwise we have to do it chunks at a time */
146 while (*inbytesleft > 0) {
147 char *bufp1 = cvtbuf;
148 const char *bufp2 = cvtbuf;
150 bufsize = sizeof(cvtbuf);
152 if (cd->pull(cd->cd_pull,
153 inbuf, inbytesleft, &bufp1, &bufsize) == -1
154 && errno != E2BIG) return -1;
156 bufsize = sizeof(cvtbuf) - bufsize;
158 if (cd->push(cd->cd_push,
160 outbuf, outbytesleft) == -1) return -1;
166 static BOOL is_utf16(const char *name)
168 return strcasecmp(name, "UCS-2LE") == 0 ||
169 strcasecmp(name, "UTF-16LE") == 0;
173 simple iconv_open() wrapper
175 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
178 struct charset_functions *from, *to;
180 lazy_initialize_iconv();
184 ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret),
185 "iconv(%s,%s)", tocode, fromcode);
188 return (smb_iconv_t)-1;
190 memset(ret, 0, sizeof(*ret));
192 /* check for the simplest null conversion */
193 if (strcmp(fromcode, tocode) == 0) {
194 ret->direct = iconv_copy;
199 if (strcasecmp(from->name, fromcode) == 0) break;
204 if (strcasecmp(to->name, tocode) == 0) break;
208 #ifdef HAVE_NATIVE_ICONV
210 ret->pull = sys_iconv;
211 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
212 if (ret->cd_pull == (iconv_t)-1)
213 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
214 if (ret->cd_pull == (iconv_t)-1) goto failed;
218 ret->push = sys_iconv;
219 ret->cd_push = iconv_open(tocode, "UTF-16LE");
220 if (ret->cd_push == (iconv_t)-1)
221 ret->cd_push = iconv_open(tocode, "UCS-2LE");
222 if (ret->cd_push == (iconv_t)-1) goto failed;
230 /* check for conversion to/from ucs2 */
231 if (is_utf16(fromcode) && to) {
232 ret->direct = to->push;
235 if (is_utf16(tocode) && from) {
236 ret->direct = from->pull;
240 #ifdef HAVE_NATIVE_ICONV
241 if (is_utf16(fromcode)) {
242 ret->direct = sys_iconv;
243 ret->cd_direct = ret->cd_push;
247 if (is_utf16(tocode)) {
248 ret->direct = sys_iconv;
249 ret->cd_direct = ret->cd_pull;
255 /* the general case has to go via a buffer */
256 if (!ret->pull) ret->pull = from->pull;
257 if (!ret->push) ret->push = to->push;
263 return (smb_iconv_t)-1;
267 simple iconv_close() wrapper
269 int smb_iconv_close(smb_iconv_t cd)
271 #ifdef HAVE_NATIVE_ICONV
272 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
273 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
274 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
282 /**********************************************************************
283 the following functions implement the builtin character sets in Samba
284 and also the "test" character sets that are designed to test
285 multi-byte character set support for english users
286 ***********************************************************************/
287 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
288 char **outbuf, size_t *outbytesleft)
290 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
291 (*outbuf)[0] = (*inbuf)[0];
294 (*outbytesleft) -= 2;
299 if (*inbytesleft > 0) {
307 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
308 char **outbuf, size_t *outbytesleft)
312 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
313 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
314 if ((*inbuf)[1]) ir_count++;
316 (*outbytesleft) -= 1;
321 if (*inbytesleft == 1) {
326 if (*inbytesleft > 1) {
335 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
336 char **outbuf, size_t *outbytesleft)
338 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
341 if ((*inbuf)[0] != '@') {
342 /* seven bit ascii case */
343 (*outbuf)[0] = (*inbuf)[0];
346 (*outbytesleft) -= 2;
351 /* it's a hex character */
352 if (*inbytesleft < 5) {
357 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
362 (*outbuf)[0] = v&0xff;
365 (*outbytesleft) -= 2;
370 if (*inbytesleft > 0) {
378 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
379 char **outbuf, size_t *outbytesleft)
381 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
384 if ((*inbuf)[1] == 0 &&
385 ((*inbuf)[0] & 0x80) == 0 &&
386 (*inbuf)[0] != '@') {
387 (*outbuf)[0] = (*inbuf)[0];
389 (*outbytesleft) -= 1;
394 if (*outbytesleft < 5) {
398 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
399 memcpy(*outbuf, buf, 5);
401 (*outbytesleft) -= 5;
406 if (*inbytesleft == 1) {
411 if (*inbytesleft > 1) {
419 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
420 char **outbuf, size_t *outbytesleft)
424 n = MIN(*inbytesleft, *outbytesleft);
426 swab(*inbuf, *outbuf, (n&~1));
432 (*outbytesleft) -= n;
436 if (*inbytesleft > 0) {
445 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
446 char **outbuf, size_t *outbytesleft)
450 n = MIN(*inbytesleft, *outbytesleft);
452 memmove(*outbuf, *inbuf, n);
455 (*outbytesleft) -= n;
459 if (*inbytesleft > 0) {
467 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
468 char **outbuf, size_t *outbytesleft)
470 size_t in_left=*inbytesleft, out_left=*outbytesleft;
471 const uint8_t *c = (const uint8_t *)*inbuf;
472 uint8_t *uc = (uint8_t *)*outbuf;
474 while (in_left >= 1 && out_left >= 2) {
475 if ((c[0] & 0x80) == 0) {
485 if ((c[0] & 0xe0) == 0xc0) {
487 (c[1] & 0xc0) != 0x80) {
491 uc[1] = (c[0]>>2) & 0x7;
492 uc[0] = (c[0]<<6) | (c[1]&0x3f);
500 if ((c[0] & 0xf0) == 0xe0) {
502 (c[1] & 0xc0) != 0x80 ||
503 (c[2] & 0xc0) != 0x80) {
507 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
508 uc[0] = (c[1]<<6) | (c[2]&0x3f);
516 if ((c[0] & 0xf8) == 0xf0) {
517 unsigned int codepoint;
519 (c[1] & 0xc0) != 0x80 ||
520 (c[2] & 0xc0) != 0x80 ||
521 (c[3] & 0xc0) != 0x80) {
530 if (codepoint < 0x10000) {
531 /* accept UTF-8 characters that are not
532 minimally packed, but pack the result */
533 uc[0] = (codepoint & 0xFF);
534 uc[1] = (codepoint >> 8);
542 codepoint -= 0x10000;
549 uc[0] = (codepoint>>10) & 0xFF;
550 uc[1] = (codepoint>>18) | 0xd8;
551 uc[2] = codepoint & 0xFF;
552 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
560 /* we don't handle 5 byte sequences */
570 *inbytesleft = in_left;
571 *outbytesleft = out_left;
577 *inbytesleft = in_left;
578 *outbytesleft = out_left;
584 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
585 char **outbuf, size_t *outbytesleft)
587 size_t in_left=*inbytesleft, out_left=*outbytesleft;
588 uint8_t *c = (uint8_t *)*outbuf;
589 const uint8_t *uc = (const uint8_t *)*inbuf;
591 while (in_left >= 2 && out_left >= 1) {
592 unsigned int codepoint;
594 if (uc[1] == 0 && !(uc[0] & 0x80)) {
604 if ((uc[1]&0xf8) == 0) {
605 /* next simplest case */
610 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
611 c[1] = 0x80 | (uc[0] & 0x3f);
619 if ((uc[1] & 0xfc) == 0xdc) {
620 /* its the second part of a 4 byte sequence. Illegal */
629 if ((uc[1] & 0xfc) != 0xd8) {
630 codepoint = uc[0] | (uc[1]<<8);
635 c[0] = 0xe0 | (codepoint >> 12);
636 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
637 c[2] = 0x80 | (codepoint & 0x3f);
646 /* its the first part of a 4 byte sequence */
651 if ((uc[3] & 0xfc) != 0xdc) {
655 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
656 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
662 c[0] = 0xf0 | (codepoint >> 18);
663 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
664 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
665 c[3] = 0x80 | (codepoint & 0x3f);
683 *inbytesleft = in_left;
684 *outbytesleft = out_left;
691 *inbytesleft = in_left;
692 *outbytesleft = out_left;