r5665: the data within el2->values can still be used at this point, so don't free
[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
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 #include "dlinklist.h"
24 #include "system/iconv.h"
25 #include "system/filesys.h"
26
27
28 /**
29  * @file
30  *
31  * @brief Samba wrapper/stub for iconv character set conversion.
32  *
33  * iconv is the XPG2 interface for converting between character
34  * encodings.  This file provides a Samba wrapper around it, and also
35  * a simple reimplementation that is used if the system does not
36  * implement iconv.
37  *
38  * Samba only works with encodings that are supersets of ASCII: ascii
39  * characters like whitespace can be tested for directly, multibyte
40  * sequences start with a byte with the high bit set, and strings are
41  * terminated by a nul byte.
42  *
43  * Note that the only function provided by iconv is conversion between
44  * characters.  It doesn't directly support operations like
45  * uppercasing or comparison.  We have to convert to UTF-16LE and
46  * compare there.
47  *
48  * @sa Samba Developers Guide
49  **/
50
51 static size_t ascii_pull  (void *,const char **, size_t *, char **, size_t *);
52 static size_t ascii_push  (void *,const char **, size_t *, char **, size_t *);
53 static size_t utf8_pull   (void *,const char **, size_t *, char **, size_t *);
54 static size_t utf8_push   (void *,const char **, size_t *, char **, size_t *);
55 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
56 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
57 static size_t iconv_copy  (void *,const char **, size_t *, char **, size_t *);
58 static size_t iconv_swab  (void *,const char **, size_t *, char **, size_t *);
59
60 static const struct charset_functions builtin_functions[] = {
61         /* windows is closest to UTF-16 */
62         {"UCS-2LE",  iconv_copy, iconv_copy},
63         {"UTF-16LE",  iconv_copy, iconv_copy},
64         {"UCS-2BE",  iconv_swab, iconv_swab},
65         {"UTF-16BE",  iconv_swab, iconv_swab},
66
67         /* we include the UTF-8 alias to cope with differing locale settings */
68         {"UTF8",   utf8_pull,  utf8_push},
69         {"UTF-8",   utf8_pull,  utf8_push},
70         {"ASCII", ascii_pull, ascii_push},
71         {"UCS2-HEX", ucs2hex_pull, ucs2hex_push}
72 };
73
74 static struct charset_functions *charsets = NULL;
75
76 NTSTATUS charset_register_backend(const void *_funcs) 
77 {
78         struct charset_functions *funcs = memdup(_funcs,sizeof(struct charset_functions));
79         struct charset_functions *c = charsets;
80
81         /* Check whether we already have this charset... */
82         while(c) {
83                 if(!strcasecmp(c->name, funcs->name)){ 
84                         DEBUG(2, ("Duplicate charset %s, not registering\n", funcs->name));
85                         return NT_STATUS_OBJECT_NAME_COLLISION;
86                 }
87                 c = c->next;
88         }
89
90         funcs->next = funcs->prev = NULL;
91         DLIST_ADD(charsets, funcs);
92         return NT_STATUS_OK;
93 }
94
95 #ifdef HAVE_NATIVE_ICONV
96 /* if there was an error then reset the internal state,
97    this ensures that we don't have a shift state remaining for
98    character sets like SJIS */
99 static size_t sys_iconv(void *cd, 
100                         const char **inbuf, size_t *inbytesleft,
101                         char **outbuf, size_t *outbytesleft)
102 {
103         size_t ret = iconv((iconv_t)cd, 
104                            discard_const_p(char *, inbuf), inbytesleft, 
105                            outbuf, outbytesleft);
106         if (ret == (size_t)-1) iconv(cd, NULL, NULL, NULL, NULL);
107         return ret;
108 }
109 #endif
110
111 /**
112  * This is a simple portable iconv() implementaion.
113  *
114  * It only knows about a very small number of character sets - just
115  * enough that Samba works on systems that don't have iconv.
116  **/
117 size_t smb_iconv(smb_iconv_t cd, 
118                  const char **inbuf, size_t *inbytesleft,
119                  char **outbuf, size_t *outbytesleft)
120 {
121         char cvtbuf[2048];
122         size_t bufsize;
123
124         /* in many cases we can go direct */
125         if (cd->direct) {
126                 return cd->direct(cd->cd_direct, 
127                                   inbuf, inbytesleft, outbuf, outbytesleft);
128         }
129
130
131         /* otherwise we have to do it chunks at a time */
132         while (*inbytesleft > 0) {
133                 char *bufp1 = cvtbuf;
134                 const char *bufp2 = cvtbuf;
135
136                 bufsize = sizeof(cvtbuf);
137                 
138                 if (cd->pull(cd->cd_pull, 
139                              inbuf, inbytesleft, &bufp1, &bufsize) == -1
140                     && errno != E2BIG) return -1;
141
142                 bufsize = sizeof(cvtbuf) - bufsize;
143
144                 if (cd->push(cd->cd_push, 
145                              &bufp2, &bufsize, 
146                              outbuf, outbytesleft) == -1) return -1;
147         }
148
149         return 0;
150 }
151
152 static BOOL is_utf16(const char *name)
153 {
154         return strcasecmp(name, "UCS-2LE") == 0 ||
155                 strcasecmp(name, "UTF-16LE") == 0;
156 }
157
158 /*
159   simple iconv_open() wrapper
160  */
161 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
162 {
163         smb_iconv_t ret;
164         const struct charset_functions *from=NULL, *to=NULL;
165         int i;
166
167         ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret), 
168                                         "iconv(%s,%s)", tocode, fromcode);
169         if (!ret) {
170                 errno = ENOMEM;
171                 return (smb_iconv_t)-1;
172         }
173         memset(ret, 0, sizeof(*ret));
174
175         /* check for the simplest null conversion */
176         if (strcmp(fromcode, tocode) == 0) {
177                 ret->direct = iconv_copy;
178                 return ret;
179         }
180
181         for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
182                 if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
183                         from = &builtin_functions[i];
184                 }
185                 if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
186                         to = &builtin_functions[i];
187                 }
188         }
189
190         if (from == NULL) {
191                 for (from=charsets; from; from=from->next) {
192                         if (strcasecmp(from->name, fromcode) == 0) break;
193                 }
194         }
195
196         if (to == NULL) {
197                 for (to=charsets; to; to=to->next) {
198                         if (strcasecmp(to->name, tocode) == 0) break;
199                 }
200         }
201
202 #ifdef HAVE_NATIVE_ICONV
203         if (!from) {
204                 ret->pull = sys_iconv;
205                 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
206                 if (ret->cd_pull == (iconv_t)-1)
207                         ret->cd_pull = iconv_open("UCS-2LE", fromcode);
208                 if (ret->cd_pull == (iconv_t)-1) goto failed;
209         }
210
211         if (!to) {
212                 ret->push = sys_iconv;
213                 ret->cd_push = iconv_open(tocode, "UTF-16LE");
214                 if (ret->cd_push == (iconv_t)-1)
215                         ret->cd_push = iconv_open(tocode, "UCS-2LE");
216                 if (ret->cd_push == (iconv_t)-1) goto failed;
217         }
218 #else
219         if (!from || !to) {
220                 goto failed;
221         }
222 #endif
223
224         /* check for conversion to/from ucs2 */
225         if (is_utf16(fromcode) && to) {
226                 ret->direct = to->push;
227                 return ret;
228         }
229         if (is_utf16(tocode) && from) {
230                 ret->direct = from->pull;
231                 return ret;
232         }
233
234 #ifdef HAVE_NATIVE_ICONV
235         if (is_utf16(fromcode)) {
236                 ret->direct = sys_iconv;
237                 ret->cd_direct = ret->cd_push;
238                 ret->cd_push = NULL;
239                 return ret;
240         }
241         if (is_utf16(tocode)) {
242                 ret->direct = sys_iconv;
243                 ret->cd_direct = ret->cd_pull;
244                 ret->cd_pull = NULL;
245                 return ret;
246         }
247 #endif
248
249         /* the general case has to go via a buffer */
250         if (!ret->pull) ret->pull = from->pull;
251         if (!ret->push) ret->push = to->push;
252         return ret;
253
254 failed:
255         talloc_free(ret);
256         errno = EINVAL;
257         return (smb_iconv_t)-1;
258 }
259
260 /*
261   simple iconv_close() wrapper
262 */
263 int smb_iconv_close(smb_iconv_t cd)
264 {
265 #ifdef HAVE_NATIVE_ICONV
266         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
267         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
268         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
269 #endif
270
271         talloc_free(cd);
272         return 0;
273 }
274
275
276 /**********************************************************************
277  the following functions implement the builtin character sets in Samba
278  and also the "test" character sets that are designed to test
279  multi-byte character set support for english users
280 ***********************************************************************/
281 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
282                          char **outbuf, size_t *outbytesleft)
283 {
284         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
285                 (*outbuf)[0] = (*inbuf)[0];
286                 (*outbuf)[1] = 0;
287                 (*inbytesleft)  -= 1;
288                 (*outbytesleft) -= 2;
289                 (*inbuf)  += 1;
290                 (*outbuf) += 2;
291         }
292
293         if (*inbytesleft > 0) {
294                 errno = E2BIG;
295                 return -1;
296         }
297         
298         return 0;
299 }
300
301 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
302                          char **outbuf, size_t *outbytesleft)
303 {
304         int ir_count=0;
305
306         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
307                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
308                 if ((*inbuf)[1]) ir_count++;
309                 (*inbytesleft)  -= 2;
310                 (*outbytesleft) -= 1;
311                 (*inbuf)  += 2;
312                 (*outbuf) += 1;
313         }
314
315         if (*inbytesleft == 1) {
316                 errno = EINVAL;
317                 return -1;
318         }
319
320         if (*inbytesleft > 1) {
321                 errno = E2BIG;
322                 return -1;
323         }
324         
325         return ir_count;
326 }
327
328
329 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
330                          char **outbuf, size_t *outbytesleft)
331 {
332         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
333                 uint_t v;
334
335                 if ((*inbuf)[0] != '@') {
336                         /* seven bit ascii case */
337                         (*outbuf)[0] = (*inbuf)[0];
338                         (*outbuf)[1] = 0;
339                         (*inbytesleft)  -= 1;
340                         (*outbytesleft) -= 2;
341                         (*inbuf)  += 1;
342                         (*outbuf) += 2;
343                         continue;
344                 }
345                 /* it's a hex character */
346                 if (*inbytesleft < 5) {
347                         errno = EINVAL;
348                         return -1;
349                 }
350                 
351                 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
352                         errno = EILSEQ;
353                         return -1;
354                 }
355
356                 (*outbuf)[0] = v&0xff;
357                 (*outbuf)[1] = v>>8;
358                 (*inbytesleft)  -= 5;
359                 (*outbytesleft) -= 2;
360                 (*inbuf)  += 5;
361                 (*outbuf) += 2;
362         }
363
364         if (*inbytesleft > 0) {
365                 errno = E2BIG;
366                 return -1;
367         }
368         
369         return 0;
370 }
371
372 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
373                            char **outbuf, size_t *outbytesleft)
374 {
375         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
376                 char buf[6];
377
378                 if ((*inbuf)[1] == 0 && 
379                     ((*inbuf)[0] & 0x80) == 0 &&
380                     (*inbuf)[0] != '@') {
381                         (*outbuf)[0] = (*inbuf)[0];
382                         (*inbytesleft)  -= 2;
383                         (*outbytesleft) -= 1;
384                         (*inbuf)  += 2;
385                         (*outbuf) += 1;
386                         continue;
387                 }
388                 if (*outbytesleft < 5) {
389                         errno = E2BIG;
390                         return -1;
391                 }
392                 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
393                 memcpy(*outbuf, buf, 5);
394                 (*inbytesleft)  -= 2;
395                 (*outbytesleft) -= 5;
396                 (*inbuf)  += 2;
397                 (*outbuf) += 5;
398         }
399
400         if (*inbytesleft == 1) {
401                 errno = EINVAL;
402                 return -1;
403         }
404
405         if (*inbytesleft > 1) {
406                 errno = E2BIG;
407                 return -1;
408         }
409         
410         return 0;
411 }
412
413 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
414                          char **outbuf, size_t *outbytesleft)
415 {
416         int n;
417
418         n = MIN(*inbytesleft, *outbytesleft);
419
420         swab(*inbuf, *outbuf, (n&~1));
421         if (n&1) {
422                 (*outbuf)[n-1] = 0;
423         }
424
425         (*inbytesleft) -= n;
426         (*outbytesleft) -= n;
427         (*inbuf) += n;
428         (*outbuf) += n;
429
430         if (*inbytesleft > 0) {
431                 errno = E2BIG;
432                 return -1;
433         }
434
435         return 0;
436 }
437
438
439 static size_t iconv_copy(void *cd, const 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 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
462                          char **outbuf, size_t *outbytesleft)
463 {
464         size_t in_left=*inbytesleft, out_left=*outbytesleft;
465         const uint8_t *c = (const uint8_t *)*inbuf;
466         uint8_t *uc = (uint8_t *)*outbuf;
467
468         while (in_left >= 1 && out_left >= 2) {
469                 if ((c[0] & 0x80) == 0) {
470                         uc[0] = c[0];
471                         uc[1] = 0;
472                         c  += 1;
473                         in_left  -= 1;
474                         out_left -= 2;
475                         uc += 2;
476                         continue;
477                 }
478
479                 if ((c[0] & 0xe0) == 0xc0) {
480                         if (in_left < 2 ||
481                             (c[1] & 0xc0) != 0x80) {
482                                 errno = EILSEQ;
483                                 goto error;
484                         }
485                         uc[1] = (c[0]>>2) & 0x7;
486                         uc[0] = (c[0]<<6) | (c[1]&0x3f);
487                         c  += 2;
488                         in_left  -= 2;
489                         out_left -= 2;
490                         uc += 2;
491                         continue;
492                 }
493
494                 if ((c[0] & 0xf0) == 0xe0) {
495                         if (in_left < 3 ||
496                             (c[1] & 0xc0) != 0x80 || 
497                             (c[2] & 0xc0) != 0x80) {
498                                 errno = EILSEQ;
499                                 goto error;
500                         }
501                         uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
502                         uc[0] = (c[1]<<6) | (c[2]&0x3f);
503                         c  += 3;
504                         in_left  -= 3;
505                         out_left -= 2;
506                         uc += 2;
507                         continue;
508                 }
509
510                 if ((c[0] & 0xf8) == 0xf0) {
511                         unsigned int codepoint;
512                         if (in_left < 4 ||
513                             (c[1] & 0xc0) != 0x80 || 
514                             (c[2] & 0xc0) != 0x80 ||
515                             (c[3] & 0xc0) != 0x80) {
516                                 errno = EILSEQ;
517                                 goto error;
518                         }
519                         codepoint = 
520                                 (c[3]&0x3f) | 
521                                 ((c[2]&0x3f)<<6) | 
522                                 ((c[1]&0x3f)<<12) |
523                                 ((c[0]&0x7)<<18);
524                         if (codepoint < 0x10000) {
525                                 /* accept UTF-8 characters that are not
526                                    minimally packed, but pack the result */
527                                 uc[0] = (codepoint & 0xFF);
528                                 uc[1] = (codepoint >> 8);
529                                 c += 4;
530                                 in_left -= 4;
531                                 out_left -= 2;
532                                 uc += 2;
533                                 continue;
534                         }
535
536                         codepoint -= 0x10000;
537
538                         if (out_left < 4) {
539                                 errno = E2BIG;
540                                 goto error;
541                         }
542
543                         uc[0] = (codepoint>>10) & 0xFF;
544                         uc[1] = (codepoint>>18) | 0xd8;
545                         uc[2] = codepoint & 0xFF;
546                         uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
547                         c  += 4;
548                         in_left  -= 4;
549                         out_left -= 4;
550                         uc += 4;
551                         continue;
552                 }
553
554                 /* we don't handle 5 byte sequences */
555                 errno = EINVAL;
556                 goto error;
557         }
558
559         if (in_left > 0) {
560                 errno = E2BIG;
561                 goto error;
562         }
563
564         *inbytesleft = in_left;
565         *outbytesleft = out_left;
566         *inbuf = c;
567         *outbuf = uc;   
568         return 0;
569
570 error:
571         *inbytesleft = in_left;
572         *outbytesleft = out_left;
573         *inbuf = c;
574         *outbuf = uc;
575         return -1;
576 }
577
578 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
579                         char **outbuf, size_t *outbytesleft)
580 {
581         size_t in_left=*inbytesleft, out_left=*outbytesleft;
582         uint8_t *c = (uint8_t *)*outbuf;
583         const uint8_t *uc = (const uint8_t *)*inbuf;
584
585         while (in_left >= 2 && out_left >= 1) {
586                 unsigned int codepoint;
587
588                 if (uc[1] == 0 && !(uc[0] & 0x80)) {
589                         /* simplest case */
590                         c[0] = uc[0];
591                         in_left  -= 2;
592                         out_left -= 1;
593                         uc += 2;
594                         c  += 1;
595                         continue;
596                 }
597
598                 if ((uc[1]&0xf8) == 0) {
599                         /* next simplest case */
600                         if (out_left < 2) {
601                                 errno = E2BIG;
602                                 goto error;
603                         }
604                         c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
605                         c[1] = 0x80 | (uc[0] & 0x3f);
606                         in_left  -= 2;
607                         out_left -= 2;
608                         uc += 2;
609                         c  += 2;
610                         continue;
611                 }
612
613                 if ((uc[1] & 0xfc) == 0xdc) {
614                         /* its the second part of a 4 byte sequence. Illegal */
615                         if (in_left < 4) {
616                                 errno = EINVAL;
617                         } else {
618                                 errno = EILSEQ;
619                         }
620                         goto error;
621                 }
622
623                 if ((uc[1] & 0xfc) != 0xd8) {
624                         codepoint = uc[0] | (uc[1]<<8);
625                         if (out_left < 3) {
626                                 errno = E2BIG;
627                                 goto error;
628                         }
629                         c[0] = 0xe0 | (codepoint >> 12);
630                         c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
631                         c[2] = 0x80 | (codepoint & 0x3f);
632                         
633                         in_left  -= 2;
634                         out_left -= 3;
635                         uc  += 2;
636                         c   += 3;
637                         continue;
638                 }
639
640                 /* its the first part of a 4 byte sequence */
641                 if (in_left < 4) {
642                         errno = EINVAL;
643                         goto error;
644                 }
645                 if ((uc[3] & 0xfc) != 0xdc) {
646                         errno = EILSEQ;
647                         goto error;
648                 }
649                 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) | 
650                                        (uc[0]<<10) | ((uc[1] & 0x3)<<18));
651                 
652                 if (out_left < 4) {
653                         errno = E2BIG;
654                         goto error;
655                 }
656                 c[0] = 0xf0 | (codepoint >> 18);
657                 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
658                 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
659                 c[3] = 0x80 | (codepoint & 0x3f);
660                 
661                 in_left  -= 4;
662                 out_left -= 4;
663                 uc       += 4;
664                 c        += 4;
665         }
666
667         if (in_left == 1) {
668                 errno = EINVAL;
669                 goto error;
670         }
671
672         if (in_left > 1) {
673                 errno = E2BIG;
674                 goto error;
675         }
676
677         *inbytesleft = in_left;
678         *outbytesleft = out_left;
679         *inbuf  = uc;
680         *outbuf = c;
681         
682         return 0;
683
684 error:
685         *inbytesleft = in_left;
686         *outbytesleft = out_left;
687         *inbuf  = uc;
688         *outbuf = c;
689         return -1;
690 }
691
692
693