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 *dst
, 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
;
526 void **dest
= (void **)dst
;
530 if (src
== NULL
|| srclen
== (size_t)-1)
535 lazy_initialize_conv();
537 descriptor
= conv_handles
[from
][to
];
539 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
541 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
547 if ((destlen
*2) < destlen
) {
548 /* wrapped ! abort. */
550 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
555 destlen
= destlen
* 2;
559 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
);
561 ob
= (char *)SMB_REALLOC(ob
, destlen
);
565 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
574 retval
= smb_iconv(descriptor
,
577 if(retval
== (size_t)-1) {
578 const char *reason
="unknown error";
581 reason
="Incomplete multibyte sequence";
583 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
590 reason
="Illegal multibyte sequence";
592 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
598 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
599 /* smb_panic(reason); */
605 destlen
= destlen
- o_len
;
607 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
);
609 ob
= (char *)SMB_REALLOC(ob
,destlen
);
612 if (destlen
&& !ob
) {
613 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
623 * Conversion not supported. This is actually an error, but there are so
624 * many misconfigured iconv systems and smb.conf's out there we can't just
625 * fail. Do a very bad conversion instead.... JRA.
629 if (o_len
== 0 || i_len
== 0)
632 if (from
== CH_UCS2
&& to
!= CH_UCS2
) {
633 /* Can't convert from ucs2 to multibyte. Just use the default fail char. */
638 *outbuf
= lp_failed_convert_char();
647 if (o_len
== 0 || i_len
== 0)
650 /* Keep trying with the next char... */
653 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
654 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
658 outbuf
[0] = lp_failed_convert_char();
667 if (o_len
== 0 || i_len
== 0)
670 /* Keep trying with the next char... */
673 } else if (from
!= CH_UCS2
&& to
!= CH_UCS2
) {
674 /* Failed multibyte to multibyte. Just copy the default fail char and
676 outbuf
[0] = lp_failed_convert_char();
684 if (o_len
== 0 || i_len
== 0)
687 /* Keep trying with the next char... */
691 /* Keep compiler happy.... */
698 * Convert between character sets, allocating a new buffer using talloc for the result.
700 * @param srclen length of source buffer.
701 * @param dest always set at least to NULL
702 * @note -1 is not accepted for srclen.
704 * @returns Size in bytes of the converted string; or -1 in case of error.
706 size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
707 void const *src
, size_t srclen
, void *dst
,
710 void **dest
= (void **)dst
;
714 dest_len
=convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
, allow_bad_conv
);
715 if (dest_len
== (size_t)-1)
722 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
727 size
= push_ucs2_allocate(&buffer
, src
);
728 if (size
== (size_t)-1) {
729 smb_panic("failed to create UCS2 buffer");
731 if (!strupper_w(buffer
) && (dest
== src
)) {
736 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
742 strdup() a unix string to upper case.
746 char *strdup_upper(const char *s
)
749 const unsigned char *p
= (const unsigned char *)s
;
750 unsigned char *q
= (unsigned char *)out_buffer
;
752 /* this is quite a common operation, so we want it to be
753 fast. We optimise for the ascii case, knowing that all our
754 supported multi-byte character sets are ascii-compatible
755 (ie. they match for the first 128 chars) */
760 *q
++ = toupper_ascii(*p
);
764 if (p
- ( const unsigned char *)s
>= sizeof(pstring
))
772 size
= convert_string(CH_UNIX
, CH_UCS2
, s
, -1, buffer
, sizeof(buffer
), True
);
773 if (size
== (size_t)-1) {
779 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, -1, out_buffer
, sizeof(out_buffer
), True
);
780 if (size
== (size_t)-1) {
785 return SMB_STRDUP(out_buffer
);
788 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
791 smb_ucs2_t
*buffer
= NULL
;
793 size
= convert_string_allocate(NULL
, CH_UNIX
, CH_UCS2
, src
, srclen
,
794 (void **)(void *)&buffer
, True
);
795 if (size
== (size_t)-1 || !buffer
) {
796 smb_panic("failed to create UCS2 buffer");
798 if (!strlower_w(buffer
) && (dest
== src
)) {
802 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
808 strdup() a unix string to lower case.
811 char *strdup_lower(const char *s
)
814 smb_ucs2_t
*buffer
= NULL
;
817 size
= push_ucs2_allocate(&buffer
, s
);
818 if (size
== -1 || !buffer
) {
824 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
827 if (size
== (size_t)-1) {
834 static size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
836 if (flags
& (STR_NOALIGN
|STR_ASCII
))
838 return PTR_DIFF(p
, base_ptr
) & 1;
843 * Copy a string from a char* unix src to a dos codepage string destination.
845 * @return the number of bytes occupied by the string in the destination.
847 * @param flags can include
849 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
850 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
853 * @param dest_len the maximum length in bytes allowed in the
854 * destination. If @p dest_len is -1 then no maximum is used.
856 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
858 size_t src_len
= strlen(src
);
861 /* treat a pstring as "unlimited" length */
862 if (dest_len
== (size_t)-1)
863 dest_len
= sizeof(pstring
);
865 if (flags
& STR_UPPER
) {
866 pstrcpy(tmpbuf
, src
);
871 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
874 return convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
877 size_t push_ascii_fstring(void *dest
, const char *src
)
879 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
882 size_t push_ascii_pstring(void *dest
, const char *src
)
884 return push_ascii(dest
, src
, sizeof(pstring
), STR_TERMINATE
);
887 /********************************************************************
888 Push an nstring - ensure null terminated. Written by
889 moriyama@miraclelinux.com (MORIYAMA Masayuki).
890 ********************************************************************/
892 size_t push_ascii_nstring(void *dest
, const char *src
)
894 size_t i
, buffer_len
, dest_len
;
898 buffer_len
= push_ucs2_allocate(&buffer
, src
);
899 if (buffer_len
== (size_t)-1) {
900 smb_panic("failed to create UCS2 buffer");
903 /* We're using buffer_len below to count ucs2 characters, not bytes. */
904 buffer_len
/= sizeof(smb_ucs2_t
);
907 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
908 unsigned char mb
[10];
909 /* Convert one smb_ucs2_t character at a time. */
910 size_t mb_len
= convert_string(CH_UCS2
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
911 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
912 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
919 ((char *)dest
)[dest_len
] = '\0';
927 * Copy a string from a dos codepage source to a unix char* destination.
929 * The resulting string in "dest" is always null terminated.
931 * @param flags can have:
933 * <dt>STR_TERMINATE</dt>
934 * <dd>STR_TERMINATE means the string in @p src
935 * is null terminated, and src_len is ignored.</dd>
938 * @param src_len is the length of the source area in bytes.
939 * @returns the number of bytes occupied by the string in @p src.
941 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
945 if (dest_len
== (size_t)-1)
946 dest_len
= sizeof(pstring
);
948 if (flags
& STR_TERMINATE
) {
949 if (src_len
== (size_t)-1) {
950 src_len
= strlen((const char *)src
) + 1;
952 size_t len
= strnlen((const char *)src
, src_len
);
959 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
960 if (ret
== (size_t)-1) {
965 dest
[MIN(ret
, dest_len
-1)] = 0;
972 size_t pull_ascii_pstring(char *dest
, const void *src
)
974 return pull_ascii(dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
977 size_t pull_ascii_fstring(char *dest
, const void *src
)
979 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
982 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
984 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
986 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
990 * Copy a string from a char* src to a unicode destination.
992 * @returns the number of bytes occupied by the string in the destination.
994 * @param flags can have:
997 * <dt>STR_TERMINATE <dd>means include the null termination.
998 * <dt>STR_UPPER <dd>means uppercase in the destination.
999 * <dt>STR_NOALIGN <dd>means don't do alignment.
1002 * @param dest_len is the maximum length allowed in the
1003 * destination. If dest_len is -1 then no maxiumum is used.
1006 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1012 /* treat a pstring as "unlimited" length */
1013 if (dest_len
== (size_t)-1)
1014 dest_len
= sizeof(pstring
);
1016 if (flags
& STR_TERMINATE
)
1017 src_len
= (size_t)-1;
1019 src_len
= strlen(src
);
1021 if (ucs2_align(base_ptr
, dest
, flags
)) {
1023 dest
= (void *)((char *)dest
+ 1);
1029 /* ucs2 is always a multiple of 2 bytes */
1032 ret
= convert_string(CH_UNIX
, CH_UCS2
, src
, src_len
, dest
, dest_len
, True
);
1033 if (ret
== (size_t)-1) {
1039 if (flags
& STR_UPPER
) {
1040 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1042 for (i
= 0; i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1043 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1044 if (v
!= dest_ucs2
[i
]) {
1055 * Copy a string from a unix char* src to a UCS2 destination,
1056 * allocating a buffer using talloc().
1058 * @param dest always set at least to NULL
1060 * @returns The number of bytes occupied by the string in the destination
1061 * or -1 in case of error.
1063 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1065 size_t src_len
= strlen(src
)+1;
1068 return convert_string_talloc(ctx
, CH_UNIX
, CH_UCS2
, src
, src_len
, (void **)dest
, True
);
1073 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1075 * @param dest always set at least to NULL
1077 * @returns The number of bytes occupied by the string in the destination
1078 * or -1 in case of error.
1081 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1083 size_t src_len
= strlen(src
)+1;
1086 return convert_string_allocate(NULL
, CH_UNIX
, CH_UCS2
, src
, src_len
, (void **)dest
, True
);
1090 Copy a string from a char* src to a UTF-8 destination.
1091 Return the number of bytes occupied by the string in the destination
1093 STR_TERMINATE means include the null termination
1094 STR_UPPER means uppercase in the destination
1095 dest_len is the maximum length allowed in the destination. If dest_len
1096 is -1 then no maxiumum is used.
1099 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1101 size_t src_len
= strlen(src
);
1104 /* treat a pstring as "unlimited" length */
1105 if (dest_len
== (size_t)-1)
1106 dest_len
= sizeof(pstring
);
1108 if (flags
& STR_UPPER
) {
1109 pstrcpy(tmpbuf
, src
);
1114 if (flags
& STR_TERMINATE
)
1117 return convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1120 size_t push_utf8_fstring(void *dest
, const char *src
)
1122 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1126 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1128 * @param dest always set at least to NULL
1130 * @returns The number of bytes occupied by the string in the destination
1133 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1135 size_t src_len
= strlen(src
)+1;
1138 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1142 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1144 * @param dest always set at least to NULL
1146 * @returns The number of bytes occupied by the string in the destination
1149 size_t push_utf8_allocate(char **dest
, const char *src
)
1151 size_t src_len
= strlen(src
)+1;
1154 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, True
);
1158 Copy a string from a ucs2 source to a unix char* destination.
1160 STR_TERMINATE means the string in src is null terminated.
1161 STR_NOALIGN means don't try to align.
1162 if STR_TERMINATE is set then src_len is ignored if it is -1.
1163 src_len is the length of the source area in bytes
1164 Return the number of bytes occupied by the string in src.
1165 The resulting string in "dest" is always null terminated.
1168 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1172 if (dest_len
== (size_t)-1)
1173 dest_len
= sizeof(pstring
);
1175 if (ucs2_align(base_ptr
, src
, flags
)) {
1176 src
= (const void *)((const char *)src
+ 1);
1177 if (src_len
!= (size_t)-1)
1181 if (flags
& STR_TERMINATE
) {
1182 /* src_len -1 is the default for null terminated strings. */
1183 if (src_len
!= (size_t)-1) {
1184 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1186 if (len
< src_len
/2)
1192 /* ucs2 is always a multiple of 2 bytes */
1193 if (src_len
!= (size_t)-1)
1196 ret
= convert_string(CH_UCS2
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1197 if (ret
== (size_t)-1) {
1201 if (src_len
== (size_t)-1)
1205 dest
[MIN(ret
, dest_len
-1)] = 0;
1212 size_t pull_ucs2_pstring(char *dest
, const void *src
)
1214 return pull_ucs2(NULL
, dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1217 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1219 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1223 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1225 * @param dest always set at least to NULL
1227 * @returns The number of bytes occupied by the string in the destination
1230 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1232 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1234 return convert_string_talloc(ctx
, CH_UCS2
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1238 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1240 * @param dest always set at least to NULL
1242 * @returns The number of bytes occupied by the string in the destination
1245 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1247 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1249 return convert_string_allocate(NULL
, CH_UCS2
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1253 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1255 * @param dest always set at least to NULL
1257 * @returns The number of bytes occupied by the string in the destination
1260 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1262 size_t src_len
= strlen(src
)+1;
1264 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1268 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1270 * @param dest always set at least to NULL
1272 * @returns The number of bytes occupied by the string in the destination
1275 size_t pull_utf8_allocate(char **dest
, const char *src
)
1277 size_t src_len
= strlen(src
)+1;
1279 return convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1283 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1285 * @param dest always set at least to NULL
1287 * @returns The number of bytes occupied by the string in the destination
1290 size_t pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1292 size_t src_len
= strlen(src
)+1;
1294 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1298 Copy a string from a char* src to a unicode or ascii
1299 dos codepage destination choosing unicode or ascii based on the
1300 flags in the SMB buffer starting at base_ptr.
1301 Return the number of bytes occupied by the string in the destination.
1303 STR_TERMINATE means include the null termination.
1304 STR_UPPER means uppercase in the destination.
1305 STR_ASCII use ascii even with unicode packet.
1306 STR_NOALIGN means don't do alignment.
1307 dest_len is the maximum length allowed in the destination. If dest_len
1308 is -1 then no maxiumum is used.
1311 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
)
1314 /* We really need to zero fill here, not clobber
1315 * region, as we want to ensure that valgrind thinks
1316 * all of the outgoing buffer has been written to
1317 * so a send() or write() won't trap an error.
1321 if (dest_len
!= (size_t)-1)
1322 clobber_region(function
, line
, dest
, dest_len
);
1324 if (dest_len
!= (size_t)-1)
1325 memset(dest
, '\0', dest_len
);
1329 if (!(flags
& STR_ASCII
) && \
1330 ((flags
& STR_UNICODE
|| \
1331 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1332 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1334 return push_ascii(dest
, src
, dest_len
, flags
);
1339 Copy a string from a unicode or ascii source (depending on
1340 the packet flags) to a char* destination.
1342 STR_TERMINATE means the string in src is null terminated.
1343 STR_UNICODE means to force as unicode.
1344 STR_ASCII use ascii even with unicode packet.
1345 STR_NOALIGN means don't do alignment.
1346 if STR_TERMINATE is set then src_len is ignored is it is -1
1347 src_len is the length of the source area in bytes.
1348 Return the number of bytes occupied by the string in src.
1349 The resulting string in "dest" is always null terminated.
1352 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
)
1355 if (dest_len
!= (size_t)-1)
1356 clobber_region(function
, line
, dest
, dest_len
);
1359 if (!(flags
& STR_ASCII
) && \
1360 ((flags
& STR_UNICODE
|| \
1361 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1362 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1364 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1367 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1369 if (!(flags
& STR_ASCII
) && \
1370 ((flags
& STR_UNICODE
|| \
1371 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1372 return ucs2_align(base_ptr
, p
, flags
);
1377 /****************************************************************
1378 Calculate the size (in bytes) of the next multibyte character in
1379 our internal character set. Note that p must be pointing to a
1380 valid mb char, not within one.
1381 ****************************************************************/
1383 size_t next_mb_char_size(const char *s
)
1388 return 1; /* ascii. */
1391 for ( i
= 1; i
<=4; i
++ ) {
1393 if (convert_string(CH_UNIX
, CH_UCS2
, s
, i
, &uc
, 2, False
) == 2) {
1395 DEBUG(10,("next_mb_char_size: size %u at string %s\n",
1396 (unsigned int)i
, s
));
1398 conv_silent
= False
;
1402 /* We're hosed - we don't know how big this is... */
1403 DEBUG(10,("next_mb_char_size: unknown size at string %s\n", s
));
1404 conv_silent
= False
;