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