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