]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/iconv.c
big merge for db frontend and unicode.
[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
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 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 *);
76
77 static char hexdig[] = "0123456789abcdef";
78 #define hextoint( c )   ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
79
80 struct charset_functions charset_ucs2 =
81 {
82         "UCS-2LE",
83         0,
84         iconv_copy,
85         iconv_copy,
86         CHARSET_WIDECHAR
87 };
88
89 struct charset_functions charset_ascii =
90 {
91         "ASCII",
92         0,
93         ascii_pull,
94         ascii_push,
95         CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED 
96 };
97
98 struct charset_functions charset_iconv =
99 {
100         NULL,
101         0,
102         NULL,
103         NULL,
104         CHARSET_ICONV | CHARSET_PRECOMPOSED
105 };
106
107
108 extern  struct charset_functions charset_mac_roman;
109 extern  struct charset_functions charset_mac_hebrew;
110 extern  struct charset_functions charset_mac_centraleurope;
111 extern  struct charset_functions charset_mac_cyrillic;
112 extern  struct charset_functions charset_mac_turkish;
113 extern  struct charset_functions charset_utf8;
114 extern  struct charset_functions charset_utf8_mac;
115
116
117 static struct charset_functions builtin_functions[] = {
118         {"UCS-2LE",   0, iconv_copy, iconv_copy, CHARSET_WIDECHAR},
119         {"ASCII",     0, ascii_pull, ascii_push, CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED},
120         {NULL, 0, NULL, NULL, 0}
121 };
122
123
124 #define DLIST_ADD(list, p) \
125 { \
126         if (!(list)) { \
127                 (list) = (p); \
128                 (p)->next = (p)->prev = NULL; \
129         } else { \
130                 (list)->prev = (p); \
131                 (p)->next = (list); \
132                 (p)->prev = NULL; \
133                 (list) = (p); \
134         }\
135 }
136
137 static struct charset_functions *charsets = NULL;
138
139 struct charset_functions *find_charset_functions(const char *name) 
140 {
141         struct charset_functions *c = charsets;
142
143         while(c) {
144                 if (strcasecmp(name, c->name) == 0) {
145                         return c;
146                 }
147                 c = c->next;
148         }
149
150         return NULL;
151 }
152
153 int atalk_register_charset(struct charset_functions *funcs) 
154 {
155         if (!funcs) {
156                 return -1;
157         }
158
159         /* Check whether we already have this charset... */
160         if (find_charset_functions(funcs->name)) {
161                 LOG (log_debug, logtype_default, "Duplicate charset %s, not registering", funcs->name);
162                 return -2;
163         }
164
165         funcs->next = funcs->prev = NULL;
166         DLIST_ADD(charsets, funcs);
167         return 0;
168 }
169
170 void lazy_initialize_iconv(void)
171 {
172         static int initialized = 0;
173         int i;
174
175         if (!initialized) {
176                 initialized = 1;
177                 for(i = 0; builtin_functions[i].name; i++) 
178                         atalk_register_charset(&builtin_functions[i]);
179
180                 /* register additional charsets */
181                 atalk_register_charset(&charset_utf8);
182                 atalk_register_charset(&charset_utf8_mac);
183                 atalk_register_charset(&charset_mac_roman);
184                 atalk_register_charset(&charset_mac_hebrew);
185                 atalk_register_charset(&charset_mac_turkish);
186                 atalk_register_charset(&charset_mac_centraleurope);
187                 atalk_register_charset(&charset_mac_cyrillic);
188         }
189 }
190
191 /* if there was an error then reset the internal state,
192    this ensures that we don't have a shift state remaining for
193    character sets like SJIS */
194 static size_t sys_iconv(void *cd, 
195                         char **inbuf, size_t *inbytesleft,
196                         char **outbuf, size_t *outbytesleft)
197 {
198 #ifdef HAVE_USABLE_ICONV
199         size_t ret = iconv((iconv_t)cd, 
200                            inbuf, inbytesleft, 
201                            outbuf, outbytesleft);
202         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
203         return ret;
204 #else
205         errno = EINVAL;
206         return -1;
207 #endif
208 }
209
210 /**
211  * This is a simple portable iconv() implementaion.
212  *
213  * It only knows about a very small number of character sets - just
214  * enough that netatalk works on systems that don't have iconv.
215  **/
216 size_t atalk_iconv(atalk_iconv_t cd, 
217                  const char **inbuf, size_t *inbytesleft,
218                  char **outbuf, size_t *outbytesleft)
219 {
220         char cvtbuf[2048];
221         char *bufp = cvtbuf;
222         size_t bufsize;
223
224         /* in many cases we can go direct */
225         if (cd->direct) {
226                 return cd->direct(cd->cd_direct, 
227                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
228         }
229
230
231         /* otherwise we have to do it chunks at a time */
232         while (*inbytesleft > 0) {
233                 bufp = cvtbuf;
234                 bufsize = sizeof(cvtbuf);
235                 
236                 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
237                        && errno != E2BIG) {
238                     return -1;
239                 }
240
241                 bufp = cvtbuf;
242                 bufsize = sizeof(cvtbuf) - bufsize;
243
244                 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
245                     return -1;
246                 }
247         }
248
249         return 0;
250 }
251
252
253 size_t atalk_iconv_ignore(atalk_iconv_t cd, 
254                  const char **inbuf, size_t *inbytesleft,
255                  char **outbuf, size_t *outbytesleft, int *ignore)
256 {
257         char cvtbuf[2048];
258         char *bufp = cvtbuf;
259         size_t bufsize;
260         size_t outlen = *outbytesleft;
261         char *o_save;
262         
263         /* in many cases we can go direct */
264         if (cd->direct) {
265         o_save = *outbuf;
266 direct_loop:            
267                 if ((size_t)-1 == cd->direct(cd->cd_direct, (char **)inbuf, inbytesleft, outbuf, outbytesleft)) {
268                         if (errno == EILSEQ) {
269                                 o_save[outlen-*outbytesleft] = '_';
270                                 (*outbuf) = o_save + outlen-*outbytesleft+1;
271                                 (*outbytesleft) -=1;
272                                 (*inbuf) += 2;
273                                 (*inbytesleft) -= 2;
274                                 *ignore = 1;
275                                 goto direct_loop;
276                     }
277                     else
278                         return (size_t)(-1);
279                 }
280         }
281         
282         /* we have to do it chunks at a time */
283         while (*inbytesleft > 0) {
284                 bufp = cvtbuf;
285                 bufsize = sizeof(cvtbuf);
286                 
287                 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
288                         && errno != E2BIG) {
289                     return -1;
290                 }
291
292                 bufp = cvtbuf;
293                 bufsize = sizeof(cvtbuf) - bufsize;
294
295                 o_save = *outbuf;
296 convert_push:
297                 if (cd->push(cd->cd_push, 
298                              &bufp, &bufsize, 
299                              outbuf, outbytesleft) == (size_t)-1) {
300                     if (errno == EILSEQ) {
301                         o_save[outlen-*outbytesleft] = '_';
302                         (*outbuf) = o_save + outlen-*outbytesleft+1;
303                         (*outbytesleft) -=1;
304                         bufp += 2;
305                         bufsize -= 2;
306                         *ignore = 1;
307                         goto convert_push;
308                     }
309                     else
310                         return (size_t)(-1);
311                 }
312         }
313         return 0;
314 }
315
316 /*
317   simple iconv_open() wrapper
318  */
319 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
320 {
321         atalk_iconv_t ret;
322         struct charset_functions *from, *to;
323
324
325         lazy_initialize_iconv();
326         from = charsets;
327         to = charsets;
328
329         ret = (atalk_iconv_t)malloc(sizeof(*ret));
330         if (!ret) {
331                 errno = ENOMEM;
332                 return (atalk_iconv_t)-1;
333         }
334         memset(ret, 0, sizeof(*ret));
335
336         ret->from_name = strdup(fromcode);
337         ret->to_name = strdup(tocode);
338
339         /* check for the simplest null conversion */
340         if (strcasecmp(fromcode, tocode) == 0) {
341                 ret->direct = iconv_copy;
342                 return ret;
343         }
344
345         /* check if we have a builtin function for this conversion */
346         from = find_charset_functions(fromcode);
347         if(from)ret->pull = from->pull;
348         
349         to = find_charset_functions(tocode);
350         if(to)ret->push = to->push;
351
352         /* check if we can use iconv for this conversion */
353 #ifdef HAVE_USABLE_ICONV
354         if (!ret->pull) {
355                 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
356                 if (ret->cd_pull != (iconv_t)-1)
357                         ret->pull = sys_iconv;
358         }
359
360         if (!ret->push) {
361                 ret->cd_push = iconv_open(tocode, "UCS-2LE");
362                 if (ret->cd_push != (iconv_t)-1)
363                         ret->push = sys_iconv;
364         }
365 #endif
366         
367         if (!ret->push || !ret->pull) {
368                 SAFE_FREE(ret->from_name);
369                 SAFE_FREE(ret->to_name);
370                 SAFE_FREE(ret);
371                 errno = EINVAL;
372                 return (atalk_iconv_t)-1;
373         }
374
375         /* check for conversion to/from ucs2 */
376         if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
377                 ret->direct = to->push;
378                 ret->push = ret->pull = NULL;
379                 return ret;
380         }
381
382         if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
383                 ret->direct = from->pull;
384                 ret->push = ret->pull = NULL;
385                 return ret;
386         }
387
388         /* Check if we can do the conversion direct */
389 #ifdef HAVE_USABLE_ICONV
390         if (strcasecmp(fromcode, "UCS-2LE") == 0) {
391                 ret->direct = sys_iconv;
392                 ret->cd_direct = ret->cd_push;
393                 ret->cd_push = NULL;
394                 return ret;
395         }
396         if (strcasecmp(tocode, "UCS-2LE") == 0) {
397                 ret->direct = sys_iconv;
398                 ret->cd_direct = ret->cd_pull;
399                 ret->cd_pull = NULL;
400                 return ret;
401         }
402 #endif
403
404         return ret;
405 }
406
407 /*
408   simple iconv_close() wrapper
409 */
410 int atalk_iconv_close (atalk_iconv_t cd)
411 {
412 #ifdef HAVE_USABLE_ICONV
413         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
414         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
415         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
416 #endif
417
418         SAFE_FREE(cd->from_name);
419         SAFE_FREE(cd->to_name);
420
421         memset(cd, 0, sizeof(*cd));
422         SAFE_FREE(cd);
423         return 0;
424 }
425
426
427 /************************************************************************
428  the following functions implement the builtin character sets in Netatalk
429 *************************************************************************/
430
431 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
432                          char **outbuf, size_t *outbytesleft)
433 {
434         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
435                 (*outbuf)[0] = (*inbuf)[0];
436                 (*outbuf)[1] = 0;
437                 (*inbytesleft)  -= 1;
438                 (*outbytesleft) -= 2;
439                 (*inbuf)  += 1;
440                 (*outbuf) += 2;
441         }
442
443         if (*inbytesleft > 0) {
444                 errno = E2BIG;
445                 return -1;
446         }
447         
448         return 0;
449 }
450
451 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
452                          char **outbuf, size_t *outbytesleft)
453 {
454         int ir_count=0;
455
456         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
457                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
458                 if ((*inbuf)[1]) ir_count++;
459                 (*inbytesleft)  -= 2;
460                 (*outbytesleft) -= 1;
461                 (*inbuf)  += 2;
462                 (*outbuf) += 1;
463         }
464
465         if (*inbytesleft == 1) {
466                 errno = EINVAL;
467                 return -1;
468         }
469
470         if (*inbytesleft > 1) {
471                 errno = E2BIG;
472                 return -1;
473         }
474         
475         return ir_count;
476 }
477
478
479 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
480                          char **outbuf, size_t *outbytesleft)
481 {
482         int n;
483
484         n = MIN(*inbytesleft, *outbytesleft);
485
486         memmove(*outbuf, *inbuf, n);
487
488         (*inbytesleft) -= n;
489         (*outbytesleft) -= n;
490         (*inbuf) += n;
491         (*outbuf) += n;
492
493         if (*inbytesleft > 0) {
494                 errno = E2BIG;
495                 return -1;
496         }
497
498         return 0;
499 }
500
501 /* ------------------------ */