2 Unix SMB/CIFS implementation.
3 Samba utility functions
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 2006
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.
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.
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.
29 * @brief String utilities.
33 * Internal function to get the next token from a string, return False if none
34 * found. Handles double-quotes. This is the work horse function called by
35 * next_token() and next_token_no_ltrim().
37 * Based on a routine by GJC@VILLAGE.COM.
38 * Extensively modified by Andrew.Tridgell@anu.edu.au
40 static BOOL next_token_internal(const char **ptr,
56 /* default to simple separators */
60 /* find the first non sep char, if left-trimming is requested */
62 while (*s && strchr_m(sep,*s))
70 /* copy over the token */
72 for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
81 *ptr = (*s) ? s+1 : s;
88 * Get the next token from a string, return False if none found. Handles
89 * double-quotes. This version trims leading separator characters before
90 * looking for a token.
92 BOOL next_token(const char **ptr, char *buff, const char *sep, size_t bufsize)
94 return next_token_internal(ptr, buff, sep, bufsize, True);
98 * Get the next token from a string, return False if none found. Handles
99 * double-quotes. This version does not trim leading separator characters
100 * before looking for a token.
102 BOOL next_token_no_ltrim(const char **ptr,
107 return next_token_internal(ptr, buff, sep, bufsize, False);
111 This is like next_token but is not re-entrant and "remembers" the first
112 parameter so you can pass NULL. This is useful for user interface code
113 but beware the fact that it is not re-entrant!
116 static const char *last_ptr=NULL;
118 BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize)
124 ret = next_token(ptr, buff, sep, bufsize);
129 static uint16 tmpbuf[sizeof(pstring)];
131 void set_first_token(char *ptr)
137 Convert list of tokens to array; dependent on above routine.
138 Uses last_ptr from above - bit of a hack.
141 char **toktocliplist(int *ctok, const char *sep)
143 char *s=(char *)last_ptr;
150 while(*s && strchr_m(sep,*s))
159 while(*s && (!strchr_m(sep,*s)))
161 while(*s && strchr_m(sep,*s))
168 if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1)))
186 * Case insensitive string compararison.
188 * iconv does not directly give us a way to compare strings in
189 * arbitrary unix character sets -- all we can is convert and then
190 * compare. This is expensive.
192 * As an optimization, we do a first pass that considers only the
193 * prefix of the strings that is entirely 7-bit. Within this, we
194 * check whether they have the same value.
196 * Hopefully this will often give the answer without needing to copy.
197 * In particular it should speed comparisons to literal ascii strings
198 * or comparisons of strings that are "obviously" different.
200 * If we find a non-ascii character we fall back to converting via
203 * This should never be slower than convering the whole thing, and
206 * A different optimization would be to compare for bitwise equality
207 * in the binary encoding. (It would be possible thought hairy to do
208 * both simultaneously.) But in that case if they turn out to be
209 * different, we'd need to restart the whole thing.
211 * Even better is to implement strcasecmp for each encoding and use a
214 int StrCaseCmp(const char *s, const char *t)
219 smb_ucs2_t *buffer_s, *buffer_t;
222 for (ps = s, pt = t; ; ps++, pt++) {
226 return 0; /* both ended */
228 return -1; /* s is a prefix */
230 return +1; /* t is a prefix */
231 else if ((*ps & 0x80) || (*pt & 0x80))
232 /* not ascii anymore, do it the hard way from here on in */
235 us = toupper_ascii(*ps);
236 ut = toupper_ascii(*pt);
245 size = push_ucs2_allocate(&buffer_s, ps);
246 if (size == (size_t)-1) {
247 return strcmp(ps, pt);
248 /* Not quite the right answer, but finding the right one
249 under this failure case is expensive, and it's pretty close */
252 size = push_ucs2_allocate(&buffer_t, pt);
253 if (size == (size_t)-1) {
255 return strcmp(ps, pt);
256 /* Not quite the right answer, but finding the right one
257 under this failure case is expensive, and it's pretty close */
260 ret = strcasecmp_w(buffer_s, buffer_t);
268 Case insensitive string compararison, length limited.
270 int StrnCaseCmp(const char *s, const char *t, size_t n)
273 unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1));
274 unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2));
275 return strncmp(buf1,buf2,n);
281 * @note The comparison is case-insensitive.
283 BOOL strequal(const char *s1, const char *s2)
290 return(StrCaseCmp(s1,s2)==0);
294 * Compare 2 strings up to and including the nth char.
296 * @note The comparison is case-insensitive.
298 BOOL strnequal(const char *s1,const char *s2,size_t n)
302 if (!s1 || !s2 || !n)
305 return(StrnCaseCmp(s1,s2,n)==0);
309 Compare 2 strings (case sensitive).
312 BOOL strcsequal(const char *s1,const char *s2)
319 return(strcmp(s1,s2)==0);
323 Do a case-insensitive, whitespace-ignoring string compare.
326 int strwicmp(const char *psz1, const char *psz2)
328 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
329 /* appropriate value. */
332 else if (psz1 == NULL)
334 else if (psz2 == NULL)
337 /* sync the strings on first non-whitespace */
339 while (isspace((int)*psz1))
341 while (isspace((int)*psz2))
343 if (toupper_ascii(*psz1) != toupper_ascii(*psz2) || *psz1 == '\0'
349 return (*psz1 - *psz2);
354 Convert a string to upper case, but don't modify it.
357 char *strupper_static(const char *s)
368 Convert a string to "normal" form.
371 void strnorm(char *s, int case_default)
373 if (case_default == CASE_UPPER)
380 Check if a string is in "normal" case.
383 BOOL strisnormal(const char *s, int case_default)
385 if (case_default == CASE_UPPER)
386 return(!strhaslower(s));
388 return(!strhasupper(s));
394 NOTE: oldc and newc must be 7 bit characters
397 void string_replace( pstring s, char oldc, char newc )
401 /* this is quite a common operation, so we want it to be
402 fast. We optimise for the ascii case, knowing that all our
403 supported multi-byte character sets are ascii-compatible
404 (ie. they match for the first 128 chars) */
406 for (p = s; *p; p++) {
407 if (*p & 0x80) /* mb string - slow path. */
416 /* Slow (mb) path. */
417 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
418 /* With compose characters we must restart from the beginning. JRA. */
421 push_ucs2(NULL, tmpbuf, p, sizeof(tmpbuf), STR_TERMINATE);
422 string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc));
423 pull_ucs2(NULL, p, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE);
427 Skip past some strings in a buffer.
430 char *skip_string(char *buf,size_t n)
433 buf += strlen(buf) + 1;
438 Count the number of characters in a string. Normally this will
439 be the same as the number of bytes in a string for single byte strings,
440 but will be different for multibyte.
443 size_t str_charnum(const char *s)
445 uint16 tmpbuf2[sizeof(pstring)];
446 push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE);
447 return strlen_w(tmpbuf2);
451 Count the number of characters in a string. Normally this will
452 be the same as the number of bytes in a string for single byte strings,
453 but will be different for multibyte.
456 size_t str_ascii_charnum(const char *s)
459 push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE);
460 return strlen(tmpbuf2);
463 BOOL trim_char(char *s,char cfront,char cback)
469 /* Ignore null or empty strings. */
470 if (!s || (s[0] == '\0'))
474 while (*fp && *fp == cfront)
477 /* We ate the string. */
485 ep = fp + strlen(fp) - 1;
487 /* Attempt ascii only. Bail for mb strings. */
488 while ((ep >= fp) && (*ep == cback)) {
490 if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
491 /* Could be mb... bail back to tim_string. */
499 return trim_string(s, cfront ? fs : NULL, bs);
505 /* We ate the string. */
512 memmove(s, fp, ep-fp+2);
517 Trim the specified elements off the front and back of a string.
520 BOOL trim_string(char *s,const char *front,const char *back)
527 /* Ignore null or empty strings. */
528 if (!s || (s[0] == '\0'))
531 front_len = front? strlen(front) : 0;
532 back_len = back? strlen(back) : 0;
537 while (len && strncmp(s, front, front_len)==0) {
538 /* Must use memmove here as src & dest can
539 * easily overlap. Found by valgrind. JRA. */
540 memmove(s, s+front_len, (len-front_len)+1);
547 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
548 s[len-back_len]='\0';
557 Does a string have any uppercase chars in it?
560 BOOL strhasupper(const char *s)
563 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
564 for(ptr=tmpbuf;*ptr;ptr++)
571 Does a string have any lowercase chars in it?
574 BOOL strhaslower(const char *s)
577 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
578 for(ptr=tmpbuf;*ptr;ptr++)
585 Find the number of 'c' chars in a string
588 size_t count_chars(const char *s,char c)
592 smb_ucs2_t *alloc_tmpbuf = NULL;
594 if (push_ucs2_allocate(&alloc_tmpbuf, s) == (size_t)-1) {
598 for(count=0,ptr=alloc_tmpbuf;*ptr;ptr++)
599 if(*ptr==UCS2_CHAR(c))
602 SAFE_FREE(alloc_tmpbuf);
607 Safe string copy into a known length string. maxlength does not
608 include the terminating zero.
611 char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_t maxlength)
616 DEBUG(0,("ERROR: NULL dest in safe_strcpy, called from [%s][%d]\n", fn, line));
621 clobber_region(fn,line,dest, maxlength+1);
629 len = strnlen(src, maxlength+1);
631 if (len > maxlength) {
632 DEBUG(0,("ERROR: string overflow by %lu (%lu - %lu) in safe_strcpy [%.50s]\n",
633 (unsigned long)(len-maxlength), (unsigned long)len,
634 (unsigned long)maxlength, src));
638 memmove(dest, src, len);
644 Safe string cat into a string. maxlength does not
645 include the terminating zero.
647 char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size_t maxlength)
649 size_t src_len, dest_len;
652 DEBUG(0,("ERROR: NULL dest in safe_strcat, called from [%s][%d]\n", fn, line));
659 src_len = strnlen(src, maxlength + 1);
660 dest_len = strnlen(dest, maxlength + 1);
663 clobber_region(fn, line, dest + dest_len, maxlength + 1 - dest_len);
666 if (src_len + dest_len > maxlength) {
667 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
668 (int)(src_len + dest_len - maxlength), src));
669 if (maxlength > dest_len) {
670 memcpy(&dest[dest_len], src, maxlength - dest_len);
676 memcpy(&dest[dest_len], src, src_len);
677 dest[dest_len + src_len] = 0;
682 Paranoid strcpy into a buffer of given length (includes terminating
683 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
684 and replaces with '_'. Deliberately does *NOT* check for multibyte
685 characters. Don't change it !
687 char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
692 clobber_region(fn, line, dest, maxlength);
696 DEBUG(0,("ERROR: NULL dest in alpha_strcpy, called from [%s][%d]\n", fn, line));
706 if (len >= maxlength)
709 if (!other_safe_chars)
710 other_safe_chars = "";
712 for(i = 0; i < len; i++) {
713 int val = (src[i] & 0xff);
714 if (isupper_ascii(val) || islower_ascii(val) || isdigit(val) || strchr_m(other_safe_chars, val))
726 Like strncpy but always null terminates. Make sure there is room!
727 The variable n should always be one less than the available size.
729 char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n)
734 clobber_region(fn, line, dest, n+1);
738 DEBUG(0,("ERROR: NULL dest in StrnCpy, called from [%s][%d]\n", fn, line));
747 while (n-- && (*d = *src)) {
758 Like strncpy but copies up to the character marker. always null terminates.
759 returns a pointer to the character marker in the source string (src).
762 static char *strncpyn(char *dest, const char *src, size_t n, char c)
768 clobber_region(dest, n+1);
770 p = strchr_m(src, c);
772 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
776 str_len = PTR_DIFF(p, src);
777 strncpy(dest, src, MIN(n, str_len));
778 dest[str_len] = '\0';
785 Routine to get hex characters and turn them into a 16 byte array.
786 the array can be variable length, and any non-hex-numeric
787 characters are skipped. "0xnn" or "0Xnn" is specially catered
790 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
794 size_t strhex_to_str(char *p, size_t len, const char *strhex)
797 size_t num_chars = 0;
798 unsigned char lonybble, hinybble;
799 const char *hexchars = "0123456789ABCDEF";
800 char *p1 = NULL, *p2 = NULL;
802 for (i = 0; i < len && strhex[i] != 0; i++) {
803 if (strnequal(hexchars, "0x", 2)) {
804 i++; /* skip two chars */
808 if (!(p1 = strchr_m(hexchars, toupper_ascii(strhex[i]))))
811 i++; /* next hex digit */
813 if (!(p2 = strchr_m(hexchars, toupper_ascii(strhex[i]))))
816 /* get the two nybbles */
817 hinybble = PTR_DIFF(p1, hexchars);
818 lonybble = PTR_DIFF(p2, hexchars);
820 p[num_chars] = (hinybble << 4) | lonybble;
829 DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex)
834 ret_blob = data_blob_talloc(mem_ctx, NULL, strlen(strhex)/2+1);
836 ret_blob = data_blob(NULL, strlen(strhex)/2+1);
838 ret_blob.length = strhex_to_str((char*)ret_blob.data,
846 * Routine to print a buffer as HEX digits, into an allocated string.
849 char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
854 hex_buffer = TALLOC_ARRAY(mem_ctx, char, (len*2)+1);
856 for (i = 0; i < len; i++)
857 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
863 Check if a string is part of a list.
866 BOOL in_list(const char *s, const char *list, BOOL casesensitive)
874 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
876 if (strcmp(tok,s) == 0)
879 if (StrCaseCmp(tok,s) == 0)
886 /* this is used to prevent lots of mallocs of size 1 */
887 static const char *null_string = "";
890 Set a string value, allocing the space for the string
893 static BOOL string_init(char **dest,const char *src)
903 *dest = CONST_DISCARD(char*, null_string);
905 (*dest) = SMB_STRDUP(src);
906 if ((*dest) == NULL) {
907 DEBUG(0,("Out of memory in string_init\n"));
918 void string_free(char **s)
922 if (*s == null_string)
928 Set a string value, deallocating any existing space, and allocing the space
932 BOOL string_set(char **dest,const char *src)
935 return(string_init(dest,src));
939 Substitute a string for a pattern in another string. Make sure there is
942 This routine looks for pattern in s and replaces it with
943 insert. It may do multiple replacements or just one.
945 Any of " ; ' $ or ` in the insert string are replaced with _
946 if len==0 then the string cannot be extended. This is different from the old
947 use of len==0 which was for no length checks to be done.
950 void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
951 BOOL remove_unsafe_characters, BOOL replace_once, BOOL allow_trailing_dollar)
956 if (!insert || !pattern || !*pattern || !s)
959 ls = (ssize_t)strlen(s);
960 lp = (ssize_t)strlen(pattern);
961 li = (ssize_t)strlen(insert);
964 len = ls + 1; /* len is number of *bytes* */
966 while (lp <= ls && (p = strstr_m(s,pattern))) {
967 if (ls + (li-lp) >= len) {
968 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
969 (int)(ls + (li-lp) - len),
974 memmove(p+li,p+lp,strlen(p+lp)+1);
983 /* allow a trailing $ (as in machine accounts) */
984 if (allow_trailing_dollar && (i == li - 1 )) {
991 if ( remove_unsafe_characters ) {
993 /* yes this break should be here since we want to
994 fall throw if not replacing unsafe chars */
1009 void string_sub_once(char *s, const char *pattern, const char *insert, size_t len)
1011 string_sub2( s, pattern, insert, len, True, True, False );
1014 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
1016 string_sub2( s, pattern, insert, len, True, False, False );
1019 void fstring_sub(char *s,const char *pattern,const char *insert)
1021 string_sub(s, pattern, insert, sizeof(fstring));
1024 void pstring_sub(char *s,const char *pattern,const char *insert)
1026 string_sub(s, pattern, insert, sizeof(pstring));
1030 Similar to string_sub, but it will accept only allocated strings
1031 and may realloc them so pay attention at what you pass on no
1032 pointers inside strings, no pstrings or const may be passed
1036 char *realloc_string_sub(char *string, const char *pattern,
1041 ssize_t ls,lp,li,ld, i;
1043 if (!insert || !pattern || !*pattern || !string || !*string)
1048 in = SMB_STRDUP(insert);
1050 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
1053 ls = (ssize_t)strlen(s);
1054 lp = (ssize_t)strlen(pattern);
1055 li = (ssize_t)strlen(insert);
1057 for (i=0;i<li;i++) {
1074 while ((p = strstr_m(s,pattern))) {
1076 int offset = PTR_DIFF(s,string);
1077 string = SMB_REALLOC(string, ls + ld + 1);
1079 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
1083 p = string + offset + (p - s);
1086 memmove(p+li,p+lp,strlen(p+lp)+1);
1096 /* Same as string_sub, but returns a talloc'ed string */
1098 char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
1099 const char *pattern, const char *insert)
1104 ssize_t ls,lp,li,ld, i;
1106 if (!insert || !pattern || !*pattern || !src || !*src)
1109 string = talloc_strdup(mem_ctx, src);
1110 if (string == NULL) {
1111 DEBUG(0, ("talloc_strdup failed\n"));
1117 in = SMB_STRDUP(insert);
1119 DEBUG(0, ("talloc_string_sub: out of memory!\n"));
1122 ls = (ssize_t)strlen(s);
1123 lp = (ssize_t)strlen(pattern);
1124 li = (ssize_t)strlen(insert);
1126 for (i=0;i<li;i++) {
1143 while ((p = strstr_m(s,pattern))) {
1145 int offset = PTR_DIFF(s,string);
1146 string = TALLOC_REALLOC(mem_ctx, string, ls + ld + 1);
1148 DEBUG(0, ("talloc_string_sub: out of "
1153 p = string + offset + (p - s);
1156 memmove(p+li,p+lp,strlen(p+lp)+1);
1167 Similar to string_sub() but allows for any character to be substituted.
1169 if len==0 then the string cannot be extended. This is different from the old
1170 use of len==0 which was for no length checks to be done.
1173 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1178 if (!insert || !pattern || !s)
1181 ls = (ssize_t)strlen(s);
1182 lp = (ssize_t)strlen(pattern);
1183 li = (ssize_t)strlen(insert);
1189 len = ls + 1; /* len is number of *bytes* */
1191 while (lp <= ls && (p = strstr_m(s,pattern))) {
1192 if (ls + (li-lp) >= len) {
1193 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
1194 (int)(ls + (li-lp) - len),
1195 pattern, (int)len));
1199 memmove(p+li,p+lp,strlen(p+lp)+1);
1201 memcpy(p, insert, li);
1208 Similar to all_string_sub but for unicode strings.
1209 Return a new allocated unicode string.
1210 similar to string_sub() but allows for any character to be substituted.
1214 static smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern,
1215 const smb_ucs2_t *insert)
1218 const smb_ucs2_t *sp;
1219 size_t lr, lp, li, lt;
1221 if (!insert || !pattern || !*pattern || !s)
1224 lt = (size_t)strlen_w(s);
1225 lp = (size_t)strlen_w(pattern);
1226 li = (size_t)strlen_w(insert);
1229 const smb_ucs2_t *st = s;
1231 while ((sp = strstr_w(st, pattern))) {
1237 r = rp = SMB_MALLOC_ARRAY(smb_ucs2_t, lt + 1);
1239 DEBUG(0, ("all_string_sub_w: out of memory!\n"));
1243 while ((sp = strstr_w(s, pattern))) {
1244 memcpy(rp, s, (sp - s));
1245 rp += ((sp - s) / sizeof(smb_ucs2_t));
1246 memcpy(rp, insert, (li * sizeof(smb_ucs2_t)));
1250 lr = ((rp - r) / sizeof(smb_ucs2_t));
1252 memcpy(rp, s, ((lt - lr) * sizeof(smb_ucs2_t)));
1260 smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern,
1265 if (!insert || !pattern || !s)
1267 push_ucs2(NULL, p, pattern, sizeof(wpstring) - 1, STR_TERMINATE);
1268 push_ucs2(NULL, i, insert, sizeof(wpstring) - 1, STR_TERMINATE);
1269 return all_string_sub_w(s, p, i);
1274 Splits out the front and back at a separator.
1277 static void split_at_last_component(char *path, char *front, char sep, char *back)
1279 char *p = strrchr_m(path, sep);
1285 pstrcpy(front, path);
1299 Write an octal as a string.
1302 const char *octal_string(int i)
1304 static char ret[64];
1307 slprintf(ret, sizeof(ret)-1, "0%o", i);
1313 Truncate a string at a specified length.
1316 char *string_truncate(char *s, unsigned int length)
1318 if (s && strlen(s) > length)
1324 Strchr and strrchr_m are very hard to do on general multi-byte strings.
1325 We convert via ucs2 for now.
1328 char *strchr_m(const char *src, char c)
1335 /* characters below 0x3F are guaranteed to not appear in
1336 non-initial position in multi-byte charsets */
1337 if ((c & 0xC0) == 0) {
1338 return strchr(src, c);
1341 /* this is quite a common operation, so we want it to be
1342 fast. We optimise for the ascii case, knowing that all our
1343 supported multi-byte character sets are ascii-compatible
1344 (ie. they match for the first 128 chars) */
1346 for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
1354 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
1355 /* With compose characters we must restart from the beginning. JRA. */
1359 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
1360 p = strchr_w(ws, UCS2_CHAR(c));
1364 pull_ucs2_pstring(s2, ws);
1365 return (char *)(s+strlen(s2));
1368 char *strrchr_m(const char *s, char c)
1370 /* characters below 0x3F are guaranteed to not appear in
1371 non-initial position in multi-byte charsets */
1372 if ((c & 0xC0) == 0) {
1373 return strrchr(s, c);
1376 /* this is quite a common operation, so we want it to be
1377 fast. We optimise for the ascii case, knowing that all our
1378 supported multi-byte character sets are ascii-compatible
1379 (ie. they match for the first 128 chars). Also, in Samba
1380 we only search for ascii characters in 'c' and that
1381 in all mb character sets with a compound character
1382 containing c, if 'c' is not a match at position
1383 p, then p[-1] > 0x7f. JRA. */
1386 size_t len = strlen(s);
1388 BOOL got_mb = False;
1395 /* Could be a match. Part of a multibyte ? */
1396 if ((cp > s) && (((unsigned char)cp[-1]) & 0x80)) {
1397 /* Yep - go slow :-( */
1401 /* No - we have a match ! */
1404 } while (cp-- != s);
1409 /* String contained a non-ascii char. Slow path. */
1415 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
1416 p = strrchr_w(ws, UCS2_CHAR(c));
1420 pull_ucs2_pstring(s2, ws);
1421 return (char *)(s+strlen(s2));
1425 /***********************************************************************
1426 Return the equivalent of doing strrchr 'n' times - always going
1428 ***********************************************************************/
1430 char *strnrchr_m(const char *s, char c, unsigned int n)
1436 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
1437 p = strnrchr_w(ws, UCS2_CHAR(c), n);
1441 pull_ucs2_pstring(s2, ws);
1442 return (char *)(s+strlen(s2));
1445 /***********************************************************************
1446 strstr_m - We convert via ucs2 for now.
1447 ***********************************************************************/
1449 char *strstr_m(const char *src, const char *findstr)
1452 smb_ucs2_t *src_w, *find_w;
1457 size_t findstr_len = 0;
1459 /* for correctness */
1464 /* Samba does single character findstr calls a *lot*. */
1465 if (findstr[1] == '\0')
1466 return strchr_m(src, *findstr);
1468 /* We optimise for the ascii case, knowing that all our
1469 supported multi-byte character sets are ascii-compatible
1470 (ie. they match for the first 128 chars) */
1472 for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
1473 if (*s == *findstr) {
1475 findstr_len = strlen(findstr);
1477 if (strncmp(s, findstr, findstr_len) == 0) {
1486 #if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */
1487 /* 'make check' fails unless we do this */
1489 /* With compose characters we must restart from the beginning. JRA. */
1493 if (push_ucs2_allocate(&src_w, src) == (size_t)-1) {
1494 DEBUG(0,("strstr_m: src malloc fail\n"));
1498 if (push_ucs2_allocate(&find_w, findstr) == (size_t)-1) {
1500 DEBUG(0,("strstr_m: find malloc fail\n"));
1504 p = strstr_w(src_w, find_w);
1513 if (pull_ucs2_allocate(&s2, src_w) == (size_t)-1) {
1516 DEBUG(0,("strstr_m: dest malloc fail\n"));
1519 retp = (char *)(s+strlen(s2));
1527 Convert a string to lower case.
1530 void strlower_m(char *s)
1535 /* this is quite a common operation, so we want it to be
1536 fast. We optimise for the ascii case, knowing that all our
1537 supported multi-byte character sets are ascii-compatible
1538 (ie. they match for the first 128 chars) */
1540 while (*s && !(((unsigned char)s[0]) & 0x80)) {
1541 *s = tolower_ascii((unsigned char)*s);
1548 /* I assume that lowercased string takes the same number of bytes
1549 * as source string even in UTF-8 encoding. (VIV) */
1550 len = strlen(s) + 1;
1553 unix_strlower(s,len,s,len);
1554 /* Catch mb conversion errors that may not terminate. */
1561 Convert a string to upper case.
1564 void strupper_m(char *s)
1569 /* this is quite a common operation, so we want it to be
1570 fast. We optimise for the ascii case, knowing that all our
1571 supported multi-byte character sets are ascii-compatible
1572 (ie. they match for the first 128 chars) */
1574 while (*s && !(((unsigned char)s[0]) & 0x80)) {
1575 *s = toupper_ascii((unsigned char)*s);
1582 /* I assume that lowercased string takes the same number of bytes
1583 * as source string even in multibyte encoding. (VIV) */
1584 len = strlen(s) + 1;
1587 unix_strupper(s,len,s,len);
1588 /* Catch mb conversion errors that may not terminate. */
1595 Return a RFC2254 binary string representation of a buffer.
1596 Used in LDAP filters.
1600 char *binary_string_rfc2254(char *buf, int len)
1604 const char *hex = "0123456789ABCDEF";
1605 s = SMB_MALLOC(len * 3 + 1);
1608 for (j=i=0;i<len;i++) {
1610 s[j+1] = hex[((unsigned char)buf[i]) >> 4];
1611 s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
1618 char *binary_string(char *buf, int len)
1622 const char *hex = "0123456789ABCDEF";
1623 s = SMB_MALLOC(len * 2 + 1);
1626 for (j=i=0;i<len;i++) {
1627 s[j] = hex[((unsigned char)buf[i]) >> 4];
1628 s[j+1] = hex[((unsigned char)buf[i]) & 0xF];
1635 Just a typesafety wrapper for snprintf into a pstring.
1638 int pstr_sprintf(pstring s, const char *fmt, ...)
1644 ret = vsnprintf(s, PSTRING_LEN, fmt, ap);
1651 Just a typesafety wrapper for snprintf into a fstring.
1654 int fstr_sprintf(fstring s, const char *fmt, ...)
1660 ret = vsnprintf(s, FSTRING_LEN, fmt, ap);
1666 #if !defined(HAVE_STRNDUP) || defined(BROKEN_STRNDUP)
1668 Some platforms don't have strndup.
1670 #if defined(PARANOID_MALLOC_CHECKER)
1674 char *strndup(const char *s, size_t n)
1679 ret = SMB_MALLOC(n+1);
1688 #if defined(PARANOID_MALLOC_CHECKER)
1689 #define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
1694 #if !defined(HAVE_STRNLEN) || defined(BROKEN_STRNLEN)
1696 Some platforms don't have strnlen
1699 size_t strnlen(const char *s, size_t n)
1702 for (i=0; i<n && s[i] != '\0'; i++)
1709 List of Strings manipulation functions
1712 #define S_LIST_ABS 16 /* List Allocation Block Size */
1714 static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
1716 char **list, **rlist;
1722 if (!string || !*string)
1725 s = talloc_strdup(mem_ctx, string);
1727 s = SMB_STRDUP(string);
1730 DEBUG(0,("str_list_make: Unable to allocate memory"));
1733 if (!sep) sep = LIST_SEP;
1739 while (next_token(&str, tok, sep, sizeof(tok))) {
1741 lsize += S_LIST_ABS;
1743 rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *, lsize +1);
1745 /* We need to keep the old list on error so we can free the elements
1746 if the realloc fails. */
1747 rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, char *, lsize +1);
1750 DEBUG(0,("str_list_make: Unable to allocate memory"));
1751 str_list_free(&list);
1761 memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1)));
1765 list[num] = talloc_strdup(mem_ctx, tok);
1767 list[num] = SMB_STRDUP(tok);
1771 DEBUG(0,("str_list_make: Unable to allocate memory"));
1772 str_list_free(&list);
1793 char **str_list_make_talloc(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
1795 return str_list_make_internal(mem_ctx, string, sep);
1798 char **str_list_make(const char *string, const char *sep)
1800 return str_list_make_internal(NULL, string, sep);
1803 BOOL str_list_copy(char ***dest, const char **src)
1805 char **list, **rlist;
1817 lsize += S_LIST_ABS;
1818 rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, char *, lsize +1);
1820 DEBUG(0,("str_list_copy: Unable to re-allocate memory"));
1821 str_list_free(&list);
1826 memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1)));
1829 list[num] = SMB_STRDUP(src[num]);
1831 DEBUG(0,("str_list_copy: Unable to allocate memory"));
1832 str_list_free(&list);
1844 * Return true if all the elements of the list match exactly.
1846 BOOL str_list_compare(char **list1, char **list2)
1850 if (!list1 || !list2)
1851 return (list1 == list2);
1853 for (num = 0; list1[num]; num++) {
1856 if (!strcsequal(list1[num], list2[num]))
1860 return False; /* if list2 has more elements than list1 fail */
1865 static void str_list_free_internal(TALLOC_CTX *mem_ctx, char ***list)
1869 if (!list || !*list)
1872 for(; *tlist; tlist++) {
1874 TALLOC_FREE(*tlist);
1880 TALLOC_FREE(*tlist);
1886 void str_list_free_talloc(TALLOC_CTX *mem_ctx, char ***list)
1888 str_list_free_internal(mem_ctx, list);
1891 void str_list_free(char ***list)
1893 str_list_free_internal(NULL, list);
1896 /******************************************************************************
1897 *****************************************************************************/
1899 int str_list_count( const char **list )
1906 /* count the number of list members */
1908 for ( i=0; *list; i++, list++ );
1913 /******************************************************************************
1914 version of standard_sub_basic() for string lists; uses alloc_sub_basic()
1916 *****************************************************************************/
1918 BOOL str_list_sub_basic( char **list, const char *smb_name,
1919 const char *domain_name )
1925 tmpstr = alloc_sub_basic(smb_name, domain_name, s);
1927 DEBUG(0,("str_list_sub_basic: alloc_sub_basic() return NULL!\n"));
1940 /******************************************************************************
1941 substritute a specific pattern in a string list
1942 *****************************************************************************/
1944 BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
1947 ssize_t ls, lp, li, ld, i, d;
1956 lp = (ssize_t)strlen(pattern);
1957 li = (ssize_t)strlen(insert);
1962 ls = (ssize_t)strlen(s);
1964 while ((p = strstr_m(s, pattern))) {
1968 t = (char *) SMB_MALLOC(ls +ld +1);
1970 DEBUG(0,("str_list_substitute: Unable to allocate memory"));
1973 memcpy(t, *list, d);
1974 memcpy(t +d +li, p +lp, ls -d -lp +1);
1981 for (i = 0; i < li; i++) {
1982 switch (insert[i]) {
1994 t[d +i] = insert[i];
2007 #define IPSTR_LIST_SEP ","
2008 #define IPSTR_LIST_CHAR ','
2011 * Add ip string representation to ipstr list. Used also
2012 * as part of @function ipstr_list_make
2014 * @param ipstr_list pointer to string containing ip list;
2015 * MUST BE already allocated and IS reallocated if necessary
2016 * @param ipstr_size pointer to current size of ipstr_list (might be changed
2017 * as a result of reallocation)
2018 * @param ip IP address which is to be added to list
2019 * @return pointer to string appended with new ip and possibly
2020 * reallocated to new length
2023 char* ipstr_list_add(char** ipstr_list, const struct ip_service *service)
2025 char* new_ipstr = NULL;
2027 /* arguments checking */
2028 if (!ipstr_list || !service) return NULL;
2030 /* attempt to convert ip to a string and append colon separator to it */
2032 asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list, IPSTR_LIST_SEP,
2033 inet_ntoa(service->ip), service->port);
2034 SAFE_FREE(*ipstr_list);
2036 asprintf(&new_ipstr, "%s:%d", inet_ntoa(service->ip), service->port);
2038 *ipstr_list = new_ipstr;
2044 * Allocate and initialise an ipstr list using ip adresses
2045 * passed as arguments.
2047 * @param ipstr_list pointer to string meant to be allocated and set
2048 * @param ip_list array of ip addresses to place in the list
2049 * @param ip_count number of addresses stored in ip_list
2050 * @return pointer to allocated ip string
2053 char* ipstr_list_make(char** ipstr_list, const struct ip_service* ip_list, int ip_count)
2057 /* arguments checking */
2058 if (!ip_list && !ipstr_list) return 0;
2062 /* process ip addresses given as arguments */
2063 for (i = 0; i < ip_count; i++)
2064 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
2066 return (*ipstr_list);
2071 * Parse given ip string list into array of ip addresses
2072 * (as ip_service structures)
2073 * e.g. 192.168.1.100:389,192.168.1.78, ...
2075 * @param ipstr ip string list to be parsed
2076 * @param ip_list pointer to array of ip addresses which is
2077 * allocated by this function and must be freed by caller
2078 * @return number of succesfully parsed addresses
2081 int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list)
2087 if (!ipstr_list || !ip_list)
2090 count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1;
2091 if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) {
2092 DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n", (unsigned long)count));
2097 next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN) && i<count;
2100 struct in_addr addr;
2102 char *p = strchr(token_str, ':');
2109 /* convert single token to ip address */
2110 if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE )
2113 (*ip_list)[i].ip = addr;
2114 (*ip_list)[i].port = port;
2122 * Safely free ip string list
2124 * @param ipstr_list ip string list to be freed
2127 void ipstr_list_free(char* ipstr_list)
2129 SAFE_FREE(ipstr_list);
2134 Unescape a URL encoded string, in place.
2137 void rfc1738_unescape(char *buf)
2141 while (p && *p && (p=strchr_m(p,'%'))) {
2145 if (c1 >= '0' && c1 <= '9')
2147 else if (c1 >= 'A' && c1 <= 'F')
2149 else if (c1 >= 'a' && c1 <= 'f')
2151 else {p++; continue;}
2153 if (c2 >= '0' && c2 <= '9')
2155 else if (c2 >= 'A' && c2 <= 'F')
2157 else if (c2 >= 'a' && c2 <= 'f')
2159 else {p++; continue;}
2163 memmove(p+1, p+3, strlen(p+3)+1);
2168 static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2171 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
2173 DATA_BLOB base64_decode_data_blob(const char *s)
2175 int bit_offset, byte_offset, idx, i, n;
2176 DATA_BLOB decoded = data_blob(s, strlen(s)+1);
2177 unsigned char *d = decoded.data;
2182 while (*s && (p=strchr_m(b64,*s))) {
2183 idx = (int)(p - b64);
2184 byte_offset = (i*6)/8;
2185 bit_offset = (i*6)%8;
2186 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
2187 if (bit_offset < 3) {
2188 d[byte_offset] |= (idx << (2-bit_offset));
2191 d[byte_offset] |= (idx >> (bit_offset-2));
2192 d[byte_offset+1] = 0;
2193 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
2199 if ((n > 0) && (*s == '=')) {
2209 * Decode a base64 string in-place - wrapper for the above
2211 void base64_decode_inplace(char *s)
2213 DATA_BLOB decoded = base64_decode_data_blob(s);
2215 if ( decoded.length != 0 ) {
2216 memcpy(s, decoded.data, decoded.length);
2218 /* null terminate */
2219 s[decoded.length] = '\0';
2224 data_blob_free(&decoded);
2228 * Encode a base64 string into a malloc()ed string caller to free.
2230 *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
2232 char * base64_encode_data_blob(DATA_BLOB data)
2236 size_t out_cnt, len, output_len;
2239 if (!data.length || !data.data)
2244 output_len = data.length * 2;
2245 result = SMB_MALLOC(output_len); /* get us plenty of space */
2247 while (len-- && out_cnt < (data.length * 2) - 5) {
2248 int c = (unsigned char) *(data.data++);
2251 if (char_count == 3) {
2252 result[out_cnt++] = b64[bits >> 18];
2253 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
2254 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
2255 result[out_cnt++] = b64[bits & 0x3f];
2262 if (char_count != 0) {
2263 bits <<= 16 - (8 * char_count);
2264 result[out_cnt++] = b64[bits >> 18];
2265 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
2266 if (char_count == 1) {
2267 result[out_cnt++] = '=';
2268 result[out_cnt++] = '=';
2270 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
2271 result[out_cnt++] = '=';
2274 result[out_cnt] = '\0'; /* terminate */
2278 /* read a SMB_BIG_UINT from a string */
2279 SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
2282 SMB_BIG_UINT val = -1;
2283 const char *p = nptr;
2290 while (*p && isspace(*p))
2293 #ifdef LARGE_SMB_OFF_T
2294 sscanf(p,"%llu",&val);
2295 #else /* LARGE_SMB_OFF_T */
2296 sscanf(p,"%lu",&val);
2297 #endif /* LARGE_SMB_OFF_T */
2299 while (*p && isdigit(*p))
2307 /* Convert a size specification to a count of bytes. We accept the following
2309 * bytes if there is no suffix
2314 * pP whatever the ISO name for petabytes is
2316 * Returns 0 if the string can't be converted.
2318 SMB_OFF_T conv_str_size(const char * str)
2323 if (str == NULL || *str == '\0') {
2327 #ifdef HAVE_STRTOULL
2328 if (sizeof(SMB_OFF_T) == 8) {
2329 lval = strtoull(str, &end, 10 /* base */);
2331 lval = strtoul(str, &end, 10 /* base */);
2334 lval = strtoul(str, &end, 10 /* base */);
2337 if (end == NULL || end == str) {
2342 SMB_OFF_T lval_orig = lval;
2344 if (strwicmp(end, "K") == 0) {
2345 lval *= (SMB_OFF_T)1024;
2346 } else if (strwicmp(end, "M") == 0) {
2347 lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024);
2348 } else if (strwicmp(end, "G") == 0) {
2349 lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2351 } else if (strwicmp(end, "T") == 0) {
2352 lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2353 (SMB_OFF_T)1024 * (SMB_OFF_T)1024);
2354 } else if (strwicmp(end, "P") == 0) {
2355 lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2356 (SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2362 /* Primitive attempt to detect wrapping on platforms with
2363 * 4-byte SMB_OFF_T. It's better to let the caller handle
2364 * a failure than some random number.
2366 if (lval_orig <= lval) {
2374 void string_append(char **left, const char *right)
2376 int new_len = strlen(right) + 1;
2378 if (*left == NULL) {
2379 *left = SMB_MALLOC(new_len);
2382 new_len += strlen(*left);
2383 *left = SMB_REALLOC(*left, new_len);
2386 if (*left == NULL) {
2390 safe_strcat(*left, right, new_len-1);
2393 BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
2394 const char *str, const char ***strings,
2397 char *dup_str = talloc_strdup(mem_ctx, str);
2399 *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings, const char *, (*num)+1);
2401 if ((*strings == NULL) || (dup_str == NULL))
2404 (*strings)[*num] = dup_str;
2409 /* Append an sprintf'ed string. Double buffer size on demand. Usable without
2410 * error checking in between. The indiation that something weird happened is
2413 void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len,
2414 size_t *bufsize, const char *fmt, ...)
2421 /* len<0 is an internal marker that something failed */
2425 if (*string == NULL) {
2429 if (mem_ctx != NULL)
2430 *string = TALLOC_ARRAY(mem_ctx, char, *bufsize);
2432 *string = SMB_MALLOC_ARRAY(char, *bufsize);
2434 if (*string == NULL)
2439 ret = vasprintf(&newstr, fmt, ap);
2447 while ((*len)+ret >= *bufsize) {
2450 if (*bufsize >= (1024*1024*256))
2455 if (mem_ctx != NULL) {
2456 *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char,
2459 *string = SMB_REALLOC_ARRAY(*string, char, *bufsize);
2462 if (*string == NULL) {
2467 StrnCpy((*string)+(*len), newstr, ret);
2474 if (mem_ctx == NULL) {
2481 Returns the substring from src between the first occurrence of
2482 the char "front" and the first occurence of the char "back".
2483 Mallocs the return string which must be freed. Not for use
2484 with wide character strings.
2486 char *sstring_sub(const char *src, char front, char back)
2488 char *temp1, *temp2, *temp3;
2491 temp1 = strchr(src, front);
2492 if (temp1 == NULL) return NULL;
2493 temp2 = strchr(src, back);
2494 if (temp2 == NULL) return NULL;
2495 len = temp2 - temp1;
2496 if (len <= 0) return NULL;
2497 temp3 = (char*)SMB_MALLOC(len);
2498 if (temp3 == NULL) {
2499 DEBUG(1,("Malloc failure in sstring_sub\n"));
2502 memcpy(temp3, temp1+1, len-1);
2503 temp3[len-1] = '\0';
2507 /********************************************************************
2508 Check a string for any occurrences of a specified list of invalid
2510 ********************************************************************/
2512 BOOL validate_net_name( const char *name, const char *invalid_chars, int max_len )
2516 for ( i=0; i<max_len && name[i]; i++ ) {
2517 /* fail if strchr_m() finds one of the invalid characters */
2518 if ( name[i] && strchr_m( invalid_chars, name[i] ) ) {
2528 return the number of bytes occupied by a buffer in ASCII format
2529 the result includes the null termination
2530 limited by 'n' bytes
2532 size_t ascii_len_n(const char *src, size_t n)
2536 len = strnlen(src, n);
2545 return the number of bytes occupied by a buffer in CH_UTF16 format
2546 the result includes the null termination
2548 size_t utf16_len(const void *buf)
2552 for (len = 0; SVAL(buf,len); len += 2) ;
2558 return the number of bytes occupied by a buffer in CH_UTF16 format
2559 the result includes the null termination
2560 limited by 'n' bytes
2562 size_t utf16_len_n(const void *src, size_t n)
2566 for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;