2 Unix SMB/CIFS implementation.
3 Character set conversion Extensions
4 Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
5 Copyright (C) Andrew Tridgell 2001
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Martin Pool 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * @brief Character-set conversion routines built on our iconv.
31 * @note Samba's internal character set (at least in the 3.0 series)
32 * is always the same as the one for the Unix filesystem. It is
33 * <b>not</b> necessarily UTF-8 and may be different on machines that
34 * need i18n filenames to be compatible with Unix software. It does
35 * have to be a superset of ASCII. All multibyte sequences must start
36 * with a byte with the high bit set.
42 static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
43 static BOOL conv_silent; /* Should we do a debug if the conversion fails ? */
46 * Return the name of a charset to give to iconv().
48 static const char *charset_name(charset_t ch)
50 const char *ret = NULL;
52 if (ch == CH_UCS2) ret = "UCS-2LE";
53 else if (ch == CH_UNIX) ret = lp_unix_charset();
54 else if (ch == CH_DOS) ret = lp_dos_charset();
55 else if (ch == CH_DISPLAY) ret = lp_display_charset();
56 else if (ch == CH_UTF8) ret = "UTF8";
58 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
59 if (ret && !strcmp(ret, "LOCALE")) {
60 const char *ln = NULL;
63 setlocale(LC_ALL, "");
65 ln = nl_langinfo(CODESET);
67 /* Check whether the charset name is supported
69 smb_iconv_t handle = smb_iconv_open(ln,"UCS-2LE");
70 if (handle == (smb_iconv_t) -1) {
71 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln));
74 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln));
75 smb_iconv_close(handle);
82 if (!ret || !*ret) ret = "ASCII";
86 void lazy_initialize_conv(void)
88 static int initialized = False;
98 * Initialize iconv conversion descriptors.
100 * This is called the first time it is needed, and also called again
101 * every time the configuration is reloaded, because the charset or
102 * codepage might have changed.
104 void init_iconv(void)
107 BOOL did_reload = False;
109 /* so that charset_name() works we need to get the UNIX<->UCS2 going
111 if (!conv_handles[CH_UNIX][CH_UCS2])
112 conv_handles[CH_UNIX][CH_UCS2] = smb_iconv_open("UCS-2LE", "ASCII");
114 if (!conv_handles[CH_UCS2][CH_UNIX])
115 conv_handles[CH_UCS2][CH_UNIX] = smb_iconv_open("ASCII", "UCS-2LE");
117 for (c1=0;c1<NUM_CHARSETS;c1++) {
118 for (c2=0;c2<NUM_CHARSETS;c2++) {
119 const char *n1 = charset_name((charset_t)c1);
120 const char *n2 = charset_name((charset_t)c2);
121 if (conv_handles[c1][c2] &&
122 strcmp(n1, conv_handles[c1][c2]->from_name) == 0 &&
123 strcmp(n2, conv_handles[c1][c2]->to_name) == 0)
128 if (conv_handles[c1][c2])
129 smb_iconv_close(conv_handles[c1][c2]);
131 conv_handles[c1][c2] = smb_iconv_open(n2,n1);
132 if (conv_handles[c1][c2] == (smb_iconv_t)-1) {
133 DEBUG(0,("Conversion from %s to %s not supported\n",
134 charset_name((charset_t)c1), charset_name((charset_t)c2)));
135 conv_handles[c1][c2] = NULL;
141 /* XXX: Does this really get called every time the dos
142 * codepage changes? */
143 /* XXX: Is the did_reload test too strict? */
145 init_doschar_table();
152 * Convert string from one encoding to another, making error checking etc
153 * Slow path version - uses (slow) iconv.
155 * @param src pointer to source string (multibyte or singlebyte)
156 * @param srclen length of the source string in bytes
157 * @param dest pointer to destination string (multibyte or singlebyte)
158 * @param destlen maximal length allowed for string
159 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
160 * @returns the number of bytes occupied in the destination
162 * Ensure the srclen contains the terminating zero.
166 static size_t convert_string_internal(charset_t from, charset_t to,
167 void const *src, size_t srclen,
168 void *dest, size_t destlen, BOOL allow_bad_conv)
172 const char* inbuf = (const char*)src;
173 char* outbuf = (char*)dest;
174 smb_iconv_t descriptor;
176 lazy_initialize_conv();
178 descriptor = conv_handles[from][to];
180 if (srclen == (size_t)-1) {
181 if (from == CH_UCS2) {
182 srclen = (strlen_w((const smb_ucs2_t *)src)+1) * 2;
184 srclen = strlen((const char *)src)+1;
189 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
191 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
200 retval = smb_iconv(descriptor, (char **)&inbuf, &i_len, &outbuf, &o_len);
201 if(retval==(size_t)-1) {
202 const char *reason="unknown error";
205 reason="Incomplete multibyte sequence";
207 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
212 reason="No more room";
214 DEBUG(3, ("convert_string_internal: Required %lu, available %lu\n",
215 (unsigned long)srclen, (unsigned long)destlen));
216 /* we are not sure we need srclen bytes,
217 may be more, may be less.
218 We only know we need more than destlen
222 reason="Illegal multibyte sequence";
224 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
230 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
233 /* smb_panic(reason); */
235 return destlen-o_len;
240 * Conversion not supported. This is actually an error, but there are so
241 * many misconfigured iconv systems and smb.conf's out there we can't just
242 * fail. Do a very bad conversion instead.... JRA.
246 if (o_len == 0 || i_len == 0)
247 return destlen - o_len;
249 if (from == CH_UCS2 && to != CH_UCS2) {
250 /* Can't convert from ucs2 to multibyte. Just truncate this char to ascii. */
252 return destlen - o_len;
263 if (o_len == 0 || i_len == 0)
264 return destlen - o_len;
266 /* Keep trying with the next char... */
269 } else if (from != CH_UCS2 && to == CH_UCS2) {
270 /* Can't convert to ucs2 - just widen by adding zero. */
272 return destlen - o_len;
274 outbuf[0] = inbuf[0];
283 if (o_len == 0 || i_len == 0)
284 return destlen - o_len;
286 /* Keep trying with the next char... */
289 } else if (from != CH_UCS2 && to != CH_UCS2) {
290 /* Failed multibyte to multibyte. Just copy 1 char and
292 outbuf[0] = inbuf[0];
300 if (o_len == 0 || i_len == 0)
301 return destlen - o_len;
303 /* Keep trying with the next char... */
307 /* Keep compiler happy.... */
308 return destlen - o_len;
314 * Convert string from one encoding to another, making error checking etc
315 * Fast path version - handles ASCII first.
317 * @param src pointer to source string (multibyte or singlebyte)
318 * @param srclen length of the source string in bytes, or -1 for nul terminated.
319 * @param dest pointer to destination string (multibyte or singlebyte)
320 * @param destlen maximal length allowed for string - *NEVER* -1.
321 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
322 * @returns the number of bytes occupied in the destination
324 * Ensure the srclen contains the terminating zero.
326 * This function has been hand-tuned to provide a fast path.
327 * Don't change unless you really know what you are doing. JRA.
330 size_t convert_string(charset_t from, charset_t to,
331 void const *src, size_t srclen,
332 void *dest, size_t destlen, BOOL allow_bad_conv)
335 * NB. We deliberately don't do a strlen here if srclen == -1.
336 * This is very expensive over millions of calls and is taken
337 * care of in the slow path in convert_string_internal. JRA.
341 SMB_ASSERT(destlen != (size_t)-1);
347 if (from != CH_UCS2 && to != CH_UCS2) {
348 const unsigned char *p = (const unsigned char *)src;
349 unsigned char *q = (unsigned char *)dest;
350 size_t slen = srclen;
351 size_t dlen = destlen;
355 /* If all characters are ascii, fast path here. */
356 while (slen && dlen) {
357 if ((lastp = *p) <= 0x7f) {
359 if (slen != (size_t)-1) {
367 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
370 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
375 } else if (from == CH_UCS2 && to != CH_UCS2) {
376 const unsigned char *p = (const unsigned char *)src;
377 unsigned char *q = (unsigned char *)dest;
379 size_t slen = srclen;
380 size_t dlen = destlen;
383 /* If all characters are ascii, fast path here. */
384 while (((slen == (size_t)-1) || (slen >= 2)) && dlen) {
385 if (((lastp = *p) <= 0x7f) && (p[1] == 0)) {
387 if (slen != (size_t)-1) {
396 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
399 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
404 } else if (from != CH_UCS2 && to == CH_UCS2) {
405 const unsigned char *p = (const unsigned char *)src;
406 unsigned char *q = (unsigned char *)dest;
408 size_t slen = srclen;
409 size_t dlen = destlen;
412 /* If all characters are ascii, fast path here. */
413 while (slen && (dlen >= 2)) {
414 if ((lastp = *p) <= 0x7F) {
417 if (slen != (size_t)-1) {
425 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
428 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
435 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
438 return convert_string_internal(from, to, src, srclen, dest, destlen, allow_bad_conv);
442 * Convert between character sets, allocating a new buffer for the result.
444 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
445 * @param srclen length of source buffer.
446 * @param dest always set at least to NULL
447 * @note -1 is not accepted for srclen.
449 * @returns Size in bytes of the converted string; or -1 in case of error.
451 * Ensure the srclen contains the terminating zero.
453 * I hate the goto's in this function. It's embarressing.....
454 * There has to be a cleaner way to do this. JRA.
457 size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
458 void const *src, size_t srclen, void **dest, BOOL allow_bad_conv)
460 size_t i_len, o_len, destlen = MAX(srclen, 512);
462 const char *inbuf = (const char *)src;
463 char *outbuf = NULL, *ob = NULL;
464 smb_iconv_t descriptor;
468 if (src == NULL || srclen == (size_t)-1)
473 lazy_initialize_conv();
475 descriptor = conv_handles[from][to];
477 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
479 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
487 if ((destlen*2) < destlen) {
488 /* wrapped ! abort. */
490 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
495 destlen = destlen * 2;
499 ob = (char *)talloc_realloc(ctx, ob, destlen);
501 ob = (char *)Realloc(ob, destlen);
504 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
516 retval = smb_iconv(descriptor,
517 (char **)&inbuf, &i_len,
519 if(retval == (size_t)-1) {
520 const char *reason="unknown error";
523 reason="Incomplete multibyte sequence";
525 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
532 reason="Illegal multibyte sequence";
534 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
540 DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
541 /* smb_panic(reason); */
547 destlen = destlen - o_len;
549 *dest = (char *)talloc_realloc(ctx,ob,destlen);
551 *dest = (char *)Realloc(ob,destlen);
552 if (destlen && !*dest) {
553 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
564 * Conversion not supported. This is actually an error, but there are so
565 * many misconfigured iconv systems and smb.conf's out there we can't just
566 * fail. Do a very bad conversion instead.... JRA.
570 if (o_len == 0 || i_len == 0)
573 if (from == CH_UCS2 && to != CH_UCS2) {
574 /* Can't convert from ucs2 to multibyte. Just truncate this char to ascii. */
588 if (o_len == 0 || i_len == 0)
591 /* Keep trying with the next char... */
594 } else if (from != CH_UCS2 && to == CH_UCS2) {
595 /* Can't convert to ucs2 - just widen by adding zero. */
599 outbuf[0] = inbuf[0];
608 if (o_len == 0 || i_len == 0)
611 /* Keep trying with the next char... */
614 } else if (from != CH_UCS2 && to != CH_UCS2) {
615 /* Failed multibyte to multibyte. Just copy 1 char and
617 outbuf[0] = inbuf[0];
625 if (o_len == 0 || i_len == 0)
628 /* Keep trying with the next char... */
632 /* Keep compiler happy.... */
639 * Convert between character sets, allocating a new buffer using talloc for the result.
641 * @param srclen length of source buffer.
642 * @param dest always set at least to NULL
643 * @note -1 is not accepted for srclen.
645 * @returns Size in bytes of the converted string; or -1 in case of error.
647 static size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
648 void const *src, size_t srclen, void **dest, BOOL allow_bad_conv)
653 dest_len=convert_string_allocate(ctx, from, to, src, srclen, dest, allow_bad_conv);
654 if (dest_len == (size_t)-1)
661 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
666 size = push_ucs2_allocate(&buffer, src);
667 if (size == (size_t)-1) {
668 smb_panic("failed to create UCS2 buffer");
670 if (!strupper_w(buffer) && (dest == src)) {
675 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen, True);
681 strdup() a unix string to upper case.
685 char *strdup_upper(const char *s)
688 const unsigned char *p = (const unsigned char *)s;
689 unsigned char *q = (unsigned char *)out_buffer;
691 /* this is quite a common operation, so we want it to be
692 fast. We optimise for the ascii case, knowing that all our
693 supported multi-byte character sets are ascii-compatible
694 (ie. they match for the first 128 chars) */
703 if (p - ( const unsigned char *)s >= sizeof(pstring))
711 size = convert_string(CH_UNIX, CH_UCS2, s, -1, buffer, sizeof(buffer), True);
712 if (size == (size_t)-1) {
718 size = convert_string(CH_UCS2, CH_UNIX, buffer, -1, out_buffer, sizeof(out_buffer), True);
719 if (size == (size_t)-1) {
724 return strdup(out_buffer);
727 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
730 smb_ucs2_t *buffer = NULL;
732 size = convert_string_allocate(NULL, CH_UNIX, CH_UCS2, src, srclen,
733 (void **) &buffer, True);
734 if (size == (size_t)-1 || !buffer) {
735 smb_panic("failed to create UCS2 buffer");
737 if (!strlower_w(buffer) && (dest == src)) {
741 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen, True);
747 strdup() a unix string to lower case.
750 char *strdup_lower(const char *s)
753 smb_ucs2_t *buffer = NULL;
756 size = push_ucs2_allocate(&buffer, s);
757 if (size == -1 || !buffer) {
763 size = pull_ucs2_allocate(&out_buffer, buffer);
766 if (size == (size_t)-1) {
773 static size_t ucs2_align(const void *base_ptr, const void *p, int flags)
775 if (flags & (STR_NOALIGN|STR_ASCII))
777 return PTR_DIFF(p, base_ptr) & 1;
782 * Copy a string from a char* unix src to a dos codepage string destination.
784 * @return the number of bytes occupied by the string in the destination.
786 * @param flags can include
788 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
789 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
792 * @param dest_len the maximum length in bytes allowed in the
793 * destination. If @p dest_len is -1 then no maximum is used.
795 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
797 size_t src_len = strlen(src);
800 /* treat a pstring as "unlimited" length */
801 if (dest_len == (size_t)-1)
802 dest_len = sizeof(pstring);
804 if (flags & STR_UPPER) {
805 pstrcpy(tmpbuf, src);
810 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
813 return convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len, True);
816 size_t push_ascii_fstring(void *dest, const char *src)
818 return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
821 size_t push_ascii_pstring(void *dest, const char *src)
823 return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
826 size_t push_ascii_nstring(void *dest, const char *src)
828 return push_ascii(dest, src, sizeof(nstring), STR_TERMINATE);
832 * Copy a string from a dos codepage source to a unix char* destination.
834 * The resulting string in "dest" is always null terminated.
836 * @param flags can have:
838 * <dt>STR_TERMINATE</dt>
839 * <dd>STR_TERMINATE means the string in @p src
840 * is null terminated, and src_len is ignored.</dd>
843 * @param src_len is the length of the source area in bytes.
844 * @returns the number of bytes occupied by the string in @p src.
846 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
850 if (dest_len == (size_t)-1)
851 dest_len = sizeof(pstring);
853 if (flags & STR_TERMINATE) {
854 if (src_len == (size_t)-1) {
855 src_len = strlen(src) + 1;
857 size_t len = strnlen(src, src_len);
864 ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len, True);
865 if (ret == (size_t)-1) {
870 dest[MIN(ret, dest_len-1)] = 0;
877 size_t pull_ascii_pstring(char *dest, const void *src)
879 return pull_ascii(dest, src, sizeof(pstring), -1, STR_TERMINATE);
882 size_t pull_ascii_fstring(char *dest, const void *src)
884 return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
887 size_t pull_ascii_nstring(char *dest, const void *src)
889 return pull_ascii(dest, src, sizeof(nstring), sizeof(nstring), STR_TERMINATE);
893 * Copy a string from a char* src to a unicode destination.
895 * @returns the number of bytes occupied by the string in the destination.
897 * @param flags can have:
900 * <dt>STR_TERMINATE <dd>means include the null termination.
901 * <dt>STR_UPPER <dd>means uppercase in the destination.
902 * <dt>STR_NOALIGN <dd>means don't do alignment.
905 * @param dest_len is the maximum length allowed in the
906 * destination. If dest_len is -1 then no maxiumum is used.
909 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
915 /* treat a pstring as "unlimited" length */
916 if (dest_len == (size_t)-1)
917 dest_len = sizeof(pstring);
919 if (flags & STR_TERMINATE)
920 src_len = (size_t)-1;
922 src_len = strlen(src);
924 if (ucs2_align(base_ptr, dest, flags)) {
926 dest = (void *)((char *)dest + 1);
932 /* ucs2 is always a multiple of 2 bytes */
935 ret = convert_string(CH_UNIX, CH_UCS2, src, src_len, dest, dest_len, True);
936 if (ret == (size_t)-1) {
942 if (flags & STR_UPPER) {
943 smb_ucs2_t *dest_ucs2 = dest;
945 for (i = 0; i < (dest_len / 2) && dest_ucs2[i]; i++) {
946 smb_ucs2_t v = toupper_w(dest_ucs2[i]);
947 if (v != dest_ucs2[i]) {
958 * Copy a string from a unix char* src to a UCS2 destination,
959 * allocating a buffer using talloc().
961 * @param dest always set at least to NULL
963 * @returns The number of bytes occupied by the string in the destination
964 * or -1 in case of error.
966 size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
968 size_t src_len = strlen(src)+1;
971 return convert_string_talloc(ctx, CH_UNIX, CH_UCS2, src, src_len, (void **)dest, True);
976 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
978 * @param dest always set at least to NULL
980 * @returns The number of bytes occupied by the string in the destination
981 * or -1 in case of error.
984 size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
986 size_t src_len = strlen(src)+1;
989 return convert_string_allocate(NULL, CH_UNIX, CH_UCS2, src, src_len, (void **)dest, True);
993 Copy a string from a char* src to a UTF-8 destination.
994 Return the number of bytes occupied by the string in the destination
996 STR_TERMINATE means include the null termination
997 STR_UPPER means uppercase in the destination
998 dest_len is the maximum length allowed in the destination. If dest_len
999 is -1 then no maxiumum is used.
1002 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
1004 size_t src_len = strlen(src);
1007 /* treat a pstring as "unlimited" length */
1008 if (dest_len == (size_t)-1)
1009 dest_len = sizeof(pstring);
1011 if (flags & STR_UPPER) {
1012 pstrcpy(tmpbuf, src);
1017 if (flags & STR_TERMINATE)
1020 return convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len, True);
1023 size_t push_utf8_fstring(void *dest, const char *src)
1025 return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
1029 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1031 * @param dest always set at least to NULL
1033 * @returns The number of bytes occupied by the string in the destination
1036 size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1038 size_t src_len = strlen(src)+1;
1041 return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest, True);
1045 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1047 * @param dest always set at least to NULL
1049 * @returns The number of bytes occupied by the string in the destination
1052 size_t push_utf8_allocate(char **dest, const char *src)
1054 size_t src_len = strlen(src)+1;
1057 return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, True);
1061 Copy a string from a ucs2 source to a unix char* destination.
1063 STR_TERMINATE means the string in src is null terminated.
1064 STR_NOALIGN means don't try to align.
1065 if STR_TERMINATE is set then src_len is ignored if it is -1.
1066 src_len is the length of the source area in bytes
1067 Return the number of bytes occupied by the string in src.
1068 The resulting string in "dest" is always null terminated.
1071 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1075 if (dest_len == (size_t)-1)
1076 dest_len = sizeof(pstring);
1078 if (ucs2_align(base_ptr, src, flags)) {
1079 src = (const void *)((const char *)src + 1);
1084 if (flags & STR_TERMINATE) {
1085 /* src_len -1 is the default for null terminated strings. */
1086 if (src_len != (size_t)-1) {
1087 size_t len = strnlen_w(src, src_len/2);
1088 if (len < src_len/2)
1094 /* ucs2 is always a multiple of 2 bytes */
1095 if (src_len != (size_t)-1)
1098 ret = convert_string(CH_UCS2, CH_UNIX, src, src_len, dest, dest_len, True);
1099 if (ret == (size_t)-1) {
1103 if (src_len == (size_t)-1)
1107 dest[MIN(ret, dest_len-1)] = 0;
1114 size_t pull_ucs2_pstring(char *dest, const void *src)
1116 return pull_ucs2(NULL, dest, src, sizeof(pstring), -1, STR_TERMINATE);
1119 size_t pull_ucs2_fstring(char *dest, const void *src)
1121 return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
1125 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1127 * @param dest always set at least to NULL
1129 * @returns The number of bytes occupied by the string in the destination
1132 size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
1134 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1136 return convert_string_talloc(ctx, CH_UCS2, CH_UNIX, src, src_len, (void **)dest, True);
1140 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1142 * @param dest always set at least to NULL
1144 * @returns The number of bytes occupied by the string in the destination
1147 size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
1149 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1151 return convert_string_allocate(NULL, CH_UCS2, CH_UNIX, src, src_len, (void **)dest, True);
1155 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1157 * @param dest always set at least to NULL
1159 * @returns The number of bytes occupied by the string in the destination
1162 size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1164 size_t src_len = strlen(src)+1;
1166 return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1170 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1172 * @param dest always set at least to NULL
1174 * @returns The number of bytes occupied by the string in the destination
1177 size_t pull_utf8_allocate(char **dest, const char *src)
1179 size_t src_len = strlen(src)+1;
1181 return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1185 Copy a string from a char* src to a unicode or ascii
1186 dos codepage destination choosing unicode or ascii based on the
1187 flags in the SMB buffer starting at base_ptr.
1188 Return the number of bytes occupied by the string in the destination.
1190 STR_TERMINATE means include the null termination.
1191 STR_UPPER means uppercase in the destination.
1192 STR_ASCII use ascii even with unicode packet.
1193 STR_NOALIGN means don't do alignment.
1194 dest_len is the maximum length allowed in the destination. If dest_len
1195 is -1 then no maxiumum is used.
1198 size_t push_string_fn(const char *function, unsigned int line, const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
1201 /* We really need to zero fill here, not clobber
1202 * region, as we want to ensure that valgrind thinks
1203 * all of the outgoing buffer has been written to
1204 * so a send() or write() won't trap an error.
1208 if (dest_len != (size_t)-1)
1209 clobber_region(function, line, dest, dest_len);
1211 if (dest_len != (size_t)-1)
1212 memset(dest, '\0', dest_len);
1216 if (!(flags & STR_ASCII) && \
1217 ((flags & STR_UNICODE || \
1218 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1219 return push_ucs2(base_ptr, dest, src, dest_len, flags);
1221 return push_ascii(dest, src, dest_len, flags);
1226 Copy a string from a unicode or ascii source (depending on
1227 the packet flags) to a char* destination.
1229 STR_TERMINATE means the string in src is null terminated.
1230 STR_UNICODE means to force as unicode.
1231 STR_ASCII use ascii even with unicode packet.
1232 STR_NOALIGN means don't do alignment.
1233 if STR_TERMINATE is set then src_len is ignored is it is -1
1234 src_len is the length of the source area in bytes.
1235 Return the number of bytes occupied by the string in src.
1236 The resulting string in "dest" is always null terminated.
1239 size_t pull_string_fn(const char *function, unsigned int line, const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1242 if (dest_len != (size_t)-1)
1243 clobber_region(function, line, dest, dest_len);
1246 if (!(flags & STR_ASCII) && \
1247 ((flags & STR_UNICODE || \
1248 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1249 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
1251 return pull_ascii(dest, src, dest_len, src_len, flags);
1254 size_t align_string(const void *base_ptr, const char *p, int flags)
1256 if (!(flags & STR_ASCII) && \
1257 ((flags & STR_UNICODE || \
1258 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1259 return ucs2_align(base_ptr, p, flags);