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.
24 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
25 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
26 static size_t utf8_pull(void *,char **, size_t *, char **, size_t *);
27 static size_t utf8_push(void *,char **, size_t *, char **, size_t *);
28 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
29 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
30 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
32 struct charset_functions builtin_functions[] = {
33 {"UCS-2LE", iconv_copy, iconv_copy},
34 {"UTF8", utf8_pull, utf8_push},
35 {"ASCII", ascii_pull, ascii_push},
36 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
40 static struct charset_functions *charsets = NULL;
42 BOOL smb_register_charset(struct charset_functions *funcs)
44 struct charset_functions *c = charsets;
46 DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
47 /* Check whether we already have this charset... */
49 if(!strcasecmp(c->name, funcs->name)){
50 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
56 funcs->next = funcs->prev = NULL;
57 DEBUG(5, ("Registered charset %s\n", funcs->name));
58 DLIST_ADD(charsets, funcs);
62 void lazy_initialize_iconv(void)
64 static BOOL initialized = False;
69 for(i = 0; builtin_functions[i].name; i++)
70 smb_register_charset(&builtin_functions[i]);
74 /* if there was an error then reset the internal state,
75 this ensures that we don't have a shift state remaining for
76 character sets like SJIS */
77 static size_t sys_iconv(void *cd,
78 char **inbuf, size_t *inbytesleft,
79 char **outbuf, size_t *outbytesleft)
81 #ifdef HAVE_NATIVE_ICONV
82 size_t ret = iconv((iconv_t)cd,
84 outbuf, outbytesleft);
85 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
94 this is a simple portable iconv() implementaion. It only knows about
95 a very small number of character sets - just enough that Samba works
96 on systems that don't have iconv
98 size_t smb_iconv(smb_iconv_t cd,
99 const char **inbuf, size_t *inbytesleft,
100 char **outbuf, size_t *outbytesleft)
106 /* in many cases we can go direct */
108 return cd->direct(cd->cd_direct,
109 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
113 /* otherwise we have to do it chunks at a time */
114 while (*inbytesleft > 0) {
116 bufsize = sizeof(cvtbuf);
118 if (cd->pull(cd->cd_pull,
119 (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
120 && errno != E2BIG) return -1;
123 bufsize = sizeof(cvtbuf) - bufsize;
125 if (cd->push(cd->cd_push,
127 outbuf, outbytesleft) == -1) return -1;
134 simple iconv_open() wrapper
136 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
139 struct charset_functions *from, *to;
141 lazy_initialize_iconv();
145 ret = (smb_iconv_t)malloc(sizeof(*ret));
148 return (smb_iconv_t)-1;
150 memset(ret, 0, sizeof(*ret));
152 ret->from_name = strdup(fromcode);
153 ret->to_name = strdup(tocode);
155 /* check for the simplest null conversion */
156 if (strcmp(fromcode, tocode) == 0) {
157 ret->direct = iconv_copy;
162 if (strcasecmp(from->name, fromcode) == 0) break;
167 if (strcasecmp(to->name, tocode) == 0) break;
171 #ifdef HAVE_NATIVE_ICONV
173 ret->pull = sys_iconv;
174 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
175 if (ret->cd_pull == (iconv_t)-1) goto failed;
179 ret->push = sys_iconv;
180 ret->cd_push = iconv_open(tocode, "UCS-2LE");
181 if (ret->cd_push == (iconv_t)-1) goto failed;
189 /* check for conversion to/from ucs2 */
190 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
191 ret->direct = to->push;
194 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
195 ret->direct = from->pull;
199 #ifdef HAVE_NATIVE_ICONV
200 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
201 ret->direct = sys_iconv;
202 ret->cd_direct = ret->cd_push;
206 if (strcasecmp(tocode, "UCS-2LE") == 0) {
207 ret->direct = sys_iconv;
208 ret->cd_direct = ret->cd_pull;
214 /* the general case has to go via a buffer */
215 if (!ret->pull) ret->pull = from->pull;
216 if (!ret->push) ret->push = to->push;
222 return (smb_iconv_t)-1;
226 simple iconv_close() wrapper
228 int smb_iconv_close (smb_iconv_t cd)
230 #ifdef HAVE_NATIVE_ICONV
231 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
232 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
233 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
236 SAFE_FREE(cd->from_name);
237 SAFE_FREE(cd->to_name);
239 memset(cd, 0, sizeof(*cd));
245 /**********************************************************************
246 the following functions implement the builtin character sets in Samba
247 and also the "test" character sets that are designed to test
248 multi-byte character set support for english users
249 ***********************************************************************/
251 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
252 char **outbuf, size_t *outbytesleft)
254 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
255 (*outbuf)[0] = (*inbuf)[0];
258 (*outbytesleft) -= 2;
263 if (*inbytesleft > 0) {
271 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
272 char **outbuf, size_t *outbytesleft)
276 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
277 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
278 if ((*inbuf)[1]) ir_count++;
280 (*outbytesleft) -= 1;
285 if (*inbytesleft == 1) {
290 if (*inbytesleft > 1) {
299 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
300 char **outbuf, size_t *outbytesleft)
302 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
305 if ((*inbuf)[0] != '@') {
306 /* seven bit ascii case */
307 (*outbuf)[0] = (*inbuf)[0];
310 (*outbytesleft) -= 2;
315 /* it's a hex character */
316 if (*inbytesleft < 5) {
321 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
326 (*outbuf)[0] = v&0xff;
329 (*outbytesleft) -= 2;
334 if (*inbytesleft > 0) {
342 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
343 char **outbuf, size_t *outbytesleft)
345 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
348 if ((*inbuf)[1] == 0 &&
349 ((*inbuf)[0] & 0x80) == 0 &&
350 (*inbuf)[0] != '@') {
351 (*outbuf)[0] = (*inbuf)[0];
353 (*outbytesleft) -= 1;
358 if (*outbytesleft < 5) {
362 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
363 memcpy(*outbuf, buf, 5);
365 (*outbytesleft) -= 5;
370 if (*inbytesleft == 1) {
375 if (*inbytesleft > 1) {
384 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
385 char **outbuf, size_t *outbytesleft)
389 n = MIN(*inbytesleft, *outbytesleft);
391 memmove(*outbuf, *inbuf, n);
394 (*outbytesleft) -= n;
398 if (*inbytesleft > 0) {
406 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
407 char **outbuf, size_t *outbytesleft)
409 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
410 unsigned char *c = (unsigned char *)*inbuf;
411 unsigned char *uc = (unsigned char *)*outbuf;
414 if ((c[0] & 0x80) == 0) {
417 } else if ((c[0] & 0xf0) == 0xe0) {
418 if (*inbytesleft < 3) {
419 DEBUG(0,("short utf8 char\n"));
422 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
423 uc[0] = (c[1]<<6) | (c[2]&0x3f);
425 } else if ((c[0] & 0xe0) == 0xc0) {
426 if (*inbytesleft < 2) {
427 DEBUG(0,("short utf8 char\n"));
430 uc[1] = (c[0]>>2) & 0x7;
431 uc[0] = (c[0]<<6) | (c[1]&0x3f);
436 (*inbytesleft) -= len;
437 (*outbytesleft) -= 2;
441 if (*inbytesleft > 0) {
453 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
454 char **outbuf, size_t *outbytesleft)
456 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
457 unsigned char *c = (unsigned char *)*outbuf;
458 unsigned char *uc = (unsigned char *)*inbuf;
462 if (*outbytesleft < 3) {
463 DEBUG(0,("short utf8 write\n"));
466 c[0] = 0xe0 | (uc[1]>>4);
467 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
468 c[2] = 0x80 | (uc[0]&0x3f);
470 } else if (uc[1] | (uc[0] & 0x80)) {
471 if (*outbytesleft < 2) {
472 DEBUG(0,("short utf8 write\n"));
475 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
476 c[1] = 0x80 | (uc[0]&0x3f);
484 (*outbytesleft) -= len;
489 if (*inbytesleft == 1) {
494 if (*inbytesleft > 1) {