2 Unix SMB/CIFS implementation.
3 Character set conversion Extensions
4 Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
5 Copyright (C) Andrew Tridgell 2001
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Martin Pool 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* We can parameterize this if someone complains.... JRA. */
28 char lp_failed_convert_char(void)
36 * @brief Character-set conversion routines built on our iconv.
38 * @note Samba's internal character set (at least in the 3.0 series)
39 * is always the same as the one for the Unix filesystem. It is
40 * <b>not</b> necessarily UTF-8 and may be different on machines that
41 * need i18n filenames to be compatible with Unix software. It does
42 * have to be a superset of ASCII. All multibyte sequences must start
43 * with a byte with the high bit set.
49 static smb_iconv_t conv_handles
[NUM_CHARSETS
][NUM_CHARSETS
];
50 static BOOL conv_silent
; /* Should we do a debug if the conversion fails ? */
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_UCS2
) ret
= "UTF-16LE";
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_UCS2
])
136 conv_handles
[CH_UNIX
][CH_UCS2
] = smb_iconv_open(charset_name(CH_UCS2
), "ASCII");
138 if (!conv_handles
[CH_UCS2
][CH_UNIX
])
139 conv_handles
[CH_UCS2
][CH_UNIX
] = smb_iconv_open("ASCII", charset_name(CH_UCS2
));
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
)));
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_UCS2
) {
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_UCS2
&& to
!= CH_UCS2
) {
290 /* Can't convert from ucs2 to multibyte. Replace with the default fail char. */
292 return destlen
- o_len
;
294 *outbuf
= lp_failed_convert_char();
303 if (o_len
== 0 || i_len
== 0)
304 return destlen
- o_len
;
306 /* Keep trying with the next char... */
309 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
310 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
312 return destlen
- o_len
;
314 outbuf
[0] = lp_failed_convert_char();
323 if (o_len
== 0 || i_len
== 0)
324 return destlen
- o_len
;
326 /* Keep trying with the next char... */
329 } else if (from
!= CH_UCS2
&& to
!= CH_UCS2
) {
330 /* Failed multibyte to multibyte. Just copy the default fail char and
332 outbuf
[0] = lp_failed_convert_char();
340 if (o_len
== 0 || i_len
== 0)
341 return destlen
- o_len
;
343 /* Keep trying with the next char... */
347 /* Keep compiler happy.... */
348 return destlen
- o_len
;
354 * Convert string from one encoding to another, making error checking etc
355 * Fast path version - handles ASCII first.
357 * @param src pointer to source string (multibyte or singlebyte)
358 * @param srclen length of the source string in bytes, or -1 for nul terminated.
359 * @param dest pointer to destination string (multibyte or singlebyte)
360 * @param destlen maximal length allowed for string - *NEVER* -1.
361 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
362 * @returns the number of bytes occupied in the destination
364 * Ensure the srclen contains the terminating zero.
366 * This function has been hand-tuned to provide a fast path.
367 * Don't change unless you really know what you are doing. JRA.
370 size_t convert_string(charset_t from
, charset_t to
,
371 void const *src
, size_t srclen
,
372 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
375 * NB. We deliberately don't do a strlen here if srclen == -1.
376 * This is very expensive over millions of calls and is taken
377 * care of in the slow path in convert_string_internal. JRA.
381 SMB_ASSERT(destlen
!= (size_t)-1);
387 if (from
!= CH_UCS2
&& to
!= CH_UCS2
) {
388 const unsigned char *p
= (const unsigned char *)src
;
389 unsigned char *q
= (unsigned char *)dest
;
390 size_t slen
= srclen
;
391 size_t dlen
= destlen
;
392 unsigned char lastp
= '\0';
395 /* If all characters are ascii, fast path here. */
396 while (slen
&& dlen
) {
397 if ((lastp
= *p
) <= 0x7f) {
399 if (slen
!= (size_t)-1) {
407 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
410 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
415 /* Even if we fast path we should note if we ran out of room. */
416 if (((slen
!= (size_t)-1) && slen
) ||
417 ((slen
== (size_t)-1) && lastp
)) {
422 } else if (from
== CH_UCS2
&& to
!= CH_UCS2
) {
423 const unsigned char *p
= (const unsigned char *)src
;
424 unsigned char *q
= (unsigned char *)dest
;
426 size_t slen
= srclen
;
427 size_t dlen
= destlen
;
428 unsigned char lastp
= '\0';
430 /* If all characters are ascii, fast path here. */
431 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
432 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
434 if (slen
!= (size_t)-1) {
443 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
446 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
451 /* Even if we fast path we should note if we ran out of room. */
452 if (((slen
!= (size_t)-1) && slen
) ||
453 ((slen
== (size_t)-1) && lastp
)) {
458 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
459 const unsigned char *p
= (const unsigned char *)src
;
460 unsigned char *q
= (unsigned char *)dest
;
462 size_t slen
= srclen
;
463 size_t dlen
= destlen
;
464 unsigned char lastp
= '\0';
466 /* If all characters are ascii, fast path here. */
467 while (slen
&& (dlen
>= 2)) {
468 if ((lastp
= *p
) <= 0x7F) {
471 if (slen
!= (size_t)-1) {
479 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
482 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
487 /* Even if we fast path we should note if we ran out of room. */
488 if (((slen
!= (size_t)-1) && slen
) ||
489 ((slen
== (size_t)-1) && lastp
)) {
496 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
499 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
503 * Convert between character sets, allocating a new buffer for the result.
505 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
506 * @param srclen length of source buffer.
507 * @param dest always set at least to NULL
508 * @note -1 is not accepted for srclen.
510 * @returns Size in bytes of the converted string; or -1 in case of error.
512 * Ensure the srclen contains the terminating zero.
514 * I hate the goto's in this function. It's embarressing.....
515 * There has to be a cleaner way to do this. JRA.
518 size_t convert_string_allocate(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
519 void const *src
, size_t srclen
, void **dest
, BOOL allow_bad_conv
)
521 size_t i_len
, o_len
, destlen
= MAX(srclen
, 512);
523 const char *inbuf
= (const char *)src
;
524 char *outbuf
= NULL
, *ob
= NULL
;
525 smb_iconv_t descriptor
;
529 if (src
== NULL
|| srclen
== (size_t)-1)
534 lazy_initialize_conv();
536 descriptor
= conv_handles
[from
][to
];
538 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
540 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
546 if ((destlen
*2) < destlen
) {
547 /* wrapped ! abort. */
549 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
554 destlen
= destlen
* 2;
558 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
);
560 ob
= (char *)SMB_REALLOC(ob
, destlen
);
564 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
573 retval
= smb_iconv(descriptor
,
576 if(retval
== (size_t)-1) {
577 const char *reason
="unknown error";
580 reason
="Incomplete multibyte sequence";
582 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
589 reason
="Illegal multibyte sequence";
591 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
597 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
598 /* smb_panic(reason); */
604 destlen
= destlen
- o_len
;
606 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
);
608 ob
= (char *)SMB_REALLOC(ob
,destlen
);
611 if (destlen
&& !ob
) {
612 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
622 * Conversion not supported. This is actually an error, but there are so
623 * many misconfigured iconv systems and smb.conf's out there we can't just
624 * fail. Do a very bad conversion instead.... JRA.
628 if (o_len
== 0 || i_len
== 0)
631 if (from
== CH_UCS2
&& to
!= CH_UCS2
) {
632 /* Can't convert from ucs2 to multibyte. Just use the default fail char. */
637 *outbuf
= lp_failed_convert_char();
646 if (o_len
== 0 || i_len
== 0)
649 /* Keep trying with the next char... */
652 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
653 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
657 outbuf
[0] = lp_failed_convert_char();
666 if (o_len
== 0 || i_len
== 0)
669 /* Keep trying with the next char... */
672 } else if (from
!= CH_UCS2
&& to
!= CH_UCS2
) {
673 /* Failed multibyte to multibyte. Just copy the default fail char and
675 outbuf
[0] = lp_failed_convert_char();
683 if (o_len
== 0 || i_len
== 0)
686 /* Keep trying with the next char... */
690 /* Keep compiler happy.... */
697 * Convert between character sets, allocating a new buffer using talloc for the result.
699 * @param srclen length of source buffer.
700 * @param dest always set at least to NULL
701 * @note -1 is not accepted for srclen.
703 * @returns Size in bytes of the converted string; or -1 in case of error.
705 static size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
706 void const *src
, size_t srclen
, void **dest
, BOOL allow_bad_conv
)
711 dest_len
=convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
, allow_bad_conv
);
712 if (dest_len
== (size_t)-1)
719 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
724 size
= push_ucs2_allocate(&buffer
, src
);
725 if (size
== (size_t)-1) {
726 smb_panic("failed to create UCS2 buffer");
728 if (!strupper_w(buffer
) && (dest
== src
)) {
733 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
739 strdup() a unix string to upper case.
743 char *strdup_upper(const char *s
)
746 const unsigned char *p
= (const unsigned char *)s
;
747 unsigned char *q
= (unsigned char *)out_buffer
;
749 /* this is quite a common operation, so we want it to be
750 fast. We optimise for the ascii case, knowing that all our
751 supported multi-byte character sets are ascii-compatible
752 (ie. they match for the first 128 chars) */
757 *q
++ = toupper_ascii(*p
);
761 if (p
- ( const unsigned char *)s
>= sizeof(pstring
))
769 size
= convert_string(CH_UNIX
, CH_UCS2
, s
, -1, buffer
, sizeof(buffer
), True
);
770 if (size
== (size_t)-1) {
776 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, -1, out_buffer
, sizeof(out_buffer
), True
);
777 if (size
== (size_t)-1) {
782 return SMB_STRDUP(out_buffer
);
785 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
788 smb_ucs2_t
*buffer
= NULL
;
790 size
= convert_string_allocate(NULL
, CH_UNIX
, CH_UCS2
, src
, srclen
,
791 (void **)(void *)&buffer
, True
);
792 if (size
== (size_t)-1 || !buffer
) {
793 smb_panic("failed to create UCS2 buffer");
795 if (!strlower_w(buffer
) && (dest
== src
)) {
799 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
805 strdup() a unix string to lower case.
808 char *strdup_lower(const char *s
)
811 smb_ucs2_t
*buffer
= NULL
;
814 size
= push_ucs2_allocate(&buffer
, s
);
815 if (size
== -1 || !buffer
) {
821 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
824 if (size
== (size_t)-1) {
831 static size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
833 if (flags
& (STR_NOALIGN
|STR_ASCII
))
835 return PTR_DIFF(p
, base_ptr
) & 1;
840 * Copy a string from a char* unix src to a dos codepage string destination.
842 * @return the number of bytes occupied by the string in the destination.
844 * @param flags can include
846 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
847 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
850 * @param dest_len the maximum length in bytes allowed in the
851 * destination. If @p dest_len is -1 then no maximum is used.
853 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
855 size_t src_len
= strlen(src
);
858 /* treat a pstring as "unlimited" length */
859 if (dest_len
== (size_t)-1)
860 dest_len
= sizeof(pstring
);
862 if (flags
& STR_UPPER
) {
863 pstrcpy(tmpbuf
, src
);
868 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
871 return convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
874 size_t push_ascii_fstring(void *dest
, const char *src
)
876 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
879 size_t push_ascii_pstring(void *dest
, const char *src
)
881 return push_ascii(dest
, src
, sizeof(pstring
), STR_TERMINATE
);
884 /********************************************************************
885 Push an nstring - ensure null terminated. Written by
886 moriyama@miraclelinux.com (MORIYAMA Masayuki).
887 ********************************************************************/
889 size_t push_ascii_nstring(void *dest
, const char *src
)
891 size_t i
, buffer_len
, dest_len
;
895 buffer_len
= push_ucs2_allocate(&buffer
, src
);
896 if (buffer_len
== (size_t)-1) {
897 smb_panic("failed to create UCS2 buffer");
900 /* We're using buffer_len below to count ucs2 characters, not bytes. */
901 buffer_len
/= sizeof(smb_ucs2_t
);
904 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
905 unsigned char mb
[10];
906 /* Convert one smb_ucs2_t character at a time. */
907 size_t mb_len
= convert_string(CH_UCS2
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
908 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
909 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
916 ((char *)dest
)[dest_len
] = '\0';
924 * Copy a string from a dos codepage source to a unix char* destination.
926 * The resulting string in "dest" is always null terminated.
928 * @param flags can have:
930 * <dt>STR_TERMINATE</dt>
931 * <dd>STR_TERMINATE means the string in @p src
932 * is null terminated, and src_len is ignored.</dd>
935 * @param src_len is the length of the source area in bytes.
936 * @returns the number of bytes occupied by the string in @p src.
938 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
942 if (dest_len
== (size_t)-1)
943 dest_len
= sizeof(pstring
);
945 if (flags
& STR_TERMINATE
) {
946 if (src_len
== (size_t)-1) {
947 src_len
= strlen(src
) + 1;
949 size_t len
= strnlen(src
, src_len
);
956 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
957 if (ret
== (size_t)-1) {
962 dest
[MIN(ret
, dest_len
-1)] = 0;
969 size_t pull_ascii_pstring(char *dest
, const void *src
)
971 return pull_ascii(dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
974 size_t pull_ascii_fstring(char *dest
, const void *src
)
976 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
979 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
981 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
983 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
987 * Copy a string from a char* src to a unicode destination.
989 * @returns the number of bytes occupied by the string in the destination.
991 * @param flags can have:
994 * <dt>STR_TERMINATE <dd>means include the null termination.
995 * <dt>STR_UPPER <dd>means uppercase in the destination.
996 * <dt>STR_NOALIGN <dd>means don't do alignment.
999 * @param dest_len is the maximum length allowed in the
1000 * destination. If dest_len is -1 then no maxiumum is used.
1003 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1009 /* treat a pstring as "unlimited" length */
1010 if (dest_len
== (size_t)-1)
1011 dest_len
= sizeof(pstring
);
1013 if (flags
& STR_TERMINATE
)
1014 src_len
= (size_t)-1;
1016 src_len
= strlen(src
);
1018 if (ucs2_align(base_ptr
, dest
, flags
)) {
1020 dest
= (void *)((char *)dest
+ 1);
1026 /* ucs2 is always a multiple of 2 bytes */
1029 ret
= convert_string(CH_UNIX
, CH_UCS2
, src
, src_len
, dest
, dest_len
, True
);
1030 if (ret
== (size_t)-1) {
1036 if (flags
& STR_UPPER
) {
1037 smb_ucs2_t
*dest_ucs2
= dest
;
1039 for (i
= 0; i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1040 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1041 if (v
!= dest_ucs2
[i
]) {
1052 * Copy a string from a unix char* src to a UCS2 destination,
1053 * allocating a buffer using talloc().
1055 * @param dest always set at least to NULL
1057 * @returns The number of bytes occupied by the string in the destination
1058 * or -1 in case of error.
1060 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1062 size_t src_len
= strlen(src
)+1;
1065 return convert_string_talloc(ctx
, CH_UNIX
, CH_UCS2
, src
, src_len
, (void **)dest
, True
);
1070 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1072 * @param dest always set at least to NULL
1074 * @returns The number of bytes occupied by the string in the destination
1075 * or -1 in case of error.
1078 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1080 size_t src_len
= strlen(src
)+1;
1083 return convert_string_allocate(NULL
, CH_UNIX
, CH_UCS2
, src
, src_len
, (void **)dest
, True
);
1087 Copy a string from a char* src to a UTF-8 destination.
1088 Return the number of bytes occupied by the string in the destination
1090 STR_TERMINATE means include the null termination
1091 STR_UPPER means uppercase in the destination
1092 dest_len is the maximum length allowed in the destination. If dest_len
1093 is -1 then no maxiumum is used.
1096 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1098 size_t src_len
= strlen(src
);
1101 /* treat a pstring as "unlimited" length */
1102 if (dest_len
== (size_t)-1)
1103 dest_len
= sizeof(pstring
);
1105 if (flags
& STR_UPPER
) {
1106 pstrcpy(tmpbuf
, src
);
1111 if (flags
& STR_TERMINATE
)
1114 return convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1117 size_t push_utf8_fstring(void *dest
, const char *src
)
1119 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1123 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1125 * @param dest always set at least to NULL
1127 * @returns The number of bytes occupied by the string in the destination
1130 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1132 size_t src_len
= strlen(src
)+1;
1135 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1139 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1141 * @param dest always set at least to NULL
1143 * @returns The number of bytes occupied by the string in the destination
1146 size_t push_utf8_allocate(char **dest
, const char *src
)
1148 size_t src_len
= strlen(src
)+1;
1151 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, True
);
1155 Copy a string from a ucs2 source to a unix char* destination.
1157 STR_TERMINATE means the string in src is null terminated.
1158 STR_NOALIGN means don't try to align.
1159 if STR_TERMINATE is set then src_len is ignored if it is -1.
1160 src_len is the length of the source area in bytes
1161 Return the number of bytes occupied by the string in src.
1162 The resulting string in "dest" is always null terminated.
1165 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1169 if (dest_len
== (size_t)-1)
1170 dest_len
= sizeof(pstring
);
1172 if (ucs2_align(base_ptr
, src
, flags
)) {
1173 src
= (const void *)((const char *)src
+ 1);
1174 if (src_len
!= (size_t)-1)
1178 if (flags
& STR_TERMINATE
) {
1179 /* src_len -1 is the default for null terminated strings. */
1180 if (src_len
!= (size_t)-1) {
1181 size_t len
= strnlen_w(src
, src_len
/2);
1182 if (len
< src_len
/2)
1188 /* ucs2 is always a multiple of 2 bytes */
1189 if (src_len
!= (size_t)-1)
1192 ret
= convert_string(CH_UCS2
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1193 if (ret
== (size_t)-1) {
1197 if (src_len
== (size_t)-1)
1201 dest
[MIN(ret
, dest_len
-1)] = 0;
1208 size_t pull_ucs2_pstring(char *dest
, const void *src
)
1210 return pull_ucs2(NULL
, dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1213 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1215 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1219 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1221 * @param dest always set at least to NULL
1223 * @returns The number of bytes occupied by the string in the destination
1226 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1228 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1230 return convert_string_talloc(ctx
, CH_UCS2
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1234 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1236 * @param dest always set at least to NULL
1238 * @returns The number of bytes occupied by the string in the destination
1241 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1243 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1245 return convert_string_allocate(NULL
, CH_UCS2
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1249 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1251 * @param dest always set at least to NULL
1253 * @returns The number of bytes occupied by the string in the destination
1256 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1258 size_t src_len
= strlen(src
)+1;
1260 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1264 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1266 * @param dest always set at least to NULL
1268 * @returns The number of bytes occupied by the string in the destination
1271 size_t pull_utf8_allocate(char **dest
, const char *src
)
1273 size_t src_len
= strlen(src
)+1;
1275 return convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1279 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1281 * @param dest always set at least to NULL
1283 * @returns The number of bytes occupied by the string in the destination
1286 size_t pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1288 size_t src_len
= strlen(src
)+1;
1290 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1294 Copy a string from a char* src to a unicode or ascii
1295 dos codepage destination choosing unicode or ascii based on the
1296 flags in the SMB buffer starting at base_ptr.
1297 Return the number of bytes occupied by the string in the destination.
1299 STR_TERMINATE means include the null termination.
1300 STR_UPPER means uppercase in the destination.
1301 STR_ASCII use ascii even with unicode packet.
1302 STR_NOALIGN means don't do alignment.
1303 dest_len is the maximum length allowed in the destination. If dest_len
1304 is -1 then no maxiumum is used.
1307 size_t push_string_fn(const char *function
, unsigned int line
, const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1310 /* We really need to zero fill here, not clobber
1311 * region, as we want to ensure that valgrind thinks
1312 * all of the outgoing buffer has been written to
1313 * so a send() or write() won't trap an error.
1317 if (dest_len
!= (size_t)-1)
1318 clobber_region(function
, line
, dest
, dest_len
);
1320 if (dest_len
!= (size_t)-1)
1321 memset(dest
, '\0', dest_len
);
1325 if (!(flags
& STR_ASCII
) && \
1326 ((flags
& STR_UNICODE
|| \
1327 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1328 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1330 return push_ascii(dest
, src
, dest_len
, flags
);
1335 Copy a string from a unicode or ascii source (depending on
1336 the packet flags) to a char* destination.
1338 STR_TERMINATE means the string in src is null terminated.
1339 STR_UNICODE means to force as unicode.
1340 STR_ASCII use ascii even with unicode packet.
1341 STR_NOALIGN means don't do alignment.
1342 if STR_TERMINATE is set then src_len is ignored is it is -1
1343 src_len is the length of the source area in bytes.
1344 Return the number of bytes occupied by the string in src.
1345 The resulting string in "dest" is always null terminated.
1348 size_t pull_string_fn(const char *function
, unsigned int line
, const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1351 if (dest_len
!= (size_t)-1)
1352 clobber_region(function
, line
, dest
, dest_len
);
1355 if (!(flags
& STR_ASCII
) && \
1356 ((flags
& STR_UNICODE
|| \
1357 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1358 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1360 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1363 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1365 if (!(flags
& STR_ASCII
) && \
1366 ((flags
& STR_UNICODE
|| \
1367 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1368 return ucs2_align(base_ptr
, p
, flags
);
1373 /****************************************************************
1374 Calculate the size (in bytes) of the next multibyte character in
1375 our internal character set. Note that p must be pointing to a
1376 valid mb char, not within one.
1377 ****************************************************************/
1379 size_t next_mb_char_size(const char *s
)
1384 return 1; /* ascii. */
1387 for ( i
= 1; i
<=4; i
++ ) {
1389 if (convert_string(CH_UNIX
, CH_UCS2
, s
, i
, &uc
, 2, False
) == 2) {
1391 DEBUG(10,("next_mb_char_size: size %u at string %s\n",
1392 (unsigned int)i
, s
));
1394 conv_silent
= False
;
1398 /* We're hosed - we don't know how big this is... */
1399 DEBUG(10,("next_mb_char_size: unknown size at string %s\n", s
));
1400 conv_silent
= False
;