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