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 ? */
50 static bool initialized
;
53 * Return the name of a charset to give to iconv().
55 static const char *charset_name(charset_t ch
)
57 const char *ret
= NULL
;
59 if (ch
== CH_UTF16LE
) ret
= "UTF-16LE";
60 else if (ch
== CH_UTF16BE
) ret
= "UTF-16BE";
61 else if (ch
== CH_UNIX
) ret
= lp_unix_charset();
62 else if (ch
== CH_DOS
) ret
= lp_dos_charset();
63 else if (ch
== CH_DISPLAY
) ret
= lp_display_charset();
64 else if (ch
== CH_UTF8
) ret
= "UTF8";
66 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
67 if (ret
&& !strcmp(ret
, "LOCALE")) {
68 const char *ln
= NULL
;
71 setlocale(LC_ALL
, "");
73 ln
= nl_langinfo(CODESET
);
75 /* Check whether the charset name is supported
77 smb_iconv_t handle
= smb_iconv_open(ln
,"UCS-2LE");
78 if (handle
== (smb_iconv_t
) -1) {
79 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln
));
82 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln
));
83 smb_iconv_close(handle
);
90 if (!ret
|| !*ret
) ret
= "ASCII";
94 void lazy_initialize_conv(void)
104 * Destroy global objects allocated by init_iconv()
106 void gfree_charcnv(void)
110 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
111 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
112 if ( conv_handles
[c1
][c2
] ) {
113 smb_iconv_close( conv_handles
[c1
][c2
] );
114 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
));
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 size_t ret
= convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
417 if (ret
== (size_t)-1) {
425 /* Even if we fast path we should note if we ran out of room. */
426 if (((slen
!= (size_t)-1) && slen
) ||
427 ((slen
== (size_t)-1) && lastp
)) {
432 } else if (from
== CH_UTF16LE
&& to
!= CH_UTF16LE
) {
433 const unsigned char *p
= (const unsigned char *)src
;
434 unsigned char *q
= (unsigned char *)dest
;
436 size_t slen
= srclen
;
437 size_t dlen
= destlen
;
438 unsigned char lastp
= '\0';
440 /* If all characters are ascii, fast path here. */
441 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
442 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
444 if (slen
!= (size_t)-1) {
453 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
456 size_t ret
= convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
457 if (ret
== (size_t)-1) {
465 /* Even if we fast path we should note if we ran out of room. */
466 if (((slen
!= (size_t)-1) && slen
) ||
467 ((slen
== (size_t)-1) && lastp
)) {
472 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
473 const unsigned char *p
= (const unsigned char *)src
;
474 unsigned char *q
= (unsigned char *)dest
;
476 size_t slen
= srclen
;
477 size_t dlen
= destlen
;
478 unsigned char lastp
= '\0';
480 /* If all characters are ascii, fast path here. */
481 while (slen
&& (dlen
>= 2)) {
482 if ((lastp
= *p
) <= 0x7F) {
485 if (slen
!= (size_t)-1) {
493 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
496 size_t ret
= convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
497 if (ret
== (size_t)-1) {
505 /* Even if we fast path we should note if we ran out of room. */
506 if (((slen
!= (size_t)-1) && slen
) ||
507 ((slen
== (size_t)-1) && lastp
)) {
514 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
517 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
521 * Convert between character sets, allocating a new buffer using talloc for the result.
523 * @param srclen length of source buffer.
524 * @param dest always set at least to NULL
525 * @parm converted_size set to the number of bytes occupied by the string in
526 * the destination on success.
527 * @note -1 is not accepted for srclen.
529 * @return true if new buffer was correctly allocated, and string was
532 * Ensure the srclen contains the terminating zero.
534 * I hate the goto's in this function. It's embarressing.....
535 * There has to be a cleaner way to do this. JRA.
537 bool convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
538 void const *src
, size_t srclen
, void *dst
,
539 size_t *converted_size
, bool allow_bad_conv
)
542 size_t i_len
, o_len
, destlen
= (srclen
* 3) / 2;
544 const char *inbuf
= (const char *)src
;
545 char *outbuf
= NULL
, *ob
= NULL
;
546 smb_iconv_t descriptor
;
547 void **dest
= (void **)dst
;
551 if (!converted_size
) {
556 if (src
== NULL
|| srclen
== (size_t)-1) {
561 ob
= ((ctx
!= NULL
) ? talloc_strdup(ctx
, "") : SMB_STRDUP(""));
571 lazy_initialize_conv();
573 descriptor
= conv_handles
[from
][to
];
575 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
577 DEBUG(0,("convert_string_talloc: Conversion not supported.\n"));
584 /* +2 is for ucs2 null termination. */
585 if ((destlen
*2)+2 < destlen
) {
586 /* wrapped ! abort. */
588 DEBUG(0, ("convert_string_talloc: destlen wrapped !\n"));
594 destlen
= destlen
* 2;
597 /* +2 is for ucs2 null termination. */
598 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
+ 2);
601 DEBUG(0, ("convert_string_talloc: realloc failed!\n"));
611 retval
= smb_iconv(descriptor
,
614 if(retval
== (size_t)-1) {
615 const char *reason
="unknown error";
618 reason
="Incomplete multibyte sequence";
620 DEBUG(3,("convert_string_talloc: Conversion error: %s(%s)\n",reason
,inbuf
));
627 reason
="Illegal multibyte sequence";
629 DEBUG(3,("convert_string_talloc: Conversion error: %s(%s)\n",reason
,inbuf
));
635 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
636 /* smb_panic(reason); */
643 destlen
= destlen
- o_len
;
644 /* Don't shrink unless we're reclaiming a lot of
645 * space. This is in the hot codepath and these
646 * reallocs *cost*. JRA.
649 /* We're shrinking here so we know the +2 is safe from wrap. */
650 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
+ 2);
653 if (destlen
&& !ob
) {
654 DEBUG(0, ("convert_string_talloc: out of memory!\n"));
661 /* Must ucs2 null terminate in the extra space we allocated. */
663 ob
[destlen
+1] = '\0';
665 *converted_size
= destlen
;
671 * Conversion not supported. This is actually an error, but there are so
672 * many misconfigured iconv systems and smb.conf's out there we can't just
673 * fail. Do a very bad conversion instead.... JRA.
677 if (o_len
== 0 || i_len
== 0)
680 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
681 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
682 /* Can't convert from utf16 any endian to multibyte.
683 Replace with the default fail char.
690 *outbuf
= lp_failed_convert_char();
699 if (o_len
== 0 || i_len
== 0)
702 /* Keep trying with the next char... */
705 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
706 /* Can't convert to UTF16LE - just widen by adding the
707 default fail char then zero.
712 outbuf
[0] = lp_failed_convert_char();
721 if (o_len
== 0 || i_len
== 0)
724 /* Keep trying with the next char... */
727 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
728 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
729 /* Failed multibyte to multibyte. Just copy the default fail char and
731 outbuf
[0] = lp_failed_convert_char();
739 if (o_len
== 0 || i_len
== 0)
742 /* Keep trying with the next char... */
746 /* Keep compiler happy.... */
752 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
757 if (!push_ucs2_talloc(NULL
, &buffer
, src
, &size
)) {
761 if (!strupper_w(buffer
) && (dest
== src
)) {
766 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
772 talloc_strdup() a unix string to upper case.
775 char *talloc_strdup_upper(TALLOC_CTX
*ctx
, const char *s
)
777 char *out_buffer
= talloc_strdup(ctx
,s
);
778 const unsigned char *p
= (const unsigned char *)s
;
779 unsigned char *q
= (unsigned char *)out_buffer
;
785 /* this is quite a common operation, so we want it to be
786 fast. We optimise for the ascii case, knowing that all our
787 supported multi-byte character sets are ascii-compatible
788 (ie. they match for the first 128 chars) */
793 *q
++ = toupper_ascii_fast(*p
);
799 size_t converted_size
, converted_size2
;
800 smb_ucs2_t
*ubuf
= NULL
;
802 /* We're not using the ascii buffer above. */
803 TALLOC_FREE(out_buffer
);
805 if (!convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, s
,
806 strlen(s
)+1, (void *)&ubuf
,
807 &converted_size
, True
))
814 if (!convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, ubuf
,
815 converted_size
, (void *)&out_buffer
,
816 &converted_size2
, True
))
822 /* Don't need the intermediate buffer
831 char *strupper_talloc(TALLOC_CTX
*ctx
, const char *s
) {
832 return talloc_strdup_upper(ctx
, s
);
836 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
839 smb_ucs2_t
*buffer
= NULL
;
841 if (!convert_string_talloc(NULL
, CH_UNIX
, CH_UTF16LE
, src
, srclen
,
842 (void **)(void *)&buffer
, &size
,
845 smb_panic("failed to create UCS2 buffer");
847 if (!strlower_w(buffer
) && (dest
== src
)) {
851 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
857 char *talloc_strdup_lower(TALLOC_CTX
*ctx
, const char *s
)
859 size_t converted_size
;
860 smb_ucs2_t
*buffer
= NULL
;
863 if (!push_ucs2_talloc(ctx
, &buffer
, s
, &converted_size
)) {
869 if (!pull_ucs2_talloc(ctx
, &out_buffer
, buffer
, &converted_size
)) {
879 char *strlower_talloc(TALLOC_CTX
*ctx
, const char *s
) {
880 return talloc_strdup_lower(ctx
, s
);
883 size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
885 if (flags
& (STR_NOALIGN
|STR_ASCII
))
887 return PTR_DIFF(p
, base_ptr
) & 1;
892 * Copy a string from a char* unix src to a dos codepage string destination.
894 * @return the number of bytes occupied by the string in the destination.
896 * @param flags can include
898 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
899 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
902 * @param dest_len the maximum length in bytes allowed in the
905 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
907 size_t src_len
= strlen(src
);
911 /* No longer allow a length of -1. */
912 if (dest_len
== (size_t)-1) {
913 smb_panic("push_ascii - dest_len == -1");
916 if (flags
& STR_UPPER
) {
917 tmpbuf
= SMB_STRDUP(src
);
919 smb_panic("malloc fail");
925 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
)) {
929 ret
= convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
930 if (ret
== (size_t)-1 &&
931 (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
933 ((char *)dest
)[0] = '\0';
939 size_t push_ascii_fstring(void *dest
, const char *src
)
941 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
944 /********************************************************************
945 Push an nstring - ensure null terminated. Written by
946 moriyama@miraclelinux.com (MORIYAMA Masayuki).
947 ********************************************************************/
949 size_t push_ascii_nstring(void *dest
, const char *src
)
951 size_t i
, buffer_len
, dest_len
;
955 if (!push_ucs2_talloc(NULL
, &buffer
, src
, &buffer_len
)) {
956 smb_panic("failed to create UCS2 buffer");
959 /* We're using buffer_len below to count ucs2 characters, not bytes. */
960 buffer_len
/= sizeof(smb_ucs2_t
);
963 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
964 unsigned char mb
[10];
965 /* Convert one smb_ucs2_t character at a time. */
966 size_t mb_len
= convert_string(CH_UTF16LE
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
967 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
968 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
975 ((char *)dest
)[dest_len
] = '\0';
982 /********************************************************************
983 Push and malloc an ascii string. src and dest null terminated.
984 ********************************************************************/
986 bool push_ascii_talloc(TALLOC_CTX
*mem_ctx
, char **dest
, const char *src
, size_t *converted_size
)
988 size_t src_len
= strlen(src
)+1;
991 return convert_string_talloc(mem_ctx
, CH_UNIX
, CH_DOS
, src
, src_len
,
992 (void **)dest
, converted_size
, True
);
996 * Copy a string from a dos codepage source to a unix char* destination.
998 * The resulting string in "dest" is always null terminated.
1000 * @param flags can have:
1002 * <dt>STR_TERMINATE</dt>
1003 * <dd>STR_TERMINATE means the string in @p src
1004 * is null terminated, and src_len is ignored.</dd>
1007 * @param src_len is the length of the source area in bytes.
1008 * @returns the number of bytes occupied by the string in @p src.
1010 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1014 if (dest_len
== (size_t)-1) {
1015 /* No longer allow dest_len of -1. */
1016 smb_panic("pull_ascii - invalid dest_len of -1");
1019 if (flags
& STR_TERMINATE
) {
1020 if (src_len
== (size_t)-1) {
1021 src_len
= strlen((const char *)src
) + 1;
1023 size_t len
= strnlen((const char *)src
, src_len
);
1030 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1031 if (ret
== (size_t)-1) {
1036 if (dest_len
&& ret
) {
1037 /* Did we already process the terminating zero ? */
1038 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1039 dest
[MIN(ret
, dest_len
-1)] = 0;
1049 * Copy a string from a dos codepage source to a unix char* destination.
1051 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1054 * The resulting string in "dest" is always null terminated.
1056 * @param flags can have:
1058 * <dt>STR_TERMINATE</dt>
1059 * <dd>STR_TERMINATE means the string in @p src
1060 * is null terminated, and src_len is ignored.</dd>
1063 * @param src_len is the length of the source area in bytes.
1064 * @returns the number of bytes occupied by the string in @p src.
1067 static size_t pull_ascii_base_talloc(TALLOC_CTX
*ctx
,
1082 if (flags
& STR_TERMINATE
) {
1083 if (src_len
== (size_t)-1) {
1084 src_len
= strlen((const char *)src
) + 1;
1086 size_t len
= strnlen((const char *)src
, src_len
);
1091 /* Ensure we don't use an insane length from the client. */
1092 if (src_len
>= 1024*1024) {
1093 char *msg
= talloc_asprintf(ctx
,
1094 "Bad src length (%u) in "
1095 "pull_ascii_base_talloc",
1096 (unsigned int)src_len
);
1100 /* Can't have an unlimited length
1101 * non STR_TERMINATE'd.
1103 if (src_len
== (size_t)-1) {
1109 /* src_len != -1 here. */
1111 if (!convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, &dest
,
1116 if (dest_len
&& dest
) {
1117 /* Did we already process the terminating zero ? */
1118 if (dest
[dest_len
-1] != 0) {
1119 size_t size
= talloc_get_size(dest
);
1120 /* Have we got space to append the '\0' ? */
1121 if (size
<= dest_len
) {
1123 dest
= TALLOC_REALLOC_ARRAY(ctx
, dest
, char,
1127 dest_len
= (size_t)-1;
1132 dest
[dest_len
] = '\0';
1143 size_t pull_ascii_fstring(char *dest
, const void *src
)
1145 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1148 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1150 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
1152 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
1156 * Copy a string from a char* src to a unicode destination.
1158 * @returns the number of bytes occupied by the string in the destination.
1160 * @param flags can have:
1163 * <dt>STR_TERMINATE <dd>means include the null termination.
1164 * <dt>STR_UPPER <dd>means uppercase in the destination.
1165 * <dt>STR_NOALIGN <dd>means don't do alignment.
1168 * @param dest_len is the maximum length allowed in the
1172 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1178 if (dest_len
== (size_t)-1) {
1179 /* No longer allow dest_len of -1. */
1180 smb_panic("push_ucs2 - invalid dest_len of -1");
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) {
1201 if ((flags
& STR_TERMINATE
) &&
1211 if (flags
& STR_UPPER
) {
1212 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1215 /* We check for i < (ret / 2) below as the dest string isn't null
1216 terminated if STR_TERMINATE isn't set. */
1218 for (i
= 0; i
< (ret
/ 2) && i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1219 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1220 if (v
!= dest_ucs2
[i
]) {
1231 * Copy a string from a unix char* src to a UCS2 destination,
1232 * allocating a buffer using talloc().
1234 * @param dest always set at least to NULL
1235 * @parm converted_size set to the number of bytes occupied by the string in
1236 * the destination on success.
1238 * @return true if new buffer was correctly allocated, and string was
1241 bool push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
,
1242 size_t *converted_size
)
1244 size_t src_len
= strlen(src
)+1;
1247 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, src
, src_len
,
1248 (void **)dest
, converted_size
, True
);
1253 Copy a string from a char* src to a UTF-8 destination.
1254 Return the number of bytes occupied by the string in the destination
1256 STR_TERMINATE means include the null termination
1257 STR_UPPER means uppercase in the destination
1258 dest_len is the maximum length allowed in the destination. If dest_len
1259 is -1 then no maxiumum is used.
1262 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1266 char *tmpbuf
= NULL
;
1268 if (dest_len
== (size_t)-1) {
1269 /* No longer allow dest_len of -1. */
1270 smb_panic("push_utf8 - invalid dest_len of -1");
1273 if (flags
& STR_UPPER
) {
1274 tmpbuf
= strupper_talloc(NULL
, src
);
1279 src_len
= strlen(src
);
1282 src_len
= strlen(src
);
1283 if (flags
& STR_TERMINATE
) {
1287 ret
= convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1288 TALLOC_FREE(tmpbuf
);
1292 size_t push_utf8_fstring(void *dest
, const char *src
)
1294 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1298 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1300 * @param dest always set at least to NULL
1301 * @parm converted_size set to the number of bytes occupied by the string in
1302 * the destination on success.
1304 * @return true if new buffer was correctly allocated, and string was
1308 bool push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
,
1309 size_t *converted_size
)
1311 size_t src_len
= strlen(src
)+1;
1314 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
,
1315 (void**)dest
, converted_size
, True
);
1319 Copy a string from a ucs2 source to a unix char* destination.
1321 STR_TERMINATE means the string in src is null terminated.
1322 STR_NOALIGN means don't try to align.
1323 if STR_TERMINATE is set then src_len is ignored if it is -1.
1324 src_len is the length of the source area in bytes
1325 Return the number of bytes occupied by the string in src.
1326 The resulting string in "dest" is always null terminated.
1329 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1333 if (dest_len
== (size_t)-1) {
1334 /* No longer allow dest_len of -1. */
1335 smb_panic("pull_ucs2 - invalid dest_len of -1");
1339 if (dest
&& dest_len
> 0) {
1345 if (ucs2_align(base_ptr
, src
, flags
)) {
1346 src
= (const void *)((const char *)src
+ 1);
1347 if (src_len
!= (size_t)-1)
1351 if (flags
& STR_TERMINATE
) {
1352 /* src_len -1 is the default for null terminated strings. */
1353 if (src_len
!= (size_t)-1) {
1354 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1356 if (len
< src_len
/2)
1362 /* ucs2 is always a multiple of 2 bytes */
1363 if (src_len
!= (size_t)-1)
1366 ret
= convert_string(CH_UTF16LE
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1367 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 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");
1424 if (ucs2_align(base_ptr
, src
, flags
)) {
1425 src
= (const void *)((const char *)src
+ 1);
1426 if (src_len
!= (size_t)-1)
1430 if (flags
& STR_TERMINATE
) {
1431 /* src_len -1 is the default for null terminated strings. */
1432 if (src_len
!= (size_t)-1) {
1433 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1435 if (len
< src_len
/2)
1440 * src_len == -1 - alloc interface won't take this
1441 * so we must calculate.
1443 src_len
= (strlen_w((const smb_ucs2_t
*)src
)+1)*sizeof(smb_ucs2_t
);
1445 /* Ensure we don't use an insane length from the client. */
1446 if (src_len
>= 1024*1024) {
1447 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1450 /* Can't have an unlimited length
1451 * non STR_TERMINATE'd.
1453 if (src_len
== (size_t)-1) {
1459 /* src_len != -1 here. */
1461 /* ucs2 is always a multiple of 2 bytes */
1464 if (!convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
,
1465 (void *)&dest
, &dest_len
, True
)) {
1470 /* Did we already process the terminating zero ? */
1471 if (dest
[dest_len
-1] != 0) {
1472 size_t size
= talloc_get_size(dest
);
1473 /* Have we got space to append the '\0' ? */
1474 if (size
<= dest_len
) {
1476 dest
= TALLOC_REALLOC_ARRAY(ctx
, dest
, char,
1480 dest_len
= (size_t)-1;
1485 dest
[dest_len
] = '\0';
1496 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1498 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1502 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1504 * @param dest always set at least to NULL
1505 * @parm converted_size set to the number of bytes occupied by the string in
1506 * the destination on success.
1508 * @return true if new buffer was correctly allocated, and string was
1512 bool pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
,
1513 size_t *converted_size
)
1515 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1518 return convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
,
1519 (void **)dest
, converted_size
, True
);
1523 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1525 * @param dest always set at least to NULL
1526 * @parm converted_size set to the number of bytes occupied by the string in
1527 * the destination on success.
1529 * @return true if new buffer was correctly allocated, and string was
1533 bool pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
,
1534 size_t *converted_size
)
1536 size_t src_len
= strlen(src
)+1;
1539 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
,
1540 (void **)dest
, converted_size
, True
);
1545 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1547 * @param dest always set at least to NULL
1548 * @parm converted_size set to the number of bytes occupied by the string in
1549 * the destination on success.
1551 * @return true if new buffer was correctly allocated, and string was
1555 bool pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
,
1556 size_t *converted_size
)
1558 size_t src_len
= strlen(src
)+1;
1561 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
,
1562 (void **)dest
, converted_size
, True
);
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 size_t push_string_check_fn(const char *function
, unsigned int line
,
1580 void *dest
, const char *src
,
1581 size_t dest_len
, int flags
)
1584 /* We really need to zero fill here, not clobber
1585 * region, as we want to ensure that valgrind thinks
1586 * all of the outgoing buffer has been written to
1587 * so a send() or write() won't trap an error.
1591 clobber_region(function
, line
, dest
, dest_len
);
1593 memset(dest
, '\0', dest_len
);
1597 if (!(flags
& STR_ASCII
) && (flags
& STR_UNICODE
)) {
1598 return push_ucs2(NULL
, dest
, src
, dest_len
, flags
);
1600 return push_ascii(dest
, src
, dest_len
, flags
);
1605 Copy a string from a char* src to a unicode or ascii
1606 dos codepage destination choosing unicode or ascii based on the
1607 flags in the SMB buffer starting at base_ptr.
1608 Return the number of bytes occupied by the string in the destination.
1610 STR_TERMINATE means include the null termination.
1611 STR_UPPER means uppercase in the destination.
1612 STR_ASCII use ascii even with unicode packet.
1613 STR_NOALIGN means don't do alignment.
1614 dest_len is the maximum length allowed in the destination. If dest_len
1615 is -1 then no maxiumum is used.
1618 size_t push_string_base(const char *function
, unsigned int line
,
1619 const char *base
, uint16 flags2
,
1620 void *dest
, const char *src
,
1621 size_t dest_len
, int flags
)
1624 /* We really need to zero fill here, not clobber
1625 * region, as we want to ensure that valgrind thinks
1626 * all of the outgoing buffer has been written to
1627 * so a send() or write() won't trap an error.
1631 clobber_region(function
, line
, dest
, dest_len
);
1633 memset(dest
, '\0', dest_len
);
1637 if (!(flags
& STR_ASCII
) && \
1638 ((flags
& STR_UNICODE
|| \
1639 (flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1640 return push_ucs2(base
, dest
, src
, dest_len
, flags
);
1642 return push_ascii(dest
, src
, dest_len
, flags
);
1646 Copy a string from a char* src to a unicode or ascii
1647 dos codepage destination choosing unicode or ascii based on the
1649 Return the number of bytes occupied by the string in the destination.
1651 STR_TERMINATE means include the null termination.
1652 STR_UPPER means uppercase in the destination.
1653 STR_ASCII use ascii even with unicode packet.
1654 STR_NOALIGN means don't do alignment.
1655 dest_len is the maximum length allowed in the destination. If dest_len
1656 is -1 then no maxiumum is used.
1659 ssize_t
push_string(void *dest
, const char *src
, size_t dest_len
, int flags
)
1663 /* We really need to zero fill here, not clobber
1664 * region, as we want to ensure that valgrind thinks
1665 * all of the outgoing buffer has been written to
1666 * so a send() or write() won't trap an error.
1669 memset(dest
, '\0', dest_len
);
1672 if (!(flags
& STR_ASCII
) && \
1673 (flags
& STR_UNICODE
)) {
1674 ret
= push_ucs2(NULL
, dest
, src
, dest_len
, flags
);
1676 ret
= push_ascii(dest
, src
, dest_len
, flags
);
1678 if (ret
== (size_t)-1) {
1685 Copy a string from a unicode or ascii source (depending on
1686 the packet flags) to a char* destination.
1688 STR_TERMINATE means the string in src is null terminated.
1689 STR_UNICODE means to force as unicode.
1690 STR_ASCII use ascii even with unicode packet.
1691 STR_NOALIGN means don't do alignment.
1692 if STR_TERMINATE is set then src_len is ignored is it is -1
1693 src_len is the length of the source area in bytes.
1694 Return the number of bytes occupied by the string in src.
1695 The resulting string in "dest" is always null terminated.
1698 size_t pull_string_fn(const char *function
,
1700 const void *base_ptr
,
1709 clobber_region(function
, line
, dest
, dest_len
);
1712 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1713 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1717 if (!(flags
& STR_ASCII
) && \
1718 ((flags
& STR_UNICODE
|| \
1719 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1720 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1722 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1726 Copy a string from a unicode or ascii source (depending on
1727 the packet flags) to a char* destination.
1728 Variant that uses talloc.
1730 STR_TERMINATE means the string in src is null terminated.
1731 STR_UNICODE means to force as unicode.
1732 STR_ASCII use ascii even with unicode packet.
1733 STR_NOALIGN means don't do alignment.
1734 if STR_TERMINATE is set then src_len is ignored is it is -1
1735 src_len is the length of the source area in bytes.
1736 Return the number of bytes occupied by the string in src.
1737 The resulting string in "dest" is always null terminated.
1740 size_t pull_string_talloc_fn(const char *function
,
1743 const void *base_ptr
,
1750 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1751 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1755 if (!(flags
& STR_ASCII
) && \
1756 ((flags
& STR_UNICODE
|| \
1757 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1758 return pull_ucs2_base_talloc(ctx
,
1765 return pull_ascii_base_talloc(ctx
,
1773 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1775 if (!(flags
& STR_ASCII
) && \
1776 ((flags
& STR_UNICODE
|| \
1777 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1778 return ucs2_align(base_ptr
, p
, flags
);
1784 Return the unicode codepoint for the next multi-byte CH_UNIX character
1785 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1787 Also return the number of bytes consumed (which tells the caller
1788 how many bytes to skip to get to the next CH_UNIX character).
1790 Return INVALID_CODEPOINT if the next character cannot be converted.
1793 codepoint_t
next_codepoint(const char *str
, size_t *size
)
1795 /* It cannot occupy more than 4 bytes in UTF16 format */
1797 smb_iconv_t descriptor
;
1803 if ((str
[0] & 0x80) == 0) {
1805 return (codepoint_t
)str
[0];
1808 /* We assume that no multi-byte character can take
1809 more than 5 bytes. This is OK as we only
1810 support codepoints up to 1M */
1812 ilen_orig
= strnlen(str
, 5);
1815 lazy_initialize_conv();
1817 descriptor
= conv_handles
[CH_UNIX
][CH_UTF16LE
];
1818 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
1820 return INVALID_CODEPOINT
;
1823 /* This looks a little strange, but it is needed to cope
1824 with codepoints above 64k which are encoded as per RFC2781. */
1826 outbuf
= (char *)buf
;
1827 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1829 /* We failed to convert to a 2 byte character.
1830 See if we can convert to a 4 UTF16-LE byte char encoding.
1833 outbuf
= (char *)buf
;
1834 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1836 /* We didn't convert any bytes */
1838 return INVALID_CODEPOINT
;
1845 *size
= ilen_orig
- ilen
;
1848 /* 2 byte, UTF16-LE encoded value. */
1849 return (codepoint_t
)SVAL(buf
, 0);
1852 /* Decode a 4 byte UTF16-LE character manually.
1853 See RFC2871 for the encoding machanism.
1855 codepoint_t w1
= SVAL(buf
,0) & ~0xD800;
1856 codepoint_t w2
= SVAL(buf
,2) & ~0xDC00;
1858 return (codepoint_t
)0x10000 +
1862 /* no other length is valid */
1863 return INVALID_CODEPOINT
;
1867 push a single codepoint into a CH_UNIX string the target string must
1868 be able to hold the full character, which is guaranteed if it is at
1869 least 5 bytes in size. The caller may pass less than 5 bytes if they
1870 are sure the character will fit (for example, you can assume that
1871 uppercase/lowercase of a character will not add more than 1 byte)
1873 return the number of bytes occupied by the CH_UNIX character, or
1876 _PUBLIC_ ssize_t
push_codepoint(char *str
, codepoint_t c
)
1878 smb_iconv_t descriptor
;
1888 lazy_initialize_conv();
1890 descriptor
= conv_handles
[CH_UNIX
][CH_UTF16LE
];
1891 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
1898 inbuf
= (char *)buf
;
1900 smb_iconv(descriptor
, &inbuf
, &ilen
, &str
, &olen
);
1909 buf
[0] = (c
>>10) & 0xFF;
1910 buf
[1] = (c
>>18) | 0xd8;
1912 buf
[3] = ((c
>>8) & 0x3) | 0xdc;
1916 inbuf
= (char *)buf
;
1918 smb_iconv(descriptor
, &inbuf
, &ilen
, &str
, &olen
);