2 Unix SMB/CIFS implementation.
3 Character set conversion Extensions
4 Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
5 Copyright (C) Andrew Tridgell 2001
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Martin Pool 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /* We can parameterize this if someone complains.... JRA. */
27 char lp_failed_convert_char(void)
35 * @brief Character-set conversion routines built on our iconv.
37 * @note Samba's internal character set (at least in the 3.0 series)
38 * is always the same as the one for the Unix filesystem. It is
39 * <b>not</b> necessarily UTF-8 and may be different on machines that
40 * need i18n filenames to be compatible with Unix software. It does
41 * have to be a superset of ASCII. All multibyte sequences must start
42 * with a byte with the high bit set.
48 static smb_iconv_t conv_handles
[NUM_CHARSETS
][NUM_CHARSETS
];
49 static bool conv_silent
; /* Should we do a debug if the conversion fails ? */
50 static bool initialized
;
53 * Return the name of a charset to give to iconv().
55 static const char *charset_name(charset_t ch
)
57 const char *ret
= NULL
;
59 if (ch
== CH_UTF16LE
) ret
= "UTF-16LE";
60 else if (ch
== CH_UTF16BE
) ret
= "UTF-16BE";
61 else if (ch
== CH_UNIX
) ret
= lp_unix_charset();
62 else if (ch
== CH_DOS
) ret
= lp_dos_charset();
63 else if (ch
== CH_DISPLAY
) ret
= lp_display_charset();
64 else if (ch
== CH_UTF8
) ret
= "UTF8";
66 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
67 if (ret
&& !strcmp(ret
, "LOCALE")) {
68 const char *ln
= NULL
;
71 setlocale(LC_ALL
, "");
73 ln
= nl_langinfo(CODESET
);
75 /* Check whether the charset name is supported
77 smb_iconv_t handle
= smb_iconv_open(ln
,"UCS-2LE");
78 if (handle
== (smb_iconv_t
) -1) {
79 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln
));
82 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln
));
83 smb_iconv_close(handle
);
90 if (!ret
|| !*ret
) ret
= "ASCII";
94 void lazy_initialize_conv(void)
104 * Destroy global objects allocated by init_iconv()
106 void gfree_charcnv(void)
110 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
111 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
112 if ( conv_handles
[c1
][c2
] ) {
113 smb_iconv_close( conv_handles
[c1
][c2
] );
114 conv_handles
[c1
][c2
] = 0;
122 * Initialize iconv conversion descriptors.
124 * This is called the first time it is needed, and also called again
125 * every time the configuration is reloaded, because the charset or
126 * codepage might have changed.
128 void init_iconv(void)
131 bool did_reload
= False
;
133 /* so that charset_name() works we need to get the UNIX<->UCS2 going
135 if (!conv_handles
[CH_UNIX
][CH_UTF16LE
])
136 conv_handles
[CH_UNIX
][CH_UTF16LE
] = smb_iconv_open(charset_name(CH_UTF16LE
), "ASCII");
138 if (!conv_handles
[CH_UTF16LE
][CH_UNIX
])
139 conv_handles
[CH_UTF16LE
][CH_UNIX
] = smb_iconv_open("ASCII", charset_name(CH_UTF16LE
));
141 for (c1
=0;c1
<NUM_CHARSETS
;c1
++) {
142 for (c2
=0;c2
<NUM_CHARSETS
;c2
++) {
143 const char *n1
= charset_name((charset_t
)c1
);
144 const char *n2
= charset_name((charset_t
)c2
);
145 if (conv_handles
[c1
][c2
] &&
146 strcmp(n1
, conv_handles
[c1
][c2
]->from_name
) == 0 &&
147 strcmp(n2
, conv_handles
[c1
][c2
]->to_name
) == 0)
152 if (conv_handles
[c1
][c2
])
153 smb_iconv_close(conv_handles
[c1
][c2
]);
155 conv_handles
[c1
][c2
] = smb_iconv_open(n2
,n1
);
156 if (conv_handles
[c1
][c2
] == (smb_iconv_t
)-1) {
157 DEBUG(0,("init_iconv: Conversion from %s to %s not supported\n",
158 charset_name((charset_t
)c1
), charset_name((charset_t
)c2
)));
159 if (c1
!= CH_UTF16LE
&& c1
!= CH_UTF16BE
) {
162 if (c2
!= CH_UTF16LE
&& c2
!= CH_UTF16BE
) {
165 DEBUG(0,("init_iconv: Attempting to replace with conversion from %s to %s\n",
167 conv_handles
[c1
][c2
] = smb_iconv_open(n2
,n1
);
168 if (!conv_handles
[c1
][c2
]) {
169 DEBUG(0,("init_iconv: Conversion from %s to %s failed", n1
, n2
));
170 smb_panic("init_iconv: conv_handle initialization failed");
177 /* XXX: Does this really get called every time the dos
178 * codepage changes? */
179 /* XXX: Is the did_reload test too strict? */
187 * Convert string from one encoding to another, making error checking etc
188 * Slow path version - uses (slow) iconv.
190 * @param src pointer to source string (multibyte or singlebyte)
191 * @param srclen length of the source string in bytes
192 * @param dest pointer to destination string (multibyte or singlebyte)
193 * @param destlen maximal length allowed for string
194 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
195 * @returns the number of bytes occupied in the destination
197 * Ensure the srclen contains the terminating zero.
201 static size_t convert_string_internal(charset_t from
, charset_t to
,
202 void const *src
, size_t srclen
,
203 void *dest
, size_t destlen
, bool allow_bad_conv
)
207 const char* inbuf
= (const char*)src
;
208 char* outbuf
= (char*)dest
;
209 smb_iconv_t descriptor
;
211 lazy_initialize_conv();
213 descriptor
= conv_handles
[from
][to
];
215 if (srclen
== (size_t)-1) {
216 if (from
== CH_UTF16LE
|| from
== CH_UTF16BE
) {
217 srclen
= (strlen_w((const smb_ucs2_t
*)src
)+1) * 2;
219 srclen
= strlen((const char *)src
)+1;
224 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
226 DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
235 retval
= smb_iconv(descriptor
, &inbuf
, &i_len
, &outbuf
, &o_len
);
236 if(retval
==(size_t)-1) {
237 const char *reason
="unknown error";
240 reason
="Incomplete multibyte sequence";
242 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
247 reason
="No more room";
249 if (from
== CH_UNIX
) {
250 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
251 charset_name(from
), charset_name(to
),
252 (unsigned int)srclen
, (unsigned int)destlen
, (const char *)src
));
254 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
255 charset_name(from
), charset_name(to
),
256 (unsigned int)srclen
, (unsigned int)destlen
));
261 reason
="Illegal multibyte sequence";
263 DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
269 DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason
,inbuf
));
272 /* smb_panic(reason); */
274 return destlen
-o_len
;
279 * Conversion not supported. This is actually an error, but there are so
280 * many misconfigured iconv systems and smb.conf's out there we can't just
281 * fail. Do a very bad conversion instead.... JRA.
285 if (o_len
== 0 || i_len
== 0)
286 return destlen
- o_len
;
288 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
289 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
290 /* Can't convert from utf16 any endian to multibyte.
291 Replace with the default fail char.
294 return destlen
- o_len
;
296 *outbuf
= lp_failed_convert_char();
305 if (o_len
== 0 || i_len
== 0)
306 return destlen
- o_len
;
308 /* Keep trying with the next char... */
311 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
312 /* Can't convert to UTF16LE - just widen by adding the
313 default fail char then zero.
316 return destlen
- o_len
;
318 outbuf
[0] = lp_failed_convert_char();
327 if (o_len
== 0 || i_len
== 0)
328 return destlen
- o_len
;
330 /* Keep trying with the next char... */
333 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
334 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
335 /* Failed multibyte to multibyte. Just copy the default fail char and
337 outbuf
[0] = lp_failed_convert_char();
345 if (o_len
== 0 || i_len
== 0)
346 return destlen
- o_len
;
348 /* Keep trying with the next char... */
352 /* Keep compiler happy.... */
353 return destlen
- o_len
;
359 * Convert string from one encoding to another, making error checking etc
360 * Fast path version - handles ASCII first.
362 * @param src pointer to source string (multibyte or singlebyte)
363 * @param srclen length of the source string in bytes, or -1 for nul terminated.
364 * @param dest pointer to destination string (multibyte or singlebyte)
365 * @param destlen maximal length allowed for string - *NEVER* -1.
366 * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
367 * @returns the number of bytes occupied in the destination
369 * Ensure the srclen contains the terminating zero.
371 * This function has been hand-tuned to provide a fast path.
372 * Don't change unless you really know what you are doing. JRA.
375 size_t convert_string(charset_t from
, charset_t to
,
376 void const *src
, size_t srclen
,
377 void *dest
, size_t destlen
, bool allow_bad_conv
)
380 * NB. We deliberately don't do a strlen here if srclen == -1.
381 * This is very expensive over millions of calls and is taken
382 * care of in the slow path in convert_string_internal. JRA.
386 SMB_ASSERT(destlen
!= (size_t)-1);
392 if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
393 const unsigned char *p
= (const unsigned char *)src
;
394 unsigned char *q
= (unsigned char *)dest
;
395 size_t slen
= srclen
;
396 size_t dlen
= destlen
;
397 unsigned char lastp
= '\0';
400 /* If all characters are ascii, fast path here. */
401 while (slen
&& dlen
) {
402 if ((lastp
= *p
) <= 0x7f) {
404 if (slen
!= (size_t)-1) {
412 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
415 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
420 /* Even if we fast path we should note if we ran out of room. */
421 if (((slen
!= (size_t)-1) && slen
) ||
422 ((slen
== (size_t)-1) && lastp
)) {
427 } else if (from
== CH_UTF16LE
&& to
!= CH_UTF16LE
) {
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
;
433 unsigned char lastp
= '\0';
435 /* If all characters are ascii, fast path here. */
436 while (((slen
== (size_t)-1) || (slen
>= 2)) && dlen
) {
437 if (((lastp
= *p
) <= 0x7f) && (p
[1] == 0)) {
439 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
);
456 /* Even if we fast path we should note if we ran out of room. */
457 if (((slen
!= (size_t)-1) && slen
) ||
458 ((slen
== (size_t)-1) && lastp
)) {
463 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
464 const unsigned char *p
= (const unsigned char *)src
;
465 unsigned char *q
= (unsigned char *)dest
;
467 size_t slen
= srclen
;
468 size_t dlen
= destlen
;
469 unsigned char lastp
= '\0';
471 /* If all characters are ascii, fast path here. */
472 while (slen
&& (dlen
>= 2)) {
473 if ((lastp
= *p
) <= 0x7F) {
476 if (slen
!= (size_t)-1) {
484 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
487 return retval
+ convert_string_internal(from
, to
, p
, slen
, q
, dlen
, allow_bad_conv
);
492 /* Even if we fast path we should note if we ran out of room. */
493 if (((slen
!= (size_t)-1) && slen
) ||
494 ((slen
== (size_t)-1) && lastp
)) {
501 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
504 return convert_string_internal(from
, to
, src
, srclen
, dest
, destlen
, allow_bad_conv
);
508 * Convert between character sets, allocating a new buffer for the result.
510 * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
511 * (this is a bad interface and needs fixing. JRA).
512 * @param srclen length of source buffer.
513 * @param dest always set at least to NULL
514 * @param converted_size set to the size of the allocated buffer on return
516 * @note -1 is not accepted for srclen.
518 * @return True if new buffer was correctly allocated, and string was
521 * Ensure the srclen contains the terminating zero.
523 * I hate the goto's in this function. It's embarressing.....
524 * There has to be a cleaner way to do this. JRA.
527 bool convert_string_allocate(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
528 void const *src
, size_t srclen
, void *dst
,
529 size_t *converted_size
, bool allow_bad_conv
)
531 size_t i_len
, o_len
, destlen
= (srclen
* 3) / 2;
533 const char *inbuf
= (const char *)src
;
534 char *outbuf
= NULL
, *ob
= NULL
;
535 smb_iconv_t descriptor
;
536 void **dest
= (void **)dst
;
540 if (!converted_size
) {
545 if (src
== NULL
|| srclen
== (size_t)-1) {
554 lazy_initialize_conv();
556 descriptor
= conv_handles
[from
][to
];
558 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
560 DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
567 /* +2 is for ucs2 null termination. */
568 if ((destlen
*2)+2 < destlen
) {
569 /* wrapped ! abort. */
571 DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
577 destlen
= destlen
* 2;
580 /* +2 is for ucs2 null termination. */
582 ob
= (char *)TALLOC_REALLOC(ctx
, ob
, destlen
+ 2);
584 ob
= (char *)SMB_REALLOC(ob
, destlen
+ 2);
588 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
598 retval
= smb_iconv(descriptor
,
601 if(retval
== (size_t)-1) {
602 const char *reason
="unknown error";
605 reason
="Incomplete multibyte sequence";
607 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
614 reason
="Illegal multibyte sequence";
616 DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason
,inbuf
));
622 DEBUG(0,("Conversion error: %s(%s)\n",reason
,inbuf
));
623 /* smb_panic(reason); */
634 destlen
= destlen
- o_len
;
635 /* Don't shrink unless we're reclaiming a lot of
636 * space. This is in the hot codepath and these
637 * reallocs *cost*. JRA.
640 /* We're shrinking here so we know the +2 is safe from wrap. */
642 ob
= (char *)TALLOC_REALLOC(ctx
,ob
,destlen
+ 2);
644 ob
= (char *)SMB_REALLOC(ob
,destlen
+ 2);
648 if (destlen
&& !ob
) {
649 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
656 /* Must ucs2 null terminate in the extra space we allocated. */
658 ob
[destlen
+1] = '\0';
660 *converted_size
= destlen
;
666 * Conversion not supported. This is actually an error, but there are so
667 * many misconfigured iconv systems and smb.conf's out there we can't just
668 * fail. Do a very bad conversion instead.... JRA.
672 if (o_len
== 0 || i_len
== 0)
675 if (((from
== CH_UTF16LE
)||(from
== CH_UTF16BE
)) &&
676 ((to
!= CH_UTF16LE
)||(to
!= CH_UTF16BE
))) {
677 /* Can't convert from utf16 any endian to multibyte.
678 Replace with the default fail char.
685 *outbuf
= lp_failed_convert_char();
694 if (o_len
== 0 || i_len
== 0)
697 /* Keep trying with the next char... */
700 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&& to
== CH_UTF16LE
) {
701 /* Can't convert to UTF16LE - just widen by adding the
702 default fail char then zero.
707 outbuf
[0] = lp_failed_convert_char();
716 if (o_len
== 0 || i_len
== 0)
719 /* Keep trying with the next char... */
722 } else if (from
!= CH_UTF16LE
&& from
!= CH_UTF16BE
&&
723 to
!= CH_UTF16LE
&& to
!= CH_UTF16BE
) {
724 /* Failed multibyte to multibyte. Just copy the default fail char and
726 outbuf
[0] = lp_failed_convert_char();
734 if (o_len
== 0 || i_len
== 0)
737 /* Keep trying with the next char... */
741 /* Keep compiler happy.... */
748 * Convert between character sets, allocating a new buffer using talloc for the result.
750 * @param srclen length of source buffer.
751 * @param dest always set at least to NULL
752 * @note -1 is not accepted for srclen.
754 * @returns Size in bytes of the converted string; or -1 in case of error.
756 size_t convert_string_talloc(TALLOC_CTX
*ctx
, charset_t from
, charset_t to
,
757 void const *src
, size_t srclen
, void *dst
,
760 void **dest
= (void **)dst
;
764 if (!convert_string_allocate(ctx
, from
, to
, src
, srclen
, dest
,
765 &dest_len
, allow_bad_conv
))
772 size_t unix_strupper(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
777 size
= push_ucs2_allocate(&buffer
, src
);
778 if (size
== (size_t)-1) {
781 if (!strupper_w(buffer
) && (dest
== src
)) {
786 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
792 strdup() a unix string to upper case.
795 char *strdup_upper(const char *s
)
797 char *out_buffer
= SMB_STRDUP(s
);
798 const unsigned char *p
= (const unsigned char *)s
;
799 unsigned char *q
= (unsigned char *)out_buffer
;
805 /* this is quite a common operation, so we want it to be
806 fast. We optimise for the ascii case, knowing that all our
807 supported multi-byte character sets are ascii-compatible
808 (ie. they match for the first 128 chars) */
813 *q
++ = toupper_ascii_fast(*p
);
820 smb_ucs2_t
*buffer
= NULL
;
822 SAFE_FREE(out_buffer
);
823 if (!convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, s
,
824 strlen(s
) + 1, (void **)(void *)&buffer
, &size
,
831 if (!convert_string_allocate(NULL
, CH_UTF16LE
, CH_UNIX
, buffer
,
832 size
, (void **)(void *)&out_buffer
, &size2
, True
)) {
837 /* Don't need the intermediate buffer
847 talloc_strdup() a unix string to upper case.
850 char *talloc_strdup_upper(TALLOC_CTX
*ctx
, const char *s
)
852 char *out_buffer
= talloc_strdup(ctx
,s
);
853 const unsigned char *p
= (const unsigned char *)s
;
854 unsigned char *q
= (unsigned char *)out_buffer
;
860 /* this is quite a common operation, so we want it to be
861 fast. We optimise for the ascii case, knowing that all our
862 supported multi-byte character sets are ascii-compatible
863 (ie. they match for the first 128 chars) */
868 *q
++ = toupper_ascii_fast(*p
);
875 smb_ucs2_t
*ubuf
= NULL
;
877 /* We're not using the ascii buffer above. */
878 TALLOC_FREE(out_buffer
);
880 size
= convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
,
884 if (size
== (size_t)-1) {
890 size
= convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
,
895 /* Don't need the intermediate buffer
901 if (size
== (size_t)-1) {
909 size_t unix_strlower(const char *src
, size_t srclen
, char *dest
, size_t destlen
)
912 smb_ucs2_t
*buffer
= NULL
;
914 if (!convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, srclen
,
915 (void **)(void *)&buffer
, &size
, True
)) {
916 smb_panic("failed to create UCS2 buffer");
918 if (!strlower_w(buffer
) && (dest
== src
)) {
922 size
= convert_string(CH_UTF16LE
, CH_UNIX
, buffer
, size
, dest
, destlen
, True
);
928 strdup() a unix string to lower case.
931 char *strdup_lower(const char *s
)
934 smb_ucs2_t
*buffer
= NULL
;
937 size
= push_ucs2_allocate(&buffer
, s
);
938 if (size
== -1 || !buffer
) {
944 size
= pull_ucs2_allocate(&out_buffer
, buffer
);
947 if (size
== (size_t)-1) {
954 char *talloc_strdup_lower(TALLOC_CTX
*ctx
, const char *s
)
957 smb_ucs2_t
*buffer
= NULL
;
960 size
= push_ucs2_talloc(ctx
, &buffer
, s
);
961 if (size
== -1 || !buffer
) {
968 size
= pull_ucs2_talloc(ctx
, &out_buffer
, buffer
);
971 if (size
== (size_t)-1) {
972 TALLOC_FREE(out_buffer
);
980 size_t ucs2_align(const void *base_ptr
, const void *p
, int flags
)
982 if (flags
& (STR_NOALIGN
|STR_ASCII
))
984 return PTR_DIFF(p
, base_ptr
) & 1;
989 * Copy a string from a char* unix src to a dos codepage string destination.
991 * @return the number of bytes occupied by the string in the destination.
993 * @param flags can include
995 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
996 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
999 * @param dest_len the maximum length in bytes allowed in the
1002 size_t push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
1004 size_t src_len
= strlen(src
);
1005 char *tmpbuf
= NULL
;
1008 /* No longer allow a length of -1. */
1009 if (dest_len
== (size_t)-1) {
1010 smb_panic("push_ascii - dest_len == -1");
1013 if (flags
& STR_UPPER
) {
1014 tmpbuf
= SMB_STRDUP(src
);
1016 smb_panic("malloc fail");
1022 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
)) {
1026 ret
= convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, True
);
1027 if (ret
== (size_t)-1 &&
1028 (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
1030 ((char *)dest
)[0] = '\0';
1036 size_t push_ascii_fstring(void *dest
, const char *src
)
1038 return push_ascii(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1041 /********************************************************************
1042 Push an nstring - ensure null terminated. Written by
1043 moriyama@miraclelinux.com (MORIYAMA Masayuki).
1044 ********************************************************************/
1046 size_t push_ascii_nstring(void *dest
, const char *src
)
1048 size_t i
, buffer_len
, dest_len
;
1052 buffer_len
= push_ucs2_allocate(&buffer
, src
);
1053 if (buffer_len
== (size_t)-1) {
1054 smb_panic("failed to create UCS2 buffer");
1057 /* We're using buffer_len below to count ucs2 characters, not bytes. */
1058 buffer_len
/= sizeof(smb_ucs2_t
);
1061 for (i
= 0; buffer
[i
] != 0 && (i
< buffer_len
); i
++) {
1062 unsigned char mb
[10];
1063 /* Convert one smb_ucs2_t character at a time. */
1064 size_t mb_len
= convert_string(CH_UTF16LE
, CH_DOS
, buffer
+i
, sizeof(smb_ucs2_t
), mb
, sizeof(mb
), False
);
1065 if ((mb_len
!= (size_t)-1) && (dest_len
+ mb_len
<= MAX_NETBIOSNAME_LEN
- 1)) {
1066 memcpy((char *)dest
+ dest_len
, mb
, mb_len
);
1073 ((char *)dest
)[dest_len
] = '\0';
1076 conv_silent
= False
;
1080 /********************************************************************
1081 Push and malloc an ascii string. src and dest null terminated.
1082 ********************************************************************/
1084 size_t push_ascii_allocate(char **dest
, const char *src
)
1086 size_t dest_len
, src_len
= strlen(src
)+1;
1089 if (!convert_string_allocate(NULL
, CH_UNIX
, CH_DOS
, src
, src_len
,
1090 (void **)dest
, &dest_len
, True
))
1097 * Copy a string from a dos codepage source to a unix char* destination.
1099 * The resulting string in "dest" is always null terminated.
1101 * @param flags can have:
1103 * <dt>STR_TERMINATE</dt>
1104 * <dd>STR_TERMINATE means the string in @p src
1105 * is null terminated, and src_len is ignored.</dd>
1108 * @param src_len is the length of the source area in bytes.
1109 * @returns the number of bytes occupied by the string in @p src.
1111 size_t pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1115 if (dest_len
== (size_t)-1) {
1116 /* No longer allow dest_len of -1. */
1117 smb_panic("pull_ascii - invalid dest_len of -1");
1120 if (flags
& STR_TERMINATE
) {
1121 if (src_len
== (size_t)-1) {
1122 src_len
= strlen((const char *)src
) + 1;
1124 size_t len
= strnlen((const char *)src
, src_len
);
1131 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1132 if (ret
== (size_t)-1) {
1137 if (dest_len
&& ret
) {
1138 /* Did we already process the terminating zero ? */
1139 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1140 dest
[MIN(ret
, dest_len
-1)] = 0;
1150 * Copy a string from a dos codepage source to a unix char* destination.
1152 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1155 * The resulting string in "dest" is always null terminated.
1157 * @param flags can have:
1159 * <dt>STR_TERMINATE</dt>
1160 * <dd>STR_TERMINATE means the string in @p src
1161 * is null terminated, and src_len is ignored.</dd>
1164 * @param src_len is the length of the source area in bytes.
1165 * @returns the number of bytes occupied by the string in @p src.
1168 static size_t pull_ascii_base_talloc(TALLOC_CTX
*ctx
,
1175 size_t dest_len
= 0;
1178 /* Ensure we never use the braindead "malloc" varient. */
1180 smb_panic("NULL talloc CTX in pull_ascii_base_talloc\n");
1186 if (flags
& STR_TERMINATE
) {
1187 if (src_len
== (size_t)-1) {
1188 src_len
= strlen((const char *)src
) + 1;
1190 size_t len
= strnlen((const char *)src
, src_len
);
1195 /* Ensure we don't use an insane length from the client. */
1196 if (src_len
>= 1024*1024) {
1197 char *msg
= talloc_asprintf(ctx
,
1198 "Bad src length (%u) in "
1199 "pull_ascii_base_talloc",
1200 (unsigned int)src_len
);
1205 if (!convert_string_allocate(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, &dest
,
1209 if (dest_len
&& dest
) {
1210 /* Did we already process the terminating zero ? */
1211 if (dest
[dest_len
-1] != 0) {
1212 dest
[dest_len
-1] = 0;
1222 size_t pull_ascii_fstring(char *dest
, const void *src
)
1224 return pull_ascii(dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1227 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1229 size_t pull_ascii_nstring(char *dest
, size_t dest_len
, const void *src
)
1231 return pull_ascii(dest
, src
, dest_len
, sizeof(nstring
)-1, STR_TERMINATE
);
1235 * Copy a string from a char* src to a unicode destination.
1237 * @returns the number of bytes occupied by the string in the destination.
1239 * @param flags can have:
1242 * <dt>STR_TERMINATE <dd>means include the null termination.
1243 * <dt>STR_UPPER <dd>means uppercase in the destination.
1244 * <dt>STR_NOALIGN <dd>means don't do alignment.
1247 * @param dest_len is the maximum length allowed in the
1251 size_t push_ucs2(const void *base_ptr
, void *dest
, const char *src
, size_t dest_len
, int flags
)
1257 if (dest_len
== (size_t)-1) {
1258 /* No longer allow dest_len of -1. */
1259 smb_panic("push_ucs2 - invalid dest_len of -1");
1262 if (flags
& STR_TERMINATE
)
1263 src_len
= (size_t)-1;
1265 src_len
= strlen(src
);
1267 if (ucs2_align(base_ptr
, dest
, flags
)) {
1269 dest
= (void *)((char *)dest
+ 1);
1275 /* ucs2 is always a multiple of 2 bytes */
1278 ret
= convert_string(CH_UNIX
, CH_UTF16LE
, src
, src_len
, dest
, dest_len
, True
);
1279 if (ret
== (size_t)-1) {
1280 if ((flags
& STR_TERMINATE
) &&
1290 if (flags
& STR_UPPER
) {
1291 smb_ucs2_t
*dest_ucs2
= (smb_ucs2_t
*)dest
;
1294 /* We check for i < (ret / 2) below as the dest string isn't null
1295 terminated if STR_TERMINATE isn't set. */
1297 for (i
= 0; i
< (ret
/ 2) && i
< (dest_len
/ 2) && dest_ucs2
[i
]; i
++) {
1298 smb_ucs2_t v
= toupper_w(dest_ucs2
[i
]);
1299 if (v
!= dest_ucs2
[i
]) {
1310 * Copy a string from a unix char* src to a UCS2 destination,
1311 * allocating a buffer using talloc().
1313 * @param dest always set at least to NULL
1315 * @returns The number of bytes occupied by the string in the destination
1316 * or -1 in case of error.
1318 size_t push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
1320 size_t src_len
= strlen(src
)+1;
1323 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16LE
, src
, src_len
, (void **)dest
, True
);
1328 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1330 * @param dest always set at least to NULL
1332 * @returns The number of bytes occupied by the string in the destination
1333 * or -1 in case of error.
1336 size_t push_ucs2_allocate(smb_ucs2_t
**dest
, const char *src
)
1338 size_t dest_len
, src_len
= strlen(src
)+1;
1341 if (!convert_string_allocate(NULL
, CH_UNIX
, CH_UTF16LE
, src
, src_len
,
1342 (void **)dest
, &dest_len
, True
))
1349 Copy a string from a char* src to a UTF-8 destination.
1350 Return the number of bytes occupied by the string in the destination
1352 STR_TERMINATE means include the null termination
1353 STR_UPPER means uppercase in the destination
1354 dest_len is the maximum length allowed in the destination. If dest_len
1355 is -1 then no maxiumum is used.
1358 static size_t push_utf8(void *dest
, const char *src
, size_t dest_len
, int flags
)
1362 char *tmpbuf
= NULL
;
1364 if (dest_len
== (size_t)-1) {
1365 /* No longer allow dest_len of -1. */
1366 smb_panic("push_utf8 - invalid dest_len of -1");
1369 if (flags
& STR_UPPER
) {
1370 tmpbuf
= strdup_upper(src
);
1375 src_len
= strlen(src
);
1378 src_len
= strlen(src
);
1379 if (flags
& STR_TERMINATE
) {
1383 ret
= convert_string(CH_UNIX
, CH_UTF8
, src
, src_len
, dest
, dest_len
, True
);
1388 size_t push_utf8_fstring(void *dest
, const char *src
)
1390 return push_utf8(dest
, src
, sizeof(fstring
), STR_TERMINATE
);
1394 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1396 * @param dest always set at least to NULL
1398 * @returns The number of bytes occupied by the string in the destination
1401 size_t push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1403 size_t src_len
= strlen(src
)+1;
1406 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void**)dest
, True
);
1410 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1412 * @param dest always set at least to NULL
1414 * @returns The number of bytes occupied by the string in the destination
1417 size_t push_utf8_allocate(char **dest
, const char *src
)
1419 size_t dest_len
, src_len
= strlen(src
)+1;
1422 if (!convert_string_allocate(NULL
, CH_UNIX
, CH_UTF8
, src
, src_len
,
1423 (void **)dest
, &dest_len
, True
))
1430 Copy a string from a ucs2 source to a unix char* destination.
1432 STR_TERMINATE means the string in src is null terminated.
1433 STR_NOALIGN means don't try to align.
1434 if STR_TERMINATE is set then src_len is ignored if it is -1.
1435 src_len is the length of the source area in bytes
1436 Return the number of bytes occupied by the string in src.
1437 The resulting string in "dest" is always null terminated.
1440 size_t pull_ucs2(const void *base_ptr
, char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
1444 if (dest_len
== (size_t)-1) {
1445 /* No longer allow dest_len of -1. */
1446 smb_panic("pull_ucs2 - invalid dest_len of -1");
1450 if (dest
&& dest_len
> 0) {
1456 if (ucs2_align(base_ptr
, src
, flags
)) {
1457 src
= (const void *)((const char *)src
+ 1);
1458 if (src_len
!= (size_t)-1)
1462 if (flags
& STR_TERMINATE
) {
1463 /* src_len -1 is the default for null terminated strings. */
1464 if (src_len
!= (size_t)-1) {
1465 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1467 if (len
< src_len
/2)
1473 /* ucs2 is always a multiple of 2 bytes */
1474 if (src_len
!= (size_t)-1)
1477 ret
= convert_string(CH_UTF16LE
, CH_UNIX
, src
, src_len
, dest
, dest_len
, True
);
1478 if (ret
== (size_t)-1) {
1483 if (src_len
== (size_t)-1)
1486 if (dest_len
&& ret
) {
1487 /* Did we already process the terminating zero ? */
1488 if (dest
[MIN(ret
-1, dest_len
-1)] != 0) {
1489 dest
[MIN(ret
, dest_len
-1)] = 0;
1499 Copy a string from a ucs2 source to a unix char* destination.
1500 Talloc version with a base pointer.
1501 Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1504 STR_TERMINATE means the string in src is null terminated.
1505 STR_NOALIGN means don't try to align.
1506 if STR_TERMINATE is set then src_len is ignored if it is -1.
1507 src_len is the length of the source area in bytes
1508 Return the number of bytes occupied by the string in src.
1509 The resulting string in "dest" is always null terminated.
1512 size_t pull_ucs2_base_talloc(TALLOC_CTX
*ctx
,
1513 const void *base_ptr
,
1525 /* Ensure we never use the braindead "malloc" varient. */
1527 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
1535 if (ucs2_align(base_ptr
, src
, flags
)) {
1536 src
= (const void *)((const char *)src
+ 1);
1537 if (src_len
!= (size_t)-1)
1541 if (flags
& STR_TERMINATE
) {
1542 /* src_len -1 is the default for null terminated strings. */
1543 if (src_len
!= (size_t)-1) {
1544 size_t len
= strnlen_w((const smb_ucs2_t
*)src
,
1546 if (len
< src_len
/2)
1551 * src_len == -1 - alloc interface won't take this
1552 * so we must calculate.
1554 src_len
= (strlen_w((const smb_ucs2_t
*)src
)+1)*sizeof(smb_ucs2_t
);
1556 /* Ensure we don't use an insane length from the client. */
1557 if (src_len
>= 1024*1024) {
1558 smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1562 /* ucs2 is always a multiple of 2 bytes */
1563 if (src_len
!= (size_t)-1) {
1567 dest_len
= convert_string_talloc(ctx
,
1574 if (dest_len
== (size_t)-1) {
1578 if (src_len
== (size_t)-1)
1579 src_len
= dest_len
*2;
1582 /* Did we already process the terminating zero ? */
1583 if (dest
[dest_len
-1] != 0) {
1584 size_t size
= talloc_get_size(dest
);
1585 /* Have we got space to append the '\0' ? */
1586 if (size
<= dest_len
) {
1588 dest
= TALLOC_REALLOC_ARRAY(ctx
, dest
, char,
1592 dest_len
= (size_t)-1;
1597 dest
[dest_len
] = '\0';
1608 size_t pull_ucs2_fstring(char *dest
, const void *src
)
1610 return pull_ucs2(NULL
, dest
, src
, sizeof(fstring
), -1, STR_TERMINATE
);
1614 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1616 * @param dest always set at least to NULL
1618 * @returns The number of bytes occupied by the string in the destination
1621 size_t pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
)
1623 size_t src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1625 return convert_string_talloc(ctx
, CH_UTF16LE
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1629 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1631 * @param dest always set at least to NULL
1633 * @returns The number of bytes occupied by the string in the destination
1636 size_t pull_ucs2_allocate(char **dest
, const smb_ucs2_t
*src
)
1638 size_t dest_len
, src_len
= (strlen_w(src
)+1) * sizeof(smb_ucs2_t
);
1640 if (!convert_string_allocate(NULL
, CH_UTF16LE
, CH_UNIX
, src
, src_len
,
1641 (void **)dest
, &dest_len
, True
))
1648 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1650 * @param dest always set at least to NULL
1652 * @returns The number of bytes occupied by the string in the destination
1655 size_t pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1657 size_t src_len
= strlen(src
)+1;
1659 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1663 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1665 * @param dest always set at least to NULL
1667 * @returns The number of bytes occupied by the string in the destination
1670 size_t pull_utf8_allocate(char **dest
, const char *src
)
1672 size_t dest_len
, src_len
= strlen(src
)+1;
1674 if (!convert_string_allocate(NULL
, CH_UTF8
, CH_UNIX
, src
, src_len
,
1675 (void **)dest
, &dest_len
, True
))
1682 * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1684 * @param dest always set at least to NULL
1686 * @returns The number of bytes occupied by the string in the destination
1689 size_t pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
)
1691 size_t src_len
= strlen(src
)+1;
1693 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, True
);
1697 Copy a string from a char* src to a unicode or ascii
1698 dos codepage destination choosing unicode or ascii based on the
1699 flags in the SMB buffer starting at base_ptr.
1700 Return the number of bytes occupied by the string in the destination.
1702 STR_TERMINATE means include the null termination.
1703 STR_UPPER means uppercase in the destination.
1704 STR_ASCII use ascii even with unicode packet.
1705 STR_NOALIGN means don't do alignment.
1706 dest_len is the maximum length allowed in the destination. If dest_len
1707 is -1 then no maxiumum is used.
1710 size_t push_string_fn(const char *function
, unsigned int line
,
1711 const void *base_ptr
, uint16 flags2
,
1712 void *dest
, const char *src
,
1713 size_t dest_len
, int flags
)
1716 /* We really need to zero fill here, not clobber
1717 * region, as we want to ensure that valgrind thinks
1718 * all of the outgoing buffer has been written to
1719 * so a send() or write() won't trap an error.
1723 clobber_region(function
, line
, dest
, dest_len
);
1725 memset(dest
, '\0', dest_len
);
1729 if (!(flags
& STR_ASCII
) && \
1730 ((flags
& STR_UNICODE
|| \
1731 (flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1732 return push_ucs2(base_ptr
, dest
, src
, dest_len
, flags
);
1734 return push_ascii(dest
, src
, dest_len
, flags
);
1739 Copy a string from a unicode or ascii source (depending on
1740 the packet flags) to a char* destination.
1742 STR_TERMINATE means the string in src is null terminated.
1743 STR_UNICODE means to force as unicode.
1744 STR_ASCII use ascii even with unicode packet.
1745 STR_NOALIGN means don't do alignment.
1746 if STR_TERMINATE is set then src_len is ignored is it is -1
1747 src_len is the length of the source area in bytes.
1748 Return the number of bytes occupied by the string in src.
1749 The resulting string in "dest" is always null terminated.
1752 size_t pull_string_fn(const char *function
,
1754 const void *base_ptr
,
1763 clobber_region(function
, line
, dest
, dest_len
);
1766 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1767 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1771 if (!(flags
& STR_ASCII
) && \
1772 ((flags
& STR_UNICODE
|| \
1773 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1774 return pull_ucs2(base_ptr
, dest
, src
, dest_len
, src_len
, flags
);
1776 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
1780 Copy a string from a unicode or ascii source (depending on
1781 the packet flags) to a char* destination.
1782 Variant that uses talloc.
1784 STR_TERMINATE means the string in src is null terminated.
1785 STR_UNICODE means to force as unicode.
1786 STR_ASCII use ascii even with unicode packet.
1787 STR_NOALIGN means don't do alignment.
1788 if STR_TERMINATE is set then src_len is ignored is it is -1
1789 src_len is the length of the source area in bytes.
1790 Return the number of bytes occupied by the string in src.
1791 The resulting string in "dest" is always null terminated.
1794 size_t pull_string_talloc_fn(const char *function
,
1797 const void *base_ptr
,
1804 if ((base_ptr
== NULL
) && ((flags
& (STR_ASCII
|STR_UNICODE
)) == 0)) {
1805 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1809 if (!(flags
& STR_ASCII
) && \
1810 ((flags
& STR_UNICODE
|| \
1811 (smb_flags2
& FLAGS2_UNICODE_STRINGS
)))) {
1812 return pull_ucs2_base_talloc(ctx
,
1819 return pull_ascii_base_talloc(ctx
,
1827 size_t align_string(const void *base_ptr
, const char *p
, int flags
)
1829 if (!(flags
& STR_ASCII
) && \
1830 ((flags
& STR_UNICODE
|| \
1831 (SVAL(base_ptr
, smb_flg2
) & FLAGS2_UNICODE_STRINGS
)))) {
1832 return ucs2_align(base_ptr
, p
, flags
);
1838 Return the unicode codepoint for the next multi-byte CH_UNIX character
1839 in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1841 Also return the number of bytes consumed (which tells the caller
1842 how many bytes to skip to get to the next CH_UNIX character).
1844 Return INVALID_CODEPOINT if the next character cannot be converted.
1847 codepoint_t
next_codepoint(const char *str
, size_t *size
)
1849 /* It cannot occupy more than 4 bytes in UTF16 format */
1851 smb_iconv_t descriptor
;
1857 if ((str
[0] & 0x80) == 0) {
1859 return (codepoint_t
)str
[0];
1862 /* We assume that no multi-byte character can take
1863 more than 5 bytes. This is OK as we only
1864 support codepoints up to 1M */
1866 ilen_orig
= strnlen(str
, 5);
1869 lazy_initialize_conv();
1871 descriptor
= conv_handles
[CH_UNIX
][CH_UTF16LE
];
1872 if (descriptor
== (smb_iconv_t
)-1 || descriptor
== (smb_iconv_t
)0) {
1874 return INVALID_CODEPOINT
;
1877 /* This looks a little strange, but it is needed to cope
1878 with codepoints above 64k which are encoded as per RFC2781. */
1880 outbuf
= (char *)buf
;
1881 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1883 /* We failed to convert to a 2 byte character.
1884 See if we can convert to a 4 UTF16-LE byte char encoding.
1887 outbuf
= (char *)buf
;
1888 smb_iconv(descriptor
, &str
, &ilen
, &outbuf
, &olen
);
1890 /* We didn't convert any bytes */
1892 return INVALID_CODEPOINT
;
1899 *size
= ilen_orig
- ilen
;
1902 /* 2 byte, UTF16-LE encoded value. */
1903 return (codepoint_t
)SVAL(buf
, 0);
1906 /* Decode a 4 byte UTF16-LE character manually.
1907 See RFC2871 for the encoding machanism.
1909 codepoint_t w1
= SVAL(buf
,0) & ~0xD800;
1910 codepoint_t w2
= SVAL(buf
,2) & ~0xDC00;
1912 return (codepoint_t
)0x10000 +
1916 /* no other length is valid */
1917 return INVALID_CODEPOINT
;