]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/iconv.c
20962307c3751a7663b5e76d11cad7c7fcbbf606
[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 WITH_LIBICONV
74 #define UCS2ICONV "UCS-2-INTERNAL"
75 #else
76 #if BYTE_ORDER==LITTLE_ENDIAN
77 #define UCS2ICONV "UCS-2LE"
78 #else
79 #define UCS2ICONV "UCS-2BE"
80 #endif
81 #endif
82
83
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 *);
87
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;
95
96
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}
102 };
103
104
105 #define DLIST_ADD(list, p) \
106 { \
107         if (!(list)) { \
108                 (list) = (p); \
109                 (p)->next = (p)->prev = NULL; \
110         } else { \
111                 (list)->prev = (p); \
112                 (p)->next = (list); \
113                 (p)->prev = NULL; \
114                 (list) = (p); \
115         }\
116 }
117
118 static struct charset_functions *charsets = NULL;
119
120 struct charset_functions *find_charset_functions(const char *name) 
121 {
122         struct charset_functions *c = charsets;
123
124         while(c) {
125                 if (strcasecmp(name, c->name) == 0) {
126                         return c;
127                 }
128                 c = c->next;
129         }
130
131         return NULL;
132 }
133
134 int atalk_register_charset(struct charset_functions *funcs) 
135 {
136         if (!funcs) {
137                 return -1;
138         }
139
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);
143                 return -2;
144         }
145
146         funcs->next = funcs->prev = NULL;
147         DLIST_ADD(charsets, funcs);
148         return 0;
149 }
150
151 void lazy_initialize_iconv(void)
152 {
153         static int initialized = 0;
154         int i;
155
156         if (!initialized) {
157                 initialized = 1;
158                 for(i = 0; builtin_functions[i].name; i++) 
159                         atalk_register_charset(&builtin_functions[i]);
160
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);
169         }
170 }
171
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)
178 {
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);
184         return ret;
185 #else
186         errno = EINVAL;
187         return -1;
188 #endif
189 }
190
191 /**
192  * This is a simple portable iconv() implementaion.
193  *
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.
196  **/
197 size_t atalk_iconv(atalk_iconv_t cd, 
198                  const char **inbuf, size_t *inbytesleft,
199                  char **outbuf, size_t *outbytesleft)
200 {
201         char cvtbuf[2048];
202         char *bufp = cvtbuf;
203         size_t bufsize;
204
205         /* in many cases we can go direct */
206         if (cd->direct) {
207                 return cd->direct(cd->cd_direct, 
208                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
209         }
210
211
212         /* otherwise we have to do it chunks at a time */
213         while (*inbytesleft > 0) {
214                 bufp = cvtbuf;
215                 bufsize = sizeof(cvtbuf);
216                 
217                 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
218                        && errno != E2BIG) {
219                     return -1;
220                 }
221
222                 bufp = cvtbuf;
223                 bufsize = sizeof(cvtbuf) - bufsize;
224
225                 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
226                     return -1;
227                 }
228         }
229
230         return 0;
231 }
232
233
234 /*
235   simple iconv_open() wrapper
236  */
237 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
238 {
239         atalk_iconv_t ret;
240         struct charset_functions *from, *to;
241
242
243         lazy_initialize_iconv();
244         from = charsets;
245         to = charsets;
246
247         ret = (atalk_iconv_t)malloc(sizeof(*ret));
248         if (!ret) {
249                 errno = ENOMEM;
250                 return (atalk_iconv_t)-1;
251         }
252         memset(ret, 0, sizeof(*ret));
253
254         ret->from_name = strdup(fromcode);
255         ret->to_name = strdup(tocode);
256
257         /* check for the simplest null conversion */
258         if (strcasecmp(fromcode, tocode) == 0) {
259                 ret->direct = iconv_copy;
260                 return ret;
261         }
262
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;
266         
267         to = find_charset_functions(tocode);
268         if(to && to->push)ret->push = to->push;
269
270         /* check if we can use iconv for this conversion */
271 #ifdef HAVE_USABLE_ICONV
272         if (!ret->pull) {
273                 ret->cd_pull = iconv_open(UCS2ICONV, fromcode);
274                 if (ret->cd_pull != (iconv_t)-1)
275                         ret->pull = sys_iconv;
276         }
277
278         if (!ret->push) {
279                 ret->cd_push = iconv_open(tocode, UCS2ICONV);
280                 if (ret->cd_push != (iconv_t)-1)
281                         ret->push = sys_iconv;
282         }
283 #endif
284         
285         if (!ret->push || !ret->pull) {
286                 SAFE_FREE(ret->from_name);
287                 SAFE_FREE(ret->to_name);
288                 SAFE_FREE(ret);
289                 errno = EINVAL;
290                 return (atalk_iconv_t)-1;
291         }
292
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;
297                 return ret;
298         }
299
300         if (strcasecmp(tocode, "UCS-2") == 0 && from && from->pull) {
301                 ret->direct = from->pull;
302                 ret->push = ret->pull = NULL;
303                 return ret;
304         }
305
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;
311                 ret->cd_push = NULL;
312                 return ret;
313         }
314         if (strcasecmp(tocode, "UCS-2") == 0) {
315                 ret->direct = sys_iconv;
316                 ret->cd_direct = ret->cd_pull;
317                 ret->cd_pull = NULL;
318                 return ret;
319         }
320 #endif
321
322         return ret;
323 }
324
325 /*
326   simple iconv_close() wrapper
327 */
328 int atalk_iconv_close (atalk_iconv_t cd)
329 {
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);
334 #endif
335
336         SAFE_FREE(cd->from_name);
337         SAFE_FREE(cd->to_name);
338
339         memset(cd, 0, sizeof(*cd));
340         SAFE_FREE(cd);
341         return 0;
342 }
343
344
345 /************************************************************************
346  the following functions implement the builtin character sets in Netatalk
347 *************************************************************************/
348
349 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
350                          char **outbuf, size_t *outbytesleft)
351 {
352         ucs2_t curchar;
353
354         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
355                 if ((unsigned char)(*inbuf)[0] < 0x80) {
356                         curchar = (ucs2_t) (*inbuf)[0];
357                         SSVAL((*outbuf),0,curchar);
358                 }
359                 else {
360                         errno = EILSEQ;
361                         return -1;
362                 }
363                 (*inbytesleft)  -= 1;
364                 (*outbytesleft) -= 2;
365                 (*inbuf)  += 1;
366                 (*outbuf) += 2;
367         }
368
369         if (*inbytesleft > 0) {
370                 errno = E2BIG;
371                 return -1;
372         }
373         
374         return 0;
375 }
376
377 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
378                          char **outbuf, size_t *outbytesleft)
379 {
380         int ir_count=0;
381         ucs2_t curchar;
382
383         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
384                 curchar = SVAL((*inbuf), 0);
385                 if (curchar < 0x0080) {
386                         (*outbuf)[0] = curchar;
387                 }
388                 else {
389                         errno = EILSEQ;
390                         return -1;
391                 }       
392                 (*inbytesleft)  -= 2;
393                 (*outbytesleft) -= 1;
394                 (*inbuf)  += 2;
395                 (*outbuf) += 1;
396         }
397
398         if (*inbytesleft == 1) {
399                 errno = EINVAL;
400                 return -1;
401         }
402
403         if (*inbytesleft > 1) {
404                 errno = E2BIG;
405                 return -1;
406         }
407         
408         return ir_count;
409 }
410
411
412 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
413                          char **outbuf, size_t *outbytesleft)
414 {
415         int n;
416
417         n = MIN(*inbytesleft, *outbytesleft);
418
419         memmove(*outbuf, *inbuf, n);
420
421         (*inbytesleft) -= n;
422         (*outbytesleft) -= n;
423         (*inbuf) += n;
424         (*outbuf) += n;
425
426         if (*inbytesleft > 0) {
427                 errno = E2BIG;
428                 return -1;
429         }
430
431         return 0;
432 }
433
434 /* ------------------------ */