r25184: Fix some C++ warnings and an uninitialized variable
[amitay/samba.git] / source3 / lib / charcnv.c
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
6    Copyright (C) Simo Sorce 2001
7    Copyright (C) Martin Pool 2003
8
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.
13
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.
18
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/>.
21
22 */
23 #include "includes.h"
24
25 /* We can parameterize this if someone complains.... JRA. */
26
27 char lp_failed_convert_char(void)
28 {
29         return '_';
30 }
31
32 /**
33  * @file
34  *
35  * @brief Character-set conversion routines built on our iconv.
36  *
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.
43  *
44  * @sa lib/iconv.c
45  */
46
47
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
51 /**
52  * Return the name of a charset to give to iconv().
53  **/
54 static const char *charset_name(charset_t ch)
55 {
56         const char *ret = NULL;
57
58         if (ch == CH_UTF16LE) ret = "UTF-16LE";
59         else if (ch == CH_UTF16BE) ret = "UTF-16BE";
60         else if (ch == CH_UNIX) ret = lp_unix_charset();
61         else if (ch == CH_DOS) ret = lp_dos_charset();
62         else if (ch == CH_DISPLAY) ret = lp_display_charset();
63         else if (ch == CH_UTF8) ret = "UTF8";
64
65 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
66         if (ret && !strcmp(ret, "LOCALE")) {
67                 const char *ln = NULL;
68
69 #ifdef HAVE_SETLOCALE
70                 setlocale(LC_ALL, "");
71 #endif
72                 ln = nl_langinfo(CODESET);
73                 if (ln) {
74                         /* Check whether the charset name is supported
75                            by iconv */
76                         smb_iconv_t handle = smb_iconv_open(ln,"UCS-2LE");
77                         if (handle == (smb_iconv_t) -1) {
78                                 DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln));
79                                 ln = NULL;
80                         } else {
81                                 DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln));
82                                 smb_iconv_close(handle);
83                         }
84                 }
85                 ret = ln;
86         }
87 #endif
88
89         if (!ret || !*ret) ret = "ASCII";
90         return ret;
91 }
92
93 void lazy_initialize_conv(void)
94 {
95         static int initialized = False;
96
97         if (!initialized) {
98                 initialized = True;
99                 load_case_tables();
100                 init_iconv();
101         }
102 }
103
104 /**
105  * Destroy global objects allocated by init_iconv()
106  **/
107 void gfree_charcnv(void)
108 {
109         int c1, c2;
110
111         for (c1=0;c1<NUM_CHARSETS;c1++) {
112                 for (c2=0;c2<NUM_CHARSETS;c2++) {
113                         if ( conv_handles[c1][c2] ) {
114                                 smb_iconv_close( conv_handles[c1][c2] );
115                                 conv_handles[c1][c2] = 0;
116                         }
117                 }
118         }
119 }
120
121 /**
122  * Initialize iconv conversion descriptors.
123  *
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.
127  **/
128 void init_iconv(void)
129 {
130         int c1, c2;
131         BOOL did_reload = False;
132
133         /* so that charset_name() works we need to get the UNIX<->UCS2 going
134            first */
135         if (!conv_handles[CH_UNIX][CH_UTF16LE])
136                 conv_handles[CH_UNIX][CH_UTF16LE] = smb_iconv_open(charset_name(CH_UTF16LE), "ASCII");
137
138         if (!conv_handles[CH_UTF16LE][CH_UNIX])
139                 conv_handles[CH_UTF16LE][CH_UNIX] = smb_iconv_open("ASCII", charset_name(CH_UTF16LE));
140
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)
148                                 continue;
149
150                         did_reload = True;
151
152                         if (conv_handles[c1][c2])
153                                 smb_iconv_close(conv_handles[c1][c2]);
154
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) {
160                                         n1 = "ASCII";
161                                 }
162                                 if (c2 != CH_UTF16LE && c2 != CH_UTF16BE) {
163                                         n2 = "ASCII";
164                                 }
165                                 DEBUG(0,("init_iconv: Attempting to replace with conversion from %s to %s\n",
166                                         n1, n2 ));
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");
171                                 }
172                         }
173                 }
174         }
175
176         if (did_reload) {
177                 /* XXX: Does this really get called every time the dos
178                  * codepage changes? */
179                 /* XXX: Is the did_reload test too strict? */
180                 conv_silent = True;
181                 init_doschar_table();
182                 init_valid_table();
183                 conv_silent = False;
184         }
185 }
186
187 /**
188  * Convert string from one encoding to another, making error checking etc
189  * Slow path version - uses (slow) iconv.
190  *
191  * @param src pointer to source string (multibyte or singlebyte)
192  * @param srclen length of the source string in bytes
193  * @param dest pointer to destination string (multibyte or singlebyte)
194  * @param destlen maximal length allowed for string
195  * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
196  * @returns the number of bytes occupied in the destination
197  *
198  * Ensure the srclen contains the terminating zero.
199  *
200  **/
201
202 static size_t convert_string_internal(charset_t from, charset_t to,
203                       void const *src, size_t srclen, 
204                       void *dest, size_t destlen, BOOL allow_bad_conv)
205 {
206         size_t i_len, o_len;
207         size_t retval;
208         const char* inbuf = (const char*)src;
209         char* outbuf = (char*)dest;
210         smb_iconv_t descriptor;
211
212         lazy_initialize_conv();
213
214         descriptor = conv_handles[from][to];
215
216         if (srclen == (size_t)-1) {
217                 if (from == CH_UTF16LE || from == CH_UTF16BE) {
218                         srclen = (strlen_w((const smb_ucs2_t *)src)+1) * 2;
219                 } else {
220                         srclen = strlen((const char *)src)+1;
221                 }
222         }
223
224
225         if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
226                 if (!conv_silent)
227                         DEBUG(0,("convert_string_internal: Conversion not supported.\n"));
228                 return (size_t)-1;
229         }
230
231         i_len=srclen;
232         o_len=destlen;
233
234  again:
235
236         retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
237         if(retval==(size_t)-1) {
238                 const char *reason="unknown error";
239                 switch(errno) {
240                         case EINVAL:
241                                 reason="Incomplete multibyte sequence";
242                                 if (!conv_silent)
243                                         DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
244                                 if (allow_bad_conv)
245                                         goto use_as_is;
246                                 break;
247                         case E2BIG:
248                                 reason="No more room"; 
249                                 if (!conv_silent) {
250                                         if (from == CH_UNIX) {
251                                                 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n",
252                                                         charset_name(from), charset_name(to),
253                                                         (unsigned int)srclen, (unsigned int)destlen, (const char *)src));
254                                         } else {
255                                                 DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u\n",
256                                                         charset_name(from), charset_name(to),
257                                                         (unsigned int)srclen, (unsigned int)destlen));
258                                         }
259                                 }
260                                 break;
261                         case EILSEQ:
262                                 reason="Illegal multibyte sequence";
263                                 if (!conv_silent)
264                                         DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
265                                 if (allow_bad_conv)
266                                         goto use_as_is;
267                                 break;
268                         default:
269                                 if (!conv_silent)
270                                         DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",reason,inbuf));
271                                 break;
272                 }
273                 /* smb_panic(reason); */
274         }
275         return destlen-o_len;
276
277  use_as_is:
278
279         /* 
280          * Conversion not supported. This is actually an error, but there are so
281          * many misconfigured iconv systems and smb.conf's out there we can't just
282          * fail. Do a very bad conversion instead.... JRA.
283          */
284
285         {
286                 if (o_len == 0 || i_len == 0)
287                         return destlen - o_len;
288
289                 if (((from == CH_UTF16LE)||(from == CH_UTF16BE)) &&
290                                 ((to != CH_UTF16LE)||(to != CH_UTF16BE))) {
291                         /* Can't convert from utf16 any endian to multibyte.
292                            Replace with the default fail char.
293                         */
294                         if (i_len < 2)
295                                 return destlen - o_len;
296                         if (i_len >= 2) {
297                                 *outbuf = lp_failed_convert_char();
298
299                                 outbuf++;
300                                 o_len--;
301
302                                 inbuf += 2;
303                                 i_len -= 2;
304                         }
305
306                         if (o_len == 0 || i_len == 0)
307                                 return destlen - o_len;
308
309                         /* Keep trying with the next char... */
310                         goto again;
311
312                 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
313                         /* Can't convert to UTF16LE - just widen by adding the
314                            default fail char then zero.
315                         */
316                         if (o_len < 2)
317                                 return destlen - o_len;
318
319                         outbuf[0] = lp_failed_convert_char();
320                         outbuf[1] = '\0';
321
322                         inbuf++;
323                         i_len--;
324
325                         outbuf += 2;
326                         o_len -= 2;
327
328                         if (o_len == 0 || i_len == 0)
329                                 return destlen - o_len;
330
331                         /* Keep trying with the next char... */
332                         goto again;
333
334                 } else if (from != CH_UTF16LE && from != CH_UTF16BE &&
335                                 to != CH_UTF16LE && to != CH_UTF16BE) {
336                         /* Failed multibyte to multibyte. Just copy the default fail char and
337                                 try again. */
338                         outbuf[0] = lp_failed_convert_char();
339
340                         inbuf++;
341                         i_len--;
342
343                         outbuf++;
344                         o_len--;
345
346                         if (o_len == 0 || i_len == 0)
347                                 return destlen - o_len;
348
349                         /* Keep trying with the next char... */
350                         goto again;
351
352                 } else {
353                         /* Keep compiler happy.... */
354                         return destlen - o_len;
355                 }
356         }
357 }
358
359 /**
360  * Convert string from one encoding to another, making error checking etc
361  * Fast path version - handles ASCII first.
362  *
363  * @param src pointer to source string (multibyte or singlebyte)
364  * @param srclen length of the source string in bytes, or -1 for nul terminated.
365  * @param dest pointer to destination string (multibyte or singlebyte)
366  * @param destlen maximal length allowed for string - *NEVER* -1.
367  * @param allow_bad_conv determines if a "best effort" conversion is acceptable (never returns errors)
368  * @returns the number of bytes occupied in the destination
369  *
370  * Ensure the srclen contains the terminating zero.
371  *
372  * This function has been hand-tuned to provide a fast path.
373  * Don't change unless you really know what you are doing. JRA.
374  **/
375
376 size_t convert_string(charset_t from, charset_t to,
377                       void const *src, size_t srclen, 
378                       void *dest, size_t destlen, BOOL allow_bad_conv)
379 {
380         /*
381          * NB. We deliberately don't do a strlen here if srclen == -1.
382          * This is very expensive over millions of calls and is taken
383          * care of in the slow path in convert_string_internal. JRA.
384          */
385
386 #ifdef DEVELOPER
387         SMB_ASSERT(destlen != (size_t)-1);
388 #endif
389
390         if (srclen == 0)
391                 return 0;
392
393         if (from != CH_UTF16LE && from != CH_UTF16BE && to != CH_UTF16LE && to != CH_UTF16BE) {
394                 const unsigned char *p = (const unsigned char *)src;
395                 unsigned char *q = (unsigned char *)dest;
396                 size_t slen = srclen;
397                 size_t dlen = destlen;
398                 unsigned char lastp = '\0';
399                 size_t retval = 0;
400
401                 /* If all characters are ascii, fast path here. */
402                 while (slen && dlen) {
403                         if ((lastp = *p) <= 0x7f) {
404                                 *q++ = *p++;
405                                 if (slen != (size_t)-1) {
406                                         slen--;
407                                 }
408                                 dlen--;
409                                 retval++;
410                                 if (!lastp)
411                                         break;
412                         } else {
413 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
414                                 goto general_case;
415 #else
416                                 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
417 #endif
418                         }
419                 }
420                 if (!dlen) {
421                         /* Even if we fast path we should note if we ran out of room. */
422                         if (((slen != (size_t)-1) && slen) ||
423                                         ((slen == (size_t)-1) && lastp)) {
424                                 errno = E2BIG;
425                         }
426                 }
427                 return retval;
428         } else if (from == CH_UTF16LE && to != CH_UTF16LE) {
429                 const unsigned char *p = (const unsigned char *)src;
430                 unsigned char *q = (unsigned char *)dest;
431                 size_t retval = 0;
432                 size_t slen = srclen;
433                 size_t dlen = destlen;
434                 unsigned char lastp = '\0';
435
436                 /* If all characters are ascii, fast path here. */
437                 while (((slen == (size_t)-1) || (slen >= 2)) && dlen) {
438                         if (((lastp = *p) <= 0x7f) && (p[1] == 0)) {
439                                 *q++ = *p;
440                                 if (slen != (size_t)-1) {
441                                         slen -= 2;
442                                 }
443                                 p += 2;
444                                 dlen--;
445                                 retval++;
446                                 if (!lastp)
447                                         break;
448                         } else {
449 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
450                                 goto general_case;
451 #else
452                                 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
453 #endif
454                         }
455                 }
456                 if (!dlen) {
457                         /* Even if we fast path we should note if we ran out of room. */
458                         if (((slen != (size_t)-1) && slen) ||
459                                         ((slen == (size_t)-1) && lastp)) {
460                                 errno = E2BIG;
461                         }
462                 }
463                 return retval;
464         } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
465                 const unsigned char *p = (const unsigned char *)src;
466                 unsigned char *q = (unsigned char *)dest;
467                 size_t retval = 0;
468                 size_t slen = srclen;
469                 size_t dlen = destlen;
470                 unsigned char lastp = '\0';
471
472                 /* If all characters are ascii, fast path here. */
473                 while (slen && (dlen >= 2)) {
474                         if ((lastp = *p) <= 0x7F) {
475                                 *q++ = *p++;
476                                 *q++ = '\0';
477                                 if (slen != (size_t)-1) {
478                                         slen--;
479                                 }
480                                 dlen -= 2;
481                                 retval += 2;
482                                 if (!lastp)
483                                         break;
484                         } else {
485 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
486                                 goto general_case;
487 #else
488                                 return retval + convert_string_internal(from, to, p, slen, q, dlen, allow_bad_conv);
489 #endif
490                         }
491                 }
492                 if (!dlen) {
493                         /* Even if we fast path we should note if we ran out of room. */
494                         if (((slen != (size_t)-1) && slen) ||
495                                         ((slen == (size_t)-1) && lastp)) {
496                                 errno = E2BIG;
497                         }
498                 }
499                 return retval;
500         }
501
502 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
503   general_case:
504 #endif
505         return convert_string_internal(from, to, src, srclen, dest, destlen, allow_bad_conv);
506 }
507
508 /**
509  * Convert between character sets, allocating a new buffer for the result.
510  *
511  * @param ctx TALLOC_CTX to use to allocate with. If NULL use malloc.
512  * (this is a bad interface and needs fixing. JRA).
513  * @param srclen length of source buffer.
514  * @param dest always set at least to NULL
515  * @note -1 is not accepted for srclen.
516  *
517  * @returns Size in bytes of the converted string; or -1 in case of error.
518  *
519  * Ensure the srclen contains the terminating zero.
520  *
521  * I hate the goto's in this function. It's embarressing.....
522  * There has to be a cleaner way to do this. JRA.
523  **/
524
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)
527 {
528         size_t i_len, o_len, destlen = MAX(srclen, 512);
529         size_t retval;
530         const char *inbuf = (const char *)src;
531         char *outbuf = NULL, *ob = NULL;
532         smb_iconv_t descriptor;
533         void **dest = (void **)dst;
534
535         *dest = NULL;
536
537         if (src == NULL || srclen == (size_t)-1)
538                 return (size_t)-1;
539         if (srclen == 0)
540                 return 0;
541
542         lazy_initialize_conv();
543
544         descriptor = conv_handles[from][to];
545
546         if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
547                 if (!conv_silent)
548                         DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
549                 return (size_t)-1;
550         }
551
552   convert:
553
554         if ((destlen*2) < destlen) {
555                 /* wrapped ! abort. */
556                 if (!conv_silent)
557                         DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
558                 if (!ctx)
559                         SAFE_FREE(outbuf);
560                 return (size_t)-1;
561         } else {
562                 destlen = destlen * 2;
563         }
564
565         if (ctx) {
566                 ob = (char *)TALLOC_REALLOC(ctx, ob, destlen);
567         } else {
568                 ob = (char *)SMB_REALLOC(ob, destlen);
569         }
570
571         if (!ob) {
572                 DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
573                 return (size_t)-1;
574         }
575         outbuf = ob;
576         i_len = srclen;
577         o_len = destlen;
578
579  again:
580
581         retval = smb_iconv(descriptor,
582                            &inbuf, &i_len,
583                            &outbuf, &o_len);
584         if(retval == (size_t)-1)                {
585                 const char *reason="unknown error";
586                 switch(errno) {
587                         case EINVAL:
588                                 reason="Incomplete multibyte sequence";
589                                 if (!conv_silent)
590                                         DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
591                                 if (allow_bad_conv)
592                                         goto use_as_is;
593                                 break;
594                         case E2BIG:
595                                 goto convert;           
596                         case EILSEQ:
597                                 reason="Illegal multibyte sequence";
598                                 if (!conv_silent)
599                                         DEBUG(3,("convert_string_allocate: Conversion error: %s(%s)\n",reason,inbuf));
600                                 if (allow_bad_conv)
601                                         goto use_as_is;
602                                 break;
603                 }
604                 if (!conv_silent)
605                         DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
606                 /* smb_panic(reason); */
607                 if (ctx) {
608                         TALLOC_FREE(ob);
609                 } else {
610                         SAFE_FREE(ob);
611                 }
612                 return (size_t)-1;
613         }
614
615   out:
616
617         destlen = destlen - o_len;
618         if (ctx) {
619                 ob = (char *)TALLOC_REALLOC(ctx,ob,destlen);
620         } else {
621                 ob = (char *)SMB_REALLOC(ob,destlen);
622         }
623
624         if (destlen && !ob) {
625                 DEBUG(0, ("convert_string_allocate: out of memory!\n"));
626                 return (size_t)-1;
627         }
628
629         *dest = ob;
630         return destlen;
631
632  use_as_is:
633
634         /* 
635          * Conversion not supported. This is actually an error, but there are so
636          * many misconfigured iconv systems and smb.conf's out there we can't just
637          * fail. Do a very bad conversion instead.... JRA.
638          */
639
640         {
641                 if (o_len == 0 || i_len == 0)
642                         goto out;
643
644                 if (((from == CH_UTF16LE)||(from == CH_UTF16BE)) &&
645                                 ((to != CH_UTF16LE)||(to != CH_UTF16BE))) {
646                         /* Can't convert from utf16 any endian to multibyte.
647                            Replace with the default fail char.
648                         */
649
650                         if (i_len < 2)
651                                 goto out;
652
653                         if (i_len >= 2) {
654                                 *outbuf = lp_failed_convert_char();
655
656                                 outbuf++;
657                                 o_len--;
658
659                                 inbuf += 2;
660                                 i_len -= 2;
661                         }
662
663                         if (o_len == 0 || i_len == 0)
664                                 goto out;
665
666                         /* Keep trying with the next char... */
667                         goto again;
668
669                 } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
670                         /* Can't convert to UTF16LE - just widen by adding the
671                            default fail char then zero.
672                         */
673                         if (o_len < 2)
674                                 goto out;
675
676                         outbuf[0] = lp_failed_convert_char();
677                         outbuf[1] = '\0';
678
679                         inbuf++;
680                         i_len--;
681
682                         outbuf += 2;
683                         o_len -= 2;
684
685                         if (o_len == 0 || i_len == 0)
686                                 goto out;
687
688                         /* Keep trying with the next char... */
689                         goto again;
690
691                 } else if (from != CH_UTF16LE && from != CH_UTF16BE &&
692                                 to != CH_UTF16LE && to != CH_UTF16BE) {
693                         /* Failed multibyte to multibyte. Just copy the default fail char and
694                            try again. */
695                         outbuf[0] = lp_failed_convert_char();
696
697                         inbuf++;
698                         i_len--;
699
700                         outbuf++;
701                         o_len--;
702
703                         if (o_len == 0 || i_len == 0)
704                                 goto out;
705
706                         /* Keep trying with the next char... */
707                         goto again;
708
709                 } else {
710                         /* Keep compiler happy.... */
711                         goto out;
712                 }
713         }
714 }
715
716 /**
717  * Convert between character sets, allocating a new buffer using talloc for the result.
718  *
719  * @param srclen length of source buffer.
720  * @param dest always set at least to NULL
721  * @note -1 is not accepted for srclen.
722  *
723  * @returns Size in bytes of the converted string; or -1 in case of error.
724  **/
725 size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
726                              void const *src, size_t srclen, void *dst,
727                              BOOL allow_bad_conv)
728 {
729         void **dest = (void **)dst;
730         size_t dest_len;
731
732         *dest = NULL;
733         dest_len=convert_string_allocate(ctx, from, to, src, srclen, dest, allow_bad_conv);
734         if (dest_len == (size_t)-1)
735                 return (size_t)-1;
736         if (*dest == NULL)
737                 return (size_t)-1;
738         return dest_len;
739 }
740
741 size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
742 {
743         size_t size;
744         smb_ucs2_t *buffer;
745
746         size = push_ucs2_allocate(&buffer, src);
747         if (size == (size_t)-1) {
748                 smb_panic("failed to create UCS2 buffer");
749         }
750         if (!strupper_w(buffer) && (dest == src)) {
751                 free(buffer);
752                 return srclen;
753         }
754
755         size = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, True);
756         free(buffer);
757         return size;
758 }
759
760 /**
761  strdup() a unix string to upper case.
762  Max size is pstring.
763 **/
764
765 char *strdup_upper(const char *s)
766 {
767         pstring out_buffer;
768         const unsigned char *p = (const unsigned char *)s;
769         unsigned char *q = (unsigned char *)out_buffer;
770
771         /* this is quite a common operation, so we want it to be
772            fast. We optimise for the ascii case, knowing that all our
773            supported multi-byte character sets are ascii-compatible
774            (ie. they match for the first 128 chars) */
775
776         while (1) {
777                 if (*p & 0x80)
778                         break;
779                 *q++ = toupper_ascii(*p);
780                 if (!*p)
781                         break;
782                 p++;
783                 if (p - ( const unsigned char *)s >= sizeof(pstring))
784                         break;
785         }
786
787         if (*p) {
788                 /* MB case. */
789                 size_t size;
790                 wpstring buffer;
791                 size = convert_string(CH_UNIX, CH_UTF16LE, s, -1, buffer, sizeof(buffer), True);
792                 if (size == (size_t)-1) {
793                         return NULL;
794                 }
795
796                 strupper_w(buffer);
797
798                 size = convert_string(CH_UTF16LE, CH_UNIX, buffer, -1, out_buffer, sizeof(out_buffer), True);
799                 if (size == (size_t)-1) {
800                         return NULL;
801                 }
802         }
803
804         return SMB_STRDUP(out_buffer);
805 }
806
807 /**
808  talloc_strdup() a unix string to upper case.
809 **/
810
811 char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s)
812 {
813         char *out_buffer = talloc_strdup(ctx,s);
814         const unsigned char *p = (const unsigned char *)s;
815         unsigned char *q = (unsigned char *)out_buffer;
816
817         if (!q) {
818                 return NULL;
819         }
820
821         /* this is quite a common operation, so we want it to be
822            fast. We optimise for the ascii case, knowing that all our
823            supported multi-byte character sets are ascii-compatible
824            (ie. they match for the first 128 chars) */
825
826         while (1) {
827                 if (*p & 0x80)
828                         break;
829                 *q++ = toupper_ascii(*p);
830                 if (!*p)
831                         break;
832                 p++;
833         }
834
835         if (*p) {
836                 /* MB case. */
837                 size_t size;
838                 smb_ucs2_t *ubuf = NULL;
839
840                 /* We're not using the ascii buffer above. */
841                 TALLOC_FREE(out_buffer);
842
843                 size = convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE,
844                                 s, strlen(s)+1,
845                                 (void *)&ubuf,
846                                 True);
847                 if (size == (size_t)-1) {
848                         return NULL;
849                 }
850
851                 strupper_w(ubuf);
852
853                 size = convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX,
854                                 ubuf, size,
855                                 (void *)&out_buffer,
856                                 True);
857
858                 /* Don't need the intermediate buffer
859                  * anymore.
860                  */
861
862                 TALLOC_FREE(ubuf);
863
864                 if (size == (size_t)-1) {
865                         return NULL;
866                 }
867         }
868
869         return out_buffer;
870 }
871
872 size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
873 {
874         size_t size;
875         smb_ucs2_t *buffer = NULL;
876
877         size = convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, srclen,
878                                        (void **)(void *)&buffer, True);
879         if (size == (size_t)-1 || !buffer) {
880                 smb_panic("failed to create UCS2 buffer");
881         }
882         if (!strlower_w(buffer) && (dest == src)) {
883                 SAFE_FREE(buffer);
884                 return srclen;
885         }
886         size = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, True);
887         SAFE_FREE(buffer);
888         return size;
889 }
890
891 /**
892  strdup() a unix string to lower case.
893 **/
894
895 char *strdup_lower(const char *s)
896 {
897         size_t size;
898         smb_ucs2_t *buffer = NULL;
899         char *out_buffer;
900
901         size = push_ucs2_allocate(&buffer, s);
902         if (size == -1 || !buffer) {
903                 return NULL;
904         }
905
906         strlower_w(buffer);
907
908         size = pull_ucs2_allocate(&out_buffer, buffer);
909         SAFE_FREE(buffer);
910
911         if (size == (size_t)-1) {
912                 return NULL;
913         }
914
915         return out_buffer;
916 }
917
918 static size_t ucs2_align(const void *base_ptr, const void *p, int flags)
919 {
920         if (flags & (STR_NOALIGN|STR_ASCII))
921                 return 0;
922         return PTR_DIFF(p, base_ptr) & 1;
923 }
924
925
926 /**
927  * Copy a string from a char* unix src to a dos codepage string destination.
928  *
929  * @return the number of bytes occupied by the string in the destination.
930  *
931  * @param flags can include
932  * <dl>
933  * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
934  * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
935  * </dl>
936  *
937  * @param dest_len the maximum length in bytes allowed in the
938  * destination.
939  **/
940 size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
941 {
942         size_t src_len = strlen(src);
943         char *tmpbuf = NULL;
944         size_t ret;
945
946         /* No longer allow a length of -1. */
947         if (dest_len == (size_t)-1) {
948                 smb_panic("push_ascii - dest_len == -1");
949                 return (size_t)0;
950         }
951
952         if (flags & STR_UPPER) {
953                 tmpbuf = SMB_STRDUP(src);
954                 if (!tmpbuf) {
955                         smb_panic("malloc fail");
956                         return (size_t)0;
957                 }
958                 strupper_m(tmpbuf);
959                 src = tmpbuf;
960         }
961
962         if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
963                 src_len++;
964         }
965
966         ret = convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len, True);
967         SAFE_FREE(tmpbuf);
968         if (ret == (size_t)-1) {
969                 return 0;
970         }
971         return ret;
972 }
973
974 size_t push_ascii_fstring(void *dest, const char *src)
975 {
976         return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
977 }
978
979 size_t push_ascii_pstring(void *dest, const char *src)
980 {
981         return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
982 }
983
984 /********************************************************************
985  Push an nstring - ensure null terminated. Written by
986  moriyama@miraclelinux.com (MORIYAMA Masayuki).
987 ********************************************************************/
988
989 size_t push_ascii_nstring(void *dest, const char *src)
990 {
991         size_t i, buffer_len, dest_len;
992         smb_ucs2_t *buffer;
993
994         conv_silent = True;
995         buffer_len = push_ucs2_allocate(&buffer, src);
996         if (buffer_len == (size_t)-1) {
997                 smb_panic("failed to create UCS2 buffer");
998         }
999
1000         /* We're using buffer_len below to count ucs2 characters, not bytes. */
1001         buffer_len /= sizeof(smb_ucs2_t);
1002
1003         dest_len = 0;
1004         for (i = 0; buffer[i] != 0 && (i < buffer_len); i++) {
1005                 unsigned char mb[10];
1006                 /* Convert one smb_ucs2_t character at a time. */
1007                 size_t mb_len = convert_string(CH_UTF16LE, CH_DOS, buffer+i, sizeof(smb_ucs2_t), mb, sizeof(mb), False);
1008                 if ((mb_len != (size_t)-1) && (dest_len + mb_len <= MAX_NETBIOSNAME_LEN - 1)) {
1009                         memcpy((char *)dest + dest_len, mb, mb_len);
1010                         dest_len += mb_len;
1011                 } else {
1012                         errno = E2BIG;
1013                         break;
1014                 }
1015         }
1016         ((char *)dest)[dest_len] = '\0';
1017
1018         SAFE_FREE(buffer);
1019         conv_silent = False;
1020         return dest_len;
1021 }
1022
1023 /********************************************************************
1024  Push and malloc an ascii string. src and dest null terminated.
1025 ********************************************************************/
1026
1027 size_t push_ascii_allocate(char **dest, const char *src)
1028 {
1029         size_t src_len = strlen(src)+1;
1030
1031         *dest = NULL;
1032         return convert_string_allocate(NULL, CH_UNIX, CH_DOS, src, src_len, (void **)dest, True);
1033 }
1034
1035 /**
1036  * Copy a string from a dos codepage source to a unix char* destination.
1037  *
1038  * The resulting string in "dest" is always null terminated.
1039  *
1040  * @param flags can have:
1041  * <dl>
1042  * <dt>STR_TERMINATE</dt>
1043  * <dd>STR_TERMINATE means the string in @p src
1044  * is null terminated, and src_len is ignored.</dd>
1045  * </dl>
1046  *
1047  * @param src_len is the length of the source area in bytes.
1048  * @returns the number of bytes occupied by the string in @p src.
1049  **/
1050 size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1051 {
1052         size_t ret;
1053
1054         if (dest_len == (size_t)-1)
1055                 dest_len = sizeof(pstring);
1056
1057         if (flags & STR_TERMINATE) {
1058                 if (src_len == (size_t)-1) {
1059                         src_len = strlen((const char *)src) + 1;
1060                 } else {
1061                         size_t len = strnlen((const char *)src, src_len);
1062                         if (len < src_len)
1063                                 len++;
1064                         src_len = len;
1065                 }
1066         }
1067
1068         ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len, True);
1069         if (ret == (size_t)-1) {
1070                 ret = 0;
1071                 dest_len = 0;
1072         }
1073
1074         if (dest_len && ret) {
1075                 /* Did we already process the terminating zero ? */
1076                 if (dest[MIN(ret-1, dest_len-1)] != 0) {
1077                         dest[MIN(ret, dest_len-1)] = 0;
1078                 }
1079         } else  {
1080                 dest[0] = 0;
1081         }
1082
1083         return src_len;
1084 }
1085
1086 /**
1087  * Copy a string from a dos codepage source to a unix char* destination.
1088  Talloc version.
1089  Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1090  needs fixing. JRA).
1091  *
1092  * The resulting string in "dest" is always null terminated.
1093  *
1094  * @param flags can have:
1095  * <dl>
1096  * <dt>STR_TERMINATE</dt>
1097  * <dd>STR_TERMINATE means the string in @p src
1098  * is null terminated, and src_len is ignored.</dd>
1099  * </dl>
1100  *
1101  * @param src_len is the length of the source area in bytes.
1102  * @returns the number of bytes occupied by the string in @p src.
1103  **/
1104
1105 static size_t pull_ascii_base_talloc(TALLOC_CTX *ctx,
1106                                         char **ppdest,
1107                                         const void *src,
1108                                         size_t src_len,
1109                                         int flags)
1110 {
1111         char *dest = NULL;
1112         size_t dest_len = 0;
1113
1114 #ifdef DEVELOPER
1115         /* Ensure we never use the braindead "malloc" varient. */
1116         if (ctx == NULL) {
1117                 smb_panic("NULL talloc CTX in pull_ascii_base_talloc\n");
1118         }
1119 #endif
1120
1121         *ppdest = NULL;
1122
1123         if (flags & STR_TERMINATE) {
1124                 if (src_len == (size_t)-1) {
1125                         src_len = strlen((const char *)src) + 1;
1126                 } else {
1127                         size_t len = strnlen((const char *)src, src_len);
1128                         if (len < src_len)
1129                                 len++;
1130                         src_len = len;
1131                 }
1132                 /* Ensure we don't use an insane length from the client. */
1133                 if (src_len >= 1024*1024) {
1134                         char *msg = talloc_asprintf(ctx,
1135                                         "Bad src length (%u) in "
1136                                         "pull_ascii_base_talloc",
1137                                         (unsigned int)src_len);
1138                         smb_panic(msg);
1139                 }
1140         }
1141
1142         dest_len = convert_string_allocate(ctx,
1143                                 CH_DOS,
1144                                 CH_UNIX,
1145                                 src,
1146                                 src_len,
1147                                 &dest,
1148                                 True);
1149
1150         if (dest_len == (size_t)-1) {
1151                 return 0;
1152         }
1153
1154         if (dest_len && dest) {
1155                 /* Did we already process the terminating zero ? */
1156                 if (dest[dest_len-1] != 0) {
1157                         dest[dest_len-1] = 0;
1158                 }
1159         } else if (dest) {
1160                 dest[0] = 0;
1161         }
1162
1163         *ppdest = dest;
1164         return src_len;
1165 }
1166
1167
1168 size_t pull_ascii_pstring(char *dest, const void *src)
1169 {
1170         return pull_ascii(dest, src, sizeof(pstring), -1, STR_TERMINATE);
1171 }
1172
1173 size_t pull_ascii_fstring(char *dest, const void *src)
1174 {
1175         return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
1176 }
1177
1178 /* When pulling an nstring it can expand into a larger size (dos cp -> utf8). Cope with this. */
1179
1180 size_t pull_ascii_nstring(char *dest, size_t dest_len, const void *src)
1181 {
1182         return pull_ascii(dest, src, dest_len, sizeof(nstring)-1, STR_TERMINATE);
1183 }
1184
1185 /**
1186  * Copy a string from a char* src to a unicode destination.
1187  *
1188  * @returns the number of bytes occupied by the string in the destination.
1189  *
1190  * @param flags can have:
1191  *
1192  * <dl>
1193  * <dt>STR_TERMINATE <dd>means include the null termination.
1194  * <dt>STR_UPPER     <dd>means uppercase in the destination.
1195  * <dt>STR_NOALIGN   <dd>means don't do alignment.
1196  * </dl>
1197  *
1198  * @param dest_len is the maximum length allowed in the
1199  * destination. If dest_len is -1 then no maxiumum is used.
1200  **/
1201
1202 size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
1203 {
1204         size_t len=0;
1205         size_t src_len;
1206         size_t ret;
1207
1208         /* treat a pstring as "unlimited" length */
1209         if (dest_len == (size_t)-1)
1210                 dest_len = sizeof(pstring);
1211
1212         if (flags & STR_TERMINATE)
1213                 src_len = (size_t)-1;
1214         else
1215                 src_len = strlen(src);
1216
1217         if (ucs2_align(base_ptr, dest, flags)) {
1218                 *(char *)dest = 0;
1219                 dest = (void *)((char *)dest + 1);
1220                 if (dest_len)
1221                         dest_len--;
1222                 len++;
1223         }
1224
1225         /* ucs2 is always a multiple of 2 bytes */
1226         dest_len &= ~1;
1227
1228         ret =  convert_string(CH_UNIX, CH_UTF16LE, src, src_len, dest, dest_len, True);
1229         if (ret == (size_t)-1) {
1230                 return 0;
1231         }
1232
1233         len += ret;
1234
1235         if (flags & STR_UPPER) {
1236                 smb_ucs2_t *dest_ucs2 = (smb_ucs2_t *)dest;
1237                 size_t i;
1238
1239                 /* We check for i < (ret / 2) below as the dest string isn't null
1240                    terminated if STR_TERMINATE isn't set. */
1241
1242                 for (i = 0; i < (ret / 2) && i < (dest_len / 2) && dest_ucs2[i]; i++) {
1243                         smb_ucs2_t v = toupper_w(dest_ucs2[i]);
1244                         if (v != dest_ucs2[i]) {
1245                                 dest_ucs2[i] = v;
1246                         }
1247                 }
1248         }
1249
1250         return len;
1251 }
1252
1253
1254 /**
1255  * Copy a string from a unix char* src to a UCS2 destination,
1256  * allocating a buffer using talloc().
1257  *
1258  * @param dest always set at least to NULL 
1259  *
1260  * @returns The number of bytes occupied by the string in the destination
1261  *         or -1 in case of error.
1262  **/
1263 size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
1264 {
1265         size_t src_len = strlen(src)+1;
1266
1267         *dest = NULL;
1268         return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len, (void **)dest, True);
1269 }
1270
1271
1272 /**
1273  * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
1274  *
1275  * @param dest always set at least to NULL 
1276  *
1277  * @returns The number of bytes occupied by the string in the destination
1278  *         or -1 in case of error.
1279  **/
1280
1281 size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
1282 {
1283         size_t src_len = strlen(src)+1;
1284
1285         *dest = NULL;
1286         return convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, src_len, (void **)dest, True);
1287 }
1288
1289 /**
1290  Copy a string from a char* src to a UTF-8 destination.
1291  Return the number of bytes occupied by the string in the destination
1292  Flags can have:
1293   STR_TERMINATE means include the null termination
1294   STR_UPPER     means uppercase in the destination
1295  dest_len is the maximum length allowed in the destination. If dest_len
1296  is -1 then no maxiumum is used.
1297 **/
1298
1299 static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
1300 {
1301         size_t src_len = strlen(src);
1302         pstring tmpbuf;
1303
1304         /* treat a pstring as "unlimited" length */
1305         if (dest_len == (size_t)-1)
1306                 dest_len = sizeof(pstring);
1307
1308         if (flags & STR_UPPER) {
1309                 pstrcpy(tmpbuf, src);
1310                 strupper_m(tmpbuf);
1311                 src = tmpbuf;
1312         }
1313
1314         if (flags & STR_TERMINATE)
1315                 src_len++;
1316
1317         return convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len, True);
1318 }
1319
1320 size_t push_utf8_fstring(void *dest, const char *src)
1321 {
1322         return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
1323 }
1324
1325 /**
1326  * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
1327  *
1328  * @param dest always set at least to NULL 
1329  *
1330  * @returns The number of bytes occupied by the string in the destination
1331  **/
1332
1333 size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1334 {
1335         size_t src_len = strlen(src)+1;
1336
1337         *dest = NULL;
1338         return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest, True);
1339 }
1340
1341 /**
1342  * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
1343  *
1344  * @param dest always set at least to NULL 
1345  *
1346  * @returns The number of bytes occupied by the string in the destination
1347  **/
1348
1349 size_t push_utf8_allocate(char **dest, const char *src)
1350 {
1351         size_t src_len = strlen(src)+1;
1352
1353         *dest = NULL;
1354         return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, True);      
1355 }
1356
1357 /**
1358  Copy a string from a ucs2 source to a unix char* destination.
1359  Flags can have:
1360   STR_TERMINATE means the string in src is null terminated.
1361   STR_NOALIGN   means don't try to align.
1362  if STR_TERMINATE is set then src_len is ignored if it is -1.
1363  src_len is the length of the source area in bytes
1364  Return the number of bytes occupied by the string in src.
1365  The resulting string in "dest" is always null terminated.
1366 **/
1367
1368 size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
1369 {
1370         size_t ret;
1371
1372         if (dest_len == (size_t)-1)
1373                 dest_len = sizeof(pstring);
1374
1375         if (ucs2_align(base_ptr, src, flags)) {
1376                 src = (const void *)((const char *)src + 1);
1377                 if (src_len != (size_t)-1)
1378                         src_len--;
1379         }
1380
1381         if (flags & STR_TERMINATE) {
1382                 /* src_len -1 is the default for null terminated strings. */
1383                 if (src_len != (size_t)-1) {
1384                         size_t len = strnlen_w((const smb_ucs2_t *)src,
1385                                                 src_len/2);
1386                         if (len < src_len/2)
1387                                 len++;
1388                         src_len = len*2;
1389                 }
1390         }
1391
1392         /* ucs2 is always a multiple of 2 bytes */
1393         if (src_len != (size_t)-1)
1394                 src_len &= ~1;
1395
1396         ret = convert_string(CH_UTF16LE, CH_UNIX, src, src_len, dest, dest_len, True);
1397         if (ret == (size_t)-1) {
1398                 return 0;
1399         }
1400
1401         if (src_len == (size_t)-1)
1402                 src_len = ret*2;
1403
1404         if (dest_len && ret) {
1405                 /* Did we already process the terminating zero ? */
1406                 if (dest[MIN(ret-1, dest_len-1)] != 0) {
1407                         dest[MIN(ret, dest_len-1)] = 0;
1408                 }
1409         } else {
1410                 dest[0] = 0;
1411         }
1412
1413         return src_len;
1414 }
1415
1416 /**
1417  Copy a string from a ucs2 source to a unix char* destination.
1418  Talloc version with a base pointer.
1419  Uses malloc if TALLOC_CTX is NULL (this is a bad interface and
1420  needs fixing. JRA).
1421  Flags can have:
1422   STR_TERMINATE means the string in src is null terminated.
1423   STR_NOALIGN   means don't try to align.
1424  if STR_TERMINATE is set then src_len is ignored if it is -1.
1425  src_len is the length of the source area in bytes
1426  Return the number of bytes occupied by the string in src.
1427  The resulting string in "dest" is always null terminated.
1428 **/
1429
1430 static size_t pull_ucs2_base_talloc(TALLOC_CTX *ctx,
1431                         const void *base_ptr,
1432                         char **ppdest,
1433                         const void *src,
1434                         size_t src_len,
1435                         int flags)
1436 {
1437         char *dest;
1438         size_t dest_len;
1439
1440         *ppdest = NULL;
1441
1442 #ifdef DEVELOPER
1443         /* Ensure we never use the braindead "malloc" varient. */
1444         if (ctx == NULL) {
1445                 smb_panic("NULL talloc CTX in pull_ucs2_base_talloc\n");
1446         }
1447 #endif
1448
1449         if (ucs2_align(base_ptr, src, flags)) {
1450                 src = (const void *)((const char *)src + 1);
1451                 if (src_len != (size_t)-1)
1452                         src_len--;
1453         }
1454
1455         if (flags & STR_TERMINATE) {
1456                 /* src_len -1 is the default for null terminated strings. */
1457                 if (src_len != (size_t)-1) {
1458                         size_t len = strnlen_w((const smb_ucs2_t *)src,
1459                                                 src_len/2);
1460                         if (len < src_len/2)
1461                                 len++;
1462                         src_len = len*2;
1463                 }
1464                 /* Ensure we don't use an insane length from the client. */
1465                 if (src_len >= 1024*1024) {
1466                         smb_panic("Bad src length in pull_ucs2_base_talloc\n");
1467                 }
1468         }
1469
1470         /* ucs2 is always a multiple of 2 bytes */
1471         if (src_len != (size_t)-1) {
1472                 src_len &= ~1;
1473         }
1474
1475         dest_len = convert_string_talloc(ctx,
1476                                         CH_UTF16LE,
1477                                         CH_UNIX,
1478                                         src,
1479                                         src_len,
1480                                         (void *)&dest,
1481                                         True);
1482         if (dest_len == (size_t)-1) {
1483                 return 0;
1484         }
1485
1486         if (src_len == (size_t)-1)
1487                 src_len = dest_len*2;
1488
1489         if (dest_len) {
1490                 /* Did we already process the terminating zero ? */
1491                 if (dest[dest_len-1] != 0) {
1492                         size_t size = talloc_get_size(dest);
1493                         /* Have we got space to append the '\0' ? */
1494                         if (size <= dest_len) {
1495                                 /* No, realloc. */
1496                                 dest = TALLOC_REALLOC_ARRAY(ctx, dest, char,
1497                                                 dest_len+1);
1498                                 if (!dest) {
1499                                         /* talloc fail. */
1500                                         dest_len = (size_t)-1;
1501                                         return 0;
1502                                 }
1503                         }
1504                         /* Yay - space ! */
1505                         dest[dest_len] = '\0';
1506                         dest_len++;
1507                 }
1508         } else if (dest) {
1509                 dest[0] = 0;
1510         }
1511
1512         *ppdest = dest;
1513         return src_len;
1514 }
1515
1516 size_t pull_ucs2_pstring(char *dest, const void *src)
1517 {
1518         return pull_ucs2(NULL, dest, src, sizeof(pstring), -1, STR_TERMINATE);
1519 }
1520
1521 size_t pull_ucs2_fstring(char *dest, const void *src)
1522 {
1523         return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
1524 }
1525
1526 /**
1527  * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
1528  *
1529  * @param dest always set at least to NULL 
1530  *
1531  * @returns The number of bytes occupied by the string in the destination
1532  **/
1533
1534 size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
1535 {
1536         size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1537         *dest = NULL;
1538         return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len, (void **)dest, True);
1539 }
1540
1541 /**
1542  * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
1543  *
1544  * @param dest always set at least to NULL 
1545  *
1546  * @returns The number of bytes occupied by the string in the destination
1547  **/
1548
1549 size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
1550 {
1551         size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
1552         *dest = NULL;
1553         return convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, src, src_len, (void **)dest, True);
1554 }
1555
1556 /**
1557  * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
1558  *
1559  * @param dest always set at least to NULL 
1560  *
1561  * @returns The number of bytes occupied by the string in the destination
1562  **/
1563
1564 size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1565 {
1566         size_t src_len = strlen(src)+1;
1567         *dest = NULL;
1568         return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1569 }
1570
1571 /**
1572  * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
1573  *
1574  * @param dest always set at least to NULL 
1575  *
1576  * @returns The number of bytes occupied by the string in the destination
1577  **/
1578
1579 size_t pull_utf8_allocate(char **dest, const char *src)
1580 {
1581         size_t src_len = strlen(src)+1;
1582         *dest = NULL;
1583         return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
1584 }
1585  
1586 /**
1587  * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc
1588  *
1589  * @param dest always set at least to NULL 
1590  *
1591  * @returns The number of bytes occupied by the string in the destination
1592  **/
1593
1594 size_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
1595 {
1596         size_t src_len = strlen(src)+1;
1597         *dest = NULL;
1598         return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest, True);
1599 }
1600
1601 /**
1602  Copy a string from a char* src to a unicode or ascii
1603  dos codepage destination choosing unicode or ascii based on the 
1604  flags in the SMB buffer starting at base_ptr.
1605  Return the number of bytes occupied by the string in the destination.
1606  flags can have:
1607   STR_TERMINATE means include the null termination.
1608   STR_UPPER     means uppercase in the destination.
1609   STR_ASCII     use ascii even with unicode packet.
1610   STR_NOALIGN   means don't do alignment.
1611  dest_len is the maximum length allowed in the destination. If dest_len
1612  is -1 then no maxiumum is used.
1613 **/
1614
1615 size_t push_string_fn(const char *function, unsigned int line,
1616                       const void *base_ptr, uint16 flags2,
1617                       void *dest, const char *src,
1618                       size_t dest_len, int flags)
1619 {
1620 #ifdef DEVELOPER
1621         /* We really need to zero fill here, not clobber
1622          * region, as we want to ensure that valgrind thinks
1623          * all of the outgoing buffer has been written to
1624          * so a send() or write() won't trap an error.
1625          * JRA.
1626          */
1627 #if 0
1628         if (dest_len != (size_t)-1)
1629                 clobber_region(function, line, dest, dest_len);
1630 #else
1631         if (dest_len != (size_t)-1)
1632                 memset(dest, '\0', dest_len);
1633 #endif
1634 #endif
1635
1636         if (!(flags & STR_ASCII) && \
1637             ((flags & STR_UNICODE || \
1638               (flags2 & FLAGS2_UNICODE_STRINGS)))) {
1639                 return push_ucs2(base_ptr, dest, src, dest_len, flags);
1640         }
1641         return push_ascii(dest, src, dest_len, flags);
1642 }
1643
1644
1645 /**
1646  Copy a string from a unicode or ascii source (depending on
1647  the packet flags) to a char* destination.
1648  Flags can have:
1649   STR_TERMINATE means the string in src is null terminated.
1650   STR_UNICODE   means to force as unicode.
1651   STR_ASCII     use ascii even with unicode packet.
1652   STR_NOALIGN   means don't do alignment.
1653  if STR_TERMINATE is set then src_len is ignored is it is -1
1654  src_len is the length of the source area in bytes.
1655  Return the number of bytes occupied by the string in src.
1656  The resulting string in "dest" is always null terminated.
1657 **/
1658
1659 size_t pull_string_fn(const char *function, unsigned int line,
1660                       const void *base_ptr, uint16 smb_flags2, char *dest,
1661                       const void *src, size_t dest_len, size_t src_len,
1662                       int flags)
1663 {
1664 #ifdef DEVELOPER
1665         if (dest_len != (size_t)-1)
1666                 clobber_region(function, line, dest, dest_len);
1667 #endif
1668
1669         if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
1670                 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1671                           "UNICODE defined");
1672         }
1673
1674         if (!(flags & STR_ASCII) && \
1675             ((flags & STR_UNICODE || \
1676               (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
1677                 return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
1678         }
1679         return pull_ascii(dest, src, dest_len, src_len, flags);
1680 }
1681
1682 /**
1683  Copy a string from a unicode or ascii source (depending on
1684  the packet flags) to a char* destination.
1685  Variant that uses talloc.
1686  Flags can have:
1687   STR_TERMINATE means the string in src is null terminated.
1688   STR_UNICODE   means to force as unicode.
1689   STR_ASCII     use ascii even with unicode packet.
1690   STR_NOALIGN   means don't do alignment.
1691  if STR_TERMINATE is set then src_len is ignored is it is -1
1692  src_len is the length of the source area in bytes.
1693  Return the number of bytes occupied by the string in src.
1694  The resulting string in "dest" is always null terminated.
1695 **/
1696
1697 size_t pull_string_talloc_fn(const char *function,
1698                         unsigned int line,
1699                         TALLOC_CTX *ctx,
1700                         const void *base_ptr,
1701                         uint16 smb_flags2,
1702                         char **ppdest,
1703                         const void *src,
1704                         size_t src_len,
1705                         int flags)
1706 {
1707         if ((base_ptr == NULL) && ((flags & (STR_ASCII|STR_UNICODE)) == 0)) {
1708                 smb_panic("No base ptr to get flg2 and neither ASCII nor "
1709                           "UNICODE defined");
1710         }
1711
1712         if (!(flags & STR_ASCII) && \
1713             ((flags & STR_UNICODE || \
1714               (smb_flags2 & FLAGS2_UNICODE_STRINGS)))) {
1715                 return pull_ucs2_base_talloc(ctx,
1716                                         base_ptr,
1717                                         ppdest,
1718                                         src,
1719                                         src_len,
1720                                         flags);
1721         }
1722         return pull_ascii_base_talloc(ctx,
1723                                         ppdest,
1724                                         src,
1725                                         src_len,
1726                                         flags);
1727 }
1728
1729
1730 size_t align_string(const void *base_ptr, const char *p, int flags)
1731 {
1732         if (!(flags & STR_ASCII) && \
1733             ((flags & STR_UNICODE || \
1734               (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
1735                 return ucs2_align(base_ptr, p, flags);
1736         }
1737         return 0;
1738 }
1739
1740 /*
1741   Return the unicode codepoint for the next multi-byte CH_UNIX character
1742   in the string. The unicode codepoint (codepoint_t) is an unsinged 32 bit value.
1743
1744   Also return the number of bytes consumed (which tells the caller
1745   how many bytes to skip to get to the next CH_UNIX character).
1746
1747   Return INVALID_CODEPOINT if the next character cannot be converted.
1748 */
1749
1750 codepoint_t next_codepoint(const char *str, size_t *size)
1751 {
1752         /* It cannot occupy more than 4 bytes in UTF16 format */
1753         uint8_t buf[4];
1754         smb_iconv_t descriptor;
1755         size_t ilen_orig;
1756         size_t ilen;
1757         size_t olen;
1758         char *outbuf;
1759
1760         if ((str[0] & 0x80) == 0) {
1761                 *size = 1;
1762                 return (codepoint_t)str[0];
1763         }
1764
1765         /* We assume that no multi-byte character can take
1766            more than 5 bytes. This is OK as we only
1767            support codepoints up to 1M */
1768
1769         ilen_orig = strnlen(str, 5);
1770         ilen = ilen_orig;
1771
1772         lazy_initialize_conv();
1773
1774         descriptor = conv_handles[CH_UNIX][CH_UTF16LE];
1775         if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
1776                 *size = 1;
1777                 return INVALID_CODEPOINT;
1778         }
1779
1780         /* This looks a little strange, but it is needed to cope
1781            with codepoints above 64k which are encoded as per RFC2781. */
1782         olen = 2;
1783         outbuf = (char *)buf;
1784         smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
1785         if (olen == 2) {
1786                 /* We failed to convert to a 2 byte character.
1787                    See if we can convert to a 4 UTF16-LE byte char encoding.
1788                 */
1789                 olen = 4;
1790                 outbuf = (char *)buf;
1791                 smb_iconv(descriptor,  &str, &ilen, &outbuf, &olen);
1792                 if (olen == 4) {
1793                         /* We didn't convert any bytes */
1794                         *size = 1;
1795                         return INVALID_CODEPOINT;
1796                 }
1797                 olen = 4 - olen;
1798         } else {
1799                 olen = 2 - olen;
1800         }
1801
1802         *size = ilen_orig - ilen;
1803
1804         if (olen == 2) {
1805                 /* 2 byte, UTF16-LE encoded value. */
1806                 return (codepoint_t)SVAL(buf, 0);
1807         }
1808         if (olen == 4) {
1809                 /* Decode a 4 byte UTF16-LE character manually.
1810                    See RFC2871 for the encoding machanism.
1811                 */
1812                 codepoint_t w1 = SVAL(buf,0) & ~0xD800;
1813                 codepoint_t w2 = SVAL(buf,2) & ~0xDC00;
1814
1815                 return (codepoint_t)0x10000 +
1816                                 (w1 << 10) + w2;
1817         }
1818
1819         /* no other length is valid */
1820         return INVALID_CODEPOINT;
1821 }