create-tarball: Remove trailing/leading whitespaces.
[Samba.git] / source3 / lib / charcnv.c
blob485212b100f510523e7701aa074a6468e33da95d
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 ? */
50 static bool initialized;
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_UTF16LE) ret = "UTF-16LE";
60 else if (ch == CH_UTF16BE) ret = "UTF-16BE";
61 else if (ch == CH_UNIX) ret = lp_unix_charset();
62 else if (ch == CH_DOS) ret = lp_dos_charset();
63 else if (ch == CH_DISPLAY) ret = lp_display_charset();
64 else if (ch == CH_UTF8) ret = "UTF8";
66 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
67 if (ret && !strcmp(ret, "LOCALE")) {
68 const char *ln = NULL;
70 #ifdef HAVE_SETLOCALE
71 setlocale(LC_ALL, "");
72 #endif
73 ln = nl_langinfo(CODESET);
74 if (ln) {
75 /* Check whether the charset name is supported
76 by iconv */
77 smb_iconv_t handle = smb_iconv_open(ln,"UCS-2LE");
78 if (handle == (smb_iconv_t) -1) {
79 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln));
80 ln = NULL;
81 } else {
82 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln));
83 smb_iconv_close(handle);
86 ret = ln;
88 #endif
90 if (!ret || !*ret) ret = "ASCII";
91 return ret;
94 void lazy_initialize_conv(void)
96 if (!initialized) {
97 load_case_tables();
98 init_iconv();
99 initialized = true;
104 * Destroy global objects allocated by init_iconv()
106 void gfree_charcnv(void)
108 int c1, c2;
110 for (c1=0;c1<NUM_CHARSETS;c1++) {
111 for (c2=0;c2<NUM_CHARSETS;c2++) {
112 if ( conv_handles[c1][c2] ) {
113 smb_iconv_close( conv_handles[c1][c2] );
114 conv_handles[c1][c2] = 0;
118 initialized = false;
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 * @param converted_size set to the size of the allocated buffer on return
515 * true
516 * @note -1 is not accepted for srclen.
518 * @return true if new buffer was correctly allocated, and string was
519 * converted.
521 * Ensure the srclen contains the terminating zero.
523 * I hate the goto's in this function. It's embarressing.....
524 * There has to be a cleaner way to do this. JRA.
527 bool convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
528 void const *src, size_t srclen, void *dst,
529 size_t *converted_size, bool allow_bad_conv)
531 size_t i_len, o_len, destlen = (srclen * 3) / 2;
532 size_t retval;
533 const char *inbuf = (const char *)src;
534 char *outbuf = NULL, *ob = NULL;
535 smb_iconv_t descriptor;
536 void **dest = (void **)dst;
538 *dest = NULL;
540 if (!converted_size) {
541 errno = EINVAL;
542 return false;
545 if (src == NULL || srclen == (size_t)-1) {
546 errno = EINVAL;
547 return false;
549 if (srclen == 0) {
550 *converted_size = 0;
551 return true;
554 lazy_initialize_conv();
556 descriptor = conv_handles[from][to];
558 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
559 if (!conv_silent)
560 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
561 errno = EOPNOTSUPP;
562 return false;
565 convert:
567 /* +2 is for ucs2 null termination. */
568 if ((destlen*2)+2 < destlen) {
569 /* wrapped ! abort. */
570 if (!conv_silent)
571 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
572 if (!ctx)
573 SAFE_FREE(outbuf);
574 errno = EOPNOTSUPP;
575 return false;
576 } else {
577 destlen = destlen * 2;
580 /* +2 is for ucs2 null termination. */
581 if (ctx) {
582 ob = (char *)TALLOC_REALLOC(ctx, ob, destlen + 2);
583 } else {
584 ob = (char *)SMB_REALLOC(ob, destlen + 2);
587 if (!ob) {
588 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
589 errno = ENOMEM;
590 return false;
592 outbuf = ob;
593 i_len = srclen;
594 o_len = destlen;
596 again:
598 retval = smb_iconv(descriptor,
599 &inbuf, &i_len,
600 &outbuf, &o_len);
601 if(retval == (size_t)-1) {
602 const char *reason="unknown error";
603 switch(errno) {
604 case EINVAL:
605 reason="Incomplete multibyte sequence";
606 if (!conv_silent)
607 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
608 if (allow_bad_conv)
609 goto use_as_is;
610 break;
611 case E2BIG:
612 goto convert;
613 case EILSEQ:
614 reason="Illegal multibyte sequence";
615 if (!conv_silent)
616 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
617 if (allow_bad_conv)
618 goto use_as_is;
619 break;
621 if (!conv_silent)
622 DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
623 /* smb_panic(reason); */
624 if (ctx) {
625 TALLOC_FREE(ob);
626 } else {
627 SAFE_FREE(ob);
629 return false;
632 out:
634 destlen = destlen - o_len;
635 /* Don't shrink unless we're reclaiming a lot of
636 * space. This is in the hot codepath and these
637 * reallocs *cost*. JRA.
639 if (o_len > 1024) {
640 /* We're shrinking here so we know the +2 is safe from wrap. */
641 if (ctx) {
642 ob = (char *)TALLOC_REALLOC(ctx,ob,destlen + 2);
643 } else {
644 ob = (char *)SMB_REALLOC(ob,destlen + 2);
648 if (destlen && !ob) {
649 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
650 errno = ENOMEM;
651 return false;
654 *dest = ob;
656 /* Must ucs2 null terminate in the extra space we allocated. */
657 ob[destlen] = '\0';
658 ob[destlen+1] = '\0';
660 *converted_size = destlen;
661 return true;
663 use_as_is:
666 * Conversion not supported. This is actually an error, but there are so
667 * many misconfigured iconv systems and smb.conf's out there we can't just
668 * fail. Do a very bad conversion instead.... JRA.
672 if (o_len == 0 || i_len == 0)
673 goto out;
675 if (((from == CH_UTF16LE)||(from == CH_UTF16BE)) &&
676 ((to != CH_UTF16LE)||(to != CH_UTF16BE))) {
677 /* Can't convert from utf16 any endian to multibyte.
678 Replace with the default fail char.
681 if (i_len < 2)
682 goto out;
684 if (i_len >= 2) {
685 *outbuf = lp_failed_convert_char();
687 outbuf++;
688 o_len--;
690 inbuf += 2;
691 i_len -= 2;
694 if (o_len == 0 || i_len == 0)
695 goto out;
697 /* Keep trying with the next char... */
698 goto again;
700 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
701 /* Can't convert to UTF16LE - just widen by adding the
702 default fail char then zero.
704 if (o_len < 2)
705 goto out;
707 outbuf[0] = lp_failed_convert_char();
708 outbuf[1] = '\0';
710 inbuf++;
711 i_len--;
713 outbuf += 2;
714 o_len -= 2;
716 if (o_len == 0 || i_len == 0)
717 goto out;
719 /* Keep trying with the next char... */
720 goto again;
722 } else if (from != CH_UTF16LE && from != CH_UTF16BE &&
723 to != CH_UTF16LE && to != CH_UTF16BE) {
724 /* Failed multibyte to multibyte. Just copy the default fail char and
725 try again. */
726 outbuf[0] = lp_failed_convert_char();
728 inbuf++;
729 i_len--;
731 outbuf++;
732 o_len--;
734 if (o_len == 0 || i_len == 0)
735 goto out;
737 /* Keep trying with the next char... */
738 goto again;
740 } else {
741 /* Keep compiler happy.... */
742 goto out;
748 * Convert between character sets, allocating a new buffer using talloc for the result.
750 * @param srclen length of source buffer.
751 * @param dest always set at least to NULL
752 * @parm converted_size set to the number of bytes occupied by the string in
753 * the destination on success.
754 * @note -1 is not accepted for srclen.
756 * @return true if new buffer was correctly allocated, and string was
757 * converted.
759 bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
760 void const *src, size_t srclen, void *dst,
761 size_t *converted_size, bool allow_bad_conv)
763 void **dest = (void **)dst;
765 *dest = NULL;
766 return convert_string_allocate(ctx, from, to, src, srclen, dest,
767 converted_size, allow_bad_conv);
770 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
772 size_t size;
773 smb_ucs2_t *buffer;
775 if (!push_ucs2_allocate(&buffer, src, &size)) {
776 return (size_t)-1;
779 if (!strupper_w(buffer) && (dest == src)) {
780 free(buffer);
781 return srclen;
784 size = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, True);
785 free(buffer);
786 return size;
790 strdup() a unix string to upper case.
793 char *strdup_upper(const char *s)
795 char *out_buffer = SMB_STRDUP(s);
796 const unsigned char *p = (const unsigned char *)s;
797 unsigned char *q = (unsigned char *)out_buffer;
799 if (!q) {
800 return NULL;
803 /* this is quite a common operation, so we want it to be
804 fast. We optimise for the ascii case, knowing that all our
805 supported multi-byte character sets are ascii-compatible
806 (ie. they match for the first 128 chars) */
808 while (*p) {
809 if (*p & 0x80)
810 break;
811 *q++ = toupper_ascii_fast(*p);
812 p++;
815 if (*p) {
816 /* MB case. */
817 size_t converted_size, converted_size2;
818 smb_ucs2_t *buffer = NULL;
820 SAFE_FREE(out_buffer);
821 if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, s,
822 strlen(s) + 1,
823 (void **)(void *)&buffer,
824 &converted_size, True))
826 return NULL;
829 strupper_w(buffer);
831 if (!convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, buffer,
832 converted_size,
833 (void **)(void *)&out_buffer,
834 &converted_size2, True))
836 TALLOC_FREE(buffer);
837 return NULL;
840 /* Don't need the intermediate buffer
841 * anymore.
843 TALLOC_FREE(buffer);
846 return out_buffer;
850 talloc_strdup() a unix string to upper case.
853 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s)
855 char *out_buffer = talloc_strdup(ctx,s);
856 const unsigned char *p = (const unsigned char *)s;
857 unsigned char *q = (unsigned char *)out_buffer;
859 if (!q) {
860 return NULL;
863 /* this is quite a common operation, so we want it to be
864 fast. We optimise for the ascii case, knowing that all our
865 supported multi-byte character sets are ascii-compatible
866 (ie. they match for the first 128 chars) */
868 while (*p) {
869 if (*p & 0x80)
870 break;
871 *q++ = toupper_ascii_fast(*p);
872 p++;
875 if (*p) {
876 /* MB case. */
877 size_t converted_size, converted_size2;
878 smb_ucs2_t *ubuf = NULL;
880 /* We're not using the ascii buffer above. */
881 TALLOC_FREE(out_buffer);
883 if (!convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, s,
884 strlen(s)+1, (void *)&ubuf,
885 &converted_size, True))
887 return NULL;
890 strupper_w(ubuf);
892 if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, ubuf,
893 converted_size, (void *)&out_buffer,
894 &converted_size2, True))
896 TALLOC_FREE(ubuf);
897 return NULL;
900 /* Don't need the intermediate buffer
901 * anymore.
903 TALLOC_FREE(ubuf);
906 return out_buffer;
909 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
911 size_t size;
912 smb_ucs2_t *buffer = NULL;
914 if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, srclen,
915 (void **)(void *)&buffer, &size,
916 True))
918 smb_panic("failed to create UCS2 buffer");
920 if (!strlower_w(buffer) && (dest == src)) {
921 SAFE_FREE(buffer);
922 return srclen;
924 size = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, True);
925 SAFE_FREE(buffer);
926 return size;
930 strdup() a unix string to lower case.
933 char *strdup_lower(const char *s)
935 size_t converted_size;
936 smb_ucs2_t *buffer = NULL;
937 char *out_buffer;
939 if (!push_ucs2_allocate(&buffer, s, &converted_size)) {
940 return NULL;
943 strlower_w(buffer);
945 if (!pull_ucs2_allocate(&out_buffer, buffer, &converted_size)) {
946 SAFE_FREE(buffer);
947 return NULL;
950 SAFE_FREE(buffer);
952 return out_buffer;
955 char *talloc_strdup_lower(TALLOC_CTX *ctx, const char *s)
957 size_t converted_size;
958 smb_ucs2_t *buffer = NULL;
959 char *out_buffer;
961 if (!push_ucs2_talloc(ctx, &buffer, s, &converted_size)) {
962 return NULL;
965 strlower_w(buffer);
967 if (!pull_ucs2_talloc(ctx, &out_buffer, buffer, &converted_size)) {
968 TALLOC_FREE(buffer);
969 return NULL;
972 TALLOC_FREE(buffer);
974 return out_buffer;
978 size_t ucs2_align(const void *base_ptr, const void *p, int flags)
980 if (flags & (STR_NOALIGN|STR_ASCII))
981 return 0;
982 return PTR_DIFF(p, base_ptr) & 1;
987 * Copy a string from a char* unix src to a dos codepage string destination.
989 * @return the number of bytes occupied by the string in the destination.
991 * @param flags can include
992 * <dl>
993 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
994 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
995 * </dl>
997 * @param dest_len the maximum length in bytes allowed in the
998 * destination.
1000 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
1002 size_t src_len = strlen(src);
1003 char *tmpbuf = NULL;
1004 size_t ret;
1006 /* No longer allow a length of -1. */
1007 if (dest_len == (size_t)-1) {
1008 smb_panic("push_ascii - dest_len == -1");
1011 if (flags & STR_UPPER) {
1012 tmpbuf = SMB_STRDUP(src);
1013 if (!tmpbuf) {
1014 smb_panic("malloc fail");
1016 strupper_m(tmpbuf);
1017 src = tmpbuf;
1020 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
1021 src_len++;
1024 ret = convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len, True);
1025 if (ret == (size_t)-1 &&
1026 (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
1027 && dest_len > 0) {
1028 ((char *)dest)[0] = '\0';
1030 SAFE_FREE(tmpbuf);
1031 return ret;
1034 size_t push_ascii_fstring(void *dest, const char *src)
1036 return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
1039 /********************************************************************
1040 Push an nstring - ensure null terminated. Written by
1041 moriyama@miraclelinux.com (MORIYAMA Masayuki).
1042 ********************************************************************/
1044 size_t push_ascii_nstring(void *dest, const char *src)
1046 size_t i, buffer_len, dest_len;
1047 smb_ucs2_t *buffer;
1049 conv_silent = True;
1050 if (!push_ucs2_allocate(&buffer, src, &buffer_len)) {
1051 smb_panic("failed to create UCS2 buffer");
1054 /* We're using buffer_len below to count ucs2 characters, not bytes. */
1055 buffer_len /= sizeof(smb_ucs2_t);
1057 dest_len = 0;
1058 for (i = 0; buffer[i] != 0 && (i < buffer_len); i++) {
1059 unsigned char mb[10];
1060 /* Convert one smb_ucs2_t character at a time. */
1061 size_t mb_len = convert_string(CH_UTF16LE, CH_DOS, buffer+i, sizeof(smb_ucs2_t), mb, sizeof(mb), False);
1062 if ((mb_len != (size_t)-1) && (dest_len + mb_len <= MAX_NETBIOSNAME_LEN - 1)) {
1063 memcpy((char *)dest + dest_len, mb, mb_len);
1064 dest_len += mb_len;
1065 } else {
1066 errno = E2BIG;
1067 break;
1070 ((char *)dest)[dest_len] = '\0';
1072 SAFE_FREE(buffer);
1073 conv_silent = False;
1074 return dest_len;
1077 /********************************************************************
1078 Push and malloc an ascii string. src and dest null terminated.
1079 ********************************************************************/
1081 bool push_ascii_allocate(char **dest, const char *src, size_t *converted_size)
1083 size_t src_len = strlen(src)+1;
1085 *dest = NULL;
1086 return convert_string_allocate(NULL, CH_UNIX, CH_DOS, src, src_len,
1087 (void **)dest, converted_size, True);
1091 * Copy a string from a dos codepage source to a unix char* destination.
1093 * The resulting string in "dest" is always null terminated.
1095 * @param flags can have:
1096 * <dl>
1097 * <dt>STR_TERMINATE</dt>
1098 * <dd>STR_TERMINATE means the string in @p src
1099 * is null terminated, and src_len is ignored.</dd>
1100 * </dl>
1102 * @param src_len is the length of the source area in bytes.
1103 * @returns the number of bytes occupied by the string in @p src.
1105 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1107 size_t ret;
1109 if (dest_len == (size_t)-1) {
1110 /* No longer allow dest_len of -1. */
1111 smb_panic("pull_ascii - invalid dest_len of -1");
1114 if (flags & STR_TERMINATE) {
1115 if (src_len == (size_t)-1) {
1116 src_len = strlen((const char *)src) + 1;
1117 } else {
1118 size_t len = strnlen((const char *)src, src_len);
1119 if (len < src_len)
1120 len++;
1121 src_len = len;
1125 ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len, True);
1126 if (ret == (size_t)-1) {
1127 ret = 0;
1128 dest_len = 0;
1131 if (dest_len && ret) {
1132 /* Did we already process the terminating zero ? */
1133 if (dest[MIN(ret-1, dest_len-1)] != 0) {
1134 dest[MIN(ret, dest_len-1)] = 0;
1136 } else {
1137 dest[0] = 0;
1140 return src_len;
1144 * Copy a string from a dos codepage source to a unix char* destination.
1145 Talloc version.
1146 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1147 needs fixing. JRA).
1149 * The resulting string in "dest" is always null terminated.
1151 * @param flags can have:
1152 * <dl>
1153 * <dt>STR_TERMINATE</dt>
1154 * <dd>STR_TERMINATE means the string in @p src
1155 * is null terminated, and src_len is ignored.</dd>
1156 * </dl>
1158 * @param src_len is the length of the source area in bytes.
1159 * @returns the number of bytes occupied by the string in @p src.
1162 static size_t pull_ascii_base_talloc(TALLOC_CTX *ctx,
1163 char **ppdest,
1164 const void *src,
1165 size_t src_len,
1166 int flags)
1168 char *dest = NULL;
1169 size_t converted_size;
1171 #ifdef DEVELOPER
1172 /* Ensure we never use the braindead "malloc" varient. */
1173 if (ctx == NULL) {
1174 smb_panic("NULL talloc CTX in pull_ascii_base_talloc\n");
1176 #endif
1178 *ppdest = NULL;
1180 if (flags & STR_TERMINATE) {
1181 if (src_len == (size_t)-1) {
1182 src_len = strlen((const char *)src) + 1;
1183 } else {
1184 size_t len = strnlen((const char *)src, src_len);
1185 if (len < src_len)
1186 len++;
1187 src_len = len;
1189 /* Ensure we don't use an insane length from the client. */
1190 if (src_len >= 1024*1024) {
1191 char *msg = talloc_asprintf(ctx,
1192 "Bad src length (%u) in "
1193 "pull_ascii_base_talloc",
1194 (unsigned int)src_len);
1195 smb_panic(msg);
1199 if (!convert_string_allocate(ctx, CH_DOS, CH_UNIX, src, src_len, &dest,
1200 &converted_size, True))
1202 converted_size = 0;
1205 if (converted_size && dest) {
1206 /* Did we already process the terminating zero ? */
1207 if (dest[converted_size - 1] != 0) {
1208 dest[converted_size - 1] = 0;
1210 } else if (dest) {
1211 dest[0] = 0;
1214 *ppdest = dest;
1215 return src_len;
1218 size_t pull_ascii_fstring(char *dest, const void *src)
1220 return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
1223 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1225 size_t pull_ascii_nstring(char *dest, size_t dest_len, const void *src)
1227 return pull_ascii(dest, src, dest_len, sizeof(nstring)-1, STR_TERMINATE);
1231 * Copy a string from a char* src to a unicode destination.
1233 * @returns the number of bytes occupied by the string in the destination.
1235 * @param flags can have:
1237 * <dl>
1238 * <dt>STR_TERMINATE <dd>means include the null termination.
1239 * <dt>STR_UPPER <dd>means uppercase in the destination.
1240 * <dt>STR_NOALIGN <dd>means don't do alignment.
1241 * </dl>
1243 * @param dest_len is the maximum length allowed in the
1244 * destination.
1247 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
1249 size_t len=0;
1250 size_t src_len;
1251 size_t ret;
1253 if (dest_len == (size_t)-1) {
1254 /* No longer allow dest_len of -1. */
1255 smb_panic("push_ucs2 - invalid dest_len of -1");
1258 if (flags & STR_TERMINATE)
1259 src_len = (size_t)-1;
1260 else
1261 src_len = strlen(src);
1263 if (ucs2_align(base_ptr, dest, flags)) {
1264 *(char *)dest = 0;
1265 dest = (void *)((char *)dest + 1);
1266 if (dest_len)
1267 dest_len--;
1268 len++;
1271 /* ucs2 is always a multiple of 2 bytes */
1272 dest_len &= ~1;
1274 ret = convert_string(CH_UNIX, CH_UTF16LE, src, src_len, dest, dest_len, True);
1275 if (ret == (size_t)-1) {
1276 if ((flags & STR_TERMINATE) &&
1277 dest &&
1278 dest_len) {
1279 *(char *)dest = 0;
1281 return len;
1284 len += ret;
1286 if (flags & STR_UPPER) {
1287 smb_ucs2_t *dest_ucs2 = (smb_ucs2_t *)dest;
1288 size_t i;
1290 /* We check for i < (ret / 2) below as the dest string isn't null
1291 terminated if STR_TERMINATE isn't set. */
1293 for (i = 0; i < (ret / 2) && i < (dest_len / 2) && dest_ucs2[i]; i++) {
1294 smb_ucs2_t v = toupper_w(dest_ucs2[i]);
1295 if (v != dest_ucs2[i]) {
1296 dest_ucs2[i] = v;
1301 return len;
1306 * Copy a string from a unix char* src to a UCS2 destination,
1307 * allocating a buffer using talloc().
1309 * @param dest always set at least to NULL
1310 * @parm converted_size set to the number of bytes occupied by the string in
1311 * the destination on success.
1313 * @return true if new buffer was correctly allocated, and string was
1314 * converted.
1316 bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src,
1317 size_t *converted_size)
1319 size_t src_len = strlen(src)+1;
1321 *dest = NULL;
1322 return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len,
1323 (void **)dest, converted_size, True);
1328 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1330 * @param dest always set at least to NULL
1331 * @parm converted_size set to the number of bytes occupied by the string in
1332 * the destination on success.
1334 * @return true if new buffer was correctly allocated, and string was
1335 * converted.
1338 bool push_ucs2_allocate(smb_ucs2_t **dest, const char *src,
1339 size_t *converted_size)
1341 size_t src_len = strlen(src)+1;
1343 *dest = NULL;
1344 return convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, src_len,
1345 (void **)dest, converted_size, True);
1349 Copy a string from a char* src to a UTF-8 destination.
1350 Return the number of bytes occupied by the string in the destination
1351 Flags can have:
1352 STR_TERMINATE means include the null termination
1353 STR_UPPER means uppercase in the destination
1354 dest_len is the maximum length allowed in the destination. If dest_len
1355 is -1 then no maxiumum is used.
1358 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
1360 size_t src_len = 0;
1361 size_t ret;
1362 char *tmpbuf = NULL;
1364 if (dest_len == (size_t)-1) {
1365 /* No longer allow dest_len of -1. */
1366 smb_panic("push_utf8 - invalid dest_len of -1");
1369 if (flags & STR_UPPER) {
1370 tmpbuf = strdup_upper(src);
1371 if (!tmpbuf) {
1372 return (size_t)-1;
1374 src = tmpbuf;
1375 src_len = strlen(src);
1378 src_len = strlen(src);
1379 if (flags & STR_TERMINATE) {
1380 src_len++;
1383 ret = convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len, True);
1384 SAFE_FREE(tmpbuf);
1385 return ret;
1388 size_t push_utf8_fstring(void *dest, const char *src)
1390 return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
1394 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1396 * @param dest always set at least to NULL
1397 * @parm converted_size set to the number of bytes occupied by the string in
1398 * the destination on success.
1400 * @return true if new buffer was correctly allocated, and string was
1401 * converted.
1404 bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src,
1405 size_t *converted_size)
1407 size_t src_len = strlen(src)+1;
1409 *dest = NULL;
1410 return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len,
1411 (void**)dest, converted_size, True);
1415 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1417 * @param dest always set at least to NULL
1418 * @parm converted_size set to the number of bytes occupied by the string in
1419 * the destination on success.
1421 * @return true if new buffer was correctly allocated, and string was
1422 * converted.
1425 bool push_utf8_allocate(char **dest, const char *src, size_t *converted_size)
1427 size_t src_len = strlen(src)+1;
1429 *dest = NULL;
1430 return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len,
1431 (void **)dest, converted_size, True);
1435 Copy a string from a ucs2 source to a unix char* destination.
1436 Flags can have:
1437 STR_TERMINATE means the string in src is null terminated.
1438 STR_NOALIGN means don't try to align.
1439 if STR_TERMINATE is set then src_len is ignored if it is -1.
1440 src_len is the length of the source area in bytes
1441 Return the number of bytes occupied by the string in src.
1442 The resulting string in "dest" is always null terminated.
1445 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1447 size_t ret;
1449 if (dest_len == (size_t)-1) {
1450 /* No longer allow dest_len of -1. */
1451 smb_panic("pull_ucs2 - invalid dest_len of -1");
1454 if (!src_len) {
1455 if (dest && dest_len > 0) {
1456 dest[0] = '\0';
1458 return 0;
1461 if (ucs2_align(base_ptr, src, flags)) {
1462 src = (const void *)((const char *)src + 1);
1463 if (src_len != (size_t)-1)
1464 src_len--;
1467 if (flags & STR_TERMINATE) {
1468 /* src_len -1 is the default for null terminated strings. */
1469 if (src_len != (size_t)-1) {
1470 size_t len = strnlen_w((const smb_ucs2_t *)src,
1471 src_len/2);
1472 if (len < src_len/2)
1473 len++;
1474 src_len = len*2;
1478 /* ucs2 is always a multiple of 2 bytes */
1479 if (src_len != (size_t)-1)
1480 src_len &= ~1;
1482 ret = convert_string(CH_UTF16LE, CH_UNIX, src, src_len, dest, dest_len, True);
1483 if (ret == (size_t)-1) {
1484 ret = 0;
1485 dest_len = 0;
1488 if (src_len == (size_t)-1)
1489 src_len = ret*2;
1491 if (dest_len && ret) {
1492 /* Did we already process the terminating zero ? */
1493 if (dest[MIN(ret-1, dest_len-1)] != 0) {
1494 dest[MIN(ret, dest_len-1)] = 0;
1496 } else {
1497 dest[0] = 0;
1500 return src_len;
1504 Copy a string from a ucs2 source to a unix char* destination.
1505 Talloc version with a base pointer.
1506 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1507 needs fixing. JRA).
1508 Flags can have:
1509 STR_TERMINATE means the string in src is null terminated.
1510 STR_NOALIGN means don't try to align.
1511 if STR_TERMINATE is set then src_len is ignored if it is -1.
1512 src_len is the length of the source area in bytes
1513 Return the number of bytes occupied by the string in src.
1514 The resulting string in "dest" is always null terminated.
1517 size_t pull_ucs2_base_talloc(TALLOC_CTX *ctx,
1518 const void *base_ptr,
1519 char **ppdest,
1520 const void *src,
1521 size_t src_len,
1522 int flags)
1524 char *dest;
1525 size_t dest_len;
1527 *ppdest = NULL;
1529 #ifdef DEVELOPER
1530 /* Ensure we never use the braindead "malloc" varient. */
1531 if (ctx == NULL) {
1532 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
1534 #endif
1536 if (!src_len) {
1537 return 0;
1540 if (ucs2_align(base_ptr, src, flags)) {
1541 src = (const void *)((const char *)src + 1);
1542 if (src_len != (size_t)-1)
1543 src_len--;
1546 if (flags & STR_TERMINATE) {
1547 /* src_len -1 is the default for null terminated strings. */
1548 if (src_len != (size_t)-1) {
1549 size_t len = strnlen_w((const smb_ucs2_t *)src,
1550 src_len/2);
1551 if (len < src_len/2)
1552 len++;
1553 src_len = len*2;
1554 } else {
1556 * src_len == -1 - alloc interface won't take this
1557 * so we must calculate.
1559 src_len = (strlen_w((const smb_ucs2_t *)src)+1)*sizeof(smb_ucs2_t);
1561 /* Ensure we don't use an insane length from the client. */
1562 if (src_len >= 1024*1024) {
1563 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1567 /* ucs2 is always a multiple of 2 bytes */
1568 if (src_len != (size_t)-1) {
1569 src_len &= ~1;
1572 if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len,
1573 (void *)&dest, &dest_len, True)) {
1574 dest_len = 0;
1577 if (src_len == (size_t)-1)
1578 src_len = dest_len*2;
1580 if (dest_len) {
1581 /* Did we already process the terminating zero ? */
1582 if (dest[dest_len-1] != 0) {
1583 size_t size = talloc_get_size(dest);
1584 /* Have we got space to append the '\0' ? */
1585 if (size <= dest_len) {
1586 /* No, realloc. */
1587 dest = TALLOC_REALLOC_ARRAY(ctx, dest, char,
1588 dest_len+1);
1589 if (!dest) {
1590 /* talloc fail. */
1591 dest_len = (size_t)-1;
1592 return 0;
1595 /* Yay - space ! */
1596 dest[dest_len] = '\0';
1597 dest_len++;
1599 } else if (dest) {
1600 dest[0] = 0;
1603 *ppdest = dest;
1604 return src_len;
1607 size_t pull_ucs2_fstring(char *dest, const void *src)
1609 return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
1613 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1615 * @param dest always set at least to NULL
1616 * @parm converted_size set to the number of bytes occupied by the string in
1617 * the destination on success.
1619 * @return true if new buffer was correctly allocated, and string was
1620 * converted.
1623 bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src,
1624 size_t *converted_size)
1626 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1628 *dest = NULL;
1629 return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len,
1630 (void **)dest, converted_size, True);
1634 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1636 * @param dest always set at least to NULL
1637 * @parm converted_size set to the number of bytes occupied by the string in
1638 * the destination on success.
1639 * @return true if new buffer was correctly allocated, and string was
1640 * converted.
1643 bool pull_ucs2_allocate(char **dest, const smb_ucs2_t *src,
1644 size_t *converted_size)
1646 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1648 *dest = NULL;
1649 return convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, src, src_len,
1650 (void **)dest, converted_size, True);
1654 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1656 * @param dest always set at least to NULL
1657 * @parm converted_size set to the number of bytes occupied by the string in
1658 * the destination on success.
1660 * @return true if new buffer was correctly allocated, and string was
1661 * converted.
1664 bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src,
1665 size_t *converted_size)
1667 size_t src_len = strlen(src)+1;
1669 *dest = NULL;
1670 return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len,
1671 (void **)dest, converted_size, True);
1675 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1677 * @param dest always set at least to NULL
1678 * @parm converted_size set to the number of bytes occupied by the string in
1679 * the destination on success.
1681 * @return true if new buffer was correctly allocated, and string was
1682 * converted.
1685 bool pull_utf8_allocate(char **dest, const char *src, size_t *converted_size)
1687 size_t src_len = strlen(src)+1;
1689 *dest = NULL;
1690 return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len,
1691 (void **)dest, converted_size, True);
1695 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1697 * @param dest always set at least to NULL
1698 * @parm converted_size set to the number of bytes occupied by the string in
1699 * the destination on success.
1701 * @return true if new buffer was correctly allocated, and string was
1702 * converted.
1705 bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src,
1706 size_t *converted_size)
1708 size_t src_len = strlen(src)+1;
1710 *dest = NULL;
1711 return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len,
1712 (void **)dest, converted_size, True);
1716 Copy a string from a char* src to a unicode or ascii
1717 dos codepage destination choosing unicode or ascii based on the
1718 flags in the SMB buffer starting at base_ptr.
1719 Return the number of bytes occupied by the string in the destination.
1720 flags can have:
1721 STR_TERMINATE means include the null termination.
1722 STR_UPPER means uppercase in the destination.
1723 STR_ASCII use ascii even with unicode packet.
1724 STR_NOALIGN means don't do alignment.
1725 dest_len is the maximum length allowed in the destination. If dest_len
1726 is -1 then no maxiumum is used.
1729 size_t push_string_fn(const char *function, unsigned int line,
1730 const void *base_ptr, uint16 flags2,
1731 void *dest, const char *src,
1732 size_t dest_len, int flags)
1734 #ifdef DEVELOPER
1735 /* We really need to zero fill here, not clobber
1736 * region, as we want to ensure that valgrind thinks
1737 * all of the outgoing buffer has been written to
1738 * so a send() or write() won't trap an error.
1739 * JRA.
1741 #if 0
1742 clobber_region(function, line, dest, dest_len);
1743 #else
1744 memset(dest, '\0', dest_len);
1745 #endif
1746 #endif
1748 if (!(flags & STR_ASCII) && \
1749 ((flags & STR_UNICODE || \
1750 (flags2 & FLAGS2_UNICODE_STRINGS)))) {
1751 return push_ucs2(base_ptr, dest, src, dest_len, flags);
1753 return push_ascii(dest, src, dest_len, flags);
1758 Copy a string from a unicode or ascii source (depending on
1759 the packet flags) to a char* destination.
1760 Flags can have:
1761 STR_TERMINATE means the string in src is null terminated.
1762 STR_UNICODE means to force as unicode.
1763 STR_ASCII use ascii even with unicode packet.
1764 STR_NOALIGN means don't do alignment.
1765 if STR_TERMINATE is set then src_len is ignored is it is -1
1766 src_len is the length of the source area in bytes.
1767 Return the number of bytes occupied by the string in src.
1768 The resulting string in "dest" is always null terminated.
1771 size_t pull_string_fn(const char *function,
1772 unsigned int line,
1773 const void *base_ptr,
1774 uint16 smb_flags2,
1775 char *dest,
1776 const void *src,
1777 size_t dest_len,
1778 size_t src_len,
1779 int flags)
1781 #ifdef DEVELOPER
1782 clobber_region(function, line, dest, dest_len);
1783 #endif
1785 if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
1786 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1787 "UNICODE defined");
1790 if (!(flags & STR_ASCII) && \
1791 ((flags & STR_UNICODE || \
1792 (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
1793 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
1795 return pull_ascii(dest, src, dest_len, src_len, flags);
1799 Copy a string from a unicode or ascii source (depending on
1800 the packet flags) to a char* destination.
1801 Variant that uses talloc.
1802 Flags can have:
1803 STR_TERMINATE means the string in src is null terminated.
1804 STR_UNICODE means to force as unicode.
1805 STR_ASCII use ascii even with unicode packet.
1806 STR_NOALIGN means don't do alignment.
1807 if STR_TERMINATE is set then src_len is ignored is it is -1
1808 src_len is the length of the source area in bytes.
1809 Return the number of bytes occupied by the string in src.
1810 The resulting string in "dest" is always null terminated.
1813 size_t pull_string_talloc_fn(const char *function,
1814 unsigned int line,
1815 TALLOC_CTX *ctx,
1816 const void *base_ptr,
1817 uint16 smb_flags2,
1818 char **ppdest,
1819 const void *src,
1820 size_t src_len,
1821 int flags)
1823 if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
1824 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1825 "UNICODE defined");
1828 if (!(flags & STR_ASCII) && \
1829 ((flags & STR_UNICODE || \
1830 (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
1831 return pull_ucs2_base_talloc(ctx,
1832 base_ptr,
1833 ppdest,
1834 src,
1835 src_len,
1836 flags);
1838 return pull_ascii_base_talloc(ctx,
1839 ppdest,
1840 src,
1841 src_len,
1842 flags);
1846 size_t align_string(const void *base_ptr, const char *p, int flags)
1848 if (!(flags & STR_ASCII) && \
1849 ((flags & STR_UNICODE || \
1850 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1851 return ucs2_align(base_ptr, p, flags);
1853 return 0;
1857 Return the unicode codepoint for the next multi-byte CH_UNIX character
1858 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1860 Also return the number of bytes consumed (which tells the caller
1861 how many bytes to skip to get to the next CH_UNIX character).
1863 Return INVALID_CODEPOINT if the next character cannot be converted.
1866 codepoint_t next_codepoint(const char *str, size_t *size)
1868 /* It cannot occupy more than 4 bytes in UTF16 format */
1869 uint8_t buf[4];
1870 smb_iconv_t descriptor;
1871 size_t ilen_orig;
1872 size_t ilen;
1873 size_t olen;
1874 char *outbuf;
1876 if ((str[0] & 0x80) == 0) {
1877 *size = 1;
1878 return (codepoint_t)str[0];
1881 /* We assume that no multi-byte character can take
1882 more than 5 bytes. This is OK as we only
1883 support codepoints up to 1M */
1885 ilen_orig = strnlen(str, 5);
1886 ilen = ilen_orig;
1888 lazy_initialize_conv();
1890 descriptor = conv_handles[CH_UNIX][CH_UTF16LE];
1891 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
1892 *size = 1;
1893 return INVALID_CODEPOINT;
1896 /* This looks a little strange, but it is needed to cope
1897 with codepoints above 64k which are encoded as per RFC2781. */
1898 olen = 2;
1899 outbuf = (char *)buf;
1900 smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
1901 if (olen == 2) {
1902 /* We failed to convert to a 2 byte character.
1903 See if we can convert to a 4 UTF16-LE byte char encoding.
1905 olen = 4;
1906 outbuf = (char *)buf;
1907 smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
1908 if (olen == 4) {
1909 /* We didn't convert any bytes */
1910 *size = 1;
1911 return INVALID_CODEPOINT;
1913 olen = 4 - olen;
1914 } else {
1915 olen = 2 - olen;
1918 *size = ilen_orig - ilen;
1920 if (olen == 2) {
1921 /* 2 byte, UTF16-LE encoded value. */
1922 return (codepoint_t)SVAL(buf, 0);
1924 if (olen == 4) {
1925 /* Decode a 4 byte UTF16-LE character manually.
1926 See RFC2871 for the encoding machanism.
1928 codepoint_t w1 = SVAL(buf,0) & ~0xD800;
1929 codepoint_t w2 = SVAL(buf,2) & ~0xDC00;
1931 return (codepoint_t)0x10000 +
1932 (w1 << 10) + w2;
1935 /* no other length is valid */
1936 return INVALID_CODEPOINT;