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"
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
204 ret->pull = sys_iconv;
205 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
206 if (ret->cd_pull == (iconv_t)-1)
207 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
208 if (ret->cd_pull == (iconv_t)-1) goto failed;
212 ret->push = sys_iconv;
213 ret->cd_push = iconv_open(tocode, "UTF-16LE");
214 if (ret->cd_push == (iconv_t)-1)
215 ret->cd_push = iconv_open(tocode, "UCS-2LE");
216 if (ret->cd_push == (iconv_t)-1) goto failed;
224 /* check for conversion to/from ucs2 */
225 if (is_utf16(fromcode) && to) {
226 ret->direct = to->push;
229 if (is_utf16(tocode) && from) {
230 ret->direct = from->pull;
234 #ifdef HAVE_NATIVE_ICONV
235 if (is_utf16(fromcode)) {
236 ret->direct = sys_iconv;
237 ret->cd_direct = ret->cd_push;
241 if (is_utf16(tocode)) {
242 ret->direct = sys_iconv;
243 ret->cd_direct = ret->cd_pull;
249 /* the general case has to go via a buffer */
250 if (!ret->pull) ret->pull = from->pull;
251 if (!ret->push) ret->push = to->push;
257 return (smb_iconv_t)-1;
261 simple iconv_close() wrapper
263 int smb_iconv_close(smb_iconv_t cd)
265 #ifdef HAVE_NATIVE_ICONV
266 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
267 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
268 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
276 /**********************************************************************
277 the following functions implement the builtin character sets in Samba
278 and also the "test" character sets that are designed to test
279 multi-byte character set support for english users
280 ***********************************************************************/
281 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
282 char **outbuf, size_t *outbytesleft)
284 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
285 (*outbuf)[0] = (*inbuf)[0];
288 (*outbytesleft) -= 2;
293 if (*inbytesleft > 0) {
301 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
302 char **outbuf, size_t *outbytesleft)
306 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
307 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
308 if ((*inbuf)[1]) ir_count++;
310 (*outbytesleft) -= 1;
315 if (*inbytesleft == 1) {
320 if (*inbytesleft > 1) {
329 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
330 char **outbuf, size_t *outbytesleft)
332 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
335 if ((*inbuf)[0] != '@') {
336 /* seven bit ascii case */
337 (*outbuf)[0] = (*inbuf)[0];
340 (*outbytesleft) -= 2;
345 /* it's a hex character */
346 if (*inbytesleft < 5) {
351 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
356 (*outbuf)[0] = v&0xff;
359 (*outbytesleft) -= 2;
364 if (*inbytesleft > 0) {
372 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
373 char **outbuf, size_t *outbytesleft)
375 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
378 if ((*inbuf)[1] == 0 &&
379 ((*inbuf)[0] & 0x80) == 0 &&
380 (*inbuf)[0] != '@') {
381 (*outbuf)[0] = (*inbuf)[0];
383 (*outbytesleft) -= 1;
388 if (*outbytesleft < 5) {
392 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
393 memcpy(*outbuf, buf, 5);
395 (*outbytesleft) -= 5;
400 if (*inbytesleft == 1) {
405 if (*inbytesleft > 1) {
413 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
414 char **outbuf, size_t *outbytesleft)
418 n = MIN(*inbytesleft, *outbytesleft);
420 swab(*inbuf, *outbuf, (n&~1));
426 (*outbytesleft) -= n;
430 if (*inbytesleft > 0) {
439 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
440 char **outbuf, size_t *outbytesleft)
444 n = MIN(*inbytesleft, *outbytesleft);
446 memmove(*outbuf, *inbuf, n);
449 (*outbytesleft) -= n;
453 if (*inbytesleft > 0) {
461 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
462 char **outbuf, size_t *outbytesleft)
464 size_t in_left=*inbytesleft, out_left=*outbytesleft;
465 const uint8_t *c = (const uint8_t *)*inbuf;
466 uint8_t *uc = (uint8_t *)*outbuf;
468 while (in_left >= 1 && out_left >= 2) {
469 if ((c[0] & 0x80) == 0) {
479 if ((c[0] & 0xe0) == 0xc0) {
481 (c[1] & 0xc0) != 0x80) {
485 uc[1] = (c[0]>>2) & 0x7;
486 uc[0] = (c[0]<<6) | (c[1]&0x3f);
494 if ((c[0] & 0xf0) == 0xe0) {
496 (c[1] & 0xc0) != 0x80 ||
497 (c[2] & 0xc0) != 0x80) {
501 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
502 uc[0] = (c[1]<<6) | (c[2]&0x3f);
510 if ((c[0] & 0xf8) == 0xf0) {
511 unsigned int codepoint;
513 (c[1] & 0xc0) != 0x80 ||
514 (c[2] & 0xc0) != 0x80 ||
515 (c[3] & 0xc0) != 0x80) {
524 if (codepoint < 0x10000) {
525 /* accept UTF-8 characters that are not
526 minimally packed, but pack the result */
527 uc[0] = (codepoint & 0xFF);
528 uc[1] = (codepoint >> 8);
536 codepoint -= 0x10000;
543 uc[0] = (codepoint>>10) & 0xFF;
544 uc[1] = (codepoint>>18) | 0xd8;
545 uc[2] = codepoint & 0xFF;
546 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
554 /* we don't handle 5 byte sequences */
564 *inbytesleft = in_left;
565 *outbytesleft = out_left;
571 *inbytesleft = in_left;
572 *outbytesleft = out_left;
578 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
579 char **outbuf, size_t *outbytesleft)
581 size_t in_left=*inbytesleft, out_left=*outbytesleft;
582 uint8_t *c = (uint8_t *)*outbuf;
583 const uint8_t *uc = (const uint8_t *)*inbuf;
585 while (in_left >= 2 && out_left >= 1) {
586 unsigned int codepoint;
588 if (uc[1] == 0 && !(uc[0] & 0x80)) {
598 if ((uc[1]&0xf8) == 0) {
599 /* next simplest case */
604 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
605 c[1] = 0x80 | (uc[0] & 0x3f);
613 if ((uc[1] & 0xfc) == 0xdc) {
614 /* its the second part of a 4 byte sequence. Illegal */
623 if ((uc[1] & 0xfc) != 0xd8) {
624 codepoint = uc[0] | (uc[1]<<8);
629 c[0] = 0xe0 | (codepoint >> 12);
630 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
631 c[2] = 0x80 | (codepoint & 0x3f);
640 /* its the first part of a 4 byte sequence */
645 if ((uc[3] & 0xfc) != 0xdc) {
649 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
650 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
656 c[0] = 0xf0 | (codepoint >> 18);
657 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
658 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
659 c[3] = 0x80 | (codepoint & 0x3f);
677 *inbytesleft = in_left;
678 *outbytesleft = out_left;
685 *inbytesleft = in_left;
686 *outbytesleft = out_left;