2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
24 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
25 static size_t utf8_pull(void *,char **, size_t *, char **, size_t *);
26 static size_t utf8_push(void *,char **, size_t *, char **, size_t *);
27 static size_t weird_pull(void *,char **, size_t *, char **, size_t *);
28 static size_t weird_push(void *,char **, size_t *, char **, size_t *);
29 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
30 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
31 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
34 for each charset we have a function that pulls from that charset to
35 a ucs2 buffer, and a function that pushes to a ucs2 buffer
39 size_t (*pull)(void *, char **inbuf, size_t *inbytesleft,
40 char **outbuf, size_t *outbytesleft);
41 size_t (*push)(void *, char **inbuf, size_t *inbytesleft,
42 char **outbuf, size_t *outbytesleft);
44 {"UCS-2LE", iconv_copy, iconv_copy},
45 {"UTF8", utf8_pull, utf8_push},
46 {"ASCII", ascii_pull, ascii_push},
47 {"WEIRD", weird_pull, weird_push},
48 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
53 /* if there was an error then reset the internal state,
54 this ensures that we don't have a shift state remaining for
55 character sets like SJIS */
56 static size_t sys_iconv(void *cd,
57 char **inbuf, size_t *inbytesleft,
58 char **outbuf, size_t *outbytesleft)
60 #ifdef HAVE_NATIVE_ICONV
61 size_t ret = iconv((iconv_t)cd,
63 outbuf, outbytesleft);
64 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
73 this is a simple portable iconv() implementaion. It only knows about
74 a very small number of character sets - just enough that Samba works
75 on systems that don't have iconv
77 size_t smb_iconv(smb_iconv_t cd,
78 const char **inbuf, size_t *inbytesleft,
79 char **outbuf, size_t *outbytesleft)
85 /* in many cases we can go direct */
87 return cd->direct(cd->cd_direct,
88 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
92 /* otherwise we have to do it chunks at a time */
93 while (*inbytesleft > 0) {
95 bufsize = sizeof(cvtbuf);
97 if (cd->pull(cd->cd_pull,
98 (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
99 && errno != E2BIG) return -1;
102 bufsize = sizeof(cvtbuf) - bufsize;
104 if (cd->push(cd->cd_push,
106 outbuf, outbytesleft) == -1) return -1;
113 simple iconv_open() wrapper
115 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
120 ret = (smb_iconv_t)malloc(sizeof(*ret));
123 return (smb_iconv_t)-1;
125 memset(ret, 0, sizeof(*ret));
127 ret->from_name = strdup(fromcode);
128 ret->to_name = strdup(tocode);
130 /* check for the simplest null conversion */
131 if (strcmp(fromcode, tocode) == 0) {
132 ret->direct = iconv_copy;
136 for (from=0; charsets[from].name; from++) {
137 if (strcasecmp(charsets[from].name, fromcode) == 0) break;
139 for (to=0; charsets[to].name; to++) {
140 if (strcasecmp(charsets[to].name, tocode) == 0) break;
143 #ifdef HAVE_NATIVE_ICONV
144 if (!charsets[from].name) {
145 ret->pull = sys_iconv;
146 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
147 if (ret->cd_pull == (iconv_t)-1) goto failed;
149 if (!charsets[to].name) {
150 ret->push = sys_iconv;
151 ret->cd_push = iconv_open(tocode, "UCS-2LE");
152 if (ret->cd_push == (iconv_t)-1) goto failed;
155 if (!charsets[from].name || !charsets[to].name) {
160 /* check for conversion to/from ucs2 */
161 if (from == 0 && charsets[to].name) {
162 ret->direct = charsets[to].push;
165 if (to == 0 && charsets[from].name) {
166 ret->direct = charsets[from].pull;
170 #ifdef HAVE_NATIVE_ICONV
172 ret->direct = sys_iconv;
173 ret->cd_direct = ret->cd_push;
178 ret->direct = sys_iconv;
179 ret->cd_direct = ret->cd_pull;
185 /* the general case has to go via a buffer */
186 if (!ret->pull) ret->pull = charsets[from].pull;
187 if (!ret->push) ret->push = charsets[to].push;
193 return (smb_iconv_t)-1;
197 simple iconv_close() wrapper
199 int smb_iconv_close (smb_iconv_t cd)
201 #ifdef HAVE_NATIVE_ICONV
202 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
203 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
204 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
207 SAFE_FREE(cd->from_name);
208 SAFE_FREE(cd->to_name);
210 memset(cd, 0, sizeof(*cd));
216 /**********************************************************************
217 the following functions implement the builtin character sets in Samba
218 and also the "test" character sets that are designed to test
219 multi-byte character set support for english users
220 ***********************************************************************/
222 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
223 char **outbuf, size_t *outbytesleft)
225 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
226 (*outbuf)[0] = (*inbuf)[0];
229 (*outbytesleft) -= 2;
234 if (*inbytesleft > 0) {
242 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
243 char **outbuf, size_t *outbytesleft)
247 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
248 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
249 if ((*inbuf)[1]) ir_count++;
251 (*outbytesleft) -= 1;
256 if (*inbytesleft == 1) {
261 if (*inbytesleft > 1) {
270 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
271 char **outbuf, size_t *outbytesleft)
273 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
276 if ((*inbuf)[0] != '@') {
277 /* seven bit ascii case */
278 (*outbuf)[0] = (*inbuf)[0];
281 (*outbytesleft) -= 2;
286 /* it's a hex character */
287 if (*inbytesleft < 5) {
292 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
297 (*outbuf)[0] = v&0xff;
300 (*outbytesleft) -= 2;
305 if (*inbytesleft > 0) {
313 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
314 char **outbuf, size_t *outbytesleft)
316 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
319 if ((*inbuf)[1] == 0 &&
320 ((*inbuf)[0] & 0x80) == 0 &&
321 (*inbuf)[0] != '@') {
322 (*outbuf)[0] = (*inbuf)[0];
324 (*outbytesleft) -= 1;
329 if (*outbytesleft < 5) {
333 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
334 memcpy(*outbuf, buf, 5);
336 (*outbytesleft) -= 5;
341 if (*inbytesleft == 1) {
346 if (*inbytesleft > 1) {
355 /* the "weird" character set is very useful for testing multi-byte
356 support and finding bugs. Don't use on a production system!
368 static size_t weird_pull(void *cd, char **inbuf, size_t *inbytesleft,
369 char **outbuf, size_t *outbytesleft)
371 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
374 for (i=0;weird_table[i].from;i++) {
375 if (strncmp((*inbuf),
377 weird_table[i].len) == 0) {
378 if (*inbytesleft < weird_table[i].len) {
379 DEBUG(0,("ERROR: truncated weird string\n"));
380 /* smb_panic("weird_pull"); */
383 (*outbuf)[0] = weird_table[i].from;
385 (*inbytesleft) -= weird_table[i].len;
386 (*outbytesleft) -= 2;
387 (*inbuf) += weird_table[i].len;
395 (*outbuf)[0] = (*inbuf)[0];
398 (*outbytesleft) -= 2;
403 if (*inbytesleft > 0) {
411 static size_t weird_push(void *cd, char **inbuf, size_t *inbytesleft,
412 char **outbuf, size_t *outbytesleft)
416 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
419 for (i=0;weird_table[i].from;i++) {
420 if ((*inbuf)[0] == weird_table[i].from &&
422 if (*outbytesleft < weird_table[i].len) {
423 DEBUG(0,("No room for weird character\n"));
424 /* smb_panic("weird_push"); */
426 memcpy(*outbuf, weird_table[i].to,
429 (*outbytesleft) -= weird_table[i].len;
431 (*outbuf) += weird_table[i].len;
439 (*outbuf)[0] = (*inbuf)[0];
440 if ((*inbuf)[1]) ir_count++;
442 (*outbytesleft) -= 1;
447 if (*inbytesleft == 1) {
452 if (*inbytesleft > 1) {
460 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
461 char **outbuf, size_t *outbytesleft)
465 n = MIN(*inbytesleft, *outbytesleft);
467 memmove(*outbuf, *inbuf, n);
470 (*outbytesleft) -= n;
474 if (*inbytesleft > 0) {
482 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
483 char **outbuf, size_t *outbytesleft)
485 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
486 unsigned char *c = (unsigned char *)*inbuf;
487 unsigned char *uc = (unsigned char *)*outbuf;
490 if ((c[0] & 0x80) == 0) {
493 } else if ((c[0] & 0xf0) == 0xe0) {
494 if (*inbytesleft < 3) {
495 DEBUG(0,("short utf8 char\n"));
498 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
499 uc[0] = (c[1]<<6) | (c[2]&0x3f);
501 } else if ((c[0] & 0xe0) == 0xc0) {
502 if (*inbytesleft < 2) {
503 DEBUG(0,("short utf8 char\n"));
506 uc[1] = (c[0]>>2) & 0x7;
507 uc[0] = (c[0]<<6) | (c[1]&0x3f);
512 (*inbytesleft) -= len;
513 (*outbytesleft) -= 2;
517 if (*inbytesleft > 0) {
529 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
530 char **outbuf, size_t *outbytesleft)
532 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
533 unsigned char *c = (unsigned char *)*outbuf;
534 unsigned char *uc = (unsigned char *)*inbuf;
538 if (*outbytesleft < 3) {
539 DEBUG(0,("short utf8 write\n"));
542 c[0] = 0xe0 | (uc[1]>>4);
543 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
544 c[2] = 0x80 | (uc[0]&0x3f);
546 } else if (uc[1] | (uc[0] & 0x80)) {
547 if (*outbytesleft < 2) {
548 DEBUG(0,("short utf8 write\n"));
551 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
552 c[1] = 0x80 | (uc[0]&0x3f);
560 (*outbytesleft) -= len;
565 if (*inbytesleft == 1) {
570 if (*inbytesleft > 1) {