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 */
37 #include <sys/param.h>
39 #ifdef HAVE_USABLE_ICONV
43 #include <netatalk/endian.h>
44 #include <atalk/unicode.h>
45 #include <atalk/logger.h>
46 #include "byteorder.h"
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 #ifdef HAVE_USABLE_ICONV
74 #ifdef HAVE_UCS2INTERNAL
75 #define UCS2ICONV "UCS-2-INTERNAL"
76 #else /* !HAVE_UCS2INTERNAL */
77 #if BYTE_ORDER==LITTLE_ENDIAN
78 #define UCS2ICONV "UCS-2LE"
79 #else /* !LITTLE_ENDIAN */
80 #define UCS2ICONV "UCS-2BE"
81 #endif /* BYTE_ORDER */
82 #endif /* HAVE_UCS2INTERNAL */
83 #else /* !HAVE_USABLE_ICONV */
84 #define UCS2ICONV "UCS-2"
85 #endif /* HAVE_USABLE_ICONV */
87 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
88 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
89 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
91 extern struct charset_functions charset_mac_roman;
92 extern struct charset_functions charset_mac_hebrew;
93 extern struct charset_functions charset_mac_centraleurope;
94 extern struct charset_functions charset_mac_cyrillic;
95 extern struct charset_functions charset_mac_greek;
96 extern struct charset_functions charset_mac_turkish;
97 extern struct charset_functions charset_utf8;
98 extern struct charset_functions charset_utf8_mac;
99 #ifdef HAVE_USABLE_ICONV
100 extern struct charset_functions charset_mac_japanese;
101 extern struct charset_functions charset_mac_chinese_trad;
102 extern struct charset_functions charset_mac_korean;
103 extern struct charset_functions charset_mac_chinese_simp;
107 static struct charset_functions builtin_functions[] = {
108 {"UCS-2", 0, iconv_copy, iconv_copy, CHARSET_WIDECHAR | CHARSET_PRECOMPOSED, NULL, NULL, NULL},
109 {"ASCII", 0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED, NULL, NULL, NULL},
110 {"SHIFT_JIS", 1568, NULL, NULL, CHARSET_ICONV | CHARSET_PRECOMPOSED | CHARSET_CLIENT, NULL, NULL, NULL},
111 {NULL, 0, NULL, NULL, 0, NULL, NULL, NULL}
115 #define DLIST_ADD(list, p) \
119 (p)->next = (p)->prev = NULL; \
121 (list)->prev = (p); \
122 (p)->next = (list); \
128 static struct charset_functions *charsets = NULL;
130 struct charset_functions *find_charset_functions(const char *name)
132 struct charset_functions *c = charsets;
135 if (strcasecmp(name, c->name) == 0) {
144 int atalk_register_charset(struct charset_functions *funcs)
150 /* Check whether we already have this charset... */
151 if (find_charset_functions(funcs->name)) {
152 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
156 funcs->next = funcs->prev = NULL;
157 DLIST_ADD(charsets, funcs);
161 void lazy_initialize_iconv(void)
163 static int initialized = 0;
168 for(i = 0; builtin_functions[i].name; i++)
169 atalk_register_charset(&builtin_functions[i]);
171 /* register additional charsets */
172 atalk_register_charset(&charset_utf8);
173 atalk_register_charset(&charset_utf8_mac);
174 atalk_register_charset(&charset_mac_roman);
175 atalk_register_charset(&charset_mac_hebrew);
176 atalk_register_charset(&charset_mac_greek);
177 atalk_register_charset(&charset_mac_turkish);
178 atalk_register_charset(&charset_mac_centraleurope);
179 atalk_register_charset(&charset_mac_cyrillic);
180 #ifdef HAVE_USABLE_ICONV
181 atalk_register_charset(&charset_mac_japanese);
182 atalk_register_charset(&charset_mac_chinese_trad);
183 atalk_register_charset(&charset_mac_korean);
184 atalk_register_charset(&charset_mac_chinese_simp);
189 /* if there was an error then reset the internal state,
190 this ensures that we don't have a shift state remaining for
191 character sets like SJIS */
192 static size_t sys_iconv(void *cd,
193 char **inbuf, size_t *inbytesleft,
194 char **outbuf, size_t *outbytesleft)
196 #ifdef HAVE_USABLE_ICONV
197 size_t ret = iconv((iconv_t)cd,
198 (ICONV_CONST char**)inbuf, inbytesleft,
199 outbuf, outbytesleft);
200 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
209 * This is a simple portable iconv() implementaion.
211 * It only knows about a very small number of character sets - just
212 * enough that netatalk works on systems that don't have iconv.
214 size_t atalk_iconv(atalk_iconv_t cd,
215 const char **inbuf, size_t *inbytesleft,
216 char **outbuf, size_t *outbytesleft)
222 /* in many cases we can go direct */
224 return cd->direct(cd->cd_direct,
225 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
229 /* otherwise we have to do it chunks at a time */
230 while (*inbytesleft > 0) {
232 bufsize = sizeof(cvtbuf);
234 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
240 bufsize = sizeof(cvtbuf) - bufsize;
242 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
252 simple iconv_open() wrapper
254 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
257 struct charset_functions *from, *to;
260 lazy_initialize_iconv();
264 ret = (atalk_iconv_t)malloc(sizeof(*ret));
267 return (atalk_iconv_t)-1;
269 memset(ret, 0, sizeof(*ret));
271 ret->from_name = strdup(fromcode);
272 ret->to_name = strdup(tocode);
274 /* check for the simplest null conversion */
275 if (strcasecmp(fromcode, tocode) == 0) {
276 ret->direct = iconv_copy;
280 /* check if we have a builtin function for this conversion */
281 from = find_charset_functions(fromcode);
282 if (from) ret->pull = from->pull;
284 to = find_charset_functions(tocode);
285 if (to) ret->push = to->push;
287 /* check if we can use iconv for this conversion */
288 #ifdef HAVE_USABLE_ICONV
289 if (!from || (from->flags & CHARSET_ICONV)) {
290 ret->cd_pull = iconv_open(UCS2ICONV, from && from->iname ? from->iname : fromcode);
291 if (ret->cd_pull != (iconv_t)-1) {
292 if (!ret->pull) ret->pull = sys_iconv;
293 } else ret->pull = NULL;
296 if (!to || (to->flags & CHARSET_ICONV)) {
297 ret->cd_push = iconv_open(to && to->iname ? to->iname : tocode, UCS2ICONV);
298 if (ret->cd_push != (iconv_t)-1) {
299 if (!ret->push) ret->push = sys_iconv;
300 } else ret->push = NULL;
302 if (!ret->push && ret->cd_pull) iconv_close((iconv_t)ret->cd_pull);
306 if (!ret->push || !ret->pull) {
307 SAFE_FREE(ret->from_name);
308 SAFE_FREE(ret->to_name);
311 return (atalk_iconv_t)-1;
314 /* check for conversion to/from ucs2 */
315 if (strcasecmp(fromcode, "UCS-2") == 0) {
316 ret->direct = ret->push;
317 ret->cd_direct = ret->cd_push;
320 if (strcasecmp(tocode, "UCS-2") == 0) {
321 ret->direct = ret->pull;
322 ret->cd_direct = ret->cd_pull;
330 simple iconv_close() wrapper
332 int atalk_iconv_close (atalk_iconv_t cd)
334 #ifdef HAVE_USABLE_ICONV
335 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
336 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
337 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
340 SAFE_FREE(cd->from_name);
341 SAFE_FREE(cd->to_name);
343 memset(cd, 0, sizeof(*cd));
349 /************************************************************************
350 the following functions implement the builtin character sets in Netatalk
351 *************************************************************************/
353 static size_t ascii_pull(void *cd _U_, char **inbuf, size_t *inbytesleft,
354 char **outbuf, size_t *outbytesleft)
358 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
359 if ((unsigned char)(*inbuf)[0] < 0x80) {
360 curchar = (ucs2_t) (*inbuf)[0];
361 SSVAL((*outbuf),0,curchar);
368 (*outbytesleft) -= 2;
373 if (*inbytesleft > 0) {
381 static size_t ascii_push(void *cd _U_, char **inbuf, size_t *inbytesleft,
382 char **outbuf, size_t *outbytesleft)
387 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
388 curchar = SVAL((*inbuf), 0);
389 if (curchar < 0x0080) {
390 (*outbuf)[0] = curchar;
397 (*outbytesleft) -= 1;
402 if (*inbytesleft == 1) {
407 if (*inbytesleft > 1) {
416 static size_t iconv_copy(void *cd _U_, char **inbuf, size_t *inbytesleft,
417 char **outbuf, size_t *outbytesleft)
421 n = MIN(*inbytesleft, *outbytesleft);
423 memmove(*outbuf, *inbuf, n);
426 (*outbytesleft) -= n;
430 if (*inbytesleft > 0) {
438 /* ------------------------ */