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
47 #include "byteorder.h"
53 * @brief Samba wrapper/stub for iconv character set conversion.
55 * iconv is the XPG2 interface for converting between character
56 * encodings. This file provides a Samba wrapper around it, and also
57 * a simple reimplementation that is used if the system does not
60 * Samba only works with encodings that are supersets of ASCII: ascii
61 * characters like whitespace can be tested for directly, multibyte
62 * sequences start with a byte with the high bit set, and strings are
63 * terminated by a nul byte.
65 * Note that the only function provided by iconv is conversion between
66 * characters. It doesn't directly support operations like
67 * uppercasing or comparison. We have to convert to UCS-2 and compare
70 * @sa Samba Developers Guide
72 #define CHARSET_WIDECHAR 32
75 #define UCS2ICONV "UCS-2-INTERNAL"
77 #if BYTE_ORDER==LITTLE_ENDIAN
78 #define UCS2ICONV "UCS-2LE"
80 #define UCS2ICONV "UCS-2BE"
85 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
86 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
87 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
89 struct charset_functions charset_ucs2 =
98 struct charset_functions charset_ascii =
104 CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED
107 struct charset_functions charset_iconv =
113 CHARSET_ICONV | CHARSET_PRECOMPOSED
117 extern struct charset_functions charset_mac_roman;
118 extern struct charset_functions charset_mac_hebrew;
119 extern struct charset_functions charset_mac_centraleurope;
120 extern struct charset_functions charset_mac_cyrillic;
121 extern struct charset_functions charset_mac_turkish;
122 extern struct charset_functions charset_utf8;
123 extern struct charset_functions charset_utf8_mac;
126 static struct charset_functions builtin_functions[] = {
127 {"UCS-2", 0, iconv_copy, iconv_copy, CHARSET_WIDECHAR},
128 {"ASCII", 0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED},
129 {NULL, 0, NULL, NULL, 0}
133 #define DLIST_ADD(list, p) \
137 (p)->next = (p)->prev = NULL; \
139 (list)->prev = (p); \
140 (p)->next = (list); \
146 static struct charset_functions *charsets = NULL;
148 struct charset_functions *find_charset_functions(const char *name)
150 struct charset_functions *c = charsets;
153 if (strcasecmp(name, c->name) == 0) {
162 int atalk_register_charset(struct charset_functions *funcs)
168 /* Check whether we already have this charset... */
169 if (find_charset_functions(funcs->name)) {
170 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
174 funcs->next = funcs->prev = NULL;
175 DLIST_ADD(charsets, funcs);
179 void lazy_initialize_iconv(void)
181 static int initialized = 0;
186 for(i = 0; builtin_functions[i].name; i++)
187 atalk_register_charset(&builtin_functions[i]);
189 /* register additional charsets */
190 atalk_register_charset(&charset_utf8);
191 atalk_register_charset(&charset_utf8_mac);
192 atalk_register_charset(&charset_mac_roman);
193 atalk_register_charset(&charset_mac_hebrew);
194 atalk_register_charset(&charset_mac_turkish);
195 atalk_register_charset(&charset_mac_centraleurope);
196 atalk_register_charset(&charset_mac_cyrillic);
200 /* if there was an error then reset the internal state,
201 this ensures that we don't have a shift state remaining for
202 character sets like SJIS */
203 static size_t sys_iconv(void *cd,
204 char **inbuf, size_t *inbytesleft,
205 char **outbuf, size_t *outbytesleft)
207 #ifdef HAVE_USABLE_ICONV
208 size_t ret = iconv((iconv_t)cd,
210 outbuf, outbytesleft);
211 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
220 * This is a simple portable iconv() implementaion.
222 * It only knows about a very small number of character sets - just
223 * enough that netatalk works on systems that don't have iconv.
225 size_t atalk_iconv(atalk_iconv_t cd,
226 const char **inbuf, size_t *inbytesleft,
227 char **outbuf, size_t *outbytesleft)
233 /* in many cases we can go direct */
235 return cd->direct(cd->cd_direct,
236 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
240 /* otherwise we have to do it chunks at a time */
241 while (*inbytesleft > 0) {
243 bufsize = sizeof(cvtbuf);
245 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
251 bufsize = sizeof(cvtbuf) - bufsize;
253 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
263 simple iconv_open() wrapper
265 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
268 struct charset_functions *from, *to;
271 lazy_initialize_iconv();
275 ret = (atalk_iconv_t)malloc(sizeof(*ret));
278 return (atalk_iconv_t)-1;
280 memset(ret, 0, sizeof(*ret));
282 ret->from_name = strdup(fromcode);
283 ret->to_name = strdup(tocode);
285 /* check for the simplest null conversion */
286 if (strcasecmp(fromcode, tocode) == 0) {
287 ret->direct = iconv_copy;
291 /* check if we have a builtin function for this conversion */
292 from = find_charset_functions(fromcode);
293 if(from)ret->pull = from->pull;
295 to = find_charset_functions(tocode);
296 if(to)ret->push = to->push;
298 /* check if we can use iconv for this conversion */
299 #ifdef HAVE_USABLE_ICONV
300 LOG (log_debug, logtype_default, "Trying %s/%s", UCS2ICONV, fromcode);
302 ret->cd_pull = iconv_open(UCS2ICONV, fromcode);
303 if (ret->cd_pull != (iconv_t)-1)
304 ret->pull = sys_iconv;
307 LOG (log_debug, logtype_default, "Trying %s/%s", tocode, UCS2ICONV);
309 ret->cd_push = iconv_open(tocode, UCS2ICONV);
310 if (ret->cd_push != (iconv_t)-1)
311 ret->push = sys_iconv;
315 if (!ret->push || !ret->pull) {
316 SAFE_FREE(ret->from_name);
317 SAFE_FREE(ret->to_name);
320 return (atalk_iconv_t)-1;
323 /* check for conversion to/from ucs2 */
324 if (strcasecmp(fromcode, "UCS-2") == 0 && to) {
325 ret->direct = to->push;
326 ret->push = ret->pull = NULL;
330 if (strcasecmp(tocode, "UCS-2") == 0 && from) {
331 ret->direct = from->pull;
332 ret->push = ret->pull = NULL;
336 /* Check if we can do the conversion direct */
337 #ifdef HAVE_USABLE_ICONV
338 if (strcasecmp(fromcode, "UCS-2") == 0) {
339 ret->direct = sys_iconv;
340 ret->cd_direct = ret->cd_push;
344 if (strcasecmp(tocode, "UCS-2") == 0) {
345 ret->direct = sys_iconv;
346 ret->cd_direct = ret->cd_pull;
356 simple iconv_close() wrapper
358 int atalk_iconv_close (atalk_iconv_t cd)
360 #ifdef HAVE_USABLE_ICONV
361 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
362 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
363 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
366 SAFE_FREE(cd->from_name);
367 SAFE_FREE(cd->to_name);
369 memset(cd, 0, sizeof(*cd));
375 /************************************************************************
376 the following functions implement the builtin character sets in Netatalk
377 *************************************************************************/
379 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
380 char **outbuf, size_t *outbytesleft)
384 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
385 if ((unsigned char)(*inbuf)[0] < 0x80) {
386 curchar = (ucs2_t) (*inbuf)[0];
387 SSVAL((*outbuf),0,curchar);
394 (*outbytesleft) -= 2;
399 if (*inbytesleft > 0) {
407 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
408 char **outbuf, size_t *outbytesleft)
413 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
414 curchar = SVAL((*inbuf), 0);
415 if (curchar < 0x0080) {
416 (*outbuf)[0] = curchar;
423 (*outbytesleft) -= 1;
428 if (*inbytesleft == 1) {
433 if (*inbytesleft > 1) {
442 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
443 char **outbuf, size_t *outbytesleft)
447 n = MIN(*inbytesleft, *outbytesleft);
449 memmove(*outbuf, *inbuf, n);
452 (*outbytesleft) -= n;
456 if (*inbytesleft > 0) {
464 /* ------------------------ */