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? */
181 init_doschar_table();
188 * Convert string from one encoding to another, making error checking etc
189 * Slow path version - uses (slow) iconv.
191 * @param src pointer to source string (multibyte or singlebyte)
192 * @param srclen length of the source string in bytes
193 * @param dest pointer to destination string (multibyte or singlebyte)
194 * @param destlen maximal length allowed for string
195 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
196 * @returns the number of bytes occupied in the destination
198 * Ensure the srclen contains the terminating zero.
202 static size_t convert_string_internal(charset_t from
, charset_t to
,
203 void const *src
, size_t srclen
,
204 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
208 const char* inbuf
= (const char*)src
;
209 char* outbuf
= (char*)dest
;
210 smb_iconv_t descriptor
;
212 lazy_initialize_conv();
214 descriptor
= conv_handles
[from
][to
];
216 if (srclen
== (size_t)-1) {
217 if (from
== CH_UTF16LE
|| from
== CH_UTF16BE
) {
218 srclen
= (strlen_w((const smb_ucs2_t
*)src
)+1) * 2;
220 srclen
= strlen((const char *)src
)+1;
225 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
227 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
236 retval
= smb_iconv(descriptor
, &inbuf
, &i_len
, &outbuf
, &o_len
);
237 if(retval
==(size_t)-1) {
238 const char *reason
="unknown error";
241 reason
="Incomplete multibyte sequence";
243 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
248 reason
="No more room";
250 if (from
== CH_UNIX
) {
251 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
252 charset_name(from
), charset_name(to
),
253 (unsigned int)srclen
, (unsigned int)destlen
, (const char *)src
));
255 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
256 charset_name(from
), charset_name(to
),
257 (unsigned int)srclen
, (unsigned int)destlen
));
262 reason
="Illegal multibyte sequence";
264 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
270 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
273 /* smb_panic(reason); */
275 return destlen
-o_len
;
280 * Conversion not supported. This is actually an error, but there are so
281 * many misconfigured iconv systems and smb.conf's out there we can't just
282 * fail. Do a very bad conversion instead.... JRA.
286 if (o_len
== 0 || i_len
== 0)
287 return destlen
- o_len
;
289 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
290 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
291 /* Can't convert from utf16 any endian to multibyte.
292 Replace with the default fail char.
295 return destlen
- o_len
;
297 *outbuf
= lp_failed_convert_char();
306 if (o_len
== 0 || i_len
== 0)
307 return destlen
- o_len
;
309 /* Keep trying with the next char... */
312 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
313 /* Can't convert to UTF16LE - just widen by adding the
314 default fail char then zero.
317 return destlen
- o_len
;
319 outbuf
[0] = lp_failed_convert_char();
328 if (o_len
== 0 || i_len
== 0)
329 return destlen
- o_len
;
331 /* Keep trying with the next char... */
334 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
335 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
336 /* Failed multibyte to multibyte. Just copy the default fail char and
338 outbuf
[0] = lp_failed_convert_char();
346 if (o_len
== 0 || i_len
== 0)
347 return destlen
- o_len
;
349 /* Keep trying with the next char... */
353 /* Keep compiler happy.... */
354 return destlen
- o_len
;
360 * Convert string from one encoding to another, making error checking etc
361 * Fast path version - handles ASCII first.
363 * @param src pointer to source string (multibyte or singlebyte)
364 * @param srclen length of the source string in bytes, or -1 for nul terminated.
365 * @param dest pointer to destination string (multibyte or singlebyte)
366 * @param destlen maximal length allowed for string - *NEVER* -1.
367 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
368 * @returns the number of bytes occupied in the destination
370 * Ensure the srclen contains the terminating zero.
372 * This function has been hand-tuned to provide a fast path.
373 * Don't change unless you really know what you are doing. JRA.
376 size_t convert_string(charset_t from
, charset_t to
,
377 void const *src
, size_t srclen
,
378 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
381 * NB. We deliberately don't do a strlen here if srclen == -1.
382 * This is very expensive over millions of calls and is taken
383 * care of in the slow path in convert_string_internal. JRA.
387 SMB_ASSERT(destlen
!= (size_t)-1);
393 if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
394 const unsigned char *p
= (const unsigned char *)src
;
395 unsigned char *q
= (unsigned char *)dest
;
396 size_t slen
= srclen
;
397 size_t dlen
= destlen
;
398 unsigned char lastp
= '\0';
401 /* If all characters are ascii, fast path here. */
402 while (slen
&& dlen
) {
403 if ((lastp
= *p
) <= 0x7f) {
405 if (slen
!= (size_t)-1) {
413 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
416 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
421 /* Even if we fast path we should note if we ran out of room. */
422 if (((slen
!= (size_t)-1) && slen
) ||
423 ((slen
== (size_t)-1) && lastp
)) {
428 } else if (from
== CH_UTF16LE
&& to
!= CH_UTF16LE
) {
429 const unsigned char *p
= (const unsigned char *)src
;
430 unsigned char *q
= (unsigned char *)dest
;
432 size_t slen
= srclen
;
433 size_t dlen
= destlen
;
434 unsigned char lastp
= '\0';
436 /* If all characters are ascii, fast path here. */
437 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
438 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
440 if (slen
!= (size_t)-1) {
449 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
452 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
457 /* Even if we fast path we should note if we ran out of room. */
458 if (((slen
!= (size_t)-1) && slen
) ||
459 ((slen
== (size_t)-1) && lastp
)) {
464 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
465 const unsigned char *p
= (const unsigned char *)src
;
466 unsigned char *q
= (unsigned char *)dest
;
468 size_t slen
= srclen
;
469 size_t dlen
= destlen
;
470 unsigned char lastp
= '\0';
472 /* If all characters are ascii, fast path here. */
473 while (slen
&& (dlen
>= 2)) {
474 if ((lastp
= *p
) <= 0x7F) {
477 if (slen
!= (size_t)-1) {
485 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
488 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
493 /* Even if we fast path we should note if we ran out of room. */
494 if (((slen
!= (size_t)-1) && slen
) ||
495 ((slen
== (size_t)-1) && lastp
)) {
502 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
505 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
509 * Convert between character sets, allocating a new buffer for the result.
511 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
512 * (this is a bad interface and needs fixing. JRA).
513 * @param srclen length of source buffer.
514 * @param dest always set at least to NULL
515 * @note -1 is not accepted for srclen.
517 * @returns Size in bytes of the converted string; or -1 in case of error.
519 * Ensure the srclen contains the terminating zero.
521 * I hate the goto's in this function. It's embarressing.....
522 * There has to be a cleaner way to do this. JRA.
525 size_t convert_string_allocate(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
526 void const *src
, size_t srclen
, void *dst
, BOOL allow_bad_conv
)
528 size_t i_len
, o_len
, destlen
= MAX(srclen
, 512);
530 const char *inbuf
= (const char *)src
;
531 char *outbuf
= NULL
, *ob
= NULL
;
532 smb_iconv_t descriptor
;
533 void **dest
= (void **)dst
;
537 if (src
== NULL
|| srclen
== (size_t)-1)
542 lazy_initialize_conv();
544 descriptor
= conv_handles
[from
][to
];
546 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
548 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
554 if ((destlen
*2) < destlen
) {
555 /* wrapped ! abort. */
557 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
562 destlen
= destlen
* 2;
566 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
);
568 ob
= (char *)SMB_REALLOC(ob
, destlen
);
572 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
581 retval
= smb_iconv(descriptor
,
584 if(retval
== (size_t)-1) {
585 const char *reason
="unknown error";
588 reason
="Incomplete multibyte sequence";
590 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
597 reason
="Illegal multibyte sequence";
599 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
605 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
606 /* smb_panic(reason); */
617 destlen
= destlen
- o_len
;
619 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
);
621 ob
= (char *)SMB_REALLOC(ob
,destlen
);
624 if (destlen
&& !ob
) {
625 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
635 * Conversion not supported. This is actually an error, but there are so
636 * many misconfigured iconv systems and smb.conf's out there we can't just
637 * fail. Do a very bad conversion instead.... JRA.
641 if (o_len
== 0 || i_len
== 0)
644 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
645 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
646 /* Can't convert from utf16 any endian to multibyte.
647 Replace with the default fail char.
654 *outbuf
= lp_failed_convert_char();
663 if (o_len
== 0 || i_len
== 0)
666 /* Keep trying with the next char... */
669 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
670 /* Can't convert to UTF16LE - just widen by adding the
671 default fail char then zero.
676 outbuf
[0] = lp_failed_convert_char();
685 if (o_len
== 0 || i_len
== 0)
688 /* Keep trying with the next char... */
691 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
692 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
693 /* Failed multibyte to multibyte. Just copy the default fail char and
695 outbuf
[0] = lp_failed_convert_char();
703 if (o_len
== 0 || i_len
== 0)
706 /* Keep trying with the next char... */
710 /* Keep compiler happy.... */
717 * Convert between character sets, allocating a new buffer using talloc for the result.
719 * @param srclen length of source buffer.
720 * @param dest always set at least to NULL
721 * @note -1 is not accepted for srclen.
723 * @returns Size in bytes of the converted string; or -1 in case of error.
725 size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
726 void const *src
, size_t srclen
, void *dst
,
729 void **dest
= (void **)dst
;
733 dest_len
=convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
, allow_bad_conv
);
734 if (dest_len
== (size_t)-1)
741 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
746 size
= push_ucs2_allocate(&buffer
, src
);
747 if (size
== (size_t)-1) {
748 smb_panic("failed to create UCS2 buffer");
750 if (!strupper_w(buffer
) && (dest
== src
)) {
755 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
761 strdup() a unix string to upper case.
765 char *strdup_upper(const char *s
)
768 const unsigned char *p
= (const unsigned char *)s
;
769 unsigned char *q
= (unsigned char *)out_buffer
;
771 /* this is quite a common operation, so we want it to be
772 fast. We optimise for the ascii case, knowing that all our
773 supported multi-byte character sets are ascii-compatible
774 (ie. they match for the first 128 chars) */
779 *q
++ = toupper_ascii(*p
);
783 if (p
- ( const unsigned char *)s
>= sizeof(pstring
))
791 size
= convert_string(CH_UNIX
, CH_UTF16LE
, s
, -1, buffer
, sizeof(buffer
), True
);
792 if (size
== (size_t)-1) {
798 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, -1, out_buffer
, sizeof(out_buffer
), True
);
799 if (size
== (size_t)-1) {
804 return SMB_STRDUP(out_buffer
);
808 talloc_strdup() a unix string to upper case.
811 char *talloc_strdup_upper(TALLOC_CTX
*ctx
, const char *s
)
813 char *out_buffer
= talloc_strdup(ctx
,s
);
814 const unsigned char *p
= (const unsigned char *)s
;
815 unsigned char *q
= (unsigned char *)out_buffer
;
821 /* this is quite a common operation, so we want it to be
822 fast. We optimise for the ascii case, knowing that all our
823 supported multi-byte character sets are ascii-compatible
824 (ie. they match for the first 128 chars) */
829 *q
++ = toupper_ascii(*p
);
838 smb_ucs2_t
*ubuf
= NULL
;
840 /* We're not using the ascii buffer above. */
841 TALLOC_FREE(out_buffer
);
843 size
= convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
,
847 if (size
== (size_t)-1) {
853 size
= convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
,
858 /* Don't need the intermediate buffer
864 if (size
== (size_t)-1) {
872 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
875 smb_ucs2_t
*buffer
= NULL
;
877 size
= convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, srclen
,
878 (void **)(void *)&buffer
, True
);
879 if (size
== (size_t)-1 || !buffer
) {
880 smb_panic("failed to create UCS2 buffer");
882 if (!strlower_w(buffer
) && (dest
== src
)) {
886 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
892 strdup() a unix string to lower case.
895 char *strdup_lower(const char *s
)
898 smb_ucs2_t
*buffer
= NULL
;
901 size
= push_ucs2_allocate(&buffer
, s
);
902 if (size
== -1 || !buffer
) {
908 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
911 if (size
== (size_t)-1) {
918 static size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
920 if (flags
& (STR_NOALIGN
|STR_ASCII
))
922 return PTR_DIFF(p
, base_ptr
) & 1;
927 * Copy a string from a char* unix src to a dos codepage string destination.
929 * @return the number of bytes occupied by the string in the destination.
931 * @param flags can include
933 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
934 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
937 * @param dest_len the maximum length in bytes allowed in the
938 * destination. If @p dest_len is -1 then no maximum is used.
940 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
942 size_t src_len
= strlen(src
);
945 /* treat a pstring as "unlimited" length */
946 if (dest_len
== (size_t)-1)
947 dest_len
= sizeof(pstring
);
949 if (flags
& STR_UPPER
) {
950 pstrcpy(tmpbuf
, src
);
955 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
958 return convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
961 size_t push_ascii_fstring(void *dest
, const char *src
)
963 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
966 size_t push_ascii_pstring(void *dest
, const char *src
)
968 return push_ascii(dest
, src
, sizeof(pstring
), STR_TERMINATE
);
971 /********************************************************************
972 Push an nstring - ensure null terminated. Written by
973 moriyama@miraclelinux.com (MORIYAMA Masayuki).
974 ********************************************************************/
976 size_t push_ascii_nstring(void *dest
, const char *src
)
978 size_t i
, buffer_len
, dest_len
;
982 buffer_len
= push_ucs2_allocate(&buffer
, src
);
983 if (buffer_len
== (size_t)-1) {
984 smb_panic("failed to create UCS2 buffer");
987 /* We're using buffer_len below to count ucs2 characters, not bytes. */
988 buffer_len
/= sizeof(smb_ucs2_t
);
991 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
992 unsigned char mb
[10];
993 /* Convert one smb_ucs2_t character at a time. */
994 size_t mb_len
= convert_string(CH_UTF16LE
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
995 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
996 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
1003 ((char *)dest
)[dest_len
] = '\0';
1006 conv_silent
= False
;
1011 * Copy a string from a dos codepage source to a unix char* destination.
1013 * The resulting string in "dest" is always null terminated.
1015 * @param flags can have:
1017 * <dt>STR_TERMINATE</dt>
1018 * <dd>STR_TERMINATE means the string in @p src
1019 * is null terminated, and src_len is ignored.</dd>
1022 * @param src_len is the length of the source area in bytes.
1023 * @returns the number of bytes occupied by the string in @p src.
1025 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1029 if (dest_len
== (size_t)-1)
1030 dest_len
= sizeof(pstring
);
1032 if (flags
& STR_TERMINATE
) {
1033 if (src_len
== (size_t)-1) {
1034 src_len
= strlen((const char *)src
) + 1;
1036 size_t len
= strnlen((const char *)src
, src_len
);
1043 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1044 if (ret
== (size_t)-1) {
1049 if (dest_len
&& ret
) {
1050 /* Did we already process the terminating zero ? */
1051 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1052 dest
[MIN(ret
, dest_len
-1)] = 0;
1062 * Copy a string from a dos codepage source to a unix char* destination.
1064 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1067 * The resulting string in "dest" is always null terminated.
1069 * @param flags can have:
1071 * <dt>STR_TERMINATE</dt>
1072 * <dd>STR_TERMINATE means the string in @p src
1073 * is null terminated, and src_len is ignored.</dd>
1076 * @param src_len is the length of the source area in bytes.
1077 * @returns the number of bytes occupied by the string in @p src.
1080 static size_t pull_ascii_base_talloc(TALLOC_CTX
*ctx
,
1087 size_t dest_len
= 0;
1090 /* Ensure we never use the braindead "malloc" varient. */
1092 smb_panic("NULL talloc CTX in pull_ascii_base_talloc\n");
1098 if (flags
& STR_TERMINATE
) {
1099 if (src_len
== (size_t)-1) {
1100 src_len
= strlen((const char *)src
) + 1;
1102 size_t len
= strnlen((const char *)src
, src_len
);
1107 /* Ensure we don't use an insane length from the client. */
1108 if (src_len
>= 1024*1024) {
1109 smb_panic("Bad src length in pull_ascii_base_talloc\n");
1113 dest_len
= convert_string_allocate(ctx
,
1121 if (dest_len
== (size_t)-1) {
1125 if (dest_len
&& dest
) {
1126 /* Did we already process the terminating zero ? */
1127 if (dest
[dest_len
-1] != 0) {
1128 dest
[dest_len
-1] = 0;
1139 size_t pull_ascii_pstring(char *dest
, const void *src
)
1141 return pull_ascii(dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1144 size_t pull_ascii_fstring(char *dest
, const void *src
)
1146 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1149 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1151 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
1153 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
1157 * Copy a string from a char* src to a unicode destination.
1159 * @returns the number of bytes occupied by the string in the destination.
1161 * @param flags can have:
1164 * <dt>STR_TERMINATE <dd>means include the null termination.
1165 * <dt>STR_UPPER <dd>means uppercase in the destination.
1166 * <dt>STR_NOALIGN <dd>means don't do alignment.
1169 * @param dest_len is the maximum length allowed in the
1170 * destination. If dest_len is -1 then no maxiumum is used.
1173 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1179 /* treat a pstring as "unlimited" length */
1180 if (dest_len
== (size_t)-1)
1181 dest_len
= sizeof(pstring
);
1183 if (flags
& STR_TERMINATE
)
1184 src_len
= (size_t)-1;
1186 src_len
= strlen(src
);
1188 if (ucs2_align(base_ptr
, dest
, flags
)) {
1190 dest
= (void *)((char *)dest
+ 1);
1196 /* ucs2 is always a multiple of 2 bytes */
1199 ret
= convert_string(CH_UNIX
, CH_UTF16LE
, src
, src_len
, dest
, dest_len
, True
);
1200 if (ret
== (size_t)-1) {
1206 if (flags
& STR_UPPER
) {
1207 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1210 /* We check for i < (ret / 2) below as the dest string isn't null
1211 terminated if STR_TERMINATE isn't set. */
1213 for (i
= 0; i
< (ret
/ 2) && i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1214 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1215 if (v
!= dest_ucs2
[i
]) {
1226 * Copy a string from a unix char* src to a UCS2 destination,
1227 * allocating a buffer using talloc().
1229 * @param dest always set at least to NULL
1231 * @returns The number of bytes occupied by the string in the destination
1232 * or -1 in case of error.
1234 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1236 size_t src_len
= strlen(src
)+1;
1239 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1244 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1246 * @param dest always set at least to NULL
1248 * @returns The number of bytes occupied by the string in the destination
1249 * or -1 in case of error.
1252 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1254 size_t src_len
= strlen(src
)+1;
1257 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1261 Copy a string from a char* src to a UTF-8 destination.
1262 Return the number of bytes occupied by the string in the destination
1264 STR_TERMINATE means include the null termination
1265 STR_UPPER means uppercase in the destination
1266 dest_len is the maximum length allowed in the destination. If dest_len
1267 is -1 then no maxiumum is used.
1270 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1272 size_t src_len
= strlen(src
);
1275 /* treat a pstring as "unlimited" length */
1276 if (dest_len
== (size_t)-1)
1277 dest_len
= sizeof(pstring
);
1279 if (flags
& STR_UPPER
) {
1280 pstrcpy(tmpbuf
, src
);
1285 if (flags
& STR_TERMINATE
)
1288 return convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1291 size_t push_utf8_fstring(void *dest
, const char *src
)
1293 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1297 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1299 * @param dest always set at least to NULL
1301 * @returns The number of bytes occupied by the string in the destination
1304 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1306 size_t src_len
= strlen(src
)+1;
1309 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1313 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1315 * @param dest always set at least to NULL
1317 * @returns The number of bytes occupied by the string in the destination
1320 size_t push_utf8_allocate(char **dest
, const char *src
)
1322 size_t src_len
= strlen(src
)+1;
1325 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, True
);
1329 Copy a string from a ucs2 source to a unix char* destination.
1331 STR_TERMINATE means the string in src is null terminated.
1332 STR_NOALIGN means don't try to align.
1333 if STR_TERMINATE is set then src_len is ignored if it is -1.
1334 src_len is the length of the source area in bytes
1335 Return the number of bytes occupied by the string in src.
1336 The resulting string in "dest" is always null terminated.
1339 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1343 if (dest_len
== (size_t)-1)
1344 dest_len
= sizeof(pstring
);
1346 if (ucs2_align(base_ptr
, src
, flags
)) {
1347 src
= (const void *)((const char *)src
+ 1);
1348 if (src_len
!= (size_t)-1)
1352 if (flags
& STR_TERMINATE
) {
1353 /* src_len -1 is the default for null terminated strings. */
1354 if (src_len
!= (size_t)-1) {
1355 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1357 if (len
< src_len
/2)
1363 /* ucs2 is always a multiple of 2 bytes */
1364 if (src_len
!= (size_t)-1)
1367 ret
= convert_string(CH_UTF16LE
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1368 if (ret
== (size_t)-1) {
1372 if (src_len
== (size_t)-1)
1375 if (dest_len
&& ret
) {
1376 /* Did we already process the terminating zero ? */
1377 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1378 dest
[MIN(ret
, dest_len
-1)] = 0;
1388 Copy a string from a ucs2 source to a unix char* destination.
1389 Talloc version with a base pointer.
1390 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1393 STR_TERMINATE means the string in src is null terminated.
1394 STR_NOALIGN means don't try to align.
1395 if STR_TERMINATE is set then src_len is ignored if it is -1.
1396 src_len is the length of the source area in bytes
1397 Return the number of bytes occupied by the string in src.
1398 The resulting string in "dest" is always null terminated.
1401 static size_t pull_ucs2_base_talloc(TALLOC_CTX
*ctx
,
1402 const void *base_ptr
,
1414 /* Ensure we never use the braindead "malloc" varient. */
1416 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
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)
1435 /* Ensure we don't use an insane length from the client. */
1436 if (src_len
>= 1024*1024) {
1437 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1441 /* ucs2 is always a multiple of 2 bytes */
1442 if (src_len
!= (size_t)-1) {
1446 dest_len
= convert_string_talloc(ctx
,
1453 if (dest_len
== (size_t)-1) {
1457 if (src_len
== (size_t)-1)
1458 src_len
= dest_len
*2;
1461 /* Did we already process the terminating zero ? */
1462 if (dest
[dest_len
-1] != 0) {
1463 size_t size
= talloc_get_size(dest
);
1464 /* Have we got space to append the '\0' ? */
1465 if (size
<= dest_len
) {
1467 dest
= TALLOC_REALLOC(ctx
, dest
,
1471 dest_len
= (size_t)-1;
1476 dest
[dest_len
] = '\0';
1487 size_t pull_ucs2_pstring(char *dest
, const void *src
)
1489 return pull_ucs2(NULL
, dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1492 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1494 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1498 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1500 * @param dest always set at least to NULL
1502 * @returns The number of bytes occupied by the string in the destination
1505 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1507 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1509 return convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1513 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1515 * @param dest always set at least to NULL
1517 * @returns The number of bytes occupied by the string in the destination
1520 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1522 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1524 return convert_string_allocate(NULL
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1528 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1530 * @param dest always set at least to NULL
1532 * @returns The number of bytes occupied by the string in the destination
1535 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1537 size_t src_len
= strlen(src
)+1;
1539 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1543 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1545 * @param dest always set at least to NULL
1547 * @returns The number of bytes occupied by the string in the destination
1550 size_t pull_utf8_allocate(char **dest
, const char *src
)
1552 size_t src_len
= strlen(src
)+1;
1554 return convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1558 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1560 * @param dest always set at least to NULL
1562 * @returns The number of bytes occupied by the string in the destination
1565 size_t pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1567 size_t src_len
= strlen(src
)+1;
1569 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1573 Copy a string from a char* src to a unicode or ascii
1574 dos codepage destination choosing unicode or ascii based on the
1575 flags in the SMB buffer starting at base_ptr.
1576 Return the number of bytes occupied by the string in the destination.
1578 STR_TERMINATE means include the null termination.
1579 STR_UPPER means uppercase in the destination.
1580 STR_ASCII use ascii even with unicode packet.
1581 STR_NOALIGN means don't do alignment.
1582 dest_len is the maximum length allowed in the destination. If dest_len
1583 is -1 then no maxiumum is used.
1586 size_t push_string_fn(const char *function
, unsigned int line
,
1587 const void *base_ptr
, uint16 flags2
,
1588 void *dest
, const char *src
,
1589 size_t dest_len
, int flags
)
1592 /* We really need to zero fill here, not clobber
1593 * region, as we want to ensure that valgrind thinks
1594 * all of the outgoing buffer has been written to
1595 * so a send() or write() won't trap an error.
1599 if (dest_len
!= (size_t)-1)
1600 clobber_region(function
, line
, dest
, dest_len
);
1602 if (dest_len
!= (size_t)-1)
1603 memset(dest
, '\0', dest_len
);
1607 if (!(flags
& STR_ASCII
) && \
1608 ((flags
& STR_UNICODE
|| \
1609 (flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1610 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1612 return push_ascii(dest
, src
, dest_len
, flags
);
1617 Copy a string from a unicode or ascii source (depending on
1618 the packet flags) to a char* destination.
1620 STR_TERMINATE means the string in src is null terminated.
1621 STR_UNICODE means to force as unicode.
1622 STR_ASCII use ascii even with unicode packet.
1623 STR_NOALIGN means don't do alignment.
1624 if STR_TERMINATE is set then src_len is ignored is it is -1
1625 src_len is the length of the source area in bytes.
1626 Return the number of bytes occupied by the string in src.
1627 The resulting string in "dest" is always null terminated.
1630 size_t pull_string_fn(const char *function
, unsigned int line
,
1631 const void *base_ptr
, uint16 smb_flags2
, char *dest
,
1632 const void *src
, size_t dest_len
, size_t src_len
,
1636 if (dest_len
!= (size_t)-1)
1637 clobber_region(function
, line
, dest
, dest_len
);
1640 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1641 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1645 if (!(flags
& STR_ASCII
) && \
1646 ((flags
& STR_UNICODE
|| \
1647 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1648 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1650 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1654 Copy a string from a unicode or ascii source (depending on
1655 the packet flags) to a char* destination.
1656 Variant that uses talloc.
1658 STR_TERMINATE means the string in src is null terminated.
1659 STR_UNICODE means to force as unicode.
1660 STR_ASCII use ascii even with unicode packet.
1661 STR_NOALIGN means don't do alignment.
1662 if STR_TERMINATE is set then src_len is ignored is it is -1
1663 src_len is the length of the source area in bytes.
1664 Return the number of bytes occupied by the string in src.
1665 The resulting string in "dest" is always null terminated.
1668 size_t pull_string_talloc_fn(const char *function
,
1671 const void *base_ptr
,
1678 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1679 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1683 if (!(flags
& STR_ASCII
) && \
1684 ((flags
& STR_UNICODE
|| \
1685 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1686 return pull_ucs2_base_talloc(ctx
,
1693 return pull_ascii_base_talloc(ctx
,
1701 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1703 if (!(flags
& STR_ASCII
) && \
1704 ((flags
& STR_UNICODE
|| \
1705 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1706 return ucs2_align(base_ptr
, p
, flags
);
1712 Return the unicode codepoint for the next multi-byte CH_UNIX character
1713 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1715 Also return the number of bytes consumed (which tells the caller
1716 how many bytes to skip to get to the next CH_UNIX character).
1718 Return INVALID_CODEPOINT if the next character cannot be converted.
1721 codepoint_t
next_codepoint(const char *str
, size_t *size
)
1723 /* It cannot occupy more than 4 bytes in UTF16 format */
1725 smb_iconv_t descriptor
;
1731 if ((str
[0] & 0x80) == 0) {
1733 return (codepoint_t
)str
[0];
1736 /* We assume that no multi-byte character can take
1737 more than 5 bytes. This is OK as we only
1738 support codepoints up to 1M */
1740 ilen_orig
= strnlen(str
, 5);
1743 lazy_initialize_conv();
1745 descriptor
= conv_handles
[CH_UNIX
][CH_UTF16LE
];
1746 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
1748 return INVALID_CODEPOINT
;
1751 /* This looks a little strange, but it is needed to cope
1752 with codepoints above 64k which are encoded as per RFC2781. */
1754 outbuf
= (char *)buf
;
1755 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1757 /* We failed to convert to a 2 byte character.
1758 See if we can convert to a 4 UTF16-LE byte char encoding.
1761 outbuf
= (char *)buf
;
1762 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1764 /* We didn't convert any bytes */
1766 return INVALID_CODEPOINT
;
1773 *size
= ilen_orig
- ilen
;
1776 /* 2 byte, UTF16-LE encoded value. */
1777 return (codepoint_t
)SVAL(buf
, 0);
1780 /* Decode a 4 byte UTF16-LE character manually.
1781 See RFC2871 for the encoding machanism.
1783 codepoint_t w1
= SVAL(buf
,0) & ~0xD800;
1784 codepoint_t w2
= SVAL(buf
,2) & ~0xDC00;
1786 return (codepoint_t
)0x10000 +
1790 /* no other length is valid */
1791 return INVALID_CODEPOINT
;