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 && strcasecmp(ret, "LOCALE") == 0) {
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 * @returns the number of bytes occupied in the destination
161 * Ensure the srclen contains the terminating zero.
165 static size_t convert_string_internal(charset_t from, charset_t to,
166 void const *src, size_t srclen,
167 void *dest, size_t destlen)
171 const char* inbuf = (const char*)src;
172 char* outbuf = (char*)dest;
173 smb_iconv_t descriptor;
175 lazy_initialize_conv();
177 descriptor = conv_handles[from][to];
179 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
181 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
187 retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
188 if(retval==(size_t)-1) {
189 const char *reason="unknown error";
192 reason="Incomplete multibyte sequence";
194 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
197 reason="No more room";
199 DEBUG(3, ("convert_string_internal: Required %lu, available %lu\n",
200 (unsigned long)srclen, (unsigned long)destlen));
201 /* we are not sure we need srclen bytes,
202 may be more, may be less.
203 We only know we need more than destlen
207 reason="Illegal multibyte sequence";
209 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
213 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
216 /* smb_panic(reason); */
218 return destlen-o_len;
222 /* conversion not supported, use as is */
224 size_t len = MIN(srclen,destlen);
226 memcpy(dest,src,len);
232 * Convert string from one encoding to another, making error checking etc
233 * Fast path version - handles ASCII first.
235 * @param src pointer to source string (multibyte or singlebyte)
236 * @param srclen length of the source string in bytes
237 * @param dest pointer to destination string (multibyte or singlebyte)
238 * @param destlen maximal length allowed for string
239 * @returns the number of bytes occupied in the destination
241 * Ensure the srclen contains the terminating zero.
243 * This function has been hand-tuned to provide a fast path.
244 * Don't change unless you really know what you are doing. JRA.
247 size_t convert_string(charset_t from, charset_t to,
248 void const *src, size_t srclen,
249 void *dest, size_t destlen)
254 if (from != CH_UCS2 && to != CH_UCS2) {
255 const unsigned char *p = (const unsigned char *)src;
256 unsigned char *q = (unsigned char *)dest;
260 /* If all characters are ascii, fast path here. */
261 while (srclen && destlen) {
262 if ((lastp = *p) <= 0x7f) {
264 if (srclen != (size_t)-1) {
272 if (srclen == (size_t)-1) {
273 srclen = strlen(p)+1;
275 return retval + convert_string_internal(from, to, p, srclen, q, destlen);
279 } else if (from == CH_UCS2 && to != CH_UCS2) {
280 const unsigned char *p = (const unsigned char *)src;
281 unsigned char *q = (unsigned char *)dest;
285 /* If all characters are ascii, fast path here. */
286 while ((srclen >= 2) && destlen) {
287 if ((lastp = *p) <= 0x7f && p[1] == 0) {
289 if (srclen != (size_t)-1) {
298 if (srclen == (size_t)-1) {
299 srclen = strlen_w((const void *)p)+2;
301 return retval + convert_string_internal(from, to, p, srclen, q, destlen);
305 } else if (from != CH_UCS2 && to == CH_UCS2) {
306 const unsigned char *p = (const unsigned char *)src;
307 unsigned char *q = (unsigned char *)dest;
311 /* If all characters are ascii, fast path here. */
312 while (srclen && (destlen >= 2)) {
313 if ((lastp = *p) <= 0x7F) {
316 if (srclen != (size_t)-1) {
324 if (srclen == (size_t)-1) {
325 srclen = strlen(p)+1;
327 return retval + convert_string_internal(from, to, p, srclen, q, destlen);
332 return convert_string_internal(from, to, src, srclen, dest, destlen);
336 * Convert between character sets, allocating a new buffer for the result.
338 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
339 * @param srclen length of source buffer.
340 * @param dest always set at least to NULL
341 * @note -1 is not accepted for srclen.
343 * @returns Size in bytes of the converted string; or -1 in case of error.
345 * Ensure the srclen contains the terminating zero.
348 size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
349 void const *src, size_t srclen, void **dest)
351 size_t i_len, o_len, destlen = MAX(srclen, 512);
353 const char *inbuf = (const char *)src;
354 char *outbuf = NULL, *ob = NULL;
355 smb_iconv_t descriptor;
359 if (src == NULL || srclen == (size_t)-1)
364 lazy_initialize_conv();
366 descriptor = conv_handles[from][to];
368 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
370 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
375 if ((destlen*2) < destlen) {
376 /* wrapped ! abort. */
378 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
383 destlen = destlen * 2;
387 ob = (char *)talloc_realloc(ctx, ob, destlen);
389 ob = (char *)Realloc(ob, destlen);
392 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
401 retval = smb_iconv(descriptor,
404 if(retval == (size_t)-1) {
405 const char *reason="unknown error";
408 reason="Incomplete multibyte sequence";
410 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
415 reason="Illegal multibyte sequence";
417 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
421 DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
422 /* smb_panic(reason); */
426 destlen = destlen - o_len;
428 *dest = (char *)talloc_realloc(ctx,ob,destlen);
430 *dest = (char *)Realloc(ob,destlen);
431 if (destlen && !*dest) {
432 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
442 /* conversion not supported, use as is */
444 if (srclen && (destlen != srclen)) {
447 ob = (char *)talloc_realloc(ctx, ob, destlen);
449 ob = (char *)Realloc(ob, destlen);
451 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
458 memcpy(ob,(const char *)src,srclen);
466 * Convert between character sets, allocating a new buffer using talloc for the result.
468 * @param srclen length of source buffer.
469 * @param dest always set at least to NULL
470 * @note -1 is not accepted for srclen.
472 * @returns Size in bytes of the converted string; or -1 in case of error.
474 static size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
475 void const *src, size_t srclen, void **dest)
480 dest_len=convert_string_allocate(ctx, from, to, src, srclen, dest);
481 if (dest_len == (size_t)-1)
488 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
493 size = push_ucs2_allocate(&buffer, src);
495 smb_panic("failed to create UCS2 buffer");
497 if (!strupper_w(buffer) && (dest == src)) {
502 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen);
508 strdup() a unix string to upper case.
512 char *strdup_upper(const char *s)
515 const unsigned char *p = (const unsigned char *)s;
516 unsigned char *q = (unsigned char *)out_buffer;
518 /* this is quite a common operation, so we want it to be
519 fast. We optimise for the ascii case, knowing that all our
520 supported multi-byte character sets are ascii-compatible
521 (ie. they match for the first 128 chars) */
530 if (p - ( const unsigned char *)s >= sizeof(pstring))
538 size = convert_string(CH_UNIX, CH_UCS2, s, -1, buffer, sizeof(buffer));
545 size = convert_string(CH_UCS2, CH_UNIX, buffer, sizeof(buffer), out_buffer, sizeof(out_buffer));
551 return strdup(out_buffer);
554 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
559 size = convert_string_allocate(NULL, CH_UNIX, CH_UCS2, src, srclen,
562 smb_panic("failed to create UCS2 buffer");
564 if (!strlower_w(buffer) && (dest == src)) {
568 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen);
574 strdup() a unix string to lower case.
577 char *strdup_lower(const char *s)
583 size = push_ucs2_allocate(&buffer, s);
590 size = pull_ucs2_allocate(&out_buffer, buffer);
600 static size_t ucs2_align(const void *base_ptr, const void *p, int flags)
602 if (flags & (STR_NOALIGN|STR_ASCII))
604 return PTR_DIFF(p, base_ptr) & 1;
609 * Copy a string from a char* unix src to a dos codepage string destination.
611 * @return the number of bytes occupied by the string in the destination.
613 * @param flags can include
615 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
616 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
619 * @param dest_len the maximum length in bytes allowed in the
620 * destination. If @p dest_len is -1 then no maximum is used.
622 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
624 size_t src_len = strlen(src);
627 /* treat a pstring as "unlimited" length */
628 if (dest_len == (size_t)-1)
629 dest_len = sizeof(pstring);
631 if (flags & STR_UPPER) {
632 pstrcpy(tmpbuf, src);
637 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
640 return convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len);
643 size_t push_ascii_fstring(void *dest, const char *src)
645 return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
648 size_t push_ascii_pstring(void *dest, const char *src)
650 return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
653 size_t push_ascii_nstring(void *dest, const char *src)
655 return push_ascii(dest, src, sizeof(nstring), STR_TERMINATE);
659 * Copy a string from a dos codepage source to a unix char* destination.
661 * The resulting string in "dest" is always null terminated.
663 * @param flags can have:
665 * <dt>STR_TERMINATE</dt>
666 * <dd>STR_TERMINATE means the string in @p src
667 * is null terminated, and src_len is ignored.</dd>
670 * @param src_len is the length of the source area in bytes.
671 * @returns the number of bytes occupied by the string in @p src.
673 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
677 if (dest_len == (size_t)-1)
678 dest_len = sizeof(pstring);
680 if (flags & STR_TERMINATE) {
681 if (src_len == (size_t)-1) {
682 src_len = strlen(src) + 1;
684 size_t len = strnlen(src, src_len);
691 ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len);
694 dest[MIN(ret, dest_len-1)] = 0;
701 size_t pull_ascii_pstring(char *dest, const void *src)
703 return pull_ascii(dest, src, sizeof(pstring), -1, STR_TERMINATE);
706 size_t pull_ascii_fstring(char *dest, const void *src)
708 return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
711 size_t pull_ascii_nstring(char *dest, const void *src)
713 return pull_ascii(dest, src, sizeof(nstring), sizeof(nstring), STR_TERMINATE);
717 * Copy a string from a char* src to a unicode destination.
719 * @returns the number of bytes occupied by the string in the destination.
721 * @param flags can have:
724 * <dt>STR_TERMINATE <dd>means include the null termination.
725 * <dt>STR_UPPER <dd>means uppercase in the destination.
726 * <dt>STR_NOALIGN <dd>means don't do alignment.
729 * @param dest_len is the maximum length allowed in the
730 * destination. If dest_len is -1 then no maxiumum is used.
733 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
738 /* treat a pstring as "unlimited" length */
739 if (dest_len == (size_t)-1)
740 dest_len = sizeof(pstring);
742 if (flags & STR_TERMINATE)
743 src_len = (size_t)-1;
745 src_len = strlen(src);
747 if (ucs2_align(base_ptr, dest, flags)) {
749 dest = (void *)((char *)dest + 1);
755 /* ucs2 is always a multiple of 2 bytes */
758 len += convert_string(CH_UNIX, CH_UCS2, src, src_len, dest, dest_len);
760 if (flags & STR_UPPER) {
761 smb_ucs2_t *dest_ucs2 = dest;
763 for (i = 0; i < (dest_len / 2) && dest_ucs2[i]; i++) {
764 smb_ucs2_t v = toupper_w(dest_ucs2[i]);
765 if (v != dest_ucs2[i]) {
776 * Copy a string from a unix char* src to a UCS2 destination,
777 * allocating a buffer using talloc().
779 * @param dest always set at least to NULL
781 * @returns The number of bytes occupied by the string in the destination
782 * or -1 in case of error.
784 size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
786 size_t src_len = strlen(src)+1;
789 return convert_string_talloc(ctx, CH_UNIX, CH_UCS2, src, src_len, (void **)dest);
794 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
796 * @param dest always set at least to NULL
798 * @returns The number of bytes occupied by the string in the destination
799 * or -1 in case of error.
802 size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
804 size_t src_len = strlen(src)+1;
807 return convert_string_allocate(NULL, CH_UNIX, CH_UCS2, src, src_len, (void **)dest);
811 Copy a string from a char* src to a UTF-8 destination.
812 Return the number of bytes occupied by the string in the destination
814 STR_TERMINATE means include the null termination
815 STR_UPPER means uppercase in the destination
816 dest_len is the maximum length allowed in the destination. If dest_len
817 is -1 then no maxiumum is used.
820 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
822 size_t src_len = strlen(src);
825 /* treat a pstring as "unlimited" length */
826 if (dest_len == (size_t)-1)
827 dest_len = sizeof(pstring);
829 if (flags & STR_UPPER) {
830 pstrcpy(tmpbuf, src);
835 if (flags & STR_TERMINATE)
838 return convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len);
841 size_t push_utf8_fstring(void *dest, const char *src)
843 return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
847 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
849 * @param dest always set at least to NULL
851 * @returns The number of bytes occupied by the string in the destination
854 size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
856 size_t src_len = strlen(src)+1;
859 return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest);
863 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
865 * @param dest always set at least to NULL
867 * @returns The number of bytes occupied by the string in the destination
870 size_t push_utf8_allocate(char **dest, const char *src)
872 size_t src_len = strlen(src)+1;
875 return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, (void **)dest);
879 Copy a string from a ucs2 source to a unix char* destination.
881 STR_TERMINATE means the string in src is null terminated.
882 STR_NOALIGN means don't try to align.
883 if STR_TERMINATE is set then src_len is ignored if it is -1.
884 src_len is the length of the source area in bytes
885 Return the number of bytes occupied by the string in src.
886 The resulting string in "dest" is always null terminated.
889 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
893 if (dest_len == (size_t)-1)
894 dest_len = sizeof(pstring);
896 if (ucs2_align(base_ptr, src, flags)) {
897 src = (const void *)((const char *)src + 1);
902 if (flags & STR_TERMINATE) {
903 /* src_len -1 is the default for null terminated strings. */
904 if (src_len != (size_t)-1) {
905 size_t len = strnlen_w(src, src_len/2);
912 /* ucs2 is always a multiple of 2 bytes */
913 if (src_len != (size_t)-1)
916 ret = convert_string(CH_UCS2, CH_UNIX, src, src_len, dest, dest_len);
918 dest[MIN(ret, dest_len-1)] = 0;
925 size_t pull_ucs2_pstring(char *dest, const void *src)
927 return pull_ucs2(NULL, dest, src, sizeof(pstring), -1, STR_TERMINATE);
930 size_t pull_ucs2_fstring(char *dest, const void *src)
932 return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
936 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
938 * @param dest always set at least to NULL
940 * @returns The number of bytes occupied by the string in the destination
943 size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
945 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
947 return convert_string_talloc(ctx, CH_UCS2, CH_UNIX, src, src_len, (void **)dest);
951 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
953 * @param dest always set at least to NULL
955 * @returns The number of bytes occupied by the string in the destination
958 size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
960 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
962 return convert_string_allocate(NULL, CH_UCS2, CH_UNIX, src, src_len, (void **)dest);
966 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
968 * @param dest always set at least to NULL
970 * @returns The number of bytes occupied by the string in the destination
973 size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
975 size_t src_len = strlen(src)+1;
977 return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest);
981 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
983 * @param dest always set at least to NULL
985 * @returns The number of bytes occupied by the string in the destination
988 size_t pull_utf8_allocate(void **dest, const char *src)
990 size_t src_len = strlen(src)+1;
992 return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, dest);
996 Copy a string from a char* src to a unicode or ascii
997 dos codepage destination choosing unicode or ascii based on the
998 flags in the SMB buffer starting at base_ptr.
999 Return the number of bytes occupied by the string in the destination.
1001 STR_TERMINATE means include the null termination.
1002 STR_UPPER means uppercase in the destination.
1003 STR_ASCII use ascii even with unicode packet.
1004 STR_NOALIGN means don't do alignment.
1005 dest_len is the maximum length allowed in the destination. If dest_len
1006 is -1 then no maxiumum is used.
1009 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)
1012 /* We really need to zero fill here, not clobber
1013 * region, as we want to ensure that valgrind thinks
1014 * all of the outgoing buffer has been written to
1015 * so a send() or write() won't trap an error.
1019 if (dest_len != (size_t)-1)
1020 clobber_region(function, line, dest, dest_len);
1022 if (dest_len != (size_t)-1)
1023 memset(dest, '\0', dest_len);
1027 if (!(flags & STR_ASCII) && \
1028 ((flags & STR_UNICODE || \
1029 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1030 return push_ucs2(base_ptr, dest, src, dest_len, flags);
1032 return push_ascii(dest, src, dest_len, flags);
1037 Copy a string from a unicode or ascii source (depending on
1038 the packet flags) to a char* destination.
1040 STR_TERMINATE means the string in src is null terminated.
1041 STR_UNICODE means to force as unicode.
1042 STR_ASCII use ascii even with unicode packet.
1043 STR_NOALIGN means don't do alignment.
1044 if STR_TERMINATE is set then src_len is ignored is it is -1
1045 src_len is the length of the source area in bytes.
1046 Return the number of bytes occupied by the string in src.
1047 The resulting string in "dest" is always null terminated.
1050 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)
1053 if (dest_len != (size_t)-1)
1054 clobber_region(function, line, dest, dest_len);
1057 if (!(flags & STR_ASCII) && \
1058 ((flags & STR_UNICODE || \
1059 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1060 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
1062 return pull_ascii(dest, src, dest_len, src_len, flags);
1065 size_t align_string(const void *base_ptr, const char *p, int flags)
1067 if (!(flags & STR_ASCII) && \
1068 ((flags & STR_UNICODE || \
1069 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1070 return ucs2_align(base_ptr, p, flags);