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
38 * terminated by a nul byte.
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 *,const char **, size_t *, char **, size_t *);
49 static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
50 static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
51 static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
52 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
53 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
54 static size_t iconv_copy(void *,const 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 static 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 #ifdef HAVE_NATIVE_ICONV
99 /* if there was an error then reset the internal state,
100 this ensures that we don't have a shift state remaining for
101 character sets like SJIS */
102 static size_t sys_iconv(void *cd,
103 const char **inbuf, size_t *inbytesleft,
104 char **outbuf, size_t *outbytesleft)
106 size_t ret = iconv((iconv_t)cd,
108 outbuf, outbytesleft);
109 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
115 * This is a simple portable iconv() implementaion.
117 * It only knows about a very small number of character sets - just
118 * enough that Samba works on systems that don't have iconv.
120 size_t smb_iconv(smb_iconv_t cd,
121 const char **inbuf, size_t *inbytesleft,
122 char **outbuf, size_t *outbytesleft)
128 /* in many cases we can go direct */
130 return cd->direct(cd->cd_direct,
131 inbuf, inbytesleft, outbuf, outbytesleft);
135 /* otherwise we have to do it chunks at a time */
136 while (*inbytesleft > 0) {
138 bufsize = sizeof(cvtbuf);
140 if (cd->pull(cd->cd_pull,
141 inbuf, inbytesleft, &bufp, &bufsize) == -1
142 && errno != E2BIG) return -1;
145 bufsize = sizeof(cvtbuf) - bufsize;
147 if (cd->push(cd->cd_push,
149 outbuf, outbytesleft) == -1) return -1;
156 simple iconv_open() wrapper
158 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
161 struct charset_functions *from, *to;
163 lazy_initialize_iconv();
167 ret = (smb_iconv_t)malloc(sizeof(*ret));
170 return (smb_iconv_t)-1;
172 memset(ret, 0, sizeof(*ret));
174 ret->from_name = strdup(fromcode);
175 ret->to_name = strdup(tocode);
177 /* check for the simplest null conversion */
178 if (strcmp(fromcode, tocode) == 0) {
179 ret->direct = iconv_copy;
184 if (strcasecmp(from->name, fromcode) == 0) break;
189 if (strcasecmp(to->name, tocode) == 0) break;
193 #ifdef HAVE_NATIVE_ICONV
195 ret->pull = sys_iconv;
196 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
197 if (ret->cd_pull == (iconv_t)-1) goto failed;
201 ret->push = sys_iconv;
202 ret->cd_push = iconv_open(tocode, "UCS-2LE");
203 if (ret->cd_push == (iconv_t)-1) goto failed;
211 /* check for conversion to/from ucs2 */
212 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
213 ret->direct = to->push;
216 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
217 ret->direct = from->pull;
221 #ifdef HAVE_NATIVE_ICONV
222 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
223 ret->direct = sys_iconv;
224 ret->cd_direct = ret->cd_push;
228 if (strcasecmp(tocode, "UCS-2LE") == 0) {
229 ret->direct = sys_iconv;
230 ret->cd_direct = ret->cd_pull;
236 /* the general case has to go via a buffer */
237 if (!ret->pull) ret->pull = from->pull;
238 if (!ret->push) ret->push = to->push;
244 return (smb_iconv_t)-1;
248 simple iconv_close() wrapper
250 int smb_iconv_close (smb_iconv_t cd)
252 #ifdef HAVE_NATIVE_ICONV
253 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
254 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
255 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
258 SAFE_FREE(cd->from_name);
259 SAFE_FREE(cd->to_name);
261 memset(cd, 0, sizeof(*cd));
267 /**********************************************************************
268 the following functions implement the builtin character sets in Samba
269 and also the "test" character sets that are designed to test
270 multi-byte character set support for english users
271 ***********************************************************************/
272 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
273 char **outbuf, size_t *outbytesleft)
275 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
276 (*outbuf)[0] = (*inbuf)[0];
279 (*outbytesleft) -= 2;
284 if (*inbytesleft > 0) {
292 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
293 char **outbuf, size_t *outbytesleft)
297 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
298 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
299 if ((*inbuf)[1]) ir_count++;
301 (*outbytesleft) -= 1;
306 if (*inbytesleft == 1) {
311 if (*inbytesleft > 1) {
320 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
321 char **outbuf, size_t *outbytesleft)
323 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
326 if ((*inbuf)[0] != '@') {
327 /* seven bit ascii case */
328 (*outbuf)[0] = (*inbuf)[0];
331 (*outbytesleft) -= 2;
336 /* it's a hex character */
337 if (*inbytesleft < 5) {
342 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
347 (*outbuf)[0] = v&0xff;
350 (*outbytesleft) -= 2;
355 if (*inbytesleft > 0) {
363 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
364 char **outbuf, size_t *outbytesleft)
366 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
369 if ((*inbuf)[1] == 0 &&
370 ((*inbuf)[0] & 0x80) == 0 &&
371 (*inbuf)[0] != '@') {
372 (*outbuf)[0] = (*inbuf)[0];
374 (*outbytesleft) -= 1;
379 if (*outbytesleft < 5) {
383 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
384 memcpy(*outbuf, buf, 5);
386 (*outbytesleft) -= 5;
391 if (*inbytesleft == 1) {
396 if (*inbytesleft > 1) {
405 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
406 char **outbuf, size_t *outbytesleft)
410 n = MIN(*inbytesleft, *outbytesleft);
412 memmove(*outbuf, *inbuf, n);
415 (*outbytesleft) -= n;
419 if (*inbytesleft > 0) {
427 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
428 char **outbuf, size_t *outbytesleft)
430 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
431 const unsigned char *c = (const unsigned char *)*inbuf;
432 unsigned char *uc = (unsigned char *)*outbuf;
435 if ((c[0] & 0x80) == 0) {
438 } else if ((c[0] & 0xf0) == 0xe0) {
439 if (*inbytesleft < 3) {
440 DEBUG(0,("short utf8 char\n"));
443 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
444 uc[0] = (c[1]<<6) | (c[2]&0x3f);
446 } else if ((c[0] & 0xe0) == 0xc0) {
447 if (*inbytesleft < 2) {
448 DEBUG(0,("short utf8 char\n"));
451 uc[1] = (c[0]>>2) & 0x7;
452 uc[0] = (c[0]<<6) | (c[1]&0x3f);
457 (*inbytesleft) -= len;
458 (*outbytesleft) -= 2;
462 if (*inbytesleft > 0) {
474 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
475 char **outbuf, size_t *outbytesleft)
477 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
478 unsigned char *c = (unsigned char *)*outbuf;
479 const unsigned char *uc = (const unsigned char *)*inbuf;
483 if (*outbytesleft < 3) {
484 DEBUG(0,("short utf8 write\n"));
487 c[0] = 0xe0 | (uc[1]>>4);
488 c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
489 c[2] = 0x80 | (uc[0]&0x3f);
491 } else if (uc[1] | (uc[0] & 0x80)) {
492 if (*outbytesleft < 2) {
493 DEBUG(0,("short utf8 write\n"));
496 c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
497 c[1] = 0x80 | (uc[0]&0x3f);
505 (*outbytesleft) -= len;
510 if (*inbytesleft == 1) {
515 if (*inbytesleft > 1) {