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
74 #define UCS2ICONV "UCS-2-INTERNAL"
76 #if BYTE_ORDER==LITTLE_ENDIAN
77 #define UCS2ICONV "UCS-2LE"
79 #define UCS2ICONV "UCS-2BE"
84 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
85 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
86 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
88 struct charset_functions charset_ucs2 =
94 CHARSET_WIDECHAR | CHARSET_PRECOMPOSED
97 struct charset_functions charset_ascii =
103 CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED
106 struct charset_functions charset_iconv =
112 CHARSET_ICONV | CHARSET_PRECOMPOSED
116 extern struct charset_functions charset_mac_roman;
117 extern struct charset_functions charset_mac_hebrew;
118 extern struct charset_functions charset_mac_centraleurope;
119 extern struct charset_functions charset_mac_cyrillic;
120 extern struct charset_functions charset_mac_turkish;
121 extern struct charset_functions charset_utf8;
122 extern struct charset_functions charset_utf8_mac;
125 static struct charset_functions builtin_functions[] = {
126 {"UCS-2", 0, iconv_copy, iconv_copy, CHARSET_WIDECHAR},
127 {"ASCII", 0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED},
128 {NULL, 0, NULL, NULL, 0}
132 #define DLIST_ADD(list, p) \
136 (p)->next = (p)->prev = NULL; \
138 (list)->prev = (p); \
139 (p)->next = (list); \
145 static struct charset_functions *charsets = NULL;
147 struct charset_functions *find_charset_functions(const char *name)
149 struct charset_functions *c = charsets;
152 if (strcasecmp(name, c->name) == 0) {
161 int atalk_register_charset(struct charset_functions *funcs)
167 /* Check whether we already have this charset... */
168 if (find_charset_functions(funcs->name)) {
169 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
173 funcs->next = funcs->prev = NULL;
174 DLIST_ADD(charsets, funcs);
178 void lazy_initialize_iconv(void)
180 static int initialized = 0;
185 for(i = 0; builtin_functions[i].name; i++)
186 atalk_register_charset(&builtin_functions[i]);
188 /* register additional charsets */
189 atalk_register_charset(&charset_utf8);
190 atalk_register_charset(&charset_utf8_mac);
191 atalk_register_charset(&charset_mac_roman);
192 atalk_register_charset(&charset_mac_hebrew);
193 atalk_register_charset(&charset_mac_turkish);
194 atalk_register_charset(&charset_mac_centraleurope);
195 atalk_register_charset(&charset_mac_cyrillic);
199 /* if there was an error then reset the internal state,
200 this ensures that we don't have a shift state remaining for
201 character sets like SJIS */
202 static size_t sys_iconv(void *cd,
203 char **inbuf, size_t *inbytesleft,
204 char **outbuf, size_t *outbytesleft)
206 #ifdef HAVE_USABLE_ICONV
207 size_t ret = iconv((iconv_t)cd,
209 outbuf, outbytesleft);
210 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
219 * This is a simple portable iconv() implementaion.
221 * It only knows about a very small number of character sets - just
222 * enough that netatalk works on systems that don't have iconv.
224 size_t atalk_iconv(atalk_iconv_t cd,
225 const char **inbuf, size_t *inbytesleft,
226 char **outbuf, size_t *outbytesleft)
232 /* in many cases we can go direct */
234 return cd->direct(cd->cd_direct,
235 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
239 /* otherwise we have to do it chunks at a time */
240 while (*inbytesleft > 0) {
242 bufsize = sizeof(cvtbuf);
244 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
250 bufsize = sizeof(cvtbuf) - bufsize;
252 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
262 simple iconv_open() wrapper
264 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
267 struct charset_functions *from, *to;
270 lazy_initialize_iconv();
274 ret = (atalk_iconv_t)malloc(sizeof(*ret));
277 return (atalk_iconv_t)-1;
279 memset(ret, 0, sizeof(*ret));
281 ret->from_name = strdup(fromcode);
282 ret->to_name = strdup(tocode);
284 /* check for the simplest null conversion */
285 if (strcasecmp(fromcode, tocode) == 0) {
286 ret->direct = iconv_copy;
290 /* check if we have a builtin function for this conversion */
291 from = find_charset_functions(fromcode);
292 if(from)ret->pull = from->pull;
294 to = find_charset_functions(tocode);
295 if(to)ret->push = to->push;
297 /* check if we can use iconv for this conversion */
298 #ifdef HAVE_USABLE_ICONV
300 ret->cd_pull = iconv_open(UCS2ICONV, fromcode);
301 if (ret->cd_pull != (iconv_t)-1)
302 ret->pull = sys_iconv;
306 ret->cd_push = iconv_open(tocode, UCS2ICONV);
307 if (ret->cd_push != (iconv_t)-1)
308 ret->push = sys_iconv;
312 if (!ret->push || !ret->pull) {
313 SAFE_FREE(ret->from_name);
314 SAFE_FREE(ret->to_name);
317 return (atalk_iconv_t)-1;
320 /* check for conversion to/from ucs2 */
321 if (strcasecmp(fromcode, "UCS-2") == 0 && to) {
322 ret->direct = to->push;
323 ret->push = ret->pull = NULL;
327 if (strcasecmp(tocode, "UCS-2") == 0 && from) {
328 ret->direct = from->pull;
329 ret->push = ret->pull = NULL;
333 /* Check if we can do the conversion direct */
334 #ifdef HAVE_USABLE_ICONV
335 if (strcasecmp(fromcode, "UCS-2") == 0) {
336 ret->direct = sys_iconv;
337 ret->cd_direct = ret->cd_push;
341 if (strcasecmp(tocode, "UCS-2") == 0) {
342 ret->direct = sys_iconv;
343 ret->cd_direct = ret->cd_pull;
353 simple iconv_close() wrapper
355 int atalk_iconv_close (atalk_iconv_t cd)
357 #ifdef HAVE_USABLE_ICONV
358 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
359 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
360 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
363 SAFE_FREE(cd->from_name);
364 SAFE_FREE(cd->to_name);
366 memset(cd, 0, sizeof(*cd));
372 /************************************************************************
373 the following functions implement the builtin character sets in Netatalk
374 *************************************************************************/
376 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
377 char **outbuf, size_t *outbytesleft)
381 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
382 if ((unsigned char)(*inbuf)[0] < 0x80) {
383 curchar = (ucs2_t) (*inbuf)[0];
384 SSVAL((*outbuf),0,curchar);
391 (*outbytesleft) -= 2;
396 if (*inbytesleft > 0) {
404 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
405 char **outbuf, size_t *outbytesleft)
410 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
411 curchar = SVAL((*inbuf), 0);
412 if (curchar < 0x0080) {
413 (*outbuf)[0] = curchar;
420 (*outbytesleft) -= 1;
425 if (*inbytesleft == 1) {
430 if (*inbytesleft > 1) {
439 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
440 char **outbuf, size_t *outbytesleft)
444 n = MIN(*inbytesleft, *outbytesleft);
446 memmove(*outbuf, *inbuf, n);
449 (*outbytesleft) -= n;
453 if (*inbytesleft > 0) {
461 /* ------------------------ */