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