2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jelmer Vernooij 2002,2003
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.
25 * We have to use strcasecmp here as the character conversions
26 * haven't been initialised yet. JRA.
34 * @brief Samba wrapper/stub for iconv character set conversion.
36 * iconv is the XPG2 interface for converting between character
37 * encodings. This file provides a Samba wrapper around it, and also
38 * a simple reimplementation that is used if the system does not
41 * Samba only works with encodings that are supersets of ASCII: ascii
42 * characters like whitespace can be tested for directly, multibyte
43 * sequences start with a byte with the high bit set, and strings are
44 * terminated by a nul byte.
46 * Note that the only function provided by iconv is conversion between
47 * characters. It doesn't directly support operations like
48 * uppercasing or comparison. We have to convert to UCS-2 and compare
51 * @sa Samba Developers Guide
54 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
55 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
56 static size_t utf8_pull(void *,char **, size_t *, char **, size_t *);
57 static size_t utf8_push(void *,char **, size_t *, char **, size_t *);
58 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
59 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
60 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
62 static struct charset_functions builtin_functions[] = {
63 {"UCS-2LE", iconv_copy, iconv_copy},
64 {"UTF8", utf8_pull, utf8_push},
65 {"ASCII", ascii_pull, ascii_push},
66 {"646", ascii_pull, ascii_push},
67 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
71 static struct charset_functions *charsets = NULL;
73 static struct charset_functions *find_charset_functions(const char *name)
75 struct charset_functions *c = charsets;
78 if (strcasecmp(name, c->name) == 0) {
87 NTSTATUS smb_register_charset(struct charset_functions *funcs)
90 return NT_STATUS_INVALID_PARAMETER;
93 DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
94 /* Check whether we already have this charset... */
95 if (find_charset_functions(funcs->name)) {
96 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
97 return NT_STATUS_OBJECT_NAME_COLLISION;
100 funcs->next = funcs->prev = NULL;
101 DEBUG(5, ("Registered charset %s\n", funcs->name));
102 DLIST_ADD(charsets, funcs);
106 void lazy_initialize_iconv(void)
108 static BOOL initialized;
113 for(i = 0; builtin_functions[i].name; i++)
114 smb_register_charset(&builtin_functions[i]);
119 /* if there was an error then reset the internal state,
120 this ensures that we don't have a shift state remaining for
121 character sets like SJIS */
122 static size_t sys_iconv(void *cd,
123 char **inbuf, size_t *inbytesleft,
124 char **outbuf, size_t *outbytesleft)
126 #ifdef HAVE_NATIVE_ICONV
127 size_t ret = iconv((iconv_t)cd,
129 outbuf, outbytesleft);
130 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
139 * This is a simple portable iconv() implementaion.
141 * It only knows about a very small number of character sets - just
142 * enough that Samba works on systems that don't have iconv.
144 size_t smb_iconv(smb_iconv_t cd,
145 char **inbuf, size_t *inbytesleft,
146 char **outbuf, size_t *outbytesleft)
152 /* in many cases we can go direct */
154 return cd->direct(cd->cd_direct,
155 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
159 /* otherwise we have to do it chunks at a time */
160 while (*inbytesleft > 0) {
162 bufsize = sizeof(cvtbuf);
164 if (cd->pull(cd->cd_pull,
165 (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
166 && errno != E2BIG) return -1;
169 bufsize = sizeof(cvtbuf) - bufsize;
171 if (cd->push(cd->cd_push,
173 outbuf, outbytesleft) == -1) return -1;
180 simple iconv_open() wrapper
182 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
185 struct charset_functions *from, *to;
187 lazy_initialize_iconv();
191 ret = (smb_iconv_t)malloc(sizeof(*ret));
194 return (smb_iconv_t)-1;
196 memset(ret, 0, sizeof(*ret));
198 ret->from_name = strdup(fromcode);
199 ret->to_name = strdup(tocode);
201 /* check for the simplest null conversion */
202 if (strcasecmp(fromcode, tocode) == 0) {
203 ret->direct = iconv_copy;
207 /* check if we have a builtin function for this conversion */
208 from = find_charset_functions(fromcode);
209 if(from)ret->pull = from->pull;
211 to = find_charset_functions(tocode);
212 if(to)ret->push = to->push;
214 /* check if we can use iconv for this conversion */
215 #ifdef HAVE_NATIVE_ICONV
217 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
218 if (ret->cd_pull != (iconv_t)-1)
219 ret->pull = sys_iconv;
223 ret->cd_push = iconv_open(tocode, "UCS-2LE");
224 if (ret->cd_push != (iconv_t)-1)
225 ret->push = sys_iconv;
229 /* check if there is a module available that can do this conversion */
230 if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
231 if(!(from = find_charset_functions(fromcode)))
232 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
234 ret->pull = from->pull;
237 if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
238 if(!(to = find_charset_functions(tocode)))
239 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
241 ret->push = to->push;
244 if (!ret->push || !ret->pull) {
245 SAFE_FREE(ret->from_name);
246 SAFE_FREE(ret->to_name);
249 return (smb_iconv_t)-1;
252 /* check for conversion to/from ucs2 */
253 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
254 ret->direct = to->push;
255 ret->push = ret->pull = NULL;
259 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
260 ret->direct = from->pull;
261 ret->push = ret->pull = NULL;
265 /* Check if we can do the conversion direct */
266 #ifdef HAVE_NATIVE_ICONV
267 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
268 ret->direct = sys_iconv;
269 ret->cd_direct = ret->cd_push;
273 if (strcasecmp(tocode, "UCS-2LE") == 0) {
274 ret->direct = sys_iconv;
275 ret->cd_direct = ret->cd_pull;
285 simple iconv_close() wrapper
287 int smb_iconv_close (smb_iconv_t cd)
289 #ifdef HAVE_NATIVE_ICONV
290 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
291 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
292 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
295 SAFE_FREE(cd->from_name);
296 SAFE_FREE(cd->to_name);
298 memset(cd, 0, sizeof(*cd));
304 /**********************************************************************
305 the following functions implement the builtin character sets in Samba
306 and also the "test" character sets that are designed to test
307 multi-byte character set support for english users
308 ***********************************************************************/
310 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
311 char **outbuf, size_t *outbytesleft)
313 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
314 (*outbuf)[0] = (*inbuf)[0];
317 (*outbytesleft) -= 2;
322 if (*inbytesleft > 0) {
330 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
331 char **outbuf, size_t *outbytesleft)
335 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
336 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
337 if ((*inbuf)[1]) ir_count++;
339 (*outbytesleft) -= 1;
344 if (*inbytesleft == 1) {
349 if (*inbytesleft > 1) {
358 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
359 char **outbuf, size_t *outbytesleft)
361 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
364 if ((*inbuf)[0] != '@') {
365 /* seven bit ascii case */
366 (*outbuf)[0] = (*inbuf)[0];
369 (*outbytesleft) -= 2;
374 /* it's a hex character */
375 if (*inbytesleft < 5) {
380 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
385 (*outbuf)[0] = v&0xff;
388 (*outbytesleft) -= 2;
393 if (*inbytesleft > 0) {
401 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
402 char **outbuf, size_t *outbytesleft)
404 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
407 if ((*inbuf)[1] == 0 &&
408 ((*inbuf)[0] & 0x80) == 0 &&
409 (*inbuf)[0] != '@') {
410 (*outbuf)[0] = (*inbuf)[0];
412 (*outbytesleft) -= 1;
417 if (*outbytesleft < 5) {
421 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
422 memcpy(*outbuf, buf, 5);
424 (*outbytesleft) -= 5;
429 if (*inbytesleft == 1) {
434 if (*inbytesleft > 1) {
443 static size_t iconv_copy(void *cd, 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, char **inbuf, size_t *inbytesleft,
466 char **outbuf, size_t *outbytesleft)
468 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
469 unsigned char *c = (unsigned char *)*inbuf;
470 unsigned char *uc = (unsigned char *)*outbuf;
473 if ((c[0] & 0x80) == 0) {
476 } else if ((c[0] & 0xf0) == 0xe0) {
477 if (*inbytesleft < 3) {
478 DEBUG(0,("short utf8 char\n"));
481 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
482 uc[0] = (c[1]<<6) | (c[2]&0x3f);
484 } else if ((c[0] & 0xe0) == 0xc0) {
485 if (*inbytesleft < 2) {
486 DEBUG(0,("short utf8 char\n"));
489 uc[1] = (c[0]>>2) & 0x7;
490 uc[0] = (c[0]<<6) | (c[1]&0x3f);
495 (*inbytesleft) -= len;
496 (*outbytesleft) -= 2;
500 if (*inbytesleft > 0) {
512 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
513 char **outbuf, size_t *outbytesleft)
515 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
516 unsigned char *c = (unsigned char *)*outbuf;
517 unsigned char *uc = (unsigned char *)*inbuf;
521 if (*outbytesleft < 3) {
522 DEBUG(0,("short utf8 write\n"));
525 c[0] = 0xe0 | (uc[1]>>4);
526 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
527 c[2] = 0x80 | (uc[0]&0x3f);
529 } else if (uc[1] | (uc[0] & 0x80)) {
530 if (*outbytesleft < 2) {
531 DEBUG(0,("short utf8 write\n"));
534 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
535 c[1] = 0x80 | (uc[0]&0x3f);
543 (*outbytesleft) -= len;
548 if (*inbytesleft == 1) {
553 if (*inbytesleft > 1) {