r17972: revert accidental commit to ads_verify_ticket()
[Samba.git] / source / lib / iconv.c
blobc96243633fbaf6b2049747f27743d1389a63c4fd
1 /*
2 Unix SMB/CIFS implementation.
3 minimal iconv implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jelmer Vernooij 2002,2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
25 * We have to use strcasecmp here as the character conversions
26 * haven't been initialised yet. JRA.
29 #undef strcasecmp
31 /**
32 * @file
34 * @brief Samba wrapper/stub for iconv character set conversion.
36 * iconv is the XPG2 interface for converting between character
37 * encodings. This file provides a Samba wrapper around it, and also
38 * a simple reimplementation that is used if the system does not
39 * implement iconv.
41 * Samba only works with encodings that are supersets of ASCII: ascii
42 * characters like whitespace can be tested for directly, multibyte
43 * sequences start with a byte with the high bit set, and strings are
44 * terminated by a nul byte.
46 * Note that the only function provided by iconv is conversion between
47 * characters. It doesn't directly support operations like
48 * uppercasing or comparison. We have to convert to UCS-2 and compare
49 * there.
51 * @sa Samba Developers Guide
52 **/
54 static_decl_charset;
56 static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
57 static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
58 static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
59 static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
60 static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
61 static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
62 static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
63 static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
64 static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
66 static struct charset_functions builtin_functions[] = {
67 /* windows is really neither UCS-2 not UTF-16 */
68 {"UCS-2LE", iconv_copy, iconv_copy},
69 {"UTF-16LE", iconv_copy, iconv_copy},
70 {"UCS-2BE", iconv_swab, iconv_swab},
71 {"UTF-16BE", iconv_swab, iconv_swab},
73 /* we include the UTF-8 alias to cope with differing locale settings */
74 {"UTF8", utf8_pull, utf8_push},
75 {"UTF-8", utf8_pull, utf8_push},
76 {"ASCII", ascii_pull, ascii_push},
77 {"646", ascii_pull, ascii_push},
78 {"ISO-8859-1", ascii_pull, latin1_push},
79 {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
80 {NULL, NULL, NULL}
83 static struct charset_functions *charsets = NULL;
85 static struct charset_functions *find_charset_functions(const char *name)
87 struct charset_functions *c = charsets;
89 while(c) {
90 if (strcasecmp(name, c->name) == 0) {
91 return c;
93 c = c->next;
96 return NULL;
99 NTSTATUS smb_register_charset(struct charset_functions *funcs)
101 if (!funcs) {
102 return NT_STATUS_INVALID_PARAMETER;
105 DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
106 /* Check whether we already have this charset... */
107 if (find_charset_functions(funcs->name)) {
108 DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
109 return NT_STATUS_OBJECT_NAME_COLLISION;
112 funcs->next = funcs->prev = NULL;
113 DEBUG(5, ("Registered charset %s\n", funcs->name));
114 DLIST_ADD(charsets, funcs);
115 return NT_STATUS_OK;
118 static void lazy_initialize_iconv(void)
120 static BOOL initialized;
121 int i;
123 if (!initialized) {
124 initialized = True;
125 for(i = 0; builtin_functions[i].name; i++)
126 smb_register_charset(&builtin_functions[i]);
127 static_init_charset;
131 /* if there was an error then reset the internal state,
132 this ensures that we don't have a shift state remaining for
133 character sets like SJIS */
134 static size_t sys_iconv(void *cd,
135 const char **inbuf, size_t *inbytesleft,
136 char **outbuf, size_t *outbytesleft)
138 #ifdef HAVE_NATIVE_ICONV
139 size_t ret = iconv((iconv_t)cd,
140 (char **)inbuf, inbytesleft,
141 outbuf, outbytesleft);
142 if (ret == (size_t)-1) {
143 int saved_errno = errno;
144 iconv(cd, NULL, NULL, NULL, NULL);
145 errno = saved_errno;
147 return ret;
148 #else
149 errno = EINVAL;
150 return -1;
151 #endif
155 * This is a simple portable iconv() implementaion.
157 * It only knows about a very small number of character sets - just
158 * enough that Samba works on systems that don't have iconv.
160 size_t smb_iconv(smb_iconv_t cd,
161 const char **inbuf, size_t *inbytesleft,
162 char **outbuf, size_t *outbytesleft)
164 char cvtbuf[2048];
165 char *bufp = cvtbuf;
166 size_t bufsize;
168 /* in many cases we can go direct */
169 if (cd->direct) {
170 return cd->direct(cd->cd_direct,
171 inbuf, inbytesleft, outbuf, outbytesleft);
175 /* otherwise we have to do it chunks at a time */
176 while (*inbytesleft > 0) {
177 bufp = cvtbuf;
178 bufsize = sizeof(cvtbuf);
180 if (cd->pull(cd->cd_pull,
181 inbuf, inbytesleft, &bufp, &bufsize) == -1
182 && errno != E2BIG) return -1;
184 bufp = cvtbuf;
185 bufsize = sizeof(cvtbuf) - bufsize;
187 if (cd->push(cd->cd_push,
188 (const char **)&bufp, &bufsize,
189 outbuf, outbytesleft) == -1) return -1;
192 return 0;
196 static BOOL is_utf16(const char *name)
198 return strcasecmp(name, "UCS-2LE") == 0 ||
199 strcasecmp(name, "UTF-16LE") == 0;
203 simple iconv_open() wrapper
205 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
207 smb_iconv_t ret;
208 struct charset_functions *from, *to;
210 lazy_initialize_iconv();
211 from = charsets;
212 to = charsets;
214 ret = SMB_MALLOC_P(struct _smb_iconv_t);
215 if (!ret) {
216 errno = ENOMEM;
217 return (smb_iconv_t)-1;
219 memset(ret, 0, sizeof(struct _smb_iconv_t));
221 ret->from_name = SMB_STRDUP(fromcode);
222 ret->to_name = SMB_STRDUP(tocode);
224 /* check for the simplest null conversion */
225 if (strcasecmp(fromcode, tocode) == 0) {
226 ret->direct = iconv_copy;
227 return ret;
230 /* check if we have a builtin function for this conversion */
231 from = find_charset_functions(fromcode);
232 if(from)ret->pull = from->pull;
234 to = find_charset_functions(tocode);
235 if(to)ret->push = to->push;
237 /* check if we can use iconv for this conversion */
238 #ifdef HAVE_NATIVE_ICONV
239 if (!ret->pull) {
240 ret->cd_pull = iconv_open("UTF-16LE", fromcode);
241 if (ret->cd_pull == (iconv_t)-1)
242 ret->cd_pull = iconv_open("UCS-2LE", fromcode);
243 if (ret->cd_pull != (iconv_t)-1)
244 ret->pull = sys_iconv;
247 if (!ret->push) {
248 ret->cd_push = iconv_open(tocode, "UTF-16LE");
249 if (ret->cd_push == (iconv_t)-1)
250 ret->cd_push = iconv_open(tocode, "UCS-2LE");
251 if (ret->cd_push != (iconv_t)-1)
252 ret->push = sys_iconv;
254 #endif
256 /* check if there is a module available that can do this conversion */
257 if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
258 if(!(from = find_charset_functions(fromcode)))
259 DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
260 else
261 ret->pull = from->pull;
264 if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
265 if(!(to = find_charset_functions(tocode)))
266 DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
267 else
268 ret->push = to->push;
271 if (!ret->push || !ret->pull) {
272 SAFE_FREE(ret->from_name);
273 SAFE_FREE(ret->to_name);
274 SAFE_FREE(ret);
275 errno = EINVAL;
276 return (smb_iconv_t)-1;
279 /* check for conversion to/from ucs2 */
280 if (is_utf16(fromcode) && to) {
281 ret->direct = to->push;
282 ret->push = ret->pull = NULL;
283 return ret;
286 if (is_utf16(tocode) && from) {
287 ret->direct = from->pull;
288 ret->push = ret->pull = NULL;
289 return ret;
292 /* Check if we can do the conversion direct */
293 #ifdef HAVE_NATIVE_ICONV
294 if (is_utf16(fromcode)) {
295 ret->direct = sys_iconv;
296 ret->cd_direct = ret->cd_push;
297 ret->cd_push = NULL;
298 return ret;
300 if (is_utf16(tocode)) {
301 ret->direct = sys_iconv;
302 ret->cd_direct = ret->cd_pull;
303 ret->cd_pull = NULL;
304 return ret;
306 #endif
308 return ret;
312 simple iconv_close() wrapper
314 int smb_iconv_close (smb_iconv_t cd)
316 #ifdef HAVE_NATIVE_ICONV
317 if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
318 if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
319 if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
320 #endif
322 SAFE_FREE(cd->from_name);
323 SAFE_FREE(cd->to_name);
325 memset(cd, 0, sizeof(*cd));
326 SAFE_FREE(cd);
327 return 0;
331 /**********************************************************************
332 the following functions implement the builtin character sets in Samba
333 and also the "test" character sets that are designed to test
334 multi-byte character set support for english users
335 ***********************************************************************/
337 static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
338 char **outbuf, size_t *outbytesleft)
340 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
341 (*outbuf)[0] = (*inbuf)[0];
342 (*outbuf)[1] = 0;
343 (*inbytesleft) -= 1;
344 (*outbytesleft) -= 2;
345 (*inbuf) += 1;
346 (*outbuf) += 2;
349 if (*inbytesleft > 0) {
350 errno = E2BIG;
351 return -1;
354 return 0;
357 static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
358 char **outbuf, size_t *outbytesleft)
360 int ir_count=0;
362 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
363 (*outbuf)[0] = (*inbuf)[0] & 0x7F;
364 if ((*inbuf)[1]) ir_count++;
365 (*inbytesleft) -= 2;
366 (*outbytesleft) -= 1;
367 (*inbuf) += 2;
368 (*outbuf) += 1;
371 if (*inbytesleft == 1) {
372 errno = EINVAL;
373 return -1;
376 if (*inbytesleft > 1) {
377 errno = E2BIG;
378 return -1;
381 return ir_count;
384 static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
385 char **outbuf, size_t *outbytesleft)
387 int ir_count=0;
389 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
390 (*outbuf)[0] = (*inbuf)[0];
391 if ((*inbuf)[1]) ir_count++;
392 (*inbytesleft) -= 2;
393 (*outbytesleft) -= 1;
394 (*inbuf) += 2;
395 (*outbuf) += 1;
398 if (*inbytesleft == 1) {
399 errno = EINVAL;
400 return -1;
403 if (*inbytesleft > 1) {
404 errno = E2BIG;
405 return -1;
408 return ir_count;
411 static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
412 char **outbuf, size_t *outbytesleft)
414 while (*inbytesleft >= 1 && *outbytesleft >= 2) {
415 unsigned v;
417 if ((*inbuf)[0] != '@') {
418 /* seven bit ascii case */
419 (*outbuf)[0] = (*inbuf)[0];
420 (*outbuf)[1] = 0;
421 (*inbytesleft) -= 1;
422 (*outbytesleft) -= 2;
423 (*inbuf) += 1;
424 (*outbuf) += 2;
425 continue;
427 /* it's a hex character */
428 if (*inbytesleft < 5) {
429 errno = EINVAL;
430 return -1;
433 if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
434 errno = EILSEQ;
435 return -1;
438 (*outbuf)[0] = v&0xff;
439 (*outbuf)[1] = v>>8;
440 (*inbytesleft) -= 5;
441 (*outbytesleft) -= 2;
442 (*inbuf) += 5;
443 (*outbuf) += 2;
446 if (*inbytesleft > 0) {
447 errno = E2BIG;
448 return -1;
451 return 0;
454 static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
455 char **outbuf, size_t *outbytesleft)
457 while (*inbytesleft >= 2 && *outbytesleft >= 1) {
458 char buf[6];
460 if ((*inbuf)[1] == 0 &&
461 ((*inbuf)[0] & 0x80) == 0 &&
462 (*inbuf)[0] != '@') {
463 (*outbuf)[0] = (*inbuf)[0];
464 (*inbytesleft) -= 2;
465 (*outbytesleft) -= 1;
466 (*inbuf) += 2;
467 (*outbuf) += 1;
468 continue;
470 if (*outbytesleft < 5) {
471 errno = E2BIG;
472 return -1;
474 snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
475 memcpy(*outbuf, buf, 5);
476 (*inbytesleft) -= 2;
477 (*outbytesleft) -= 5;
478 (*inbuf) += 2;
479 (*outbuf) += 5;
482 if (*inbytesleft == 1) {
483 errno = EINVAL;
484 return -1;
487 if (*inbytesleft > 1) {
488 errno = E2BIG;
489 return -1;
492 return 0;
495 static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
496 char **outbuf, size_t *outbytesleft)
498 int n;
500 n = MIN(*inbytesleft, *outbytesleft);
502 swab(*inbuf, *outbuf, (n&~1));
503 if (n&1) {
504 (*outbuf)[n-1] = 0;
507 (*inbytesleft) -= n;
508 (*outbytesleft) -= n;
509 (*inbuf) += n;
510 (*outbuf) += n;
512 if (*inbytesleft > 0) {
513 errno = E2BIG;
514 return -1;
517 return 0;
520 static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
521 char **outbuf, size_t *outbytesleft)
523 int n;
525 n = MIN(*inbytesleft, *outbytesleft);
527 memmove(*outbuf, *inbuf, n);
529 (*inbytesleft) -= n;
530 (*outbytesleft) -= n;
531 (*inbuf) += n;
532 (*outbuf) += n;
534 if (*inbytesleft > 0) {
535 errno = E2BIG;
536 return -1;
539 return 0;
542 static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
543 char **outbuf, size_t *outbytesleft)
545 size_t in_left=*inbytesleft, out_left=*outbytesleft;
546 const uint8 *c = (const uint8 *)*inbuf;
547 uint8 *uc = (uint8 *)*outbuf;
549 while (in_left >= 1 && out_left >= 2) {
550 if ((c[0] & 0x80) == 0) {
551 uc[0] = c[0];
552 uc[1] = 0;
553 c += 1;
554 in_left -= 1;
555 out_left -= 2;
556 uc += 2;
557 continue;
560 if ((c[0] & 0xe0) == 0xc0) {
561 if (in_left < 2 ||
562 (c[1] & 0xc0) != 0x80) {
563 errno = EILSEQ;
564 goto error;
566 uc[1] = (c[0]>>2) & 0x7;
567 uc[0] = (c[0]<<6) | (c[1]&0x3f);
568 c += 2;
569 in_left -= 2;
570 out_left -= 2;
571 uc += 2;
572 continue;
575 if ((c[0] & 0xf0) == 0xe0) {
576 if (in_left < 3 ||
577 (c[1] & 0xc0) != 0x80 ||
578 (c[2] & 0xc0) != 0x80) {
579 errno = EILSEQ;
580 goto error;
582 uc[1] = ((c[0]&0xF)<<4) | ((c[1]>>2)&0xF);
583 uc[0] = (c[1]<<6) | (c[2]&0x3f);
584 c += 3;
585 in_left -= 3;
586 out_left -= 2;
587 uc += 2;
588 continue;
591 if ((c[0] & 0xf8) == 0xf0) {
592 unsigned int codepoint;
593 if (in_left < 4 ||
594 (c[1] & 0xc0) != 0x80 ||
595 (c[2] & 0xc0) != 0x80 ||
596 (c[3] & 0xc0) != 0x80) {
597 errno = EILSEQ;
598 goto error;
600 codepoint =
601 (c[3]&0x3f) |
602 ((c[2]&0x3f)<<6) |
603 ((c[1]&0x3f)<<12) |
604 ((c[0]&0x7)<<18);
605 if (codepoint < 0x10000) {
606 /* accept UTF-8 characters that are not
607 minimally packed, but pack the result */
608 uc[0] = (codepoint & 0xFF);
609 uc[1] = (codepoint >> 8);
610 c += 4;
611 in_left -= 4;
612 out_left -= 2;
613 uc += 2;
614 continue;
617 codepoint -= 0x10000;
619 if (out_left < 4) {
620 errno = E2BIG;
621 goto error;
624 uc[0] = (codepoint>>10) & 0xFF;
625 uc[1] = (codepoint>>18) | 0xd8;
626 uc[2] = codepoint & 0xFF;
627 uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
628 c += 4;
629 in_left -= 4;
630 out_left -= 4;
631 uc += 4;
632 continue;
635 /* we don't handle 5 byte sequences */
636 errno = EINVAL;
637 goto error;
640 if (in_left > 0) {
641 errno = E2BIG;
642 goto error;
645 *inbytesleft = in_left;
646 *outbytesleft = out_left;
647 *inbuf = (char *)c;
648 *outbuf = (char *)uc;
649 return 0;
651 error:
652 *inbytesleft = in_left;
653 *outbytesleft = out_left;
654 *inbuf = (char *)c;
655 *outbuf = (char *)uc;
656 return -1;
659 static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
660 char **outbuf, size_t *outbytesleft)
662 size_t in_left=*inbytesleft, out_left=*outbytesleft;
663 uint8 *c = (uint8 *)*outbuf;
664 const uint8 *uc = (const uint8 *)*inbuf;
666 while (in_left >= 2 && out_left >= 1) {
667 unsigned int codepoint;
669 if (uc[1] == 0 && !(uc[0] & 0x80)) {
670 /* simplest case */
671 c[0] = uc[0];
672 in_left -= 2;
673 out_left -= 1;
674 uc += 2;
675 c += 1;
676 continue;
679 if ((uc[1]&0xf8) == 0) {
680 /* next simplest case */
681 if (out_left < 2) {
682 errno = E2BIG;
683 goto error;
685 c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
686 c[1] = 0x80 | (uc[0] & 0x3f);
687 in_left -= 2;
688 out_left -= 2;
689 uc += 2;
690 c += 2;
691 continue;
694 if ((uc[1] & 0xfc) == 0xdc) {
695 /* its the second part of a 4 byte sequence. Illegal */
696 if (in_left < 4) {
697 errno = EINVAL;
698 } else {
699 errno = EILSEQ;
701 goto error;
704 if ((uc[1] & 0xfc) != 0xd8) {
705 codepoint = uc[0] | (uc[1]<<8);
706 if (out_left < 3) {
707 errno = E2BIG;
708 goto error;
710 c[0] = 0xe0 | (codepoint >> 12);
711 c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
712 c[2] = 0x80 | (codepoint & 0x3f);
714 in_left -= 2;
715 out_left -= 3;
716 uc += 2;
717 c += 3;
718 continue;
721 /* its the first part of a 4 byte sequence */
722 if (in_left < 4) {
723 errno = EINVAL;
724 goto error;
726 if ((uc[3] & 0xfc) != 0xdc) {
727 errno = EILSEQ;
728 goto error;
730 codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
731 (uc[0]<<10) | ((uc[1] & 0x3)<<18));
733 if (out_left < 4) {
734 errno = E2BIG;
735 goto error;
737 c[0] = 0xf0 | (codepoint >> 18);
738 c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
739 c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
740 c[3] = 0x80 | (codepoint & 0x3f);
742 in_left -= 4;
743 out_left -= 4;
744 uc += 4;
745 c += 4;
748 if (in_left == 1) {
749 errno = EINVAL;
750 goto error;
753 if (in_left > 1) {
754 errno = E2BIG;
755 goto error;
758 *inbytesleft = in_left;
759 *outbytesleft = out_left;
760 *inbuf = (char *)uc;
761 *outbuf = (char *)c;
763 return 0;
765 error:
766 *inbytesleft = in_left;
767 *outbytesleft = out_left;
768 *inbuf = (char *)uc;
769 *outbuf = (char *)c;
770 return -1;