2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jelmer Vernooij 2002,2003
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 *,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 {"646", ascii_pull
, ascii_push
},
61 {"UCS2-HEX", ucs2hex_pull
, ucs2hex_push
},
65 static struct charset_functions
*charsets
= NULL
;
67 static struct charset_functions
*find_charset_functions(const char *name
)
69 struct charset_functions
*c
= charsets
;
72 if (strcasecmp(name
, c
->name
) == 0) {
81 NTSTATUS
smb_register_charset(struct charset_functions
*funcs
)
84 return NT_STATUS_INVALID_PARAMETER
;
87 DEBUG(5, ("Attempting to register new charset %s\n", funcs
->name
));
88 /* Check whether we already have this charset... */
89 if (find_charset_functions(funcs
->name
)) {
90 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs
->name
));
91 return NT_STATUS_OBJECT_NAME_COLLISION
;
94 funcs
->next
= funcs
->prev
= NULL
;
95 DEBUG(5, ("Registered charset %s\n", funcs
->name
));
96 DLIST_ADD(charsets
, funcs
);
100 void lazy_initialize_iconv(void)
102 static BOOL initialized
;
107 for(i
= 0; builtin_functions
[i
].name
; i
++)
108 smb_register_charset(&builtin_functions
[i
]);
113 /* if there was an error then reset the internal state,
114 this ensures that we don't have a shift state remaining for
115 character sets like SJIS */
116 static size_t sys_iconv(void *cd
,
117 char **inbuf
, size_t *inbytesleft
,
118 char **outbuf
, size_t *outbytesleft
)
120 #ifdef HAVE_NATIVE_ICONV
121 size_t ret
= iconv((iconv_t
)cd
,
123 outbuf
, outbytesleft
);
124 if (ret
== (size_t)-1) iconv(cd
, NULL
, NULL
, NULL
, NULL
);
133 * This is a simple portable iconv() implementaion.
135 * It only knows about a very small number of character sets - just
136 * enough that Samba works on systems that don't have iconv.
138 size_t smb_iconv(smb_iconv_t cd
,
139 char **inbuf
, size_t *inbytesleft
,
140 char **outbuf
, size_t *outbytesleft
)
146 /* in many cases we can go direct */
148 return cd
->direct(cd
->cd_direct
,
149 (char **)inbuf
, inbytesleft
, outbuf
, outbytesleft
);
153 /* otherwise we have to do it chunks at a time */
154 while (*inbytesleft
> 0) {
156 bufsize
= sizeof(cvtbuf
);
158 if (cd
->pull(cd
->cd_pull
,
159 (char **)inbuf
, inbytesleft
, &bufp
, &bufsize
) == -1
160 && errno
!= E2BIG
) return -1;
163 bufsize
= sizeof(cvtbuf
) - bufsize
;
165 if (cd
->push(cd
->cd_push
,
167 outbuf
, outbytesleft
) == -1) return -1;
174 simple iconv_open() wrapper
176 smb_iconv_t
smb_iconv_open(const char *tocode
, const char *fromcode
)
179 struct charset_functions
*from
, *to
;
181 lazy_initialize_iconv();
185 ret
= (smb_iconv_t
)malloc(sizeof(*ret
));
188 return (smb_iconv_t
)-1;
190 memset(ret
, 0, sizeof(*ret
));
192 ret
->from_name
= strdup(fromcode
);
193 ret
->to_name
= strdup(tocode
);
195 /* check for the simplest null conversion */
196 if (strcasecmp(fromcode
, tocode
) == 0) {
197 ret
->direct
= iconv_copy
;
201 /* check if we have a builtin function for this conversion */
202 from
= find_charset_functions(fromcode
);
203 if(from
)ret
->pull
= from
->pull
;
205 to
= find_charset_functions(tocode
);
206 if(to
)ret
->push
= to
->push
;
208 /* check if we can use iconv for this conversion */
209 #ifdef HAVE_NATIVE_ICONV
211 ret
->cd_pull
= iconv_open("UCS-2LE", fromcode
);
212 if (ret
->cd_pull
!= (iconv_t
)-1)
213 ret
->pull
= sys_iconv
;
217 ret
->cd_push
= iconv_open(tocode
, "UCS-2LE");
218 if (ret
->cd_push
!= (iconv_t
)-1)
219 ret
->push
= sys_iconv
;
223 /* check if there is a module available that can do this conversion */
224 if (!ret
->pull
&& NT_STATUS_IS_OK(smb_probe_module("charset", fromcode
))) {
225 if(!(from
= find_charset_functions(fromcode
)))
226 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode
, fromcode
));
228 ret
->pull
= from
->pull
;
231 if (!ret
->push
&& NT_STATUS_IS_OK(smb_probe_module("charset", tocode
))) {
232 if(!(to
= find_charset_functions(tocode
)))
233 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode
, tocode
));
235 ret
->push
= to
->push
;
238 if (!ret
->push
|| !ret
->pull
) {
239 SAFE_FREE(ret
->from_name
);
240 SAFE_FREE(ret
->to_name
);
243 return (smb_iconv_t
)-1;
246 /* check for conversion to/from ucs2 */
247 if (strcasecmp(fromcode
, "UCS-2LE") == 0 && to
) {
248 ret
->direct
= to
->push
;
249 ret
->push
= ret
->pull
= NULL
;
253 if (strcasecmp(tocode
, "UCS-2LE") == 0 && from
) {
254 ret
->direct
= from
->pull
;
255 ret
->push
= ret
->pull
= NULL
;
259 /* Check if we can do the conversion direct */
260 #ifdef HAVE_NATIVE_ICONV
261 if (strcasecmp(fromcode
, "UCS-2LE") == 0) {
262 ret
->direct
= sys_iconv
;
263 ret
->cd_direct
= ret
->cd_push
;
267 if (strcasecmp(tocode
, "UCS-2LE") == 0) {
268 ret
->direct
= sys_iconv
;
269 ret
->cd_direct
= ret
->cd_pull
;
279 simple iconv_close() wrapper
281 int smb_iconv_close (smb_iconv_t cd
)
283 #ifdef HAVE_NATIVE_ICONV
284 if (cd
->cd_direct
) iconv_close((iconv_t
)cd
->cd_direct
);
285 if (cd
->cd_pull
) iconv_close((iconv_t
)cd
->cd_pull
);
286 if (cd
->cd_push
) iconv_close((iconv_t
)cd
->cd_push
);
289 SAFE_FREE(cd
->from_name
);
290 SAFE_FREE(cd
->to_name
);
292 memset(cd
, 0, sizeof(*cd
));
298 /**********************************************************************
299 the following functions implement the builtin character sets in Samba
300 and also the "test" character sets that are designed to test
301 multi-byte character set support for english users
302 ***********************************************************************/
304 static size_t ascii_pull(void *cd
, char **inbuf
, size_t *inbytesleft
,
305 char **outbuf
, size_t *outbytesleft
)
307 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
308 (*outbuf
)[0] = (*inbuf
)[0];
311 (*outbytesleft
) -= 2;
316 if (*inbytesleft
> 0) {
324 static size_t ascii_push(void *cd
, char **inbuf
, size_t *inbytesleft
,
325 char **outbuf
, size_t *outbytesleft
)
329 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
330 (*outbuf
)[0] = (*inbuf
)[0] & 0x7F;
331 if ((*inbuf
)[1]) ir_count
++;
333 (*outbytesleft
) -= 1;
338 if (*inbytesleft
== 1) {
343 if (*inbytesleft
> 1) {
352 static size_t ucs2hex_pull(void *cd
, char **inbuf
, size_t *inbytesleft
,
353 char **outbuf
, size_t *outbytesleft
)
355 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
358 if ((*inbuf
)[0] != '@') {
359 /* seven bit ascii case */
360 (*outbuf
)[0] = (*inbuf
)[0];
363 (*outbytesleft
) -= 2;
368 /* it's a hex character */
369 if (*inbytesleft
< 5) {
374 if (sscanf(&(*inbuf
)[1], "%04x", &v
) != 1) {
379 (*outbuf
)[0] = v
&0xff;
382 (*outbytesleft
) -= 2;
387 if (*inbytesleft
> 0) {
395 static size_t ucs2hex_push(void *cd
, char **inbuf
, size_t *inbytesleft
,
396 char **outbuf
, size_t *outbytesleft
)
398 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
401 if ((*inbuf
)[1] == 0 &&
402 ((*inbuf
)[0] & 0x80) == 0 &&
403 (*inbuf
)[0] != '@') {
404 (*outbuf
)[0] = (*inbuf
)[0];
406 (*outbytesleft
) -= 1;
411 if (*outbytesleft
< 5) {
415 snprintf(buf
, 6, "@%04x", SVAL(*inbuf
, 0));
416 memcpy(*outbuf
, buf
, 5);
418 (*outbytesleft
) -= 5;
423 if (*inbytesleft
== 1) {
428 if (*inbytesleft
> 1) {
437 static size_t iconv_copy(void *cd
, char **inbuf
, size_t *inbytesleft
,
438 char **outbuf
, size_t *outbytesleft
)
442 n
= MIN(*inbytesleft
, *outbytesleft
);
444 memmove(*outbuf
, *inbuf
, n
);
447 (*outbytesleft
) -= n
;
451 if (*inbytesleft
> 0) {
459 static size_t utf8_pull(void *cd
, char **inbuf
, size_t *inbytesleft
,
460 char **outbuf
, size_t *outbytesleft
)
462 while (*inbytesleft
>= 1 && *outbytesleft
>= 2) {
463 unsigned char *c
= (unsigned char *)*inbuf
;
464 unsigned char *uc
= (unsigned char *)*outbuf
;
467 if ((c
[0] & 0x80) == 0) {
470 } else if ((c
[0] & 0xf0) == 0xe0) {
471 if (*inbytesleft
< 3) {
472 DEBUG(0,("short utf8 char\n"));
475 uc
[1] = ((c
[0]&0xF)<<4) | ((c
[1]>>2)&0xF);
476 uc
[0] = (c
[1]<<6) | (c
[2]&0x3f);
478 } else if ((c
[0] & 0xe0) == 0xc0) {
479 if (*inbytesleft
< 2) {
480 DEBUG(0,("short utf8 char\n"));
483 uc
[1] = (c
[0]>>2) & 0x7;
484 uc
[0] = (c
[0]<<6) | (c
[1]&0x3f);
489 (*inbytesleft
) -= len
;
490 (*outbytesleft
) -= 2;
494 if (*inbytesleft
> 0) {
506 static size_t utf8_push(void *cd
, char **inbuf
, size_t *inbytesleft
,
507 char **outbuf
, size_t *outbytesleft
)
509 while (*inbytesleft
>= 2 && *outbytesleft
>= 1) {
510 unsigned char *c
= (unsigned char *)*outbuf
;
511 unsigned char *uc
= (unsigned char *)*inbuf
;
515 if (*outbytesleft
< 3) {
516 DEBUG(0,("short utf8 write\n"));
519 c
[0] = 0xe0 | (uc
[1]>>4);
520 c
[1] = 0x80 | ((uc
[1]&0xF)<<2) | (uc
[0]>>6);
521 c
[2] = 0x80 | (uc
[0]&0x3f);
523 } else if (uc
[1] | (uc
[0] & 0x80)) {
524 if (*outbytesleft
< 2) {
525 DEBUG(0,("short utf8 write\n"));
528 c
[0] = 0xc0 | (uc
[1]<<2) | (uc
[0]>>6);
529 c
[1] = 0x80 | (uc
[0]&0x3f);
537 (*outbytesleft
) -= len
;
542 if (*inbytesleft
== 1) {
547 if (*inbytesleft
> 1) {