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 struct charset_functions charset_ucs2 =
86 struct charset_functions charset_ascii =
92 CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED
95 struct charset_functions charset_iconv =
101 CHARSET_ICONV | CHARSET_PRECOMPOSED
105 extern struct charset_functions charset_mac_roman;
106 extern struct charset_functions charset_mac_hebrew;
107 extern struct charset_functions charset_mac_centraleurope;
108 extern struct charset_functions charset_mac_cyrillic;
109 extern struct charset_functions charset_mac_turkish;
110 extern struct charset_functions charset_utf8;
111 extern struct charset_functions charset_utf8_mac;
114 static struct charset_functions builtin_functions[] = {
115 {"UCS-2LE", 0, iconv_copy, iconv_copy, CHARSET_WIDECHAR},
116 {"ASCII", 0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED},
117 {NULL, 0, NULL, NULL, 0}
121 #define DLIST_ADD(list, p) \
125 (p)->next = (p)->prev = NULL; \
127 (list)->prev = (p); \
128 (p)->next = (list); \
134 static struct charset_functions *charsets = NULL;
136 struct charset_functions *find_charset_functions(const char *name)
138 struct charset_functions *c = charsets;
141 if (strcasecmp(name, c->name) == 0) {
150 int atalk_register_charset(struct charset_functions *funcs)
156 /* Check whether we already have this charset... */
157 if (find_charset_functions(funcs->name)) {
158 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
162 funcs->next = funcs->prev = NULL;
163 DLIST_ADD(charsets, funcs);
167 void lazy_initialize_iconv(void)
169 static int initialized = 0;
174 for(i = 0; builtin_functions[i].name; i++)
175 atalk_register_charset(&builtin_functions[i]);
177 /* register additional charsets */
178 atalk_register_charset(&charset_utf8);
179 atalk_register_charset(&charset_utf8_mac);
180 atalk_register_charset(&charset_mac_roman);
181 atalk_register_charset(&charset_mac_hebrew);
182 atalk_register_charset(&charset_mac_turkish);
183 atalk_register_charset(&charset_mac_centraleurope);
184 atalk_register_charset(&charset_mac_cyrillic);
188 /* if there was an error then reset the internal state,
189 this ensures that we don't have a shift state remaining for
190 character sets like SJIS */
191 static size_t sys_iconv(void *cd,
192 char **inbuf, size_t *inbytesleft,
193 char **outbuf, size_t *outbytesleft)
195 #ifdef HAVE_USABLE_ICONV
196 size_t ret = iconv((iconv_t)cd,
198 outbuf, outbytesleft);
199 if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
208 * This is a simple portable iconv() implementaion.
210 * It only knows about a very small number of character sets - just
211 * enough that netatalk works on systems that don't have iconv.
213 size_t atalk_iconv(atalk_iconv_t cd,
214 const char **inbuf, size_t *inbytesleft,
215 char **outbuf, size_t *outbytesleft)
221 /* in many cases we can go direct */
223 return cd->direct(cd->cd_direct,
224 (char **)inbuf, inbytesleft, outbuf, outbytesleft);
228 /* otherwise we have to do it chunks at a time */
229 while (*inbytesleft > 0) {
231 bufsize = sizeof(cvtbuf);
233 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
239 bufsize = sizeof(cvtbuf) - bufsize;
241 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
251 simple iconv_open() wrapper
253 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
256 struct charset_functions *from, *to;
259 lazy_initialize_iconv();
263 ret = (atalk_iconv_t)malloc(sizeof(*ret));
266 return (atalk_iconv_t)-1;
268 memset(ret, 0, sizeof(*ret));
270 ret->from_name = strdup(fromcode);
271 ret->to_name = strdup(tocode);
273 /* check for the simplest null conversion */
274 if (strcasecmp(fromcode, tocode) == 0) {
275 ret->direct = iconv_copy;
279 /* check if we have a builtin function for this conversion */
280 from = find_charset_functions(fromcode);
281 if(from)ret->pull = from->pull;
283 to = find_charset_functions(tocode);
284 if(to)ret->push = to->push;
286 /* check if we can use iconv for this conversion */
287 #ifdef HAVE_USABLE_ICONV
289 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
290 if (ret->cd_pull != (iconv_t)-1)
291 ret->pull = sys_iconv;
295 ret->cd_push = iconv_open(tocode, "UCS-2LE");
296 if (ret->cd_push != (iconv_t)-1)
297 ret->push = sys_iconv;
301 if (!ret->push || !ret->pull) {
302 SAFE_FREE(ret->from_name);
303 SAFE_FREE(ret->to_name);
306 return (atalk_iconv_t)-1;
309 /* check for conversion to/from ucs2 */
310 if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
311 ret->direct = to->push;
312 ret->push = ret->pull = NULL;
316 if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
317 ret->direct = from->pull;
318 ret->push = ret->pull = NULL;
322 /* Check if we can do the conversion direct */
323 #ifdef HAVE_USABLE_ICONV
324 if (strcasecmp(fromcode, "UCS-2LE") == 0) {
325 ret->direct = sys_iconv;
326 ret->cd_direct = ret->cd_push;
330 if (strcasecmp(tocode, "UCS-2LE") == 0) {
331 ret->direct = sys_iconv;
332 ret->cd_direct = ret->cd_pull;
342 simple iconv_close() wrapper
344 int atalk_iconv_close (atalk_iconv_t cd)
346 #ifdef HAVE_USABLE_ICONV
347 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
348 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
349 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
352 SAFE_FREE(cd->from_name);
353 SAFE_FREE(cd->to_name);
355 memset(cd, 0, sizeof(*cd));
361 /************************************************************************
362 the following functions implement the builtin character sets in Netatalk
363 *************************************************************************/
365 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
366 char **outbuf, size_t *outbytesleft)
368 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
369 if ((unsigned char)(*inbuf)[0] < 0x80) {
370 (*outbuf)[0] = (*inbuf)[0];
378 (*outbytesleft) -= 2;
383 if (*inbytesleft > 0) {
391 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
392 char **outbuf, size_t *outbytesleft)
396 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
397 if ((unsigned char)(*inbuf)[0] < 0x80 && (*inbuf)[1] == 0)
398 (*outbuf)[0] = (*inbuf)[0];
404 (*outbytesleft) -= 1;
409 if (*inbytesleft == 1) {
414 if (*inbytesleft > 1) {
423 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
424 char **outbuf, size_t *outbytesleft)
428 n = MIN(*inbytesleft, *outbytesleft);
430 memmove(*outbuf, *inbuf, n);
433 (*outbytesleft) -= n;
437 if (*inbytesleft > 0) {
445 /* ------------------------ */