d4f930b4622dc8893d4f58acd6a743374d5caf9d
[kai/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 #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 _PUBLIC_ 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
159 _PUBLIC_ smb_iconv_t smb_iconv_open_ex(TALLOC_CTX *mem_ctx, const char *tocode, 
160                               const char *fromcode, bool native_iconv)
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(mem_ctx,
167                                         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 || !to) && !native_iconv) {
204                 goto failed;
205         }
206         if (!from) {
207                 ret->pull = sys_iconv;
208                 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
209                 if (ret->cd_pull == (iconv_t)-1)
210                         ret->cd_pull = iconv_open("UCS-2LE", fromcode);
211                 if (ret->cd_pull == (iconv_t)-1) goto failed;
212         }
213
214         if (!to) {
215                 ret->push = sys_iconv;
216                 ret->cd_push = iconv_open(tocode, "UTF-16LE");
217                 if (ret->cd_push == (iconv_t)-1)
218                         ret->cd_push = iconv_open(tocode, "UCS-2LE");
219                 if (ret->cd_push == (iconv_t)-1) goto failed;
220         }
221 #else
222         if (!from || !to) {
223                 goto failed;
224         }
225 #endif
226
227         /* check for conversion to/from ucs2 */
228         if (is_utf16(fromcode) && to) {
229                 ret->direct = to->push;
230                 return ret;
231         }
232         if (is_utf16(tocode) && from) {
233                 ret->direct = from->pull;
234                 return ret;
235         }
236
237 #ifdef HAVE_NATIVE_ICONV
238         if (is_utf16(fromcode)) {
239                 ret->direct = sys_iconv;
240                 ret->cd_direct = ret->cd_push;
241                 ret->cd_push = NULL;
242                 return ret;
243         }
244         if (is_utf16(tocode)) {
245                 ret->direct = sys_iconv;
246                 ret->cd_direct = ret->cd_pull;
247                 ret->cd_pull = NULL;
248                 return ret;
249         }
250 #endif
251
252         /* the general case has to go via a buffer */
253         if (!ret->pull) ret->pull = from->pull;
254         if (!ret->push) ret->push = to->push;
255         return ret;
256
257 failed:
258         talloc_free(ret);
259         errno = EINVAL;
260         return (smb_iconv_t)-1;
261 }
262
263 /*
264   simple iconv_open() wrapper
265  */
266 _PUBLIC_ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
267 {
268         return smb_iconv_open_ex(NULL, tocode, fromcode, true);
269 }
270
271 /*
272   simple iconv_close() wrapper
273 */
274 _PUBLIC_ int smb_iconv_close(smb_iconv_t cd)
275 {
276 #ifdef HAVE_NATIVE_ICONV
277         if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
278         if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
279         if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
280 #endif
281
282         talloc_free(cd);
283         return 0;
284 }
285
286
287 /**********************************************************************
288  the following functions implement the builtin character sets in Samba
289  and also the "test" character sets that are designed to test
290  multi-byte character set support for english users
291 ***********************************************************************/
292 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
293                          char **outbuf, size_t *outbytesleft)
294 {
295         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
296                 (*outbuf)[0] = (*inbuf)[0];
297                 (*outbuf)[1] = 0;
298                 (*inbytesleft)  -= 1;
299                 (*outbytesleft) -= 2;
300                 (*inbuf)  += 1;
301                 (*outbuf) += 2;
302         }
303
304         if (*inbytesleft > 0) {
305                 errno = E2BIG;
306                 return -1;
307         }
308         
309         return 0;
310 }
311
312 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
313                          char **outbuf, size_t *outbytesleft)
314 {
315         int ir_count=0;
316
317         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
318                 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
319                 if ((*inbuf)[1]) ir_count++;
320                 (*inbytesleft)  -= 2;
321                 (*outbytesleft) -= 1;
322                 (*inbuf)  += 2;
323                 (*outbuf) += 1;
324         }
325
326         if (*inbytesleft == 1) {
327                 errno = EINVAL;
328                 return -1;
329         }
330
331         if (*inbytesleft > 1) {
332                 errno = E2BIG;
333                 return -1;
334         }
335         
336         return ir_count;
337 }
338
339
340 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
341                          char **outbuf, size_t *outbytesleft)
342 {
343         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
344                 uint_t v;
345
346                 if ((*inbuf)[0] != '@') {
347                         /* seven bit ascii case */
348                         (*outbuf)[0] = (*inbuf)[0];
349                         (*outbuf)[1] = 0;
350                         (*inbytesleft)  -= 1;
351                         (*outbytesleft) -= 2;
352                         (*inbuf)  += 1;
353                         (*outbuf) += 2;
354                         continue;
355                 }
356                 /* it's a hex character */
357                 if (*inbytesleft < 5) {
358                         errno = EINVAL;
359                         return -1;
360                 }
361                 
362                 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
363                         errno = EILSEQ;
364                         return -1;
365                 }
366
367                 (*outbuf)[0] = v&0xff;
368                 (*outbuf)[1] = v>>8;
369                 (*inbytesleft)  -= 5;
370                 (*outbytesleft) -= 2;
371                 (*inbuf)  += 5;
372                 (*outbuf) += 2;
373         }
374
375         if (*inbytesleft > 0) {
376                 errno = E2BIG;
377                 return -1;
378         }
379         
380         return 0;
381 }
382
383 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
384                            char **outbuf, size_t *outbytesleft)
385 {
386         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
387                 char buf[6];
388
389                 if ((*inbuf)[1] == 0 && 
390                     ((*inbuf)[0] & 0x80) == 0 &&
391                     (*inbuf)[0] != '@') {
392                         (*outbuf)[0] = (*inbuf)[0];
393                         (*inbytesleft)  -= 2;
394                         (*outbytesleft) -= 1;
395                         (*inbuf)  += 2;
396                         (*outbuf) += 1;
397                         continue;
398                 }
399                 if (*outbytesleft < 5) {
400                         errno = E2BIG;
401                         return -1;
402                 }
403                 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
404                 memcpy(*outbuf, buf, 5);
405                 (*inbytesleft)  -= 2;
406                 (*outbytesleft) -= 5;
407                 (*inbuf)  += 2;
408                 (*outbuf) += 5;
409         }
410
411         if (*inbytesleft == 1) {
412                 errno = EINVAL;
413                 return -1;
414         }
415
416         if (*inbytesleft > 1) {
417                 errno = E2BIG;
418                 return -1;
419         }
420         
421         return 0;
422 }
423
424 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
425                          char **outbuf, size_t *outbytesleft)
426 {
427         int n;
428
429         n = MIN(*inbytesleft, *outbytesleft);
430
431         swab(*inbuf, *outbuf, (n&~1));
432         if (n&1) {
433                 (*outbuf)[n-1] = 0;
434         }
435
436         (*inbytesleft) -= n;
437         (*outbytesleft) -= n;
438         (*inbuf) += n;
439         (*outbuf) += n;
440
441         if (*inbytesleft > 0) {
442                 errno = E2BIG;
443                 return -1;
444         }
445
446         return 0;
447 }
448
449
450 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
451                          char **outbuf, size_t *outbytesleft)
452 {
453         int n;
454
455         n = MIN(*inbytesleft, *outbytesleft);
456
457         memmove(*outbuf, *inbuf, n);
458
459         (*inbytesleft) -= n;
460         (*outbytesleft) -= n;
461         (*inbuf) += n;
462         (*outbuf) += n;
463
464         if (*inbytesleft > 0) {
465                 errno = E2BIG;
466                 return -1;
467         }
468
469         return 0;
470 }
471
472 /*
473   this takes a UTF8 sequence and produces a UTF16 sequence
474  */
475 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
476                          char **outbuf, size_t *outbytesleft)
477 {
478         size_t in_left=*inbytesleft, out_left=*outbytesleft;
479         const uint8_t *c = (const uint8_t *)*inbuf;
480         uint8_t *uc = (uint8_t *)*outbuf;
481
482         while (in_left >= 1 && out_left >= 2) {
483                 if ((c[0] & 0x80) == 0) {
484                         uc[0] = c[0];
485                         uc[1] = 0;
486                         c  += 1;
487                         in_left  -= 1;
488                         out_left -= 2;
489                         uc += 2;
490                         continue;
491                 }
492
493                 if ((c[0] & 0xe0) == 0xc0) {
494                         if (in_left < 2 ||
495                             (c[1] & 0xc0) != 0x80) {
496                                 errno = EILSEQ;
497                                 goto error;
498                         }
499                         uc[1] = (c[0]>>2) & 0x7;
500                         uc[0] = (c[0]<<6) | (c[1]&0x3f);
501                         c  += 2;
502                         in_left  -= 2;
503                         out_left -= 2;
504                         uc += 2;
505                         continue;
506                 }
507
508                 if ((c[0] & 0xf0) == 0xe0) {
509                         if (in_left < 3 ||
510                             (c[1] & 0xc0) != 0x80 || 
511                             (c[2] & 0xc0) != 0x80) {
512                                 errno = EILSEQ;
513                                 goto error;
514                         }
515                         uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
516                         uc[0] = (c[1]<<6) | (c[2]&0x3f);
517                         c  += 3;
518                         in_left  -= 3;
519                         out_left -= 2;
520                         uc += 2;
521                         continue;
522                 }
523
524                 if ((c[0] & 0xf8) == 0xf0) {
525                         unsigned int codepoint;
526                         if (in_left < 4 ||
527                             (c[1] & 0xc0) != 0x80 || 
528                             (c[2] & 0xc0) != 0x80 ||
529                             (c[3] & 0xc0) != 0x80) {
530                                 errno = EILSEQ;
531                                 goto error;
532                         }
533                         codepoint = 
534                                 (c[3]&0x3f) | 
535                                 ((c[2]&0x3f)<<6) | 
536                                 ((c[1]&0x3f)<<12) |
537                                 ((c[0]&0x7)<<18);
538                         if (codepoint < 0x10000) {
539                                 /* accept UTF-8 characters that are not
540                                    minimally packed, but pack the result */
541                                 uc[0] = (codepoint & 0xFF);
542                                 uc[1] = (codepoint >> 8);
543                                 c += 4;
544                                 in_left -= 4;
545                                 out_left -= 2;
546                                 uc += 2;
547                                 continue;
548                         }
549
550                         codepoint -= 0x10000;
551
552                         if (out_left < 4) {
553                                 errno = E2BIG;
554                                 goto error;
555                         }
556
557                         uc[0] = (codepoint>>10) & 0xFF;
558                         uc[1] = (codepoint>>18) | 0xd8;
559                         uc[2] = codepoint & 0xFF;
560                         uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
561                         c  += 4;
562                         in_left  -= 4;
563                         out_left -= 4;
564                         uc += 4;
565                         continue;
566                 }
567
568                 /* we don't handle 5 byte sequences */
569                 errno = EINVAL;
570                 goto error;
571         }
572
573         if (in_left > 0) {
574                 errno = E2BIG;
575                 goto error;
576         }
577
578         *inbytesleft = in_left;
579         *outbytesleft = out_left;
580         *inbuf = (const char *)c;
581         *outbuf = (char *)uc;
582         return 0;
583
584 error:
585         *inbytesleft = in_left;
586         *outbytesleft = out_left;
587         *inbuf = (const char *)c;
588         *outbuf = (char *)uc;
589         return -1;
590 }
591
592
593 /*
594   this takes a UTF16 sequence and produces a UTF8 sequence
595  */
596 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
597                         char **outbuf, size_t *outbytesleft)
598 {
599         size_t in_left=*inbytesleft, out_left=*outbytesleft;
600         uint8_t *c = (uint8_t *)*outbuf;
601         const uint8_t *uc = (const uint8_t *)*inbuf;
602
603         while (in_left >= 2 && out_left >= 1) {
604                 unsigned int codepoint;
605
606                 if (uc[1] == 0 && !(uc[0] & 0x80)) {
607                         /* simplest case */
608                         c[0] = uc[0];
609                         in_left  -= 2;
610                         out_left -= 1;
611                         uc += 2;
612                         c  += 1;
613                         continue;
614                 }
615
616                 if ((uc[1]&0xf8) == 0) {
617                         /* next simplest case */
618                         if (out_left < 2) {
619                                 errno = E2BIG;
620                                 goto error;
621                         }
622                         c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
623                         c[1] = 0x80 | (uc[0] & 0x3f);
624                         in_left  -= 2;
625                         out_left -= 2;
626                         uc += 2;
627                         c  += 2;
628                         continue;
629                 }
630
631                 if ((uc[1] & 0xfc) == 0xdc) {
632                         /* its the second part of a 4 byte sequence. Illegal */
633                         if (in_left < 4) {
634                                 errno = EINVAL;
635                         } else {
636                                 errno = EILSEQ;
637                         }
638                         goto error;
639                 }
640
641                 if ((uc[1] & 0xfc) != 0xd8) {
642                         codepoint = uc[0] | (uc[1]<<8);
643                         if (out_left < 3) {
644                                 errno = E2BIG;
645                                 goto error;
646                         }
647                         c[0] = 0xe0 | (codepoint >> 12);
648                         c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
649                         c[2] = 0x80 | (codepoint & 0x3f);
650                         
651                         in_left  -= 2;
652                         out_left -= 3;
653                         uc  += 2;
654                         c   += 3;
655                         continue;
656                 }
657
658                 /* its the first part of a 4 byte sequence */
659                 if (in_left < 4) {
660                         errno = EINVAL;
661                         goto error;
662                 }
663                 if ((uc[3] & 0xfc) != 0xdc) {
664                         errno = EILSEQ;
665                         goto error;
666                 }
667                 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) | 
668                                        (uc[0]<<10) | ((uc[1] & 0x3)<<18));
669                 
670                 if (out_left < 4) {
671                         errno = E2BIG;
672                         goto error;
673                 }
674                 c[0] = 0xf0 | (codepoint >> 18);
675                 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
676                 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
677                 c[3] = 0x80 | (codepoint & 0x3f);
678                 
679                 in_left  -= 4;
680                 out_left -= 4;
681                 uc       += 4;
682                 c        += 4;
683         }
684
685         if (in_left == 1) {
686                 errno = EINVAL;
687                 goto error;
688         }
689
690         if (in_left > 1) {
691                 errno = E2BIG;
692                 goto error;
693         }
694
695         *inbytesleft = in_left;
696         *outbytesleft = out_left;
697         *inbuf  = (const char *)uc;
698         *outbuf = (char *)c;
699         
700         return 0;
701
702 error:
703         *inbytesleft = in_left;
704         *outbytesleft = out_left;
705         *inbuf  = (const char *)uc;
706         *outbuf = (char *)c;
707         return -1;
708 }
709
710
711