This commit was generated by cvs2svn to compensate for changes in r30,
[kai/samba-autobuild/.git] / source4 / 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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
34
35 /*******************************************************************
36 load the case handling tables
37 ********************************************************************/
38 void load_case_tables(void)
39 {
40         static int initialised;
41         int i;
42         TALLOC_CTX *mem_ctx;
43
44         if (initialised) return;
45         initialised = 1;
46
47         mem_ctx = talloc_init("load_case_tables");
48         if (!mem_ctx) {
49                 smb_panic("No memory for case_tables");
50         }
51         upcase_table = map_file(lib_path(mem_ctx, "upcase.dat"), 0x20000);
52         lowcase_table = map_file(lib_path(mem_ctx, "lowcase.dat"), 0x20000);
53         talloc_destroy(mem_ctx);
54         
55         /* we would like Samba to limp along even if these tables are
56            not available */
57         if (!upcase_table) {
58                 DEBUG(1,("creating lame upcase table\n"));
59                 upcase_table = malloc(0x20000);
60                 if (!upcase_table) {
61                         smb_panic("No memory for upcase tables");
62                 }
63                 for (i=0;i<0x10000;i++) {
64                         smb_ucs2_t v;
65                         SSVAL(&v, 0, i);
66                         upcase_table[v] = i;
67                 }
68                 for (i=0;i<256;i++) {
69                         smb_ucs2_t v;
70                         SSVAL(&v, 0, UCS2_CHAR(i));
71                         upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
72                 }
73         }
74
75         if (!lowcase_table) {
76                 DEBUG(1,("creating lame lowcase table\n"));
77                 lowcase_table = malloc(0x20000);
78                 if (!lowcase_table) {
79                         smb_panic("No memory for lowcase tables");
80                 }
81                 for (i=0;i<0x10000;i++) {
82                         smb_ucs2_t v;
83                         SSVAL(&v, 0, i);
84                         lowcase_table[v] = i;
85                 }
86                 for (i=0;i<256;i++) {
87                         smb_ucs2_t v;
88                         SSVAL(&v, 0, UCS2_CHAR(i));
89                         lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
90                 }
91         }
92 }
93
94 /*
95   see if a ucs2 character can be mapped correctly to a dos character
96   and mapped back to the same character in ucs2
97 */
98 static int check_dos_char(smb_ucs2_t c)
99 {
100         char buf[10];
101         smb_ucs2_t c2 = 0;
102         int len1, len2;
103         len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf));
104         if (len1 == 0) return 0;
105         len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2);
106         if (len2 != 2) return 0;
107         return (c == c2);
108 }
109
110 /**
111  * Load the valid character map table from <tt>valid.dat</tt> or
112  * create from the configured codepage.
113  *
114  * This function is called whenever the configuration is reloaded.
115  * However, the valid character table is not changed if it's loaded
116  * from a file, because we can't unmap files.
117  **/
118 void init_valid_table(void)
119 {
120         static int mapped_file;
121         int i;
122         const char *allowed = ".!#$%&'()_-@^`~";
123         uint8 *valid_file;
124         TALLOC_CTX *mem_ctx;
125
126         if (mapped_file) {
127                 /* Can't unmap files, so stick with what we have */
128                 return;
129         }
130
131         mem_ctx = talloc_init("init_valid_table");
132         if (!mem_ctx) {
133                 smb_panic("No memory for valid_table");
134         }
135         valid_file = map_file(lib_path(mem_ctx, "valid.dat"), 0x10000);
136         talloc_destroy(mem_ctx);
137         if (valid_file) {
138                 valid_table = valid_file;
139                 mapped_file = 1;
140                 return;
141         }
142
143         /* Otherwise, we're using a dynamically created valid_table.
144          * It might need to be regenerated if the code page changed.
145          * We know that we're not using a mapped file, so we can
146          * free() the old one. */
147         if (valid_table) free(valid_table);
148
149         DEBUG(2,("creating default valid table\n"));
150         valid_table = malloc(0x10000);
151         if (!valid_table) {
152                 smb_panic("No memory for valid_table");
153         }
154         for (i=0;i<128;i++)
155                 valid_table[i] = isalnum(i) || strchr(allowed,i);
156         
157         for (;i<0x10000;i++) {
158                 smb_ucs2_t c;
159                 SSVAL(&c, 0, i);
160                 valid_table[i] = check_dos_char(c);
161         }
162 }
163
164
165
166 /*******************************************************************
167  Write a string in (little-endian) unicode format. src is in
168  the current DOS codepage. len is the length in bytes of the
169  string pointed to by dst.
170
171  if null_terminate is True then null terminate the packet (adds 2 bytes)
172
173  the return value is the length in bytes consumed by the string, including the
174  null termination if applied
175 ********************************************************************/
176
177 size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
178 {
179         return push_ucs2(NULL, dst, src, len, 
180                          STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
181 }
182
183
184 /*******************************************************************
185  Skip past a unicode string, but not more than len. Always move
186  past a terminating zero if found.
187 ********************************************************************/
188
189 char *skip_unibuf(char *src, size_t len)
190 {
191     char *srcend = src + len;
192
193     while (src < srcend && SVAL(src,0))
194         src += 2;
195
196     if(!SVAL(src,0))
197         src += 2;
198
199     return src;
200 }
201
202 /* Copy a string from little-endian or big-endian unicode source (depending
203  * on flags) to internal samba format destination
204  */ 
205 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
206 {
207         if (!src) return 0;
208         if(dest_len==-1) dest_len=MAXUNI-3;
209         return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
210 }
211
212 /* Copy a string from a unistr2 source to internal samba format
213    destination.  Use this instead of direct calls to rpcstr_pull() to avoid
214    having to determine whether the source string is null terminated. */
215
216 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
217 {
218         return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
219                          src->uni_str_len * 2, 0);
220 }
221
222 /* Converts a string from internal samba format to unicode
223  */ 
224 int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
225 {
226         return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
227 }
228
229 /*******************************************************************
230  Convert a (little-endian) UNISTR2 structure to an ASCII string
231 ********************************************************************/
232 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
233 {
234         if (str == NULL) {
235                 *dest='\0';
236                 return;
237         }
238         pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
239 }
240
241 /*******************************************************************
242 give a static string for displaying a UNISTR2
243 ********************************************************************/
244 const char *unistr2_static(TALLOC_CTX *mem_ctx, const UNISTR2 *str)
245 {
246         pstring ret;
247         unistr2_to_ascii(ret, str, sizeof(ret));
248         return talloc_strdup(mem_ctx, ret);
249 }
250
251
252 /*******************************************************************
253  duplicate a UNISTR2 string into a null terminated char*
254  using a talloc context
255 ********************************************************************/
256 char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
257 {
258         char *s;
259         int maxlen = (str->uni_str_len+1)*4;
260         if (!str->buffer) return NULL;
261         s = (char *)talloc(ctx, maxlen); /* convervative */
262         if (!s) return NULL;
263         pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, 
264                   STR_NOALIGN);
265         return s;
266 }
267
268
269 /*******************************************************************
270 Return a number stored in a buffer
271 ********************************************************************/
272
273 uint32 buffer2_to_uint32(BUFFER2 *str)
274 {
275         if (str->buf_len == 4)
276                 return IVAL(str->buffer, 0);
277         else
278                 return 0;
279 }
280
281 /*******************************************************************
282  Convert a wchar to upper case.
283 ********************************************************************/
284
285 smb_ucs2_t toupper_w(smb_ucs2_t val)
286 {
287         return upcase_table[SVAL(&val,0)];
288 }
289
290 /*******************************************************************
291  Convert a wchar to lower case.
292 ********************************************************************/
293
294 smb_ucs2_t tolower_w( smb_ucs2_t val )
295 {
296         return lowcase_table[SVAL(&val,0)];
297
298 }
299
300 /*******************************************************************
301 determine if a character is lowercase
302 ********************************************************************/
303 BOOL islower_w(smb_ucs2_t c)
304 {
305         return upcase_table[SVAL(&c,0)] != c;
306 }
307
308 /*******************************************************************
309 determine if a character is uppercase
310 ********************************************************************/
311 BOOL isupper_w(smb_ucs2_t c)
312 {
313         return lowcase_table[SVAL(&c,0)] != c;
314 }
315
316
317 /*******************************************************************
318 determine if a character is valid in a 8.3 name
319 ********************************************************************/
320 BOOL isvalid83_w(smb_ucs2_t c)
321 {
322         return valid_table[SVAL(&c,0)] != 0;
323 }
324
325 /*******************************************************************
326  Count the number of characters in a smb_ucs2_t string.
327 ********************************************************************/
328 size_t strlen_w(const smb_ucs2_t *src)
329 {
330         size_t len;
331
332         for (len = 0; SVAL(src,0); len++, src++) ;
333
334         return len;
335 }
336
337 /*******************************************************************
338  Count up to max number of characters in a smb_ucs2_t string.
339 ********************************************************************/
340 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
341 {
342         size_t len;
343
344         for (len = 0; (len < max) && SVAL(src, 0); len++, src++) ;
345
346         return len;
347 }
348
349 /*******************************************************************
350 wide strchr()
351 ********************************************************************/
352 const smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
353 {
354         while (*s != 0) {
355                 if (c == *s) return s;
356                 s++;
357         }
358         if (c == *s) return s;
359
360         return NULL;
361 }
362
363 const smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
364 {
365         return strchr_w(s, UCS2_CHAR(c));
366 }
367
368 const smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
369 {
370         const smb_ucs2_t *p = s;
371         int len = strlen_w(s);
372         if (len == 0) return NULL;
373         p += (len - 1);
374         do {
375                 if (c == *p) return p;
376         } while (p-- != s);
377         return NULL;
378 }
379
380 /*******************************************************************
381 wide strstr()
382 ********************************************************************/
383 const smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
384 {
385         const smb_ucs2_t *r;
386         size_t slen, inslen;
387
388         if (!s || !*s || !ins || !*ins) return NULL;
389         slen = strlen_w(s);
390         inslen = strlen_w(ins);
391         r = s;
392         while ((r = strchr_w(r, *ins))) {
393                 if (strncmp_w(r, ins, inslen) == 0) return r;
394                 r++;
395         }
396         return NULL;
397 }
398
399 /*******************************************************************
400  Convert a string to lower case.
401  return True if any char is converted
402 ********************************************************************/
403 BOOL strlower_w(smb_ucs2_t *s)
404 {
405         BOOL ret = False;
406         while (*s) {
407                 smb_ucs2_t v = tolower_w(*s);
408                 if (v != *s) {
409                         *s = v;
410                         ret = True;
411                 }
412                 s++;
413         }
414         return ret;
415 }
416
417 /*******************************************************************
418  Convert a string to upper case.
419  return True if any char is converted
420 ********************************************************************/
421 BOOL strupper_w(smb_ucs2_t *s)
422 {
423         BOOL ret = False;
424         while (*s) {
425                 smb_ucs2_t v = toupper_w(*s);
426                 if (v != *s) {
427                         *s = v;
428                         ret = True;
429                 }
430                 s++;
431         }
432         return ret;
433 }
434
435 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
436 {
437         while (*b && *a == *b) { a++; b++; }
438         return (*a - *b);
439         /* warning: if *a != *b and both are not 0 we retrun a random
440                 greater or lesser than 0 number not realted to which
441                 string is longer */
442 }
443
444 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
445 {
446         size_t n = 0;
447         while ((n < len) && *b && *a == *b) { a++; b++; n++;}
448         return (len - n)?(*a - *b):0;   
449 }
450
451 /*******************************************************************
452 case insensitive string comparison
453 ********************************************************************/
454 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
455 {
456         while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
457         return (tolower_w(*a) - tolower_w(*b));
458 }
459
460 /*******************************************************************
461 case insensitive string comparison, lenght limited
462 ********************************************************************/
463 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
464 {
465         size_t n = 0;
466         while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
467         return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
468 }
469
470 /*******************************************************************
471   compare 2 strings 
472 ********************************************************************/
473 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
474 {
475         if (s1 == s2) return(True);
476         if (!s1 || !s2) return(False);
477   
478         return(strcasecmp_w(s1,s2)==0);
479 }
480
481 /*******************************************************************
482   compare 2 strings up to and including the nth char.
483   ******************************************************************/
484 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
485 {
486   if (s1 == s2) return(True);
487   if (!s1 || !s2 || !n) return(False);
488   
489   return(strncasecmp_w(s1,s2,n)==0);
490 }
491
492 /*******************************************************************
493 duplicate string
494 ********************************************************************/
495 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
496 {
497         return strndup_w(src, 0);
498 }
499
500 /* if len == 0 then duplicate the whole string */
501 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
502 {
503         smb_ucs2_t *dest;
504         
505         if (!len) len = strlen_w(src);
506         dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
507         if (!dest) {
508                 DEBUG(0,("strdup_w: out of memory!\n"));
509                 return NULL;
510         }
511
512         memcpy(dest, src, len * sizeof(smb_ucs2_t));
513         dest[len] = 0;
514         
515         return dest;
516 }
517
518 /*******************************************************************
519 copy a string with max len
520 ********************************************************************/
521
522 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
523 {
524         size_t len;
525         
526         if (!dest || !src) return NULL;
527         
528         for (len = 0; (src[len] != 0) && (len < max); len++)
529                 dest[len] = src[len];
530         while (len < max)
531                 dest[len++] = 0;
532         
533         return dest;
534 }
535
536
537 /*******************************************************************
538 append a string of len bytes and add a terminator
539 ********************************************************************/
540
541 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
542 {       
543         size_t start;
544         size_t len;     
545         
546         if (!dest || !src) return NULL;
547         
548         start = strlen_w(dest);
549         len = strnlen_w(src, max);
550
551         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
552         dest[start+len] = 0;
553         
554         return dest;
555 }
556
557 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
558 {       
559         size_t start;
560         size_t len;     
561         
562         if (!dest || !src) return NULL;
563         
564         start = strlen_w(dest);
565         len = strlen_w(src);
566
567         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
568         dest[start+len] = 0;
569         
570         return dest;
571 }
572
573
574 /*******************************************************************
575 replace any occurence of oldc with newc in unicode string
576 ********************************************************************/
577
578 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
579 {
580         for(;*s;s++) {
581                 if(*s==oldc) *s=newc;
582         }
583 }
584
585 /*******************************************************************
586 trim unicode string
587 ********************************************************************/
588
589 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
590                                   const smb_ucs2_t *back)
591 {
592         BOOL ret = False;
593         size_t len, front_len, back_len;
594
595         if (!s || !*s) return False;
596
597         len = strlen_w(s);
598
599         if (front && *front) {
600                 front_len = strlen_w(front);
601                 while (len && strncmp_w(s, front, front_len) == 0) {
602                         memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
603                         len -= front_len;
604                         ret = True;
605                 }
606         }
607         
608         if (back && *back) {
609                 back_len = strlen_w(back);
610                 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
611                         s[len - back_len] = 0;
612                         len -= back_len;
613                         ret = True;
614                 }
615         }
616
617         return ret;
618 }
619
620 /*
621   The *_wa() functions take a combination of 7 bit ascii
622   and wide characters They are used so that you can use string
623   functions combining C string constants with ucs2 strings
624
625   The char* arguments must NOT be multibyte - to be completely sure
626   of this only pass string constants */
627
628
629 void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
630 {
631         int i;
632         for (i=0;i<PSTRING_LEN;i++) {
633                 dest[i] = UCS2_CHAR(src[i]);
634                 if (src[i] == 0) return;
635         }
636 }
637
638 int strcmp_wa(const smb_ucs2_t *a, const char *b)
639 {
640         while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
641         return (*a - UCS2_CHAR(*b));
642 }
643
644 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
645 {
646         size_t n = 0;
647         while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;}
648         return (len - n)?(*a - UCS2_CHAR(*b)):0;
649 }
650
651 const smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
652 {
653         while (*s != 0) {
654                 int i;
655                 for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++) 
656                         ;
657                 if (p[i]) return s;
658                 s++;
659         }
660         return NULL;
661 }
662
663 const smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
664 {
665         const smb_ucs2_t *r;
666         size_t slen, inslen;
667
668         if (!s || !*s || !ins || !*ins) return NULL;
669         slen = strlen_w(s);
670         inslen = strlen(ins);
671         r = s;
672         while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
673                 if (strncmp_wa(r, ins, inslen) == 0) return r;
674                 r++;
675         }
676         return NULL;
677 }
678
679 /*******************************************************************
680 copy a string with max len
681 ********************************************************************/
682
683 smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max)
684 {
685         smb_ucs2_t *ucs2_src;
686
687         if (!dest || !src) return NULL;
688         if (!(ucs2_src = acnv_uxu2(src)))
689                 return NULL;
690         
691         strncpy_w(dest, ucs2_src, max);
692         SAFE_FREE(ucs2_src);
693         return dest;
694 }
695
696 /*******************************************************************
697 convert and duplicate an ascii string
698 ********************************************************************/
699 smb_ucs2_t *strdup_wa(const char *src)
700 {
701         return strndup_wa(src, 0);
702 }
703
704 /* if len == 0 then duplicate the whole string */
705 smb_ucs2_t *strndup_wa(const char *src, size_t len)
706 {
707         smb_ucs2_t *dest, *s;
708
709         s = acnv_dosu2(src);    
710         if (!len) len = strlen_w(s);
711         dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
712         if (!dest) {
713                 DEBUG(0,("strdup_w: out of memory!\n"));
714                 SAFE_FREE(s);
715                 return NULL;
716         }
717
718         memcpy(dest, src, len * sizeof(smb_ucs2_t));
719         dest[len] = 0;
720
721         SAFE_FREE(s);
722         return dest;
723 }
724
725 /*******************************************************************
726 append a string of len bytes and add a terminator
727 ********************************************************************/
728
729 smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max)
730 {
731         smb_ucs2_t *ucs2_src;
732
733         if (!dest || !src) return NULL;
734         if (!(ucs2_src = acnv_uxu2(src)))
735                 return NULL;
736         
737         strncat_w(dest, ucs2_src, max);
738         SAFE_FREE(ucs2_src);
739         return dest;
740 }
741
742 smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src)
743 {       
744         smb_ucs2_t *ucs2_src;
745         
746         if (!dest || !src) return NULL;
747         if (!(ucs2_src = acnv_uxu2(src)))
748                 return NULL;
749         
750         strcat_w(dest, ucs2_src);
751         SAFE_FREE(ucs2_src);
752         return dest;
753 }
754
755 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
756                                   const char *back)
757 {
758         wpstring f, b;
759
760         if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
761         else *f = 0;
762         if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
763         else *b = 0;
764         return trim_string_w(s, f, b);
765 }
766
767 /*******************************************************************
768  returns the length in number of wide characters
769  ******************************************************************/
770 int unistrlen(uint16 *s)
771 {
772         int len;
773
774         if (!s)
775                 return -1;
776
777         for (len=0; *s; s++,len++);
778
779         return len;
780 }
781
782 /*******************************************************************
783  Strcpy for unicode strings.  returns length (in num of wide chars)
784 ********************************************************************/
785
786 int unistrcpy(uint16 *dst, uint16 *src)
787 {
788         int num_wchars = 0;
789
790         while (*src) {
791                 *dst++ = *src++;
792                 num_wchars++;
793         }
794         *dst = 0;
795
796         return num_wchars;
797 }
798
799 /**
800  * Samba ucs2 type to UNISTR2 conversion
801  *
802  * @param ctx Talloc context to create the dst strcture (if null) and the 
803  *            contents of the unicode string.
804  * @param dst UNISTR2 destination. If equals null, then it's allocated.
805  * @param src smb_ucs2_t source.
806  * @param max_len maximum number of unicode characters to copy. If equals
807  *        null, then null-termination of src is taken
808  *
809  * @return copied UNISTR2 destination
810  **/
811 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
812 {
813         size_t len;
814
815         if (!src) return NULL;
816         len = strlen_w(src);
817         
818         /* allocate UNISTR2 destination if not given */
819         if (!dst) {
820                 dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2));
821                 if (!dst) return NULL;
822         }
823         if (!dst->buffer) {
824                 dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1));
825                 if (!dst->buffer) return NULL;
826         }
827         
828         /* set UNISTR2 parameters */
829         dst->uni_max_len = len + 1;
830         dst->undoc = 0;
831         dst->uni_str_len = len;
832         
833         /* copy the actual unicode string */
834         strncpy_w(dst->buffer, src, dst->uni_max_len);
835         
836         return dst;
837 };
838