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.
29 * @brief Character-set conversion routines built on our iconv.
31 * @note Samba's internal character set (at least in the 3.0 series)
32 * is always the same as the one for the Unix filesystem. It is
33 * <b>not</b> necessarily UTF-8 and may be different on machines that
34 * need i18n filenames to be compatible with Unix software. It does
35 * have to be a superset of ASCII. All multibyte sequences must start
36 * with a byte with the high bit set.
42 static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
46 * Return the name of a charset to give to iconv().
48 static const char *charset_name(charset_t ch)
50 const char *ret = NULL;
52 if (ch == CH_UCS2) ret = "UCS-2LE";
53 else if (ch == CH_UNIX) ret = lp_unix_charset();
54 else if (ch == CH_DOS) ret = lp_dos_charset();
55 else if (ch == CH_DISPLAY) ret = lp_display_charset();
56 else if (ch == CH_UTF8) ret = "UTF8";
58 if (!ret || !*ret) ret = "ASCII";
62 void lazy_initialize_conv(void)
64 static int initialized = False;
74 * Initialize iconv conversion descriptors.
76 * This is called the first time it is needed, and also called again
77 * every time the configuration is reloaded, because the charset or
78 * codepage might have changed.
83 BOOL did_reload = False;
85 /* so that charset_name() works we need to get the UNIX<->UCS2 going
87 if (!conv_handles[CH_UNIX][CH_UCS2])
88 conv_handles[CH_UNIX][CH_UCS2] = smb_iconv_open("UCS-2LE", "ASCII");
90 if (!conv_handles[CH_UCS2][CH_UNIX])
91 conv_handles[CH_UCS2][CH_UNIX] = smb_iconv_open("ASCII", "UCS-2LE");
93 for (c1=0;c1<NUM_CHARSETS;c1++) {
94 for (c2=0;c2<NUM_CHARSETS;c2++) {
95 const char *n1 = charset_name((charset_t)c1);
96 const char *n2 = charset_name((charset_t)c2);
97 if (conv_handles[c1][c2] &&
98 strcmp(n1, conv_handles[c1][c2]->from_name) == 0 &&
99 strcmp(n2, conv_handles[c1][c2]->to_name) == 0)
104 if (conv_handles[c1][c2])
105 smb_iconv_close(conv_handles[c1][c2]);
107 conv_handles[c1][c2] = smb_iconv_open(n2,n1);
108 if (conv_handles[c1][c2] == (smb_iconv_t)-1) {
109 DEBUG(0,("Conversion from %s to %s not supported\n",
110 charset_name((charset_t)c1), charset_name((charset_t)c2)));
111 conv_handles[c1][c2] = NULL;
117 /* XXX: Does this really get called every time the dos
118 * codepage changes? */
119 /* XXX: Is the did_reload test too strict? */
120 init_doschar_table();
126 * Convert string from one encoding to another, making error checking etc
128 * @param src pointer to source string (multibyte or singlebyte)
129 * @param srclen length of the source string in bytes
130 * @param dest pointer to destination string (multibyte or singlebyte)
131 * @param destlen maximal length allowed for string
132 * @returns the number of bytes occupied in the destination
134 size_t convert_string(charset_t from, charset_t to,
135 void const *src, size_t srclen,
136 void *dest, size_t destlen)
140 const char* inbuf = (const char*)src;
141 char* outbuf = (char*)dest;
142 smb_iconv_t descriptor;
144 if (srclen == (size_t)-1)
145 srclen = strlen(src)+1;
147 lazy_initialize_conv();
149 descriptor = conv_handles[from][to];
151 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
152 /* conversion not supported, use as is */
153 size_t len = MIN(srclen,destlen);
154 memcpy(dest,src,len);
160 retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
161 if(retval==(size_t)-1) {
162 const char *reason="unknown error";
165 reason="Incomplete multibyte sequence";
168 reason="No more room";
169 DEBUG(0, ("convert_string: Required %d, available %d\n",
171 /* we are not sure we need srclen bytes,
172 may be more, may be less.
173 We only know we need more than destlen
177 reason="Illegal multibyte sequence";
180 /* smb_panic(reason); */
182 return destlen-o_len;
186 * Convert between character sets, allocating a new buffer for the result.
188 * @param srclen length of source buffer.
189 * @param dest always set at least to NULL
190 * @note -1 is not accepted for srclen.
192 * @returns Size in bytes of the converted string; or -1 in case of error.
195 size_t convert_string_allocate(charset_t from, charset_t to,
196 void const *src, size_t srclen, void **dest)
198 size_t i_len, o_len, destlen;
200 const char *inbuf = (const char *)src;
202 smb_iconv_t descriptor;
206 if (src == NULL || srclen == (size_t)-1)
209 lazy_initialize_conv();
211 descriptor = conv_handles[from][to];
213 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
214 /* conversion not supported, return -1*/
215 DEBUG(3, ("convert_string_allocate: conversion not supported!\n"));
219 destlen = MAX(srclen, 512);
222 destlen = destlen * 2;
223 ob = (char *)Realloc(outbuf, destlen);
225 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
233 retval = smb_iconv(descriptor,
236 if(retval == (size_t)-1) {
237 const char *reason="unknown error";
240 reason="Incomplete multibyte sequence";
245 reason="Illegal multibyte sequence";
248 DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
249 /* smb_panic(reason); */
253 destlen = destlen - o_len;
254 *dest = (char *)Realloc(ob,destlen);
256 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
266 * Convert between character sets, allocating a new buffer using talloc for the result.
268 * @param srclen length of source buffer.
269 * @param dest always set at least to NULL
270 * @note -1 is not accepted for srclen.
272 * @returns Size in bytes of the converted string; or -1 in case of error.
274 static size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
275 void const *src, size_t srclen, void **dest)
277 void *alloced_string;
280 /* FIXME: Ridiculous to allocate two buffers and then copy the string! */
283 dest_len=convert_string_allocate(from, to, src, srclen, &alloced_string);
284 if (dest_len == (size_t)-1)
286 *dest = talloc_memdup(ctx, alloced_string, dest_len);
287 SAFE_FREE(alloced_string);
293 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
298 size = convert_string_allocate(CH_UNIX, CH_UCS2, src, srclen,
301 smb_panic("failed to create UCS2 buffer");
303 if (!strupper_w(buffer) && (dest == src)) {
308 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen);
313 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
318 size = convert_string_allocate(CH_UNIX, CH_UCS2, src, srclen,
321 smb_panic("failed to create UCS2 buffer");
323 if (!strlower_w(buffer) && (dest == src)) {
327 size = convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen);
333 static size_t ucs2_align(const void *base_ptr, const void *p, int flags)
335 if (flags & (STR_NOALIGN|STR_ASCII))
337 return PTR_DIFF(p, base_ptr) & 1;
342 * Copy a string from a char* unix src to a dos codepage string destination.
344 * @return the number of bytes occupied by the string in the destination.
346 * @param flags can include
348 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
349 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
352 * @param dest_len the maximum length in bytes allowed in the
353 * destination. If @p dest_len is -1 then no maximum is used.
355 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
357 size_t src_len = strlen(src);
360 /* treat a pstring as "unlimited" length */
361 if (dest_len == (size_t)-1)
362 dest_len = sizeof(pstring);
364 if (flags & STR_UPPER) {
365 pstrcpy(tmpbuf, src);
370 if (flags & STR_TERMINATE)
373 return convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len);
376 size_t push_ascii_fstring(void *dest, const char *src)
378 return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
381 size_t push_ascii_pstring(void *dest, const char *src)
383 return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
387 * Copy a string from a dos codepage source to a unix char* destination.
389 * The resulting string in "dest" is always null terminated.
391 * @param flags can have:
393 * <dt>STR_TERMINATE</dt>
394 * <dd>STR_TERMINATE means the string in @p src
395 * is null terminated, and src_len is ignored.</dd>
398 * @param src_len is the length of the source area in bytes.
399 * @returns the number of bytes occupied by the string in @p src.
401 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
405 if (dest_len == (size_t)-1)
406 dest_len = sizeof(pstring);
408 if (flags & STR_TERMINATE) {
409 if (src_len == (size_t)-1) {
410 src_len = strlen(src) + 1;
412 size_t len = strnlen(src, src_len);
419 ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len);
422 dest[MIN(ret, dest_len-1)] = 0;
429 size_t pull_ascii_pstring(char *dest, const void *src)
431 return pull_ascii(dest, src, sizeof(pstring), -1, STR_TERMINATE);
434 size_t pull_ascii_fstring(char *dest, const void *src)
436 return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
440 * Copy a string from a char* src to a unicode destination.
442 * @returns the number of bytes occupied by the string in the destination.
444 * @param flags can have:
447 * <dt>STR_TERMINATE <dd>means include the null termination.
448 * <dt>STR_UPPER <dd>means uppercase in the destination.
449 * <dt>STR_NOALIGN <dd>means don't do alignment.
452 * @param dest_len is the maximum length allowed in the
453 * destination. If dest_len is -1 then no maxiumum is used.
455 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
458 size_t src_len = strlen(src);
461 /* treat a pstring as "unlimited" length */
462 if (dest_len == (size_t)-1)
463 dest_len = sizeof(pstring);
465 if (flags & STR_UPPER) {
466 pstrcpy(tmpbuf, src);
471 if (flags & STR_TERMINATE)
474 if (ucs2_align(base_ptr, dest, flags)) {
476 dest = (void *)((char *)dest + 1);
477 if (dest_len) dest_len--;
481 /* ucs2 is always a multiple of 2 bytes */
484 len += convert_string(CH_UNIX, CH_UCS2, src, src_len, dest, dest_len);
490 * Copy a string from a unix char* src to a UCS2 destination,
491 * allocating a buffer using talloc().
493 * @param dest always set at least to NULL
495 * @returns The number of bytes occupied by the string in the destination
496 * or -1 in case of error.
498 size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
500 size_t src_len = strlen(src)+1;
503 return convert_string_talloc(ctx, CH_UNIX, CH_UCS2, src, src_len, (void **)dest);
508 * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
510 * @param dest always set at least to NULL
512 * @returns The number of bytes occupied by the string in the destination
513 * or -1 in case of error.
516 size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
518 size_t src_len = strlen(src)+1;
521 return convert_string_allocate(CH_UNIX, CH_UCS2, src, src_len, (void **)dest);
525 Copy a string from a char* src to a UTF-8 destination.
526 Return the number of bytes occupied by the string in the destination
528 STR_TERMINATE means include the null termination
529 STR_UPPER means uppercase in the destination
530 dest_len is the maximum length allowed in the destination. If dest_len
531 is -1 then no maxiumum is used.
534 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
536 size_t src_len = strlen(src);
539 /* treat a pstring as "unlimited" length */
540 if (dest_len == (size_t)-1)
541 dest_len = sizeof(pstring);
543 if (flags & STR_UPPER) {
544 pstrcpy(tmpbuf, src);
549 if (flags & STR_TERMINATE)
552 return convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len);
555 size_t push_utf8_fstring(void *dest, const char *src)
557 return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
561 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
563 * @param dest always set at least to NULL
565 * @returns The number of bytes occupied by the string in the destination
568 size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
570 size_t src_len = strlen(src)+1;
573 return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest);
577 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
579 * @param dest always set at least to NULL
581 * @returns The number of bytes occupied by the string in the destination
584 size_t push_utf8_allocate(char **dest, const char *src)
586 size_t src_len = strlen(src)+1;
589 return convert_string_allocate(CH_UNIX, CH_UTF8, src, src_len, (void **)dest);
593 Copy a string from a ucs2 source to a unix char* destination.
595 STR_TERMINATE means the string in src is null terminated.
596 STR_NOALIGN means don't try to align.
597 if STR_TERMINATE is set then src_len is ignored if it is -1.
598 src_len is the length of the source area in bytes
599 Return the number of bytes occupied by the string in src.
600 The resulting string in "dest" is always null terminated.
603 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
607 if (dest_len == (size_t)-1)
608 dest_len = sizeof(pstring);
610 if (ucs2_align(base_ptr, src, flags)) {
611 src = (const void *)((const char *)src + 1);
616 if (flags & STR_TERMINATE) {
617 if (src_len == (size_t)-1) {
618 src_len = strlen_w(src)*2 + 2;
620 size_t len = strnlen_w(src, src_len/2);
627 /* ucs2 is always a multiple of 2 bytes */
628 if (src_len != (size_t)-1)
631 ret = convert_string(CH_UCS2, CH_UNIX, src, src_len, dest, dest_len);
633 dest[MIN(ret, dest_len-1)] = 0;
640 size_t pull_ucs2_pstring(char *dest, const void *src)
642 return pull_ucs2(NULL, dest, src, sizeof(pstring), -1, STR_TERMINATE);
645 size_t pull_ucs2_fstring(char *dest, const void *src)
647 return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
651 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
653 * @param dest always set at least to NULL
655 * @returns The number of bytes occupied by the string in the destination
658 size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
660 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
662 return convert_string_talloc(ctx, CH_UCS2, CH_UNIX, src, src_len, (void **)dest);
666 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
668 * @param dest always set at least to NULL
670 * @returns The number of bytes occupied by the string in the destination
673 size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
675 size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
677 return convert_string_allocate(CH_UCS2, CH_UNIX, src, src_len, (void **)dest);
681 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
683 * @param dest always set at least to NULL
685 * @returns The number of bytes occupied by the string in the destination
688 size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
690 size_t src_len = strlen(src)+1;
692 return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest);
696 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
698 * @param dest always set at least to NULL
700 * @returns The number of bytes occupied by the string in the destination
703 size_t pull_utf8_allocate(void **dest, const char *src)
705 size_t src_len = strlen(src)+1;
707 return convert_string_allocate(CH_UTF8, CH_UNIX, src, src_len, dest);
711 Copy a string from a char* src to a unicode or ascii
712 dos codepage destination choosing unicode or ascii based on the
713 flags in the SMB buffer starting at base_ptr.
714 Return the number of bytes occupied by the string in the destination.
716 STR_TERMINATE means include the null termination.
717 STR_UPPER means uppercase in the destination.
718 STR_ASCII use ascii even with unicode packet.
719 STR_NOALIGN means don't do alignment.
720 dest_len is the maximum length allowed in the destination. If dest_len
721 is -1 then no maxiumum is used.
724 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)
726 if (dest_len != (size_t)-1)
727 clobber_region(function, line, dest, dest_len);
729 if (!(flags & STR_ASCII) && \
730 ((flags & STR_UNICODE || \
731 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
732 return push_ucs2(base_ptr, dest, src, dest_len, flags);
734 return push_ascii(dest, src, dest_len, flags);
739 Copy a string from a unicode or ascii source (depending on
740 the packet flags) to a char* destination.
742 STR_TERMINATE means the string in src is null terminated.
743 STR_UNICODE means to force as unicode.
744 STR_ASCII use ascii even with unicode packet.
745 STR_NOALIGN means don't do alignment.
746 if STR_TERMINATE is set then src_len is ignored is it is -1
747 src_len is the length of the source area in bytes.
748 Return the number of bytes occupied by the string in src.
749 The resulting string in "dest" is always null terminated.
752 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)
754 if (dest_len != (size_t)-1)
755 clobber_region(function, line, dest, dest_len);
757 if (!(flags & STR_ASCII) && \
758 ((flags & STR_UNICODE || \
759 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
760 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
762 return pull_ascii(dest, src, dest_len, src_len, flags);
765 size_t align_string(const void *base_ptr, const char *p, int flags)
767 if (!(flags & STR_ASCII) && \
768 ((flags & STR_UNICODE || \
769 (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
770 return ucs2_align(base_ptr, p, flags);
776 Convert from unix to ucs2 charset and return the
777 allocated and converted string or NULL if an error occurred.
778 You must provide a zero terminated string.
779 The returning string will be zero terminated.
782 smb_ucs2_t *acnv_uxu2(const char *src)
788 slen = strlen(src) + 1;
789 dlen = convert_string_allocate(CH_UNIX, CH_UCS2, src, slen, &dest);
790 if (dlen == (size_t)-1)
797 Convert from dos to ucs2 charset and return the
798 allocated and converted string or NULL if an error occurred.
799 You must provide a zero terminated string.
800 The returning string will be zero terminated.
803 smb_ucs2_t *acnv_dosu2(const char *src)
809 slen = strlen(src) + 1;
810 dlen = convert_string_allocate(CH_DOS, CH_UCS2, src, slen, &dest);
811 if (dlen == (size_t)-1)