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.
28 * @brief Samba wrapper/stub for iconv character set conversion.
30 * iconv is the XPG2 interface for converting between character
31 * encodings. This file provides a Samba wrapper around it, and also
32 * a simple reimplementation that is used if the system does not
35 * Samba only works with encodings that are supersets of ASCII: ascii
36 * characters like whitespace can be tested for directly, multibyte
37 * sequences start with a byte with the high bit set, and strings are
40 * Note that the only function provided by iconv is conversion between
41 * characters. It doesn't directly support operations like
42 * uppercasing or comparison. We have to convert to UCS-2 and compare
45 * @sa Samba Developers Guide
48 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
49 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
50 static size_t utf8_pull(void *,char **, size_t *, char **, size_t *);
51 static size_t utf8_push(void *,char **, size_t *, char **, size_t *);
52 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
53 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
54 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
56 static struct charset_functions builtin_functions[] = {
57 {"UCS-2LE", iconv_copy, iconv_copy},
58 {"UTF8", utf8_pull, utf8_push},
59 {"ASCII", ascii_pull, ascii_push},
60 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
64 static struct charset_functions *charsets = NULL;
66 BOOL smb_register_charset(struct charset_functions *funcs)
68 struct charset_functions *c = charsets;
70 DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
71 /* Check whether we already have this charset... */
73 if(!strcasecmp(c->name, funcs->name)){
74 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
80 funcs->next = funcs->prev = NULL;
81 DEBUG(5, ("Registered charset %s\n", funcs->name));
82 DLIST_ADD(charsets, funcs);
86 void lazy_initialize_iconv(void)
88 static BOOL initialized = False;
93 for(i = 0; builtin_functions[i].name; i++)
94 smb_register_charset(&builtin_functions[i]);
98 /* if there was an error then reset the internal state,
99 this ensures that we don't have a shift state remaining for
100 character sets like SJIS */
101 static size_t sys_iconv(void *cd,
102 char **inbuf, size_t *inbytesleft,
103 char **outbuf, size_t *outbytesleft)
105 #ifdef HAVE_NATIVE_ICONV
106 size_t ret = iconv((iconv_t)cd,
108 outbuf, outbytesleft);
109 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
118 * This is a simple portable iconv() implementaion.
120 * It only knows about a very small number of character sets - just
121 * enough that Samba works on systems that don't have iconv.
123 size_t smb_iconv(smb_iconv_t cd,
124 const char **inbuf, size_t *inbytesleft,
125 char **outbuf, size_t *outbytesleft)
131 /* in many cases we can go direct */
133 return cd->direct(cd->cd_direct,
134 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
138 /* otherwise we have to do it chunks at a time */
139 while (*inbytesleft > 0) {
141 bufsize = sizeof(cvtbuf);
143 if (cd->pull(cd->cd_pull,
144 (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
145 && errno != E2BIG) return -1;
148 bufsize = sizeof(cvtbuf) - bufsize;
150 if (cd->push(cd->cd_push,
152 outbuf, outbytesleft) == -1) return -1;
159 simple iconv_open() wrapper
161 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
164 struct charset_functions *from, *to;
166 lazy_initialize_iconv();
170 ret = (smb_iconv_t)malloc(sizeof(*ret));
173 return (smb_iconv_t)-1;
175 memset(ret, 0, sizeof(*ret));
177 ret->from_name = strdup(fromcode);
178 ret->to_name = strdup(tocode);
180 /* check for the simplest null conversion */
181 if (strcmp(fromcode, tocode) == 0) {
182 ret->direct = iconv_copy;
187 if (strcasecmp(from->name, fromcode) == 0) break;
192 if (strcasecmp(to->name, tocode) == 0) break;
196 #ifdef HAVE_NATIVE_ICONV
198 ret->pull = sys_iconv;
199 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
200 if (ret->cd_pull == (iconv_t)-1) goto failed;
204 ret->push = sys_iconv;
205 ret->cd_push = iconv_open(tocode, "UCS-2LE");
206 if (ret->cd_push == (iconv_t)-1) goto failed;
214 /* check for conversion to/from ucs2 */
215 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
216 ret->direct = to->push;
219 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
220 ret->direct = from->pull;
224 #ifdef HAVE_NATIVE_ICONV
225 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
226 ret->direct = sys_iconv;
227 ret->cd_direct = ret->cd_push;
231 if (strcasecmp(tocode, "UCS-2LE") == 0) {
232 ret->direct = sys_iconv;
233 ret->cd_direct = ret->cd_pull;
239 /* the general case has to go via a buffer */
240 if (!ret->pull) ret->pull = from->pull;
241 if (!ret->push) ret->push = to->push;
247 return (smb_iconv_t)-1;
251 simple iconv_close() wrapper
253 int smb_iconv_close (smb_iconv_t cd)
255 #ifdef HAVE_NATIVE_ICONV
256 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
257 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
258 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
261 SAFE_FREE(cd->from_name);
262 SAFE_FREE(cd->to_name);
264 memset(cd, 0, sizeof(*cd));
270 /**********************************************************************
271 the following functions implement the builtin character sets in Samba
272 and also the "test" character sets that are designed to test
273 multi-byte character set support for english users
274 ***********************************************************************/
276 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
277 char **outbuf, size_t *outbytesleft)
279 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
280 (*outbuf)[0] = (*inbuf)[0];
283 (*outbytesleft) -= 2;
288 if (*inbytesleft > 0) {
296 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
297 char **outbuf, size_t *outbytesleft)
301 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
302 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
303 if ((*inbuf)[1]) ir_count++;
305 (*outbytesleft) -= 1;
310 if (*inbytesleft == 1) {
315 if (*inbytesleft > 1) {
324 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
325 char **outbuf, size_t *outbytesleft)
327 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
330 if ((*inbuf)[0] != '@') {
331 /* seven bit ascii case */
332 (*outbuf)[0] = (*inbuf)[0];
335 (*outbytesleft) -= 2;
340 /* it's a hex character */
341 if (*inbytesleft < 5) {
346 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
351 (*outbuf)[0] = v&0xff;
354 (*outbytesleft) -= 2;
359 if (*inbytesleft > 0) {
367 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
368 char **outbuf, size_t *outbytesleft)
370 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
373 if ((*inbuf)[1] == 0 &&
374 ((*inbuf)[0] & 0x80) == 0 &&
375 (*inbuf)[0] != '@') {
376 (*outbuf)[0] = (*inbuf)[0];
378 (*outbytesleft) -= 1;
383 if (*outbytesleft < 5) {
387 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
388 memcpy(*outbuf, buf, 5);
390 (*outbytesleft) -= 5;
395 if (*inbytesleft == 1) {
400 if (*inbytesleft > 1) {
409 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
410 char **outbuf, size_t *outbytesleft)
414 n = MIN(*inbytesleft, *outbytesleft);
416 memmove(*outbuf, *inbuf, n);
419 (*outbytesleft) -= n;
423 if (*inbytesleft > 0) {
431 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
432 char **outbuf, size_t *outbytesleft)
434 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
435 unsigned char *c = (unsigned char *)*inbuf;
436 unsigned char *uc = (unsigned char *)*outbuf;
439 if ((c[0] & 0x80) == 0) {
442 } else if ((c[0] & 0xf0) == 0xe0) {
443 if (*inbytesleft < 3) {
444 DEBUG(0,("short utf8 char\n"));
447 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
448 uc[0] = (c[1]<<6) | (c[2]&0x3f);
450 } else if ((c[0] & 0xe0) == 0xc0) {
451 if (*inbytesleft < 2) {
452 DEBUG(0,("short utf8 char\n"));
455 uc[1] = (c[0]>>2) & 0x7;
456 uc[0] = (c[0]<<6) | (c[1]&0x3f);
461 (*inbytesleft) -= len;
462 (*outbytesleft) -= 2;
466 if (*inbytesleft > 0) {
478 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
479 char **outbuf, size_t *outbytesleft)
481 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
482 unsigned char *c = (unsigned char *)*outbuf;
483 unsigned char *uc = (unsigned char *)*inbuf;
487 if (*outbytesleft < 3) {
488 DEBUG(0,("short utf8 write\n"));
491 c[0] = 0xe0 | (uc[1]>>4);
492 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
493 c[2] = 0x80 | (uc[0]&0x3f);
495 } else if (uc[1] | (uc[0] & 0x80)) {
496 if (*outbytesleft < 2) {
497 DEBUG(0,("short utf8 write\n"));
500 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
501 c[1] = 0x80 | (uc[0]&0x3f);
509 (*outbytesleft) -= len;
514 if (*inbytesleft == 1) {
519 if (*inbytesleft > 1) {