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