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 * Initialize iconv conversion descriptors.
107 * This is called the first time it is needed, and also called again
108 * every time the configuration is reloaded, because the charset or
109 * codepage might have changed.
111 void init_iconv(void)
114 BOOL did_reload
= False
;
116 /* so that charset_name() works we need to get the UNIX<->UCS2 going
118 if (!conv_handles
[CH_UNIX
][CH_UCS2
])
119 conv_handles
[CH_UNIX
][CH_UCS2
] = smb_iconv_open(charset_name(CH_UCS2
), "ASCII");
121 if (!conv_handles
[CH_UCS2
][CH_UNIX
])
122 conv_handles
[CH_UCS2
][CH_UNIX
] = smb_iconv_open("ASCII", charset_name(CH_UCS2
));
124 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
125 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
126 const char *n1
= charset_name((charset_t
)c1
);
127 const char *n2
= charset_name((charset_t
)c2
);
128 if (conv_handles
[c1
][c2
] &&
129 strcmp(n1
, conv_handles
[c1
][c2
]->from_name
) == 0 &&
130 strcmp(n2
, conv_handles
[c1
][c2
]->to_name
) == 0)
135 if (conv_handles
[c1
][c2
])
136 smb_iconv_close(conv_handles
[c1
][c2
]);
138 conv_handles
[c1
][c2
] = smb_iconv_open(n2
,n1
);
139 if (conv_handles
[c1
][c2
] == (smb_iconv_t
)-1) {
140 DEBUG(0,("init_iconv: Conversion from %s to %s not supported\n",
141 charset_name((charset_t
)c1
), charset_name((charset_t
)c2
)));
148 DEBUG(0,("init_iconv: Attempting to replace with conversion from %s to %s\n",
150 conv_handles
[c1
][c2
] = smb_iconv_open(n2
,n1
);
151 if (!conv_handles
[c1
][c2
]) {
152 DEBUG(0,("init_iconv: Conversion from %s to %s failed", n1
, n2
));
153 smb_panic("init_iconv: conv_handle initialization failed.");
160 /* XXX: Does this really get called every time the dos
161 * codepage changes? */
162 /* XXX: Is the did_reload test too strict? */
164 init_doschar_table();
171 * Convert string from one encoding to another, making error checking etc
172 * Slow path version - uses (slow) iconv.
174 * @param src pointer to source string (multibyte or singlebyte)
175 * @param srclen length of the source string in bytes
176 * @param dest pointer to destination string (multibyte or singlebyte)
177 * @param destlen maximal length allowed for string
178 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
179 * @returns the number of bytes occupied in the destination
181 * Ensure the srclen contains the terminating zero.
185 static size_t convert_string_internal(charset_t from
, charset_t to
,
186 void const *src
, size_t srclen
,
187 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
191 const char* inbuf
= (const char*)src
;
192 char* outbuf
= (char*)dest
;
193 smb_iconv_t descriptor
;
195 lazy_initialize_conv();
197 descriptor
= conv_handles
[from
][to
];
199 if (srclen
== (size_t)-1) {
200 if (from
== CH_UCS2
) {
201 srclen
= (strlen_w((const smb_ucs2_t
*)src
)+1) * 2;
203 srclen
= strlen((const char *)src
)+1;
208 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
210 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
219 retval
= smb_iconv(descriptor
, &inbuf
, &i_len
, &outbuf
, &o_len
);
220 if(retval
==(size_t)-1) {
221 const char *reason
="unknown error";
224 reason
="Incomplete multibyte sequence";
226 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
231 reason
="No more room";
233 if (from
== CH_UNIX
) {
234 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
235 charset_name(from
), charset_name(to
),
236 (unsigned int)srclen
, (unsigned int)destlen
, (const char *)src
));
238 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
239 charset_name(from
), charset_name(to
),
240 (unsigned int)srclen
, (unsigned int)destlen
));
245 reason
="Illegal multibyte sequence";
247 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
253 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
256 /* smb_panic(reason); */
258 return destlen
-o_len
;
263 * Conversion not supported. This is actually an error, but there are so
264 * many misconfigured iconv systems and smb.conf's out there we can't just
265 * fail. Do a very bad conversion instead.... JRA.
269 if (o_len
== 0 || i_len
== 0)
270 return destlen
- o_len
;
272 if (from
== CH_UCS2
&& to
!= CH_UCS2
) {
273 /* Can't convert from ucs2 to multibyte. Replace with the default fail char. */
275 return destlen
- o_len
;
277 *outbuf
= lp_failed_convert_char();
286 if (o_len
== 0 || i_len
== 0)
287 return destlen
- o_len
;
289 /* Keep trying with the next char... */
292 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
293 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
295 return destlen
- o_len
;
297 outbuf
[0] = 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_UCS2
&& to
!= CH_UCS2
) {
313 /* Failed multibyte to multibyte. Just copy the default fail char and
315 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... */
330 /* Keep compiler happy.... */
331 return destlen
- o_len
;
337 * Convert string from one encoding to another, making error checking etc
338 * Fast path version - handles ASCII first.
340 * @param src pointer to source string (multibyte or singlebyte)
341 * @param srclen length of the source string in bytes, or -1 for nul terminated.
342 * @param dest pointer to destination string (multibyte or singlebyte)
343 * @param destlen maximal length allowed for string - *NEVER* -1.
344 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
345 * @returns the number of bytes occupied in the destination
347 * Ensure the srclen contains the terminating zero.
349 * This function has been hand-tuned to provide a fast path.
350 * Don't change unless you really know what you are doing. JRA.
353 size_t convert_string(charset_t from
, charset_t to
,
354 void const *src
, size_t srclen
,
355 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
358 * NB. We deliberately don't do a strlen here if srclen == -1.
359 * This is very expensive over millions of calls and is taken
360 * care of in the slow path in convert_string_internal. JRA.
364 SMB_ASSERT(destlen
!= (size_t)-1);
370 if (from
!= CH_UCS2
&& to
!= CH_UCS2
) {
371 const unsigned char *p
= (const unsigned char *)src
;
372 unsigned char *q
= (unsigned char *)dest
;
373 size_t slen
= srclen
;
374 size_t dlen
= destlen
;
378 /* If all characters are ascii, fast path here. */
379 while (slen
&& dlen
) {
380 if ((lastp
= *p
) <= 0x7f) {
382 if (slen
!= (size_t)-1) {
390 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
393 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
398 } else if (from
== CH_UCS2
&& to
!= CH_UCS2
) {
399 const unsigned char *p
= (const unsigned char *)src
;
400 unsigned char *q
= (unsigned char *)dest
;
402 size_t slen
= srclen
;
403 size_t dlen
= destlen
;
406 /* If all characters are ascii, fast path here. */
407 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
408 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
410 if (slen
!= (size_t)-1) {
419 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
422 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
427 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
428 const unsigned char *p
= (const unsigned char *)src
;
429 unsigned char *q
= (unsigned char *)dest
;
431 size_t slen
= srclen
;
432 size_t dlen
= destlen
;
435 /* If all characters are ascii, fast path here. */
436 while (slen
&& (dlen
>= 2)) {
437 if ((lastp
= *p
) <= 0x7F) {
440 if (slen
!= (size_t)-1) {
448 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
451 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
458 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
461 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
465 * Convert between character sets, allocating a new buffer for the result.
467 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
468 * @param srclen length of source buffer.
469 * @param dest always set at least to NULL
470 * @note -1 is not accepted for srclen.
472 * @returns Size in bytes of the converted string; or -1 in case of error.
474 * Ensure the srclen contains the terminating zero.
476 * I hate the goto's in this function. It's embarressing.....
477 * There has to be a cleaner way to do this. JRA.
480 size_t convert_string_allocate(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
481 void const *src
, size_t srclen
, void **dest
, BOOL allow_bad_conv
)
483 size_t i_len
, o_len
, destlen
= MAX(srclen
, 512);
485 const char *inbuf
= (const char *)src
;
486 char *outbuf
= NULL
, *ob
= NULL
;
487 smb_iconv_t descriptor
;
491 if (src
== NULL
|| srclen
== (size_t)-1)
496 lazy_initialize_conv();
498 descriptor
= conv_handles
[from
][to
];
500 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
502 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
508 if ((destlen
*2) < destlen
) {
509 /* wrapped ! abort. */
511 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
516 destlen
= destlen
* 2;
520 ob
= (char *)talloc_realloc(ctx
, ob
, destlen
);
522 ob
= (char *)Realloc(ob
, destlen
);
525 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
537 retval
= smb_iconv(descriptor
,
540 if(retval
== (size_t)-1) {
541 const char *reason
="unknown error";
544 reason
="Incomplete multibyte sequence";
546 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
553 reason
="Illegal multibyte sequence";
555 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
561 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
562 /* smb_panic(reason); */
568 destlen
= destlen
- o_len
;
570 *dest
= (char *)talloc_realloc(ctx
,ob
,destlen
);
572 *dest
= (char *)Realloc(ob
,destlen
);
573 if (destlen
&& !*dest
) {
574 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
585 * Conversion not supported. This is actually an error, but there are so
586 * many misconfigured iconv systems and smb.conf's out there we can't just
587 * fail. Do a very bad conversion instead.... JRA.
591 if (o_len
== 0 || i_len
== 0)
594 if (from
== CH_UCS2
&& to
!= CH_UCS2
) {
595 /* Can't convert from ucs2 to multibyte. Just use the default fail char. */
600 *outbuf
= lp_failed_convert_char();
609 if (o_len
== 0 || i_len
== 0)
612 /* Keep trying with the next char... */
615 } else if (from
!= CH_UCS2
&& to
== CH_UCS2
) {
616 /* Can't convert to ucs2 - just widen by adding the default fail char then zero. */
620 outbuf
[0] = lp_failed_convert_char();
629 if (o_len
== 0 || i_len
== 0)
632 /* Keep trying with the next char... */
635 } else if (from
!= CH_UCS2
&& to
!= CH_UCS2
) {
636 /* Failed multibyte to multibyte. Just copy the default fail char and
638 outbuf
[0] = lp_failed_convert_char();
646 if (o_len
== 0 || i_len
== 0)
649 /* Keep trying with the next char... */
653 /* Keep compiler happy.... */
660 * Convert between character sets, allocating a new buffer using talloc for the result.
662 * @param srclen length of source buffer.
663 * @param dest always set at least to NULL
664 * @note -1 is not accepted for srclen.
666 * @returns Size in bytes of the converted string; or -1 in case of error.
668 static size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
669 void const *src
, size_t srclen
, void **dest
, BOOL allow_bad_conv
)
674 dest_len
=convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
, allow_bad_conv
);
675 if (dest_len
== (size_t)-1)
682 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
687 size
= push_ucs2_allocate(&buffer
, src
);
688 if (size
== (size_t)-1) {
689 smb_panic("failed to create UCS2 buffer");
691 if (!strupper_w(buffer
) && (dest
== src
)) {
696 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
702 strdup() a unix string to upper case.
706 char *strdup_upper(const char *s
)
709 const unsigned char *p
= (const unsigned char *)s
;
710 unsigned char *q
= (unsigned char *)out_buffer
;
712 /* this is quite a common operation, so we want it to be
713 fast. We optimise for the ascii case, knowing that all our
714 supported multi-byte character sets are ascii-compatible
715 (ie. they match for the first 128 chars) */
724 if (p
- ( const unsigned char *)s
>= sizeof(pstring
))
732 size
= convert_string(CH_UNIX
, CH_UCS2
, s
, -1, buffer
, sizeof(buffer
), True
);
733 if (size
== (size_t)-1) {
739 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, -1, out_buffer
, sizeof(out_buffer
), True
);
740 if (size
== (size_t)-1) {
745 return strdup(out_buffer
);
748 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
751 smb_ucs2_t
*buffer
= NULL
;
753 size
= convert_string_allocate(NULL
, CH_UNIX
, CH_UCS2
, src
, srclen
,
754 (void **) &buffer
, True
);
755 if (size
== (size_t)-1 || !buffer
) {
756 smb_panic("failed to create UCS2 buffer");
758 if (!strlower_w(buffer
) && (dest
== src
)) {
762 size
= convert_string(CH_UCS2
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
768 strdup() a unix string to lower case.
771 char *strdup_lower(const char *s
)
774 smb_ucs2_t
*buffer
= NULL
;
777 size
= push_ucs2_allocate(&buffer
, s
);
778 if (size
== -1 || !buffer
) {
784 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
787 if (size
== (size_t)-1) {
794 static size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
796 if (flags
& (STR_NOALIGN
|STR_ASCII
))
798 return PTR_DIFF(p
, base_ptr
) & 1;
803 * Copy a string from a char* unix src to a dos codepage string destination.
805 * @return the number of bytes occupied by the string in the destination.
807 * @param flags can include
809 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
810 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
813 * @param dest_len the maximum length in bytes allowed in the
814 * destination. If @p dest_len is -1 then no maximum is used.
816 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
818 size_t src_len
= strlen(src
);
821 /* treat a pstring as "unlimited" length */
822 if (dest_len
== (size_t)-1)
823 dest_len
= sizeof(pstring
);
825 if (flags
& STR_UPPER
) {
826 pstrcpy(tmpbuf
, src
);
831 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
834 return convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
837 size_t push_ascii_fstring(void *dest
, const char *src
)
839 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
842 size_t push_ascii_pstring(void *dest
, const char *src
)
844 return push_ascii(dest
, src
, sizeof(pstring
), STR_TERMINATE
);
847 /********************************************************************
848 Push an nstring - ensure null terminated. Written by
849 moriyama@miraclelinux.com (MORIYAMA Masayuki).
850 ********************************************************************/
852 size_t push_ascii_nstring(void *dest
, const char *src
)
854 size_t i
, buffer_len
, dest_len
;
858 buffer_len
= push_ucs2_allocate(&buffer
, src
);
859 if (buffer_len
== (size_t)-1) {
860 smb_panic("failed to create UCS2 buffer");
863 /* We're using buffer_len below to count ucs2 characters, not bytes. */
864 buffer_len
/= sizeof(smb_ucs2_t
);
867 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
868 unsigned char mb
[10];
869 /* Convert one smb_ucs2_t character at a time. */
870 size_t mb_len
= convert_string(CH_UCS2
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
871 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
872 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
879 ((char *)dest
)[dest_len
] = '\0';
887 * Copy a string from a dos codepage source to a unix char* destination.
889 * The resulting string in "dest" is always null terminated.
891 * @param flags can have:
893 * <dt>STR_TERMINATE</dt>
894 * <dd>STR_TERMINATE means the string in @p src
895 * is null terminated, and src_len is ignored.</dd>
898 * @param src_len is the length of the source area in bytes.
899 * @returns the number of bytes occupied by the string in @p src.
901 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
905 if (dest_len
== (size_t)-1)
906 dest_len
= sizeof(pstring
);
908 if (flags
& STR_TERMINATE
) {
909 if (src_len
== (size_t)-1) {
910 src_len
= strlen(src
) + 1;
912 size_t len
= strnlen(src
, src_len
);
919 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
920 if (ret
== (size_t)-1) {
925 dest
[MIN(ret
, dest_len
-1)] = 0;
932 size_t pull_ascii_pstring(char *dest
, const void *src
)
934 return pull_ascii(dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
937 size_t pull_ascii_fstring(char *dest
, const void *src
)
939 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
942 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
944 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
946 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
950 * Copy a string from a char* src to a unicode destination.
952 * @returns the number of bytes occupied by the string in the destination.
954 * @param flags can have:
957 * <dt>STR_TERMINATE <dd>means include the null termination.
958 * <dt>STR_UPPER <dd>means uppercase in the destination.
959 * <dt>STR_NOALIGN <dd>means don't do alignment.
962 * @param dest_len is the maximum length allowed in the
963 * destination. If dest_len is -1 then no maxiumum is used.
966 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
972 /* treat a pstring as "unlimited" length */
973 if (dest_len
== (size_t)-1)
974 dest_len
= sizeof(pstring
);
976 if (flags
& STR_TERMINATE
)
977 src_len
= (size_t)-1;
979 src_len
= strlen(src
);
981 if (ucs2_align(base_ptr
, dest
, flags
)) {
983 dest
= (void *)((char *)dest
+ 1);
989 /* ucs2 is always a multiple of 2 bytes */
992 ret
= convert_string(CH_UNIX
, CH_UCS2
, src
, src_len
, dest
, dest_len
, True
);
993 if (ret
== (size_t)-1) {
999 if (flags
& STR_UPPER
) {
1000 smb_ucs2_t
*dest_ucs2
= dest
;
1002 for (i
= 0; i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1003 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1004 if (v
!= dest_ucs2
[i
]) {
1015 * Copy a string from a unix char* src to a UCS2 destination,
1016 * allocating a buffer using talloc().
1018 * @param dest always set at least to NULL
1020 * @returns The number of bytes occupied by the string in the destination
1021 * or -1 in case of error.
1023 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1025 size_t src_len
= strlen(src
)+1;
1028 return convert_string_talloc(ctx
, CH_UNIX
, CH_UCS2
, src
, src_len
, (void **)dest
, True
);
1033 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1035 * @param dest always set at least to NULL
1037 * @returns The number of bytes occupied by the string in the destination
1038 * or -1 in case of error.
1041 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1043 size_t src_len
= strlen(src
)+1;
1046 return convert_string_allocate(NULL
, CH_UNIX
, CH_UCS2
, src
, src_len
, (void **)dest
, True
);
1050 Copy a string from a char* src to a UTF-8 destination.
1051 Return the number of bytes occupied by the string in the destination
1053 STR_TERMINATE means include the null termination
1054 STR_UPPER means uppercase in the destination
1055 dest_len is the maximum length allowed in the destination. If dest_len
1056 is -1 then no maxiumum is used.
1059 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1061 size_t src_len
= strlen(src
);
1064 /* treat a pstring as "unlimited" length */
1065 if (dest_len
== (size_t)-1)
1066 dest_len
= sizeof(pstring
);
1068 if (flags
& STR_UPPER
) {
1069 pstrcpy(tmpbuf
, src
);
1074 if (flags
& STR_TERMINATE
)
1077 return convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1080 size_t push_utf8_fstring(void *dest
, const char *src
)
1082 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1086 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1088 * @param dest always set at least to NULL
1090 * @returns The number of bytes occupied by the string in the destination
1093 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1095 size_t src_len
= strlen(src
)+1;
1098 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1102 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1104 * @param dest always set at least to NULL
1106 * @returns The number of bytes occupied by the string in the destination
1109 size_t push_utf8_allocate(char **dest
, const char *src
)
1111 size_t src_len
= strlen(src
)+1;
1114 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, True
);
1118 Copy a string from a ucs2 source to a unix char* destination.
1120 STR_TERMINATE means the string in src is null terminated.
1121 STR_NOALIGN means don't try to align.
1122 if STR_TERMINATE is set then src_len is ignored if it is -1.
1123 src_len is the length of the source area in bytes
1124 Return the number of bytes occupied by the string in src.
1125 The resulting string in "dest" is always null terminated.
1128 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1132 if (dest_len
== (size_t)-1)
1133 dest_len
= sizeof(pstring
);
1135 if (ucs2_align(base_ptr
, src
, flags
)) {
1136 src
= (const void *)((const char *)src
+ 1);
1137 if (src_len
!= (size_t)-1)
1141 if (flags
& STR_TERMINATE
) {
1142 /* src_len -1 is the default for null terminated strings. */
1143 if (src_len
!= (size_t)-1) {
1144 size_t len
= strnlen_w(src
, src_len
/2);
1145 if (len
< src_len
/2)
1151 /* ucs2 is always a multiple of 2 bytes */
1152 if (src_len
!= (size_t)-1)
1155 ret
= convert_string(CH_UCS2
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1156 if (ret
== (size_t)-1) {
1160 if (src_len
== (size_t)-1)
1164 dest
[MIN(ret
, dest_len
-1)] = 0;
1171 size_t pull_ucs2_pstring(char *dest
, const void *src
)
1173 return pull_ucs2(NULL
, dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1176 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1178 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1182 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1184 * @param dest always set at least to NULL
1186 * @returns The number of bytes occupied by the string in the destination
1189 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1191 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1193 return convert_string_talloc(ctx
, CH_UCS2
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1197 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1199 * @param dest always set at least to NULL
1201 * @returns The number of bytes occupied by the string in the destination
1204 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1206 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1208 return convert_string_allocate(NULL
, CH_UCS2
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1212 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1214 * @param dest always set at least to NULL
1216 * @returns The number of bytes occupied by the string in the destination
1219 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1221 size_t src_len
= strlen(src
)+1;
1223 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1227 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1229 * @param dest always set at least to NULL
1231 * @returns The number of bytes occupied by the string in the destination
1234 size_t pull_utf8_allocate(char **dest
, const char *src
)
1236 size_t src_len
= strlen(src
)+1;
1238 return convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1242 Copy a string from a char* src to a unicode or ascii
1243 dos codepage destination choosing unicode or ascii based on the
1244 flags in the SMB buffer starting at base_ptr.
1245 Return the number of bytes occupied by the string in the destination.
1247 STR_TERMINATE means include the null termination.
1248 STR_UPPER means uppercase in the destination.
1249 STR_ASCII use ascii even with unicode packet.
1250 STR_NOALIGN means don't do alignment.
1251 dest_len is the maximum length allowed in the destination. If dest_len
1252 is -1 then no maxiumum is used.
1255 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
)
1258 /* We really need to zero fill here, not clobber
1259 * region, as we want to ensure that valgrind thinks
1260 * all of the outgoing buffer has been written to
1261 * so a send() or write() won't trap an error.
1265 if (dest_len
!= (size_t)-1)
1266 clobber_region(function
, line
, dest
, dest_len
);
1268 if (dest_len
!= (size_t)-1)
1269 memset(dest
, '\0', dest_len
);
1273 if (!(flags
& STR_ASCII
) && \
1274 ((flags
& STR_UNICODE
|| \
1275 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1276 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1278 return push_ascii(dest
, src
, dest_len
, flags
);
1283 Copy a string from a unicode or ascii source (depending on
1284 the packet flags) to a char* destination.
1286 STR_TERMINATE means the string in src is null terminated.
1287 STR_UNICODE means to force as unicode.
1288 STR_ASCII use ascii even with unicode packet.
1289 STR_NOALIGN means don't do alignment.
1290 if STR_TERMINATE is set then src_len is ignored is it is -1
1291 src_len is the length of the source area in bytes.
1292 Return the number of bytes occupied by the string in src.
1293 The resulting string in "dest" is always null terminated.
1296 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
)
1299 if (dest_len
!= (size_t)-1)
1300 clobber_region(function
, line
, dest
, dest_len
);
1303 if (!(flags
& STR_ASCII
) && \
1304 ((flags
& STR_UNICODE
|| \
1305 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1306 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1308 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1311 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1313 if (!(flags
& STR_ASCII
) && \
1314 ((flags
& STR_UNICODE
|| \
1315 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1316 return ucs2_align(base_ptr
, p
, flags
);
1321 /****************************************************************
1322 Calculate the size (in bytes) of the next multibyte character in
1323 our internal character set. Note that p must be pointing to a
1324 valid mb char, not within one.
1325 ****************************************************************/
1327 size_t next_mb_char_size(const char *s
)
1332 return 1; /* ascii. */
1335 for ( i
= 1; i
<=4; i
++ ) {
1337 if (convert_string(CH_UNIX
, CH_UCS2
, s
, i
, &uc
, 2, False
) == 2) {
1339 DEBUG(10,("next_mb_char_size: size %u at string %s\n",
1340 (unsigned int)i
, s
));
1342 conv_silent
= False
;
1346 /* We're hosed - we don't know how big this is... */
1347 DEBUG(10,("next_mb_char_size: unknown size at string %s\n", s
));
1348 conv_silent
= False
;