vfs_shadow_copy2: Use VFS interface to derive mount point
[samba4-gss.git] / lib / util / charset / convert_string.c
blobb15273ce2bb56e4b936d4d1c71f720622f67eddb
1 /*
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-2011
6 Copyright (C) Andrew Bartlett 2011
7 Copyright (C) Simo Sorce 2001
8 Copyright (C) Martin Pool 2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "replace.h"
25 #include "system/iconv.h"
26 #include "charset.h"
27 #include "lib/util/debug.h"
28 #include "lib/util/fault.h"
30 /**
31 * @file
33 * @brief Character-set conversion routines built on our iconv.
35 * @note Samba's internal character set (at least in the 3.0 series)
36 * is always the same as the one for the Unix filesystem. It is
37 * <b>not</b> necessarily UTF-8 and may be different on machines that
38 * need i18n filenames to be compatible with Unix software. It does
39 * have to be a superset of ASCII. All multibyte sequences must start
40 * with a byte with the high bit set.
42 * @sa lib/iconv.c
46 /**
47 * Convert string from one encoding to another, making error checking etc
48 * Slow path version - uses (slow) iconv.
50 * @param src pointer to source string (multibyte or singlebyte)
51 * @param srclen length of the source string in bytes
52 * @param dest pointer to destination string (multibyte or singlebyte)
53 * @param destlen maximal length allowed for string
54 * @param converted size is the number of bytes occupied in the destination
56 * @returns false and sets errno on fail, true on success.
58 * Ensure the srclen contains the terminating zero.
60 **/
62 static bool convert_string_internal(struct smb_iconv_handle *ic,
63 charset_t from, charset_t to,
64 void const *src, size_t srclen,
65 void *dest, size_t destlen, size_t *converted_size)
67 size_t i_len, o_len;
68 size_t retval;
69 const char* inbuf = (const char*)src;
70 char* outbuf = (char*)dest;
71 smb_iconv_t descriptor;
73 descriptor = get_conv_handle(ic, from, to);
75 if (srclen == (size_t)-1) {
76 if (from == CH_UTF16LE || from == CH_UTF16BE) {
77 srclen = (strlen_w((const smb_ucs2_t *)src)+1) * 2;
78 } else {
79 srclen = strlen((const char *)src)+1;
84 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
85 errno = EINVAL;
86 return false;
89 i_len=srclen;
90 o_len=destlen;
92 retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
93 *converted_size = destlen-o_len;
95 return (retval != (size_t)-1);
98 /**
99 * Convert string from one encoding to another, making error checking etc
100 * Fast path version - handles ASCII first.
102 * @param src pointer to source string (multibyte or singlebyte)
103 * @param srclen length of the source string in bytes, or -1 for nul terminated.
104 * @param dest pointer to destination string (multibyte or singlebyte)
105 * @param destlen maximal length allowed for string - *NEVER* -1.
106 * @param converted size is the number of bytes occupied in the destination
108 * @returns false and sets errno on fail, true on success.
110 * Ensure the srclen contains the terminating zero.
112 * This function has been hand-tuned to provide a fast path.
113 * Don't change unless you really know what you are doing. JRA.
116 bool convert_string_error_handle(struct smb_iconv_handle *ic,
117 charset_t from, charset_t to,
118 void const *src, size_t srclen,
119 void *dest, size_t destlen,
120 size_t *converted_size)
123 * NB. We deliberately don't do a strlen here if srclen == -1.
124 * This is very expensive over millions of calls and is taken
125 * care of in the slow path in convert_string_internal. JRA.
128 #ifdef DEVELOPER
129 SMB_ASSERT(destlen != (size_t)-1);
130 #endif
132 if (srclen == 0) {
133 *converted_size = 0;
134 return true;
137 if (from != CH_UTF16LE && from != CH_UTF16BE && to != CH_UTF16LE && to != CH_UTF16BE) {
138 const unsigned char *p = (const unsigned char *)src;
139 unsigned char *q = (unsigned char *)dest;
140 size_t slen = srclen;
141 size_t dlen = destlen;
142 unsigned char lastp = '\0';
143 size_t retval = 0;
145 /* If all characters are ascii, fast path here. */
146 while (slen && dlen) {
147 if ((lastp = *p) <= 0x7f) {
148 *q++ = *p++;
149 if (slen != (size_t)-1) {
150 slen--;
152 dlen--;
153 retval++;
154 if (!lastp)
155 break;
156 } else {
157 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
158 goto general_case;
159 #else
160 bool ret = convert_string_internal(ic, from, to, p, slen, q, dlen, converted_size);
161 *converted_size += retval;
162 return ret;
163 #endif
167 *converted_size = retval;
169 if (!dlen) {
170 /* Even if we fast path we should note if we ran out of room. */
171 if (((slen != (size_t)-1) && slen) ||
172 ((slen == (size_t)-1) && lastp)) {
173 errno = E2BIG;
174 return false;
177 return true;
178 } else if (from == CH_UTF16LE && to != CH_UTF16LE) {
179 const unsigned char *p = (const unsigned char *)src;
180 unsigned char *q = (unsigned char *)dest;
181 size_t retval = 0;
182 size_t slen = srclen;
183 size_t dlen = destlen;
184 unsigned char lastp = '\0';
185 #ifndef BROKEN_UNICODE_COMPOSE_CHARACTERS
186 bool ret;
187 #endif
189 if (slen == (size_t)-1) {
190 while (dlen &&
191 ((lastp = *p) <= 0x7f) && (p[1] == 0)) {
192 *q++ = *p;
193 p += 2;
194 dlen--;
195 retval++;
196 if (!lastp)
197 break;
199 if (lastp != 0) goto slow_path;
200 } else {
201 while (slen >= 2 && dlen &&
202 (*p <= 0x7f) && (p[1] == 0)) {
203 *q++ = *p;
204 slen -= 2;
205 p += 2;
206 dlen--;
207 retval++;
209 if (slen != 0) goto slow_path;
212 *converted_size = retval;
213 return true;
215 slow_path:
216 /* come here when we hit a character we can't deal
217 * with in the fast path
219 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
220 goto general_case;
221 #else
222 ret = convert_string_internal(ic, from, to, p, slen, q, dlen, converted_size);
223 *converted_size += retval;
224 return ret;
225 #endif
227 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
228 const unsigned char *p = (const unsigned char *)src;
229 unsigned char *q = (unsigned char *)dest;
230 size_t retval = 0;
231 size_t slen = srclen;
232 size_t dlen = destlen;
233 unsigned char lastp = '\0';
235 /* If all characters are ascii, fast path here. */
236 while (slen && (dlen >= 1)) {
237 if (dlen >=2 && (lastp = *p) <= 0x7F) {
238 *q++ = *p++;
239 *q++ = '\0';
240 if (slen != (size_t)-1) {
241 slen--;
243 dlen -= 2;
244 retval += 2;
245 if (!lastp)
246 break;
247 } else {
248 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
249 goto general_case;
250 #else
251 bool ret = convert_string_internal(ic, from, to, p, slen, q, dlen, converted_size);
252 *converted_size += retval;
253 return ret;
254 #endif
258 *converted_size = retval;
260 if (!dlen) {
261 /* Even if we fast path we should note if we ran out of room. */
262 if (((slen != (size_t)-1) && slen) ||
263 ((slen == (size_t)-1) && lastp)) {
264 errno = E2BIG;
265 return false;
268 return true;
271 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
272 general_case:
273 #endif
274 return convert_string_internal(ic, from, to, src, srclen, dest, destlen, converted_size);
277 bool convert_string_handle(struct smb_iconv_handle *ic,
278 charset_t from, charset_t to,
279 void const *src, size_t srclen,
280 void *dest, size_t destlen,
281 size_t *converted_size)
283 bool ret = convert_string_error_handle(ic, from, to, src, srclen, dest, destlen, converted_size);
285 if(ret==false) {
286 const char *reason="unknown error";
287 switch(errno) {
288 case EINVAL:
289 reason="Incomplete multibyte sequence";
290 DBG_NOTICE("Conversion error: %s\n",
291 reason);
292 break;
293 case E2BIG:
295 reason="No more room";
296 if (from == CH_UNIX) {
297 DBG_NOTICE("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u error: %s\n",
298 charset_name(ic, from), charset_name(ic, to),
299 (unsigned int)srclen, (unsigned int)destlen, reason);
300 } else {
301 DBG_NOTICE("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u error: %s\n",
302 charset_name(ic, from), charset_name(ic, to),
303 (unsigned int)srclen, (unsigned int)destlen, reason);
305 break;
307 case EILSEQ:
308 reason="Illegal multibyte sequence";
309 DBG_NOTICE("convert_string_internal: Conversion error: %s\n",
310 reason);
311 break;
312 default:
313 DBG_ERR("convert_string_internal: Conversion error: %s\n",
314 reason);
315 break;
317 /* smb_panic(reason); */
319 return ret;
324 * Convert between character sets, allocating a new buffer using talloc for the result.
326 * @param srclen length of source buffer.
327 * @param dest always set at least to NULL
328 * @param converted_size set to the number of bytes occupied by the string in
329 * the destination on success.
330 * @note -1 is not accepted for srclen.
332 * @return true if new buffer was correctly allocated, and string was
333 * converted.
335 * Ensure the srclen contains the terminating zero.
337 bool convert_string_talloc_handle(TALLOC_CTX *ctx, struct smb_iconv_handle *ic,
338 charset_t from, charset_t to,
339 void const *src, size_t srclen, void *dst,
340 size_t *converted_size)
343 size_t i_len, o_len, destlen;
344 size_t retval;
345 const char *inbuf = NULL;
346 char *outbuf = NULL, *ob = NULL;
347 smb_iconv_t descriptor;
348 void **dest = dst;
350 *dest = NULL;
351 if (converted_size != NULL) {
352 *converted_size = 0;
355 if (src == NULL || srclen == (size_t)-1) {
356 errno = EINVAL;
357 return false;
360 if (srclen == 0) {
361 /* We really should treat this as an error, but
362 there are too many callers that need this to
363 return a NULL terminated string in the correct
364 character set. */
365 if (to == CH_UTF16LE|| to == CH_UTF16BE || to == CH_UTF16MUNGED) {
366 destlen = 2;
367 } else {
368 destlen = 1;
370 ob = talloc_zero_array(ctx, char, destlen);
371 if (ob == NULL) {
372 DBG_ERR("Could not talloc destination buffer.\n");
373 errno = ENOMEM;
374 return false;
376 if (converted_size != NULL) {
377 *converted_size = destlen;
379 *dest = ob;
380 return true;
383 descriptor = get_conv_handle(ic, from, to);
385 if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
386 DEBUG(0,("convert_string_talloc: Conversion not supported.\n"));
387 errno = EOPNOTSUPP;
388 return false;
391 if (srclen >= (SIZE_MAX - 2) / 3) {
392 DBG_ERR("convert_string_talloc: "
393 "srclen is %zu, destlen would wrap!\n",
394 srclen);
395 errno = EOPNOTSUPP;
396 return false;
398 destlen = srclen * 3;
400 /* +2 is for ucs2 null termination. */
401 ob = talloc_realloc(ctx, ob, char, destlen + 2);
403 if (!ob) {
404 DEBUG(0, ("convert_string_talloc: realloc failed!\n"));
405 errno = ENOMEM;
406 return false;
408 outbuf = ob;
409 i_len = srclen;
410 o_len = destlen;
411 inbuf = (const char *)src;
413 retval = smb_iconv(descriptor,
414 &inbuf, &i_len,
415 &outbuf, &o_len);
416 if(retval == (size_t)-1) {
417 const char *reason="unknown error";
418 switch(errno) {
419 case EINVAL:
420 reason="Incomplete multibyte sequence";
421 DBG_NOTICE("Conversion error: %s\n",
422 reason);
423 break;
424 case E2BIG:
425 reason = "output buffer is too small";
426 DBG_ERR("Conversion error: %s\n",
427 reason);
428 break;
429 case EILSEQ:
430 reason="Illegal multibyte sequence";
431 DBG_NOTICE("Conversion error: %s\n",
432 reason);
433 break;
434 default:
435 DBG_ERR("Conversion error: %s\n",
436 reason);
437 break;
439 /* smb_panic(reason); */
440 TALLOC_FREE(ob);
441 return false;
444 destlen = destlen - o_len;
445 /* Don't shrink unless we're reclaiming a lot of
446 * space. This is in the hot codepath and these
447 * reallocs *cost*. JRA.
449 if (o_len > 1024) {
450 /* We're shrinking here so we know the +2 is safe from wrap. */
451 ob = talloc_realloc(ctx,ob, char, destlen + 2);
454 if (destlen && !ob) {
455 DEBUG(0, ("convert_string_talloc: out of memory!\n"));
456 errno = ENOMEM;
457 return false;
460 *dest = ob;
462 /* Must ucs2 null terminate in the extra space we allocated. */
463 ob[destlen] = '\0';
464 ob[destlen+1] = '\0';
466 /* Ensure we can never return a *converted_size of zero. */
467 if (destlen == 0) {
468 /* As we're now returning false on a bad smb_iconv call,
469 this should never happen. But be safe anyway. */
470 if (to == CH_UTF16LE|| to == CH_UTF16BE || to == CH_UTF16MUNGED) {
471 destlen = 2;
472 } else {
473 destlen = 1;
477 if (converted_size != NULL) {
478 *converted_size = destlen;
480 return true;
484 * Convert string from one encoding to another, with error checking.
485 * This version produces more logging information than
486 * convert_string_error(), but is otherwise functionally identical.
488 * @param src pointer to source string (multibyte or singlebyte)
489 * @param srclen length of the source string in bytes
490 * @param dest pointer to destination string (multibyte or singlebyte)
491 * @param destlen maximal length allowed for string
492 * @param converted_size the number of bytes occupied in the destination
494 * @returns true on success, false on fail.
496 _PUBLIC_ bool convert_string(charset_t from, charset_t to,
497 void const *src, size_t srclen,
498 void *dest, size_t destlen,
499 size_t *converted_size)
501 return convert_string_handle(get_iconv_handle(), from, to,
502 src, srclen,
503 dest, destlen, converted_size);
507 * Convert string from one encoding to another, with error checking.
508 * This version is less verbose than convert_string().
510 * @param src pointer to source string (multibyte or singlebyte)
511 * @param srclen length of the source string in bytes
512 * @param dest pointer to destination string (multibyte or singlebyte)
513 * @param destlen maximal length allowed for string
514 * @param converted_size the number of bytes occupied in the destination
516 * @returns true on success, false on fail.
518 _PUBLIC_ bool convert_string_error(charset_t from, charset_t to,
519 void const *src, size_t srclen,
520 void *dest, size_t destlen,
521 size_t *converted_size)
523 return convert_string_error_handle(get_iconv_handle(), from, to,
524 src, srclen,
525 dest, destlen, converted_size);
529 * Convert between character sets, allocating a new buffer using talloc for the result.
531 * @param srclen length of source buffer.
532 * @param dest always set at least to NULL
533 * @param converted_size Size in bytes of the converted string
534 * @note -1 is not accepted for srclen.
536 * @returns boolean indication whether the conversion succeeded
539 _PUBLIC_ bool convert_string_talloc(TALLOC_CTX *ctx,
540 charset_t from, charset_t to,
541 void const *src, size_t srclen,
542 void *dest, size_t *converted_size)
544 return convert_string_talloc_handle(ctx, get_iconv_handle(),
545 from, to, src, srclen, dest,
546 converted_size);