Zero out the out policy handler in lsa_Close
[Samba/gebeck_regimport.git] / source3 / lib / charcnv.c
blobd11620ecd776aac539be11a02dcbb376d84f6fd8
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 /* We can parameterize this if someone complains.... JRA. */
27 char lp_failed_convert_char(void)
29 return '_';
32 /**
33 * @file
35 * @brief Character-set conversion routines built on our iconv.
37 * @note Samba's internal character set (at least in the 3.0 series)
38 * is always the same as the one for the Unix filesystem. It is
39 * <b>not</b> necessarily UTF-8 and may be different on machines that
40 * need i18n filenames to be compatible with Unix software. It does
41 * have to be a superset of ASCII. All multibyte sequences must start
42 * with a byte with the high bit set.
44 * @sa lib/iconv.c
48 static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
49 static bool conv_silent; /* Should we do a debug if the conversion fails ? */
51 /**
52 * Return the name of a charset to give to iconv().
53 **/
54 static const char *charset_name(charset_t ch)
56 const char *ret = NULL;
58 if (ch == CH_UTF16LE) ret = "UTF-16LE";
59 else if (ch == CH_UTF16BE) ret = "UTF-16BE";
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_UTF16LE])
136 conv_handles[CH_UNIX][CH_UTF16LE] = smb_iconv_open(charset_name(CH_UTF16LE), "ASCII");
138 if (!conv_handles[CH_UTF16LE][CH_UNIX])
139 conv_handles[CH_UTF16LE][CH_UNIX] = smb_iconv_open("ASCII", charset_name(CH_UTF16LE));
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_UTF16LE && c1 != CH_UTF16BE) {
160 n1 = "ASCII";
162 if (c2 != CH_UTF16LE && c2 != CH_UTF16BE) {
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_valid_table();
182 conv_silent = False;
187 * Convert string from one encoding to another, making error checking etc
188 * Slow path version - uses (slow) iconv.
190 * @param src pointer to source string (multibyte or singlebyte)
191 * @param srclen length of the source string in bytes
192 * @param dest pointer to destination string (multibyte or singlebyte)
193 * @param destlen maximal length allowed for string
194 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
195 * @returns the number of bytes occupied in the destination
197 * Ensure the srclen contains the terminating zero.
201 static size_t convert_string_internal(charset_t from, charset_t to,
202 void const *src, size_t srclen,
203 void *dest, size_t destlen, bool allow_bad_conv)
205 size_t i_len, o_len;
206 size_t retval;
207 const char* inbuf = (const char*)src;
208 char* outbuf = (char*)dest;
209 smb_iconv_t descriptor;
211 lazy_initialize_conv();
213 descriptor = conv_handles[from][to];
215 if (srclen == (size_t)-1) {
216 if (from == CH_UTF16LE || from == CH_UTF16BE) {
217 srclen = (strlen_w((const smb_ucs2_t *)src)+1) * 2;
218 } else {
219 srclen = strlen((const char *)src)+1;
224 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
225 if (!conv_silent)
226 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
227 return (size_t)-1;
230 i_len=srclen;
231 o_len=destlen;
233 again:
235 retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
236 if(retval==(size_t)-1) {
237 const char *reason="unknown error";
238 switch(errno) {
239 case EINVAL:
240 reason="Incomplete multibyte sequence";
241 if (!conv_silent)
242 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
243 if (allow_bad_conv)
244 goto use_as_is;
245 break;
246 case E2BIG:
247 reason="No more room";
248 if (!conv_silent) {
249 if (from == CH_UNIX) {
250 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
251 charset_name(from), charset_name(to),
252 (unsigned int)srclen, (unsigned int)destlen, (const char *)src));
253 } else {
254 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
255 charset_name(from), charset_name(to),
256 (unsigned int)srclen, (unsigned int)destlen));
259 break;
260 case EILSEQ:
261 reason="Illegal multibyte sequence";
262 if (!conv_silent)
263 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
264 if (allow_bad_conv)
265 goto use_as_is;
266 break;
267 default:
268 if (!conv_silent)
269 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
270 break;
272 /* smb_panic(reason); */
274 return destlen-o_len;
276 use_as_is:
279 * Conversion not supported. This is actually an error, but there are so
280 * many misconfigured iconv systems and smb.conf's out there we can't just
281 * fail. Do a very bad conversion instead.... JRA.
285 if (o_len == 0 || i_len == 0)
286 return destlen - o_len;
288 if (((from == CH_UTF16LE)||(from == CH_UTF16BE)) &&
289 ((to != CH_UTF16LE)||(to != CH_UTF16BE))) {
290 /* Can't convert from utf16 any endian to multibyte.
291 Replace with the default fail char.
293 if (i_len < 2)
294 return destlen - o_len;
295 if (i_len >= 2) {
296 *outbuf = lp_failed_convert_char();
298 outbuf++;
299 o_len--;
301 inbuf += 2;
302 i_len -= 2;
305 if (o_len == 0 || i_len == 0)
306 return destlen - o_len;
308 /* Keep trying with the next char... */
309 goto again;
311 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
312 /* Can't convert to UTF16LE - just widen by adding the
313 default fail char then zero.
315 if (o_len < 2)
316 return destlen - o_len;
318 outbuf[0] = lp_failed_convert_char();
319 outbuf[1] = '\0';
321 inbuf++;
322 i_len--;
324 outbuf += 2;
325 o_len -= 2;
327 if (o_len == 0 || i_len == 0)
328 return destlen - o_len;
330 /* Keep trying with the next char... */
331 goto again;
333 } else if (from != CH_UTF16LE && from != CH_UTF16BE &&
334 to != CH_UTF16LE && to != CH_UTF16BE) {
335 /* Failed multibyte to multibyte. Just copy the default fail char and
336 try again. */
337 outbuf[0] = lp_failed_convert_char();
339 inbuf++;
340 i_len--;
342 outbuf++;
343 o_len--;
345 if (o_len == 0 || i_len == 0)
346 return destlen - o_len;
348 /* Keep trying with the next char... */
349 goto again;
351 } else {
352 /* Keep compiler happy.... */
353 return destlen - o_len;
359 * Convert string from one encoding to another, making error checking etc
360 * Fast path version - handles ASCII first.
362 * @param src pointer to source string (multibyte or singlebyte)
363 * @param srclen length of the source string in bytes, or -1 for nul terminated.
364 * @param dest pointer to destination string (multibyte or singlebyte)
365 * @param destlen maximal length allowed for string - *NEVER* -1.
366 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
367 * @returns the number of bytes occupied in the destination
369 * Ensure the srclen contains the terminating zero.
371 * This function has been hand-tuned to provide a fast path.
372 * Don't change unless you really know what you are doing. JRA.
375 size_t convert_string(charset_t from, charset_t to,
376 void const *src, size_t srclen,
377 void *dest, size_t destlen, bool allow_bad_conv)
380 * NB. We deliberately don't do a strlen here if srclen == -1.
381 * This is very expensive over millions of calls and is taken
382 * care of in the slow path in convert_string_internal. JRA.
385 #ifdef DEVELOPER
386 SMB_ASSERT(destlen != (size_t)-1);
387 #endif
389 if (srclen == 0)
390 return 0;
392 if (from != CH_UTF16LE && from != CH_UTF16BE && to != CH_UTF16LE && to != CH_UTF16BE) {
393 const unsigned char *p = (const unsigned char *)src;
394 unsigned char *q = (unsigned char *)dest;
395 size_t slen = srclen;
396 size_t dlen = destlen;
397 unsigned char lastp = '\0';
398 size_t retval = 0;
400 /* If all characters are ascii, fast path here. */
401 while (slen && dlen) {
402 if ((lastp = *p) <= 0x7f) {
403 *q++ = *p++;
404 if (slen != (size_t)-1) {
405 slen--;
407 dlen--;
408 retval++;
409 if (!lastp)
410 break;
411 } else {
412 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
413 goto general_case;
414 #else
415 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
416 #endif
419 if (!dlen) {
420 /* Even if we fast path we should note if we ran out of room. */
421 if (((slen != (size_t)-1) && slen) ||
422 ((slen == (size_t)-1) && lastp)) {
423 errno = E2BIG;
426 return retval;
427 } else if (from == CH_UTF16LE && to != CH_UTF16LE) {
428 const unsigned char *p = (const unsigned char *)src;
429 unsigned char *q = (unsigned char *)dest;
430 size_t retval = 0;
431 size_t slen = srclen;
432 size_t dlen = destlen;
433 unsigned char lastp = '\0';
435 /* If all characters are ascii, fast path here. */
436 while (((slen == (size_t)-1) || (slen >= 2)) && dlen) {
437 if (((lastp = *p) <= 0x7f) && (p[1] == 0)) {
438 *q++ = *p;
439 if (slen != (size_t)-1) {
440 slen -= 2;
442 p += 2;
443 dlen--;
444 retval++;
445 if (!lastp)
446 break;
447 } else {
448 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
449 goto general_case;
450 #else
451 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
452 #endif
455 if (!dlen) {
456 /* Even if we fast path we should note if we ran out of room. */
457 if (((slen != (size_t)-1) && slen) ||
458 ((slen == (size_t)-1) && lastp)) {
459 errno = E2BIG;
462 return retval;
463 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
464 const unsigned char *p = (const unsigned char *)src;
465 unsigned char *q = (unsigned char *)dest;
466 size_t retval = 0;
467 size_t slen = srclen;
468 size_t dlen = destlen;
469 unsigned char lastp = '\0';
471 /* If all characters are ascii, fast path here. */
472 while (slen && (dlen >= 2)) {
473 if ((lastp = *p) <= 0x7F) {
474 *q++ = *p++;
475 *q++ = '\0';
476 if (slen != (size_t)-1) {
477 slen--;
479 dlen -= 2;
480 retval += 2;
481 if (!lastp)
482 break;
483 } else {
484 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
485 goto general_case;
486 #else
487 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
488 #endif
491 if (!dlen) {
492 /* Even if we fast path we should note if we ran out of room. */
493 if (((slen != (size_t)-1) && slen) ||
494 ((slen == (size_t)-1) && lastp)) {
495 errno = E2BIG;
498 return retval;
501 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
502 general_case:
503 #endif
504 return convert_string_internal(from, to, src, srclen, dest, destlen, allow_bad_conv);
508 * Convert between character sets, allocating a new buffer for the result.
510 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
511 * (this is a bad interface and needs fixing. JRA).
512 * @param srclen length of source buffer.
513 * @param dest always set at least to NULL
514 * @note -1 is not accepted for srclen.
516 * @returns Size in bytes of the converted string; or -1 in case of error.
518 * Ensure the srclen contains the terminating zero.
520 * I hate the goto's in this function. It's embarressing.....
521 * There has to be a cleaner way to do this. JRA.
524 size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
525 void const *src, size_t srclen, void *dst, bool allow_bad_conv)
527 size_t i_len, o_len, destlen = (srclen * 3) / 2;
528 size_t retval;
529 const char *inbuf = (const char *)src;
530 char *outbuf = NULL, *ob = NULL;
531 smb_iconv_t descriptor;
532 void **dest = (void **)dst;
534 *dest = NULL;
536 if (src == NULL || srclen == (size_t)-1)
537 return (size_t)-1;
538 if (srclen == 0)
539 return 0;
541 lazy_initialize_conv();
543 descriptor = conv_handles[from][to];
545 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
546 if (!conv_silent)
547 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
548 return (size_t)-1;
551 convert:
553 /* +2 is for ucs2 null termination. */
554 if ((destlen*2)+2 < destlen) {
555 /* wrapped ! abort. */
556 if (!conv_silent)
557 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
558 if (!ctx)
559 SAFE_FREE(outbuf);
560 return (size_t)-1;
561 } else {
562 destlen = destlen * 2;
565 /* +2 is for ucs2 null termination. */
566 if (ctx) {
567 ob = (char *)TALLOC_REALLOC(ctx, ob, destlen + 2);
568 } else {
569 ob = (char *)SMB_REALLOC(ob, destlen + 2);
572 if (!ob) {
573 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
574 return (size_t)-1;
576 outbuf = ob;
577 i_len = srclen;
578 o_len = destlen;
580 again:
582 retval = smb_iconv(descriptor,
583 &inbuf, &i_len,
584 &outbuf, &o_len);
585 if(retval == (size_t)-1) {
586 const char *reason="unknown error";
587 switch(errno) {
588 case EINVAL:
589 reason="Incomplete multibyte sequence";
590 if (!conv_silent)
591 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
592 if (allow_bad_conv)
593 goto use_as_is;
594 break;
595 case E2BIG:
596 goto convert;
597 case EILSEQ:
598 reason="Illegal multibyte sequence";
599 if (!conv_silent)
600 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
601 if (allow_bad_conv)
602 goto use_as_is;
603 break;
605 if (!conv_silent)
606 DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
607 /* smb_panic(reason); */
608 if (ctx) {
609 TALLOC_FREE(ob);
610 } else {
611 SAFE_FREE(ob);
613 return (size_t)-1;
616 out:
618 destlen = destlen - o_len;
619 /* Don't shrink unless we're reclaiming a lot of
620 * space. This is in the hot codepath and these
621 * reallocs *cost*. JRA.
623 if (o_len > 1024) {
624 /* We're shrinking here so we know the +2 is safe from wrap. */
625 if (ctx) {
626 ob = (char *)TALLOC_REALLOC(ctx,ob,destlen + 2);
627 } else {
628 ob = (char *)SMB_REALLOC(ob,destlen + 2);
632 if (destlen && !ob) {
633 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
634 return (size_t)-1;
637 *dest = ob;
639 /* Must ucs2 null terminate in the extra space we allocated. */
640 ob[destlen] = '\0';
641 ob[destlen+1] = '\0';
643 return destlen;
645 use_as_is:
648 * Conversion not supported. This is actually an error, but there are so
649 * many misconfigured iconv systems and smb.conf's out there we can't just
650 * fail. Do a very bad conversion instead.... JRA.
654 if (o_len == 0 || i_len == 0)
655 goto out;
657 if (((from == CH_UTF16LE)||(from == CH_UTF16BE)) &&
658 ((to != CH_UTF16LE)||(to != CH_UTF16BE))) {
659 /* Can't convert from utf16 any endian to multibyte.
660 Replace with the default fail char.
663 if (i_len < 2)
664 goto out;
666 if (i_len >= 2) {
667 *outbuf = lp_failed_convert_char();
669 outbuf++;
670 o_len--;
672 inbuf += 2;
673 i_len -= 2;
676 if (o_len == 0 || i_len == 0)
677 goto out;
679 /* Keep trying with the next char... */
680 goto again;
682 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
683 /* Can't convert to UTF16LE - just widen by adding the
684 default fail char then zero.
686 if (o_len < 2)
687 goto out;
689 outbuf[0] = lp_failed_convert_char();
690 outbuf[1] = '\0';
692 inbuf++;
693 i_len--;
695 outbuf += 2;
696 o_len -= 2;
698 if (o_len == 0 || i_len == 0)
699 goto out;
701 /* Keep trying with the next char... */
702 goto again;
704 } else if (from != CH_UTF16LE && from != CH_UTF16BE &&
705 to != CH_UTF16LE && to != CH_UTF16BE) {
706 /* Failed multibyte to multibyte. Just copy the default fail char and
707 try again. */
708 outbuf[0] = lp_failed_convert_char();
710 inbuf++;
711 i_len--;
713 outbuf++;
714 o_len--;
716 if (o_len == 0 || i_len == 0)
717 goto out;
719 /* Keep trying with the next char... */
720 goto again;
722 } else {
723 /* Keep compiler happy.... */
724 goto out;
730 * Convert between character sets, allocating a new buffer using talloc for the result.
732 * @param srclen length of source buffer.
733 * @param dest always set at least to NULL
734 * @note -1 is not accepted for srclen.
736 * @returns Size in bytes of the converted string; or -1 in case of error.
738 size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
739 void const *src, size_t srclen, void *dst,
740 bool allow_bad_conv)
742 void **dest = (void **)dst;
743 size_t dest_len;
745 *dest = NULL;
746 dest_len=convert_string_allocate(ctx, from, to, src, srclen, dest, allow_bad_conv);
747 if (dest_len == (size_t)-1)
748 return (size_t)-1;
749 if (*dest == NULL)
750 return (size_t)-1;
751 return dest_len;
754 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
756 size_t size;
757 smb_ucs2_t *buffer;
759 size = push_ucs2_allocate(&buffer, src);
760 if (size == (size_t)-1) {
761 return (size_t)-1;
763 if (!strupper_w(buffer) && (dest == src)) {
764 free(buffer);
765 return srclen;
768 size = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, True);
769 free(buffer);
770 return size;
774 strdup() a unix string to upper case.
777 char *strdup_upper(const char *s)
779 char *out_buffer = SMB_STRDUP(s);
780 const unsigned char *p = (const unsigned char *)s;
781 unsigned char *q = (unsigned char *)out_buffer;
783 if (!q) {
784 return NULL;
787 /* this is quite a common operation, so we want it to be
788 fast. We optimise for the ascii case, knowing that all our
789 supported multi-byte character sets are ascii-compatible
790 (ie. they match for the first 128 chars) */
792 while (*p) {
793 if (*p & 0x80)
794 break;
795 *q++ = toupper_ascii_fast(*p);
796 p++;
799 if (*p) {
800 /* MB case. */
801 size_t size;
802 smb_ucs2_t *buffer = NULL;
804 SAFE_FREE(out_buffer);
805 size = convert_string_allocate(NULL,
806 CH_UNIX,
807 CH_UTF16LE,
809 strlen(s) + 1,
810 (void **)(void *)&buffer,
811 True);
812 if (size == (size_t)-1) {
813 return NULL;
816 strupper_w(buffer);
818 size = convert_string_allocate(NULL,
819 CH_UTF16LE,
820 CH_UNIX,
821 buffer,
822 size,
823 (void **)(void *)&out_buffer,
824 True);
826 /* Don't need the intermediate buffer
827 * anymore.
830 TALLOC_FREE(buffer);
831 if (size == (size_t)-1) {
832 return NULL;
836 return out_buffer;
840 talloc_strdup() a unix string to upper case.
843 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s)
845 char *out_buffer = talloc_strdup(ctx,s);
846 const unsigned char *p = (const unsigned char *)s;
847 unsigned char *q = (unsigned char *)out_buffer;
849 if (!q) {
850 return NULL;
853 /* this is quite a common operation, so we want it to be
854 fast. We optimise for the ascii case, knowing that all our
855 supported multi-byte character sets are ascii-compatible
856 (ie. they match for the first 128 chars) */
858 while (*p) {
859 if (*p & 0x80)
860 break;
861 *q++ = toupper_ascii_fast(*p);
862 p++;
865 if (*p) {
866 /* MB case. */
867 size_t size;
868 smb_ucs2_t *ubuf = NULL;
870 /* We're not using the ascii buffer above. */
871 TALLOC_FREE(out_buffer);
873 size = convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE,
874 s, strlen(s)+1,
875 (void *)&ubuf,
876 True);
877 if (size == (size_t)-1) {
878 return NULL;
881 strupper_w(ubuf);
883 size = convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX,
884 ubuf, size,
885 (void *)&out_buffer,
886 True);
888 /* Don't need the intermediate buffer
889 * anymore.
892 TALLOC_FREE(ubuf);
894 if (size == (size_t)-1) {
895 return NULL;
899 return out_buffer;
902 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
904 size_t size;
905 smb_ucs2_t *buffer = NULL;
907 size = convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, srclen,
908 (void **)(void *)&buffer, True);
909 if (size == (size_t)-1 || !buffer) {
910 smb_panic("failed to create UCS2 buffer");
912 if (!strlower_w(buffer) && (dest == src)) {
913 SAFE_FREE(buffer);
914 return srclen;
916 size = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, True);
917 SAFE_FREE(buffer);
918 return size;
922 strdup() a unix string to lower case.
925 char *strdup_lower(const char *s)
927 size_t size;
928 smb_ucs2_t *buffer = NULL;
929 char *out_buffer;
931 size = push_ucs2_allocate(&buffer, s);
932 if (size == -1 || !buffer) {
933 return NULL;
936 strlower_w(buffer);
938 size = pull_ucs2_allocate(&out_buffer, buffer);
939 SAFE_FREE(buffer);
941 if (size == (size_t)-1) {
942 return NULL;
945 return out_buffer;
948 size_t ucs2_align(const void *base_ptr, const void *p, int flags)
950 if (flags & (STR_NOALIGN|STR_ASCII))
951 return 0;
952 return PTR_DIFF(p, base_ptr) & 1;
957 * Copy a string from a char* unix src to a dos codepage string destination.
959 * @return the number of bytes occupied by the string in the destination.
961 * @param flags can include
962 * <dl>
963 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
964 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
965 * </dl>
967 * @param dest_len the maximum length in bytes allowed in the
968 * destination.
970 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
972 size_t src_len = strlen(src);
973 char *tmpbuf = NULL;
974 size_t ret;
976 /* No longer allow a length of -1. */
977 if (dest_len == (size_t)-1) {
978 smb_panic("push_ascii - dest_len == -1");
981 if (flags & STR_UPPER) {
982 tmpbuf = SMB_STRDUP(src);
983 if (!tmpbuf) {
984 smb_panic("malloc fail");
986 strupper_m(tmpbuf);
987 src = tmpbuf;
990 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
991 src_len++;
994 ret = convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len, True);
995 if (ret == (size_t)-1 &&
996 (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
997 && dest_len > 0) {
998 ((char *)dest)[0] = '\0';
1000 SAFE_FREE(tmpbuf);
1001 return ret;
1004 size_t push_ascii_fstring(void *dest, const char *src)
1006 return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
1009 /********************************************************************
1010 Push an nstring - ensure null terminated. Written by
1011 moriyama@miraclelinux.com (MORIYAMA Masayuki).
1012 ********************************************************************/
1014 size_t push_ascii_nstring(void *dest, const char *src)
1016 size_t i, buffer_len, dest_len;
1017 smb_ucs2_t *buffer;
1019 conv_silent = True;
1020 buffer_len = push_ucs2_allocate(&buffer, src);
1021 if (buffer_len == (size_t)-1) {
1022 smb_panic("failed to create UCS2 buffer");
1025 /* We're using buffer_len below to count ucs2 characters, not bytes. */
1026 buffer_len /= sizeof(smb_ucs2_t);
1028 dest_len = 0;
1029 for (i = 0; buffer[i] != 0 && (i < buffer_len); i++) {
1030 unsigned char mb[10];
1031 /* Convert one smb_ucs2_t character at a time. */
1032 size_t mb_len = convert_string(CH_UTF16LE, CH_DOS, buffer+i, sizeof(smb_ucs2_t), mb, sizeof(mb), False);
1033 if ((mb_len != (size_t)-1) && (dest_len + mb_len <= MAX_NETBIOSNAME_LEN - 1)) {
1034 memcpy((char *)dest + dest_len, mb, mb_len);
1035 dest_len += mb_len;
1036 } else {
1037 errno = E2BIG;
1038 break;
1041 ((char *)dest)[dest_len] = '\0';
1043 SAFE_FREE(buffer);
1044 conv_silent = False;
1045 return dest_len;
1048 /********************************************************************
1049 Push and malloc an ascii string. src and dest null terminated.
1050 ********************************************************************/
1052 size_t push_ascii_allocate(char **dest, const char *src)
1054 size_t src_len = strlen(src)+1;
1056 *dest = NULL;
1057 return convert_string_allocate(NULL, CH_UNIX, CH_DOS, src, src_len, (void **)dest, True);
1061 * Copy a string from a dos codepage source to a unix char* destination.
1063 * The resulting string in "dest" is always null terminated.
1065 * @param flags can have:
1066 * <dl>
1067 * <dt>STR_TERMINATE</dt>
1068 * <dd>STR_TERMINATE means the string in @p src
1069 * is null terminated, and src_len is ignored.</dd>
1070 * </dl>
1072 * @param src_len is the length of the source area in bytes.
1073 * @returns the number of bytes occupied by the string in @p src.
1075 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1077 size_t ret;
1079 if (dest_len == (size_t)-1) {
1080 /* No longer allow dest_len of -1. */
1081 smb_panic("pull_ascii - invalid dest_len of -1");
1084 if (flags & STR_TERMINATE) {
1085 if (src_len == (size_t)-1) {
1086 src_len = strlen((const char *)src) + 1;
1087 } else {
1088 size_t len = strnlen((const char *)src, src_len);
1089 if (len < src_len)
1090 len++;
1091 src_len = len;
1095 ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len, True);
1096 if (ret == (size_t)-1) {
1097 ret = 0;
1098 dest_len = 0;
1101 if (dest_len && ret) {
1102 /* Did we already process the terminating zero ? */
1103 if (dest[MIN(ret-1, dest_len-1)] != 0) {
1104 dest[MIN(ret, dest_len-1)] = 0;
1106 } else {
1107 dest[0] = 0;
1110 return src_len;
1114 * Copy a string from a dos codepage source to a unix char* destination.
1115 Talloc version.
1116 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1117 needs fixing. JRA).
1119 * The resulting string in "dest" is always null terminated.
1121 * @param flags can have:
1122 * <dl>
1123 * <dt>STR_TERMINATE</dt>
1124 * <dd>STR_TERMINATE means the string in @p src
1125 * is null terminated, and src_len is ignored.</dd>
1126 * </dl>
1128 * @param src_len is the length of the source area in bytes.
1129 * @returns the number of bytes occupied by the string in @p src.
1132 static size_t pull_ascii_base_talloc(TALLOC_CTX *ctx,
1133 char **ppdest,
1134 const void *src,
1135 size_t src_len,
1136 int flags)
1138 char *dest = NULL;
1139 size_t dest_len = 0;
1141 #ifdef DEVELOPER
1142 /* Ensure we never use the braindead "malloc" varient. */
1143 if (ctx == NULL) {
1144 smb_panic("NULL talloc CTX in pull_ascii_base_talloc\n");
1146 #endif
1148 *ppdest = NULL;
1150 if (flags & STR_TERMINATE) {
1151 if (src_len == (size_t)-1) {
1152 src_len = strlen((const char *)src) + 1;
1153 } else {
1154 size_t len = strnlen((const char *)src, src_len);
1155 if (len < src_len)
1156 len++;
1157 src_len = len;
1159 /* Ensure we don't use an insane length from the client. */
1160 if (src_len >= 1024*1024) {
1161 char *msg = talloc_asprintf(ctx,
1162 "Bad src length (%u) in "
1163 "pull_ascii_base_talloc",
1164 (unsigned int)src_len);
1165 smb_panic(msg);
1169 dest_len = convert_string_allocate(ctx,
1170 CH_DOS,
1171 CH_UNIX,
1172 src,
1173 src_len,
1174 &dest,
1175 True);
1177 if (dest_len == (size_t)-1) {
1178 dest_len = 0;
1181 if (dest_len && dest) {
1182 /* Did we already process the terminating zero ? */
1183 if (dest[dest_len-1] != 0) {
1184 dest[dest_len-1] = 0;
1186 } else if (dest) {
1187 dest[0] = 0;
1190 *ppdest = dest;
1191 return src_len;
1194 size_t pull_ascii_fstring(char *dest, const void *src)
1196 return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
1199 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1201 size_t pull_ascii_nstring(char *dest, size_t dest_len, const void *src)
1203 return pull_ascii(dest, src, dest_len, sizeof(nstring)-1, STR_TERMINATE);
1207 * Copy a string from a char* src to a unicode destination.
1209 * @returns the number of bytes occupied by the string in the destination.
1211 * @param flags can have:
1213 * <dl>
1214 * <dt>STR_TERMINATE <dd>means include the null termination.
1215 * <dt>STR_UPPER <dd>means uppercase in the destination.
1216 * <dt>STR_NOALIGN <dd>means don't do alignment.
1217 * </dl>
1219 * @param dest_len is the maximum length allowed in the
1220 * destination.
1223 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
1225 size_t len=0;
1226 size_t src_len;
1227 size_t ret;
1229 if (dest_len == (size_t)-1) {
1230 /* No longer allow dest_len of -1. */
1231 smb_panic("push_ucs2 - invalid dest_len of -1");
1234 if (flags & STR_TERMINATE)
1235 src_len = (size_t)-1;
1236 else
1237 src_len = strlen(src);
1239 if (ucs2_align(base_ptr, dest, flags)) {
1240 *(char *)dest = 0;
1241 dest = (void *)((char *)dest + 1);
1242 if (dest_len)
1243 dest_len--;
1244 len++;
1247 /* ucs2 is always a multiple of 2 bytes */
1248 dest_len &= ~1;
1250 ret = convert_string(CH_UNIX, CH_UTF16LE, src, src_len, dest, dest_len, True);
1251 if (ret == (size_t)-1) {
1252 if ((flags & STR_TERMINATE) &&
1253 dest &&
1254 dest_len) {
1255 *(char *)dest = 0;
1257 return len;
1260 len += ret;
1262 if (flags & STR_UPPER) {
1263 smb_ucs2_t *dest_ucs2 = (smb_ucs2_t *)dest;
1264 size_t i;
1266 /* We check for i < (ret / 2) below as the dest string isn't null
1267 terminated if STR_TERMINATE isn't set. */
1269 for (i = 0; i < (ret / 2) && i < (dest_len / 2) && dest_ucs2[i]; i++) {
1270 smb_ucs2_t v = toupper_w(dest_ucs2[i]);
1271 if (v != dest_ucs2[i]) {
1272 dest_ucs2[i] = v;
1277 return len;
1282 * Copy a string from a unix char* src to a UCS2 destination,
1283 * 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
1288 * or -1 in case of error.
1290 size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
1292 size_t src_len = strlen(src)+1;
1294 *dest = NULL;
1295 return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len, (void **)dest, True);
1300 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1302 * @param dest always set at least to NULL
1304 * @returns The number of bytes occupied by the string in the destination
1305 * or -1 in case of error.
1308 size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
1310 size_t src_len = strlen(src)+1;
1312 *dest = NULL;
1313 return convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, src_len, (void **)dest, True);
1317 Copy a string from a char* src to a UTF-8 destination.
1318 Return the number of bytes occupied by the string in the destination
1319 Flags can have:
1320 STR_TERMINATE means include the null termination
1321 STR_UPPER means uppercase in the destination
1322 dest_len is the maximum length allowed in the destination. If dest_len
1323 is -1 then no maxiumum is used.
1326 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
1328 size_t src_len = 0;
1329 size_t ret;
1330 char *tmpbuf = NULL;
1332 if (dest_len == (size_t)-1) {
1333 /* No longer allow dest_len of -1. */
1334 smb_panic("push_utf8 - invalid dest_len of -1");
1337 if (flags & STR_UPPER) {
1338 tmpbuf = strdup_upper(src);
1339 if (!tmpbuf) {
1340 return (size_t)-1;
1342 src = tmpbuf;
1343 src_len = strlen(src);
1346 src_len = strlen(src);
1347 if (flags & STR_TERMINATE) {
1348 src_len++;
1351 ret = convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len, True);
1352 SAFE_FREE(tmpbuf);
1353 return ret;
1356 size_t push_utf8_fstring(void *dest, const char *src)
1358 return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
1362 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1364 * @param dest always set at least to NULL
1366 * @returns The number of bytes occupied by the string in the destination
1369 size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1371 size_t src_len = strlen(src)+1;
1373 *dest = NULL;
1374 return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest, True);
1378 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1380 * @param dest always set at least to NULL
1382 * @returns The number of bytes occupied by the string in the destination
1385 size_t push_utf8_allocate(char **dest, const char *src)
1387 size_t src_len = strlen(src)+1;
1389 *dest = NULL;
1390 return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, True);
1394 Copy a string from a ucs2 source to a unix char* destination.
1395 Flags can have:
1396 STR_TERMINATE means the string in src is null terminated.
1397 STR_NOALIGN means don't try to align.
1398 if STR_TERMINATE is set then src_len is ignored if it is -1.
1399 src_len is the length of the source area in bytes
1400 Return the number of bytes occupied by the string in src.
1401 The resulting string in "dest" is always null terminated.
1404 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1406 size_t ret;
1408 if (dest_len == (size_t)-1) {
1409 /* No longer allow dest_len of -1. */
1410 smb_panic("pull_ucs2 - invalid dest_len of -1");
1413 if (!src_len) {
1414 if (dest && dest_len > 0) {
1415 dest[0] = '\0';
1417 return 0;
1420 if (ucs2_align(base_ptr, src, flags)) {
1421 src = (const void *)((const char *)src + 1);
1422 if (src_len != (size_t)-1)
1423 src_len--;
1426 if (flags & STR_TERMINATE) {
1427 /* src_len -1 is the default for null terminated strings. */
1428 if (src_len != (size_t)-1) {
1429 size_t len = strnlen_w((const smb_ucs2_t *)src,
1430 src_len/2);
1431 if (len < src_len/2)
1432 len++;
1433 src_len = len*2;
1437 /* ucs2 is always a multiple of 2 bytes */
1438 if (src_len != (size_t)-1)
1439 src_len &= ~1;
1441 ret = convert_string(CH_UTF16LE, CH_UNIX, src, src_len, dest, dest_len, True);
1442 if (ret == (size_t)-1) {
1443 ret = 0;
1444 dest_len = 0;
1447 if (src_len == (size_t)-1)
1448 src_len = ret*2;
1450 if (dest_len && ret) {
1451 /* Did we already process the terminating zero ? */
1452 if (dest[MIN(ret-1, dest_len-1)] != 0) {
1453 dest[MIN(ret, dest_len-1)] = 0;
1455 } else {
1456 dest[0] = 0;
1459 return src_len;
1463 Copy a string from a ucs2 source to a unix char* destination.
1464 Talloc version with a base pointer.
1465 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1466 needs fixing. JRA).
1467 Flags can have:
1468 STR_TERMINATE means the string in src is null terminated.
1469 STR_NOALIGN means don't try to align.
1470 if STR_TERMINATE is set then src_len is ignored if it is -1.
1471 src_len is the length of the source area in bytes
1472 Return the number of bytes occupied by the string in src.
1473 The resulting string in "dest" is always null terminated.
1476 size_t pull_ucs2_base_talloc(TALLOC_CTX *ctx,
1477 const void *base_ptr,
1478 char **ppdest,
1479 const void *src,
1480 size_t src_len,
1481 int flags)
1483 char *dest;
1484 size_t dest_len;
1486 *ppdest = NULL;
1488 #ifdef DEVELOPER
1489 /* Ensure we never use the braindead "malloc" varient. */
1490 if (ctx == NULL) {
1491 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
1493 #endif
1495 if (!src_len) {
1496 return 0;
1499 if (ucs2_align(base_ptr, src, flags)) {
1500 src = (const void *)((const char *)src + 1);
1501 if (src_len != (size_t)-1)
1502 src_len--;
1505 if (flags & STR_TERMINATE) {
1506 /* src_len -1 is the default for null terminated strings. */
1507 if (src_len != (size_t)-1) {
1508 size_t len = strnlen_w((const smb_ucs2_t *)src,
1509 src_len/2);
1510 if (len < src_len/2)
1511 len++;
1512 src_len = len*2;
1513 } else {
1515 * src_len == -1 - alloc interface won't take this
1516 * so we must calculate.
1518 src_len = (strlen_w((const smb_ucs2_t *)src)+1)*sizeof(smb_ucs2_t);
1520 /* Ensure we don't use an insane length from the client. */
1521 if (src_len >= 1024*1024) {
1522 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1526 /* ucs2 is always a multiple of 2 bytes */
1527 if (src_len != (size_t)-1) {
1528 src_len &= ~1;
1531 dest_len = convert_string_talloc(ctx,
1532 CH_UTF16LE,
1533 CH_UNIX,
1534 src,
1535 src_len,
1536 (void *)&dest,
1537 True);
1538 if (dest_len == (size_t)-1) {
1539 dest_len = 0;
1542 if (src_len == (size_t)-1)
1543 src_len = dest_len*2;
1545 if (dest_len) {
1546 /* Did we already process the terminating zero ? */
1547 if (dest[dest_len-1] != 0) {
1548 size_t size = talloc_get_size(dest);
1549 /* Have we got space to append the '\0' ? */
1550 if (size <= dest_len) {
1551 /* No, realloc. */
1552 dest = TALLOC_REALLOC_ARRAY(ctx, dest, char,
1553 dest_len+1);
1554 if (!dest) {
1555 /* talloc fail. */
1556 dest_len = (size_t)-1;
1557 return 0;
1560 /* Yay - space ! */
1561 dest[dest_len] = '\0';
1562 dest_len++;
1564 } else if (dest) {
1565 dest[0] = 0;
1568 *ppdest = dest;
1569 return src_len;
1572 size_t pull_ucs2_fstring(char *dest, const void *src)
1574 return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
1578 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1580 * @param dest always set at least to NULL
1582 * @returns The number of bytes occupied by the string in the destination
1585 size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
1587 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1588 *dest = NULL;
1589 return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len, (void **)dest, True);
1593 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1595 * @param dest always set at least to NULL
1597 * @returns The number of bytes occupied by the string in the destination
1600 size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
1602 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1603 *dest = NULL;
1604 return convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, src, src_len, (void **)dest, True);
1608 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1610 * @param dest always set at least to NULL
1612 * @returns The number of bytes occupied by the string in the destination
1615 size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1617 size_t src_len = strlen(src)+1;
1618 *dest = NULL;
1619 return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1623 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1625 * @param dest always set at least to NULL
1627 * @returns The number of bytes occupied by the string in the destination
1630 size_t pull_utf8_allocate(char **dest, const char *src)
1632 size_t src_len = strlen(src)+1;
1633 *dest = NULL;
1634 return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1638 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1640 * @param dest always set at least to NULL
1642 * @returns The number of bytes occupied by the string in the destination
1645 size_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1647 size_t src_len = strlen(src)+1;
1648 *dest = NULL;
1649 return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest, True);
1653 Copy a string from a char* src to a unicode or ascii
1654 dos codepage destination choosing unicode or ascii based on the
1655 flags in the SMB buffer starting at base_ptr.
1656 Return the number of bytes occupied by the string in the destination.
1657 flags can have:
1658 STR_TERMINATE means include the null termination.
1659 STR_UPPER means uppercase in the destination.
1660 STR_ASCII use ascii even with unicode packet.
1661 STR_NOALIGN means don't do alignment.
1662 dest_len is the maximum length allowed in the destination. If dest_len
1663 is -1 then no maxiumum is used.
1666 size_t push_string_fn(const char *function, unsigned int line,
1667 const void *base_ptr, uint16 flags2,
1668 void *dest, const char *src,
1669 size_t dest_len, int flags)
1671 #ifdef DEVELOPER
1672 /* We really need to zero fill here, not clobber
1673 * region, as we want to ensure that valgrind thinks
1674 * all of the outgoing buffer has been written to
1675 * so a send() or write() won't trap an error.
1676 * JRA.
1678 #if 0
1679 clobber_region(function, line, dest, dest_len);
1680 #else
1681 memset(dest, '\0', dest_len);
1682 #endif
1683 #endif
1685 if (!(flags & STR_ASCII) && \
1686 ((flags & STR_UNICODE || \
1687 (flags2 & FLAGS2_UNICODE_STRINGS)))) {
1688 return push_ucs2(base_ptr, dest, src, dest_len, flags);
1690 return push_ascii(dest, src, dest_len, flags);
1695 Copy a string from a unicode or ascii source (depending on
1696 the packet flags) to a char* destination.
1697 Flags can have:
1698 STR_TERMINATE means the string in src is null terminated.
1699 STR_UNICODE means to force as unicode.
1700 STR_ASCII use ascii even with unicode packet.
1701 STR_NOALIGN means don't do alignment.
1702 if STR_TERMINATE is set then src_len is ignored is it is -1
1703 src_len is the length of the source area in bytes.
1704 Return the number of bytes occupied by the string in src.
1705 The resulting string in "dest" is always null terminated.
1708 size_t pull_string_fn(const char *function,
1709 unsigned int line,
1710 const void *base_ptr,
1711 uint16 smb_flags2,
1712 char *dest,
1713 const void *src,
1714 size_t dest_len,
1715 size_t src_len,
1716 int flags)
1718 #ifdef DEVELOPER
1719 clobber_region(function, line, dest, dest_len);
1720 #endif
1722 if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
1723 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1724 "UNICODE defined");
1727 if (!(flags & STR_ASCII) && \
1728 ((flags & STR_UNICODE || \
1729 (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
1730 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
1732 return pull_ascii(dest, src, dest_len, src_len, flags);
1736 Copy a string from a unicode or ascii source (depending on
1737 the packet flags) to a char* destination.
1738 Variant that uses talloc.
1739 Flags can have:
1740 STR_TERMINATE means the string in src is null terminated.
1741 STR_UNICODE means to force as unicode.
1742 STR_ASCII use ascii even with unicode packet.
1743 STR_NOALIGN means don't do alignment.
1744 if STR_TERMINATE is set then src_len is ignored is it is -1
1745 src_len is the length of the source area in bytes.
1746 Return the number of bytes occupied by the string in src.
1747 The resulting string in "dest" is always null terminated.
1750 size_t pull_string_talloc_fn(const char *function,
1751 unsigned int line,
1752 TALLOC_CTX *ctx,
1753 const void *base_ptr,
1754 uint16 smb_flags2,
1755 char **ppdest,
1756 const void *src,
1757 size_t src_len,
1758 int flags)
1760 if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
1761 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1762 "UNICODE defined");
1765 if (!(flags & STR_ASCII) && \
1766 ((flags & STR_UNICODE || \
1767 (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
1768 return pull_ucs2_base_talloc(ctx,
1769 base_ptr,
1770 ppdest,
1771 src,
1772 src_len,
1773 flags);
1775 return pull_ascii_base_talloc(ctx,
1776 ppdest,
1777 src,
1778 src_len,
1779 flags);
1783 size_t align_string(const void *base_ptr, const char *p, int flags)
1785 if (!(flags & STR_ASCII) && \
1786 ((flags & STR_UNICODE || \
1787 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1788 return ucs2_align(base_ptr, p, flags);
1790 return 0;
1794 Return the unicode codepoint for the next multi-byte CH_UNIX character
1795 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1797 Also return the number of bytes consumed (which tells the caller
1798 how many bytes to skip to get to the next CH_UNIX character).
1800 Return INVALID_CODEPOINT if the next character cannot be converted.
1803 codepoint_t next_codepoint(const char *str, size_t *size)
1805 /* It cannot occupy more than 4 bytes in UTF16 format */
1806 uint8_t buf[4];
1807 smb_iconv_t descriptor;
1808 size_t ilen_orig;
1809 size_t ilen;
1810 size_t olen;
1811 char *outbuf;
1813 if ((str[0] & 0x80) == 0) {
1814 *size = 1;
1815 return (codepoint_t)str[0];
1818 /* We assume that no multi-byte character can take
1819 more than 5 bytes. This is OK as we only
1820 support codepoints up to 1M */
1822 ilen_orig = strnlen(str, 5);
1823 ilen = ilen_orig;
1825 lazy_initialize_conv();
1827 descriptor = conv_handles[CH_UNIX][CH_UTF16LE];
1828 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
1829 *size = 1;
1830 return INVALID_CODEPOINT;
1833 /* This looks a little strange, but it is needed to cope
1834 with codepoints above 64k which are encoded as per RFC2781. */
1835 olen = 2;
1836 outbuf = (char *)buf;
1837 smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
1838 if (olen == 2) {
1839 /* We failed to convert to a 2 byte character.
1840 See if we can convert to a 4 UTF16-LE byte char encoding.
1842 olen = 4;
1843 outbuf = (char *)buf;
1844 smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
1845 if (olen == 4) {
1846 /* We didn't convert any bytes */
1847 *size = 1;
1848 return INVALID_CODEPOINT;
1850 olen = 4 - olen;
1851 } else {
1852 olen = 2 - olen;
1855 *size = ilen_orig - ilen;
1857 if (olen == 2) {
1858 /* 2 byte, UTF16-LE encoded value. */
1859 return (codepoint_t)SVAL(buf, 0);
1861 if (olen == 4) {
1862 /* Decode a 4 byte UTF16-LE character manually.
1863 See RFC2871 for the encoding machanism.
1865 codepoint_t w1 = SVAL(buf,0) & ~0xD800;
1866 codepoint_t w2 = SVAL(buf,2) & ~0xDC00;
1868 return (codepoint_t)0x10000 +
1869 (w1 << 10) + w2;
1872 /* no other length is valid */
1873 return INVALID_CODEPOINT;