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