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 smb_iconv_t conv_handles
[NUM_CHARSETS
][NUM_CHARSETS
];
49 static bool conv_silent
; /* Should we do a debug if the conversion fails ? */
52 * Return the name of a charset to give to iconv().
54 static const char *charset_name(charset_t ch
)
56 const char *ret
= NULL
;
58 if (ch
== CH_UTF16LE
) ret
= "UTF-16LE";
59 else if (ch
== CH_UTF16BE
) ret
= "UTF-16BE";
60 else if (ch
== CH_UNIX
) ret
= lp_unix_charset();
61 else if (ch
== CH_DOS
) ret
= lp_dos_charset();
62 else if (ch
== CH_DISPLAY
) ret
= lp_display_charset();
63 else if (ch
== CH_UTF8
) ret
= "UTF8";
65 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
66 if (ret
&& !strcmp(ret
, "LOCALE")) {
67 const char *ln
= NULL
;
70 setlocale(LC_ALL
, "");
72 ln
= nl_langinfo(CODESET
);
74 /* Check whether the charset name is supported
76 smb_iconv_t handle
= smb_iconv_open(ln
,"UCS-2LE");
77 if (handle
== (smb_iconv_t
) -1) {
78 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln
));
81 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln
));
82 smb_iconv_close(handle
);
89 if (!ret
|| !*ret
) ret
= "ASCII";
93 void lazy_initialize_conv(void)
95 static int initialized
= False
;
105 * Destroy global objects allocated by init_iconv()
107 void gfree_charcnv(void)
111 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
112 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
113 if ( conv_handles
[c1
][c2
] ) {
114 smb_iconv_close( conv_handles
[c1
][c2
] );
115 conv_handles
[c1
][c2
] = 0;
122 * Initialize iconv conversion descriptors.
124 * This is called the first time it is needed, and also called again
125 * every time the configuration is reloaded, because the charset or
126 * codepage might have changed.
128 void init_iconv(void)
131 bool did_reload
= False
;
133 /* so that charset_name() works we need to get the UNIX<->UCS2 going
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)
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
) {
162 if (c2
!= CH_UTF16LE
&& c2
!= CH_UTF16BE
) {
165 DEBUG(0,("init_iconv: Attempting to replace with conversion from %s to %s\n",
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");
177 /* XXX: Does this really get called every time the dos
178 * codepage changes? */
179 /* XXX: Is the did_reload test too strict? */
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
)
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;
219 srclen
= strlen((const char *)src
)+1;
224 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
226 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
235 retval
= smb_iconv(descriptor
, &inbuf
, &i_len
, &outbuf
, &o_len
);
236 if(retval
==(size_t)-1) {
237 const char *reason
="unknown error";
240 reason
="Incomplete multibyte sequence";
242 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
247 reason
="No more room";
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
));
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
));
261 reason
="Illegal multibyte sequence";
263 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
269 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
272 /* smb_panic(reason); */
274 return destlen
-o_len
;
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.
294 return destlen
- o_len
;
296 *outbuf
= lp_failed_convert_char();
305 if (o_len
== 0 || i_len
== 0)
306 return destlen
- o_len
;
308 /* Keep trying with the next char... */
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.
316 return destlen
- o_len
;
318 outbuf
[0] = lp_failed_convert_char();
327 if (o_len
== 0 || i_len
== 0)
328 return destlen
- o_len
;
330 /* Keep trying with the next char... */
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
337 outbuf
[0] = lp_failed_convert_char();
345 if (o_len
== 0 || i_len
== 0)
346 return destlen
- o_len
;
348 /* Keep trying with the next char... */
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.
386 SMB_ASSERT(destlen
!= (size_t)-1);
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';
400 /* If all characters are ascii, fast path here. */
401 while (slen
&& dlen
) {
402 if ((lastp
= *p
) <= 0x7f) {
404 if (slen
!= (size_t)-1) {
412 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
415 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
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
)) {
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
;
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)) {
439 if (slen
!= (size_t)-1) {
448 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
451 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
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
)) {
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
;
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) {
476 if (slen
!= (size_t)-1) {
484 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
487 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
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
)) {
501 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
504 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
508 * Convert between character sets, allocating a new buffer for the result.
510 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
511 * (this is a bad interface and needs fixing. JRA).
512 * @param srclen length of source buffer.
513 * @param dest always set at least to NULL
514 * @note -1 is not accepted for srclen.
516 * @returns Size in bytes of the converted string; or -1 in case of error.
518 * Ensure the srclen contains the terminating zero.
520 * I hate the goto's in this function. It's embarressing.....
521 * There has to be a cleaner way to do this. JRA.
524 size_t convert_string_allocate(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
525 void const *src
, size_t srclen
, void *dst
, bool allow_bad_conv
)
527 size_t i_len
, o_len
, destlen
= (srclen
* 3) / 2;
529 const char *inbuf
= (const char *)src
;
530 char *outbuf
= NULL
, *ob
= NULL
;
531 smb_iconv_t descriptor
;
532 void **dest
= (void **)dst
;
536 if (src
== NULL
|| srclen
== (size_t)-1)
541 lazy_initialize_conv();
543 descriptor
= conv_handles
[from
][to
];
545 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
547 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
553 /* +2 is for ucs2 null termination. */
554 if ((destlen
*2)+2 < destlen
) {
555 /* wrapped ! abort. */
557 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
562 destlen
= destlen
* 2;
565 /* +2 is for ucs2 null termination. */
567 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
+ 2);
569 ob
= (char *)SMB_REALLOC(ob
, destlen
+ 2);
573 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
582 retval
= smb_iconv(descriptor
,
585 if(retval
== (size_t)-1) {
586 const char *reason
="unknown error";
589 reason
="Incomplete multibyte sequence";
591 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
598 reason
="Illegal multibyte sequence";
600 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
606 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
607 /* smb_panic(reason); */
618 destlen
= destlen
- o_len
;
619 /* Don't shrink unless we're reclaiming a lot of
620 * space. This is in the hot codepath and these
621 * reallocs *cost*. JRA.
624 /* We're shrinking here so we know the +2 is safe from wrap. */
626 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
+ 2);
628 ob
= (char *)SMB_REALLOC(ob
,destlen
+ 2);
632 if (destlen
&& !ob
) {
633 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
639 /* Must ucs2 null terminate in the extra space we allocated. */
641 ob
[destlen
+1] = '\0';
648 * Conversion not supported. This is actually an error, but there are so
649 * many misconfigured iconv systems and smb.conf's out there we can't just
650 * fail. Do a very bad conversion instead.... JRA.
654 if (o_len
== 0 || i_len
== 0)
657 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
658 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
659 /* Can't convert from utf16 any endian to multibyte.
660 Replace with the default fail char.
667 *outbuf
= lp_failed_convert_char();
676 if (o_len
== 0 || i_len
== 0)
679 /* Keep trying with the next char... */
682 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
683 /* Can't convert to UTF16LE - just widen by adding the
684 default fail char then zero.
689 outbuf
[0] = lp_failed_convert_char();
698 if (o_len
== 0 || i_len
== 0)
701 /* Keep trying with the next char... */
704 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
705 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
706 /* Failed multibyte to multibyte. Just copy the default fail char and
708 outbuf
[0] = lp_failed_convert_char();
716 if (o_len
== 0 || i_len
== 0)
719 /* Keep trying with the next char... */
723 /* Keep compiler happy.... */
730 * Convert between character sets, allocating a new buffer using talloc for the result.
732 * @param srclen length of source buffer.
733 * @param dest always set at least to NULL
734 * @note -1 is not accepted for srclen.
736 * @returns Size in bytes of the converted string; or -1 in case of error.
738 size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
739 void const *src
, size_t srclen
, void *dst
,
742 void **dest
= (void **)dst
;
746 dest_len
=convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
, allow_bad_conv
);
747 if (dest_len
== (size_t)-1)
754 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
759 size
= push_ucs2_allocate(&buffer
, src
);
760 if (size
== (size_t)-1) {
763 if (!strupper_w(buffer
) && (dest
== src
)) {
768 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
774 strdup() a unix string to upper case.
777 char *strdup_upper(const char *s
)
779 char *out_buffer
= SMB_STRDUP(s
);
780 const unsigned char *p
= (const unsigned char *)s
;
781 unsigned char *q
= (unsigned char *)out_buffer
;
787 /* this is quite a common operation, so we want it to be
788 fast. We optimise for the ascii case, knowing that all our
789 supported multi-byte character sets are ascii-compatible
790 (ie. they match for the first 128 chars) */
795 *q
++ = toupper_ascii_fast(*p
);
802 smb_ucs2_t
*buffer
= NULL
;
804 SAFE_FREE(out_buffer
);
805 size
= convert_string_allocate(NULL
,
810 (void **)(void *)&buffer
,
812 if (size
== (size_t)-1) {
818 size
= convert_string_allocate(NULL
,
823 (void **)(void *)&out_buffer
,
826 /* Don't need the intermediate buffer
831 if (size
== (size_t)-1) {
840 talloc_strdup() a unix string to upper case.
843 char *talloc_strdup_upper(TALLOC_CTX
*ctx
, const char *s
)
845 char *out_buffer
= talloc_strdup(ctx
,s
);
846 const unsigned char *p
= (const unsigned char *)s
;
847 unsigned char *q
= (unsigned char *)out_buffer
;
853 /* this is quite a common operation, so we want it to be
854 fast. We optimise for the ascii case, knowing that all our
855 supported multi-byte character sets are ascii-compatible
856 (ie. they match for the first 128 chars) */
861 *q
++ = toupper_ascii_fast(*p
);
868 smb_ucs2_t
*ubuf
= NULL
;
870 /* We're not using the ascii buffer above. */
871 TALLOC_FREE(out_buffer
);
873 size
= convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
,
877 if (size
== (size_t)-1) {
883 size
= convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
,
888 /* Don't need the intermediate buffer
894 if (size
== (size_t)-1) {
902 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
905 smb_ucs2_t
*buffer
= NULL
;
907 size
= convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, srclen
,
908 (void **)(void *)&buffer
, True
);
909 if (size
== (size_t)-1 || !buffer
) {
910 smb_panic("failed to create UCS2 buffer");
912 if (!strlower_w(buffer
) && (dest
== src
)) {
916 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
922 strdup() a unix string to lower case.
925 char *strdup_lower(const char *s
)
928 smb_ucs2_t
*buffer
= NULL
;
931 size
= push_ucs2_allocate(&buffer
, s
);
932 if (size
== -1 || !buffer
) {
938 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
941 if (size
== (size_t)-1) {
948 size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
950 if (flags
& (STR_NOALIGN
|STR_ASCII
))
952 return PTR_DIFF(p
, base_ptr
) & 1;
957 * Copy a string from a char* unix src to a dos codepage string destination.
959 * @return the number of bytes occupied by the string in the destination.
961 * @param flags can include
963 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
964 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
967 * @param dest_len the maximum length in bytes allowed in the
970 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
972 size_t src_len
= strlen(src
);
976 /* No longer allow a length of -1. */
977 if (dest_len
== (size_t)-1) {
978 smb_panic("push_ascii - dest_len == -1");
981 if (flags
& STR_UPPER
) {
982 tmpbuf
= SMB_STRDUP(src
);
984 smb_panic("malloc fail");
990 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
)) {
994 ret
= convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
995 if (ret
== (size_t)-1 &&
996 (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
998 ((char *)dest
)[0] = '\0';
1004 size_t push_ascii_fstring(void *dest
, const char *src
)
1006 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1009 /********************************************************************
1010 Push an nstring - ensure null terminated. Written by
1011 moriyama@miraclelinux.com (MORIYAMA Masayuki).
1012 ********************************************************************/
1014 size_t push_ascii_nstring(void *dest
, const char *src
)
1016 size_t i
, buffer_len
, dest_len
;
1020 buffer_len
= push_ucs2_allocate(&buffer
, src
);
1021 if (buffer_len
== (size_t)-1) {
1022 smb_panic("failed to create UCS2 buffer");
1025 /* We're using buffer_len below to count ucs2 characters, not bytes. */
1026 buffer_len
/= sizeof(smb_ucs2_t
);
1029 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
1030 unsigned char mb
[10];
1031 /* Convert one smb_ucs2_t character at a time. */
1032 size_t mb_len
= convert_string(CH_UTF16LE
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
1033 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
1034 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
1041 ((char *)dest
)[dest_len
] = '\0';
1044 conv_silent
= False
;
1048 /********************************************************************
1049 Push and malloc an ascii string. src and dest null terminated.
1050 ********************************************************************/
1052 size_t push_ascii_allocate(char **dest
, const char *src
)
1054 size_t src_len
= strlen(src
)+1;
1057 return convert_string_allocate(NULL
, CH_UNIX
, CH_DOS
, src
, src_len
, (void **)dest
, True
);
1061 * Copy a string from a dos codepage source to a unix char* destination.
1063 * The resulting string in "dest" is always null terminated.
1065 * @param flags can have:
1067 * <dt>STR_TERMINATE</dt>
1068 * <dd>STR_TERMINATE means the string in @p src
1069 * is null terminated, and src_len is ignored.</dd>
1072 * @param src_len is the length of the source area in bytes.
1073 * @returns the number of bytes occupied by the string in @p src.
1075 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1079 if (dest_len
== (size_t)-1) {
1080 /* No longer allow dest_len of -1. */
1081 smb_panic("pull_ascii - invalid dest_len of -1");
1084 if (flags
& STR_TERMINATE
) {
1085 if (src_len
== (size_t)-1) {
1086 src_len
= strlen((const char *)src
) + 1;
1088 size_t len
= strnlen((const char *)src
, src_len
);
1095 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1096 if (ret
== (size_t)-1) {
1101 if (dest_len
&& ret
) {
1102 /* Did we already process the terminating zero ? */
1103 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1104 dest
[MIN(ret
, dest_len
-1)] = 0;
1114 * Copy a string from a dos codepage source to a unix char* destination.
1116 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1119 * The resulting string in "dest" is always null terminated.
1121 * @param flags can have:
1123 * <dt>STR_TERMINATE</dt>
1124 * <dd>STR_TERMINATE means the string in @p src
1125 * is null terminated, and src_len is ignored.</dd>
1128 * @param src_len is the length of the source area in bytes.
1129 * @returns the number of bytes occupied by the string in @p src.
1132 static size_t pull_ascii_base_talloc(TALLOC_CTX
*ctx
,
1139 size_t dest_len
= 0;
1142 /* Ensure we never use the braindead "malloc" varient. */
1144 smb_panic("NULL talloc CTX in pull_ascii_base_talloc\n");
1150 if (flags
& STR_TERMINATE
) {
1151 if (src_len
== (size_t)-1) {
1152 src_len
= strlen((const char *)src
) + 1;
1154 size_t len
= strnlen((const char *)src
, src_len
);
1159 /* Ensure we don't use an insane length from the client. */
1160 if (src_len
>= 1024*1024) {
1161 char *msg
= talloc_asprintf(ctx
,
1162 "Bad src length (%u) in "
1163 "pull_ascii_base_talloc",
1164 (unsigned int)src_len
);
1169 dest_len
= convert_string_allocate(ctx
,
1177 if (dest_len
== (size_t)-1) {
1181 if (dest_len
&& dest
) {
1182 /* Did we already process the terminating zero ? */
1183 if (dest
[dest_len
-1] != 0) {
1184 dest
[dest_len
-1] = 0;
1194 size_t pull_ascii_fstring(char *dest
, const void *src
)
1196 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1199 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1201 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
1203 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
1207 * Copy a string from a char* src to a unicode destination.
1209 * @returns the number of bytes occupied by the string in the destination.
1211 * @param flags can have:
1214 * <dt>STR_TERMINATE <dd>means include the null termination.
1215 * <dt>STR_UPPER <dd>means uppercase in the destination.
1216 * <dt>STR_NOALIGN <dd>means don't do alignment.
1219 * @param dest_len is the maximum length allowed in the
1223 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1229 if (dest_len
== (size_t)-1) {
1230 /* No longer allow dest_len of -1. */
1231 smb_panic("push_ucs2 - invalid dest_len of -1");
1234 if (flags
& STR_TERMINATE
)
1235 src_len
= (size_t)-1;
1237 src_len
= strlen(src
);
1239 if (ucs2_align(base_ptr
, dest
, flags
)) {
1241 dest
= (void *)((char *)dest
+ 1);
1247 /* ucs2 is always a multiple of 2 bytes */
1250 ret
= convert_string(CH_UNIX
, CH_UTF16LE
, src
, src_len
, dest
, dest_len
, True
);
1251 if (ret
== (size_t)-1) {
1252 if ((flags
& STR_TERMINATE
) &&
1262 if (flags
& STR_UPPER
) {
1263 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1266 /* We check for i < (ret / 2) below as the dest string isn't null
1267 terminated if STR_TERMINATE isn't set. */
1269 for (i
= 0; i
< (ret
/ 2) && i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1270 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1271 if (v
!= dest_ucs2
[i
]) {
1282 * Copy a string from a unix char* src to a UCS2 destination,
1283 * allocating a buffer using talloc().
1285 * @param dest always set at least to NULL
1287 * @returns The number of bytes occupied by the string in the destination
1288 * or -1 in case of error.
1290 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1292 size_t src_len
= strlen(src
)+1;
1295 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1300 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1302 * @param dest always set at least to NULL
1304 * @returns The number of bytes occupied by the string in the destination
1305 * or -1 in case of error.
1308 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1310 size_t src_len
= strlen(src
)+1;
1313 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1317 Copy a string from a char* src to a UTF-8 destination.
1318 Return the number of bytes occupied by the string in the destination
1320 STR_TERMINATE means include the null termination
1321 STR_UPPER means uppercase in the destination
1322 dest_len is the maximum length allowed in the destination. If dest_len
1323 is -1 then no maxiumum is used.
1326 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1330 char *tmpbuf
= NULL
;
1332 if (dest_len
== (size_t)-1) {
1333 /* No longer allow dest_len of -1. */
1334 smb_panic("push_utf8 - invalid dest_len of -1");
1337 if (flags
& STR_UPPER
) {
1338 tmpbuf
= strdup_upper(src
);
1343 src_len
= strlen(src
);
1346 src_len
= strlen(src
);
1347 if (flags
& STR_TERMINATE
) {
1351 ret
= convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1356 size_t push_utf8_fstring(void *dest
, const char *src
)
1358 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1362 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1364 * @param dest always set at least to NULL
1366 * @returns The number of bytes occupied by the string in the destination
1369 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1371 size_t src_len
= strlen(src
)+1;
1374 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1378 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1380 * @param dest always set at least to NULL
1382 * @returns The number of bytes occupied by the string in the destination
1385 size_t push_utf8_allocate(char **dest
, const char *src
)
1387 size_t src_len
= strlen(src
)+1;
1390 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, True
);
1394 Copy a string from a ucs2 source to a unix char* destination.
1396 STR_TERMINATE means the string in src is null terminated.
1397 STR_NOALIGN means don't try to align.
1398 if STR_TERMINATE is set then src_len is ignored if it is -1.
1399 src_len is the length of the source area in bytes
1400 Return the number of bytes occupied by the string in src.
1401 The resulting string in "dest" is always null terminated.
1404 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1408 if (dest_len
== (size_t)-1) {
1409 /* No longer allow dest_len of -1. */
1410 smb_panic("pull_ucs2 - invalid dest_len of -1");
1414 if (dest
&& dest_len
> 0) {
1420 if (ucs2_align(base_ptr
, src
, flags
)) {
1421 src
= (const void *)((const char *)src
+ 1);
1422 if (src_len
!= (size_t)-1)
1426 if (flags
& STR_TERMINATE
) {
1427 /* src_len -1 is the default for null terminated strings. */
1428 if (src_len
!= (size_t)-1) {
1429 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1431 if (len
< src_len
/2)
1437 /* ucs2 is always a multiple of 2 bytes */
1438 if (src_len
!= (size_t)-1)
1441 ret
= convert_string(CH_UTF16LE
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1442 if (ret
== (size_t)-1) {
1447 if (src_len
== (size_t)-1)
1450 if (dest_len
&& ret
) {
1451 /* Did we already process the terminating zero ? */
1452 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1453 dest
[MIN(ret
, dest_len
-1)] = 0;
1463 Copy a string from a ucs2 source to a unix char* destination.
1464 Talloc version with a base pointer.
1465 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1468 STR_TERMINATE means the string in src is null terminated.
1469 STR_NOALIGN means don't try to align.
1470 if STR_TERMINATE is set then src_len is ignored if it is -1.
1471 src_len is the length of the source area in bytes
1472 Return the number of bytes occupied by the string in src.
1473 The resulting string in "dest" is always null terminated.
1476 size_t pull_ucs2_base_talloc(TALLOC_CTX
*ctx
,
1477 const void *base_ptr
,
1489 /* Ensure we never use the braindead "malloc" varient. */
1491 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
1499 if (ucs2_align(base_ptr
, src
, flags
)) {
1500 src
= (const void *)((const char *)src
+ 1);
1501 if (src_len
!= (size_t)-1)
1505 if (flags
& STR_TERMINATE
) {
1506 /* src_len -1 is the default for null terminated strings. */
1507 if (src_len
!= (size_t)-1) {
1508 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1510 if (len
< src_len
/2)
1515 * src_len == -1 - alloc interface won't take this
1516 * so we must calculate.
1518 src_len
= (strlen_w((const smb_ucs2_t
*)src
)+1)*sizeof(smb_ucs2_t
);
1520 /* Ensure we don't use an insane length from the client. */
1521 if (src_len
>= 1024*1024) {
1522 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1526 /* ucs2 is always a multiple of 2 bytes */
1527 if (src_len
!= (size_t)-1) {
1531 dest_len
= convert_string_talloc(ctx
,
1538 if (dest_len
== (size_t)-1) {
1542 if (src_len
== (size_t)-1)
1543 src_len
= dest_len
*2;
1546 /* Did we already process the terminating zero ? */
1547 if (dest
[dest_len
-1] != 0) {
1548 size_t size
= talloc_get_size(dest
);
1549 /* Have we got space to append the '\0' ? */
1550 if (size
<= dest_len
) {
1552 dest
= TALLOC_REALLOC_ARRAY(ctx
, dest
, char,
1556 dest_len
= (size_t)-1;
1561 dest
[dest_len
] = '\0';
1572 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1574 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1578 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1580 * @param dest always set at least to NULL
1582 * @returns The number of bytes occupied by the string in the destination
1585 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1587 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1589 return convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1593 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1595 * @param dest always set at least to NULL
1597 * @returns The number of bytes occupied by the string in the destination
1600 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1602 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1604 return convert_string_allocate(NULL
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1608 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1610 * @param dest always set at least to NULL
1612 * @returns The number of bytes occupied by the string in the destination
1615 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1617 size_t src_len
= strlen(src
)+1;
1619 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1623 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1625 * @param dest always set at least to NULL
1627 * @returns The number of bytes occupied by the string in the destination
1630 size_t pull_utf8_allocate(char **dest
, const char *src
)
1632 size_t src_len
= strlen(src
)+1;
1634 return convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1638 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1640 * @param dest always set at least to NULL
1642 * @returns The number of bytes occupied by the string in the destination
1645 size_t pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1647 size_t src_len
= strlen(src
)+1;
1649 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1653 Copy a string from a char* src to a unicode or ascii
1654 dos codepage destination choosing unicode or ascii based on the
1655 flags in the SMB buffer starting at base_ptr.
1656 Return the number of bytes occupied by the string in the destination.
1658 STR_TERMINATE means include the null termination.
1659 STR_UPPER means uppercase in the destination.
1660 STR_ASCII use ascii even with unicode packet.
1661 STR_NOALIGN means don't do alignment.
1662 dest_len is the maximum length allowed in the destination. If dest_len
1663 is -1 then no maxiumum is used.
1666 size_t push_string_fn(const char *function
, unsigned int line
,
1667 const void *base_ptr
, uint16 flags2
,
1668 void *dest
, const char *src
,
1669 size_t dest_len
, int flags
)
1672 /* We really need to zero fill here, not clobber
1673 * region, as we want to ensure that valgrind thinks
1674 * all of the outgoing buffer has been written to
1675 * so a send() or write() won't trap an error.
1679 clobber_region(function
, line
, dest
, dest_len
);
1681 memset(dest
, '\0', dest_len
);
1685 if (!(flags
& STR_ASCII
) && \
1686 ((flags
& STR_UNICODE
|| \
1687 (flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1688 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1690 return push_ascii(dest
, src
, dest_len
, flags
);
1695 Copy a string from a unicode or ascii source (depending on
1696 the packet flags) to a char* destination.
1698 STR_TERMINATE means the string in src is null terminated.
1699 STR_UNICODE means to force as unicode.
1700 STR_ASCII use ascii even with unicode packet.
1701 STR_NOALIGN means don't do alignment.
1702 if STR_TERMINATE is set then src_len is ignored is it is -1
1703 src_len is the length of the source area in bytes.
1704 Return the number of bytes occupied by the string in src.
1705 The resulting string in "dest" is always null terminated.
1708 size_t pull_string_fn(const char *function
,
1710 const void *base_ptr
,
1719 clobber_region(function
, line
, dest
, dest_len
);
1722 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1723 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1727 if (!(flags
& STR_ASCII
) && \
1728 ((flags
& STR_UNICODE
|| \
1729 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1730 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1732 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1736 Copy a string from a unicode or ascii source (depending on
1737 the packet flags) to a char* destination.
1738 Variant that uses talloc.
1740 STR_TERMINATE means the string in src is null terminated.
1741 STR_UNICODE means to force as unicode.
1742 STR_ASCII use ascii even with unicode packet.
1743 STR_NOALIGN means don't do alignment.
1744 if STR_TERMINATE is set then src_len is ignored is it is -1
1745 src_len is the length of the source area in bytes.
1746 Return the number of bytes occupied by the string in src.
1747 The resulting string in "dest" is always null terminated.
1750 size_t pull_string_talloc_fn(const char *function
,
1753 const void *base_ptr
,
1760 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1761 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1765 if (!(flags
& STR_ASCII
) && \
1766 ((flags
& STR_UNICODE
|| \
1767 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1768 return pull_ucs2_base_talloc(ctx
,
1775 return pull_ascii_base_talloc(ctx
,
1783 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1785 if (!(flags
& STR_ASCII
) && \
1786 ((flags
& STR_UNICODE
|| \
1787 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1788 return ucs2_align(base_ptr
, p
, flags
);
1794 Return the unicode codepoint for the next multi-byte CH_UNIX character
1795 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1797 Also return the number of bytes consumed (which tells the caller
1798 how many bytes to skip to get to the next CH_UNIX character).
1800 Return INVALID_CODEPOINT if the next character cannot be converted.
1803 codepoint_t
next_codepoint(const char *str
, size_t *size
)
1805 /* It cannot occupy more than 4 bytes in UTF16 format */
1807 smb_iconv_t descriptor
;
1813 if ((str
[0] & 0x80) == 0) {
1815 return (codepoint_t
)str
[0];
1818 /* We assume that no multi-byte character can take
1819 more than 5 bytes. This is OK as we only
1820 support codepoints up to 1M */
1822 ilen_orig
= strnlen(str
, 5);
1825 lazy_initialize_conv();
1827 descriptor
= conv_handles
[CH_UNIX
][CH_UTF16LE
];
1828 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
1830 return INVALID_CODEPOINT
;
1833 /* This looks a little strange, but it is needed to cope
1834 with codepoints above 64k which are encoded as per RFC2781. */
1836 outbuf
= (char *)buf
;
1837 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1839 /* We failed to convert to a 2 byte character.
1840 See if we can convert to a 4 UTF16-LE byte char encoding.
1843 outbuf
= (char *)buf
;
1844 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1846 /* We didn't convert any bytes */
1848 return INVALID_CODEPOINT
;
1855 *size
= ilen_orig
- ilen
;
1858 /* 2 byte, UTF16-LE encoded value. */
1859 return (codepoint_t
)SVAL(buf
, 0);
1862 /* Decode a 4 byte UTF16-LE character manually.
1863 See RFC2871 for the encoding machanism.
1865 codepoint_t w1
= SVAL(buf
,0) & ~0xD800;
1866 codepoint_t w2
= SVAL(buf
,2) & ~0xDC00;
1868 return (codepoint_t
)0x10000 +
1872 /* no other length is valid */
1873 return INVALID_CODEPOINT
;