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