r14612: added strncasecmp_m() and fixed strcasecmp_m() for invalid codepoints
[kai/samba-autobuild/.git] / source4 / lib / util / util_str.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    
5    Copyright (C) Andrew Tridgell 1992-2001
6    Copyright (C) Simo Sorce      2001-2002
7    Copyright (C) Martin Pool     2003
8    Copyright (C) James Peach     2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "system/iconv.h"
27 #include "smb.h"
28 #include "pstring.h"
29 #include "lib/ldb/include/ldb.h"
30
31 /**
32  * @file
33  * @brief String utilities.
34  **/
35
36 /**
37  * Get the next token from a string, return False if none found.
38  * Handles double-quotes.
39  * 
40  * Based on a routine by GJC@VILLAGE.COM. 
41  * Extensively modified by Andrew.Tridgell@anu.edu.au
42  **/
43 _PUBLIC_ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
44 {
45         const char *s;
46         BOOL quoted;
47         size_t len=1;
48
49         if (!ptr)
50                 return(False);
51
52         s = *ptr;
53
54         /* default to simple separators */
55         if (!sep)
56                 sep = " \t\n\r";
57
58         /* find the first non sep char */
59         while (*s && strchr_m(sep,*s))
60                 s++;
61         
62         /* nothing left? */
63         if (! *s)
64                 return(False);
65         
66         /* copy over the token */
67         for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
68                 if (*s == '\"') {
69                         quoted = !quoted;
70                 } else {
71                         len++;
72                         *buff++ = *s;
73                 }
74         }
75         
76         *ptr = (*s) ? s+1 : s;  
77         *buff = 0;
78         
79         return(True);
80 }
81
82 /**
83  Case insensitive string compararison
84 **/
85 _PUBLIC_ int strcasecmp_m(const char *s1, const char *s2)
86 {
87         codepoint_t c1=0, c2=0;
88         size_t size1, size2;
89
90         while (*s1 && *s2) {
91                 c1 = next_codepoint(s1, &size1);
92                 c2 = next_codepoint(s2, &size2);
93
94                 s1 += size1;
95                 s2 += size2;
96
97                 if (c1 == c2) {
98                         continue;
99                 }
100
101                 if (c1 == INVALID_CODEPOINT ||
102                     c2 == INVALID_CODEPOINT) {
103                         /* what else can we do?? */
104                         return strcasecmp(s1, s2);
105                 }
106
107                 if (toupper_w(c1) != toupper_w(c2)) {
108                         return c1 - c2;
109                 }
110         }
111
112         return *s1 - *s2;
113 }
114
115 /**
116  Case insensitive string compararison, length limited
117 **/
118 _PUBLIC_ int strncasecmp_m(const char *s1, const char *s2, size_t n)
119 {
120         codepoint_t c1=0, c2=0;
121         size_t size1, size2;
122
123         while (*s1 && *s2 && n) {
124                 n--;
125
126                 c1 = next_codepoint(s1, &size1);
127                 c2 = next_codepoint(s2, &size2);
128
129                 s1 += size1;
130                 s2 += size2;
131
132                 if (c1 == c2) {
133                         continue;
134                 }
135
136                 if (c1 == INVALID_CODEPOINT ||
137                     c2 == INVALID_CODEPOINT) {
138                         /* what else can we do?? */
139                         return strcasecmp(s1, s2);
140                 }
141
142                 if (toupper_w(c1) != toupper_w(c2)) {
143                         return c1 - c2;
144                 }
145         }
146
147         if (n == 0) {
148                 return 0;
149         }
150
151         return *s1 - *s2;
152 }
153
154 /**
155  * Compare 2 strings.
156  *
157  * @note The comparison is case-insensitive.
158  **/
159 _PUBLIC_ BOOL strequal(const char *s1, const char *s2)
160 {
161         if (s1 == s2)
162                 return(True);
163         if (!s1 || !s2)
164                 return(False);
165   
166         return strcasecmp_m(s1,s2) == 0;
167 }
168
169 /**
170  Compare 2 strings (case sensitive).
171 **/
172 _PUBLIC_ BOOL strcsequal(const char *s1,const char *s2)
173 {
174         if (s1 == s2)
175                 return(True);
176         if (!s1 || !s2)
177                 return(False);
178         
179         return strcmp(s1,s2) == 0;
180 }
181
182
183 /**
184 Do a case-insensitive, whitespace-ignoring string compare.
185 **/
186 _PUBLIC_ int strwicmp(const char *psz1, const char *psz2)
187 {
188         /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
189         /* appropriate value. */
190         if (psz1 == psz2)
191                 return (0);
192         else if (psz1 == NULL)
193                 return (-1);
194         else if (psz2 == NULL)
195                 return (1);
196
197         /* sync the strings on first non-whitespace */
198         while (1) {
199                 while (isspace((int)*psz1))
200                         psz1++;
201                 while (isspace((int)*psz2))
202                         psz2++;
203                 if (toupper((unsigned char)*psz1) != toupper((unsigned char)*psz2) 
204                     || *psz1 == '\0'
205                     || *psz2 == '\0')
206                         break;
207                 psz1++;
208                 psz2++;
209         }
210         return (*psz1 - *psz2);
211 }
212
213 /**
214  String replace.
215  NOTE: oldc and newc must be 7 bit characters
216 **/
217 _PUBLIC_ void string_replace(char *s, char oldc, char newc)
218 {
219         while (*s) {
220                 size_t size;
221                 codepoint_t c = next_codepoint(s, &size);
222                 if (c == oldc) {
223                         *s = newc;
224                 }
225                 s += size;
226         }
227 }
228
229 /**
230  Trim the specified elements off the front and back of a string.
231 **/
232 _PUBLIC_ BOOL trim_string(char *s,const char *front,const char *back)
233 {
234         BOOL ret = False;
235         size_t front_len;
236         size_t back_len;
237         size_t len;
238
239         /* Ignore null or empty strings. */
240         if (!s || (s[0] == '\0'))
241                 return False;
242
243         front_len       = front? strlen(front) : 0;
244         back_len        = back? strlen(back) : 0;
245
246         len = strlen(s);
247
248         if (front_len) {
249                 while (len && strncmp(s, front, front_len)==0) {
250                         /* Must use memmove here as src & dest can
251                          * easily overlap. Found by valgrind. JRA. */
252                         memmove(s, s+front_len, (len-front_len)+1);
253                         len -= front_len;
254                         ret=True;
255                 }
256         }
257         
258         if (back_len) {
259                 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
260                         s[len-back_len]='\0';
261                         len -= back_len;
262                         ret=True;
263                 }
264         }
265         return ret;
266 }
267
268 /**
269  Find the number of 'c' chars in a string
270 **/
271 _PUBLIC_ size_t count_chars(const char *s, char c)
272 {
273         size_t count = 0;
274
275         while (*s) {
276                 size_t size;
277                 codepoint_t c2 = next_codepoint(s, &size);
278                 if (c2 == c) count++;
279                 s += size;
280         }
281
282         return count;
283 }
284
285 /**
286  Safe string copy into a known length string. maxlength does not
287  include the terminating zero.
288 **/
289 _PUBLIC_ char *safe_strcpy(char *dest,const char *src, size_t maxlength)
290 {
291         size_t len;
292
293         if (!dest) {
294                 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
295                 return NULL;
296         }
297
298 #ifdef DEVELOPER
299         /* We intentionally write out at the extremity of the destination
300          * string.  If the destination is too short (e.g. pstrcpy into mallocd
301          * or fstring) then this should cause an error under a memory
302          * checker. */
303         dest[maxlength] = '\0';
304         if (PTR_DIFF(&len, dest) > 0) {  /* check if destination is on the stack, ok if so */
305                 log_suspicious_usage("safe_strcpy", src);
306         }
307 #endif
308
309         if (!src) {
310                 *dest = 0;
311                 return dest;
312         }  
313
314         len = strlen(src);
315
316         if (len > maxlength) {
317                 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
318                          (uint_t)(len-maxlength), (unsigned)len, (unsigned)maxlength, src));
319                 len = maxlength;
320         }
321       
322         memmove(dest, src, len);
323         dest[len] = 0;
324         return dest;
325 }  
326
327 /**
328  Safe string cat into a string. maxlength does not
329  include the terminating zero.
330 **/
331 _PUBLIC_ char *safe_strcat(char *dest, const char *src, size_t maxlength)
332 {
333         size_t src_len, dest_len;
334
335         if (!dest) {
336                 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
337                 return NULL;
338         }
339
340         if (!src)
341                 return dest;
342         
343 #ifdef DEVELOPER
344         if (PTR_DIFF(&src_len, dest) > 0) {  /* check if destination is on the stack, ok if so */
345                 log_suspicious_usage("safe_strcat", src);
346         }
347 #endif
348         src_len = strlen(src);
349         dest_len = strlen(dest);
350
351         if (src_len + dest_len > maxlength) {
352                 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
353                          (int)(src_len + dest_len - maxlength), src));
354                 if (maxlength > dest_len) {
355                         memcpy(&dest[dest_len], src, maxlength - dest_len);
356                 }
357                 dest[maxlength] = 0;
358                 return NULL;
359         }
360         
361         memcpy(&dest[dest_len], src, src_len);
362         dest[dest_len + src_len] = 0;
363         return dest;
364 }
365
366 /**
367  Paranoid strcpy into a buffer of given length (includes terminating
368  zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
369  and replaces with '_'. Deliberately does *NOT* check for multibyte
370  characters. Don't change it !
371 **/
372
373 _PUBLIC_ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
374 {
375         size_t len, i;
376
377         if (maxlength == 0) {
378                 /* can't fit any bytes at all! */
379                 return NULL;
380         }
381
382         if (!dest) {
383                 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
384                 return NULL;
385         }
386
387         if (!src) {
388                 *dest = 0;
389                 return dest;
390         }  
391
392         len = strlen(src);
393         if (len >= maxlength)
394                 len = maxlength - 1;
395
396         if (!other_safe_chars)
397                 other_safe_chars = "";
398
399         for(i = 0; i < len; i++) {
400                 int val = (src[i] & 0xff);
401                 if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
402                         dest[i] = src[i];
403                 else
404                         dest[i] = '_';
405         }
406
407         dest[i] = '\0';
408
409         return dest;
410 }
411
412 /**
413  Like strncpy but always null terminates. Make sure there is room!
414  The variable n should always be one less than the available size.
415 **/
416
417 _PUBLIC_ char *StrnCpy(char *dest,const char *src,size_t n)
418 {
419         char *d = dest;
420         if (!dest)
421                 return(NULL);
422         if (!src) {
423                 *dest = 0;
424                 return(dest);
425         }
426         while (n-- && (*d++ = *src++))
427                 ;
428         *d = 0;
429         return(dest);
430 }
431
432
433 /**
434  Routine to get hex characters and turn them into a 16 byte array.
435  the array can be variable length, and any non-hex-numeric
436  characters are skipped.  "0xnn" or "0Xnn" is specially catered
437  for.
438
439  valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
440
441
442 **/
443 _PUBLIC_ size_t strhex_to_str(char *p, size_t len, const char *strhex)
444 {
445         size_t i;
446         size_t num_chars = 0;
447         uint8_t   lonybble, hinybble;
448         const char     *hexchars = "0123456789ABCDEF";
449         char           *p1 = NULL, *p2 = NULL;
450
451         for (i = 0; i < len && strhex[i] != 0; i++) {
452                 if (strncasecmp(hexchars, "0x", 2) == 0) {
453                         i++; /* skip two chars */
454                         continue;
455                 }
456
457                 if (!(p1 = strchr_m(hexchars, toupper((unsigned char)strhex[i]))))
458                         break;
459
460                 i++; /* next hex digit */
461
462                 if (!(p2 = strchr_m(hexchars, toupper((unsigned char)strhex[i]))))
463                         break;
464
465                 /* get the two nybbles */
466                 hinybble = PTR_DIFF(p1, hexchars);
467                 lonybble = PTR_DIFF(p2, hexchars);
468
469                 p[num_chars] = (hinybble << 4) | lonybble;
470                 num_chars++;
471
472                 p1 = NULL;
473                 p2 = NULL;
474         }
475         return num_chars;
476 }
477
478 /** 
479  * Parse a hex string and return a data blob. 
480  */
481 _PUBLIC_ DATA_BLOB strhex_to_data_blob(const char *strhex) 
482 {
483         DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
484
485         ret_blob.length = strhex_to_str((char *)ret_blob.data,  
486                                         strlen(strhex), 
487                                         strhex);
488
489         return ret_blob;
490 }
491
492
493 /**
494  * Routine to print a buffer as HEX digits, into an allocated string.
495  */
496 _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
497 {
498         int i;
499         char *hex_buffer;
500
501         *out_hex_buffer = smb_xmalloc((len*2)+1);
502         hex_buffer = *out_hex_buffer;
503
504         for (i = 0; i < len; i++)
505                 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
506 }
507
508 /**
509  Check if a string is part of a list.
510 **/
511 _PUBLIC_ BOOL in_list(const char *s, const char *list, BOOL casesensitive)
512 {
513         pstring tok;
514         const char *p=list;
515
516         if (!list)
517                 return(False);
518
519         while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
520                 if (casesensitive) {
521                         if (strcmp(tok,s) == 0)
522                                 return(True);
523                 } else {
524                         if (strcasecmp_m(tok,s) == 0)
525                                 return(True);
526                 }
527         }
528         return(False);
529 }
530
531 /**
532  Set a string value, allocing the space for the string
533 **/
534 static BOOL string_init(char **dest,const char *src)
535 {
536         if (!src) src = "";
537
538         (*dest) = strdup(src);
539         if ((*dest) == NULL) {
540                 DEBUG(0,("Out of memory in string_init\n"));
541                 return False;
542         }
543         return True;
544 }
545
546 /**
547  Free a string value.
548 **/
549 _PUBLIC_ void string_free(char **s)
550 {
551         if (s) SAFE_FREE(*s);
552 }
553
554 /**
555  Set a string value, deallocating any existing space, and allocing the space
556  for the string
557 **/
558 _PUBLIC_ BOOL string_set(char **dest, const char *src)
559 {
560         string_free(dest);
561         return string_init(dest,src);
562 }
563
564 /**
565  Substitute a string for a pattern in another string. Make sure there is 
566  enough room!
567
568  This routine looks for pattern in s and replaces it with 
569  insert. It may do multiple replacements.
570
571  Any of " ; ' $ or ` in the insert string are replaced with _
572  if len==0 then the string cannot be extended. This is different from the old
573  use of len==0 which was for no length checks to be done.
574 **/
575
576 _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len)
577 {
578         char *p;
579         ssize_t ls,lp,li, i;
580
581         if (!insert || !pattern || !*pattern || !s)
582                 return;
583
584         ls = (ssize_t)strlen(s);
585         lp = (ssize_t)strlen(pattern);
586         li = (ssize_t)strlen(insert);
587
588         if (len == 0)
589                 len = ls + 1; /* len is number of *bytes* */
590
591         while (lp <= ls && (p = strstr(s,pattern))) {
592                 if (ls + (li-lp) >= len) {
593                         DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", 
594                                  (int)(ls + (li-lp) - len),
595                                  pattern, (int)len));
596                         break;
597                 }
598                 if (li != lp) {
599                         memmove(p+li,p+lp,strlen(p+lp)+1);
600                 }
601                 for (i=0;i<li;i++) {
602                         switch (insert[i]) {
603                         case '`':
604                         case '"':
605                         case '\'':
606                         case ';':
607                         case '$':
608                         case '%':
609                         case '\r':
610                         case '\n':
611                                 p[i] = '_';
612                                 break;
613                         default:
614                                 p[i] = insert[i];
615                         }
616                 }
617                 s = p + li;
618                 ls += (li-lp);
619         }
620 }
621
622
623 /**
624  Similar to string_sub() but allows for any character to be substituted. 
625  Use with caution!
626  if len==0 then the string cannot be extended. This is different from the old
627  use of len==0 which was for no length checks to be done.
628 **/
629
630 _PUBLIC_ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
631 {
632         char *p;
633         ssize_t ls,lp,li;
634
635         if (!insert || !pattern || !s)
636                 return;
637
638         ls = (ssize_t)strlen(s);
639         lp = (ssize_t)strlen(pattern);
640         li = (ssize_t)strlen(insert);
641
642         if (!*pattern)
643                 return;
644         
645         if (len == 0)
646                 len = ls + 1; /* len is number of *bytes* */
647         
648         while (lp <= ls && (p = strstr(s,pattern))) {
649                 if (ls + (li-lp) >= len) {
650                         DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", 
651                                  (int)(ls + (li-lp) - len),
652                                  pattern, (int)len));
653                         break;
654                 }
655                 if (li != lp) {
656                         memmove(p+li,p+lp,strlen(p+lp)+1);
657                 }
658                 memcpy(p, insert, li);
659                 s = p + li;
660                 ls += (li-lp);
661         }
662 }
663
664
665 /**
666  Strchr and strrchr_m are a bit complex on general multi-byte strings. 
667 **/
668 _PUBLIC_ char *strchr_m(const char *s, char c)
669 {
670         /* characters below 0x3F are guaranteed to not appear in
671            non-initial position in multi-byte charsets */
672         if ((c & 0xC0) == 0) {
673                 return strchr(s, c);
674         }
675
676         while (*s) {
677                 size_t size;
678                 codepoint_t c2 = next_codepoint(s, &size);
679                 if (c2 == c) {
680                         return discard_const(s);
681                 }
682                 s += size;
683         }
684
685         return NULL;
686 }
687
688 /**
689  * Multibyte-character version of strrchr
690  */
691 _PUBLIC_ char *strrchr_m(const char *s, char c)
692 {
693         char *ret = NULL;
694
695         /* characters below 0x3F are guaranteed to not appear in
696            non-initial position in multi-byte charsets */
697         if ((c & 0xC0) == 0) {
698                 return strrchr(s, c);
699         }
700
701         while (*s) {
702                 size_t size;
703                 codepoint_t c2 = next_codepoint(s, &size);
704                 if (c2 == c) {
705                         ret = discard_const(s);
706                 }
707                 s += size;
708         }
709
710         return ret;
711 }
712
713 /**
714   return True if any (multi-byte) character is lower case
715 */
716 _PUBLIC_ BOOL strhaslower(const char *string)
717 {
718         while (*string) {
719                 size_t c_size;
720                 codepoint_t s;
721                 codepoint_t t;
722
723                 s = next_codepoint(string, &c_size);
724                 string += c_size;
725
726                 t = toupper_w(s);
727
728                 if (s != t) {
729                         return True; /* that means it has lower case chars */
730                 }
731         }
732
733         return False;
734
735
736 /**
737   return True if any (multi-byte) character is upper case
738 */
739 _PUBLIC_ BOOL strhasupper(const char *string)
740 {
741         while (*string) {
742                 size_t c_size;
743                 codepoint_t s;
744                 codepoint_t t;
745
746                 s = next_codepoint(string, &c_size);
747                 string += c_size;
748
749                 t = tolower_w(s);
750
751                 if (s != t) {
752                         return True; /* that means it has upper case chars */
753                 }
754         }
755
756         return False;
757
758
759 /**
760  Convert a string to lower case, allocated with talloc
761 **/
762 _PUBLIC_ char *strlower_talloc(TALLOC_CTX *ctx, const char *src)
763 {
764         size_t size=0;
765         char *dest;
766
767         /* this takes advantage of the fact that upper/lower can't
768            change the length of a character by more than 1 byte */
769         dest = talloc_size(ctx, 2*(strlen(src))+1);
770         if (dest == NULL) {
771                 return NULL;
772         }
773
774         while (*src) {
775                 size_t c_size;
776                 codepoint_t c = next_codepoint(src, &c_size);
777                 src += c_size;
778
779                 c = tolower_w(c);
780
781                 c_size = push_codepoint(dest+size, c);
782                 if (c_size == -1) {
783                         talloc_free(dest);
784                         return NULL;
785                 }
786                 size += c_size;
787         }
788
789         dest[size] = 0;
790
791         return dest;
792 }
793
794 /**
795  Convert a string to UPPER case, allocated with talloc
796 **/
797 _PUBLIC_ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
798 {
799         size_t size=0;
800         char *dest;
801         
802         if (!src) {
803                 return NULL;
804         }
805
806         /* this takes advantage of the fact that upper/lower can't
807            change the length of a character by more than 1 byte */
808         dest = talloc_size(ctx, 2*(strlen(src))+1);
809         if (dest == NULL) {
810                 return NULL;
811         }
812
813         while (*src) {
814                 size_t c_size;
815                 codepoint_t c = next_codepoint(src, &c_size);
816                 src += c_size;
817
818                 c = toupper_w(c);
819
820                 c_size = push_codepoint(dest+size, c);
821                 if (c_size == -1) {
822                         talloc_free(dest);
823                         return NULL;
824                 }
825                 size += c_size;
826         }
827
828         dest[size] = 0;
829
830         return dest;
831 }
832
833 /**
834  Convert a string to lower case.
835 **/
836 _PUBLIC_ void strlower_m(char *s)
837 {
838         char *d;
839
840         /* this is quite a common operation, so we want it to be
841            fast. We optimise for the ascii case, knowing that all our
842            supported multi-byte character sets are ascii-compatible
843            (ie. they match for the first 128 chars) */
844         while (*s && !(((uint8_t)*s) & 0x80)) {
845                 *s = tolower((uint8_t)*s);
846                 s++;
847         }
848
849         if (!*s)
850                 return;
851
852         d = s;
853
854         while (*s) {
855                 size_t c_size, c_size2;
856                 codepoint_t c = next_codepoint(s, &c_size);
857                 c_size2 = push_codepoint(d, tolower_w(c));
858                 if (c_size2 > c_size) {
859                         DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
860                                  c, tolower_w(c), (int)c_size, (int)c_size2));
861                         smb_panic("codepoint expansion in strlower_m\n");
862                 }
863                 s += c_size;
864                 d += c_size2;
865         }
866         *d = 0;
867 }
868
869 /**
870  Convert a string to UPPER case.
871 **/
872 _PUBLIC_ void strupper_m(char *s)
873 {
874         char *d;
875
876         /* this is quite a common operation, so we want it to be
877            fast. We optimise for the ascii case, knowing that all our
878            supported multi-byte character sets are ascii-compatible
879            (ie. they match for the first 128 chars) */
880         while (*s && !(((uint8_t)*s) & 0x80)) {
881                 *s = toupper((uint8_t)*s);
882                 s++;
883         }
884
885         if (!*s)
886                 return;
887
888         d = s;
889
890         while (*s) {
891                 size_t c_size, c_size2;
892                 codepoint_t c = next_codepoint(s, &c_size);
893                 c_size2 = push_codepoint(d, toupper_w(c));
894                 if (c_size2 > c_size) {
895                         DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
896                                  c, toupper_w(c), (int)c_size, (int)c_size2));
897                         smb_panic("codepoint expansion in strupper_m\n");
898                 }
899                 s += c_size;
900                 d += c_size2;
901         }
902         *d = 0;
903 }
904
905 /**
906  Count the number of UCS2 characters in a string. Normally this will
907  be the same as the number of bytes in a string for single byte strings,
908  but will be different for multibyte.
909 **/
910 _PUBLIC_ size_t strlen_m(const char *s)
911 {
912         size_t count = 0;
913
914         if (!s) {
915                 return 0;
916         }
917
918         while (*s && !(((uint8_t)*s) & 0x80)) {
919                 s++;
920                 count++;
921         }
922
923         if (!*s) {
924                 return count;
925         }
926
927         while (*s) {
928                 size_t c_size;
929                 codepoint_t c = next_codepoint(s, &c_size);
930                 if (c < 0x10000) {
931                         count += 1;
932                 } else {
933                         count += 2;
934                 }
935                 s += c_size;
936         }
937
938         return count;
939 }
940
941 /**
942    Work out the number of multibyte chars in a string, including the NULL
943    terminator.
944 **/
945 _PUBLIC_ size_t strlen_m_term(const char *s)
946 {
947         if (!s) {
948                 return 0;
949         }
950
951         return strlen_m(s) + 1;
952 }
953
954 /**
955  Unescape a URL encoded string, in place.
956 **/
957
958 _PUBLIC_ void rfc1738_unescape(char *buf)
959 {
960         char *p=buf;
961
962         while ((p=strchr_m(p,'+')))
963                 *p = ' ';
964
965         p = buf;
966
967         while (p && *p && (p=strchr_m(p,'%'))) {
968                 int c1 = p[1];
969                 int c2 = p[2];
970
971                 if (c1 >= '0' && c1 <= '9')
972                         c1 = c1 - '0';
973                 else if (c1 >= 'A' && c1 <= 'F')
974                         c1 = 10 + c1 - 'A';
975                 else if (c1 >= 'a' && c1 <= 'f')
976                         c1 = 10 + c1 - 'a';
977                 else {p++; continue;}
978
979                 if (c2 >= '0' && c2 <= '9')
980                         c2 = c2 - '0';
981                 else if (c2 >= 'A' && c2 <= 'F')
982                         c2 = 10 + c2 - 'A';
983                 else if (c2 >= 'a' && c2 <= 'f')
984                         c2 = 10 + c2 - 'a';
985                 else {p++; continue;}
986                         
987                 *p = (c1<<4) | c2;
988
989                 memmove(p+1, p+3, strlen(p+3)+1);
990                 p++;
991         }
992 }
993
994 /**
995  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
996  **/
997 _PUBLIC_ DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
998 {
999         DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
1000         ret.length = ldb_base64_decode((char *)ret.data);
1001         return ret;
1002 }
1003
1004 /**
1005  * Decode a base64 string in-place - wrapper for the above
1006  **/
1007 _PUBLIC_ void base64_decode_inplace(char *s)
1008 {
1009         ldb_base64_decode(s);
1010 }
1011
1012 /**
1013  * Encode a base64 string into a talloc()ed string caller to free.
1014  **/
1015 _PUBLIC_ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
1016 {
1017         return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
1018 }
1019
1020 #ifdef VALGRIND
1021 size_t valgrind_strlen(const char *s)
1022 {
1023         size_t count;
1024         for(count = 0; *s++; count++)
1025                 ;
1026         return count;
1027 }
1028 #endif
1029
1030
1031 /**
1032   format a string into length-prefixed dotted domain format, as used in NBT
1033   and in some ADS structures
1034 **/
1035 _PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
1036 {
1037         char *ret;
1038         int i;
1039         if (!s || !*s) {
1040                 return talloc_strdup(mem_ctx, "");
1041         }
1042         ret = talloc_size(mem_ctx, strlen(s)+2);
1043         if (!ret) {
1044                 return ret;
1045         }
1046         
1047         memcpy(ret+1, s, strlen(s)+1);
1048         ret[0] = '.';
1049
1050         for (i=0;ret[i];i++) {
1051                 if (ret[i] == '.') {
1052                         char *p = strchr(ret+i+1, '.');
1053                         if (p) {
1054                                 ret[i] = p-(ret+i+1);
1055                         } else {
1056                                 ret[i] = strlen(ret+i+1);
1057                         }
1058                 }
1059         }
1060
1061         return ret;
1062 }
1063
1064 /**
1065  * Add a string to an array of strings.
1066  *
1067  * num should be a pointer to an integer that holds the current 
1068  * number of elements in strings. It will be updated by this function.
1069  */
1070 _PUBLIC_ BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
1071                          const char *str, const char ***strings, int *num)
1072 {
1073         char *dup_str = talloc_strdup(mem_ctx, str);
1074
1075         *strings = talloc_realloc(mem_ctx,
1076                                     *strings,
1077                                     const char *, ((*num)+1));
1078
1079         if ((*strings == NULL) || (dup_str == NULL))
1080                 return False;
1081
1082         (*strings)[*num] = dup_str;
1083         *num += 1;
1084
1085         return True;
1086 }
1087
1088
1089
1090 /**
1091   varient of strcmp() that handles NULL ptrs
1092 **/
1093 _PUBLIC_ int strcmp_safe(const char *s1, const char *s2)
1094 {
1095         if (s1 == s2) {
1096                 return 0;
1097         }
1098         if (s1 == NULL || s2 == NULL) {
1099                 return s1?-1:1;
1100         }
1101         return strcmp(s1, s2);
1102 }
1103
1104
1105 /**
1106 return the number of bytes occupied by a buffer in ASCII format
1107 the result includes the null termination
1108 limited by 'n' bytes
1109 **/
1110 _PUBLIC_ size_t ascii_len_n(const char *src, size_t n)
1111 {
1112         size_t len;
1113
1114         len = strnlen(src, n);
1115         if (len+1 <= n) {
1116                 len += 1;
1117         }
1118
1119         return len;
1120 }
1121
1122
1123 /**
1124  Return a string representing a CIFS attribute for a file.
1125 **/
1126 _PUBLIC_ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
1127 {
1128         int i, len;
1129         const struct {
1130                 char c;
1131                 uint16_t attr;
1132         } attr_strs[] = {
1133                 {'V', FILE_ATTRIBUTE_VOLUME},
1134                 {'D', FILE_ATTRIBUTE_DIRECTORY},
1135                 {'A', FILE_ATTRIBUTE_ARCHIVE},
1136                 {'H', FILE_ATTRIBUTE_HIDDEN},
1137                 {'S', FILE_ATTRIBUTE_SYSTEM},
1138                 {'N', FILE_ATTRIBUTE_NORMAL},
1139                 {'R', FILE_ATTRIBUTE_READONLY},
1140                 {'d', FILE_ATTRIBUTE_DEVICE},
1141                 {'t', FILE_ATTRIBUTE_TEMPORARY},
1142                 {'s', FILE_ATTRIBUTE_SPARSE},
1143                 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
1144                 {'c', FILE_ATTRIBUTE_COMPRESSED},
1145                 {'o', FILE_ATTRIBUTE_OFFLINE},
1146                 {'n', FILE_ATTRIBUTE_NONINDEXED},
1147                 {'e', FILE_ATTRIBUTE_ENCRYPTED}
1148         };
1149         char *ret;
1150
1151         ret = talloc_size(mem_ctx, ARRAY_SIZE(attr_strs)+1);
1152         if (!ret) {
1153                 return NULL;
1154         }
1155
1156         for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
1157                 if (attrib & attr_strs[i].attr) {
1158                         ret[len++] = attr_strs[i].c;
1159                 }
1160         }
1161
1162         ret[len] = 0;
1163
1164         return ret;
1165 }
1166
1167 /**
1168  Set a boolean variable from the text value stored in the passed string.
1169  Returns True in success, False if the passed string does not correctly 
1170  represent a boolean.
1171 **/
1172
1173 _PUBLIC_ BOOL set_boolean(const char *boolean_string, BOOL *boolean)
1174 {
1175         if (strwicmp(boolean_string, "yes") == 0 ||
1176             strwicmp(boolean_string, "true") == 0 ||
1177             strwicmp(boolean_string, "on") == 0 ||
1178             strwicmp(boolean_string, "1") == 0) {
1179                 *boolean = True;
1180                 return True;
1181         } else if (strwicmp(boolean_string, "no") == 0 ||
1182                    strwicmp(boolean_string, "false") == 0 ||
1183                    strwicmp(boolean_string, "off") == 0 ||
1184                    strwicmp(boolean_string, "0") == 0) {
1185                 *boolean = False;
1186                 return True;
1187         }
1188         return False;
1189 }
1190
1191 /**
1192  * Parse a string containing a boolean value.
1193  *
1194  * val will be set to the read value.
1195  *
1196  * @retval True if a boolean value was parsed, False otherwise.
1197  */
1198 _PUBLIC_ BOOL conv_str_bool(const char * str, BOOL * val)
1199 {
1200         char *  end = NULL;
1201         long    lval;
1202
1203         if (str == NULL || *str == '\0') {
1204                 return False;
1205         }
1206
1207         lval = strtol(str, &end, 10 /* base */);
1208         if (end == NULL || *end != '\0' || end == str) {
1209                 return set_boolean(str, val);
1210         }
1211
1212         *val = (lval) ? True : False;
1213         return True;
1214 }
1215
1216 /**
1217  * Convert a size specification like 16K into an integral number of bytes. 
1218  **/
1219 _PUBLIC_ BOOL conv_str_size(const char * str, uint64_t * val)
1220 {
1221         char *              end = NULL;
1222         unsigned long long  lval;
1223
1224         if (str == NULL || *str == '\0') {
1225                 return False;
1226         }
1227
1228         lval = strtoull(str, &end, 10 /* base */);
1229         if (end == NULL || end == str) {
1230                 return False;
1231         }
1232
1233         if (*end) {
1234                 if (strwicmp(end, "K") == 0) {
1235                         lval *= 1024ULL;
1236                 } else if (strwicmp(end, "M") == 0) {
1237                         lval *= (1024ULL * 1024ULL);
1238                 } else if (strwicmp(end, "G") == 0) {
1239                         lval *= (1024ULL * 1024ULL * 1024ULL);
1240                 } else if (strwicmp(end, "T") == 0) {
1241                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
1242                 } else if (strwicmp(end, "P") == 0) {
1243                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
1244                 } else {
1245                         return False;
1246                 }
1247         }
1248
1249         *val = (uint64_t)lval;
1250         return True;
1251 }
1252
1253 /**
1254  * Parse a uint64_t value from a string
1255  *
1256  * val will be set to the value read.
1257  *
1258  * @retval True if parsing was successful, False otherwise
1259  */
1260 _PUBLIC_ BOOL conv_str_u64(const char * str, uint64_t * val)
1261 {
1262         char *              end = NULL;
1263         unsigned long long  lval;
1264
1265         if (str == NULL || *str == '\0') {
1266                 return False;
1267         }
1268
1269         lval = strtoull(str, &end, 10 /* base */);
1270         if (end == NULL || *end != '\0' || end == str) {
1271                 return False;
1272         }
1273
1274         *val = (uint64_t)lval;
1275         return True;
1276 }