I found lots of places where we assume error will be set when calling
[samba.git] / source3 / lib / util_unistr.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-2001
5    Copyright (C) Simo Sorce 2001
6    Copyright (C) Jeremy Allison 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 #ifndef MAXUNI
25 #define MAXUNI 1024
26 #endif
27
28 /* these 3 tables define the unicode case handling.  They are loaded
29    at startup either via mmap() or read() from the lib directory */
30 static smb_ucs2_t *upcase_table;
31 static smb_ucs2_t *lowcase_table;
32 static uint8 *valid_table;
33 static bool upcase_table_use_unmap;
34 static bool lowcase_table_use_unmap;
35 static bool valid_table_use_unmap;
36
37 /**
38  * Destroy global objects allocated by load_case_tables()
39  **/
40 void gfree_case_tables(void)
41 {
42         if ( upcase_table ) {
43                 if ( upcase_table_use_unmap )
44                         unmap_file(upcase_table, 0x20000);
45                 else
46                         SAFE_FREE(upcase_table);
47         }
48
49         if ( lowcase_table ) {
50                 if ( lowcase_table_use_unmap )
51                         unmap_file(lowcase_table, 0x20000);
52                 else
53                         SAFE_FREE(lowcase_table);
54         }
55
56         if ( valid_table ) {
57                 if ( valid_table_use_unmap )
58                         unmap_file(valid_table, 0x10000);
59                 else
60                         SAFE_FREE(valid_table);
61         }
62 }
63
64 /**
65  * Load or generate the case handling tables.
66  *
67  * The case tables are defined in UCS2 and don't depend on any
68  * configured parameters, so they never need to be reloaded.
69  **/
70
71 void load_case_tables(void)
72 {
73         static int initialised;
74         char *old_locale = NULL, *saved_locale = NULL;
75         int i;
76         TALLOC_CTX *frame = NULL;
77
78         if (initialised) {
79                 return;
80         }
81         initialised = 1;
82
83         frame = talloc_stackframe();
84
85         upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"),
86                                               0x20000);
87         upcase_table_use_unmap = ( upcase_table != NULL );
88
89         lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"),
90                                                0x20000);
91         lowcase_table_use_unmap = ( lowcase_table != NULL );
92
93 #ifdef HAVE_SETLOCALE
94         /* Get the name of the current locale.  */
95         old_locale = setlocale(LC_ALL, NULL);
96
97         if (old_locale) {
98                 /* Save it as it is in static storage. */
99                 saved_locale = SMB_STRDUP(old_locale);
100         }
101
102         /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
103         setlocale(LC_ALL, "C");
104 #endif
105
106         /* we would like Samba to limp along even if these tables are
107            not available */
108         if (!upcase_table) {
109                 DEBUG(1,("creating lame upcase table\n"));
110                 upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
111                 for (i=0;i<0x10000;i++) {
112                         smb_ucs2_t v;
113                         SSVAL(&v, 0, i);
114                         upcase_table[v] = i;
115                 }
116                 for (i=0;i<256;i++) {
117                         smb_ucs2_t v;
118                         SSVAL(&v, 0, UCS2_CHAR(i));
119                         upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
120                 }
121         }
122
123         if (!lowcase_table) {
124                 DEBUG(1,("creating lame lowcase table\n"));
125                 lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
126                 for (i=0;i<0x10000;i++) {
127                         smb_ucs2_t v;
128                         SSVAL(&v, 0, i);
129                         lowcase_table[v] = i;
130                 }
131                 for (i=0;i<256;i++) {
132                         smb_ucs2_t v;
133                         SSVAL(&v, 0, UCS2_CHAR(i));
134                         lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
135                 }
136         }
137
138 #ifdef HAVE_SETLOCALE
139         /* Restore the old locale. */
140         if (saved_locale) {
141                 setlocale (LC_ALL, saved_locale);
142                 SAFE_FREE(saved_locale);
143         }
144 #endif
145         TALLOC_FREE(frame);
146 }
147
148 static int check_dos_char_slowly(smb_ucs2_t c)
149 {
150         char buf[10];
151         smb_ucs2_t c2 = 0;
152         int len1, len2;
153
154         len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
155         if (len1 == 0) {
156                 return 0;
157         }
158         len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
159         if (len2 != 2) {
160                 return 0;
161         }
162         return (c == c2);
163 }
164
165 /**
166  * Load the valid character map table from <tt>valid.dat</tt> or
167  * create from the configured codepage.
168  *
169  * This function is called whenever the configuration is reloaded.
170  * However, the valid character table is not changed if it's loaded
171  * from a file, because we can't unmap files.
172  **/
173
174 void init_valid_table(void)
175 {
176         static int mapped_file;
177         int i;
178         const char *allowed = ".!#$%&'()_-@^`~";
179         uint8 *valid_file;
180
181         if (mapped_file) {
182                 /* Can't unmap files, so stick with what we have */
183                 return;
184         }
185
186         valid_file = (uint8 *)map_file(data_path("valid.dat"), 0x10000);
187         if (valid_file) {
188                 valid_table = valid_file;
189                 mapped_file = 1;
190                 valid_table_use_unmap = True;
191                 return;
192         }
193
194         /* Otherwise, we're using a dynamically created valid_table.
195          * It might need to be regenerated if the code page changed.
196          * We know that we're not using a mapped file, so we can
197          * free() the old one. */
198         SAFE_FREE(valid_table);
199
200         /* use free rather than unmap */
201         valid_table_use_unmap = False;
202
203         DEBUG(2,("creating default valid table\n"));
204         valid_table = (uint8 *)SMB_MALLOC(0x10000);
205         SMB_ASSERT(valid_table != NULL);
206         for (i=0;i<128;i++) {
207                 valid_table[i] = isalnum(i) || strchr(allowed,i);
208         }
209
210         lazy_initialize_conv();
211
212         for (;i<0x10000;i++) {
213                 smb_ucs2_t c;
214                 SSVAL(&c, 0, i);
215                 valid_table[i] = check_dos_char_slowly(c);
216         }
217 }
218
219 /*******************************************************************
220  Write a string in (little-endian) unicode format. src is in
221  the current DOS codepage. len is the length in bytes of the
222  string pointed to by dst.
223
224  if null_terminate is True then null terminate the packet (adds 2 bytes)
225
226  the return value is the length in bytes consumed by the string, including the
227  null termination if applied
228 ********************************************************************/
229
230 size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
231 {
232         int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
233                                    : STR_UNICODE|STR_NOALIGN;
234         return push_ucs2(NULL, dst, src, len, flags);
235 }
236
237
238 /*******************************************************************
239  Skip past a unicode string, but not more than len. Always move
240  past a terminating zero if found.
241 ********************************************************************/
242
243 char *skip_unibuf(char *src, size_t len)
244 {
245         char *srcend = src + len;
246
247         while (src < srcend && SVAL(src,0)) {
248                 src += 2;
249         }
250
251         if(!SVAL(src,0)) {
252                 src += 2;
253         }
254
255         return src;
256 }
257
258 /* Copy a string from little-endian or big-endian unicode source (depending
259  * on flags) to internal samba format destination
260  */ 
261
262 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
263 {
264         if (!src) {
265                 dest[0] = 0;
266                 return 0;
267         }
268         if(dest_len==-1) {
269                 dest_len=MAXUNI-3;
270         }
271         return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
272 }
273
274 /* Copy a string from little-endian or big-endian unicode source (depending
275  * on flags) to internal samba format destination. Allocates on talloc ctx.
276  */
277
278 int rpcstr_pull_talloc(TALLOC_CTX *ctx,
279                         char **dest,
280                         void *src,
281                         int src_len,
282                         int flags)
283 {
284         return pull_ucs2_base_talloc(ctx,
285                         NULL,
286                         dest,
287                         src,
288                         src_len,
289                         flags|STR_UNICODE|STR_NOALIGN);
290
291 }
292
293 /* Copy a string from a unistr2 source to internal samba format
294    destination.  Use this instead of direct calls to rpcstr_pull() to avoid
295    having to determine whether the source string is null terminated. */
296
297 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
298 {
299         return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
300                          src->uni_str_len * 2, 0);
301 }
302
303 /* Helper function to return a talloc'ed string. I have implemented it with a
304  * copy because I don't really know how pull_ucs2 and friends calculate the
305  * target size. If this turns out to be a major bottleneck someone with deeper
306  * multi-byte knowledge needs to revisit this.
307  * I just did (JRA :-). No longer uses copy.
308  * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would
309  * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c.
310  */
311
312 char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *ctx, const UNISTR2 *src)
313 {
314         char *dest = NULL;
315         size_t dest_len;
316
317         if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src->buffer,
318                                    src->uni_str_len * 2, (void *)&dest,
319                                    &dest_len, true))
320         {
321                 return NULL;
322         }
323
324         /* Ensure we're returning a null terminated string. */
325         if (dest_len) {
326                 /* Did we already process the terminating zero ? */
327                 if (dest[dest_len-1] != 0) {
328                         size_t size = talloc_get_size(dest);
329                         /* Have we got space to append the '\0' ? */
330                         if (size <= dest_len) {
331                                 /* No, realloc. */
332                                 dest = TALLOC_REALLOC_ARRAY(ctx, dest, char,
333                                                 dest_len+1);
334                                 if (!dest) {
335                                         /* talloc fail. */
336                                         dest_len = (size_t)-1;
337                                         return NULL;
338                                 }
339                         }
340                         /* Yay - space ! */
341                         dest[dest_len] = '\0';
342                         dest_len++;
343                 }
344         } else if (dest) {
345                 dest[0] = 0;
346         }
347
348         return dest;
349 }
350
351 /* Converts a string from internal samba format to unicode
352  */
353
354 int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags)
355 {
356         return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
357 }
358
359 /* Converts a string from internal samba format to unicode. Always terminates.
360  * Actually just a wrapper round push_ucs2_talloc().
361  */
362
363 int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
364 {
365         size_t size;
366         if (push_ucs2_talloc(ctx, dest, src, &size))
367                 return size;
368         else
369                 return -1;
370 }
371
372 /*******************************************************************
373  Convert a (little-endian) UNISTR2 structure to an ASCII string.
374 ********************************************************************/
375
376 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
377 {
378         if ((str == NULL) || (str->uni_str_len == 0)) {
379                 *dest='\0';
380                 return;
381         }
382         pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
383 }
384
385 #if 0
386 /*******************************************************************
387  Convert a (little-endian) UNISTR3 structure to an ASCII string.
388 ********************************************************************/
389
390 void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen)
391 {
392         if ((str == NULL) || (str->uni_str_len == 0)) {
393                 *dest='\0';
394                 return;
395         }
396         pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2,
397                   STR_NOALIGN);
398 }
399 #endif
400
401 /*******************************************************************
402  Duplicate a UNISTR2 string into a null terminated char*
403  using a talloc context.
404 ********************************************************************/
405
406 char *unistr2_to_ascii_talloc(TALLOC_CTX *ctx, const UNISTR2 *str)
407 {
408         char *s = NULL;
409
410         if (!str || !str->buffer) {
411                 return NULL;
412         }
413         if (pull_ucs2_base_talloc(ctx,
414                                 NULL,
415                                 &s,
416                                 str->buffer,
417                                 str->uni_str_len*2,
418                                 STR_NOALIGN) == (size_t)-1) {
419                 return NULL;
420         }
421         return s;
422 }
423
424 /*******************************************************************
425  Return a string for displaying a UNISTR2. Guarentees to return a
426  valid string - "" if nothing else.
427  Changed to use talloc_tos() under the covers.... JRA.
428 ********************************************************************/
429
430 const char *unistr2_static(const UNISTR2 *str)
431 {
432         char *dest = NULL;
433
434         if ((str == NULL) || (str->uni_str_len == 0)) {
435                 return "";
436         }
437
438         dest = unistr2_to_ascii_talloc(talloc_tos(), str);
439         if (!dest) {
440                 return "";
441         }
442
443         return dest;
444 }
445
446 /*******************************************************************
447  Convert a wchar to upper case.
448 ********************************************************************/
449
450 smb_ucs2_t toupper_w(smb_ucs2_t val)
451 {
452         return upcase_table[SVAL(&val,0)];
453 }
454
455 /*******************************************************************
456  Convert a wchar to lower case.
457 ********************************************************************/
458
459 smb_ucs2_t tolower_w( smb_ucs2_t val )
460 {
461         return lowcase_table[SVAL(&val,0)];
462 }
463
464 /*******************************************************************
465  Determine if a character is lowercase.
466 ********************************************************************/
467
468 bool islower_w(smb_ucs2_t c)
469 {
470         return upcase_table[SVAL(&c,0)] != c;
471 }
472
473 /*******************************************************************
474  Determine if a character is uppercase.
475 ********************************************************************/
476
477 bool isupper_w(smb_ucs2_t c)
478 {
479         return lowcase_table[SVAL(&c,0)] != c;
480 }
481
482 /*******************************************************************
483  Determine if a character is valid in a 8.3 name.
484 ********************************************************************/
485
486 bool isvalid83_w(smb_ucs2_t c)
487 {
488         return valid_table[SVAL(&c,0)] != 0;
489 }
490
491 /*******************************************************************
492  Count the number of characters in a smb_ucs2_t string.
493 ********************************************************************/
494
495 size_t strlen_w(const smb_ucs2_t *src)
496 {
497         size_t len;
498         smb_ucs2_t c;
499
500         for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
501                 ;
502         }
503
504         return len;
505 }
506
507 /*******************************************************************
508  Count up to max number of characters in a smb_ucs2_t string.
509 ********************************************************************/
510
511 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
512 {
513         size_t len;
514         smb_ucs2_t c;
515
516         for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
517                 ;
518         }
519
520         return len;
521 }
522
523 /*******************************************************************
524  Wide strchr().
525 ********************************************************************/
526
527 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
528 {
529         smb_ucs2_t cp;
530         while (*(COPY_UCS2_CHAR(&cp,s))) {
531                 if (c == cp) {
532                         return (smb_ucs2_t *)s;
533                 }
534                 s++;
535         }
536         if (c == cp) {
537                 return (smb_ucs2_t *)s;
538         }
539
540         return NULL;
541 }
542
543 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
544 {
545         return strchr_w(s, UCS2_CHAR(c));
546 }
547
548 /*******************************************************************
549  Wide strrchr().
550 ********************************************************************/
551
552 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
553 {
554         smb_ucs2_t cp;
555         const smb_ucs2_t *p = s;
556         int len = strlen_w(s);
557
558         if (len == 0) {
559                 return NULL;
560         }
561         p += (len - 1);
562         do {
563                 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
564                         return (smb_ucs2_t *)p;
565                 }
566         } while (p-- != s);
567         return NULL;
568 }
569
570 /*******************************************************************
571  Wide version of strrchr that returns after doing strrchr 'n' times.
572 ********************************************************************/
573
574 smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
575 {
576         smb_ucs2_t cp;
577         const smb_ucs2_t *p = s;
578         int len = strlen_w(s);
579
580         if (len == 0 || !n) {
581                 return NULL;
582         }
583         p += (len - 1);
584         do {
585                 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
586                         n--;
587                 }
588
589                 if (!n) {
590                         return (smb_ucs2_t *)p;
591                 }
592         } while (p-- != s);
593         return NULL;
594 }
595
596 /*******************************************************************
597  Wide strstr().
598 ********************************************************************/
599
600 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
601 {
602         smb_ucs2_t *r;
603         size_t inslen;
604
605         if (!s || !*s || !ins || !*ins) {
606                 return NULL;
607         }
608
609         inslen = strlen_w(ins);
610         r = (smb_ucs2_t *)s;
611
612         while ((r = strchr_w(r, *ins))) {
613                 if (strncmp_w(r, ins, inslen) == 0) {
614                         return r;
615                 }
616                 r++;
617         }
618
619         return NULL;
620 }
621
622 /*******************************************************************
623  Convert a string to lower case.
624  return True if any char is converted
625 ********************************************************************/
626
627 bool strlower_w(smb_ucs2_t *s)
628 {
629         smb_ucs2_t cp;
630         bool ret = False;
631
632         while (*(COPY_UCS2_CHAR(&cp,s))) {
633                 smb_ucs2_t v = tolower_w(cp);
634                 if (v != cp) {
635                         COPY_UCS2_CHAR(s,&v);
636                         ret = True;
637                 }
638                 s++;
639         }
640         return ret;
641 }
642
643 /*******************************************************************
644  Convert a string to upper case.
645  return True if any char is converted
646 ********************************************************************/
647
648 bool strupper_w(smb_ucs2_t *s)
649 {
650         smb_ucs2_t cp;
651         bool ret = False;
652         while (*(COPY_UCS2_CHAR(&cp,s))) {
653                 smb_ucs2_t v = toupper_w(cp);
654                 if (v != cp) {
655                         COPY_UCS2_CHAR(s,&v);
656                         ret = True;
657                 }
658                 s++;
659         }
660         return ret;
661 }
662
663 /*******************************************************************
664  Convert a string to "normal" form.
665 ********************************************************************/
666
667 void strnorm_w(smb_ucs2_t *s, int case_default)
668 {
669         if (case_default == CASE_UPPER) {
670                 strupper_w(s);
671         } else {
672                 strlower_w(s);
673         }
674 }
675
676 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
677 {
678         smb_ucs2_t cpa, cpb;
679
680         while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
681                 a++;
682                 b++;
683         }
684         return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
685         /* warning: if *a != *b and both are not 0 we return a random
686                 greater or lesser than 0 number not realted to which
687                 string is longer */
688 }
689
690 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
691 {
692         smb_ucs2_t cpa, cpb;
693         size_t n = 0;
694
695         while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
696                 a++;
697                 b++;
698                 n++;
699         }
700         return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
701 }
702
703 /*******************************************************************
704  Case insensitive string comparison.
705 ********************************************************************/
706
707 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
708 {
709         smb_ucs2_t cpa, cpb;
710
711         while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
712                 a++;
713                 b++;
714         }
715         return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
716 }
717
718 /*******************************************************************
719  Case insensitive string comparison, length limited.
720 ********************************************************************/
721
722 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
723 {
724         smb_ucs2_t cpa, cpb;
725         size_t n = 0;
726
727         while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
728                 a++;
729                 b++;
730                 n++;
731         }
732         return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
733 }
734
735 /*******************************************************************
736  Compare 2 strings.
737 ********************************************************************/
738
739 bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
740 {
741         if (s1 == s2) {
742                 return(True);
743         }
744         if (!s1 || !s2) {
745                 return(False);
746         }
747   
748         return(strcasecmp_w(s1,s2)==0);
749 }
750
751 /*******************************************************************
752  Compare 2 strings up to and including the nth char.
753 ******************************************************************/
754
755 bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
756 {
757         if (s1 == s2) {
758                 return(True);
759         }
760         if (!s1 || !s2 || !n) {
761                 return(False);
762         }
763   
764         return(strncasecmp_w(s1,s2,n)==0);
765 }
766
767 /*******************************************************************
768  Duplicate string.
769 ********************************************************************/
770
771 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
772 {
773         return strndup_w(src, 0);
774 }
775
776 /* if len == 0 then duplicate the whole string */
777
778 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
779 {
780         smb_ucs2_t *dest;
781         
782         if (!len) {
783                 len = strlen_w(src);
784         }
785         dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
786         if (!dest) {
787                 DEBUG(0,("strdup_w: out of memory!\n"));
788                 return NULL;
789         }
790
791         memcpy(dest, src, len * sizeof(smb_ucs2_t));
792         dest[len] = 0;
793         return dest;
794 }
795
796 /*******************************************************************
797  Copy a string with max len.
798 ********************************************************************/
799
800 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
801 {
802         smb_ucs2_t cp;
803         size_t len;
804         
805         if (!dest || !src) {
806                 return NULL;
807         }
808         
809         for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
810                 cp = *COPY_UCS2_CHAR(dest+len,src+len);
811         }
812         cp = 0;
813         for ( /*nothing*/ ; len < max; len++ ) {
814                 cp = *COPY_UCS2_CHAR(dest+len,&cp);
815         }
816         
817         return dest;
818 }
819
820 /*******************************************************************
821  Append a string of len bytes and add a terminator.
822 ********************************************************************/
823
824 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
825 {       
826         size_t start;
827         size_t len;     
828         smb_ucs2_t z = 0;
829
830         if (!dest || !src) {
831                 return NULL;
832         }
833         
834         start = strlen_w(dest);
835         len = strnlen_w(src, max);
836
837         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
838         z = *COPY_UCS2_CHAR(dest+start+len,&z);
839
840         return dest;
841 }
842
843 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
844 {       
845         size_t start;
846         size_t len;     
847         smb_ucs2_t z = 0;
848         
849         if (!dest || !src) {
850                 return NULL;
851         }
852         
853         start = strlen_w(dest);
854         len = strlen_w(src);
855
856         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
857         z = *COPY_UCS2_CHAR(dest+start+len,&z);
858         
859         return dest;
860 }
861
862
863 /*******************************************************************
864  Replace any occurence of oldc with newc in unicode string.
865 ********************************************************************/
866
867 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
868 {
869         smb_ucs2_t cp;
870
871         for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
872                 if(cp==oldc) {
873                         COPY_UCS2_CHAR(s,&newc);
874                 }
875         }
876 }
877
878 /*******************************************************************
879  Trim unicode string.
880 ********************************************************************/
881
882 bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
883                                   const smb_ucs2_t *back)
884 {
885         bool ret = False;
886         size_t len, front_len, back_len;
887
888         if (!s) {
889                 return False;
890         }
891
892         len = strlen_w(s);
893
894         if (front && *front) {
895                 front_len = strlen_w(front);
896                 while (len && strncmp_w(s, front, front_len) == 0) {
897                         memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
898                         len -= front_len;
899                         ret = True;
900                 }
901         }
902         
903         if (back && *back) {
904                 back_len = strlen_w(back);
905                 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
906                         s[len - back_len] = 0;
907                         len -= back_len;
908                         ret = True;
909                 }
910         }
911
912         return ret;
913 }
914
915 /*
916   The *_wa() functions take a combination of 7 bit ascii
917   and wide characters They are used so that you can use string
918   functions combining C string constants with ucs2 strings
919
920   The char* arguments must NOT be multibyte - to be completely sure
921   of this only pass string constants */
922
923 int strcmp_wa(const smb_ucs2_t *a, const char *b)
924 {
925         smb_ucs2_t cp = 0;
926
927         while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
928                 a++;
929                 b++;
930         }
931         return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
932 }
933
934 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
935 {
936         smb_ucs2_t cp = 0;
937         size_t n = 0;
938
939         while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
940                 a++;
941                 b++;
942                 n++;
943         }
944         return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
945 }
946
947 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
948 {
949         smb_ucs2_t cp;
950
951         while (*(COPY_UCS2_CHAR(&cp,s))) {
952                 int i;
953                 for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++) 
954                         ;
955                 if (p[i]) {
956                         return (smb_ucs2_t *)s;
957                 }
958                 s++;
959         }
960         return NULL;
961 }
962
963 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
964 {
965         smb_ucs2_t *r;
966         size_t inslen;
967
968         if (!s || !ins) { 
969                 return NULL;
970         }
971
972         inslen = strlen(ins);
973         r = (smb_ucs2_t *)s;
974
975         while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
976                 if (strncmp_wa(r, ins, inslen) == 0) 
977                         return r;
978                 r++;
979         }
980
981         return NULL;
982 }
983
984 /*******************************************************************
985  Returns the length in number of wide characters.
986 ******************************************************************/
987
988 int unistrlen(uint16 *s)
989 {
990         int len;
991
992         if (!s) {
993                 return -1;
994         }
995
996         for (len=0; SVAL(s,0); s++,len++) {
997                 ;
998         }
999
1000         return len;
1001 }
1002
1003 /*******************************************************************
1004  Strcpy for unicode strings. Returns length (in num of wide chars).
1005  Not odd align safe.
1006 ********************************************************************/
1007
1008 int unistrcpy(uint16 *dst, uint16 *src)
1009 {
1010         int num_wchars = 0;
1011
1012         while (SVAL(src,0)) {
1013                 *dst++ = *src++;
1014                 num_wchars++;
1015         }
1016         *dst = 0;
1017
1018         return num_wchars;
1019 }
1020
1021 /**
1022  * Samba ucs2 type to UNISTR2 conversion
1023  *
1024  * @param ctx Talloc context to create the dst strcture (if null) and the 
1025  *            contents of the unicode string.
1026  * @param dst UNISTR2 destination. If equals null, then it's allocated.
1027  * @param src smb_ucs2_t source.
1028  * @param max_len maximum number of unicode characters to copy. If equals
1029  *        null, then null-termination of src is taken
1030  *
1031  * @return copied UNISTR2 destination
1032  **/
1033
1034 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
1035 {
1036         size_t len;
1037
1038         if (!src) {
1039                 return NULL;
1040         }
1041
1042         len = strlen_w(src);
1043         
1044         /* allocate UNISTR2 destination if not given */
1045         if (!dst) {
1046                 dst = TALLOC_P(ctx, UNISTR2);
1047                 if (!dst)
1048                         return NULL;
1049         }
1050         if (!dst->buffer) {
1051                 dst->buffer = TALLOC_ARRAY(ctx, uint16, len + 1);
1052                 if (!dst->buffer)
1053                         return NULL;
1054         }
1055         
1056         /* set UNISTR2 parameters */
1057         dst->uni_max_len = len + 1;
1058         dst->offset = 0;
1059         dst->uni_str_len = len;
1060         
1061         /* copy the actual unicode string */
1062         strncpy_w(dst->buffer, src, dst->uni_max_len);
1063         
1064         return dst;
1065 }
1066
1067 /*************************************************************
1068  ascii only toupper - saves the need for smbd to be in C locale.
1069 *************************************************************/
1070
1071 int toupper_ascii(int c)
1072 {
1073         smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
1074         return UCS2_TO_CHAR(uc);
1075 }
1076
1077 /*************************************************************
1078  ascii only tolower - saves the need for smbd to be in C locale.
1079 *************************************************************/
1080
1081 int tolower_ascii(int c)
1082 {
1083         smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
1084         return UCS2_TO_CHAR(uc);
1085 }
1086
1087 /*************************************************************
1088  ascii only isupper - saves the need for smbd to be in C locale.
1089 *************************************************************/
1090
1091 int isupper_ascii(int c)
1092 {
1093         return isupper_w(UCS2_CHAR(c));
1094 }
1095
1096 /*************************************************************
1097  ascii only islower - saves the need for smbd to be in C locale.
1098 *************************************************************/
1099
1100 int islower_ascii(int c)
1101 {
1102         return islower_w(UCS2_CHAR(c));
1103 }