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 extern struct charset_functions charset_mac_roman;
89 extern struct charset_functions charset_mac_hebrew;
90 extern struct charset_functions charset_mac_centraleurope;
91 extern struct charset_functions charset_mac_cyrillic;
92 extern struct charset_functions charset_mac_turkish;
93 extern struct charset_functions charset_utf8;
94 extern struct charset_functions charset_utf8_mac;
97 static struct charset_functions builtin_functions[] = {
98 {"UCS-2", 0, iconv_copy, iconv_copy, CHARSET_WIDECHAR | CHARSET_PRECOMPOSED},
99 {"ASCII", 0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED},
100 {"SHIFT_JIS", 1568, NULL, NULL, CHARSET_ICONV | CHARSET_PRECOMPOSED | CHARSET_CLIENT},
101 {NULL, 0, NULL, NULL, 0}
105 #define DLIST_ADD(list, p) \
109 (p)->next = (p)->prev = NULL; \
111 (list)->prev = (p); \
112 (p)->next = (list); \
118 static struct charset_functions *charsets = NULL;
120 struct charset_functions *find_charset_functions(const char *name)
122 struct charset_functions *c = charsets;
125 if (strcasecmp(name, c->name) == 0) {
134 int atalk_register_charset(struct charset_functions *funcs)
140 /* Check whether we already have this charset... */
141 if (find_charset_functions(funcs->name)) {
142 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
146 funcs->next = funcs->prev = NULL;
147 DLIST_ADD(charsets, funcs);
151 void lazy_initialize_iconv(void)
153 static int initialized = 0;
158 for(i = 0; builtin_functions[i].name; i++)
159 atalk_register_charset(&builtin_functions[i]);
161 /* register additional charsets */
162 atalk_register_charset(&charset_utf8);
163 atalk_register_charset(&charset_utf8_mac);
164 atalk_register_charset(&charset_mac_roman);
165 atalk_register_charset(&charset_mac_hebrew);
166 atalk_register_charset(&charset_mac_turkish);
167 atalk_register_charset(&charset_mac_centraleurope);
168 atalk_register_charset(&charset_mac_cyrillic);
172 /* if there was an error then reset the internal state,
173 this ensures that we don't have a shift state remaining for
174 character sets like SJIS */
175 static size_t sys_iconv(void *cd,
176 char **inbuf, size_t *inbytesleft,
177 char **outbuf, size_t *outbytesleft)
179 #ifdef HAVE_USABLE_ICONV
180 size_t ret = iconv((iconv_t)cd,
181 (ICONV_CONST char**)inbuf, inbytesleft,
182 outbuf, outbytesleft);
183 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
192 * This is a simple portable iconv() implementaion.
194 * It only knows about a very small number of character sets - just
195 * enough that netatalk works on systems that don't have iconv.
197 size_t atalk_iconv(atalk_iconv_t cd,
198 const char **inbuf, size_t *inbytesleft,
199 char **outbuf, size_t *outbytesleft)
205 /* in many cases we can go direct */
207 return cd->direct(cd->cd_direct,
208 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
212 /* otherwise we have to do it chunks at a time */
213 while (*inbytesleft > 0) {
215 bufsize = sizeof(cvtbuf);
217 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
223 bufsize = sizeof(cvtbuf) - bufsize;
225 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
235 simple iconv_open() wrapper
237 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
240 struct charset_functions *from, *to;
243 lazy_initialize_iconv();
247 ret = (atalk_iconv_t)malloc(sizeof(*ret));
250 return (atalk_iconv_t)-1;
252 memset(ret, 0, sizeof(*ret));
254 ret->from_name = strdup(fromcode);
255 ret->to_name = strdup(tocode);
257 /* check for the simplest null conversion */
258 if (strcasecmp(fromcode, tocode) == 0) {
259 ret->direct = iconv_copy;
263 /* check if we have a builtin function for this conversion */
264 from = find_charset_functions(fromcode);
265 if(from && from->pull)ret->pull = from->pull;
267 to = find_charset_functions(tocode);
268 if(to && to->push)ret->push = to->push;
270 /* check if we can use iconv for this conversion */
271 #ifdef HAVE_USABLE_ICONV
273 ret->cd_pull = iconv_open(UCS2ICONV, fromcode);
274 if (ret->cd_pull != (iconv_t)-1)
275 ret->pull = sys_iconv;
279 ret->cd_push = iconv_open(tocode, UCS2ICONV);
280 if (ret->cd_push != (iconv_t)-1)
281 ret->push = sys_iconv;
285 if (!ret->push || !ret->pull) {
286 SAFE_FREE(ret->from_name);
287 SAFE_FREE(ret->to_name);
290 return (atalk_iconv_t)-1;
293 /* check for conversion to/from ucs2 */
294 if (strcasecmp(fromcode, "UCS-2") == 0 && to && to->push) {
295 ret->direct = to->push;
296 ret->push = ret->pull = NULL;
300 if (strcasecmp(tocode, "UCS-2") == 0 && from && from->pull) {
301 ret->direct = from->pull;
302 ret->push = ret->pull = NULL;
306 /* Check if we can do the conversion direct */
307 #ifdef HAVE_USABLE_ICONV
308 if (strcasecmp(fromcode, "UCS-2") == 0) {
309 ret->direct = sys_iconv;
310 ret->cd_direct = ret->cd_push;
314 if (strcasecmp(tocode, "UCS-2") == 0) {
315 ret->direct = sys_iconv;
316 ret->cd_direct = ret->cd_pull;
326 simple iconv_close() wrapper
328 int atalk_iconv_close (atalk_iconv_t cd)
330 #ifdef HAVE_USABLE_ICONV
331 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
332 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
333 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
336 SAFE_FREE(cd->from_name);
337 SAFE_FREE(cd->to_name);
339 memset(cd, 0, sizeof(*cd));
345 /************************************************************************
346 the following functions implement the builtin character sets in Netatalk
347 *************************************************************************/
349 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
350 char **outbuf, size_t *outbytesleft)
354 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
355 if ((unsigned char)(*inbuf)[0] < 0x80) {
356 curchar = (ucs2_t) (*inbuf)[0];
357 SSVAL((*outbuf),0,curchar);
364 (*outbytesleft) -= 2;
369 if (*inbytesleft > 0) {
377 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
378 char **outbuf, size_t *outbytesleft)
383 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
384 curchar = SVAL((*inbuf), 0);
385 if (curchar < 0x0080) {
386 (*outbuf)[0] = curchar;
393 (*outbytesleft) -= 1;
398 if (*inbytesleft == 1) {
403 if (*inbytesleft > 1) {
412 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
413 char **outbuf, size_t *outbytesleft)
417 n = MIN(*inbytesleft, *outbytesleft);
419 memmove(*outbuf, *inbuf, n);
422 (*outbytesleft) -= n;
426 if (*inbytesleft > 0) {
434 /* ------------------------ */