Janitorial duties to make autogen.sh portable.
[Samba/gebeck_regimport.git] / source3 / lib / util_unistr.c
blob08bb03986f145eb62868fb9c544e129163dc28ff
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 or generate the case handling tables.
46 * The case tables are defined in UCS2 and don't depend on any
47 * configured parameters, so they never need to be reloaded.
48 **/
49 void load_case_tables(void)
51 static int initialised;
52 int i;
54 if (initialised) return;
55 initialised = 1;
57 upcase_table = map_file(lib_path("upcase.dat"), 0x20000);
58 lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000);
60 /* we would like Samba to limp along even if these tables are
61 not available */
62 if (!upcase_table) {
63 DEBUG(1,("creating lame upcase table\n"));
64 upcase_table = malloc(0x20000);
65 for (i=0;i<0x10000;i++) {
66 smb_ucs2_t v;
67 SSVAL(&v, 0, i);
68 upcase_table[v] = i;
70 for (i=0;i<256;i++) {
71 smb_ucs2_t v;
72 SSVAL(&v, 0, UCS2_CHAR(i));
73 upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
77 if (!lowcase_table) {
78 DEBUG(1,("creating lame lowcase table\n"));
79 lowcase_table = malloc(0x20000);
80 for (i=0;i<0x10000;i++) {
81 smb_ucs2_t v;
82 SSVAL(&v, 0, i);
83 lowcase_table[v] = i;
85 for (i=0;i<256;i++) {
86 smb_ucs2_t v;
87 SSVAL(&v, 0, UCS2_CHAR(i));
88 lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
94 see if a ucs2 character can be mapped correctly to a dos character
95 and mapped back to the same character in ucs2
97 int check_dos_char(smb_ucs2_t c)
99 lazy_initialize_conv();
101 /* Find the right byte, and right bit within the byte; return
102 * 1 or 0 */
103 return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0;
107 static int check_dos_char_slowly(smb_ucs2_t c)
109 char buf[10];
110 smb_ucs2_t c2 = 0;
111 int len1, len2;
112 len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf));
113 if (len1 == 0) return 0;
114 len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2);
115 if (len2 != 2) return 0;
116 return (c == c2);
121 * Fill out doschar table the hard way, by examining each character
123 void init_doschar_table(void)
125 int i, j, byteval;
127 /* For each byte of packed table */
129 for (i = 0; i <= 0xffff; i += 8) {
130 byteval = 0;
131 for (j = 0; j <= 7; j++) {
132 smb_ucs2_t c;
134 c = i + j;
136 if (check_dos_char_slowly(c))
137 byteval |= 1 << j;
139 doschar_table[i/8] = byteval;
145 * Load the valid character map table from <tt>valid.dat</tt> or
146 * create from the configured codepage.
148 * This function is called whenever the configuration is reloaded.
149 * However, the valid character table is not changed if it's loaded
150 * from a file, because we can't unmap files.
152 void init_valid_table(void)
154 static int mapped_file;
155 int i;
156 const char *allowed = ".!#$%&'()_-@^`~";
157 uint8 *valid_file;
159 if (mapped_file) {
160 /* Can't unmap files, so stick with what we have */
161 return;
164 valid_file = map_file(lib_path("valid.dat"), 0x10000);
165 if (valid_file) {
166 valid_table = valid_file;
167 mapped_file = 1;
168 return;
171 /* Otherwise, we're using a dynamically created valid_table.
172 * It might need to be regenerated if the code page changed.
173 * We know that we're not using a mapped file, so we can
174 * free() the old one. */
175 if (valid_table) free(valid_table);
177 DEBUG(2,("creating default valid table\n"));
178 valid_table = malloc(0x10000);
179 for (i=0;i<128;i++)
180 valid_table[i] = isalnum(i) || strchr(allowed,i);
182 for (;i<0x10000;i++) {
183 smb_ucs2_t c;
184 SSVAL(&c, 0, i);
185 valid_table[i] = check_dos_char(c);
191 /*******************************************************************
192 Write a string in (little-endian) unicode format. src is in
193 the current DOS codepage. len is the length in bytes of the
194 string pointed to by dst.
196 if null_terminate is True then null terminate the packet (adds 2 bytes)
198 the return value is the length in bytes consumed by the string, including the
199 null termination if applied
200 ********************************************************************/
202 size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
204 return push_ucs2(NULL, dst, src, len,
205 STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
209 /*******************************************************************
210 Skip past a unicode string, but not more than len. Always move
211 past a terminating zero if found.
212 ********************************************************************/
214 char *skip_unibuf(char *src, size_t len)
216 char *srcend = src + len;
218 while (src < srcend && SVAL(src,0))
219 src += 2;
221 if(!SVAL(src,0))
222 src += 2;
224 return src;
227 /* Copy a string from little-endian or big-endian unicode source (depending
228 * on flags) to internal samba format destination
230 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
232 if (!src) return 0;
233 if(dest_len==-1) dest_len=MAXUNI-3;
234 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
237 /* Copy a string from a unistr2 source to internal samba format
238 destination. Use this instead of direct calls to rpcstr_pull() to avoid
239 having to determine whether the source string is null terminated. */
241 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
243 return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
244 src->uni_str_len * 2, 0);
247 /* Converts a string from internal samba format to unicode
249 int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
251 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
254 /*******************************************************************
255 Return a DOS codepage version of a little-endian unicode string.
256 len is the filename length (ignoring any terminating zero) in uin16
257 units. Always null terminates.
258 Hack alert: uses fixed buffer(s).
259 ********************************************************************/
260 char *dos_unistrn2(const uint16 *src, int len)
262 static char lbufs[8][MAXUNI];
263 static int nexti;
264 char *lbuf = lbufs[nexti];
265 nexti = (nexti+1)%8;
266 pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN);
267 return lbuf;
270 /*******************************************************************
271 Convert a (little-endian) UNISTR2 structure to an ASCII string
272 ********************************************************************/
273 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
275 if (str == NULL) {
276 *dest='\0';
277 return;
279 pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
282 /*******************************************************************
283 give a static string for displaying a UNISTR2
284 ********************************************************************/
285 const char *unistr2_static(const UNISTR2 *str)
287 static pstring ret;
288 unistr2_to_ascii(ret, str, sizeof(ret));
289 return ret;
293 /*******************************************************************
294 duplicate a UNISTR2 string into a null terminated char*
295 using a talloc context
296 ********************************************************************/
297 char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
299 char *s;
300 int maxlen = (str->uni_str_len+1)*4;
301 if (!str->buffer) return NULL;
302 s = (char *)talloc(ctx, maxlen); /* convervative */
303 if (!s) return NULL;
304 pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2,
305 STR_NOALIGN);
306 return s;
310 /*******************************************************************
311 Return a number stored in a buffer
312 ********************************************************************/
314 uint32 buffer2_to_uint32(BUFFER2 *str)
316 if (str->buf_len == 4)
317 return IVAL(str->buffer, 0);
318 else
319 return 0;
322 /*******************************************************************
323 Convert a wchar to upper case.
324 ********************************************************************/
326 smb_ucs2_t toupper_w(smb_ucs2_t val)
328 return upcase_table[SVAL(&val,0)];
331 /*******************************************************************
332 Convert a wchar to lower case.
333 ********************************************************************/
335 smb_ucs2_t tolower_w( smb_ucs2_t val )
337 return lowcase_table[SVAL(&val,0)];
341 /*******************************************************************
342 determine if a character is lowercase
343 ********************************************************************/
344 BOOL islower_w(smb_ucs2_t c)
346 return upcase_table[SVAL(&c,0)] != c;
349 /*******************************************************************
350 determine if a character is uppercase
351 ********************************************************************/
352 BOOL isupper_w(smb_ucs2_t c)
354 return lowcase_table[SVAL(&c,0)] != c;
358 /*******************************************************************
359 determine if a character is valid in a 8.3 name
360 ********************************************************************/
361 BOOL isvalid83_w(smb_ucs2_t c)
363 return valid_table[SVAL(&c,0)] != 0;
366 /*******************************************************************
367 Count the number of characters in a smb_ucs2_t string.
368 ********************************************************************/
369 size_t strlen_w(const smb_ucs2_t *src)
371 size_t len;
373 for(len = 0; *src++; len++) ;
375 return len;
378 /*******************************************************************
379 Count up to max number of characters in a smb_ucs2_t string.
380 ********************************************************************/
381 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
383 size_t len;
385 for(len = 0; *src++ && (len < max); len++) ;
387 return len;
390 /*******************************************************************
391 wide strchr()
392 ********************************************************************/
393 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
395 while (*s != 0) {
396 if (c == *s) return (smb_ucs2_t *)s;
397 s++;
399 if (c == *s) return (smb_ucs2_t *)s;
401 return NULL;
404 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
406 return strchr_w(s, UCS2_CHAR(c));
409 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
411 const smb_ucs2_t *p = s;
412 int len = strlen_w(s);
413 if (len == 0) return NULL;
414 p += (len - 1);
415 do {
416 if (c == *p) return (smb_ucs2_t *)p;
417 } while (p-- != s);
418 return NULL;
421 /*******************************************************************
422 wide strstr()
423 ********************************************************************/
424 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
426 smb_ucs2_t *r;
427 size_t slen, inslen;
429 if (!s || !*s || !ins || !*ins) return NULL;
430 slen = strlen_w(s);
431 inslen = strlen_w(ins);
432 r = (smb_ucs2_t *)s;
433 while ((r = strchr_w(r, *ins))) {
434 if (strncmp_w(r, ins, inslen) == 0) return r;
435 r++;
437 return NULL;
440 /*******************************************************************
441 Convert a string to lower case.
442 return True if any char is converted
443 ********************************************************************/
444 BOOL strlower_w(smb_ucs2_t *s)
446 BOOL ret = False;
447 while (*s) {
448 smb_ucs2_t v = tolower_w(*s);
449 if (v != *s) {
450 *s = v;
451 ret = True;
453 s++;
455 return ret;
458 /*******************************************************************
459 Convert a string to upper case.
460 return True if any char is converted
461 ********************************************************************/
462 BOOL strupper_w(smb_ucs2_t *s)
464 BOOL ret = False;
465 while (*s) {
466 smb_ucs2_t v = toupper_w(*s);
467 if (v != *s) {
468 *s = v;
469 ret = True;
471 s++;
473 return ret;
476 /*******************************************************************
477 convert a string to "normal" form
478 ********************************************************************/
479 void strnorm_w(smb_ucs2_t *s)
481 extern int case_default;
482 if (case_default == CASE_UPPER)
483 strupper_w(s);
484 else
485 strlower_w(s);
488 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
490 while (*b && *a == *b) { a++; b++; }
491 return (*a - *b);
492 /* warning: if *a != *b and both are not 0 we retrun a random
493 greater or lesser than 0 number not realted to which
494 string is longer */
497 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
499 size_t n = 0;
500 while ((n < len) && *b && *a == *b) { a++; b++; n++;}
501 return (len - n)?(*a - *b):0;
504 /*******************************************************************
505 case insensitive string comparison
506 ********************************************************************/
507 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
509 while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
510 return (tolower_w(*a) - tolower_w(*b));
513 /*******************************************************************
514 case insensitive string comparison, lenght limited
515 ********************************************************************/
516 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
518 size_t n = 0;
519 while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
520 return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
523 /*******************************************************************
524 compare 2 strings
525 ********************************************************************/
526 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
528 if (s1 == s2) return(True);
529 if (!s1 || !s2) return(False);
531 return(strcasecmp_w(s1,s2)==0);
534 /*******************************************************************
535 compare 2 strings up to and including the nth char.
536 ******************************************************************/
537 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
539 if (s1 == s2) return(True);
540 if (!s1 || !s2 || !n) return(False);
542 return(strncasecmp_w(s1,s2,n)==0);
545 /*******************************************************************
546 duplicate string
547 ********************************************************************/
548 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
550 return strndup_w(src, 0);
553 /* if len == 0 then duplicate the whole string */
554 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
556 smb_ucs2_t *dest;
558 if (!len) len = strlen_w(src);
559 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
560 if (!dest) {
561 DEBUG(0,("strdup_w: out of memory!\n"));
562 return NULL;
565 memcpy(dest, src, len * sizeof(smb_ucs2_t));
566 dest[len] = 0;
568 return dest;
571 /*******************************************************************
572 copy a string with max len
573 ********************************************************************/
575 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
577 size_t len;
579 if (!dest || !src) return NULL;
581 for (len = 0; (src[len] != 0) && (len < max); len++)
582 dest[len] = src[len];
583 while (len < max)
584 dest[len++] = 0;
586 return dest;
590 /*******************************************************************
591 append a string of len bytes and add a terminator
592 ********************************************************************/
594 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
596 size_t start;
597 size_t len;
599 if (!dest || !src) return NULL;
601 start = strlen_w(dest);
602 len = strnlen_w(src, max);
604 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
605 dest[start+len] = 0;
607 return dest;
610 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
612 size_t start;
613 size_t len;
615 if (!dest || !src) return NULL;
617 start = strlen_w(dest);
618 len = strlen_w(src);
620 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
621 dest[start+len] = 0;
623 return dest;
627 /*******************************************************************
628 replace any occurence of oldc with newc in unicode string
629 ********************************************************************/
631 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
633 for(;*s;s++) {
634 if(*s==oldc) *s=newc;
638 /*******************************************************************
639 trim unicode string
640 ********************************************************************/
642 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
643 const smb_ucs2_t *back)
645 BOOL ret = False;
646 size_t len, front_len, back_len;
648 if (!s || !*s) return False;
650 len = strlen_w(s);
652 if (front && *front) {
653 front_len = strlen_w(front);
654 while (len && strncmp_w(s, front, front_len) == 0) {
655 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
656 len -= front_len;
657 ret = True;
661 if (back && *back) {
662 back_len = strlen_w(back);
663 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
664 s[len - back_len] = 0;
665 len -= back_len;
666 ret = True;
670 return ret;
674 The *_wa() functions take a combination of 7 bit ascii
675 and wide characters They are used so that you can use string
676 functions combining C string constants with ucs2 strings
678 The char* arguments must NOT be multibyte - to be completely sure
679 of this only pass string constants */
682 void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
684 int i;
685 for (i=0;i<PSTRING_LEN;i++) {
686 dest[i] = UCS2_CHAR(src[i]);
687 if (src[i] == 0) return;
691 int strcmp_wa(const smb_ucs2_t *a, const char *b)
693 while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
694 return (*a - UCS2_CHAR(*b));
697 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
699 size_t n = 0;
700 while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;}
701 return (len - n)?(*a - UCS2_CHAR(*b)):0;
704 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
706 while (*s != 0) {
707 int i;
708 for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++)
710 if (p[i]) return (smb_ucs2_t *)s;
711 s++;
713 return NULL;
716 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
718 smb_ucs2_t *r;
719 size_t slen, inslen;
721 if (!s || !*s || !ins || !*ins) return NULL;
722 slen = strlen_w(s);
723 inslen = strlen(ins);
724 r = (smb_ucs2_t *)s;
725 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
726 if (strncmp_wa(r, ins, inslen) == 0) return r;
727 r++;
729 return NULL;
732 /*******************************************************************
733 copy a string with max len
734 ********************************************************************/
736 smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max)
738 smb_ucs2_t *ucs2_src;
740 if (!dest || !src) return NULL;
741 if (!(ucs2_src = acnv_uxu2(src)))
742 return NULL;
744 strncpy_w(dest, ucs2_src, max);
745 SAFE_FREE(ucs2_src);
746 return dest;
749 /*******************************************************************
750 convert and duplicate an ascii string
751 ********************************************************************/
752 smb_ucs2_t *strdup_wa(const char *src)
754 return strndup_wa(src, 0);
757 /* if len == 0 then duplicate the whole string */
758 smb_ucs2_t *strndup_wa(const char *src, size_t len)
760 smb_ucs2_t *dest, *s;
762 s = acnv_dosu2(src);
763 if (!len) len = strlen_w(s);
764 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
765 if (!dest) {
766 DEBUG(0,("strdup_w: out of memory!\n"));
767 SAFE_FREE(s);
768 return NULL;
771 memcpy(dest, src, len * sizeof(smb_ucs2_t));
772 dest[len] = 0;
774 SAFE_FREE(s);
775 return dest;
778 /*******************************************************************
779 append a string of len bytes and add a terminator
780 ********************************************************************/
782 smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max)
784 smb_ucs2_t *ucs2_src;
786 if (!dest || !src) return NULL;
787 if (!(ucs2_src = acnv_uxu2(src)))
788 return NULL;
790 strncat_w(dest, ucs2_src, max);
791 SAFE_FREE(ucs2_src);
792 return dest;
795 smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src)
797 smb_ucs2_t *ucs2_src;
799 if (!dest || !src) return NULL;
800 if (!(ucs2_src = acnv_uxu2(src)))
801 return NULL;
803 strcat_w(dest, ucs2_src);
804 SAFE_FREE(ucs2_src);
805 return dest;
808 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
809 const char *back)
811 wpstring f, b;
813 if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
814 else *f = 0;
815 if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
816 else *b = 0;
817 return trim_string_w(s, f, b);
820 /*******************************************************************
821 returns the length in number of wide characters
822 ******************************************************************/
823 int unistrlen(uint16 *s)
825 int len;
827 if (!s)
828 return -1;
830 for (len=0; *s; s++,len++);
832 return len;
835 /*******************************************************************
836 Strcpy for unicode strings. returns length (in num of wide chars)
837 ********************************************************************/
839 int unistrcpy(uint16 *dst, uint16 *src)
841 int num_wchars = 0;
843 while (*src) {
844 *dst++ = *src++;
845 num_wchars++;
847 *dst = 0;
849 return num_wchars;
853 * Samba ucs2 type to UNISTR2 conversion
855 * @param ctx Talloc context to create the dst strcture (if null) and the
856 * contents of the unicode string.
857 * @param dst UNISTR2 destination. If equals null, then it's allocated.
858 * @param src smb_ucs2_t source.
859 * @param max_len maximum number of unicode characters to copy. If equals
860 * null, then null-termination of src is taken
862 * @return copied UNISTR2 destination
864 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
866 size_t len;
868 if (!src) return NULL;
869 len = strlen_w(src);
871 /* allocate UNISTR2 destination if not given */
872 if (!dst) {
873 dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2));
874 if (!dst) return NULL;
876 if (!dst->buffer) {
877 dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1));
878 if (!dst->buffer) return NULL;
881 /* set UNISTR2 parameters */
882 dst->uni_max_len = len + 1;
883 dst->undoc = 0;
884 dst->uni_str_len = len;
886 /* copy the actual unicode string */
887 strncpy_w(dst->buffer, src, dst->uni_max_len);
889 return dst;