Zero out the out policy handler in lsa_Close
[Samba/gebeck_regimport.git] / source3 / lib / util_unistr.c
blob84ee673a67315e19095fded7bcc80f44f9cca7e3
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
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
33 static bool upcase_table_use_unmap;
34 static bool lowcase_table_use_unmap;
35 static bool valid_table_use_unmap;
37 /**
38 * Destroy global objects allocated by load_case_tables()
39 **/
40 void gfree_case_tables(void)
42 if ( upcase_table ) {
43 if ( upcase_table_use_unmap )
44 unmap_file(upcase_table, 0x20000);
45 else
46 SAFE_FREE(upcase_table);
49 if ( lowcase_table ) {
50 if ( lowcase_table_use_unmap )
51 unmap_file(lowcase_table, 0x20000);
52 else
53 SAFE_FREE(lowcase_table);
56 if ( valid_table ) {
57 if ( valid_table_use_unmap )
58 unmap_file(valid_table, 0x10000);
59 else
60 SAFE_FREE(valid_table);
64 /**
65 * Load or generate the case handling tables.
67 * The case tables are defined in UCS2 and don't depend on any
68 * configured parameters, so they never need to be reloaded.
69 **/
71 void load_case_tables(void)
73 static int initialised;
74 char *old_locale = NULL, *saved_locale = NULL;
75 int i;
76 TALLOC_CTX *frame = NULL;
78 if (initialised) {
79 return;
81 initialised = 1;
83 frame = talloc_stackframe();
85 upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"),
86 0x20000);
87 upcase_table_use_unmap = ( upcase_table != NULL );
89 lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"),
90 0x20000);
91 lowcase_table_use_unmap = ( lowcase_table != NULL );
93 #ifdef HAVE_SETLOCALE
94 /* Get the name of the current locale. */
95 old_locale = setlocale(LC_ALL, NULL);
97 if (old_locale) {
98 /* Save it as it is in static storage. */
99 saved_locale = SMB_STRDUP(old_locale);
102 /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
103 setlocale(LC_ALL, "C");
104 #endif
106 /* we would like Samba to limp along even if these tables are
107 not available */
108 if (!upcase_table) {
109 DEBUG(1,("creating lame upcase table\n"));
110 upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
111 for (i=0;i<0x10000;i++) {
112 smb_ucs2_t v;
113 SSVAL(&v, 0, i);
114 upcase_table[v] = i;
116 for (i=0;i<256;i++) {
117 smb_ucs2_t v;
118 SSVAL(&v, 0, UCS2_CHAR(i));
119 upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
123 if (!lowcase_table) {
124 DEBUG(1,("creating lame lowcase table\n"));
125 lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
126 for (i=0;i<0x10000;i++) {
127 smb_ucs2_t v;
128 SSVAL(&v, 0, i);
129 lowcase_table[v] = i;
131 for (i=0;i<256;i++) {
132 smb_ucs2_t v;
133 SSVAL(&v, 0, UCS2_CHAR(i));
134 lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
138 #ifdef HAVE_SETLOCALE
139 /* Restore the old locale. */
140 if (saved_locale) {
141 setlocale (LC_ALL, saved_locale);
142 SAFE_FREE(saved_locale);
144 #endif
145 TALLOC_FREE(frame);
148 static int check_dos_char_slowly(smb_ucs2_t c)
150 char buf[10];
151 smb_ucs2_t c2 = 0;
152 int len1, len2;
154 len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
155 if (len1 == 0) {
156 return 0;
158 len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
159 if (len2 != 2) {
160 return 0;
162 return (c == c2);
166 * Load the valid character map table from <tt>valid.dat</tt> or
167 * create from the configured codepage.
169 * This function is called whenever the configuration is reloaded.
170 * However, the valid character table is not changed if it's loaded
171 * from a file, because we can't unmap files.
174 void init_valid_table(void)
176 static int mapped_file;
177 int i;
178 const char *allowed = ".!#$%&'()_-@^`~";
179 uint8 *valid_file;
181 if (mapped_file) {
182 /* Can't unmap files, so stick with what we have */
183 return;
186 valid_file = (uint8 *)map_file(data_path("valid.dat"), 0x10000);
187 if (valid_file) {
188 valid_table = valid_file;
189 mapped_file = 1;
190 valid_table_use_unmap = True;
191 return;
194 /* Otherwise, we're using a dynamically created valid_table.
195 * It might need to be regenerated if the code page changed.
196 * We know that we're not using a mapped file, so we can
197 * free() the old one. */
198 SAFE_FREE(valid_table);
200 /* use free rather than unmap */
201 valid_table_use_unmap = False;
203 DEBUG(2,("creating default valid table\n"));
204 valid_table = (uint8 *)SMB_MALLOC(0x10000);
205 SMB_ASSERT(valid_table != NULL);
206 for (i=0;i<128;i++) {
207 valid_table[i] = isalnum(i) || strchr(allowed,i);
210 lazy_initialize_conv();
212 for (;i<0x10000;i++) {
213 smb_ucs2_t c;
214 SSVAL(&c, 0, i);
215 valid_table[i] = check_dos_char_slowly(c);
219 /*******************************************************************
220 Write a string in (little-endian) unicode format. src is in
221 the current DOS codepage. len is the length in bytes of the
222 string pointed to by dst.
224 if null_terminate is True then null terminate the packet (adds 2 bytes)
226 the return value is the length in bytes consumed by the string, including the
227 null termination if applied
228 ********************************************************************/
230 size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
232 int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
233 : STR_UNICODE|STR_NOALIGN;
234 return push_ucs2(NULL, dst, src, len, flags);
238 /*******************************************************************
239 Skip past a unicode string, but not more than len. Always move
240 past a terminating zero if found.
241 ********************************************************************/
243 char *skip_unibuf(char *src, size_t len)
245 char *srcend = src + len;
247 while (src < srcend && SVAL(src,0)) {
248 src += 2;
251 if(!SVAL(src,0)) {
252 src += 2;
255 return src;
258 /* Copy a string from little-endian or big-endian unicode source (depending
259 * on flags) to internal samba format destination
262 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
264 if (!src) {
265 dest[0] = 0;
266 return 0;
268 if(dest_len==-1) {
269 dest_len=MAXUNI-3;
271 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
274 /* Copy a string from little-endian or big-endian unicode source (depending
275 * on flags) to internal samba format destination. Allocates on talloc ctx.
278 int rpcstr_pull_talloc(TALLOC_CTX *ctx,
279 char **dest,
280 void *src,
281 int src_len,
282 int flags)
284 return pull_ucs2_base_talloc(ctx,
285 NULL,
286 dest,
287 src,
288 src_len,
289 flags|STR_UNICODE|STR_NOALIGN);
293 /* Copy a string from a unistr2 source to internal samba format
294 destination. Use this instead of direct calls to rpcstr_pull() to avoid
295 having to determine whether the source string is null terminated. */
297 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
299 return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
300 src->uni_str_len * 2, 0);
303 /* Helper function to return a talloc'ed string. I have implemented it with a
304 * copy because I don't really know how pull_ucs2 and friends calculate the
305 * target size. If this turns out to be a major bottleneck someone with deeper
306 * multi-byte knowledge needs to revisit this.
307 * I just did (JRA :-). No longer uses copy.
308 * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would
309 * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c.
312 char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *ctx, const UNISTR2 *src)
314 char *dest = NULL;
315 size_t dest_len = convert_string_talloc(ctx,
316 CH_UTF16LE,
317 CH_UNIX,
318 src->buffer,
319 src->uni_str_len * 2,
320 (void *)&dest,
321 true);
322 if (dest_len == (size_t)-1) {
323 return NULL;
326 /* Ensure we're returning a null terminated string. */
327 if (dest_len) {
328 /* Did we already process the terminating zero ? */
329 if (dest[dest_len-1] != 0) {
330 size_t size = talloc_get_size(dest);
331 /* Have we got space to append the '\0' ? */
332 if (size <= dest_len) {
333 /* No, realloc. */
334 dest = TALLOC_REALLOC_ARRAY(ctx, dest, char,
335 dest_len+1);
336 if (!dest) {
337 /* talloc fail. */
338 dest_len = (size_t)-1;
339 return NULL;
342 /* Yay - space ! */
343 dest[dest_len] = '\0';
344 dest_len++;
346 } else if (dest) {
347 dest[0] = 0;
350 return dest;
353 /* Converts a string from internal samba format to unicode
356 int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags)
358 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
361 /* Converts a string from internal samba format to unicode. Always terminates.
362 * Actually just a wrapper round push_ucs2_talloc().
365 int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
367 return push_ucs2_talloc(ctx, dest, src);
370 /*******************************************************************
371 Convert a (little-endian) UNISTR2 structure to an ASCII string.
372 ********************************************************************/
374 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
376 if ((str == NULL) || (str->uni_str_len == 0)) {
377 *dest='\0';
378 return;
380 pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
383 #if 0
384 /*******************************************************************
385 Convert a (little-endian) UNISTR3 structure to an ASCII string.
386 ********************************************************************/
388 void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen)
390 if ((str == NULL) || (str->uni_str_len == 0)) {
391 *dest='\0';
392 return;
394 pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2,
395 STR_NOALIGN);
397 #endif
399 /*******************************************************************
400 Duplicate a UNISTR2 string into a null terminated char*
401 using a talloc context.
402 ********************************************************************/
404 char *unistr2_to_ascii_talloc(TALLOC_CTX *ctx, const UNISTR2 *str)
406 char *s = NULL;
408 if (!str || !str->buffer) {
409 return NULL;
411 if (pull_ucs2_base_talloc(ctx,
412 NULL,
414 str->buffer,
415 str->uni_str_len*2,
416 STR_NOALIGN) == (size_t)-1) {
417 return NULL;
419 return s;
422 /*******************************************************************
423 Return a string for displaying a UNISTR2. Guarentees to return a
424 valid string - "" if nothing else.
425 Changed to use talloc_tos() under the covers.... JRA.
426 ********************************************************************/
428 const char *unistr2_static(const UNISTR2 *str)
430 char *dest = NULL;
432 if ((str == NULL) || (str->uni_str_len == 0)) {
433 return "";
436 dest = unistr2_to_ascii_talloc(talloc_tos(), str);
437 if (!dest) {
438 return "";
441 return dest;
444 /*******************************************************************
445 Convert a wchar to upper case.
446 ********************************************************************/
448 smb_ucs2_t toupper_w(smb_ucs2_t val)
450 return upcase_table[SVAL(&val,0)];
453 /*******************************************************************
454 Convert a wchar to lower case.
455 ********************************************************************/
457 smb_ucs2_t tolower_w( smb_ucs2_t val )
459 return lowcase_table[SVAL(&val,0)];
462 /*******************************************************************
463 Determine if a character is lowercase.
464 ********************************************************************/
466 bool islower_w(smb_ucs2_t c)
468 return upcase_table[SVAL(&c,0)] != c;
471 /*******************************************************************
472 Determine if a character is uppercase.
473 ********************************************************************/
475 bool isupper_w(smb_ucs2_t c)
477 return lowcase_table[SVAL(&c,0)] != c;
480 /*******************************************************************
481 Determine if a character is valid in a 8.3 name.
482 ********************************************************************/
484 bool isvalid83_w(smb_ucs2_t c)
486 return valid_table[SVAL(&c,0)] != 0;
489 /*******************************************************************
490 Count the number of characters in a smb_ucs2_t string.
491 ********************************************************************/
493 size_t strlen_w(const smb_ucs2_t *src)
495 size_t len;
496 smb_ucs2_t c;
498 for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
502 return len;
505 /*******************************************************************
506 Count up to max number of characters in a smb_ucs2_t string.
507 ********************************************************************/
509 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
511 size_t len;
512 smb_ucs2_t c;
514 for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
518 return len;
521 /*******************************************************************
522 Wide strchr().
523 ********************************************************************/
525 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
527 smb_ucs2_t cp;
528 while (*(COPY_UCS2_CHAR(&cp,s))) {
529 if (c == cp) {
530 return (smb_ucs2_t *)s;
532 s++;
534 if (c == cp) {
535 return (smb_ucs2_t *)s;
538 return NULL;
541 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
543 return strchr_w(s, UCS2_CHAR(c));
546 /*******************************************************************
547 Wide strrchr().
548 ********************************************************************/
550 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
552 smb_ucs2_t cp;
553 const smb_ucs2_t *p = s;
554 int len = strlen_w(s);
556 if (len == 0) {
557 return NULL;
559 p += (len - 1);
560 do {
561 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
562 return (smb_ucs2_t *)p;
564 } while (p-- != s);
565 return NULL;
568 /*******************************************************************
569 Wide version of strrchr that returns after doing strrchr 'n' times.
570 ********************************************************************/
572 smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
574 smb_ucs2_t cp;
575 const smb_ucs2_t *p = s;
576 int len = strlen_w(s);
578 if (len == 0 || !n) {
579 return NULL;
581 p += (len - 1);
582 do {
583 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
584 n--;
587 if (!n) {
588 return (smb_ucs2_t *)p;
590 } while (p-- != s);
591 return NULL;
594 /*******************************************************************
595 Wide strstr().
596 ********************************************************************/
598 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
600 smb_ucs2_t *r;
601 size_t inslen;
603 if (!s || !*s || !ins || !*ins) {
604 return NULL;
607 inslen = strlen_w(ins);
608 r = (smb_ucs2_t *)s;
610 while ((r = strchr_w(r, *ins))) {
611 if (strncmp_w(r, ins, inslen) == 0) {
612 return r;
614 r++;
617 return NULL;
620 /*******************************************************************
621 Convert a string to lower case.
622 return True if any char is converted
623 ********************************************************************/
625 bool strlower_w(smb_ucs2_t *s)
627 smb_ucs2_t cp;
628 bool ret = False;
630 while (*(COPY_UCS2_CHAR(&cp,s))) {
631 smb_ucs2_t v = tolower_w(cp);
632 if (v != cp) {
633 COPY_UCS2_CHAR(s,&v);
634 ret = True;
636 s++;
638 return ret;
641 /*******************************************************************
642 Convert a string to upper case.
643 return True if any char is converted
644 ********************************************************************/
646 bool strupper_w(smb_ucs2_t *s)
648 smb_ucs2_t cp;
649 bool ret = False;
650 while (*(COPY_UCS2_CHAR(&cp,s))) {
651 smb_ucs2_t v = toupper_w(cp);
652 if (v != cp) {
653 COPY_UCS2_CHAR(s,&v);
654 ret = True;
656 s++;
658 return ret;
661 /*******************************************************************
662 Convert a string to "normal" form.
663 ********************************************************************/
665 void strnorm_w(smb_ucs2_t *s, int case_default)
667 if (case_default == CASE_UPPER) {
668 strupper_w(s);
669 } else {
670 strlower_w(s);
674 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
676 smb_ucs2_t cpa, cpb;
678 while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
679 a++;
680 b++;
682 return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
683 /* warning: if *a != *b and both are not 0 we return a random
684 greater or lesser than 0 number not realted to which
685 string is longer */
688 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
690 smb_ucs2_t cpa, cpb;
691 size_t n = 0;
693 while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
694 a++;
695 b++;
696 n++;
698 return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
701 /*******************************************************************
702 Case insensitive string comparison.
703 ********************************************************************/
705 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
707 smb_ucs2_t cpa, cpb;
709 while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
710 a++;
711 b++;
713 return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
716 /*******************************************************************
717 Case insensitive string comparison, length limited.
718 ********************************************************************/
720 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
722 smb_ucs2_t cpa, cpb;
723 size_t n = 0;
725 while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
726 a++;
727 b++;
728 n++;
730 return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
733 /*******************************************************************
734 Compare 2 strings.
735 ********************************************************************/
737 bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
739 if (s1 == s2) {
740 return(True);
742 if (!s1 || !s2) {
743 return(False);
746 return(strcasecmp_w(s1,s2)==0);
749 /*******************************************************************
750 Compare 2 strings up to and including the nth char.
751 ******************************************************************/
753 bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
755 if (s1 == s2) {
756 return(True);
758 if (!s1 || !s2 || !n) {
759 return(False);
762 return(strncasecmp_w(s1,s2,n)==0);
765 /*******************************************************************
766 Duplicate string.
767 ********************************************************************/
769 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
771 return strndup_w(src, 0);
774 /* if len == 0 then duplicate the whole string */
776 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
778 smb_ucs2_t *dest;
780 if (!len) {
781 len = strlen_w(src);
783 dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
784 if (!dest) {
785 DEBUG(0,("strdup_w: out of memory!\n"));
786 return NULL;
789 memcpy(dest, src, len * sizeof(smb_ucs2_t));
790 dest[len] = 0;
791 return dest;
794 /*******************************************************************
795 Copy a string with max len.
796 ********************************************************************/
798 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
800 smb_ucs2_t cp;
801 size_t len;
803 if (!dest || !src) {
804 return NULL;
807 for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
808 cp = *COPY_UCS2_CHAR(dest+len,src+len);
810 cp = 0;
811 for ( /*nothing*/ ; len < max; len++ ) {
812 cp = *COPY_UCS2_CHAR(dest+len,&cp);
815 return dest;
818 /*******************************************************************
819 Append a string of len bytes and add a terminator.
820 ********************************************************************/
822 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
824 size_t start;
825 size_t len;
826 smb_ucs2_t z = 0;
828 if (!dest || !src) {
829 return NULL;
832 start = strlen_w(dest);
833 len = strnlen_w(src, max);
835 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
836 z = *COPY_UCS2_CHAR(dest+start+len,&z);
838 return dest;
841 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
843 size_t start;
844 size_t len;
845 smb_ucs2_t z = 0;
847 if (!dest || !src) {
848 return NULL;
851 start = strlen_w(dest);
852 len = strlen_w(src);
854 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
855 z = *COPY_UCS2_CHAR(dest+start+len,&z);
857 return dest;
861 /*******************************************************************
862 Replace any occurence of oldc with newc in unicode string.
863 ********************************************************************/
865 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
867 smb_ucs2_t cp;
869 for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
870 if(cp==oldc) {
871 COPY_UCS2_CHAR(s,&newc);
876 /*******************************************************************
877 Trim unicode string.
878 ********************************************************************/
880 bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
881 const smb_ucs2_t *back)
883 bool ret = False;
884 size_t len, front_len, back_len;
886 if (!s) {
887 return False;
890 len = strlen_w(s);
892 if (front && *front) {
893 front_len = strlen_w(front);
894 while (len && strncmp_w(s, front, front_len) == 0) {
895 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
896 len -= front_len;
897 ret = True;
901 if (back && *back) {
902 back_len = strlen_w(back);
903 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
904 s[len - back_len] = 0;
905 len -= back_len;
906 ret = True;
910 return ret;
914 The *_wa() functions take a combination of 7 bit ascii
915 and wide characters They are used so that you can use string
916 functions combining C string constants with ucs2 strings
918 The char* arguments must NOT be multibyte - to be completely sure
919 of this only pass string constants */
921 int strcmp_wa(const smb_ucs2_t *a, const char *b)
923 smb_ucs2_t cp = 0;
925 while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
926 a++;
927 b++;
929 return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
932 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
934 smb_ucs2_t cp = 0;
935 size_t n = 0;
937 while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
938 a++;
939 b++;
940 n++;
942 return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
945 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
947 smb_ucs2_t cp;
949 while (*(COPY_UCS2_CHAR(&cp,s))) {
950 int i;
951 for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++)
953 if (p[i]) {
954 return (smb_ucs2_t *)s;
956 s++;
958 return NULL;
961 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
963 smb_ucs2_t *r;
964 size_t inslen;
966 if (!s || !ins) {
967 return NULL;
970 inslen = strlen(ins);
971 r = (smb_ucs2_t *)s;
973 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
974 if (strncmp_wa(r, ins, inslen) == 0)
975 return r;
976 r++;
979 return NULL;
982 /*******************************************************************
983 Returns the length in number of wide characters.
984 ******************************************************************/
986 int unistrlen(uint16 *s)
988 int len;
990 if (!s) {
991 return -1;
994 for (len=0; SVAL(s,0); s++,len++) {
998 return len;
1001 /*******************************************************************
1002 Strcpy for unicode strings. Returns length (in num of wide chars).
1003 Not odd align safe.
1004 ********************************************************************/
1006 int unistrcpy(uint16 *dst, uint16 *src)
1008 int num_wchars = 0;
1010 while (SVAL(src,0)) {
1011 *dst++ = *src++;
1012 num_wchars++;
1014 *dst = 0;
1016 return num_wchars;
1020 * Samba ucs2 type to UNISTR2 conversion
1022 * @param ctx Talloc context to create the dst strcture (if null) and the
1023 * contents of the unicode string.
1024 * @param dst UNISTR2 destination. If equals null, then it's allocated.
1025 * @param src smb_ucs2_t source.
1026 * @param max_len maximum number of unicode characters to copy. If equals
1027 * null, then null-termination of src is taken
1029 * @return copied UNISTR2 destination
1032 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
1034 size_t len;
1036 if (!src) {
1037 return NULL;
1040 len = strlen_w(src);
1042 /* allocate UNISTR2 destination if not given */
1043 if (!dst) {
1044 dst = TALLOC_P(ctx, UNISTR2);
1045 if (!dst)
1046 return NULL;
1048 if (!dst->buffer) {
1049 dst->buffer = TALLOC_ARRAY(ctx, uint16, len + 1);
1050 if (!dst->buffer)
1051 return NULL;
1054 /* set UNISTR2 parameters */
1055 dst->uni_max_len = len + 1;
1056 dst->offset = 0;
1057 dst->uni_str_len = len;
1059 /* copy the actual unicode string */
1060 strncpy_w(dst->buffer, src, dst->uni_max_len);
1062 return dst;
1065 /*************************************************************
1066 ascii only toupper - saves the need for smbd to be in C locale.
1067 *************************************************************/
1069 int toupper_ascii(int c)
1071 smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
1072 return UCS2_TO_CHAR(uc);
1075 /*************************************************************
1076 ascii only tolower - saves the need for smbd to be in C locale.
1077 *************************************************************/
1079 int tolower_ascii(int c)
1081 smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
1082 return UCS2_TO_CHAR(uc);
1085 /*************************************************************
1086 ascii only isupper - saves the need for smbd to be in C locale.
1087 *************************************************************/
1089 int isupper_ascii(int c)
1091 return isupper_w(UCS2_CHAR(c));
1094 /*************************************************************
1095 ascii only islower - saves the need for smbd to be in C locale.
1096 *************************************************************/
1098 int islower_ascii(int c)
1100 return islower_w(UCS2_CHAR(c));