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/>.
25 /* We can parameterize this if someone complains.... JRA. */
27 char lp_failed_convert_char(void)
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.
48 static bool conv_silent
; /* Should we do a debug if the conversion fails ? */
49 static bool initialized
;
51 void lazy_initialize_conv(void)
54 load_case_tables_library();
61 * Destroy global objects allocated by init_iconv()
63 void gfree_charcnv(void)
65 TALLOC_FREE(global_iconv_convenience
);
70 * Initialize iconv conversion descriptors.
72 * This is called the first time it is needed, and also called again
73 * every time the configuration is reloaded, because the charset or
74 * codepage might have changed.
78 global_iconv_convenience
= smb_iconv_convenience_reinit(NULL
, lp_dos_charset(),
79 lp_unix_charset(), lp_display_charset(),
80 true, global_iconv_convenience
);
84 * Convert string from one encoding to another, making error checking etc
85 * Slow path version - uses (slow) iconv.
87 * @param src pointer to source string (multibyte or singlebyte)
88 * @param srclen length of the source string in bytes
89 * @param dest pointer to destination string (multibyte or singlebyte)
90 * @param destlen maximal length allowed for string
91 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
92 * @returns the number of bytes occupied in the destination
94 * Ensure the srclen contains the terminating zero.
98 static size_t convert_string_internal(charset_t from
, charset_t to
,
99 void const *src
, size_t srclen
,
100 void *dest
, size_t destlen
, bool allow_bad_conv
)
104 const char* inbuf
= (const char*)src
;
105 char* outbuf
= (char*)dest
;
106 smb_iconv_t descriptor
;
107 struct smb_iconv_convenience
*ic
;
109 lazy_initialize_conv();
110 ic
= get_iconv_convenience();
111 descriptor
= get_conv_handle(ic
, from
, to
);
113 if (srclen
== (size_t)-1) {
114 if (from
== CH_UTF16LE
|| from
== CH_UTF16BE
) {
115 srclen
= (strlen_w((const smb_ucs2_t
*)src
)+1) * 2;
117 srclen
= strlen((const char *)src
)+1;
122 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
124 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
133 retval
= smb_iconv(descriptor
, &inbuf
, &i_len
, &outbuf
, &o_len
);
134 if(retval
==(size_t)-1) {
135 const char *reason
="unknown error";
138 reason
="Incomplete multibyte sequence";
140 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
145 reason
="No more room";
147 if (from
== CH_UNIX
) {
148 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
149 charset_name(ic
, from
), charset_name(ic
, to
),
150 (unsigned int)srclen
, (unsigned int)destlen
, (const char *)src
));
152 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
153 charset_name(ic
, from
), charset_name(ic
, to
),
154 (unsigned int)srclen
, (unsigned int)destlen
));
159 reason
="Illegal multibyte sequence";
161 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
168 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
171 /* smb_panic(reason); */
173 return destlen
-o_len
;
178 * Conversion not supported. This is actually an error, but there are so
179 * many misconfigured iconv systems and smb.conf's out there we can't just
180 * fail. Do a very bad conversion instead.... JRA.
184 if (o_len
== 0 || i_len
== 0)
185 return destlen
- o_len
;
187 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
188 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
189 /* Can't convert from utf16 any endian to multibyte.
190 Replace with the default fail char.
193 return destlen
- o_len
;
195 *outbuf
= lp_failed_convert_char();
204 if (o_len
== 0 || i_len
== 0)
205 return destlen
- o_len
;
207 /* Keep trying with the next char... */
210 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
211 /* Can't convert to UTF16LE - just widen by adding the
212 default fail char then zero.
215 return destlen
- o_len
;
217 outbuf
[0] = lp_failed_convert_char();
226 if (o_len
== 0 || i_len
== 0)
227 return destlen
- o_len
;
229 /* Keep trying with the next char... */
232 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
233 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
234 /* Failed multibyte to multibyte. Just copy the default fail char and
236 outbuf
[0] = lp_failed_convert_char();
244 if (o_len
== 0 || i_len
== 0)
245 return destlen
- o_len
;
247 /* Keep trying with the next char... */
251 /* Keep compiler happy.... */
252 return destlen
- o_len
;
258 * Convert string from one encoding to another, making error checking etc
259 * Fast path version - handles ASCII first.
261 * @param src pointer to source string (multibyte or singlebyte)
262 * @param srclen length of the source string in bytes, or -1 for nul terminated.
263 * @param dest pointer to destination string (multibyte or singlebyte)
264 * @param destlen maximal length allowed for string - *NEVER* -1.
265 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
266 * @returns the number of bytes occupied in the destination
268 * Ensure the srclen contains the terminating zero.
270 * This function has been hand-tuned to provide a fast path.
271 * Don't change unless you really know what you are doing. JRA.
274 size_t convert_string(charset_t from
, charset_t to
,
275 void const *src
, size_t srclen
,
276 void *dest
, size_t destlen
, bool allow_bad_conv
)
279 * NB. We deliberately don't do a strlen here if srclen == -1.
280 * This is very expensive over millions of calls and is taken
281 * care of in the slow path in convert_string_internal. JRA.
285 SMB_ASSERT(destlen
!= (size_t)-1);
291 if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
292 const unsigned char *p
= (const unsigned char *)src
;
293 unsigned char *q
= (unsigned char *)dest
;
294 size_t slen
= srclen
;
295 size_t dlen
= destlen
;
296 unsigned char lastp
= '\0';
299 /* If all characters are ascii, fast path here. */
300 while (slen
&& dlen
) {
301 if ((lastp
= *p
) <= 0x7f) {
303 if (slen
!= (size_t)-1) {
311 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
314 size_t ret
= convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
315 if (ret
== (size_t)-1) {
323 /* Even if we fast path we should note if we ran out of room. */
324 if (((slen
!= (size_t)-1) && slen
) ||
325 ((slen
== (size_t)-1) && lastp
)) {
330 } else if (from
== CH_UTF16LE
&& to
!= CH_UTF16LE
) {
331 const unsigned char *p
= (const unsigned char *)src
;
332 unsigned char *q
= (unsigned char *)dest
;
334 size_t slen
= srclen
;
335 size_t dlen
= destlen
;
336 unsigned char lastp
= '\0';
338 /* If all characters are ascii, fast path here. */
339 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
340 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
342 if (slen
!= (size_t)-1) {
351 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
354 size_t ret
= convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
355 if (ret
== (size_t)-1) {
363 /* Even if we fast path we should note if we ran out of room. */
364 if (((slen
!= (size_t)-1) && slen
) ||
365 ((slen
== (size_t)-1) && lastp
)) {
370 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
371 const unsigned char *p
= (const unsigned char *)src
;
372 unsigned char *q
= (unsigned char *)dest
;
374 size_t slen
= srclen
;
375 size_t dlen
= destlen
;
376 unsigned char lastp
= '\0';
378 /* If all characters are ascii, fast path here. */
379 while (slen
&& (dlen
>= 2)) {
380 if ((lastp
= *p
) <= 0x7F) {
383 if (slen
!= (size_t)-1) {
391 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
394 size_t ret
= convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
395 if (ret
== (size_t)-1) {
403 /* Even if we fast path we should note if we ran out of room. */
404 if (((slen
!= (size_t)-1) && slen
) ||
405 ((slen
== (size_t)-1) && lastp
)) {
412 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
415 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
419 * Convert between character sets, allocating a new buffer using talloc for the result.
421 * @param srclen length of source buffer.
422 * @param dest always set at least to NULL
423 * @parm converted_size set to the number of bytes occupied by the string in
424 * the destination on success.
425 * @note -1 is not accepted for srclen.
427 * @return true if new buffer was correctly allocated, and string was
430 * Ensure the srclen contains the terminating zero.
432 * I hate the goto's in this function. It's embarressing.....
433 * There has to be a cleaner way to do this. JRA.
435 bool convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
436 void const *src
, size_t srclen
, void *dst
,
437 size_t *converted_size
, bool allow_bad_conv
)
440 size_t i_len
, o_len
, destlen
= (srclen
* 3) / 2;
442 const char *inbuf
= (const char *)src
;
443 char *outbuf
= NULL
, *ob
= NULL
;
444 smb_iconv_t descriptor
;
445 void **dest
= (void **)dst
;
446 struct smb_iconv_convenience
*ic
;
450 if (!converted_size
) {
455 if (src
== NULL
|| srclen
== (size_t)-1) {
461 /* We really should treat this as an error, but
462 there are too many callers that need this to
463 return a NULL terminated string in the correct
465 if (to
== CH_UTF16LE
|| to
== CH_UTF16BE
|| to
== CH_UTF16MUNGED
) {
470 ob
= talloc_zero_array(ctx
, char, destlen
);
475 *converted_size
= destlen
;
480 lazy_initialize_conv();
481 ic
= get_iconv_convenience();
482 descriptor
= get_conv_handle(ic
, from
, to
);
484 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
486 DEBUG(0,("convert_string_talloc: Conversion not supported.\n"));
493 /* +2 is for ucs2 null termination. */
494 if ((destlen
*2)+2 < destlen
) {
495 /* wrapped ! abort. */
497 DEBUG(0, ("convert_string_talloc: destlen wrapped !\n"));
502 destlen
= destlen
* 2;
505 /* +2 is for ucs2 null termination. */
506 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
+ 2);
509 DEBUG(0, ("convert_string_talloc: realloc failed!\n"));
519 retval
= smb_iconv(descriptor
,
522 if(retval
== (size_t)-1) {
523 const char *reason
="unknown error";
526 reason
="Incomplete multibyte sequence";
528 DEBUG(3,("convert_string_talloc: Conversion error: %s(%s)\n",reason
,inbuf
));
535 reason
="Illegal multibyte sequence";
537 DEBUG(3,("convert_string_talloc: Conversion error: %s(%s)\n",reason
,inbuf
));
543 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
544 /* smb_panic(reason); */
551 destlen
= destlen
- o_len
;
552 /* Don't shrink unless we're reclaiming a lot of
553 * space. This is in the hot codepath and these
554 * reallocs *cost*. JRA.
557 /* We're shrinking here so we know the +2 is safe from wrap. */
558 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
+ 2);
561 if (destlen
&& !ob
) {
562 DEBUG(0, ("convert_string_talloc: out of memory!\n"));
569 /* Must ucs2 null terminate in the extra space we allocated. */
571 ob
[destlen
+1] = '\0';
573 /* Ensure we can never return a *converted_size of zero. */
575 /* This can happen from a bad iconv "use_as_is:" call. */
576 if (to
== CH_UTF16LE
|| to
== CH_UTF16BE
|| to
== CH_UTF16MUNGED
) {
583 *converted_size
= destlen
;
589 * Conversion not supported. This is actually an error, but there are so
590 * many misconfigured iconv systems and smb.conf's out there we can't just
591 * fail. Do a very bad conversion instead.... JRA.
595 if (o_len
== 0 || i_len
== 0)
598 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
599 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
600 /* Can't convert from utf16 any endian to multibyte.
601 Replace with the default fail char.
608 *outbuf
= lp_failed_convert_char();
617 if (o_len
== 0 || i_len
== 0)
620 /* Keep trying with the next char... */
623 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
624 /* Can't convert to UTF16LE - just widen by adding the
625 default fail char then zero.
630 outbuf
[0] = lp_failed_convert_char();
639 if (o_len
== 0 || i_len
== 0)
642 /* Keep trying with the next char... */
645 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
646 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
647 /* Failed multibyte to multibyte. Just copy the default fail char and
649 outbuf
[0] = lp_failed_convert_char();
657 if (o_len
== 0 || i_len
== 0)
660 /* Keep trying with the next char... */
664 /* Keep compiler happy.... */
670 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
675 if (!push_ucs2_talloc(talloc_tos(), &buffer
, src
, &size
)) {
679 if (!strupper_w(buffer
) && (dest
== src
)) {
684 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
690 talloc_strdup() a unix string to upper case.
693 char *talloc_strdup_upper(TALLOC_CTX
*ctx
, const char *s
)
695 char *out_buffer
= talloc_strdup(ctx
,s
);
696 const unsigned char *p
= (const unsigned char *)s
;
697 unsigned char *q
= (unsigned char *)out_buffer
;
703 /* this is quite a common operation, so we want it to be
704 fast. We optimise for the ascii case, knowing that all our
705 supported multi-byte character sets are ascii-compatible
706 (ie. they match for the first 128 chars) */
711 *q
++ = toupper_ascii_fast(*p
);
717 size_t converted_size
, converted_size2
;
718 smb_ucs2_t
*ubuf
= NULL
;
720 /* We're not using the ascii buffer above. */
721 TALLOC_FREE(out_buffer
);
723 if (!convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, s
,
724 strlen(s
)+1, (void *)&ubuf
,
725 &converted_size
, True
))
732 if (!convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, ubuf
,
733 converted_size
, (void *)&out_buffer
,
734 &converted_size2
, True
))
740 /* Don't need the intermediate buffer
749 char *strupper_talloc(TALLOC_CTX
*ctx
, const char *s
) {
750 return talloc_strdup_upper(ctx
, s
);
754 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
757 smb_ucs2_t
*buffer
= NULL
;
759 if (!convert_string_talloc(talloc_tos(), CH_UNIX
, CH_UTF16LE
, src
, srclen
,
760 (void **)(void *)&buffer
, &size
,
763 smb_panic("failed to create UCS2 buffer");
765 if (!strlower_w(buffer
) && (dest
== src
)) {
769 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
775 char *talloc_strdup_lower(TALLOC_CTX
*ctx
, const char *s
)
777 size_t converted_size
;
778 smb_ucs2_t
*buffer
= NULL
;
781 if (!push_ucs2_talloc(ctx
, &buffer
, s
, &converted_size
)) {
787 if (!pull_ucs2_talloc(ctx
, &out_buffer
, buffer
, &converted_size
)) {
797 char *strlower_talloc(TALLOC_CTX
*ctx
, const char *s
) {
798 return talloc_strdup_lower(ctx
, s
);
801 size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
803 if (flags
& (STR_NOALIGN
|STR_ASCII
))
805 return PTR_DIFF(p
, base_ptr
) & 1;
810 * Copy a string from a char* unix src to a dos codepage string destination.
812 * @return the number of bytes occupied by the string in the destination.
814 * @param flags can include
816 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
817 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
820 * @param dest_len the maximum length in bytes allowed in the
823 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
825 size_t src_len
= strlen(src
);
829 /* No longer allow a length of -1. */
830 if (dest_len
== (size_t)-1) {
831 smb_panic("push_ascii - dest_len == -1");
834 if (flags
& STR_UPPER
) {
835 tmpbuf
= SMB_STRDUP(src
);
837 smb_panic("malloc fail");
843 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
)) {
847 ret
= convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
848 if (ret
== (size_t)-1 &&
849 (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
851 ((char *)dest
)[0] = '\0';
857 size_t push_ascii_fstring(void *dest
, const char *src
)
859 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
862 /********************************************************************
863 Push an nstring - ensure null terminated. Written by
864 moriyama@miraclelinux.com (MORIYAMA Masayuki).
865 ********************************************************************/
867 size_t push_ascii_nstring(void *dest
, const char *src
)
869 size_t i
, buffer_len
, dest_len
;
873 if (!push_ucs2_talloc(talloc_tos(), &buffer
, src
, &buffer_len
)) {
874 smb_panic("failed to create UCS2 buffer");
877 /* We're using buffer_len below to count ucs2 characters, not bytes. */
878 buffer_len
/= sizeof(smb_ucs2_t
);
881 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
882 unsigned char mb
[10];
883 /* Convert one smb_ucs2_t character at a time. */
884 size_t mb_len
= convert_string(CH_UTF16LE
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
885 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
886 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
893 ((char *)dest
)[dest_len
] = '\0';
900 /********************************************************************
901 Push and malloc an ascii string. src and dest null terminated.
902 ********************************************************************/
904 bool push_ascii_talloc(TALLOC_CTX
*mem_ctx
, char **dest
, const char *src
, size_t *converted_size
)
906 size_t src_len
= strlen(src
)+1;
909 return convert_string_talloc(mem_ctx
, CH_UNIX
, CH_DOS
, src
, src_len
,
910 (void **)dest
, converted_size
, True
);
914 * Copy a string from a dos codepage source to a unix char* destination.
916 * The resulting string in "dest" is always null terminated.
918 * @param flags can have:
920 * <dt>STR_TERMINATE</dt>
921 * <dd>STR_TERMINATE means the string in @p src
922 * is null terminated, and src_len is ignored.</dd>
925 * @param src_len is the length of the source area in bytes.
926 * @returns the number of bytes occupied by the string in @p src.
928 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
932 if (dest_len
== (size_t)-1) {
933 /* No longer allow dest_len of -1. */
934 smb_panic("pull_ascii - invalid dest_len of -1");
937 if (flags
& STR_TERMINATE
) {
938 if (src_len
== (size_t)-1) {
939 src_len
= strlen((const char *)src
) + 1;
941 size_t len
= strnlen((const char *)src
, src_len
);
948 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
949 if (ret
== (size_t)-1) {
954 if (dest_len
&& ret
) {
955 /* Did we already process the terminating zero ? */
956 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
957 dest
[MIN(ret
, dest_len
-1)] = 0;
967 * Copy a string from a dos codepage source to a unix char* destination.
970 * The resulting string in "dest" is always null terminated.
972 * @param flags can have:
974 * <dt>STR_TERMINATE</dt>
975 * <dd>STR_TERMINATE means the string in @p src
976 * is null terminated, and src_len is ignored.</dd>
979 * @param src_len is the length of the source area in bytes.
980 * @returns the number of bytes occupied by the string in @p src.
983 static size_t pull_ascii_base_talloc(TALLOC_CTX
*ctx
,
998 if (flags
& STR_TERMINATE
) {
999 if (src_len
== (size_t)-1) {
1000 src_len
= strlen((const char *)src
) + 1;
1002 size_t len
= strnlen((const char *)src
, src_len
);
1007 /* Ensure we don't use an insane length from the client. */
1008 if (src_len
>= 1024*1024) {
1009 char *msg
= talloc_asprintf(ctx
,
1010 "Bad src length (%u) in "
1011 "pull_ascii_base_talloc",
1012 (unsigned int)src_len
);
1016 /* Can't have an unlimited length
1017 * non STR_TERMINATE'd.
1019 if (src_len
== (size_t)-1) {
1025 /* src_len != -1 here. */
1027 if (!convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, &dest
,
1032 if (dest_len
&& dest
) {
1033 /* Did we already process the terminating zero ? */
1034 if (dest
[dest_len
-1] != 0) {
1035 size_t size
= talloc_get_size(dest
);
1036 /* Have we got space to append the '\0' ? */
1037 if (size
<= dest_len
) {
1039 dest
= TALLOC_REALLOC_ARRAY(ctx
, dest
, char,
1043 dest_len
= (size_t)-1;
1048 dest
[dest_len
] = '\0';
1059 size_t pull_ascii_fstring(char *dest
, const void *src
)
1061 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1064 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1066 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
1068 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
1072 * Copy a string from a char* src to a unicode destination.
1074 * @returns the number of bytes occupied by the string in the destination.
1076 * @param flags can have:
1079 * <dt>STR_TERMINATE <dd>means include the null termination.
1080 * <dt>STR_UPPER <dd>means uppercase in the destination.
1081 * <dt>STR_NOALIGN <dd>means don't do alignment.
1084 * @param dest_len is the maximum length allowed in the
1088 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1094 if (dest_len
== (size_t)-1) {
1095 /* No longer allow dest_len of -1. */
1096 smb_panic("push_ucs2 - invalid dest_len of -1");
1099 if (flags
& STR_TERMINATE
)
1100 src_len
= (size_t)-1;
1102 src_len
= strlen(src
);
1104 if (ucs2_align(base_ptr
, dest
, flags
)) {
1106 dest
= (void *)((char *)dest
+ 1);
1112 /* ucs2 is always a multiple of 2 bytes */
1115 ret
= convert_string(CH_UNIX
, CH_UTF16LE
, src
, src_len
, dest
, dest_len
, True
);
1116 if (ret
== (size_t)-1) {
1117 if ((flags
& STR_TERMINATE
) &&
1127 if (flags
& STR_UPPER
) {
1128 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1131 /* We check for i < (ret / 2) below as the dest string isn't null
1132 terminated if STR_TERMINATE isn't set. */
1134 for (i
= 0; i
< (ret
/ 2) && i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1135 smb_ucs2_t v
= toupper_m(dest_ucs2
[i
]);
1136 if (v
!= dest_ucs2
[i
]) {
1147 * Copy a string from a unix char* src to a UCS2 destination,
1148 * allocating a buffer using talloc().
1150 * @param dest always set at least to NULL
1151 * @parm converted_size set to the number of bytes occupied by the string in
1152 * the destination on success.
1154 * @return true if new buffer was correctly allocated, and string was
1157 bool push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
,
1158 size_t *converted_size
)
1160 size_t src_len
= strlen(src
)+1;
1163 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, src
, src_len
,
1164 (void **)dest
, converted_size
, True
);
1169 Copy a string from a char* src to a UTF-8 destination.
1170 Return the number of bytes occupied by the string in the destination
1172 STR_TERMINATE means include the null termination
1173 STR_UPPER means uppercase in the destination
1174 dest_len is the maximum length allowed in the destination. If dest_len
1175 is -1 then no maxiumum is used.
1178 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1182 char *tmpbuf
= NULL
;
1184 if (dest_len
== (size_t)-1) {
1185 /* No longer allow dest_len of -1. */
1186 smb_panic("push_utf8 - invalid dest_len of -1");
1189 if (flags
& STR_UPPER
) {
1190 tmpbuf
= strupper_talloc(talloc_tos(), src
);
1195 src_len
= strlen(src
);
1198 src_len
= strlen(src
);
1199 if (flags
& STR_TERMINATE
) {
1203 ret
= convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1204 TALLOC_FREE(tmpbuf
);
1208 size_t push_utf8_fstring(void *dest
, const char *src
)
1210 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1214 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1216 * @param dest always set at least to NULL
1217 * @parm converted_size set to the number of bytes occupied by the string in
1218 * the destination on success.
1220 * @return true if new buffer was correctly allocated, and string was
1224 bool push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
,
1225 size_t *converted_size
)
1227 size_t src_len
= strlen(src
)+1;
1230 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
,
1231 (void**)dest
, converted_size
, True
);
1235 Copy a string from a ucs2 source to a unix char* destination.
1237 STR_TERMINATE means the string in src is null terminated.
1238 STR_NOALIGN means don't try to align.
1239 if STR_TERMINATE is set then src_len is ignored if it is -1.
1240 src_len is the length of the source area in bytes
1241 Return the number of bytes occupied by the string in src.
1242 The resulting string in "dest" is always null terminated.
1245 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1248 size_t ucs2_align_len
= 0;
1250 if (dest_len
== (size_t)-1) {
1251 /* No longer allow dest_len of -1. */
1252 smb_panic("pull_ucs2 - invalid dest_len of -1");
1256 if (dest
&& dest_len
> 0) {
1262 if (ucs2_align(base_ptr
, src
, flags
)) {
1263 src
= (const void *)((const char *)src
+ 1);
1264 if (src_len
!= (size_t)-1)
1269 if (flags
& STR_TERMINATE
) {
1270 /* src_len -1 is the default for null terminated strings. */
1271 if (src_len
!= (size_t)-1) {
1272 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1274 if (len
< src_len
/2)
1280 /* ucs2 is always a multiple of 2 bytes */
1281 if (src_len
!= (size_t)-1)
1284 ret
= convert_string(CH_UTF16LE
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1285 if (ret
== (size_t)-1) {
1290 if (src_len
== (size_t)-1)
1293 if (dest_len
&& ret
) {
1294 /* Did we already process the terminating zero ? */
1295 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1296 dest
[MIN(ret
, dest_len
-1)] = 0;
1302 return src_len
+ ucs2_align_len
;
1306 Copy a string from a ucs2 source to a unix char* destination.
1307 Talloc version with a base pointer.
1308 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1311 STR_TERMINATE means the string in src is null terminated.
1312 STR_NOALIGN means don't try to align.
1313 if STR_TERMINATE is set then src_len is ignored if it is -1.
1314 src_len is the length of the source area in bytes
1315 Return the number of bytes occupied by the string in src.
1316 The resulting string in "dest" is always null terminated.
1319 size_t pull_ucs2_base_talloc(TALLOC_CTX
*ctx
,
1320 const void *base_ptr
,
1328 size_t ucs2_align_len
= 0;
1333 /* Ensure we never use the braindead "malloc" varient. */
1335 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
1343 if (ucs2_align(base_ptr
, src
, flags
)) {
1344 src
= (const void *)((const char *)src
+ 1);
1345 if (src_len
!= (size_t)-1)
1350 if (flags
& STR_TERMINATE
) {
1351 /* src_len -1 is the default for null terminated strings. */
1352 if (src_len
!= (size_t)-1) {
1353 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1355 if (len
< src_len
/2)
1360 * src_len == -1 - alloc interface won't take this
1361 * so we must calculate.
1363 src_len
= (strlen_w((const smb_ucs2_t
*)src
)+1)*sizeof(smb_ucs2_t
);
1365 /* Ensure we don't use an insane length from the client. */
1366 if (src_len
>= 1024*1024) {
1367 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1370 /* Can't have an unlimited length
1371 * non STR_TERMINATE'd.
1373 if (src_len
== (size_t)-1) {
1379 /* src_len != -1 here. */
1381 /* ucs2 is always a multiple of 2 bytes */
1384 if (!convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
,
1385 (void *)&dest
, &dest_len
, True
)) {
1390 /* Did we already process the terminating zero ? */
1391 if (dest
[dest_len
-1] != 0) {
1392 size_t size
= talloc_get_size(dest
);
1393 /* Have we got space to append the '\0' ? */
1394 if (size
<= dest_len
) {
1396 dest
= TALLOC_REALLOC_ARRAY(ctx
, dest
, char,
1400 dest_len
= (size_t)-1;
1405 dest
[dest_len
] = '\0';
1413 return src_len
+ ucs2_align_len
;
1416 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1418 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1422 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1424 * @param dest always set at least to NULL
1425 * @parm converted_size set to the number of bytes occupied by the string in
1426 * the destination on success.
1428 * @return true if new buffer was correctly allocated, and string was
1432 bool pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
,
1433 size_t *converted_size
)
1435 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1438 return convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
,
1439 (void **)dest
, converted_size
, True
);
1443 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1445 * @param dest always set at least to NULL
1446 * @parm converted_size set to the number of bytes occupied by the string in
1447 * the destination on success.
1449 * @return true if new buffer was correctly allocated, and string was
1453 bool pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
,
1454 size_t *converted_size
)
1456 size_t src_len
= strlen(src
)+1;
1459 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
,
1460 (void **)dest
, converted_size
, True
);
1465 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1467 * @param dest always set at least to NULL
1468 * @parm converted_size set to the number of bytes occupied by the string in
1469 * the destination on success.
1471 * @return true if new buffer was correctly allocated, and string was
1475 bool pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
,
1476 size_t *converted_size
)
1478 size_t src_len
= strlen(src
)+1;
1481 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
,
1482 (void **)dest
, converted_size
, True
);
1486 Copy a string from a char* src to a unicode or ascii
1487 dos codepage destination choosing unicode or ascii based on the
1489 Return the number of bytes occupied by the string in the destination.
1491 STR_TERMINATE means include the null termination.
1492 STR_UPPER means uppercase in the destination.
1493 STR_ASCII use ascii even with unicode packet.
1494 STR_NOALIGN means don't do alignment.
1495 dest_len is the maximum length allowed in the destination. If dest_len
1496 is -1 then no maxiumum is used.
1499 size_t push_string_check_fn(const char *function
, unsigned int line
,
1500 void *dest
, const char *src
,
1501 size_t dest_len
, int flags
)
1504 /* We really need to zero fill here, not clobber
1505 * region, as we want to ensure that valgrind thinks
1506 * all of the outgoing buffer has been written to
1507 * so a send() or write() won't trap an error.
1511 clobber_region(function
, line
, dest
, dest_len
);
1513 memset(dest
, '\0', dest_len
);
1517 if (!(flags
& STR_ASCII
) && (flags
& STR_UNICODE
)) {
1518 return push_ucs2(NULL
, dest
, src
, dest_len
, flags
);
1520 return push_ascii(dest
, src
, dest_len
, flags
);
1525 Copy a string from a char* src to a unicode or ascii
1526 dos codepage destination choosing unicode or ascii based on the
1527 flags in the SMB buffer starting at base_ptr.
1528 Return the number of bytes occupied by the string in the destination.
1530 STR_TERMINATE means include the null termination.
1531 STR_UPPER means uppercase in the destination.
1532 STR_ASCII use ascii even with unicode packet.
1533 STR_NOALIGN means don't do alignment.
1534 dest_len is the maximum length allowed in the destination. If dest_len
1535 is -1 then no maxiumum is used.
1538 size_t push_string_base(const char *function
, unsigned int line
,
1539 const char *base
, uint16 flags2
,
1540 void *dest
, const char *src
,
1541 size_t dest_len
, int flags
)
1544 /* We really need to zero fill here, not clobber
1545 * region, as we want to ensure that valgrind thinks
1546 * all of the outgoing buffer has been written to
1547 * so a send() or write() won't trap an error.
1551 clobber_region(function
, line
, dest
, dest_len
);
1553 memset(dest
, '\0', dest_len
);
1557 if (!(flags
& STR_ASCII
) && \
1558 ((flags
& STR_UNICODE
|| \
1559 (flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1560 return push_ucs2(base
, dest
, src
, dest_len
, flags
);
1562 return push_ascii(dest
, src
, dest_len
, flags
);
1566 Copy a string from a char* src to a unicode or ascii
1567 dos codepage destination choosing unicode or ascii based on the
1569 Return the number of bytes occupied by the string in the destination.
1571 STR_TERMINATE means include the null termination.
1572 STR_UPPER means uppercase in the destination.
1573 STR_ASCII use ascii even with unicode packet.
1574 STR_NOALIGN means don't do alignment.
1575 dest_len is the maximum length allowed in the destination. If dest_len
1576 is -1 then no maxiumum is used.
1579 ssize_t
push_string(void *dest
, const char *src
, size_t dest_len
, int flags
)
1583 /* We really need to zero fill here, not clobber
1584 * region, as we want to ensure that valgrind thinks
1585 * all of the outgoing buffer has been written to
1586 * so a send() or write() won't trap an error.
1589 memset(dest
, '\0', dest_len
);
1592 if (!(flags
& STR_ASCII
) && \
1593 (flags
& STR_UNICODE
)) {
1594 ret
= push_ucs2(NULL
, dest
, src
, dest_len
, flags
);
1596 ret
= push_ascii(dest
, src
, dest_len
, flags
);
1598 if (ret
== (size_t)-1) {
1605 Copy a string from a unicode or ascii source (depending on
1606 the packet flags) to a char* destination.
1608 STR_TERMINATE means the string in src is null terminated.
1609 STR_UNICODE means to force as unicode.
1610 STR_ASCII use ascii even with unicode packet.
1611 STR_NOALIGN means don't do alignment.
1612 if STR_TERMINATE is set then src_len is ignored is it is -1
1613 src_len is the length of the source area in bytes.
1614 Return the number of bytes occupied by the string in src.
1615 The resulting string in "dest" is always null terminated.
1618 size_t pull_string_fn(const char *function
,
1620 const void *base_ptr
,
1629 clobber_region(function
, line
, dest
, dest_len
);
1632 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1633 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1637 if (!(flags
& STR_ASCII
) && \
1638 ((flags
& STR_UNICODE
|| \
1639 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1640 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1642 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1646 Copy a string from a unicode or ascii source (depending on
1647 the packet flags) to a char* destination.
1648 Variant that uses talloc.
1650 STR_TERMINATE means the string in src is null terminated.
1651 STR_UNICODE means to force as unicode.
1652 STR_ASCII use ascii even with unicode packet.
1653 STR_NOALIGN means don't do alignment.
1654 if STR_TERMINATE is set then src_len is ignored is it is -1
1655 src_len is the length of the source area in bytes.
1656 Return the number of bytes occupied by the string in src.
1657 The resulting string in "dest" is always null terminated.
1660 size_t pull_string_talloc_fn(const char *function
,
1663 const void *base_ptr
,
1670 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1671 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1675 if (!(flags
& STR_ASCII
) && \
1676 ((flags
& STR_UNICODE
|| \
1677 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1678 return pull_ucs2_base_talloc(ctx
,
1685 return pull_ascii_base_talloc(ctx
,
1693 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1695 if (!(flags
& STR_ASCII
) && \
1696 ((flags
& STR_UNICODE
|| \
1697 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1698 return ucs2_align(base_ptr
, p
, flags
);