Some comments about the format of the on-disk reg file, as well as
[Samba/gebeck_regimport.git] / source3 / lib / util_unistr.c
blob812859000a9f73eeb98212b57c39e0b507e90c16
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001
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.
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.
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.
22 #include "includes.h"
24 #ifndef MAXUNI
25 #define MAXUNI 1024
26 #endif
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;
34 /**
35 * This table says which Unicode characters are valid dos
36 * characters.
38 * Each value is just a single bit.
39 **/
40 static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */
43 /*******************************************************************
44 load the case handling tables
45 ********************************************************************/
46 void load_case_tables(void)
48 static int initialised;
49 int i;
51 if (initialised) return;
52 initialised = 1;
54 upcase_table = map_file(lib_path("upcase.dat"), 0x20000);
55 lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000);
57 /* we would like Samba to limp along even if these tables are
58 not available */
59 if (!upcase_table) {
60 DEBUG(1,("creating lame upcase table\n"));
61 upcase_table = malloc(0x20000);
62 for (i=0;i<0x10000;i++) {
63 smb_ucs2_t v;
64 SSVAL(&v, 0, i);
65 upcase_table[v] = i;
67 for (i=0;i<256;i++) {
68 smb_ucs2_t v;
69 SSVAL(&v, 0, UCS2_CHAR(i));
70 upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
74 if (!lowcase_table) {
75 DEBUG(1,("creating lame lowcase table\n"));
76 lowcase_table = malloc(0x20000);
77 for (i=0;i<0x10000;i++) {
78 smb_ucs2_t v;
79 SSVAL(&v, 0, i);
80 lowcase_table[v] = i;
82 for (i=0;i<256;i++) {
83 smb_ucs2_t v;
84 SSVAL(&v, 0, UCS2_CHAR(i));
85 lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
91 see if a ucs2 character can be mapped correctly to a dos character
92 and mapped back to the same character in ucs2
94 static int check_dos_char(smb_ucs2_t c)
96 static int initialized = False;
98 if (!initialized) {
99 initialized = True;
100 init_doschar_table();
103 /* Find the right byte, and right bit within the byte; return
104 * 1 or 0 */
105 return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0;
109 static int check_dos_char_slowly(smb_ucs2_t c)
111 char buf[10];
112 smb_ucs2_t c2 = 0;
113 int len1, len2;
114 len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf));
115 if (len1 == 0) return 0;
116 len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2);
117 if (len2 != 2) return 0;
118 return (c == c2);
123 * Fill out doschar table the hard way, by examining each character
125 void init_doschar_table(void)
127 int i, j, byteval;
129 /* For each byte of packed table */
131 for (i = 0; i <= 0xffff; i += 8) {
132 byteval = 0;
133 for (j = 0; j <= 7; j++) {
134 smb_ucs2_t c;
136 c = i + j;
138 if (check_dos_char_slowly(c))
139 byteval |= 1 << j;
141 doschar_table[i/8] = byteval;
147 * Load the valid character map table from <tt>valid.dat</tt> or
148 * create from the configured codepage.
150 * This function is called whenever the configuration is reloaded.
151 * However, the valid character table is not changed if it's loaded
152 * from a file, because we can't unmap files.
154 void init_valid_table(void)
156 static int mapped_file;
157 int i;
158 const char *allowed = ".!#$%&'()_-@^`~";
159 uint8 *valid_file;
161 if (mapped_file) {
162 /* Can't unmap files, so stick with what we have */
163 return;
166 valid_file = map_file(lib_path("valid.dat"), 0x10000);
167 if (valid_file) {
168 valid_table = valid_file;
169 mapped_file = 1;
170 return;
173 /* Otherwise, we're using a dynamically created valid_table.
174 * It might need to be regenerated if the code page changed.
175 * We know that we're not using a mapped file, so we can
176 * free() the old one. */
177 if (valid_table) free(valid_table);
179 DEBUG(2,("creating default valid table\n"));
180 valid_table = malloc(0x10000);
181 for (i=0;i<128;i++)
182 valid_table[i] = isalnum(i) || strchr(allowed,i);
184 for (;i<0x10000;i++) {
185 smb_ucs2_t c;
186 SSVAL(&c, 0, i);
187 valid_table[i] = check_dos_char(c);
193 /*******************************************************************
194 Write a string in (little-endian) unicode format. src is in
195 the current DOS codepage. len is the length in bytes of the
196 string pointed to by dst.
198 if null_terminate is True then null terminate the packet (adds 2 bytes)
200 the return value is the length in bytes consumed by the string, including the
201 null termination if applied
202 ********************************************************************/
204 size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
206 return push_ucs2(NULL, dst, src, len,
207 STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
211 /*******************************************************************
212 Skip past a unicode string, but not more than len. Always move
213 past a terminating zero if found.
214 ********************************************************************/
216 char *skip_unibuf(char *src, size_t len)
218 char *srcend = src + len;
220 while (src < srcend && SVAL(src,0))
221 src += 2;
223 if(!SVAL(src,0))
224 src += 2;
226 return src;
229 /* Copy a string from little-endian or big-endian unicode source (depending
230 * on flags) to internal samba format destination
232 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
234 if (!src) return 0;
235 if(dest_len==-1) dest_len=MAXUNI-3;
236 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
239 /* Copy a string from a unistr2 source to internal samba format
240 destination. Use this instead of direct calls to rpcstr_pull() to avoid
241 having to determine whether the source string is null terminated. */
243 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
245 return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
246 src->uni_str_len * 2, 0);
249 /* Converts a string from internal samba format to unicode
251 int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
253 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
256 /*******************************************************************
257 Return a DOS codepage version of a little-endian unicode string.
258 len is the filename length (ignoring any terminating zero) in uin16
259 units. Always null terminates.
260 Hack alert: uses fixed buffer(s).
261 ********************************************************************/
262 char *dos_unistrn2(const uint16 *src, int len)
264 static char lbufs[8][MAXUNI];
265 static int nexti;
266 char *lbuf = lbufs[nexti];
267 nexti = (nexti+1)%8;
268 pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN);
269 return lbuf;
272 /*******************************************************************
273 Convert a (little-endian) UNISTR2 structure to an ASCII string
274 ********************************************************************/
275 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
277 if (str == NULL) {
278 *dest='\0';
279 return;
281 pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
284 /*******************************************************************
285 give a static string for displaying a UNISTR2
286 ********************************************************************/
287 const char *unistr2_static(const UNISTR2 *str)
289 static pstring ret;
290 unistr2_to_ascii(ret, str, sizeof(ret));
291 return ret;
295 /*******************************************************************
296 duplicate a UNISTR2 string into a null terminated char*
297 using a talloc context
298 ********************************************************************/
299 char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
301 char *s;
302 int maxlen = (str->uni_str_len+1)*4;
303 if (!str->buffer) return NULL;
304 s = (char *)talloc(ctx, maxlen); /* convervative */
305 if (!s) return NULL;
306 pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2,
307 STR_NOALIGN);
308 return s;
312 /*******************************************************************
313 Return a number stored in a buffer
314 ********************************************************************/
316 uint32 buffer2_to_uint32(BUFFER2 *str)
318 if (str->buf_len == 4)
319 return IVAL(str->buffer, 0);
320 else
321 return 0;
324 /*******************************************************************
325 Convert a wchar to upper case.
326 ********************************************************************/
328 smb_ucs2_t toupper_w(smb_ucs2_t val)
330 return upcase_table[SVAL(&val,0)];
333 /*******************************************************************
334 Convert a wchar to lower case.
335 ********************************************************************/
337 smb_ucs2_t tolower_w( smb_ucs2_t val )
339 return lowcase_table[SVAL(&val,0)];
343 /*******************************************************************
344 determine if a character is lowercase
345 ********************************************************************/
346 BOOL islower_w(smb_ucs2_t c)
348 return upcase_table[SVAL(&c,0)] != c;
351 /*******************************************************************
352 determine if a character is uppercase
353 ********************************************************************/
354 BOOL isupper_w(smb_ucs2_t c)
356 return lowcase_table[SVAL(&c,0)] != c;
360 /*******************************************************************
361 determine if a character is valid in a 8.3 name
362 ********************************************************************/
363 BOOL isvalid83_w(smb_ucs2_t c)
365 return valid_table[SVAL(&c,0)] != 0;
368 /*******************************************************************
369 Count the number of characters in a smb_ucs2_t string.
370 ********************************************************************/
371 size_t strlen_w(const smb_ucs2_t *src)
373 size_t len;
375 for(len = 0; *src++; len++) ;
377 return len;
380 /*******************************************************************
381 Count up to max number of characters in a smb_ucs2_t string.
382 ********************************************************************/
383 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
385 size_t len;
387 for(len = 0; *src++ && (len < max); len++) ;
389 return len;
392 /*******************************************************************
393 wide strchr()
394 ********************************************************************/
395 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
397 while (*s != 0) {
398 if (c == *s) return (smb_ucs2_t *)s;
399 s++;
401 if (c == *s) return (smb_ucs2_t *)s;
403 return NULL;
406 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
408 return strchr_w(s, UCS2_CHAR(c));
411 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
413 const smb_ucs2_t *p = s;
414 int len = strlen_w(s);
415 if (len == 0) return NULL;
416 p += (len - 1);
417 do {
418 if (c == *p) return (smb_ucs2_t *)p;
419 } while (p-- != s);
420 return NULL;
423 /*******************************************************************
424 wide strstr()
425 ********************************************************************/
426 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
428 smb_ucs2_t *r;
429 size_t slen, inslen;
431 if (!s || !*s || !ins || !*ins) return NULL;
432 slen = strlen_w(s);
433 inslen = strlen_w(ins);
434 r = (smb_ucs2_t *)s;
435 while ((r = strchr_w(r, *ins))) {
436 if (strncmp_w(r, ins, inslen) == 0) return r;
437 r++;
439 return NULL;
442 /*******************************************************************
443 Convert a string to lower case.
444 return True if any char is converted
445 ********************************************************************/
446 BOOL strlower_w(smb_ucs2_t *s)
448 BOOL ret = False;
449 while (*s) {
450 smb_ucs2_t v = tolower_w(*s);
451 if (v != *s) {
452 *s = v;
453 ret = True;
455 s++;
457 return ret;
460 /*******************************************************************
461 Convert a string to upper case.
462 return True if any char is converted
463 ********************************************************************/
464 BOOL strupper_w(smb_ucs2_t *s)
466 BOOL ret = False;
467 while (*s) {
468 smb_ucs2_t v = toupper_w(*s);
469 if (v != *s) {
470 *s = v;
471 ret = True;
473 s++;
475 return ret;
478 /*******************************************************************
479 convert a string to "normal" form
480 ********************************************************************/
481 void strnorm_w(smb_ucs2_t *s)
483 extern int case_default;
484 if (case_default == CASE_UPPER)
485 strupper_w(s);
486 else
487 strlower_w(s);
490 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
492 while (*b && *a == *b) { a++; b++; }
493 return (*a - *b);
494 /* warning: if *a != *b and both are not 0 we retrun a random
495 greater or lesser than 0 number not realted to which
496 string is longer */
499 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
501 size_t n = 0;
502 while ((n < len) && *b && *a == *b) { a++; b++; n++;}
503 return (len - n)?(*a - *b):0;
506 /*******************************************************************
507 case insensitive string comparison
508 ********************************************************************/
509 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
511 while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
512 return (tolower_w(*a) - tolower_w(*b));
515 /*******************************************************************
516 case insensitive string comparison, lenght limited
517 ********************************************************************/
518 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
520 size_t n = 0;
521 while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
522 return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
525 /*******************************************************************
526 compare 2 strings
527 ********************************************************************/
528 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
530 if (s1 == s2) return(True);
531 if (!s1 || !s2) return(False);
533 return(strcasecmp_w(s1,s2)==0);
536 /*******************************************************************
537 compare 2 strings up to and including the nth char.
538 ******************************************************************/
539 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
541 if (s1 == s2) return(True);
542 if (!s1 || !s2 || !n) return(False);
544 return(strncasecmp_w(s1,s2,n)==0);
547 /*******************************************************************
548 duplicate string
549 ********************************************************************/
550 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
552 return strndup_w(src, 0);
555 /* if len == 0 then duplicate the whole string */
556 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
558 smb_ucs2_t *dest;
560 if (!len) len = strlen_w(src);
561 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
562 if (!dest) {
563 DEBUG(0,("strdup_w: out of memory!\n"));
564 return NULL;
567 memcpy(dest, src, len * sizeof(smb_ucs2_t));
568 dest[len] = 0;
570 return dest;
573 /*******************************************************************
574 copy a string with max len
575 ********************************************************************/
577 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
579 size_t len;
581 if (!dest || !src) return NULL;
583 for (len = 0; (src[len] != 0) && (len < max); len++)
584 dest[len] = src[len];
585 while (len < max)
586 dest[len++] = 0;
588 return dest;
592 /*******************************************************************
593 append a string of len bytes and add a terminator
594 ********************************************************************/
596 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
598 size_t start;
599 size_t len;
601 if (!dest || !src) return NULL;
603 start = strlen_w(dest);
604 len = strnlen_w(src, max);
606 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
607 dest[start+len] = 0;
609 return dest;
612 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
614 size_t start;
615 size_t len;
617 if (!dest || !src) return NULL;
619 start = strlen_w(dest);
620 len = strlen_w(src);
622 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
623 dest[start+len] = 0;
625 return dest;
629 /*******************************************************************
630 replace any occurence of oldc with newc in unicode string
631 ********************************************************************/
633 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
635 for(;*s;s++) {
636 if(*s==oldc) *s=newc;
640 /*******************************************************************
641 trim unicode string
642 ********************************************************************/
644 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
645 const smb_ucs2_t *back)
647 BOOL ret = False;
648 size_t len, front_len, back_len;
650 if (!s || !*s) return False;
652 len = strlen_w(s);
654 if (front && *front) {
655 front_len = strlen_w(front);
656 while (len && strncmp_w(s, front, front_len) == 0) {
657 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
658 len -= front_len;
659 ret = True;
663 if (back && *back) {
664 back_len = strlen_w(back);
665 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
666 s[len - back_len] = 0;
667 len -= back_len;
668 ret = True;
672 return ret;
676 The *_wa() functions take a combination of 7 bit ascii
677 and wide characters They are used so that you can use string
678 functions combining C string constants with ucs2 strings
680 The char* arguments must NOT be multibyte - to be completely sure
681 of this only pass string constants */
684 void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
686 int i;
687 for (i=0;i<PSTRING_LEN;i++) {
688 dest[i] = UCS2_CHAR(src[i]);
689 if (src[i] == 0) return;
693 int strcmp_wa(const smb_ucs2_t *a, const char *b)
695 while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
696 return (*a - UCS2_CHAR(*b));
699 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
701 size_t n = 0;
702 while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;}
703 return (len - n)?(*a - UCS2_CHAR(*b)):0;
706 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
708 while (*s != 0) {
709 int i;
710 for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++)
712 if (p[i]) return (smb_ucs2_t *)s;
713 s++;
715 return NULL;
718 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
720 smb_ucs2_t *r;
721 size_t slen, inslen;
723 if (!s || !*s || !ins || !*ins) return NULL;
724 slen = strlen_w(s);
725 inslen = strlen(ins);
726 r = (smb_ucs2_t *)s;
727 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
728 if (strncmp_wa(r, ins, inslen) == 0) return r;
729 r++;
731 return NULL;
734 /*******************************************************************
735 copy a string with max len
736 ********************************************************************/
738 smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max)
740 smb_ucs2_t *ucs2_src;
742 if (!dest || !src) return NULL;
743 if (!(ucs2_src = acnv_uxu2(src)))
744 return NULL;
746 strncpy_w(dest, ucs2_src, max);
747 SAFE_FREE(ucs2_src);
748 return dest;
751 /*******************************************************************
752 convert and duplicate an ascii string
753 ********************************************************************/
754 smb_ucs2_t *strdup_wa(const char *src)
756 return strndup_wa(src, 0);
759 /* if len == 0 then duplicate the whole string */
760 smb_ucs2_t *strndup_wa(const char *src, size_t len)
762 smb_ucs2_t *dest, *s;
764 s = acnv_dosu2(src);
765 if (!len) len = strlen_w(s);
766 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
767 if (!dest) {
768 DEBUG(0,("strdup_w: out of memory!\n"));
769 SAFE_FREE(s);
770 return NULL;
773 memcpy(dest, src, len * sizeof(smb_ucs2_t));
774 dest[len] = 0;
776 SAFE_FREE(s);
777 return dest;
780 /*******************************************************************
781 append a string of len bytes and add a terminator
782 ********************************************************************/
784 smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max)
786 smb_ucs2_t *ucs2_src;
788 if (!dest || !src) return NULL;
789 if (!(ucs2_src = acnv_uxu2(src)))
790 return NULL;
792 strncat_w(dest, ucs2_src, max);
793 SAFE_FREE(ucs2_src);
794 return dest;
797 smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src)
799 smb_ucs2_t *ucs2_src;
801 if (!dest || !src) return NULL;
802 if (!(ucs2_src = acnv_uxu2(src)))
803 return NULL;
805 strcat_w(dest, ucs2_src);
806 SAFE_FREE(ucs2_src);
807 return dest;
810 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
811 const char *back)
813 wpstring f, b;
815 if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
816 else *f = 0;
817 if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
818 else *b = 0;
819 return trim_string_w(s, f, b);
822 /*******************************************************************
823 returns the length in number of wide characters
824 ******************************************************************/
825 int unistrlen(uint16 *s)
827 int len;
829 if (!s)
830 return -1;
832 for (len=0; *s; s++,len++);
834 return len;
837 /*******************************************************************
838 Strcpy for unicode strings. returns length (in num of wide chars)
839 ********************************************************************/
841 int unistrcpy(uint16 *dst, uint16 *src)
843 int num_wchars = 0;
845 while (*src) {
846 *dst++ = *src++;
847 num_wchars++;
849 *dst = 0;
851 return num_wchars;
855 * Samba ucs2 type to UNISTR2 conversion
857 * @param ctx Talloc context to create the dst strcture (if null) and the
858 * contents of the unicode string.
859 * @param dst UNISTR2 destination. If equals null, then it's allocated.
860 * @param src smb_ucs2_t source.
861 * @param max_len maximum number of unicode characters to copy. If equals
862 * null, then null-termination of src is taken
864 * @return copied UNISTR2 destination
866 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
868 size_t len;
870 if (!src) return NULL;
871 len = strlen_w(src);
873 /* allocate UNISTR2 destination if not given */
874 if (!dst) {
875 dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2));
876 if (!dst) return NULL;
878 if (!dst->buffer) {
879 dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1));
880 if (!dst->buffer) return NULL;
883 /* set UNISTR2 parameters */
884 dst->uni_max_len = len + 1;
885 dst->undoc = 0;
886 dst->uni_str_len = len;
888 /* copy the actual unicode string */
889 strncpy_w(dst->buffer, src, dst->uni_max_len);
891 return dst;