r17972: revert accidental commit to ads_verify_ticket()
[Samba.git] / source / lib / charcnv.c
blobfffdf010a058c65e5005cf5ada51c9aab8411970
1 /*
2 Unix SMB/CIFS implementation.
3 Character set conversion Extensions
4 Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
5 Copyright (C) Andrew Tridgell 2001
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Martin Pool 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 /* We can parameterize this if someone complains.... JRA. */
28 char lp_failed_convert_char(void)
30 return '_';
33 /**
34 * @file
36 * @brief Character-set conversion routines built on our iconv.
38 * @note Samba's internal character set (at least in the 3.0 series)
39 * is always the same as the one for the Unix filesystem. It is
40 * <b>not</b> necessarily UTF-8 and may be different on machines that
41 * need i18n filenames to be compatible with Unix software. It does
42 * have to be a superset of ASCII. All multibyte sequences must start
43 * with a byte with the high bit set.
45 * @sa lib/iconv.c
49 static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
50 static BOOL conv_silent; /* Should we do a debug if the conversion fails ? */
52 /**
53 * Return the name of a charset to give to iconv().
54 **/
55 static const char *charset_name(charset_t ch)
57 const char *ret = NULL;
59 if (ch == CH_UCS2) ret = "UTF-16LE";
60 else if (ch == CH_UNIX) ret = lp_unix_charset();
61 else if (ch == CH_DOS) ret = lp_dos_charset();
62 else if (ch == CH_DISPLAY) ret = lp_display_charset();
63 else if (ch == CH_UTF8) ret = "UTF8";
65 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
66 if (ret && !strcmp(ret, "LOCALE")) {
67 const char *ln = NULL;
69 #ifdef HAVE_SETLOCALE
70 setlocale(LC_ALL, "");
71 #endif
72 ln = nl_langinfo(CODESET);
73 if (ln) {
74 /* Check whether the charset name is supported
75 by iconv */
76 smb_iconv_t handle = smb_iconv_open(ln,"UCS-2LE");
77 if (handle == (smb_iconv_t) -1) {
78 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln));
79 ln = NULL;
80 } else {
81 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln));
82 smb_iconv_close(handle);
85 ret = ln;
87 #endif
89 if (!ret || !*ret) ret = "ASCII";
90 return ret;
93 void lazy_initialize_conv(void)
95 static int initialized = False;
97 if (!initialized) {
98 initialized = True;
99 load_case_tables();
100 init_iconv();
105 * Destroy global objects allocated by init_iconv()
107 void gfree_charcnv(void)
109 int c1, c2;
111 for (c1=0;c1<NUM_CHARSETS;c1++) {
112 for (c2=0;c2<NUM_CHARSETS;c2++) {
113 if ( conv_handles[c1][c2] ) {
114 smb_iconv_close( conv_handles[c1][c2] );
115 conv_handles[c1][c2] = 0;
122 * Initialize iconv conversion descriptors.
124 * This is called the first time it is needed, and also called again
125 * every time the configuration is reloaded, because the charset or
126 * codepage might have changed.
128 void init_iconv(void)
130 int c1, c2;
131 BOOL did_reload = False;
133 /* so that charset_name() works we need to get the UNIX<->UCS2 going
134 first */
135 if (!conv_handles[CH_UNIX][CH_UCS2])
136 conv_handles[CH_UNIX][CH_UCS2] = smb_iconv_open(charset_name(CH_UCS2), "ASCII");
138 if (!conv_handles[CH_UCS2][CH_UNIX])
139 conv_handles[CH_UCS2][CH_UNIX] = smb_iconv_open("ASCII", charset_name(CH_UCS2));
141 for (c1=0;c1<NUM_CHARSETS;c1++) {
142 for (c2=0;c2<NUM_CHARSETS;c2++) {
143 const char *n1 = charset_name((charset_t)c1);
144 const char *n2 = charset_name((charset_t)c2);
145 if (conv_handles[c1][c2] &&
146 strcmp(n1, conv_handles[c1][c2]->from_name) == 0 &&
147 strcmp(n2, conv_handles[c1][c2]->to_name) == 0)
148 continue;
150 did_reload = True;
152 if (conv_handles[c1][c2])
153 smb_iconv_close(conv_handles[c1][c2]);
155 conv_handles[c1][c2] = smb_iconv_open(n2,n1);
156 if (conv_handles[c1][c2] == (smb_iconv_t)-1) {
157 DEBUG(0,("init_iconv: Conversion from %s to %s not supported\n",
158 charset_name((charset_t)c1), charset_name((charset_t)c2)));
159 if (c1 != CH_UCS2) {
160 n1 = "ASCII";
162 if (c2 != CH_UCS2) {
163 n2 = "ASCII";
165 DEBUG(0,("init_iconv: Attempting to replace with conversion from %s to %s\n",
166 n1, n2 ));
167 conv_handles[c1][c2] = smb_iconv_open(n2,n1);
168 if (!conv_handles[c1][c2]) {
169 DEBUG(0,("init_iconv: Conversion from %s to %s failed", n1, n2));
170 smb_panic("init_iconv: conv_handle initialization failed.");
176 if (did_reload) {
177 /* XXX: Does this really get called every time the dos
178 * codepage changes? */
179 /* XXX: Is the did_reload test too strict? */
180 conv_silent = True;
181 init_doschar_table();
182 init_valid_table();
183 conv_silent = False;
188 * Convert string from one encoding to another, making error checking etc
189 * Slow path version - uses (slow) iconv.
191 * @param src pointer to source string (multibyte or singlebyte)
192 * @param srclen length of the source string in bytes
193 * @param dest pointer to destination string (multibyte or singlebyte)
194 * @param destlen maximal length allowed for string
195 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
196 * @returns the number of bytes occupied in the destination
198 * Ensure the srclen contains the terminating zero.
202 static size_t convert_string_internal(charset_t from, charset_t to,
203 void const *src, size_t srclen,
204 void *dest, size_t destlen, BOOL allow_bad_conv)
206 size_t i_len, o_len;
207 size_t retval;
208 const char* inbuf = (const char*)src;
209 char* outbuf = (char*)dest;
210 smb_iconv_t descriptor;
212 lazy_initialize_conv();
214 descriptor = conv_handles[from][to];
216 if (srclen == (size_t)-1) {
217 if (from == CH_UCS2) {
218 srclen = (strlen_w((const smb_ucs2_t *)src)+1) * 2;
219 } else {
220 srclen = strlen((const char *)src)+1;
225 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
226 if (!conv_silent)
227 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
228 return (size_t)-1;
231 i_len=srclen;
232 o_len=destlen;
234 again:
236 retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
237 if(retval==(size_t)-1) {
238 const char *reason="unknown error";
239 switch(errno) {
240 case EINVAL:
241 reason="Incomplete multibyte sequence";
242 if (!conv_silent)
243 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
244 if (allow_bad_conv)
245 goto use_as_is;
246 break;
247 case E2BIG:
248 reason="No more room";
249 if (!conv_silent) {
250 if (from == CH_UNIX) {
251 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
252 charset_name(from), charset_name(to),
253 (unsigned int)srclen, (unsigned int)destlen, (const char *)src));
254 } else {
255 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
256 charset_name(from), charset_name(to),
257 (unsigned int)srclen, (unsigned int)destlen));
260 break;
261 case EILSEQ:
262 reason="Illegal multibyte sequence";
263 if (!conv_silent)
264 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
265 if (allow_bad_conv)
266 goto use_as_is;
267 break;
268 default:
269 if (!conv_silent)
270 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
271 break;
273 /* smb_panic(reason); */
275 return destlen-o_len;
277 use_as_is:
280 * Conversion not supported. This is actually an error, but there are so
281 * many misconfigured iconv systems and smb.conf's out there we can't just
282 * fail. Do a very bad conversion instead.... JRA.
286 if (o_len == 0 || i_len == 0)
287 return destlen - o_len;
289 if (from == CH_UCS2 && to != CH_UCS2) {
290 /* Can't convert from ucs2 to multibyte. Replace with the default fail char. */
291 if (i_len < 2)
292 return destlen - o_len;
293 if (i_len >= 2) {
294 *outbuf = lp_failed_convert_char();
296 outbuf++;
297 o_len--;
299 inbuf += 2;
300 i_len -= 2;
303 if (o_len == 0 || i_len == 0)
304 return destlen - o_len;
306 /* Keep trying with the next char... */
307 goto again;
309 } else if (from != CH_UCS2 && to == CH_UCS2) {
310 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
311 if (o_len < 2)
312 return destlen - o_len;
314 outbuf[0] = lp_failed_convert_char();
315 outbuf[1] = '\0';
317 inbuf++;
318 i_len--;
320 outbuf += 2;
321 o_len -= 2;
323 if (o_len == 0 || i_len == 0)
324 return destlen - o_len;
326 /* Keep trying with the next char... */
327 goto again;
329 } else if (from != CH_UCS2 && to != CH_UCS2) {
330 /* Failed multibyte to multibyte. Just copy the default fail char and
331 try again. */
332 outbuf[0] = lp_failed_convert_char();
334 inbuf++;
335 i_len--;
337 outbuf++;
338 o_len--;
340 if (o_len == 0 || i_len == 0)
341 return destlen - o_len;
343 /* Keep trying with the next char... */
344 goto again;
346 } else {
347 /* Keep compiler happy.... */
348 return destlen - o_len;
354 * Convert string from one encoding to another, making error checking etc
355 * Fast path version - handles ASCII first.
357 * @param src pointer to source string (multibyte or singlebyte)
358 * @param srclen length of the source string in bytes, or -1 for nul terminated.
359 * @param dest pointer to destination string (multibyte or singlebyte)
360 * @param destlen maximal length allowed for string - *NEVER* -1.
361 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
362 * @returns the number of bytes occupied in the destination
364 * Ensure the srclen contains the terminating zero.
366 * This function has been hand-tuned to provide a fast path.
367 * Don't change unless you really know what you are doing. JRA.
370 size_t convert_string(charset_t from, charset_t to,
371 void const *src, size_t srclen,
372 void *dest, size_t destlen, BOOL allow_bad_conv)
375 * NB. We deliberately don't do a strlen here if srclen == -1.
376 * This is very expensive over millions of calls and is taken
377 * care of in the slow path in convert_string_internal. JRA.
380 #ifdef DEVELOPER
381 SMB_ASSERT(destlen != (size_t)-1);
382 #endif
384 if (srclen == 0)
385 return 0;
387 if (from != CH_UCS2 && to != CH_UCS2) {
388 const unsigned char *p = (const unsigned char *)src;
389 unsigned char *q = (unsigned char *)dest;
390 size_t slen = srclen;
391 size_t dlen = destlen;
392 unsigned char lastp = '\0';
393 size_t retval = 0;
395 /* If all characters are ascii, fast path here. */
396 while (slen && dlen) {
397 if ((lastp = *p) <= 0x7f) {
398 *q++ = *p++;
399 if (slen != (size_t)-1) {
400 slen--;
402 dlen--;
403 retval++;
404 if (!lastp)
405 break;
406 } else {
407 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
408 goto general_case;
409 #else
410 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
411 #endif
414 if (!dlen) {
415 /* Even if we fast path we should note if we ran out of room. */
416 if (((slen != (size_t)-1) && slen) ||
417 ((slen == (size_t)-1) && lastp)) {
418 errno = E2BIG;
421 return retval;
422 } else if (from == CH_UCS2 && to != CH_UCS2) {
423 const unsigned char *p = (const unsigned char *)src;
424 unsigned char *q = (unsigned char *)dest;
425 size_t retval = 0;
426 size_t slen = srclen;
427 size_t dlen = destlen;
428 unsigned char lastp = '\0';
430 /* If all characters are ascii, fast path here. */
431 while (((slen == (size_t)-1) || (slen >= 2)) && dlen) {
432 if (((lastp = *p) <= 0x7f) && (p[1] == 0)) {
433 *q++ = *p;
434 if (slen != (size_t)-1) {
435 slen -= 2;
437 p += 2;
438 dlen--;
439 retval++;
440 if (!lastp)
441 break;
442 } else {
443 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
444 goto general_case;
445 #else
446 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
447 #endif
450 if (!dlen) {
451 /* Even if we fast path we should note if we ran out of room. */
452 if (((slen != (size_t)-1) && slen) ||
453 ((slen == (size_t)-1) && lastp)) {
454 errno = E2BIG;
457 return retval;
458 } else if (from != CH_UCS2 && to == CH_UCS2) {
459 const unsigned char *p = (const unsigned char *)src;
460 unsigned char *q = (unsigned char *)dest;
461 size_t retval = 0;
462 size_t slen = srclen;
463 size_t dlen = destlen;
464 unsigned char lastp = '\0';
466 /* If all characters are ascii, fast path here. */
467 while (slen && (dlen >= 2)) {
468 if ((lastp = *p) <= 0x7F) {
469 *q++ = *p++;
470 *q++ = '\0';
471 if (slen != (size_t)-1) {
472 slen--;
474 dlen -= 2;
475 retval += 2;
476 if (!lastp)
477 break;
478 } else {
479 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
480 goto general_case;
481 #else
482 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
483 #endif
486 if (!dlen) {
487 /* Even if we fast path we should note if we ran out of room. */
488 if (((slen != (size_t)-1) && slen) ||
489 ((slen == (size_t)-1) && lastp)) {
490 errno = E2BIG;
493 return retval;
496 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
497 general_case:
498 #endif
499 return convert_string_internal(from, to, src, srclen, dest, destlen, allow_bad_conv);
503 * Convert between character sets, allocating a new buffer for the result.
505 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
506 * @param srclen length of source buffer.
507 * @param dest always set at least to NULL
508 * @note -1 is not accepted for srclen.
510 * @returns Size in bytes of the converted string; or -1 in case of error.
512 * Ensure the srclen contains the terminating zero.
514 * I hate the goto's in this function. It's embarressing.....
515 * There has to be a cleaner way to do this. JRA.
518 size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
519 void const *src, size_t srclen, void *dst, BOOL allow_bad_conv)
521 size_t i_len, o_len, destlen = MAX(srclen, 512);
522 size_t retval;
523 const char *inbuf = (const char *)src;
524 char *outbuf = NULL, *ob = NULL;
525 smb_iconv_t descriptor;
526 void **dest = (void **)dst;
528 *dest = NULL;
530 if (src == NULL || srclen == (size_t)-1)
531 return (size_t)-1;
532 if (srclen == 0)
533 return 0;
535 lazy_initialize_conv();
537 descriptor = conv_handles[from][to];
539 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
540 if (!conv_silent)
541 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
542 return (size_t)-1;
545 convert:
547 if ((destlen*2) < destlen) {
548 /* wrapped ! abort. */
549 if (!conv_silent)
550 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
551 if (!ctx)
552 SAFE_FREE(outbuf);
553 return (size_t)-1;
554 } else {
555 destlen = destlen * 2;
558 if (ctx) {
559 ob = (char *)TALLOC_REALLOC(ctx, ob, destlen);
560 } else {
561 ob = (char *)SMB_REALLOC(ob, destlen);
564 if (!ob) {
565 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
566 return (size_t)-1;
568 outbuf = ob;
569 i_len = srclen;
570 o_len = destlen;
572 again:
574 retval = smb_iconv(descriptor,
575 &inbuf, &i_len,
576 &outbuf, &o_len);
577 if(retval == (size_t)-1) {
578 const char *reason="unknown error";
579 switch(errno) {
580 case EINVAL:
581 reason="Incomplete multibyte sequence";
582 if (!conv_silent)
583 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
584 if (allow_bad_conv)
585 goto use_as_is;
586 break;
587 case E2BIG:
588 goto convert;
589 case EILSEQ:
590 reason="Illegal multibyte sequence";
591 if (!conv_silent)
592 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
593 if (allow_bad_conv)
594 goto use_as_is;
595 break;
597 if (!conv_silent)
598 DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
599 /* smb_panic(reason); */
600 return (size_t)-1;
603 out:
605 destlen = destlen - o_len;
606 if (ctx) {
607 ob = (char *)TALLOC_REALLOC(ctx,ob,destlen);
608 } else {
609 ob = (char *)SMB_REALLOC(ob,destlen);
612 if (destlen && !ob) {
613 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
614 return (size_t)-1;
617 *dest = ob;
618 return destlen;
620 use_as_is:
623 * Conversion not supported. This is actually an error, but there are so
624 * many misconfigured iconv systems and smb.conf's out there we can't just
625 * fail. Do a very bad conversion instead.... JRA.
629 if (o_len == 0 || i_len == 0)
630 goto out;
632 if (from == CH_UCS2 && to != CH_UCS2) {
633 /* Can't convert from ucs2 to multibyte. Just use the default fail char. */
634 if (i_len < 2)
635 goto out;
637 if (i_len >= 2) {
638 *outbuf = lp_failed_convert_char();
640 outbuf++;
641 o_len--;
643 inbuf += 2;
644 i_len -= 2;
647 if (o_len == 0 || i_len == 0)
648 goto out;
650 /* Keep trying with the next char... */
651 goto again;
653 } else if (from != CH_UCS2 && to == CH_UCS2) {
654 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
655 if (o_len < 2)
656 goto out;
658 outbuf[0] = lp_failed_convert_char();
659 outbuf[1] = '\0';
661 inbuf++;
662 i_len--;
664 outbuf += 2;
665 o_len -= 2;
667 if (o_len == 0 || i_len == 0)
668 goto out;
670 /* Keep trying with the next char... */
671 goto again;
673 } else if (from != CH_UCS2 && to != CH_UCS2) {
674 /* Failed multibyte to multibyte. Just copy the default fail char and
675 try again. */
676 outbuf[0] = lp_failed_convert_char();
678 inbuf++;
679 i_len--;
681 outbuf++;
682 o_len--;
684 if (o_len == 0 || i_len == 0)
685 goto out;
687 /* Keep trying with the next char... */
688 goto again;
690 } else {
691 /* Keep compiler happy.... */
692 goto out;
698 * Convert between character sets, allocating a new buffer using talloc for the result.
700 * @param srclen length of source buffer.
701 * @param dest always set at least to NULL
702 * @note -1 is not accepted for srclen.
704 * @returns Size in bytes of the converted string; or -1 in case of error.
706 size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
707 void const *src, size_t srclen, void *dst,
708 BOOL allow_bad_conv)
710 void **dest = (void **)dst;
711 size_t dest_len;
713 *dest = NULL;
714 dest_len=convert_string_allocate(ctx, from, to, src, srclen, dest, allow_bad_conv);
715 if (dest_len == (size_t)-1)
716 return (size_t)-1;
717 if (*dest == NULL)
718 return (size_t)-1;
719 return dest_len;
722 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
724 size_t size;
725 smb_ucs2_t *buffer;
727 size = push_ucs2_allocate(&buffer, src);
728 if (size == (size_t)-1) {
729 smb_panic("failed to create UCS2 buffer");
731 if (!strupper_w(buffer) && (dest == src)) {
732 free(buffer);
733 return srclen;
736 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen, True);
737 free(buffer);
738 return size;
742 strdup() a unix string to upper case.
743 Max size is pstring.
746 char *strdup_upper(const char *s)
748 pstring out_buffer;
749 const unsigned char *p = (const unsigned char *)s;
750 unsigned char *q = (unsigned char *)out_buffer;
752 /* this is quite a common operation, so we want it to be
753 fast. We optimise for the ascii case, knowing that all our
754 supported multi-byte character sets are ascii-compatible
755 (ie. they match for the first 128 chars) */
757 while (1) {
758 if (*p & 0x80)
759 break;
760 *q++ = toupper_ascii(*p);
761 if (!*p)
762 break;
763 p++;
764 if (p - ( const unsigned char *)s >= sizeof(pstring))
765 break;
768 if (*p) {
769 /* MB case. */
770 size_t size;
771 wpstring buffer;
772 size = convert_string(CH_UNIX, CH_UCS2, s, -1, buffer, sizeof(buffer), True);
773 if (size == (size_t)-1) {
774 return NULL;
777 strupper_w(buffer);
779 size = convert_string(CH_UCS2, CH_UNIX, buffer, -1, out_buffer, sizeof(out_buffer), True);
780 if (size == (size_t)-1) {
781 return NULL;
785 return SMB_STRDUP(out_buffer);
788 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
790 size_t size;
791 smb_ucs2_t *buffer = NULL;
793 size = convert_string_allocate(NULL, CH_UNIX, CH_UCS2, src, srclen,
794 (void **)(void *)&buffer, True);
795 if (size == (size_t)-1 || !buffer) {
796 smb_panic("failed to create UCS2 buffer");
798 if (!strlower_w(buffer) && (dest == src)) {
799 SAFE_FREE(buffer);
800 return srclen;
802 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen, True);
803 SAFE_FREE(buffer);
804 return size;
808 strdup() a unix string to lower case.
811 char *strdup_lower(const char *s)
813 size_t size;
814 smb_ucs2_t *buffer = NULL;
815 char *out_buffer;
817 size = push_ucs2_allocate(&buffer, s);
818 if (size == -1 || !buffer) {
819 return NULL;
822 strlower_w(buffer);
824 size = pull_ucs2_allocate(&out_buffer, buffer);
825 SAFE_FREE(buffer);
827 if (size == (size_t)-1) {
828 return NULL;
831 return out_buffer;
834 static size_t ucs2_align(const void *base_ptr, const void *p, int flags)
836 if (flags & (STR_NOALIGN|STR_ASCII))
837 return 0;
838 return PTR_DIFF(p, base_ptr) & 1;
843 * Copy a string from a char* unix src to a dos codepage string destination.
845 * @return the number of bytes occupied by the string in the destination.
847 * @param flags can include
848 * <dl>
849 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
850 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
851 * </dl>
853 * @param dest_len the maximum length in bytes allowed in the
854 * destination. If @p dest_len is -1 then no maximum is used.
856 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
858 size_t src_len = strlen(src);
859 pstring tmpbuf;
861 /* treat a pstring as "unlimited" length */
862 if (dest_len == (size_t)-1)
863 dest_len = sizeof(pstring);
865 if (flags & STR_UPPER) {
866 pstrcpy(tmpbuf, src);
867 strupper_m(tmpbuf);
868 src = tmpbuf;
871 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
872 src_len++;
874 return convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len, True);
877 size_t push_ascii_fstring(void *dest, const char *src)
879 return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
882 size_t push_ascii_pstring(void *dest, const char *src)
884 return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
887 /********************************************************************
888 Push an nstring - ensure null terminated. Written by
889 moriyama@miraclelinux.com (MORIYAMA Masayuki).
890 ********************************************************************/
892 size_t push_ascii_nstring(void *dest, const char *src)
894 size_t i, buffer_len, dest_len;
895 smb_ucs2_t *buffer;
897 conv_silent = True;
898 buffer_len = push_ucs2_allocate(&buffer, src);
899 if (buffer_len == (size_t)-1) {
900 smb_panic("failed to create UCS2 buffer");
903 /* We're using buffer_len below to count ucs2 characters, not bytes. */
904 buffer_len /= sizeof(smb_ucs2_t);
906 dest_len = 0;
907 for (i = 0; buffer[i] != 0 && (i < buffer_len); i++) {
908 unsigned char mb[10];
909 /* Convert one smb_ucs2_t character at a time. */
910 size_t mb_len = convert_string(CH_UCS2, CH_DOS, buffer+i, sizeof(smb_ucs2_t), mb, sizeof(mb), False);
911 if ((mb_len != (size_t)-1) && (dest_len + mb_len <= MAX_NETBIOSNAME_LEN - 1)) {
912 memcpy((char *)dest + dest_len, mb, mb_len);
913 dest_len += mb_len;
914 } else {
915 errno = E2BIG;
916 break;
919 ((char *)dest)[dest_len] = '\0';
921 SAFE_FREE(buffer);
922 conv_silent = False;
923 return dest_len;
927 * Copy a string from a dos codepage source to a unix char* destination.
929 * The resulting string in "dest" is always null terminated.
931 * @param flags can have:
932 * <dl>
933 * <dt>STR_TERMINATE</dt>
934 * <dd>STR_TERMINATE means the string in @p src
935 * is null terminated, and src_len is ignored.</dd>
936 * </dl>
938 * @param src_len is the length of the source area in bytes.
939 * @returns the number of bytes occupied by the string in @p src.
941 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
943 size_t ret;
945 if (dest_len == (size_t)-1)
946 dest_len = sizeof(pstring);
948 if (flags & STR_TERMINATE) {
949 if (src_len == (size_t)-1) {
950 src_len = strlen((const char *)src) + 1;
951 } else {
952 size_t len = strnlen((const char *)src, src_len);
953 if (len < src_len)
954 len++;
955 src_len = len;
959 ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len, True);
960 if (ret == (size_t)-1) {
961 dest_len = 0;
964 if (dest_len)
965 dest[MIN(ret, dest_len-1)] = 0;
966 else
967 dest[0] = 0;
969 return src_len;
972 size_t pull_ascii_pstring(char *dest, const void *src)
974 return pull_ascii(dest, src, sizeof(pstring), -1, STR_TERMINATE);
977 size_t pull_ascii_fstring(char *dest, const void *src)
979 return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
982 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
984 size_t pull_ascii_nstring(char *dest, size_t dest_len, const void *src)
986 return pull_ascii(dest, src, dest_len, sizeof(nstring)-1, STR_TERMINATE);
990 * Copy a string from a char* src to a unicode destination.
992 * @returns the number of bytes occupied by the string in the destination.
994 * @param flags can have:
996 * <dl>
997 * <dt>STR_TERMINATE <dd>means include the null termination.
998 * <dt>STR_UPPER <dd>means uppercase in the destination.
999 * <dt>STR_NOALIGN <dd>means don't do alignment.
1000 * </dl>
1002 * @param dest_len is the maximum length allowed in the
1003 * destination. If dest_len is -1 then no maxiumum is used.
1006 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
1008 size_t len=0;
1009 size_t src_len;
1010 size_t ret;
1012 /* treat a pstring as "unlimited" length */
1013 if (dest_len == (size_t)-1)
1014 dest_len = sizeof(pstring);
1016 if (flags & STR_TERMINATE)
1017 src_len = (size_t)-1;
1018 else
1019 src_len = strlen(src);
1021 if (ucs2_align(base_ptr, dest, flags)) {
1022 *(char *)dest = 0;
1023 dest = (void *)((char *)dest + 1);
1024 if (dest_len)
1025 dest_len--;
1026 len++;
1029 /* ucs2 is always a multiple of 2 bytes */
1030 dest_len &= ~1;
1032 ret = convert_string(CH_UNIX, CH_UCS2, src, src_len, dest, dest_len, True);
1033 if (ret == (size_t)-1) {
1034 return 0;
1037 len += ret;
1039 if (flags & STR_UPPER) {
1040 smb_ucs2_t *dest_ucs2 = (smb_ucs2_t *)dest;
1041 size_t i;
1042 for (i = 0; i < (dest_len / 2) && dest_ucs2[i]; i++) {
1043 smb_ucs2_t v = toupper_w(dest_ucs2[i]);
1044 if (v != dest_ucs2[i]) {
1045 dest_ucs2[i] = v;
1050 return len;
1055 * Copy a string from a unix char* src to a UCS2 destination,
1056 * allocating a buffer using talloc().
1058 * @param dest always set at least to NULL
1060 * @returns The number of bytes occupied by the string in the destination
1061 * or -1 in case of error.
1063 size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
1065 size_t src_len = strlen(src)+1;
1067 *dest = NULL;
1068 return convert_string_talloc(ctx, CH_UNIX, CH_UCS2, src, src_len, (void **)dest, True);
1073 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1075 * @param dest always set at least to NULL
1077 * @returns The number of bytes occupied by the string in the destination
1078 * or -1 in case of error.
1081 size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
1083 size_t src_len = strlen(src)+1;
1085 *dest = NULL;
1086 return convert_string_allocate(NULL, CH_UNIX, CH_UCS2, src, src_len, (void **)dest, True);
1090 Copy a string from a char* src to a UTF-8 destination.
1091 Return the number of bytes occupied by the string in the destination
1092 Flags can have:
1093 STR_TERMINATE means include the null termination
1094 STR_UPPER means uppercase in the destination
1095 dest_len is the maximum length allowed in the destination. If dest_len
1096 is -1 then no maxiumum is used.
1099 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
1101 size_t src_len = strlen(src);
1102 pstring tmpbuf;
1104 /* treat a pstring as "unlimited" length */
1105 if (dest_len == (size_t)-1)
1106 dest_len = sizeof(pstring);
1108 if (flags & STR_UPPER) {
1109 pstrcpy(tmpbuf, src);
1110 strupper_m(tmpbuf);
1111 src = tmpbuf;
1114 if (flags & STR_TERMINATE)
1115 src_len++;
1117 return convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len, True);
1120 size_t push_utf8_fstring(void *dest, const char *src)
1122 return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
1126 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1128 * @param dest always set at least to NULL
1130 * @returns The number of bytes occupied by the string in the destination
1133 size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1135 size_t src_len = strlen(src)+1;
1137 *dest = NULL;
1138 return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest, True);
1142 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1144 * @param dest always set at least to NULL
1146 * @returns The number of bytes occupied by the string in the destination
1149 size_t push_utf8_allocate(char **dest, const char *src)
1151 size_t src_len = strlen(src)+1;
1153 *dest = NULL;
1154 return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, True);
1158 Copy a string from a ucs2 source to a unix char* destination.
1159 Flags can have:
1160 STR_TERMINATE means the string in src is null terminated.
1161 STR_NOALIGN means don't try to align.
1162 if STR_TERMINATE is set then src_len is ignored if it is -1.
1163 src_len is the length of the source area in bytes
1164 Return the number of bytes occupied by the string in src.
1165 The resulting string in "dest" is always null terminated.
1168 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1170 size_t ret;
1172 if (dest_len == (size_t)-1)
1173 dest_len = sizeof(pstring);
1175 if (ucs2_align(base_ptr, src, flags)) {
1176 src = (const void *)((const char *)src + 1);
1177 if (src_len != (size_t)-1)
1178 src_len--;
1181 if (flags & STR_TERMINATE) {
1182 /* src_len -1 is the default for null terminated strings. */
1183 if (src_len != (size_t)-1) {
1184 size_t len = strnlen_w((const smb_ucs2_t *)src,
1185 src_len/2);
1186 if (len < src_len/2)
1187 len++;
1188 src_len = len*2;
1192 /* ucs2 is always a multiple of 2 bytes */
1193 if (src_len != (size_t)-1)
1194 src_len &= ~1;
1196 ret = convert_string(CH_UCS2, CH_UNIX, src, src_len, dest, dest_len, True);
1197 if (ret == (size_t)-1) {
1198 return 0;
1201 if (src_len == (size_t)-1)
1202 src_len = ret*2;
1204 if (dest_len)
1205 dest[MIN(ret, dest_len-1)] = 0;
1206 else
1207 dest[0] = 0;
1209 return src_len;
1212 size_t pull_ucs2_pstring(char *dest, const void *src)
1214 return pull_ucs2(NULL, dest, src, sizeof(pstring), -1, STR_TERMINATE);
1217 size_t pull_ucs2_fstring(char *dest, const void *src)
1219 return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
1223 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1225 * @param dest always set at least to NULL
1227 * @returns The number of bytes occupied by the string in the destination
1230 size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
1232 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1233 *dest = NULL;
1234 return convert_string_talloc(ctx, CH_UCS2, CH_UNIX, src, src_len, (void **)dest, True);
1238 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1240 * @param dest always set at least to NULL
1242 * @returns The number of bytes occupied by the string in the destination
1245 size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
1247 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1248 *dest = NULL;
1249 return convert_string_allocate(NULL, CH_UCS2, CH_UNIX, src, src_len, (void **)dest, True);
1253 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1255 * @param dest always set at least to NULL
1257 * @returns The number of bytes occupied by the string in the destination
1260 size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1262 size_t src_len = strlen(src)+1;
1263 *dest = NULL;
1264 return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1268 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1270 * @param dest always set at least to NULL
1272 * @returns The number of bytes occupied by the string in the destination
1275 size_t pull_utf8_allocate(char **dest, const char *src)
1277 size_t src_len = strlen(src)+1;
1278 *dest = NULL;
1279 return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1283 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1285 * @param dest always set at least to NULL
1287 * @returns The number of bytes occupied by the string in the destination
1290 size_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1292 size_t src_len = strlen(src)+1;
1293 *dest = NULL;
1294 return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest, True);
1298 Copy a string from a char* src to a unicode or ascii
1299 dos codepage destination choosing unicode or ascii based on the
1300 flags in the SMB buffer starting at base_ptr.
1301 Return the number of bytes occupied by the string in the destination.
1302 flags can have:
1303 STR_TERMINATE means include the null termination.
1304 STR_UPPER means uppercase in the destination.
1305 STR_ASCII use ascii even with unicode packet.
1306 STR_NOALIGN means don't do alignment.
1307 dest_len is the maximum length allowed in the destination. If dest_len
1308 is -1 then no maxiumum is used.
1311 size_t push_string_fn(const char *function, unsigned int line, const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
1313 #ifdef DEVELOPER
1314 /* We really need to zero fill here, not clobber
1315 * region, as we want to ensure that valgrind thinks
1316 * all of the outgoing buffer has been written to
1317 * so a send() or write() won't trap an error.
1318 * JRA.
1320 #if 0
1321 if (dest_len != (size_t)-1)
1322 clobber_region(function, line, dest, dest_len);
1323 #else
1324 if (dest_len != (size_t)-1)
1325 memset(dest, '\0', dest_len);
1326 #endif
1327 #endif
1329 if (!(flags & STR_ASCII) && \
1330 ((flags & STR_UNICODE || \
1331 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1332 return push_ucs2(base_ptr, dest, src, dest_len, flags);
1334 return push_ascii(dest, src, dest_len, flags);
1339 Copy a string from a unicode or ascii source (depending on
1340 the packet flags) to a char* destination.
1341 Flags can have:
1342 STR_TERMINATE means the string in src is null terminated.
1343 STR_UNICODE means to force as unicode.
1344 STR_ASCII use ascii even with unicode packet.
1345 STR_NOALIGN means don't do alignment.
1346 if STR_TERMINATE is set then src_len is ignored is it is -1
1347 src_len is the length of the source area in bytes.
1348 Return the number of bytes occupied by the string in src.
1349 The resulting string in "dest" is always null terminated.
1352 size_t pull_string_fn(const char *function, unsigned int line, const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1354 #ifdef DEVELOPER
1355 if (dest_len != (size_t)-1)
1356 clobber_region(function, line, dest, dest_len);
1357 #endif
1359 if (!(flags & STR_ASCII) && \
1360 ((flags & STR_UNICODE || \
1361 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1362 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
1364 return pull_ascii(dest, src, dest_len, src_len, flags);
1367 size_t align_string(const void *base_ptr, const char *p, int flags)
1369 if (!(flags & STR_ASCII) && \
1370 ((flags & STR_UNICODE || \
1371 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1372 return ucs2_align(base_ptr, p, flags);
1374 return 0;
1377 /****************************************************************
1378 Calculate the size (in bytes) of the next multibyte character in
1379 our internal character set. Note that p must be pointing to a
1380 valid mb char, not within one.
1381 ****************************************************************/
1383 size_t next_mb_char_size(const char *s)
1385 size_t i;
1387 if (!(*s & 0x80))
1388 return 1; /* ascii. */
1390 conv_silent = True;
1391 for ( i = 1; i <=4; i++ ) {
1392 smb_ucs2_t uc;
1393 if (convert_string(CH_UNIX, CH_UCS2, s, i, &uc, 2, False) == 2) {
1394 #if 0 /* JRATEST */
1395 DEBUG(10,("next_mb_char_size: size %u at string %s\n",
1396 (unsigned int)i, s));
1397 #endif
1398 conv_silent = False;
1399 return i;
1402 /* We're hosed - we don't know how big this is... */
1403 DEBUG(10,("next_mb_char_size: unknown size at string %s\n", s));
1404 conv_silent = False;
1405 return 1;