Get rid of module_path_get_name()
[abartlet/samba.git/.git] / source3 / lib / 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,2003,2003,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
22 #include "includes.h"
23
24
25 /**
26  * @file
27  *
28  * @brief Samba wrapper/stub for iconv character set conversion.
29  *
30  * iconv is the XPG2 interface for converting between character
31  * encodings.  This file provides a Samba wrapper around it, and also
32  * a simple reimplementation that is used if the system does not
33  * implement iconv.
34  *
35  * Samba only works with encodings that are supersets of ASCII: ascii
36  * characters like whitespace can be tested for directly, multibyte
37  * sequences start with a byte with the high bit set, and strings are
38  * terminated by a nul byte.
39  *
40  * Note that the only function provided by iconv is conversion between
41  * characters.  It doesn't directly support operations like
42  * uppercasing or comparison.  We have to convert to UCS-2 and compare
43  * there.
44  *
45  * @sa Samba Developers Guide
46  **/
47
48 static size_t ascii_pull(void *,char **, size_t *, char **, size_t *);
49 static size_t ascii_push(void *,char **, size_t *, char **, size_t *);
50 static size_t  utf8_pull(void *,char **, size_t *, char **, size_t *);
51 static size_t  utf8_push(void *,char **, size_t *, char **, size_t *);
52 static size_t ucs2hex_pull(void *,char **, size_t *, char **, size_t *);
53 static size_t ucs2hex_push(void *,char **, size_t *, char **, size_t *);
54 static size_t iconv_copy(void *,char **, size_t *, char **, size_t *);
55
56 static struct charset_functions builtin_functions[] = {
57         {"UCS-2LE",  iconv_copy, iconv_copy},
58         {"UTF8",   utf8_pull,  utf8_push},
59         {"ASCII", ascii_pull, ascii_push},
60         {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
61         {NULL, NULL, NULL}
62 };
63
64 static struct charset_functions *charsets = NULL;
65
66 static struct charset_functions *find_charset_functions(const char *name) 
67 {
68         struct charset_functions *c = charsets;
69
70         while(c) {
71                 if (strcasecmp(name, c->name) == 0) {
72                         return c;
73                 }
74                 c = c->next;
75         }
76
77         return NULL;
78 }
79
80 BOOL smb_register_charset(struct charset_functions *funcs) 
81 {
82         struct charset_functions *c = charsets;
83
84         DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
85         /* Check whether we already have this charset... */
86
87         if (find_charset_functions(funcs->name)) {
88                 DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
89                 return False;
90         }
91
92         funcs->next = funcs->prev = NULL;
93         DEBUG(5, ("Registered charset %s\n", funcs->name));
94         DLIST_ADD(charsets, funcs);
95         return True;
96 }
97
98 void lazy_initialize_iconv(void)
99 {
100         static BOOL initialized;
101         int i;
102
103         if (!initialized) {
104                 initialized = True;
105                 for(i = 0; builtin_functions[i].name; i++) 
106                         smb_register_charset(&builtin_functions[i]);
107                 static_init_charset;
108         }
109 }
110
111 /* if there was an error then reset the internal state,
112    this ensures that we don't have a shift state remaining for
113    character sets like SJIS */
114 static size_t sys_iconv(void *cd, 
115                         char **inbuf, size_t *inbytesleft,
116                         char **outbuf, size_t *outbytesleft)
117 {
118 #ifdef HAVE_NATIVE_ICONV
119         size_t ret = iconv((iconv_t)cd, 
120                            inbuf, inbytesleft, 
121                            outbuf, outbytesleft);
122         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
123         return ret;
124 #else
125         errno = EINVAL;
126         return -1;
127 #endif
128 }
129
130 /**
131  * This is a simple portable iconv() implementaion.
132  *
133  * It only knows about a very small number of character sets - just
134  * enough that Samba works on systems that don't have iconv.
135  **/
136 size_t smb_iconv(smb_iconv_t cd, 
137                  const char **inbuf, size_t *inbytesleft,
138                  char **outbuf, size_t *outbytesleft)
139 {
140         char cvtbuf[2048];
141         char *bufp = cvtbuf;
142         size_t bufsize;
143
144         /* in many cases we can go direct */
145         if (cd->direct) {
146                 return cd->direct(cd->cd_direct, 
147                                   (char **)inbuf, inbytesleft, outbuf, outbytesleft);
148         }
149
150
151         /* otherwise we have to do it chunks at a time */
152         while (*inbytesleft > 0) {
153                 bufp = cvtbuf;
154                 bufsize = sizeof(cvtbuf);
155                 
156                 if (cd->pull(cd->cd_pull, 
157                              (char **)inbuf, inbytesleft, &bufp, &bufsize) == -1
158                     && errno != E2BIG) return -1;
159
160                 bufp = cvtbuf;
161                 bufsize = sizeof(cvtbuf) - bufsize;
162
163                 if (cd->push(cd->cd_push, 
164                              &bufp, &bufsize, 
165                              outbuf, outbytesleft) == -1) return -1;
166         }
167
168         return 0;
169 }
170
171 /*
172   simple iconv_open() wrapper
173  */
174 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
175 {
176         smb_iconv_t ret;
177         struct charset_functions *from, *to;
178         
179         lazy_initialize_iconv();
180         from = charsets;
181         to = charsets;
182
183         ret = (smb_iconv_t)malloc(sizeof(*ret));
184         if (!ret) {
185                 errno = ENOMEM;
186                 return (smb_iconv_t)-1;
187         }
188         memset(ret, 0, sizeof(*ret));
189
190         ret->from_name = strdup(fromcode);
191         ret->to_name = strdup(tocode);
192
193         /* check for the simplest null conversion */
194         if (strcasecmp(fromcode, tocode) == 0) {
195                 ret->direct = iconv_copy;
196                 return ret;
197         }
198
199         /* check if we have a builtin function for this conversion */
200         from = find_charset_functions(fromcode);
201         if(from)ret->pull = from->pull;
202         
203         to = find_charset_functions(tocode);
204         if(to)ret->push = to->push;
205
206         /* check if we can use iconv for this conversion */
207 #ifdef HAVE_NATIVE_ICONV
208         if (!ret->pull) {
209                 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
210                 if (ret->cd_pull != (iconv_t)-1)
211                         ret->pull = sys_iconv;
212         }
213
214         if (!ret->push) {
215                 ret->cd_push = iconv_open(tocode, "UCS-2LE");
216                 if (ret->cd_push != (iconv_t)-1)
217                         ret->push = sys_iconv;
218         }
219 #endif
220         
221         /* check if there is a module available that can do this conversion */
222         if (!ret->pull && smb_probe_module("charset", fromcode)) {
223                 if(!(from = find_charset_functions(fromcode)))
224                         DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
225                 else 
226                         ret->pull = from->pull;
227         }
228
229         if (!ret->push && smb_probe_module("charset", tocode)) {
230                 if(!(to = find_charset_functions(tocode)))
231                         DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
232                 else 
233                         ret->push = to->push;
234         }
235
236         if (!ret->push || !ret->pull) {
237                 SAFE_FREE(ret->from_name);
238                 SAFE_FREE(ret->to_name);
239                 SAFE_FREE(ret);
240                 errno = EINVAL;
241                 return (smb_iconv_t)-1;
242         }
243
244         /* check for conversion to/from ucs2 */
245         if (strcasecmp(fromcode, "UCS-2LE") == 0 && to) {
246                 ret->direct = to->push;
247                 ret->push = ret->pull = NULL;
248                 return ret;
249         }
250
251         if (strcasecmp(tocode, "UCS-2LE") == 0 && from) {
252                 ret->direct = from->pull;
253                 ret->push = ret->pull = NULL;
254                 return ret;
255         }
256
257         /* Check if we can do the conversion direct */
258 #ifdef HAVE_NATIVE_ICONV
259         if (strcasecmp(fromcode, "UCS-2LE") == 0) {
260                 ret->direct = sys_iconv;
261                 ret->cd_direct = ret->cd_push;
262                 ret->cd_push = NULL;
263                 return ret;
264         }
265         if (strcasecmp(tocode, "UCS-2LE") == 0) {
266                 ret->direct = sys_iconv;
267                 ret->cd_direct = ret->cd_pull;
268                 ret->cd_pull = NULL;
269                 return ret;
270         }
271 #endif
272
273         return ret;
274 }
275
276 /*
277   simple iconv_close() wrapper
278 */
279 int smb_iconv_close (smb_iconv_t cd)
280 {
281 #ifdef HAVE_NATIVE_ICONV
282         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
283         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
284         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
285 #endif
286
287         SAFE_FREE(cd->from_name);
288         SAFE_FREE(cd->to_name);
289
290         memset(cd, 0, sizeof(*cd));
291         SAFE_FREE(cd);
292         return 0;
293 }
294
295
296 /**********************************************************************
297  the following functions implement the builtin character sets in Samba
298  and also the "test" character sets that are designed to test
299  multi-byte character set support for english users
300 ***********************************************************************/
301
302 static size_t ascii_pull(void *cd, char **inbuf, size_t *inbytesleft,
303                          char **outbuf, size_t *outbytesleft)
304 {
305         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
306                 (*outbuf)[0] = (*inbuf)[0];
307                 (*outbuf)[1] = 0;
308                 (*inbytesleft)  -= 1;
309                 (*outbytesleft) -= 2;
310                 (*inbuf)  += 1;
311                 (*outbuf) += 2;
312         }
313
314         if (*inbytesleft > 0) {
315                 errno = E2BIG;
316                 return -1;
317         }
318         
319         return 0;
320 }
321
322 static size_t ascii_push(void *cd, char **inbuf, size_t *inbytesleft,
323                          char **outbuf, size_t *outbytesleft)
324 {
325         int ir_count=0;
326
327         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
328                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
329                 if ((*inbuf)[1]) ir_count++;
330                 (*inbytesleft)  -= 2;
331                 (*outbytesleft) -= 1;
332                 (*inbuf)  += 2;
333                 (*outbuf) += 1;
334         }
335
336         if (*inbytesleft == 1) {
337                 errno = EINVAL;
338                 return -1;
339         }
340
341         if (*inbytesleft > 1) {
342                 errno = E2BIG;
343                 return -1;
344         }
345         
346         return ir_count;
347 }
348
349
350 static size_t ucs2hex_pull(void *cd, char **inbuf, size_t *inbytesleft,
351                          char **outbuf, size_t *outbytesleft)
352 {
353         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
354                 unsigned v;
355
356                 if ((*inbuf)[0] != '@') {
357                         /* seven bit ascii case */
358                         (*outbuf)[0] = (*inbuf)[0];
359                         (*outbuf)[1] = 0;
360                         (*inbytesleft)  -= 1;
361                         (*outbytesleft) -= 2;
362                         (*inbuf)  += 1;
363                         (*outbuf) += 2;
364                         continue;
365                 }
366                 /* it's a hex character */
367                 if (*inbytesleft < 5) {
368                         errno = EINVAL;
369                         return -1;
370                 }
371                 
372                 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
373                         errno = EILSEQ;
374                         return -1;
375                 }
376
377                 (*outbuf)[0] = v&0xff;
378                 (*outbuf)[1] = v>>8;
379                 (*inbytesleft)  -= 5;
380                 (*outbytesleft) -= 2;
381                 (*inbuf)  += 5;
382                 (*outbuf) += 2;
383         }
384
385         if (*inbytesleft > 0) {
386                 errno = E2BIG;
387                 return -1;
388         }
389         
390         return 0;
391 }
392
393 static size_t ucs2hex_push(void *cd, char **inbuf, size_t *inbytesleft,
394                            char **outbuf, size_t *outbytesleft)
395 {
396         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
397                 char buf[6];
398
399                 if ((*inbuf)[1] == 0 && 
400                     ((*inbuf)[0] & 0x80) == 0 &&
401                     (*inbuf)[0] != '@') {
402                         (*outbuf)[0] = (*inbuf)[0];
403                         (*inbytesleft)  -= 2;
404                         (*outbytesleft) -= 1;
405                         (*inbuf)  += 2;
406                         (*outbuf) += 1;
407                         continue;
408                 }
409                 if (*outbytesleft < 5) {
410                         errno = E2BIG;
411                         return -1;
412                 }
413                 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
414                 memcpy(*outbuf, buf, 5);
415                 (*inbytesleft)  -= 2;
416                 (*outbytesleft) -= 5;
417                 (*inbuf)  += 2;
418                 (*outbuf) += 5;
419         }
420
421         if (*inbytesleft == 1) {
422                 errno = EINVAL;
423                 return -1;
424         }
425
426         if (*inbytesleft > 1) {
427                 errno = E2BIG;
428                 return -1;
429         }
430         
431         return 0;
432 }
433
434
435 static size_t iconv_copy(void *cd, char **inbuf, size_t *inbytesleft,
436                          char **outbuf, size_t *outbytesleft)
437 {
438         int n;
439
440         n = MIN(*inbytesleft, *outbytesleft);
441
442         memmove(*outbuf, *inbuf, n);
443
444         (*inbytesleft) -= n;
445         (*outbytesleft) -= n;
446         (*inbuf) += n;
447         (*outbuf) += n;
448
449         if (*inbytesleft > 0) {
450                 errno = E2BIG;
451                 return -1;
452         }
453
454         return 0;
455 }
456
457 static size_t utf8_pull(void *cd, char **inbuf, size_t *inbytesleft,
458                          char **outbuf, size_t *outbytesleft)
459 {
460         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
461                 unsigned char *c = (unsigned char *)*inbuf;
462                 unsigned char *uc = (unsigned char *)*outbuf;
463                 int len = 1;
464
465                 if ((c[0] & 0x80) == 0) {
466                         uc[0] = c[0];
467                         uc[1] = 0;
468                 } else if ((c[0] & 0xf0) == 0xe0) {
469                         if (*inbytesleft < 3) {
470                                 DEBUG(0,("short utf8 char\n"));
471                                 goto badseq;
472                         }
473                         uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
474                         uc[0] = (c[1]<<6) | (c[2]&0x3f);
475                         len = 3;
476                 } else if ((c[0] & 0xe0) == 0xc0) {
477                         if (*inbytesleft < 2) {
478                                 DEBUG(0,("short utf8 char\n"));
479                                 goto badseq;
480                         }
481                         uc[1] = (c[0]>>2) & 0x7;
482                         uc[0] = (c[0]<<6) | (c[1]&0x3f);
483                         len = 2;
484                 }
485
486                 (*inbuf)  += len;
487                 (*inbytesleft)  -= len;
488                 (*outbytesleft) -= 2;
489                 (*outbuf) += 2;
490         }
491
492         if (*inbytesleft > 0) {
493                 errno = E2BIG;
494                 return -1;
495         }
496         
497         return 0;
498
499 badseq:
500         errno = EINVAL;
501         return -1;
502 }
503
504 static size_t utf8_push(void *cd, char **inbuf, size_t *inbytesleft,
505                          char **outbuf, size_t *outbytesleft)
506 {
507         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
508                 unsigned char *c = (unsigned char *)*outbuf;
509                 unsigned char *uc = (unsigned char *)*inbuf;
510                 int len=1;
511
512                 if (uc[1] & 0xf8) {
513                         if (*outbytesleft < 3) {
514                                 DEBUG(0,("short utf8 write\n"));
515                                 goto toobig;
516                         }
517                         c[0] = 0xe0 | (uc[1]>>4);
518                         c[1] = 0x80 | ((uc[1]&0xF)<<2) | (uc[0]>>6);
519                         c[2] = 0x80 | (uc[0]&0x3f);
520                         len = 3;
521                 } else if (uc[1] | (uc[0] & 0x80)) {
522                         if (*outbytesleft < 2) {
523                                 DEBUG(0,("short utf8 write\n"));
524                                 goto toobig;
525                         }
526                         c[0] = 0xc0 | (uc[1]<<2) | (uc[0]>>6);
527                         c[1] = 0x80 | (uc[0]&0x3f);
528                         len = 2;
529                 } else {
530                         c[0] = uc[0];
531                 }
532
533
534                 (*inbytesleft)  -= 2;
535                 (*outbytesleft) -= len;
536                 (*inbuf)  += 2;
537                 (*outbuf) += len;
538         }
539
540         if (*inbytesleft == 1) {
541                 errno = EINVAL;
542                 return -1;
543         }
544
545         if (*inbytesleft > 1) {
546                 errno = E2BIG;
547                 return -1;
548         }
549         
550         return 0;
551
552 toobig:
553         errno = E2BIG;
554         return -1;
555 }
556