2 Unix SMB/Netbios implementation.
4 minimal iconv implementation
5 Copyright (C) Andrew Tridgell 2001
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 weird_pull(void *,char **, size_t *, char **, size_t *);
29 static size_t weird_push(void *,char **, size_t *, char **, size_t *);
30 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
31 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
32 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
35 for each charset we have a function that pulls from that charset to
36 a ucs2 buffer, and a function that pushes to a ucs2 buffer
40 size_t (*pull)(void *, char **inbuf, size_t *inbytesleft,
41 char **outbuf, size_t *outbytesleft);
42 size_t (*push)(void *, char **inbuf, size_t *inbytesleft,
43 char **outbuf, size_t *outbytesleft);
45 {"UCS-2LE", iconv_copy, iconv_copy},
46 {"UTF8", utf8_pull, utf8_push},
47 {"ASCII", ascii_pull, ascii_push},
48 {"WEIRD", weird_pull, weird_push},
49 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
54 /* if there was an error then reset the internal state,
55 this ensures that we don't have a shift state remaining for
56 character sets like SJIS */
57 static size_t sys_iconv(void *cd,
58 char **inbuf, size_t *inbytesleft,
59 char **outbuf, size_t *outbytesleft)
61 #ifdef HAVE_NATIVE_ICONV
62 size_t ret = iconv((iconv_t)cd,
64 outbuf, outbytesleft);
65 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
74 this is a simple portable iconv() implementaion. It only knows about
75 a very small number of character sets - just enough that Samba works
76 on systems that don't have iconv
78 size_t smb_iconv(smb_iconv_t cd,
79 char **inbuf, size_t *inbytesleft,
80 char **outbuf, size_t *outbytesleft)
86 /* in many cases we can go direct */
88 return cd->direct(cd->cd_direct,
89 inbuf, inbytesleft, outbuf, outbytesleft);
93 /* otherwise we have to do it chunks at a time */
94 while (*inbytesleft > 0) {
96 bufsize = sizeof(cvtbuf);
98 if (cd->pull(cd->cd_pull,
99 inbuf, inbytesleft, &bufp, &bufsize) == -1
100 && errno != E2BIG) return -1;
103 bufsize = sizeof(cvtbuf) - bufsize;
105 if (cd->push(cd->cd_push,
107 outbuf, outbytesleft) == -1) return -1;
114 simple iconv_open() wrapper
116 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
121 ret = (smb_iconv_t)malloc(sizeof(*ret));
124 return (smb_iconv_t)-1;
126 memset(ret, 0, sizeof(*ret));
128 /* check for the simplest null conversion */
129 if (strcmp(fromcode, tocode) == 0) {
130 ret->direct = iconv_copy;
134 for (from=0; charsets[from].name; from++) {
135 if (strcasecmp(charsets[from].name, fromcode) == 0) break;
137 for (to=0; charsets[to].name; to++) {
138 if (strcasecmp(charsets[to].name, tocode) == 0) break;
141 #ifdef HAVE_NATIVE_ICONV
142 if (!charsets[from].name) {
143 ret->pull = sys_iconv;
144 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
145 if (ret->cd_pull == (iconv_t)-1) goto failed;
147 if (!charsets[to].name) {
148 ret->push = sys_iconv;
149 ret->cd_push = iconv_open(tocode, "UCS-2LE");
150 if (ret->cd_push == (iconv_t)-1) goto failed;
153 if (!charsets[from].name || !charsets[to].name) {
158 /* check for conversion to/from ucs2 */
159 if (from == 0 && charsets[to].name) {
160 ret->direct = charsets[to].push;
163 if (to == 0 && charsets[from].name) {
164 ret->direct = charsets[from].pull;
168 #ifdef HAVE_NATIVE_ICONV
170 ret->direct = sys_iconv;
171 ret->cd_direct = ret->cd_push;
176 ret->direct = sys_iconv;
177 ret->cd_direct = ret->cd_pull;
183 /* the general case has to go via a buffer */
184 if (!ret->pull) ret->pull = charsets[from].pull;
185 if (!ret->push) ret->push = charsets[to].push;
191 return (smb_iconv_t)-1;
195 simple iconv_close() wrapper
197 int smb_iconv_close (smb_iconv_t cd)
199 #ifdef HAVE_NATIVE_ICONV
200 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
201 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
202 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
205 memset(cd, 0, sizeof(*cd));
211 /**********************************************************************
212 the following functions implement the builtin character sets in Samba
213 and also the "test" character sets that are designed to test
214 multi-byte character set support for english users
215 ***********************************************************************/
217 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
218 char **outbuf, size_t *outbytesleft)
220 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
221 (*outbuf)[0] = (*inbuf)[0];
224 (*outbytesleft) -= 2;
229 if (*inbytesleft > 0) {
237 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
238 char **outbuf, size_t *outbytesleft)
242 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
243 (*outbuf)[0] = (*inbuf)[0];
244 if ((*inbuf)[1]) ir_count++;
246 (*outbytesleft) -= 1;
251 if (*inbytesleft == 1) {
256 if (*inbytesleft > 1) {
265 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
266 char **outbuf, size_t *outbytesleft)
268 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
271 if ((*inbuf)[0] != '@') {
272 /* seven bit ascii case */
273 (*outbuf)[0] = (*inbuf)[0];
276 (*outbytesleft) -= 2;
281 /* it's a hex character */
282 if (*inbytesleft < 5) {
287 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
292 (*outbuf)[0] = v&0xff;
295 (*outbytesleft) -= 2;
300 if (*inbytesleft > 0) {
308 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
309 char **outbuf, size_t *outbytesleft)
311 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
314 if ((*inbuf)[1] == 0 &&
315 ((*inbuf)[0] & 0x80) == 0 &&
316 (*inbuf)[0] != '@') {
317 (*outbuf)[0] = (*inbuf)[0];
319 (*outbytesleft) -= 1;
324 if (*outbytesleft < 5) {
328 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
329 memcpy(*outbuf, buf, 5);
331 (*outbytesleft) -= 5;
336 if (*inbytesleft == 1) {
341 if (*inbytesleft > 1) {
350 /* the "weird" character set is very useful for testing multi-byte
351 support and finding bugs. Don't use on a production system!
363 static size_t weird_pull(void *cd, char **inbuf, size_t *inbytesleft,
364 char **outbuf, size_t *outbytesleft)
366 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
369 for (i=0;weird_table[i].from;i++) {
370 if (strncmp((*inbuf),
372 weird_table[i].len) == 0) {
373 if (*inbytesleft < weird_table[i].len) {
374 DEBUG(0,("ERROR: truncated weird string\n"));
375 /* smb_panic("weird_pull"); */
378 (*outbuf)[0] = weird_table[i].from;
380 (*inbytesleft) -= weird_table[i].len;
381 (*outbytesleft) -= 2;
382 (*inbuf) += weird_table[i].len;
390 (*outbuf)[0] = (*inbuf)[0];
393 (*outbytesleft) -= 2;
398 if (*inbytesleft > 0) {
406 static size_t weird_push(void *cd, char **inbuf, size_t *inbytesleft,
407 char **outbuf, size_t *outbytesleft)
411 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
414 for (i=0;weird_table[i].from;i++) {
415 if ((*inbuf)[0] == weird_table[i].from &&
417 if (*outbytesleft < weird_table[i].len) {
418 DEBUG(0,("No room for weird character\n"));
419 /* smb_panic("weird_push"); */
421 memcpy(*outbuf, weird_table[i].to,
424 (*outbytesleft) -= weird_table[i].len;
426 (*outbuf) += weird_table[i].len;
434 (*outbuf)[0] = (*inbuf)[0];
435 if ((*inbuf)[1]) ir_count++;
437 (*outbytesleft) -= 1;
442 if (*inbytesleft == 1) {
447 if (*inbytesleft > 1) {
455 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
456 char **outbuf, size_t *outbytesleft)
460 n = MIN(*inbytesleft, *outbytesleft);
462 memmove(*outbuf, *inbuf, n);
465 (*outbytesleft) -= n;
469 if (*inbytesleft > 0) {
477 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
478 char **outbuf, size_t *outbytesleft)
480 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
481 unsigned char *c = (unsigned char *)*inbuf;
482 unsigned char *uc = (unsigned char *)*outbuf;
485 if ((c[0] & 0x80) == 0) {
488 } else if ((c[0] & 0xf0) == 0xe0) {
489 if (*inbytesleft < 3) {
490 DEBUG(0,("short utf8 char\n"));
493 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
494 uc[0] = (c[1]<<6) | (c[2]&0x3f);
496 } else if ((c[0] & 0xe0) == 0xc0) {
497 if (*inbytesleft < 2) {
498 DEBUG(0,("short utf8 char\n"));
501 uc[1] = (c[0]>>2) & 0x7;
502 uc[0] = (c[0]<<6) | (c[1]&0x3f);
507 (*inbytesleft) -= len;
508 (*outbytesleft) -= 2;
512 if (*inbytesleft > 0) {
524 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
525 char **outbuf, size_t *outbytesleft)
527 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
528 unsigned char *c = (unsigned char *)*outbuf;
529 unsigned char *uc = (unsigned char *)*inbuf;
532 if ((uc[1] & 0xf8) == 0xd8) {
533 if (*outbytesleft < 3) {
534 DEBUG(0,("short utf8 write\n"));
541 } else if (uc[1] & 0xf8) {
542 if (*outbytesleft < 3) {
543 DEBUG(0,("short utf8 write\n"));
546 c[0] = 0xe0 | (uc[1]>>4);
547 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
548 c[2] = 0x80 | (uc[0]&0x3f);
550 } else if (uc[1] | (uc[0] & 0x80)) {
551 if (*outbytesleft < 2) {
552 DEBUG(0,("short utf8 write\n"));
555 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
556 c[1] = 0x80 | (uc[0]&0x3f);
564 (*outbytesleft) -= len;
569 if (*inbytesleft == 1) {
574 if (*inbytesleft > 1) {