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_UTF16LE
) ret
= "UTF-16LE";
60 else if (ch
== CH_UTF16BE
) ret
= "UTF-16BE";
61 else if (ch
== CH_UNIX
) ret
= lp_unix_charset();
62 else if (ch
== CH_DOS
) ret
= lp_dos_charset();
63 else if (ch
== CH_DISPLAY
) ret
= lp_display_charset();
64 else if (ch
== CH_UTF8
) ret
= "UTF8";
66 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
67 if (ret
&& !strcmp(ret
, "LOCALE")) {
68 const char *ln
= NULL
;
71 setlocale(LC_ALL
, "");
73 ln
= nl_langinfo(CODESET
);
75 /* Check whether the charset name is supported
77 smb_iconv_t handle
= smb_iconv_open(ln
,"UCS-2LE");
78 if (handle
== (smb_iconv_t
) -1) {
79 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln
));
82 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln
));
83 smb_iconv_close(handle
);
90 if (!ret
|| !*ret
) ret
= "ASCII";
94 void lazy_initialize_conv(void)
96 static int initialized
= False
;
106 * Destroy global objects allocated by init_iconv()
108 void gfree_charcnv(void)
112 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
113 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
114 if ( conv_handles
[c1
][c2
] ) {
115 smb_iconv_close( conv_handles
[c1
][c2
] );
116 conv_handles
[c1
][c2
] = 0;
123 * Initialize iconv conversion descriptors.
125 * This is called the first time it is needed, and also called again
126 * every time the configuration is reloaded, because the charset or
127 * codepage might have changed.
129 void init_iconv(void)
132 BOOL did_reload
= False
;
134 /* so that charset_name() works we need to get the UNIX<->UCS2 going
136 if (!conv_handles
[CH_UNIX
][CH_UTF16LE
])
137 conv_handles
[CH_UNIX
][CH_UTF16LE
] = smb_iconv_open(charset_name(CH_UTF16LE
), "ASCII");
139 if (!conv_handles
[CH_UTF16LE
][CH_UNIX
])
140 conv_handles
[CH_UTF16LE
][CH_UNIX
] = smb_iconv_open("ASCII", charset_name(CH_UTF16LE
));
142 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
143 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
144 const char *n1
= charset_name((charset_t
)c1
);
145 const char *n2
= charset_name((charset_t
)c2
);
146 if (conv_handles
[c1
][c2
] &&
147 strcmp(n1
, conv_handles
[c1
][c2
]->from_name
) == 0 &&
148 strcmp(n2
, conv_handles
[c1
][c2
]->to_name
) == 0)
153 if (conv_handles
[c1
][c2
])
154 smb_iconv_close(conv_handles
[c1
][c2
]);
156 conv_handles
[c1
][c2
] = smb_iconv_open(n2
,n1
);
157 if (conv_handles
[c1
][c2
] == (smb_iconv_t
)-1) {
158 DEBUG(0,("init_iconv: Conversion from %s to %s not supported\n",
159 charset_name((charset_t
)c1
), charset_name((charset_t
)c2
)));
160 if (c1
!= CH_UTF16LE
&& c1
!= CH_UTF16BE
) {
163 if (c2
!= CH_UTF16LE
&& c2
!= CH_UTF16BE
) {
166 DEBUG(0,("init_iconv: Attempting to replace with conversion from %s to %s\n",
168 conv_handles
[c1
][c2
] = smb_iconv_open(n2
,n1
);
169 if (!conv_handles
[c1
][c2
]) {
170 DEBUG(0,("init_iconv: Conversion from %s to %s failed", n1
, n2
));
171 smb_panic("init_iconv: conv_handle initialization failed.");
178 /* XXX: Does this really get called every time the dos
179 * codepage changes? */
180 /* XXX: Is the did_reload test too strict? */
182 init_doschar_table();
189 * Convert string from one encoding to another, making error checking etc
190 * Slow path version - uses (slow) iconv.
192 * @param src pointer to source string (multibyte or singlebyte)
193 * @param srclen length of the source string in bytes
194 * @param dest pointer to destination string (multibyte or singlebyte)
195 * @param destlen maximal length allowed for string
196 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
197 * @returns the number of bytes occupied in the destination
199 * Ensure the srclen contains the terminating zero.
203 static size_t convert_string_internal(charset_t from
, charset_t to
,
204 void const *src
, size_t srclen
,
205 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
209 const char* inbuf
= (const char*)src
;
210 char* outbuf
= (char*)dest
;
211 smb_iconv_t descriptor
;
213 lazy_initialize_conv();
215 descriptor
= conv_handles
[from
][to
];
217 if (srclen
== (size_t)-1) {
218 if (from
== CH_UTF16LE
|| from
== CH_UTF16BE
) {
219 srclen
= (strlen_w((const smb_ucs2_t
*)src
)+1) * 2;
221 srclen
= strlen((const char *)src
)+1;
226 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
228 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
237 retval
= smb_iconv(descriptor
, &inbuf
, &i_len
, &outbuf
, &o_len
);
238 if(retval
==(size_t)-1) {
239 const char *reason
="unknown error";
242 reason
="Incomplete multibyte sequence";
244 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
249 reason
="No more room";
251 if (from
== CH_UNIX
) {
252 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
253 charset_name(from
), charset_name(to
),
254 (unsigned int)srclen
, (unsigned int)destlen
, (const char *)src
));
256 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
257 charset_name(from
), charset_name(to
),
258 (unsigned int)srclen
, (unsigned int)destlen
));
263 reason
="Illegal multibyte sequence";
265 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
271 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
274 /* smb_panic(reason); */
276 return destlen
-o_len
;
281 * Conversion not supported. This is actually an error, but there are so
282 * many misconfigured iconv systems and smb.conf's out there we can't just
283 * fail. Do a very bad conversion instead.... JRA.
287 if (o_len
== 0 || i_len
== 0)
288 return destlen
- o_len
;
290 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
291 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
292 /* Can't convert from utf16 any endian to multibyte.
293 Replace with the default fail char.
296 return destlen
- o_len
;
298 *outbuf
= lp_failed_convert_char();
307 if (o_len
== 0 || i_len
== 0)
308 return destlen
- o_len
;
310 /* Keep trying with the next char... */
313 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
314 /* Can't convert to UTF16LE - just widen by adding the
315 default fail char then zero.
318 return destlen
- o_len
;
320 outbuf
[0] = lp_failed_convert_char();
329 if (o_len
== 0 || i_len
== 0)
330 return destlen
- o_len
;
332 /* Keep trying with the next char... */
335 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
336 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
337 /* Failed multibyte to multibyte. Just copy the default fail char and
339 outbuf
[0] = lp_failed_convert_char();
347 if (o_len
== 0 || i_len
== 0)
348 return destlen
- o_len
;
350 /* Keep trying with the next char... */
354 /* Keep compiler happy.... */
355 return destlen
- o_len
;
361 * Convert string from one encoding to another, making error checking etc
362 * Fast path version - handles ASCII first.
364 * @param src pointer to source string (multibyte or singlebyte)
365 * @param srclen length of the source string in bytes, or -1 for nul terminated.
366 * @param dest pointer to destination string (multibyte or singlebyte)
367 * @param destlen maximal length allowed for string - *NEVER* -1.
368 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
369 * @returns the number of bytes occupied in the destination
371 * Ensure the srclen contains the terminating zero.
373 * This function has been hand-tuned to provide a fast path.
374 * Don't change unless you really know what you are doing. JRA.
377 size_t convert_string(charset_t from
, charset_t to
,
378 void const *src
, size_t srclen
,
379 void *dest
, size_t destlen
, BOOL allow_bad_conv
)
382 * NB. We deliberately don't do a strlen here if srclen == -1.
383 * This is very expensive over millions of calls and is taken
384 * care of in the slow path in convert_string_internal. JRA.
388 SMB_ASSERT(destlen
!= (size_t)-1);
394 if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
395 const unsigned char *p
= (const unsigned char *)src
;
396 unsigned char *q
= (unsigned char *)dest
;
397 size_t slen
= srclen
;
398 size_t dlen
= destlen
;
399 unsigned char lastp
= '\0';
402 /* If all characters are ascii, fast path here. */
403 while (slen
&& dlen
) {
404 if ((lastp
= *p
) <= 0x7f) {
406 if (slen
!= (size_t)-1) {
414 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
417 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
422 /* Even if we fast path we should note if we ran out of room. */
423 if (((slen
!= (size_t)-1) && slen
) ||
424 ((slen
== (size_t)-1) && lastp
)) {
429 } else if (from
== CH_UTF16LE
&& to
!= CH_UTF16LE
) {
430 const unsigned char *p
= (const unsigned char *)src
;
431 unsigned char *q
= (unsigned char *)dest
;
433 size_t slen
= srclen
;
434 size_t dlen
= destlen
;
435 unsigned char lastp
= '\0';
437 /* If all characters are ascii, fast path here. */
438 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
439 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
441 if (slen
!= (size_t)-1) {
450 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
453 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
458 /* Even if we fast path we should note if we ran out of room. */
459 if (((slen
!= (size_t)-1) && slen
) ||
460 ((slen
== (size_t)-1) && lastp
)) {
465 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
466 const unsigned char *p
= (const unsigned char *)src
;
467 unsigned char *q
= (unsigned char *)dest
;
469 size_t slen
= srclen
;
470 size_t dlen
= destlen
;
471 unsigned char lastp
= '\0';
473 /* If all characters are ascii, fast path here. */
474 while (slen
&& (dlen
>= 2)) {
475 if ((lastp
= *p
) <= 0x7F) {
478 if (slen
!= (size_t)-1) {
486 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
489 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
494 /* Even if we fast path we should note if we ran out of room. */
495 if (((slen
!= (size_t)-1) && slen
) ||
496 ((slen
== (size_t)-1) && lastp
)) {
503 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
506 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
510 * Convert between character sets, allocating a new buffer for the result.
512 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
513 * @param srclen length of source buffer.
514 * @param dest always set at least to NULL
515 * @note -1 is not accepted for srclen.
517 * @returns Size in bytes of the converted string; or -1 in case of error.
519 * Ensure the srclen contains the terminating zero.
521 * I hate the goto's in this function. It's embarressing.....
522 * There has to be a cleaner way to do this. JRA.
525 size_t convert_string_allocate(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
526 void const *src
, size_t srclen
, void *dst
, BOOL allow_bad_conv
)
528 size_t i_len
, o_len
, destlen
= (srclen
* 3) / 2;
530 const char *inbuf
= (const char *)src
;
531 char *outbuf
= NULL
, *ob
= NULL
;
532 smb_iconv_t descriptor
;
533 void **dest
= (void **)dst
;
537 if (src
== NULL
|| srclen
== (size_t)-1)
542 lazy_initialize_conv();
544 descriptor
= conv_handles
[from
][to
];
546 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
548 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
554 /* +2 is for ucs2 null termination. */
555 if ((destlen
*2)+2 < destlen
) {
556 /* wrapped ! abort. */
558 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
563 destlen
= destlen
* 2;
566 /* +2 is for ucs2 null termination. */
568 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
+ 2);
570 ob
= (char *)SMB_REALLOC(ob
, destlen
+ 2);
574 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
583 retval
= smb_iconv(descriptor
,
586 if(retval
== (size_t)-1) {
587 const char *reason
="unknown error";
590 reason
="Incomplete multibyte sequence";
592 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
599 reason
="Illegal multibyte sequence";
601 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
607 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
608 /* smb_panic(reason); */
614 destlen
= destlen
- o_len
;
616 /* We're shrinking here so we know the +2 is safe from wrap. */
617 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
+ 2);
619 ob
= (char *)SMB_REALLOC(ob
,destlen
+ 2);
622 if (destlen
&& !ob
) {
623 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
629 /* Must ucs2 null terminate in the extra space we allocated. */
631 ob
[destlen
+1] = '\0';
638 * Conversion not supported. This is actually an error, but there are so
639 * many misconfigured iconv systems and smb.conf's out there we can't just
640 * fail. Do a very bad conversion instead.... JRA.
644 if (o_len
== 0 || i_len
== 0)
647 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
648 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
649 /* Can't convert from utf16 any endian to multibyte.
650 Replace with the default fail char.
657 *outbuf
= lp_failed_convert_char();
666 if (o_len
== 0 || i_len
== 0)
669 /* Keep trying with the next char... */
672 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
673 /* Can't convert to UTF16LE - just widen by adding the
674 default fail char then zero.
679 outbuf
[0] = lp_failed_convert_char();
688 if (o_len
== 0 || i_len
== 0)
691 /* Keep trying with the next char... */
694 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
695 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
696 /* Failed multibyte to multibyte. Just copy the default fail char and
698 outbuf
[0] = lp_failed_convert_char();
706 if (o_len
== 0 || i_len
== 0)
709 /* Keep trying with the next char... */
713 /* Keep compiler happy.... */
720 * Convert between character sets, allocating a new buffer using talloc for the result.
722 * @param srclen length of source buffer.
723 * @param dest always set at least to NULL
724 * @note -1 is not accepted for srclen.
726 * @returns Size in bytes of the converted string; or -1 in case of error.
728 size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
729 void const *src
, size_t srclen
, void *dst
,
732 void **dest
= (void **)dst
;
736 dest_len
=convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
, allow_bad_conv
);
737 if (dest_len
== (size_t)-1)
744 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
749 size
= push_ucs2_allocate(&buffer
, src
);
750 if (size
== (size_t)-1) {
751 smb_panic("failed to create UCS2 buffer");
753 if (!strupper_w(buffer
) && (dest
== src
)) {
758 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
764 strdup() a unix string to upper case.
768 char *strdup_upper(const char *s
)
771 const unsigned char *p
= (const unsigned char *)s
;
772 unsigned char *q
= (unsigned char *)out_buffer
;
774 /* this is quite a common operation, so we want it to be
775 fast. We optimise for the ascii case, knowing that all our
776 supported multi-byte character sets are ascii-compatible
777 (ie. they match for the first 128 chars) */
782 *q
++ = toupper_ascii(*p
);
786 if (p
- ( const unsigned char *)s
>= sizeof(pstring
))
794 size
= convert_string(CH_UNIX
, CH_UTF16LE
, s
, -1, buffer
, sizeof(buffer
), True
);
795 if (size
== (size_t)-1) {
801 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, -1, out_buffer
, sizeof(out_buffer
), True
);
802 if (size
== (size_t)-1) {
807 return SMB_STRDUP(out_buffer
);
810 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
813 smb_ucs2_t
*buffer
= NULL
;
815 size
= convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, srclen
,
816 (void **)(void *)&buffer
, True
);
817 if (size
== (size_t)-1 || !buffer
) {
818 smb_panic("failed to create UCS2 buffer");
820 if (!strlower_w(buffer
) && (dest
== src
)) {
824 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
830 strdup() a unix string to lower case.
833 char *strdup_lower(const char *s
)
836 smb_ucs2_t
*buffer
= NULL
;
839 size
= push_ucs2_allocate(&buffer
, s
);
840 if (size
== -1 || !buffer
) {
846 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
849 if (size
== (size_t)-1) {
856 static size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
858 if (flags
& (STR_NOALIGN
|STR_ASCII
))
860 return PTR_DIFF(p
, base_ptr
) & 1;
865 * Copy a string from a char* unix src to a dos codepage string destination.
867 * @return the number of bytes occupied by the string in the destination.
869 * @param flags can include
871 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
872 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
875 * @param dest_len the maximum length in bytes allowed in the
876 * destination. If @p dest_len is -1 then no maximum is used.
878 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
880 size_t src_len
= strlen(src
);
884 /* No longer allow a length of -1 */
885 if (dest_len
== (size_t)-1)
886 smb_panic("push_ascii - dest_len == -1");
888 if (flags
& STR_UPPER
) {
889 pstrcpy(tmpbuf
, src
);
894 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
897 ret
=convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
898 if (ret
== (size_t)-1 &&
899 (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
901 ((char *)dest
)[0] = '\0';
906 size_t push_ascii_fstring(void *dest
, const char *src
)
908 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
911 size_t push_ascii_pstring(void *dest
, const char *src
)
913 return push_ascii(dest
, src
, sizeof(pstring
), STR_TERMINATE
);
916 /********************************************************************
917 Push an nstring - ensure null terminated. Written by
918 moriyama@miraclelinux.com (MORIYAMA Masayuki).
919 ********************************************************************/
921 size_t push_ascii_nstring(void *dest
, const char *src
)
923 size_t i
, buffer_len
, dest_len
;
927 buffer_len
= push_ucs2_allocate(&buffer
, src
);
928 if (buffer_len
== (size_t)-1) {
929 smb_panic("failed to create UCS2 buffer");
932 /* We're using buffer_len below to count ucs2 characters, not bytes. */
933 buffer_len
/= sizeof(smb_ucs2_t
);
936 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
937 unsigned char mb
[10];
938 /* Convert one smb_ucs2_t character at a time. */
939 size_t mb_len
= convert_string(CH_UTF16LE
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
940 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
941 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
948 ((char *)dest
)[dest_len
] = '\0';
956 * Copy a string from a dos codepage source to a unix char* destination.
958 * The resulting string in "dest" is always null terminated.
960 * @param flags can have:
962 * <dt>STR_TERMINATE</dt>
963 * <dd>STR_TERMINATE means the string in @p src
964 * is null terminated, and src_len is ignored.</dd>
967 * @param src_len is the length of the source area in bytes.
968 * @returns the number of bytes occupied by the string in @p src.
970 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
974 if (dest_len
== (size_t)-1)
975 dest_len
= sizeof(pstring
);
977 if (flags
& STR_TERMINATE
) {
978 if (src_len
== (size_t)-1) {
979 src_len
= strlen((const char *)src
) + 1;
981 size_t len
= strnlen((const char *)src
, src_len
);
988 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
989 if (ret
== (size_t)-1) {
994 if (dest_len
&& ret
) {
995 /* Did we already process the terminating zero ? */
996 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
997 dest
[MIN(ret
, dest_len
-1)] = 0;
1006 size_t pull_ascii_pstring(char *dest
, const void *src
)
1008 return pull_ascii(dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1011 size_t pull_ascii_fstring(char *dest
, const void *src
)
1013 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1016 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1018 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
1020 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
1024 * Copy a string from a char* src to a unicode destination.
1026 * @returns the number of bytes occupied by the string in the destination.
1028 * @param flags can have:
1031 * <dt>STR_TERMINATE <dd>means include the null termination.
1032 * <dt>STR_UPPER <dd>means uppercase in the destination.
1033 * <dt>STR_NOALIGN <dd>means don't do alignment.
1036 * @param dest_len is the maximum length allowed in the
1037 * destination. If dest_len is -1 then no maxiumum is used.
1040 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1046 /* treat a pstring as "unlimited" length */
1047 if (dest_len
== (size_t)-1)
1048 dest_len
= sizeof(pstring
);
1050 if (flags
& STR_TERMINATE
)
1051 src_len
= (size_t)-1;
1053 src_len
= strlen(src
);
1055 if (ucs2_align(base_ptr
, dest
, flags
)) {
1057 dest
= (void *)((char *)dest
+ 1);
1063 /* ucs2 is always a multiple of 2 bytes */
1066 ret
= convert_string(CH_UNIX
, CH_UTF16LE
, src
, src_len
, dest
, dest_len
, True
);
1067 if (ret
== (size_t)-1) {
1073 if (flags
& STR_UPPER
) {
1074 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1077 /* We check for i < (ret / 2) below as the dest string isn't null
1078 terminated if STR_TERMINATE isn't set. */
1080 for (i
= 0; i
< (ret
/ 2) && i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1081 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1082 if (v
!= dest_ucs2
[i
]) {
1093 * Copy a string from a unix char* src to a UCS2 destination,
1094 * allocating a buffer using talloc().
1096 * @param dest always set at least to NULL
1098 * @returns The number of bytes occupied by the string in the destination
1099 * or -1 in case of error.
1101 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1103 size_t src_len
= strlen(src
)+1;
1106 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1111 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1113 * @param dest always set at least to NULL
1115 * @returns The number of bytes occupied by the string in the destination
1116 * or -1 in case of error.
1119 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1121 size_t src_len
= strlen(src
)+1;
1124 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1128 Copy a string from a char* src to a UTF-8 destination.
1129 Return the number of bytes occupied by the string in the destination
1131 STR_TERMINATE means include the null termination
1132 STR_UPPER means uppercase in the destination
1133 dest_len is the maximum length allowed in the destination. If dest_len
1134 is -1 then no maxiumum is used.
1137 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1139 size_t src_len
= strlen(src
);
1142 /* treat a pstring as "unlimited" length */
1143 if (dest_len
== (size_t)-1)
1144 dest_len
= sizeof(pstring
);
1146 if (flags
& STR_UPPER
) {
1147 pstrcpy(tmpbuf
, src
);
1152 if (flags
& STR_TERMINATE
)
1155 return convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1158 size_t push_utf8_fstring(void *dest
, const char *src
)
1160 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1164 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1166 * @param dest always set at least to NULL
1168 * @returns The number of bytes occupied by the string in the destination
1171 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1173 size_t src_len
= strlen(src
)+1;
1176 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1180 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1182 * @param dest always set at least to NULL
1184 * @returns The number of bytes occupied by the string in the destination
1187 size_t push_utf8_allocate(char **dest
, const char *src
)
1189 size_t src_len
= strlen(src
)+1;
1192 return convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, True
);
1196 Copy a string from a ucs2 source to a unix char* destination.
1198 STR_TERMINATE means the string in src is null terminated.
1199 STR_NOALIGN means don't try to align.
1200 if STR_TERMINATE is set then src_len is ignored if it is -1.
1201 src_len is the length of the source area in bytes
1202 Return the number of bytes occupied by the string in src.
1203 The resulting string in "dest" is always null terminated.
1206 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1210 if (dest_len
== (size_t)-1)
1211 dest_len
= sizeof(pstring
);
1213 if (ucs2_align(base_ptr
, src
, flags
)) {
1214 src
= (const void *)((const char *)src
+ 1);
1215 if (src_len
!= (size_t)-1)
1219 if (flags
& STR_TERMINATE
) {
1220 /* src_len -1 is the default for null terminated strings. */
1221 if (src_len
!= (size_t)-1) {
1222 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1224 if (len
< src_len
/2)
1230 /* ucs2 is always a multiple of 2 bytes */
1231 if (src_len
!= (size_t)-1)
1234 ret
= convert_string(CH_UTF16LE
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1235 if (ret
== (size_t)-1) {
1239 if (src_len
== (size_t)-1)
1242 if (dest_len
&& ret
) {
1243 /* Did we already process the terminating zero ? */
1244 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1245 dest
[MIN(ret
, dest_len
-1)] = 0;
1254 size_t pull_ucs2_pstring(char *dest
, const void *src
)
1256 return pull_ucs2(NULL
, dest
, src
, sizeof(pstring
), -1, STR_TERMINATE
);
1259 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1261 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1265 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1267 * @param dest always set at least to NULL
1269 * @returns The number of bytes occupied by the string in the destination
1272 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1274 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1276 return convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1280 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1282 * @param dest always set at least to NULL
1284 * @returns The number of bytes occupied by the string in the destination
1287 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1289 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1291 return convert_string_allocate(NULL
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1295 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1297 * @param dest always set at least to NULL
1299 * @returns The number of bytes occupied by the string in the destination
1302 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1304 size_t src_len
= strlen(src
)+1;
1306 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1310 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1312 * @param dest always set at least to NULL
1314 * @returns The number of bytes occupied by the string in the destination
1317 size_t pull_utf8_allocate(char **dest
, const char *src
)
1319 size_t src_len
= strlen(src
)+1;
1321 return convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1325 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1327 * @param dest always set at least to NULL
1329 * @returns The number of bytes occupied by the string in the destination
1332 size_t pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1334 size_t src_len
= strlen(src
)+1;
1336 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1340 Copy a string from a char* src to a unicode or ascii
1341 dos codepage destination choosing unicode or ascii based on the
1342 flags in the SMB buffer starting at base_ptr.
1343 Return the number of bytes occupied by the string in the destination.
1345 STR_TERMINATE means include the null termination.
1346 STR_UPPER means uppercase in the destination.
1347 STR_ASCII use ascii even with unicode packet.
1348 STR_NOALIGN means don't do alignment.
1349 dest_len is the maximum length allowed in the destination. If dest_len
1350 is -1 then no maxiumum is used.
1353 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
)
1356 /* We really need to zero fill here, not clobber
1357 * region, as we want to ensure that valgrind thinks
1358 * all of the outgoing buffer has been written to
1359 * so a send() or write() won't trap an error.
1363 if (dest_len
!= (size_t)-1)
1364 clobber_region(function
, line
, dest
, dest_len
);
1366 if (dest_len
!= (size_t)-1)
1367 memset(dest
, '\0', dest_len
);
1371 if (!(flags
& STR_ASCII
) && \
1372 ((flags
& STR_UNICODE
|| \
1373 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1374 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1376 return push_ascii(dest
, src
, dest_len
, flags
);
1381 Copy a string from a unicode or ascii source (depending on
1382 the packet flags) to a char* destination.
1384 STR_TERMINATE means the string in src is null terminated.
1385 STR_UNICODE means to force as unicode.
1386 STR_ASCII use ascii even with unicode packet.
1387 STR_NOALIGN means don't do alignment.
1388 if STR_TERMINATE is set then src_len is ignored is it is -1
1389 src_len is the length of the source area in bytes.
1390 Return the number of bytes occupied by the string in src.
1391 The resulting string in "dest" is always null terminated.
1394 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
)
1397 if (dest_len
!= (size_t)-1)
1398 clobber_region(function
, line
, dest
, dest_len
);
1401 if (!(flags
& STR_ASCII
) && \
1402 ((flags
& STR_UNICODE
|| \
1403 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1404 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1406 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1409 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1411 if (!(flags
& STR_ASCII
) && \
1412 ((flags
& STR_UNICODE
|| \
1413 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1414 return ucs2_align(base_ptr
, p
, flags
);
1420 Return the unicode codepoint for the next multi-byte CH_UNIX character
1421 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1423 Also return the number of bytes consumed (which tells the caller
1424 how many bytes to skip to get to the next CH_UNIX character).
1426 Return INVALID_CODEPOINT if the next character cannot be converted.
1429 codepoint_t
next_codepoint(const char *str
, size_t *size
)
1431 /* It cannot occupy more than 4 bytes in UTF16 format */
1433 smb_iconv_t descriptor
;
1439 if ((str
[0] & 0x80) == 0) {
1441 return (codepoint_t
)str
[0];
1444 /* We assume that no multi-byte character can take
1445 more than 5 bytes. This is OK as we only
1446 support codepoints up to 1M */
1448 ilen_orig
= strnlen(str
, 5);
1451 lazy_initialize_conv();
1453 descriptor
= conv_handles
[CH_UNIX
][CH_UTF16LE
];
1454 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
1456 return INVALID_CODEPOINT
;
1459 /* This looks a little strange, but it is needed to cope
1460 with codepoints above 64k which are encoded as per RFC2781. */
1462 outbuf
= (char *)buf
;
1463 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1465 /* We failed to convert to a 2 byte character.
1466 See if we can convert to a 4 UTF16-LE byte char encoding.
1469 outbuf
= (char *)buf
;
1470 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1472 /* We didn't convert any bytes */
1474 return INVALID_CODEPOINT
;
1481 *size
= ilen_orig
- ilen
;
1484 /* 2 byte, UTF16-LE encoded value. */
1485 return (codepoint_t
)SVAL(buf
, 0);
1488 /* Decode a 4 byte UTF16-LE character manually.
1489 See RFC2871 for the encoding machanism.
1491 codepoint_t w1
= SVAL(buf
,0) & ~0xD800;
1492 codepoint_t w2
= SVAL(buf
,2) & ~0xDC00;
1494 return (codepoint_t
)0x10000 +
1498 /* no other length is valid */
1499 return INVALID_CODEPOINT
;