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.
21 From samba 3.0 beta and GNU libiconv-1.8
22 It's bad but most of the time we can't use libc iconv service:
23 - it doesn't round trip for most encoding
24 - it doesn't know about Apple extension
29 #endif /* HAVE_CONFIG_H */
36 #include <sys/param.h>
38 #include <atalk/logger.h>
41 #include <netatalk/endian.h>
42 #include <atalk/unicode.h>
44 #ifdef HAVE_USABLE_ICONV
52 * @brief Samba wrapper/stub for iconv character set conversion.
54 * iconv is the XPG2 interface for converting between character
55 * encodings. This file provides a Samba wrapper around it, and also
56 * a simple reimplementation that is used if the system does not
59 * Samba only works with encodings that are supersets of ASCII: ascii
60 * characters like whitespace can be tested for directly, multibyte
61 * sequences start with a byte with the high bit set, and strings are
62 * terminated by a nul byte.
64 * Note that the only function provided by iconv is conversion between
65 * characters. It doesn't directly support operations like
66 * uppercasing or comparison. We have to convert to UCS-2 and compare
69 * @sa Samba Developers Guide
71 #define CHARSET_WIDECHAR 32
73 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
74 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
75 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
77 static char hexdig[] = "0123456789abcdef";
78 #define hextoint( c ) ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
80 struct charset_functions charset_ucs2 =
89 struct charset_functions charset_ascii =
95 CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED
98 struct charset_functions charset_iconv =
104 CHARSET_ICONV | CHARSET_PRECOMPOSED
108 extern struct charset_functions charset_mac_roman;
109 extern struct charset_functions charset_mac_hebrew;
110 extern struct charset_functions charset_mac_centraleurope;
111 extern struct charset_functions charset_mac_cyrillic;
112 extern struct charset_functions charset_mac_turkish;
113 extern struct charset_functions charset_utf8;
114 extern struct charset_functions charset_utf8_mac;
117 static struct charset_functions builtin_functions[] = {
118 {"UCS-2LE", 0, iconv_copy, iconv_copy, CHARSET_WIDECHAR},
119 {"ASCII", 0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED},
120 {NULL, 0, NULL, NULL, 0}
124 #define DLIST_ADD(list, p) \
128 (p)->next = (p)->prev = NULL; \
130 (list)->prev = (p); \
131 (p)->next = (list); \
137 static struct charset_functions *charsets = NULL;
139 struct charset_functions *find_charset_functions(const char *name)
141 struct charset_functions *c = charsets;
144 if (strcasecmp(name, c->name) == 0) {
153 int atalk_register_charset(struct charset_functions *funcs)
159 /* Check whether we already have this charset... */
160 if (find_charset_functions(funcs->name)) {
161 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
165 funcs->next = funcs->prev = NULL;
166 DLIST_ADD(charsets, funcs);
170 void lazy_initialize_iconv(void)
172 static int initialized = 0;
177 for(i = 0; builtin_functions[i].name; i++)
178 atalk_register_charset(&builtin_functions[i]);
180 /* register additional charsets */
181 atalk_register_charset(&charset_utf8);
182 atalk_register_charset(&charset_utf8_mac);
183 atalk_register_charset(&charset_mac_roman);
184 atalk_register_charset(&charset_mac_hebrew);
185 atalk_register_charset(&charset_mac_turkish);
186 atalk_register_charset(&charset_mac_centraleurope);
187 atalk_register_charset(&charset_mac_cyrillic);
191 /* if there was an error then reset the internal state,
192 this ensures that we don't have a shift state remaining for
193 character sets like SJIS */
194 static size_t sys_iconv(void *cd,
195 char **inbuf, size_t *inbytesleft,
196 char **outbuf, size_t *outbytesleft)
198 #ifdef HAVE_USABLE_ICONV
199 size_t ret = iconv((iconv_t)cd,
201 outbuf, outbytesleft);
202 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
211 * This is a simple portable iconv() implementaion.
213 * It only knows about a very small number of character sets - just
214 * enough that netatalk works on systems that don't have iconv.
216 size_t atalk_iconv(atalk_iconv_t cd,
217 const char **inbuf, size_t *inbytesleft,
218 char **outbuf, size_t *outbytesleft)
224 /* in many cases we can go direct */
226 return cd->direct(cd->cd_direct,
227 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
231 /* otherwise we have to do it chunks at a time */
232 while (*inbytesleft > 0) {
234 bufsize = sizeof(cvtbuf);
236 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
242 bufsize = sizeof(cvtbuf) - bufsize;
244 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
253 size_t atalk_iconv_ignore(atalk_iconv_t cd,
254 const char **inbuf, size_t *inbytesleft,
255 char **outbuf, size_t *outbytesleft, int *ignore)
260 size_t outlen = *outbytesleft;
263 /* in many cases we can go direct */
267 if ((size_t)-1 == cd->direct(cd->cd_direct, (char **)inbuf, inbytesleft, outbuf, outbytesleft)) {
268 if (errno == EILSEQ) {
269 o_save[outlen-*outbytesleft] = '_';
270 (*outbuf) = o_save + outlen-*outbytesleft+1;
282 /* we have to do it chunks at a time */
283 while (*inbytesleft > 0) {
285 bufsize = sizeof(cvtbuf);
287 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
293 bufsize = sizeof(cvtbuf) - bufsize;
297 if (cd->push(cd->cd_push,
299 outbuf, outbytesleft) == (size_t)-1) {
300 if (errno == EILSEQ) {
301 o_save[outlen-*outbytesleft] = '_';
302 (*outbuf) = o_save + outlen-*outbytesleft+1;
317 simple iconv_open() wrapper
319 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
322 struct charset_functions *from, *to;
325 lazy_initialize_iconv();
329 ret = (atalk_iconv_t)malloc(sizeof(*ret));
332 return (atalk_iconv_t)-1;
334 memset(ret, 0, sizeof(*ret));
336 ret->from_name = strdup(fromcode);
337 ret->to_name = strdup(tocode);
339 /* check for the simplest null conversion */
340 if (strcasecmp(fromcode, tocode) == 0) {
341 ret->direct = iconv_copy;
345 /* check if we have a builtin function for this conversion */
346 from = find_charset_functions(fromcode);
347 if(from)ret->pull = from->pull;
349 to = find_charset_functions(tocode);
350 if(to)ret->push = to->push;
352 /* check if we can use iconv for this conversion */
353 #ifdef HAVE_USABLE_ICONV
355 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
356 if (ret->cd_pull != (iconv_t)-1)
357 ret->pull = sys_iconv;
361 ret->cd_push = iconv_open(tocode, "UCS-2LE");
362 if (ret->cd_push != (iconv_t)-1)
363 ret->push = sys_iconv;
367 if (!ret->push || !ret->pull) {
368 SAFE_FREE(ret->from_name);
369 SAFE_FREE(ret->to_name);
372 return (atalk_iconv_t)-1;
375 /* check for conversion to/from ucs2 */
376 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
377 ret->direct = to->push;
378 ret->push = ret->pull = NULL;
382 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
383 ret->direct = from->pull;
384 ret->push = ret->pull = NULL;
388 /* Check if we can do the conversion direct */
389 #ifdef HAVE_USABLE_ICONV
390 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
391 ret->direct = sys_iconv;
392 ret->cd_direct = ret->cd_push;
396 if (strcasecmp(tocode, "UCS-2LE") == 0) {
397 ret->direct = sys_iconv;
398 ret->cd_direct = ret->cd_pull;
408 simple iconv_close() wrapper
410 int atalk_iconv_close (atalk_iconv_t cd)
412 #ifdef HAVE_USABLE_ICONV
413 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
414 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
415 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
418 SAFE_FREE(cd->from_name);
419 SAFE_FREE(cd->to_name);
421 memset(cd, 0, sizeof(*cd));
427 /************************************************************************
428 the following functions implement the builtin character sets in Netatalk
429 *************************************************************************/
431 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
432 char **outbuf, size_t *outbytesleft)
434 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
435 (*outbuf)[0] = (*inbuf)[0];
438 (*outbytesleft) -= 2;
443 if (*inbytesleft > 0) {
451 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
452 char **outbuf, size_t *outbytesleft)
456 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
457 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
458 if ((*inbuf)[1]) ir_count++;
460 (*outbytesleft) -= 1;
465 if (*inbytesleft == 1) {
470 if (*inbytesleft > 1) {
479 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
480 char **outbuf, size_t *outbytesleft)
484 n = MIN(*inbytesleft, *outbytesleft);
486 memmove(*outbuf, *inbuf, n);
489 (*outbytesleft) -= n;
493 if (*inbytesleft > 0) {
501 /* ------------------------ */