]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/iconv.c
unicode endian fixes
[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 <sys/param.h>
37 #include <sys/stat.h>
38 #include <atalk/logger.h>
39 #include <errno.h>
40
41 #include <netatalk/endian.h>
42 #include <atalk/unicode.h>
43
44 #ifdef HAVE_USABLE_ICONV
45 #include <iconv.h>
46 #endif
47 #include "byteorder.h"
48
49
50 /**
51  * @file
52  *
53  * @brief Samba wrapper/stub for iconv character set conversion.
54  *
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
58  * implement iconv.
59  *
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.
64  *
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
68  * there.
69  *
70  * @sa Samba Developers Guide
71  **/
72 #define CHARSET_WIDECHAR    32
73
74 #ifdef WITH_LIBICONV
75 #define UCS2ICONV "UCS-2-INTERNAL"
76 #else
77 #if BYTE_ORDER==LITTLE_ENDIAN
78 #define UCS2ICONV "UCS-2LE"
79 #else
80 #define UCS2ICONV "UCS-2BE"
81 #endif
82 #endif
83
84
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 *);
88
89 struct charset_functions charset_ucs2 =
90 {
91         "UCS-2",
92         0,
93         iconv_copy,
94         iconv_copy,
95         CHARSET_WIDECHAR
96 };
97
98 struct charset_functions charset_ascii =
99 {
100         "ASCII",
101         0,
102         ascii_pull,
103         ascii_push,
104         CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED 
105 };
106
107 struct charset_functions charset_iconv =
108 {
109         NULL,
110         0,
111         NULL,
112         NULL,
113         CHARSET_ICONV | CHARSET_PRECOMPOSED
114 };
115
116
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;
124
125
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}
130 };
131
132
133 #define DLIST_ADD(list, p) \
134 { \
135         if (!(list)) { \
136                 (list) = (p); \
137                 (p)->next = (p)->prev = NULL; \
138         } else { \
139                 (list)->prev = (p); \
140                 (p)->next = (list); \
141                 (p)->prev = NULL; \
142                 (list) = (p); \
143         }\
144 }
145
146 static struct charset_functions *charsets = NULL;
147
148 struct charset_functions *find_charset_functions(const char *name) 
149 {
150         struct charset_functions *c = charsets;
151
152         while(c) {
153                 if (strcasecmp(name, c->name) == 0) {
154                         return c;
155                 }
156                 c = c->next;
157         }
158
159         return NULL;
160 }
161
162 int atalk_register_charset(struct charset_functions *funcs) 
163 {
164         if (!funcs) {
165                 return -1;
166         }
167
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);
171                 return -2;
172         }
173
174         funcs->next = funcs->prev = NULL;
175         DLIST_ADD(charsets, funcs);
176         return 0;
177 }
178
179 void lazy_initialize_iconv(void)
180 {
181         static int initialized = 0;
182         int i;
183
184         if (!initialized) {
185                 initialized = 1;
186                 for(i = 0; builtin_functions[i].name; i++) 
187                         atalk_register_charset(&builtin_functions[i]);
188
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);
197         }
198 }
199
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)
206 {
207 #ifdef HAVE_USABLE_ICONV
208         size_t ret = iconv((iconv_t)cd, 
209                            inbuf, inbytesleft, 
210                            outbuf, outbytesleft);
211         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
212         return ret;
213 #else
214         errno = EINVAL;
215         return -1;
216 #endif
217 }
218
219 /**
220  * This is a simple portable iconv() implementaion.
221  *
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.
224  **/
225 size_t atalk_iconv(atalk_iconv_t cd, 
226                  const char **inbuf, size_t *inbytesleft,
227                  char **outbuf, size_t *outbytesleft)
228 {
229         char cvtbuf[2048];
230         char *bufp = cvtbuf;
231         size_t bufsize;
232
233         /* in many cases we can go direct */
234         if (cd->direct) {
235                 return cd->direct(cd->cd_direct, 
236                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
237         }
238
239
240         /* otherwise we have to do it chunks at a time */
241         while (*inbytesleft > 0) {
242                 bufp = cvtbuf;
243                 bufsize = sizeof(cvtbuf);
244                 
245                 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
246                        && errno != E2BIG) {
247                     return -1;
248                 }
249
250                 bufp = cvtbuf;
251                 bufsize = sizeof(cvtbuf) - bufsize;
252
253                 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
254                     return -1;
255                 }
256         }
257
258         return 0;
259 }
260
261
262 /*
263   simple iconv_open() wrapper
264  */
265 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
266 {
267         atalk_iconv_t ret;
268         struct charset_functions *from, *to;
269
270
271         lazy_initialize_iconv();
272         from = charsets;
273         to = charsets;
274
275         ret = (atalk_iconv_t)malloc(sizeof(*ret));
276         if (!ret) {
277                 errno = ENOMEM;
278                 return (atalk_iconv_t)-1;
279         }
280         memset(ret, 0, sizeof(*ret));
281
282         ret->from_name = strdup(fromcode);
283         ret->to_name = strdup(tocode);
284
285         /* check for the simplest null conversion */
286         if (strcasecmp(fromcode, tocode) == 0) {
287                 ret->direct = iconv_copy;
288                 return ret;
289         }
290
291         /* check if we have a builtin function for this conversion */
292         from = find_charset_functions(fromcode);
293         if(from)ret->pull = from->pull;
294         
295         to = find_charset_functions(tocode);
296         if(to)ret->push = to->push;
297
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);
301         if (!ret->pull) {
302                 ret->cd_pull = iconv_open(UCS2ICONV, fromcode);
303                 if (ret->cd_pull != (iconv_t)-1)
304                         ret->pull = sys_iconv;
305         }
306
307         LOG (log_debug, logtype_default, "Trying %s/%s", tocode, UCS2ICONV);
308         if (!ret->push) {
309                 ret->cd_push = iconv_open(tocode, UCS2ICONV);
310                 if (ret->cd_push != (iconv_t)-1)
311                         ret->push = sys_iconv;
312         }
313 #endif
314         
315         if (!ret->push || !ret->pull) {
316                 SAFE_FREE(ret->from_name);
317                 SAFE_FREE(ret->to_name);
318                 SAFE_FREE(ret);
319                 errno = EINVAL;
320                 return (atalk_iconv_t)-1;
321         }
322
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;
327                 return ret;
328         }
329
330         if (strcasecmp(tocode, "UCS-2") == 0 && from) {
331                 ret->direct = from->pull;
332                 ret->push = ret->pull = NULL;
333                 return ret;
334         }
335
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;
341                 ret->cd_push = NULL;
342                 return ret;
343         }
344         if (strcasecmp(tocode, "UCS-2") == 0) {
345                 ret->direct = sys_iconv;
346                 ret->cd_direct = ret->cd_pull;
347                 ret->cd_pull = NULL;
348                 return ret;
349         }
350 #endif
351
352         return ret;
353 }
354
355 /*
356   simple iconv_close() wrapper
357 */
358 int atalk_iconv_close (atalk_iconv_t cd)
359 {
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);
364 #endif
365
366         SAFE_FREE(cd->from_name);
367         SAFE_FREE(cd->to_name);
368
369         memset(cd, 0, sizeof(*cd));
370         SAFE_FREE(cd);
371         return 0;
372 }
373
374
375 /************************************************************************
376  the following functions implement the builtin character sets in Netatalk
377 *************************************************************************/
378
379 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
380                          char **outbuf, size_t *outbytesleft)
381 {
382         ucs2_t curchar;
383
384         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
385                 if ((unsigned char)(*inbuf)[0] < 0x80) {
386                         curchar = (ucs2_t) (*inbuf)[0];
387                         SSVAL((*outbuf),0,curchar);
388                 }
389                 else {
390                         errno = EILSEQ;
391                         return -1;
392                 }
393                 (*inbytesleft)  -= 1;
394                 (*outbytesleft) -= 2;
395                 (*inbuf)  += 1;
396                 (*outbuf) += 2;
397         }
398
399         if (*inbytesleft > 0) {
400                 errno = E2BIG;
401                 return -1;
402         }
403         
404         return 0;
405 }
406
407 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
408                          char **outbuf, size_t *outbytesleft)
409 {
410         int ir_count=0;
411         ucs2_t curchar;
412
413         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
414                 curchar = SVAL((*inbuf), 0);
415                 if (curchar < 0x0080) {
416                         (*outbuf)[0] = curchar;
417                 }
418                 else {
419                         errno = EILSEQ;
420                         return -1;
421                 }       
422                 (*inbytesleft)  -= 2;
423                 (*outbytesleft) -= 1;
424                 (*inbuf)  += 2;
425                 (*outbuf) += 1;
426         }
427
428         if (*inbytesleft == 1) {
429                 errno = EINVAL;
430                 return -1;
431         }
432
433         if (*inbytesleft > 1) {
434                 errno = E2BIG;
435                 return -1;
436         }
437         
438         return ir_count;
439 }
440
441
442 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
443                          char **outbuf, size_t *outbytesleft)
444 {
445         int n;
446
447         n = MIN(*inbytesleft, *outbytesleft);
448
449         memmove(*outbuf, *inbuf, n);
450
451         (*inbytesleft) -= n;
452         (*outbytesleft) -= n;
453         (*inbuf) += n;
454         (*outbuf) += n;
455
456         if (*inbytesleft > 0) {
457                 errno = E2BIG;
458                 return -1;
459         }
460
461         return 0;
462 }
463
464 /* ------------------------ */