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