]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/iconv.c
1f545bbd0820b8d8f6faa786af92919ebc74fe0b
[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 struct charset_functions charset_ucs2 =
78 {
79         "UCS-2LE",
80         0,
81         iconv_copy,
82         iconv_copy,
83         CHARSET_WIDECHAR
84 };
85
86 struct charset_functions charset_ascii =
87 {
88         "ASCII",
89         0,
90         ascii_pull,
91         ascii_push,
92         CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED 
93 };
94
95 struct charset_functions charset_iconv =
96 {
97         NULL,
98         0,
99         NULL,
100         NULL,
101         CHARSET_ICONV | CHARSET_PRECOMPOSED
102 };
103
104
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;
112
113
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}
118 };
119
120
121 #define DLIST_ADD(list, p) \
122 { \
123         if (!(list)) { \
124                 (list) = (p); \
125                 (p)->next = (p)->prev = NULL; \
126         } else { \
127                 (list)->prev = (p); \
128                 (p)->next = (list); \
129                 (p)->prev = NULL; \
130                 (list) = (p); \
131         }\
132 }
133
134 static struct charset_functions *charsets = NULL;
135
136 struct charset_functions *find_charset_functions(const char *name) 
137 {
138         struct charset_functions *c = charsets;
139
140         while(c) {
141                 if (strcasecmp(name, c->name) == 0) {
142                         return c;
143                 }
144                 c = c->next;
145         }
146
147         return NULL;
148 }
149
150 int atalk_register_charset(struct charset_functions *funcs) 
151 {
152         if (!funcs) {
153                 return -1;
154         }
155
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);
159                 return -2;
160         }
161
162         funcs->next = funcs->prev = NULL;
163         DLIST_ADD(charsets, funcs);
164         return 0;
165 }
166
167 void lazy_initialize_iconv(void)
168 {
169         static int initialized = 0;
170         int i;
171
172         if (!initialized) {
173                 initialized = 1;
174                 for(i = 0; builtin_functions[i].name; i++) 
175                         atalk_register_charset(&builtin_functions[i]);
176
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);
185         }
186 }
187
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)
194 {
195 #ifdef HAVE_USABLE_ICONV
196         size_t ret = iconv((iconv_t)cd, 
197                            inbuf, inbytesleft, 
198                            outbuf, outbytesleft);
199         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
200         return ret;
201 #else
202         errno = EINVAL;
203         return -1;
204 #endif
205 }
206
207 /**
208  * This is a simple portable iconv() implementaion.
209  *
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.
212  **/
213 size_t atalk_iconv(atalk_iconv_t cd, 
214                  const char **inbuf, size_t *inbytesleft,
215                  char **outbuf, size_t *outbytesleft)
216 {
217         char cvtbuf[2048];
218         char *bufp = cvtbuf;
219         size_t bufsize;
220
221         /* in many cases we can go direct */
222         if (cd->direct) {
223                 return cd->direct(cd->cd_direct, 
224                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
225         }
226
227
228         /* otherwise we have to do it chunks at a time */
229         while (*inbytesleft > 0) {
230                 bufp = cvtbuf;
231                 bufsize = sizeof(cvtbuf);
232                 
233                 if (cd->pull(cd->cd_pull, (char **)inbuf, inbytesleft, &bufp, &bufsize) == (size_t)-1
234                        && errno != E2BIG) {
235                     return -1;
236                 }
237
238                 bufp = cvtbuf;
239                 bufsize = sizeof(cvtbuf) - bufsize;
240
241                 if (cd->push(cd->cd_push, &bufp, &bufsize, outbuf, outbytesleft) == (size_t)-1) {
242                     return -1;
243                 }
244         }
245
246         return 0;
247 }
248
249
250 /*
251   simple iconv_open() wrapper
252  */
253 atalk_iconv_t atalk_iconv_open(const char *tocode, const char *fromcode)
254 {
255         atalk_iconv_t ret;
256         struct charset_functions *from, *to;
257
258
259         lazy_initialize_iconv();
260         from = charsets;
261         to = charsets;
262
263         ret = (atalk_iconv_t)malloc(sizeof(*ret));
264         if (!ret) {
265                 errno = ENOMEM;
266                 return (atalk_iconv_t)-1;
267         }
268         memset(ret, 0, sizeof(*ret));
269
270         ret->from_name = strdup(fromcode);
271         ret->to_name = strdup(tocode);
272
273         /* check for the simplest null conversion */
274         if (strcasecmp(fromcode, tocode) == 0) {
275                 ret->direct = iconv_copy;
276                 return ret;
277         }
278
279         /* check if we have a builtin function for this conversion */
280         from = find_charset_functions(fromcode);
281         if(from)ret->pull = from->pull;
282         
283         to = find_charset_functions(tocode);
284         if(to)ret->push = to->push;
285
286         /* check if we can use iconv for this conversion */
287 #ifdef HAVE_USABLE_ICONV
288         if (!ret->pull) {
289                 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
290                 if (ret->cd_pull != (iconv_t)-1)
291                         ret->pull = sys_iconv;
292         }
293
294         if (!ret->push) {
295                 ret->cd_push = iconv_open(tocode, "UCS-2LE");
296                 if (ret->cd_push != (iconv_t)-1)
297                         ret->push = sys_iconv;
298         }
299 #endif
300         
301         if (!ret->push || !ret->pull) {
302                 SAFE_FREE(ret->from_name);
303                 SAFE_FREE(ret->to_name);
304                 SAFE_FREE(ret);
305                 errno = EINVAL;
306                 return (atalk_iconv_t)-1;
307         }
308
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;
313                 return ret;
314         }
315
316         if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
317                 ret->direct = from->pull;
318                 ret->push = ret->pull = NULL;
319                 return ret;
320         }
321
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;
327                 ret->cd_push = NULL;
328                 return ret;
329         }
330         if (strcasecmp(tocode, "UCS-2LE") == 0) {
331                 ret->direct = sys_iconv;
332                 ret->cd_direct = ret->cd_pull;
333                 ret->cd_pull = NULL;
334                 return ret;
335         }
336 #endif
337
338         return ret;
339 }
340
341 /*
342   simple iconv_close() wrapper
343 */
344 int atalk_iconv_close (atalk_iconv_t cd)
345 {
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);
350 #endif
351
352         SAFE_FREE(cd->from_name);
353         SAFE_FREE(cd->to_name);
354
355         memset(cd, 0, sizeof(*cd));
356         SAFE_FREE(cd);
357         return 0;
358 }
359
360
361 /************************************************************************
362  the following functions implement the builtin character sets in Netatalk
363 *************************************************************************/
364
365 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
366                          char **outbuf, size_t *outbytesleft)
367 {
368         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
369                 (*outbuf)[0] = (*inbuf)[0];
370                 (*outbuf)[1] = 0;
371                 (*inbytesleft)  -= 1;
372                 (*outbytesleft) -= 2;
373                 (*inbuf)  += 1;
374                 (*outbuf) += 2;
375         }
376
377         if (*inbytesleft > 0) {
378                 errno = E2BIG;
379                 return -1;
380         }
381         
382         return 0;
383 }
384
385 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
386                          char **outbuf, size_t *outbytesleft)
387 {
388         int ir_count=0;
389
390         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
391                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
392                 if ((*inbuf)[1]) ir_count++;
393                 (*inbytesleft)  -= 2;
394                 (*outbytesleft) -= 1;
395                 (*inbuf)  += 2;
396                 (*outbuf) += 1;
397         }
398
399         if (*inbytesleft == 1) {
400                 errno = EINVAL;
401                 return -1;
402         }
403
404         if (*inbytesleft > 1) {
405                 errno = E2BIG;
406                 return -1;
407         }
408         
409         return ir_count;
410 }
411
412
413 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
414                          char **outbuf, size_t *outbytesleft)
415 {
416         int n;
417
418         n = MIN(*inbytesleft, *outbytesleft);
419
420         memmove(*outbuf, *inbuf, n);
421
422         (*inbytesleft) -= n;
423         (*outbytesleft) -= n;
424         (*inbuf) += n;
425         (*outbuf) += n;
426
427         if (*inbytesleft > 0) {
428                 errno = E2BIG;
429                 return -1;
430         }
431
432         return 0;
433 }
434
435 /* ------------------------ */