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