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