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 "lib/util/dlinklist.h"
24 #include "system/iconv.h"
25 #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 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 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 #ifdef HAVE_NATIVE_ICONV
96 /* if there was an error then reset the internal state,
97 this ensures that we don't have a shift state remaining for
98 character sets like SJIS */
99 static size_t sys_iconv(void *cd,
100 const char **inbuf, size_t *inbytesleft,
101 char **outbuf, size_t *outbytesleft)
103 size_t ret = iconv((iconv_t)cd,
104 discard_const_p(char *, inbuf), inbytesleft,
105 outbuf, outbytesleft);
106 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
112 * This is a simple portable iconv() implementaion.
114 * It only knows about a very small number of character sets - just
115 * enough that Samba works on systems that don't have iconv.
117 size_t smb_iconv(smb_iconv_t cd,
118 const char **inbuf, size_t *inbytesleft,
119 char **outbuf, size_t *outbytesleft)
124 /* in many cases we can go direct */
126 return cd->direct(cd->cd_direct,
127 inbuf, inbytesleft, outbuf, outbytesleft);
131 /* otherwise we have to do it chunks at a time */
132 while (*inbytesleft > 0) {
133 char *bufp1 = cvtbuf;
134 const char *bufp2 = cvtbuf;
136 bufsize = sizeof(cvtbuf);
138 if (cd->pull(cd->cd_pull,
139 inbuf, inbytesleft, &bufp1, &bufsize) == -1
140 && errno != E2BIG) return -1;
142 bufsize = sizeof(cvtbuf) - bufsize;
144 if (cd->push(cd->cd_push,
146 outbuf, outbytesleft) == -1) return -1;
152 static BOOL is_utf16(const char *name)
154 return strcasecmp(name, "UCS-2LE") == 0 ||
155 strcasecmp(name, "UTF-16LE") == 0;
159 simple iconv_open() wrapper
161 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
164 const struct charset_functions *from=NULL, *to=NULL;
167 ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret),
168 "iconv(%s,%s)", tocode, fromcode);
171 return (smb_iconv_t)-1;
173 memset(ret, 0, sizeof(*ret));
175 /* check for the simplest null conversion */
176 if (strcmp(fromcode, tocode) == 0) {
177 ret->direct = iconv_copy;
181 for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
182 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
183 from = &builtin_functions[i];
185 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
186 to = &builtin_functions[i];
191 for (from=charsets; from; from=from->next) {
192 if (strcasecmp(from->name, fromcode) == 0) break;
197 for (to=charsets; to; to=to->next) {
198 if (strcasecmp(to->name, tocode) == 0) break;
202 #ifdef HAVE_NATIVE_ICONV
203 if ((!from || !to) && !lp_parm_bool(-1, "iconv", "native", True)) {
207 ret->pull = sys_iconv;
208 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
209 if (ret->cd_pull == (iconv_t)-1)
210 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
211 if (ret->cd_pull == (iconv_t)-1) goto failed;
215 ret->push = sys_iconv;
216 ret->cd_push = iconv_open(tocode, "UTF-16LE");
217 if (ret->cd_push == (iconv_t)-1)
218 ret->cd_push = iconv_open(tocode, "UCS-2LE");
219 if (ret->cd_push == (iconv_t)-1) goto failed;
227 /* check for conversion to/from ucs2 */
228 if (is_utf16(fromcode) && to) {
229 ret->direct = to->push;
232 if (is_utf16(tocode) && from) {
233 ret->direct = from->pull;
237 #ifdef HAVE_NATIVE_ICONV
238 if (is_utf16(fromcode)) {
239 ret->direct = sys_iconv;
240 ret->cd_direct = ret->cd_push;
244 if (is_utf16(tocode)) {
245 ret->direct = sys_iconv;
246 ret->cd_direct = ret->cd_pull;
252 /* the general case has to go via a buffer */
253 if (!ret->pull) ret->pull = from->pull;
254 if (!ret->push) ret->push = to->push;
260 return (smb_iconv_t)-1;
264 simple iconv_close() wrapper
266 int smb_iconv_close(smb_iconv_t cd)
268 #ifdef HAVE_NATIVE_ICONV
269 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
270 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
271 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
279 /**********************************************************************
280 the following functions implement the builtin character sets in Samba
281 and also the "test" character sets that are designed to test
282 multi-byte character set support for english users
283 ***********************************************************************/
284 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
285 char **outbuf, size_t *outbytesleft)
287 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
288 (*outbuf)[0] = (*inbuf)[0];
291 (*outbytesleft) -= 2;
296 if (*inbytesleft > 0) {
304 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
305 char **outbuf, size_t *outbytesleft)
309 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
310 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
311 if ((*inbuf)[1]) ir_count++;
313 (*outbytesleft) -= 1;
318 if (*inbytesleft == 1) {
323 if (*inbytesleft > 1) {
332 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
333 char **outbuf, size_t *outbytesleft)
335 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
338 if ((*inbuf)[0] != '@') {
339 /* seven bit ascii case */
340 (*outbuf)[0] = (*inbuf)[0];
343 (*outbytesleft) -= 2;
348 /* it's a hex character */
349 if (*inbytesleft < 5) {
354 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
359 (*outbuf)[0] = v&0xff;
362 (*outbytesleft) -= 2;
367 if (*inbytesleft > 0) {
375 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
376 char **outbuf, size_t *outbytesleft)
378 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
381 if ((*inbuf)[1] == 0 &&
382 ((*inbuf)[0] & 0x80) == 0 &&
383 (*inbuf)[0] != '@') {
384 (*outbuf)[0] = (*inbuf)[0];
386 (*outbytesleft) -= 1;
391 if (*outbytesleft < 5) {
395 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
396 memcpy(*outbuf, buf, 5);
398 (*outbytesleft) -= 5;
403 if (*inbytesleft == 1) {
408 if (*inbytesleft > 1) {
416 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
417 char **outbuf, size_t *outbytesleft)
421 n = MIN(*inbytesleft, *outbytesleft);
423 swab(*inbuf, *outbuf, (n&~1));
429 (*outbytesleft) -= n;
433 if (*inbytesleft > 0) {
442 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
443 char **outbuf, size_t *outbytesleft)
447 n = MIN(*inbytesleft, *outbytesleft);
449 memmove(*outbuf, *inbuf, n);
452 (*outbytesleft) -= n;
456 if (*inbytesleft > 0) {
464 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
465 char **outbuf, size_t *outbytesleft)
467 size_t in_left=*inbytesleft, out_left=*outbytesleft;
468 const uint8_t *c = (const uint8_t *)*inbuf;
469 uint8_t *uc = (uint8_t *)*outbuf;
471 while (in_left >= 1 && out_left >= 2) {
472 if ((c[0] & 0x80) == 0) {
482 if ((c[0] & 0xe0) == 0xc0) {
484 (c[1] & 0xc0) != 0x80) {
488 uc[1] = (c[0]>>2) & 0x7;
489 uc[0] = (c[0]<<6) | (c[1]&0x3f);
497 if ((c[0] & 0xf0) == 0xe0) {
499 (c[1] & 0xc0) != 0x80 ||
500 (c[2] & 0xc0) != 0x80) {
504 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
505 uc[0] = (c[1]<<6) | (c[2]&0x3f);
513 if ((c[0] & 0xf8) == 0xf0) {
514 unsigned int codepoint;
516 (c[1] & 0xc0) != 0x80 ||
517 (c[2] & 0xc0) != 0x80 ||
518 (c[3] & 0xc0) != 0x80) {
527 if (codepoint < 0x10000) {
528 /* accept UTF-8 characters that are not
529 minimally packed, but pack the result */
530 uc[0] = (codepoint & 0xFF);
531 uc[1] = (codepoint >> 8);
539 codepoint -= 0x10000;
546 uc[0] = (codepoint>>10) & 0xFF;
547 uc[1] = (codepoint>>18) | 0xd8;
548 uc[2] = codepoint & 0xFF;
549 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
557 /* we don't handle 5 byte sequences */
567 *inbytesleft = in_left;
568 *outbytesleft = out_left;
569 *inbuf = (const char *)c;
570 *outbuf = (char *)uc;
574 *inbytesleft = in_left;
575 *outbytesleft = out_left;
576 *inbuf = (const char *)c;
577 *outbuf = (char *)uc;
581 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
582 char **outbuf, size_t *outbytesleft)
584 size_t in_left=*inbytesleft, out_left=*outbytesleft;
585 uint8_t *c = (uint8_t *)*outbuf;
586 const uint8_t *uc = (const uint8_t *)*inbuf;
588 while (in_left >= 2 && out_left >= 1) {
589 unsigned int codepoint;
591 if (uc[1] == 0 && !(uc[0] & 0x80)) {
601 if ((uc[1]&0xf8) == 0) {
602 /* next simplest case */
607 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
608 c[1] = 0x80 | (uc[0] & 0x3f);
616 if ((uc[1] & 0xfc) == 0xdc) {
617 /* its the second part of a 4 byte sequence. Illegal */
626 if ((uc[1] & 0xfc) != 0xd8) {
627 codepoint = uc[0] | (uc[1]<<8);
632 c[0] = 0xe0 | (codepoint >> 12);
633 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
634 c[2] = 0x80 | (codepoint & 0x3f);
643 /* its the first part of a 4 byte sequence */
648 if ((uc[3] & 0xfc) != 0xdc) {
652 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
653 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
659 c[0] = 0xf0 | (codepoint >> 18);
660 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
661 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
662 c[3] = 0x80 | (codepoint & 0x3f);
680 *inbytesleft = in_left;
681 *outbytesleft = out_left;
682 *inbuf = (const char *)uc;
688 *inbytesleft = in_left;
689 *outbytesleft = out_left;
690 *inbuf = (const char *)uc;