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.
28 * @brief Samba wrapper/stub for iconv character set conversion.
30 * iconv is the XPG2 interface for converting between character
31 * encodings. This file provides a Samba wrapper around it, and also
32 * a simple reimplementation that is used if the system does not
35 * Samba only works with encodings that are supersets of ASCII: ascii
36 * characters like whitespace can be tested for directly, multibyte
37 * sequences start with a byte with the high bit set, and strings are
38 * terminated by a nul byte.
40 * Note that the only function provided by iconv is conversion between
41 * characters. It doesn't directly support operations like
42 * uppercasing or comparison. We have to convert to UTF-16LE and
45 * @sa Samba Developers Guide
48 static size_t ascii_pull (void *,const char **, size_t *, char **, size_t *);
49 static size_t ascii_push (void *,const char **, size_t *, char **, size_t *);
50 static size_t utf8_pull (void *,const char **, size_t *, char **, size_t *);
51 static size_t utf8_push (void *,const char **, size_t *, char **, size_t *);
52 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
53 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
54 static size_t iconv_copy (void *,const char **, size_t *, char **, size_t *);
55 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
57 static const struct charset_functions const builtin_functions[] = {
58 /* windows is closest to UTF-16 */
59 {"UCS-2LE", iconv_copy, iconv_copy},
60 {"UTF-16LE", iconv_copy, iconv_copy},
61 {"UCS-2BE", iconv_swab, iconv_swab},
62 {"UTF-16BE", iconv_swab, iconv_swab},
64 /* we include the UTF-8 alias to cope with differing locale settings */
65 {"UTF8", utf8_pull, utf8_push},
66 {"UTF-8", utf8_pull, utf8_push},
67 {"ASCII", ascii_pull, ascii_push},
68 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
72 static struct charset_functions *charsets = NULL;
74 static NTSTATUS charset_register_backend(const void *_funcs)
76 struct charset_functions *funcs = memdup(_funcs,sizeof(struct charset_functions));
77 struct charset_functions *c = charsets;
79 /* Check whether we already have this charset... */
81 if(!strcasecmp(c->name, funcs->name)){
82 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
83 return NT_STATUS_OBJECT_NAME_COLLISION;
88 funcs->next = funcs->prev = NULL;
89 DLIST_ADD(charsets, funcs);
93 static void lazy_initialize_iconv(void)
95 static BOOL initialized = False;
100 register_subsystem("charset", charset_register_backend);
102 for(i = 0; builtin_functions[i].name; i++)
103 register_backend("charset", &builtin_functions[i]);
107 #ifdef HAVE_NATIVE_ICONV
108 /* if there was an error then reset the internal state,
109 this ensures that we don't have a shift state remaining for
110 character sets like SJIS */
111 static size_t sys_iconv(void *cd,
112 const char **inbuf, size_t *inbytesleft,
113 char **outbuf, size_t *outbytesleft)
115 size_t ret = iconv((iconv_t)cd,
116 discard_const_p(char *, inbuf), inbytesleft,
117 outbuf, outbytesleft);
118 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
124 * This is a simple portable iconv() implementaion.
126 * It only knows about a very small number of character sets - just
127 * enough that Samba works on systems that don't have iconv.
129 size_t smb_iconv(smb_iconv_t cd,
130 const char **inbuf, size_t *inbytesleft,
131 char **outbuf, size_t *outbytesleft)
136 /* in many cases we can go direct */
138 return cd->direct(cd->cd_direct,
139 inbuf, inbytesleft, outbuf, outbytesleft);
143 /* otherwise we have to do it chunks at a time */
144 while (*inbytesleft > 0) {
145 char *bufp1 = cvtbuf;
146 const char *bufp2 = cvtbuf;
148 bufsize = sizeof(cvtbuf);
150 if (cd->pull(cd->cd_pull,
151 inbuf, inbytesleft, &bufp1, &bufsize) == -1
152 && errno != E2BIG) return -1;
154 bufsize = sizeof(cvtbuf) - bufsize;
156 if (cd->push(cd->cd_push,
158 outbuf, outbytesleft) == -1) return -1;
164 static BOOL is_utf16(const char *name)
166 return strcasecmp(name, "UCS-2LE") == 0 ||
167 strcasecmp(name, "UTF-16LE") == 0;
171 simple iconv_open() wrapper
173 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
176 struct charset_functions *from, *to;
178 lazy_initialize_iconv();
182 ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret),
183 "iconv(%s,%s)", tocode, fromcode);
186 return (smb_iconv_t)-1;
188 memset(ret, 0, sizeof(*ret));
190 /* check for the simplest null conversion */
191 if (strcmp(fromcode, tocode) == 0) {
192 ret->direct = iconv_copy;
197 if (strcasecmp(from->name, fromcode) == 0) break;
202 if (strcasecmp(to->name, tocode) == 0) break;
206 #ifdef HAVE_NATIVE_ICONV
208 ret->pull = sys_iconv;
209 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
210 if (ret->cd_pull == (iconv_t)-1)
211 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
212 if (ret->cd_pull == (iconv_t)-1) goto failed;
216 ret->push = sys_iconv;
217 ret->cd_push = iconv_open(tocode, "UTF-16LE");
218 if (ret->cd_push == (iconv_t)-1)
219 ret->cd_push = iconv_open(tocode, "UCS-2LE");
220 if (ret->cd_push == (iconv_t)-1) goto failed;
228 /* check for conversion to/from ucs2 */
229 if (is_utf16(fromcode) && to) {
230 ret->direct = to->push;
233 if (is_utf16(tocode) && from) {
234 ret->direct = from->pull;
238 #ifdef HAVE_NATIVE_ICONV
239 if (is_utf16(fromcode)) {
240 ret->direct = sys_iconv;
241 ret->cd_direct = ret->cd_push;
245 if (is_utf16(tocode)) {
246 ret->direct = sys_iconv;
247 ret->cd_direct = ret->cd_pull;
253 /* the general case has to go via a buffer */
254 if (!ret->pull) ret->pull = from->pull;
255 if (!ret->push) ret->push = to->push;
261 return (smb_iconv_t)-1;
265 simple iconv_close() wrapper
267 int smb_iconv_close(smb_iconv_t cd)
269 #ifdef HAVE_NATIVE_ICONV
270 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
271 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
272 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
280 /**********************************************************************
281 the following functions implement the builtin character sets in Samba
282 and also the "test" character sets that are designed to test
283 multi-byte character set support for english users
284 ***********************************************************************/
285 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
286 char **outbuf, size_t *outbytesleft)
288 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
289 (*outbuf)[0] = (*inbuf)[0];
292 (*outbytesleft) -= 2;
297 if (*inbytesleft > 0) {
305 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
306 char **outbuf, size_t *outbytesleft)
310 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
311 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
312 if ((*inbuf)[1]) ir_count++;
314 (*outbytesleft) -= 1;
319 if (*inbytesleft == 1) {
324 if (*inbytesleft > 1) {
333 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
334 char **outbuf, size_t *outbytesleft)
336 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
339 if ((*inbuf)[0] != '@') {
340 /* seven bit ascii case */
341 (*outbuf)[0] = (*inbuf)[0];
344 (*outbytesleft) -= 2;
349 /* it's a hex character */
350 if (*inbytesleft < 5) {
355 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
360 (*outbuf)[0] = v&0xff;
363 (*outbytesleft) -= 2;
368 if (*inbytesleft > 0) {
376 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
377 char **outbuf, size_t *outbytesleft)
379 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
382 if ((*inbuf)[1] == 0 &&
383 ((*inbuf)[0] & 0x80) == 0 &&
384 (*inbuf)[0] != '@') {
385 (*outbuf)[0] = (*inbuf)[0];
387 (*outbytesleft) -= 1;
392 if (*outbytesleft < 5) {
396 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
397 memcpy(*outbuf, buf, 5);
399 (*outbytesleft) -= 5;
404 if (*inbytesleft == 1) {
409 if (*inbytesleft > 1) {
417 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
418 char **outbuf, size_t *outbytesleft)
422 n = MIN(*inbytesleft, *outbytesleft);
424 swab(*inbuf, *outbuf, (n&~1));
430 (*outbytesleft) -= n;
434 if (*inbytesleft > 0) {
443 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
444 char **outbuf, size_t *outbytesleft)
448 n = MIN(*inbytesleft, *outbytesleft);
450 memmove(*outbuf, *inbuf, n);
453 (*outbytesleft) -= n;
457 if (*inbytesleft > 0) {
465 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
466 char **outbuf, size_t *outbytesleft)
468 size_t in_left=*inbytesleft, out_left=*outbytesleft;
469 const uint8_t *c = (const uint8_t *)*inbuf;
470 uint8_t *uc = (uint8_t *)*outbuf;
472 while (in_left >= 1 && out_left >= 2) {
473 if ((c[0] & 0x80) == 0) {
483 if ((c[0] & 0xe0) == 0xc0) {
485 (c[1] & 0xc0) != 0x80) {
489 uc[1] = (c[0]>>2) & 0x7;
490 uc[0] = (c[0]<<6) | (c[1]&0x3f);
498 if ((c[0] & 0xf0) == 0xe0) {
500 (c[1] & 0xc0) != 0x80 ||
501 (c[2] & 0xc0) != 0x80) {
505 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
506 uc[0] = (c[1]<<6) | (c[2]&0x3f);
514 if ((c[0] & 0xf8) == 0xf0) {
515 unsigned int codepoint;
517 (c[1] & 0xc0) != 0x80 ||
518 (c[2] & 0xc0) != 0x80 ||
519 (c[3] & 0xc0) != 0x80) {
528 if (codepoint < 0x10000) {
529 /* accept UTF-8 characters that are not
530 minimally packed, but pack the result */
531 uc[0] = (codepoint & 0xFF);
532 uc[1] = (codepoint >> 8);
540 codepoint -= 0x10000;
547 uc[0] = (codepoint>>10) & 0xFF;
548 uc[1] = (codepoint>>18) | 0xd8;
549 uc[2] = codepoint & 0xFF;
550 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
558 /* we don't handle 5 byte sequences */
568 *inbytesleft = in_left;
569 *outbytesleft = out_left;
575 *inbytesleft = in_left;
576 *outbytesleft = out_left;
582 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
583 char **outbuf, size_t *outbytesleft)
585 size_t in_left=*inbytesleft, out_left=*outbytesleft;
586 uint8_t *c = (uint8_t *)*outbuf;
587 const uint8_t *uc = (const uint8_t *)*inbuf;
589 while (in_left >= 2 && out_left >= 1) {
590 unsigned int codepoint;
592 if (uc[1] == 0 && !(uc[0] & 0x80)) {
602 if ((uc[1]&0xf8) == 0) {
603 /* next simplest case */
608 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
609 c[1] = 0x80 | (uc[0] & 0x3f);
617 if ((uc[1] & 0xfc) == 0xdc) {
618 /* its the second part of a 4 byte sequence. Illegal */
627 if ((uc[1] & 0xfc) != 0xd8) {
628 codepoint = uc[0] | (uc[1]<<8);
633 c[0] = 0xe0 | (codepoint >> 12);
634 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
635 c[2] = 0x80 | (codepoint & 0x3f);
644 /* its the first part of a 4 byte sequence */
649 if ((uc[3] & 0xfc) != 0xdc) {
653 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
654 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
660 c[0] = 0xf0 | (codepoint >> 18);
661 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
662 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
663 c[3] = 0x80 | (codepoint & 0x3f);
681 *inbytesleft = in_left;
682 *outbytesleft = out_left;
689 *inbytesleft = in_left;
690 *outbytesleft = out_left;