]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/iconv.c
de38e13a898683e6bc81f798636cf199cc242a3e
[netatalk.git] / libatalk / unicode / iconv.c
1 /* 
2    Unix SMB/CIFS implementation.
3    minimal iconv implementation
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Jelmer Vernooij 2002,2003
6    
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.
11    
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.
16    
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.
20    
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
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif /* HAVE_CONFIG_H */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_USABLE_ICONV
40 #include <iconv.h>
41 #endif
42
43 #include <netatalk/endian.h>
44 #include <atalk/unicode.h>
45 #include <atalk/logger.h>
46 #include "byteorder.h"
47
48
49 /**
50  * @file
51  *
52  * @brief Samba wrapper/stub for iconv character set conversion.
53  *
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
57  * implement iconv.
58  *
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.
63  *
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
67  * there.
68  *
69  * @sa Samba Developers Guide
70  **/
71 #define CHARSET_WIDECHAR    32
72
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 */
86
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 *);
90
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_turkish;
96 extern  struct charset_functions charset_utf8;
97 extern  struct charset_functions charset_utf8_mac;
98 #ifdef HAVE_USABLE_ICONV
99 extern  struct charset_functions charset_mac_japanese;
100 extern  struct charset_functions charset_mac_chinese_trad;
101 extern  struct charset_functions charset_mac_korean;
102 extern  struct charset_functions charset_mac_chinese_simp;
103 #endif
104
105
106 static struct charset_functions builtin_functions[] = {
107         {"UCS-2",   0, iconv_copy, iconv_copy, CHARSET_WIDECHAR | CHARSET_PRECOMPOSED, NULL, NULL, NULL},
108         {"ASCII",     0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED, NULL, NULL, NULL},
109         {"SHIFT_JIS", 1568, NULL, NULL, CHARSET_ICONV | CHARSET_PRECOMPOSED | CHARSET_CLIENT, NULL, NULL, NULL},
110         {NULL, 0, NULL, NULL, 0, NULL, NULL, NULL}
111 };
112
113
114 #define DLIST_ADD(list, p) \
115 { \
116         if (!(list)) { \
117                 (list) = (p); \
118                 (p)->next = (p)->prev = NULL; \
119         } else { \
120                 (list)->prev = (p); \
121                 (p)->next = (list); \
122                 (p)->prev = NULL; \
123                 (list) = (p); \
124         }\
125 }
126
127 static struct charset_functions *charsets = NULL;
128
129 struct charset_functions *find_charset_functions(const char *name) 
130 {
131         struct charset_functions *c = charsets;
132
133         while(c) {
134                 if (strcasecmp(name, c->name) == 0) {
135                         return c;
136                 }
137                 c = c->next;
138         }
139
140         return NULL;
141 }
142
143 int atalk_register_charset(struct charset_functions *funcs) 
144 {
145         if (!funcs) {
146                 return -1;
147         }
148
149         /* Check whether we already have this charset... */
150         if (find_charset_functions(funcs->name)) {
151                 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
152                 return -2;
153         }
154
155         funcs->next = funcs->prev = NULL;
156         DLIST_ADD(charsets, funcs);
157         return 0;
158 }
159
160 void lazy_initialize_iconv(void)
161 {
162         static int initialized = 0;
163         int i;
164
165         if (!initialized) {
166                 initialized = 1;
167                 for(i = 0; builtin_functions[i].name; i++) 
168                         atalk_register_charset(&builtin_functions[i]);
169
170                 /* register additional charsets */
171                 atalk_register_charset(&charset_utf8);
172                 atalk_register_charset(&charset_utf8_mac);
173                 atalk_register_charset(&charset_mac_roman);
174                 atalk_register_charset(&charset_mac_hebrew);
175                 atalk_register_charset(&charset_mac_turkish);
176                 atalk_register_charset(&charset_mac_centraleurope);
177                 atalk_register_charset(&charset_mac_cyrillic);
178 #ifdef HAVE_USABLE_ICONV
179                 atalk_register_charset(&charset_mac_japanese);
180                 atalk_register_charset(&charset_mac_chinese_trad);
181                 atalk_register_charset(&charset_mac_korean);
182                 atalk_register_charset(&charset_mac_chinese_simp);
183 #endif
184         }
185 }
186
187 /* if there was an error then reset the internal state,
188    this ensures that we don't have a shift state remaining for
189    character sets like SJIS */
190 static size_t sys_iconv(void *cd, 
191                         char **inbuf, size_t *inbytesleft,
192                         char **outbuf, size_t *outbytesleft)
193 {
194 #ifdef HAVE_USABLE_ICONV
195         size_t ret = iconv((iconv_t)cd, 
196                            (ICONV_CONST char**)inbuf, inbytesleft, 
197                            outbuf, outbytesleft);
198         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
199         return ret;
200 #else
201         errno = EINVAL;
202         return -1;
203 #endif
204 }
205
206 /**
207  * This is a simple portable iconv() implementaion.
208  *
209  * It only knows about a very small number of character sets - just
210  * enough that netatalk works on systems that don't have iconv.
211  **/
212 size_t atalk_iconv(atalk_iconv_t cd, 
213                  const char **inbuf, size_t *inbytesleft,
214                  char **outbuf, size_t *outbytesleft)
215 {
216         char cvtbuf[2048];
217         char *bufp = cvtbuf;
218         size_t bufsize;
219
220         /* in many cases we can go direct */
221         if (cd->direct) {
222                 return cd->direct(cd->cd_direct, 
223                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
224         }
225
226
227         /* otherwise we have to do it chunks at a time */
228         while (*inbytesleft > 0) {
229                 bufp = cvtbuf;
230                 bufsize = sizeof(cvtbuf);
231                 
232                 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
233                        && errno != E2BIG) {
234                     return -1;
235                 }
236
237                 bufp = cvtbuf;
238                 bufsize = sizeof(cvtbuf) - bufsize;
239
240                 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
241                     return -1;
242                 }
243         }
244
245         return 0;
246 }
247
248
249 /*
250   simple iconv_open() wrapper
251  */
252 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
253 {
254         atalk_iconv_t ret;
255         struct charset_functions *from, *to;
256
257
258         lazy_initialize_iconv();
259         from = charsets;
260         to = charsets;
261
262         ret = (atalk_iconv_t)malloc(sizeof(*ret));
263         if (!ret) {
264                 errno = ENOMEM;
265                 return (atalk_iconv_t)-1;
266         }
267         memset(ret, 0, sizeof(*ret));
268
269         ret->from_name = strdup(fromcode);
270         ret->to_name = strdup(tocode);
271
272         /* check for the simplest null conversion */
273         if (strcasecmp(fromcode, tocode) == 0) {
274                 ret->direct = iconv_copy;
275                 return ret;
276         }
277
278         /* check if we have a builtin function for this conversion */
279         from = find_charset_functions(fromcode);
280         if (from) ret->pull = from->pull;
281         
282         to = find_charset_functions(tocode);
283         if (to) ret->push = to->push;
284
285         /* check if we can use iconv for this conversion */
286 #ifdef HAVE_USABLE_ICONV
287         if (!from || (from->flags & CHARSET_ICONV)) {
288           ret->cd_pull = iconv_open(UCS2ICONV, from && from->iname ? from->iname : fromcode);
289           if (ret->cd_pull != (iconv_t)-1) {
290             if (!ret->pull) ret->pull = sys_iconv;
291           } else ret->pull = NULL;
292         }
293         if (ret->pull) {
294           if (!to || (to->flags & CHARSET_ICONV)) {
295             ret->cd_push = iconv_open(to && to->iname ? to->iname : tocode, UCS2ICONV);
296             if (ret->cd_push != (iconv_t)-1) {
297               if (!ret->push) ret->push = sys_iconv;
298             } else ret->push = NULL;
299           }
300           if (!ret->push && ret->cd_pull) iconv_close((iconv_t)ret->cd_pull);
301         }
302 #endif
303         
304         if (!ret->push || !ret->pull) {
305                 SAFE_FREE(ret->from_name);
306                 SAFE_FREE(ret->to_name);
307                 SAFE_FREE(ret);
308                 errno = EINVAL;
309                 return (atalk_iconv_t)-1;
310         }
311
312         /* check for conversion to/from ucs2 */
313         if (strcasecmp(fromcode, "UCS-2") == 0) {
314           ret->direct = ret->push;
315           ret->cd_direct = ret->cd_push;
316           ret->cd_push = NULL;
317         }
318         if (strcasecmp(tocode, "UCS-2") == 0) {
319           ret->direct = ret->pull;
320           ret->cd_direct = ret->cd_pull;
321           ret->cd_pull = NULL;
322         }
323
324         return ret;
325 }
326
327 /*
328   simple iconv_close() wrapper
329 */
330 int atalk_iconv_close (atalk_iconv_t cd)
331 {
332 #ifdef HAVE_USABLE_ICONV
333         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
334         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
335         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
336 #endif
337
338         SAFE_FREE(cd->from_name);
339         SAFE_FREE(cd->to_name);
340
341         memset(cd, 0, sizeof(*cd));
342         SAFE_FREE(cd);
343         return 0;
344 }
345
346
347 /************************************************************************
348  the following functions implement the builtin character sets in Netatalk
349 *************************************************************************/
350
351 static size_t ascii_pull(void *cd _U_, char **inbuf, size_t *inbytesleft,
352                          char **outbuf, size_t *outbytesleft)
353 {
354         ucs2_t curchar;
355
356         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
357                 if ((unsigned char)(*inbuf)[0] < 0x80) {
358                         curchar = (ucs2_t) (*inbuf)[0];
359                         SSVAL((*outbuf),0,curchar);
360                 }
361                 else {
362                         errno = EILSEQ;
363                         return -1;
364                 }
365                 (*inbytesleft)  -= 1;
366                 (*outbytesleft) -= 2;
367                 (*inbuf)  += 1;
368                 (*outbuf) += 2;
369         }
370
371         if (*inbytesleft > 0) {
372                 errno = E2BIG;
373                 return -1;
374         }
375         
376         return 0;
377 }
378
379 static size_t ascii_push(void *cd _U_, char **inbuf, size_t *inbytesleft,
380                          char **outbuf, size_t *outbytesleft)
381 {
382         int ir_count=0;
383         ucs2_t curchar;
384
385         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
386                 curchar = SVAL((*inbuf), 0);
387                 if (curchar < 0x0080) {
388                         (*outbuf)[0] = curchar;
389                 }
390                 else {
391                         errno = EILSEQ;
392                         return -1;
393                 }       
394                 (*inbytesleft)  -= 2;
395                 (*outbytesleft) -= 1;
396                 (*inbuf)  += 2;
397                 (*outbuf) += 1;
398         }
399
400         if (*inbytesleft == 1) {
401                 errno = EINVAL;
402                 return -1;
403         }
404
405         if (*inbytesleft > 1) {
406                 errno = E2BIG;
407                 return -1;
408         }
409         
410         return ir_count;
411 }
412
413
414 static size_t iconv_copy(void *cd _U_, char **inbuf, size_t *inbytesleft,
415                          char **outbuf, size_t *outbytesleft)
416 {
417         int n;
418
419         n = MIN(*inbytesleft, *outbytesleft);
420
421         memmove(*outbuf, *inbuf, n);
422
423         (*inbytesleft) -= n;
424         (*outbytesleft) -= n;
425         (*inbuf) += n;
426         (*outbuf) += n;
427
428         if (*inbytesleft > 0) {
429                 errno = E2BIG;
430                 return -1;
431         }
432
433         return 0;
434 }
435
436 /* ------------------------ */