From 8c3dcf128bb1a1c8d0b7a62a7c42a17457730024 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 5 Nov 1998 16:48:35 +0000 Subject: split string and unicode string routines into these files. these are *not* going to be added into the Makefile.in yet so they still also exist in util.c. (This used to be commit 3f5feda6749dace6bc51fb0e02b16d2b72a930b8) --- source3/lib/util_str.c | 976 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 976 insertions(+) create mode 100644 source3/lib/util_str.c (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c new file mode 100644 index 0000000000..7eb1494382 --- /dev/null +++ b/source3/lib/util_str.c @@ -0,0 +1,976 @@ +/* + Unix SMB/Netbios implementation. + Version 1.9. + Samba utility functions + Copyright (C) Andrew Tridgell 1992-1998 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +extern int DEBUGLEVEL; + +static char *last_ptr=NULL; + +void set_first_token(char *ptr) +{ + last_ptr = ptr; +} + +/**************************************************************************** + Get the next token from a string, return False if none found + handles double-quotes. +Based on a routine by GJC@VILLAGE.COM. +Extensively modified by Andrew.Tridgell@anu.edu.au +****************************************************************************/ +BOOL next_token(char **ptr,char *buff,char *sep, int bufsize) +{ + char *s; + BOOL quoted; + int len=1; + + if (!ptr) ptr = &last_ptr; + if (!ptr) return(False); + + s = *ptr; + + /* default to simple separators */ + if (!sep) sep = " \t\n\r"; + + /* find the first non sep char */ + while(*s && strchr(sep,*s)) s++; + + /* nothing left? */ + if (! *s) return(False); + + /* copy over the token */ + for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) + { + if (*s == '\"') { + quoted = !quoted; + } else { + len++; + *buff++ = *s; + } + } + + *ptr = (*s) ? s+1 : s; + *buff = 0; + last_ptr = *ptr; + + return(True); +} + +/**************************************************************************** +Convert list of tokens to array; dependent on above routine. +Uses last_ptr from above - bit of a hack. +****************************************************************************/ +char **toktocliplist(int *ctok, char *sep) +{ + char *s=last_ptr; + int ictok=0; + char **ret, **iret; + + if (!sep) sep = " \t\n\r"; + + while(*s && strchr(sep,*s)) s++; + + /* nothing left? */ + if (!*s) return(NULL); + + do { + ictok++; + while(*s && (!strchr(sep,*s))) s++; + while(*s && strchr(sep,*s)) *s++=0; + } while(*s); + + *ctok=ictok; + s=last_ptr; + + if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL; + + while(ictok--) { + *iret++=s; + while(*s++); + while(!*s) s++; + } + + return ret; +} + + +/******************************************************************* + case insensitive string compararison +********************************************************************/ +int StrCaseCmp(char *s, char *t) +{ + /* compare until we run out of string, either t or s, or find a difference */ + /* We *must* use toupper rather than tolower here due to the + asynchronous upper to lower mapping. + */ +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + int diff; + for (;;) + { + if (!*s || !*t) + return toupper (*s) - toupper (*t); + else if (is_sj_alph (*s) && is_sj_alph (*t)) + { + diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1)); + if (diff) + return diff; + s += 2; + t += 2; + } + else if (is_shift_jis (*s) && is_shift_jis (*t)) + { + diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t); + if (diff) + return diff; + diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1)); + if (diff) + return diff; + s += 2; + t += 2; + } + else if (is_shift_jis (*s)) + return 1; + else if (is_shift_jis (*t)) + return -1; + else + { + diff = toupper (*s) - toupper (*t); + if (diff) + return diff; + s++; + t++; + } + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + while (*s && *t && toupper(*s) == toupper(*t)) + { + s++; + t++; + } + + return(toupper(*s) - toupper(*t)); + } +} + +/******************************************************************* + case insensitive string compararison, length limited +********************************************************************/ +int StrnCaseCmp(char *s, char *t, int n) +{ + /* compare until we run out of string, either t or s, or chars */ + /* We *must* use toupper rather than tolower here due to the + asynchronous upper to lower mapping. + */ +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + int diff; + for (;n > 0;) + { + if (!*s || !*t) + return toupper (*s) - toupper (*t); + else if (is_sj_alph (*s) && is_sj_alph (*t)) + { + diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1)); + if (diff) + return diff; + s += 2; + t += 2; + n -= 2; + } + else if (is_shift_jis (*s) && is_shift_jis (*t)) + { + diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t); + if (diff) + return diff; + diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1)); + if (diff) + return diff; + s += 2; + t += 2; + n -= 2; + } + else if (is_shift_jis (*s)) + return 1; + else if (is_shift_jis (*t)) + return -1; + else + { + diff = toupper (*s) - toupper (*t); + if (diff) + return diff; + s++; + t++; + n--; + } + } + return 0; + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + while (n && *s && *t && toupper(*s) == toupper(*t)) + { + s++; + t++; + n--; + } + + /* not run out of chars - strings are different lengths */ + if (n) + return(toupper(*s) - toupper(*t)); + + /* identical up to where we run out of chars, + and strings are same length */ + return(0); + } +} + +/******************************************************************* + compare 2 strings +********************************************************************/ +BOOL strequal(char *s1, char *s2) +{ + if (s1 == s2) return(True); + if (!s1 || !s2) return(False); + + return(StrCaseCmp(s1,s2)==0); +} + +/******************************************************************* + compare 2 strings up to and including the nth char. + ******************************************************************/ +BOOL strnequal(char *s1,char *s2,int n) +{ + if (s1 == s2) return(True); + if (!s1 || !s2 || !n) return(False); + + return(StrnCaseCmp(s1,s2,n)==0); +} + +/******************************************************************* + compare 2 strings (case sensitive) +********************************************************************/ +BOOL strcsequal(char *s1,char *s2) +{ + if (s1 == s2) return(True); + if (!s1 || !s2) return(False); + + return(strcmp(s1,s2)==0); +} + + +/******************************************************************* + convert a string to lower case +********************************************************************/ +void strlower(char *s) +{ + while (*s) + { +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + if (is_shift_jis (*s)) + { + if (is_sj_upper (s[0], s[1])) + s[1] = sj_tolower2 (s[1]); + s += 2; + } + else if (is_kana (*s)) + { + s++; + } + else + { + if (isupper(*s)) + *s = tolower(*s); + s++; + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + int skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else + { + if (isupper(*s)) + *s = tolower(*s); + s++; + } + } + } +} + +/******************************************************************* + convert a string to upper case +********************************************************************/ +void strupper(char *s) +{ + while (*s) + { +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + if (is_shift_jis (*s)) + { + if (is_sj_lower (s[0], s[1])) + s[1] = sj_toupper2 (s[1]); + s += 2; + } + else if (is_kana (*s)) + { + s++; + } + else + { + if (islower(*s)) + *s = toupper(*s); + s++; + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + int skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else + { + if (islower(*s)) + *s = toupper(*s); + s++; + } + } + } +} + +/******************************************************************* + convert a string to "normal" form +********************************************************************/ +void strnorm(char *s) +{ + extern int case_default; + if (case_default == CASE_UPPER) + strupper(s); + else + strlower(s); +} + +/******************************************************************* +check if a string is in "normal" case +********************************************************************/ +BOOL strisnormal(char *s) +{ + extern int case_default; + if (case_default == CASE_UPPER) + return(!strhaslower(s)); + + return(!strhasupper(s)); +} + + +/**************************************************************************** + string replace +****************************************************************************/ +void string_replace(char *s,char oldc,char newc) +{ + int skip; + while (*s) + { + skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else + { + if (oldc == *s) + *s = newc; + s++; + } + } +} + + +/******************************************************************* +skip past some strings in a buffer +********************************************************************/ +char *skip_string(char *buf,int n) +{ + while (n--) + buf += strlen(buf) + 1; + return(buf); +} + +/******************************************************************* + Count the number of characters in a string. Normally this will + be the same as the number of bytes in a string for single byte strings, + but will be different for multibyte. + 16.oct.98, jdblair@cobaltnet.com. +********************************************************************/ + +size_t str_charnum(char *s) +{ + size_t len = 0; + + while (*s != '\0') { + int skip = skip_multibyte_char(*s); + s += (skip ? skip : 1); + len++; + } + return len; +} + +/******************************************************************* +trim the specified elements off the front and back of a string +********************************************************************/ + +BOOL trim_string(char *s,char *front,char *back) +{ + BOOL ret = False; + size_t front_len = (front && *front) ? strlen(front) : 0; + size_t back_len = (back && *back) ? strlen(back) : 0; + size_t s_len; + + while (front_len && strncmp(s, front, front_len) == 0) + { + char *p = s; + ret = True; + while (1) + { + if (!(*p = p[front_len])) + break; + p++; + } + } + + /* + * We split out the multibyte code page + * case here for speed purposes. Under a + * multibyte code page we need to walk the + * string forwards only and multiple times. + * Thanks to John Blair for finding this + * one. JRA. + */ + + if(back_len) + { + if(!is_multibyte_codepage()) + { + s_len = strlen(s); + while ((s_len >= back_len) && + (strncmp(s + s_len - back_len, back, back_len)==0)) + { + ret = True; + s[s_len - back_len] = '\0'; + s_len = strlen(s); + } + } + else + { + + /* + * Multibyte code page case. + * Keep going through the string, trying + * to match the 'back' string with the end + * of the string. If we get a match, truncate + * 'back' off the end of the string and + * go through the string again from the + * start. Keep doing this until we have + * gone through the string with no match + * at the string end. + */ + + size_t mb_back_len = str_charnum(back); + size_t mb_s_len = str_charnum(s); + + while(mb_s_len >= mb_back_len) + { + size_t charcount = 0; + char *mbp = s; + + while(charcount < (mb_s_len - mb_back_len)) + { + size_t skip = skip_multibyte_char(*mbp); + mbp += (skip ? skip : 1); + charcount++; + } + + /* + * mbp now points at mb_back_len multibyte + * characters from the end of s. + */ + + if(strcmp(mbp, back) == 0) + { + ret = True; + *mbp = '\0'; + mb_s_len = str_charnum(s); + mbp = s; + } + else + break; + } /* end while mb_s_len... */ + } /* end else .. */ + } /* end if back_len .. */ + + return(ret); +} + + +/**************************************************************************** +does a string have any uppercase chars in it? +****************************************************************************/ +BOOL strhasupper(char *s) +{ + while (*s) + { +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + if (is_shift_jis (*s)) + s += 2; + else if (is_kana (*s)) + s++; + else + { + if (isupper(*s)) + return(True); + s++; + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + int skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else { + if (isupper(*s)) + return(True); + s++; + } + } + } + return(False); +} + +/**************************************************************************** +does a string have any lowercase chars in it? +****************************************************************************/ +BOOL strhaslower(char *s) +{ + while (*s) + { +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + if (is_shift_jis (*s)) + { + if (is_sj_upper (s[0], s[1])) + return(True); + if (is_sj_lower (s[0], s[1])) + return (True); + s += 2; + } + else if (is_kana (*s)) + { + s++; + } + else + { + if (islower(*s)) + return(True); + s++; + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + int skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else { + if (islower(*s)) + return(True); + s++; + } + } + } + return(False); +} + +/**************************************************************************** +find the number of chars in a string +****************************************************************************/ +int count_chars(char *s,char c) +{ + int count=0; + +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + while (*s) + { + if (is_shift_jis (*s)) + s += 2; + else + { + if (*s == c) + count++; + s++; + } + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + while (*s) + { + int skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else { + if (*s == c) + count++; + s++; + } + } + } + return(count); +} + + + +/******************************************************************* +safe string copy into a known length string. maxlength does not +include the terminating zero. +********************************************************************/ +char *safe_strcpy(char *dest,const char *src, int maxlength) +{ + int len; + + if (!dest) { + DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); + return NULL; + } + + if (!src) { + *dest = 0; + return dest; + } + + len = strlen(src); + + if (len > maxlength) { + DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n", + len-maxlength, src)); + len = maxlength; + } + + memcpy(dest, src, len); + dest[len] = 0; + return dest; +} + +/******************************************************************* +safe string cat into a string. maxlength does not +include the terminating zero. +********************************************************************/ +char *safe_strcat(char *dest, char *src, int maxlength) +{ + int src_len, dest_len; + + if (!dest) { + DEBUG(0,("ERROR: NULL dest in safe_strcat\n")); + return NULL; + } + + if (!src) { + return dest; + } + + src_len = strlen(src); + dest_len = strlen(dest); + + if (src_len + dest_len > maxlength) { + DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", + src_len + dest_len - maxlength, src)); + src_len = maxlength - dest_len; + } + + memcpy(&dest[dest_len], src, src_len); + dest[dest_len + src_len] = 0; + return dest; +} + +/**************************************************************************** +this is a safer strcpy(), meant to prevent core dumps when nasty things happen +****************************************************************************/ +char *StrCpy(char *dest,char *src) +{ + char *d = dest; + + /* I don't want to get lazy with these ... */ + SMB_ASSERT(dest && src); + + if (!dest) return(NULL); + if (!src) { + *dest = 0; + return(dest); + } + while ((*d++ = *src++)) ; + return(dest); +} + +/**************************************************************************** +like strncpy but always null terminates. Make sure there is room! +****************************************************************************/ +char *StrnCpy(char *dest,char *src,int n) +{ + char *d = dest; + if (!dest) return(NULL); + if (!src) { + *dest = 0; + return(dest); + } + while (n-- && (*d++ = *src++)) ; + *d = 0; + return(dest); +} + + +/**************************************************************************** +like strncpy but copies up to the character marker. always null terminates. +returns a pointer to the character marker in the source string (src). +****************************************************************************/ +char *strncpyn(char *dest, char *src,int n, char c) +{ + char *p; + int str_len; + + p = strchr(src, c); + if (p == NULL) + { + DEBUG(5, ("strncpyn: separator character (%c) not found\n", c)); + return NULL; + } + + str_len = PTR_DIFF(p, src); + strncpy(dest, src, MIN(n, str_len)); + dest[str_len] = '\0'; + + return p; +} + + +/**************************************************************************** +check if a string is part of a list +****************************************************************************/ +BOOL in_list(char *s,char *list,BOOL casesensitive) +{ + pstring tok; + char *p=list; + + if (!list) return(False); + + while (next_token(&p,tok,LIST_SEP,sizeof(tok))) { + if (casesensitive) { + if (strcmp(tok,s) == 0) + return(True); + } else { + if (StrCaseCmp(tok,s) == 0) + return(True); + } + } + return(False); +} + +/* this is used to prevent lots of mallocs of size 1 */ +static char *null_string = NULL; + +/**************************************************************************** +set a string value, allocing the space for the string +****************************************************************************/ +BOOL string_init(char **dest,char *src) +{ + int l; + if (!src) + src = ""; + + l = strlen(src); + + if (l == 0) + { + if (!null_string) { + if((null_string = (char *)malloc(1)) == NULL) { + DEBUG(0,("string_init: malloc fail for null_string.\n")); + return False; + } + *null_string = 0; + } + *dest = null_string; + } + else + { + (*dest) = (char *)malloc(l+1); + if ((*dest) == NULL) { + DEBUG(0,("Out of memory in string_init\n")); + return False; + } + + pstrcpy(*dest,src); + } + return(True); +} + +/**************************************************************************** +free a string value +****************************************************************************/ +void string_free(char **s) +{ + if (!s || !(*s)) return; + if (*s == null_string) + *s = NULL; + if (*s) free(*s); + *s = NULL; +} + +/**************************************************************************** +set a string value, allocing the space for the string, and deallocating any +existing space +****************************************************************************/ +BOOL string_set(char **dest,char *src) +{ + string_free(dest); + + return(string_init(dest,src)); +} + +/**************************************************************************** +substitute a string for a pattern in another string. Make sure there is +enough room! + +This routine looks for pattern in s and replaces it with +insert. It may do multiple replacements. + +return True if a substitution was done. +****************************************************************************/ +BOOL string_sub(char *s,char *pattern,char *insert) +{ + BOOL ret = False; + char *p; + int ls,lp,li; + + if (!insert || !pattern || !s) return(False); + + ls = strlen(s); + lp = strlen(pattern); + li = strlen(insert); + + if (!*pattern) return(False); + + while (lp <= ls && (p = strstr(s,pattern))) + { + ret = True; + memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp)); + memcpy(p,insert,li); + s = p + li; + ls = strlen(s); + } + return(ret); +} + -- cgit From 1e1c2ec93c204e6fd3ebba6dfb11e4fbc136e10c Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Tue, 10 Nov 1998 19:05:00 +0000 Subject: rpcclient registry commands. (This used to be commit 36fcb4a6e643a05d06a2a273d74318fee7f2c647) --- source3/lib/util_str.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7eb1494382..15eefb0001 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -857,6 +857,56 @@ char *strncpyn(char *dest, char *src,int n, char c) } +/************************************************************* + Routine to get hex characters and turn them into a 16 byte array. + the array can be variable length, and any non-hex-numeric + characters are skipped. "0xnn" or "0Xnn" is specially catered + for. + + valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n" + +**************************************************************/ +int strhex_to_str(char *p, int len, const char *strhex) +{ + int i; + int num_chars = 0; + unsigned char lonybble, hinybble; + char *hexchars = "0123456789ABCDEF"; + char *p1 = NULL, *p2 = NULL; + + for (i = 0; i < len && strhex[i] != 0; i++) + { + if (strnequal(hexchars, "0x", 2)) + { + i++; /* skip two chars */ + continue; + } + + while (!(p1 = strchr(hexchars, toupper(strhex[i])))) + { + continue; + } + + i++; /* next hex digit */ + + while (!(p2 = strchr(hexchars, toupper(strhex[i])))) + { + continue; + } + + /* get the two nybbles */ + hinybble = PTR_DIFF(p1, hexchars); + lonybble = PTR_DIFF(p2, hexchars); + + p[num_chars] = (hinybble << 4) | lonybble; + num_chars++; + + p1 = NULL; + p2 = NULL; + } + return num_chars; +} + /**************************************************************************** check if a string is part of a list ****************************************************************************/ -- cgit From 9f14f281ff7efa1af0242a1dd1f5220d5cfdbf49 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Wed, 11 Nov 1998 14:23:55 +0000 Subject: changed syntax of registry commands so keys can start with HKLM or HKU. sorted lookupsids command (This used to be commit 13a0ee851fe0ce9acddfe57f9aba19fc78085c39) --- source3/lib/util_str.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 15eefb0001..996273bf3a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1024,3 +1024,34 @@ BOOL string_sub(char *s,char *pattern,char *insert) return(ret); } +/**************************************************************************** + splits out the front and back at a separator. +****************************************************************************/ +void split_at_last_component(char *path, char *front, char sep, char *back) +{ + char *p = strrchr(path, sep); + + if (p != NULL) + { + *p = 0; + } + if (front != NULL) + { + pstrcpy(front, path); + } + if (p != NULL) + { + if (back != NULL) + { + pstrcpy(back, p+1); + } + *p = '\\'; + } + else + { + if (back != NULL) + { + back[0] = 0; + } + } +} -- cgit From d85dcf86d59c14cb624bbb69b658fc6aba842593 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 12 Nov 1998 06:12:19 +0000 Subject: largely rewrote smbpasswd so that the code is understandable. This should allow us to call a function in swat rather than piping to smbpasswd. while doing this I also fixed quite a few "const char *" versus "char *" issues that cropped up while using const to track down bugs in the code. This led to changes in several generic functions. The smbpasswd changes should be correct but they have not been extensively tested. At least if I have introduced bugs then we should be able to fix them more easily than before. (This used to be commit 713864dd0322ae2ae2d83e333d85be35a7eed4ec) --- source3/lib/util_str.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 996273bf3a..02fa892d7b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -115,7 +115,7 @@ char **toktocliplist(int *ctok, char *sep) /******************************************************************* case insensitive string compararison ********************************************************************/ -int StrCaseCmp(char *s, char *t) +int StrCaseCmp(const char *s, const char *t) { /* compare until we run out of string, either t or s, or find a difference */ /* We *must* use toupper rather than tolower here due to the @@ -272,7 +272,7 @@ int StrnCaseCmp(char *s, char *t, int n) /******************************************************************* compare 2 strings ********************************************************************/ -BOOL strequal(char *s1, char *s2) +BOOL strequal(const char *s1, const char *s2) { if (s1 == s2) return(True); if (!s1 || !s2) return(False); @@ -819,7 +819,7 @@ char *StrCpy(char *dest,char *src) /**************************************************************************** like strncpy but always null terminates. Make sure there is room! ****************************************************************************/ -char *StrnCpy(char *dest,char *src,int n) +char *StrnCpy(char *dest,const char *src,int n) { char *d = dest; if (!dest) return(NULL); @@ -837,7 +837,7 @@ char *StrnCpy(char *dest,char *src,int n) like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). ****************************************************************************/ -char *strncpyn(char *dest, char *src,int n, char c) +char *strncpyn(char *dest, const char *src,int n, char c) { char *p; int str_len; -- cgit From 768761820e8d7481c586c4e0ab4ac7cb36d18c4b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 17 Nov 1998 20:50:07 +0000 Subject: Added the same open()/fopen()/creat()/mmap() -> sys_XXX calls. Tidied up some of the mess (no other word for it). Still doesn't compile cleanly. There are calls with incorrect parameters that don't seem to be doing the right thing. This code still needs surgery :-(. Jeremy. (This used to be commit 18ff93a9abbf68ee8c59c0af3e57c63e4a015dac) --- source3/lib/util_str.c | 70 +++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 02fa892d7b..c943a854cf 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -36,11 +36,11 @@ void set_first_token(char *ptr) Based on a routine by GJC@VILLAGE.COM. Extensively modified by Andrew.Tridgell@anu.edu.au ****************************************************************************/ -BOOL next_token(char **ptr,char *buff,char *sep, int bufsize) +BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize) { char *s; BOOL quoted; - int len=1; + size_t len=1; if (!ptr) ptr = &last_ptr; if (!ptr) return(False); @@ -188,7 +188,7 @@ int StrCaseCmp(const char *s, const char *t) /******************************************************************* case insensitive string compararison, length limited ********************************************************************/ -int StrnCaseCmp(char *s, char *t, int n) +int StrnCaseCmp(const char *s, const char *t, size_t n) { /* compare until we run out of string, either t or s, or chars */ /* We *must* use toupper rather than tolower here due to the @@ -283,7 +283,7 @@ BOOL strequal(const char *s1, const char *s2) /******************************************************************* compare 2 strings up to and including the nth char. ******************************************************************/ -BOOL strnequal(char *s1,char *s2,int n) +BOOL strnequal(const char *s1,const char *s2,size_t n) { if (s1 == s2) return(True); if (!s1 || !s2 || !n) return(False); @@ -294,7 +294,7 @@ BOOL strnequal(char *s1,char *s2,int n) /******************************************************************* compare 2 strings (case sensitive) ********************************************************************/ -BOOL strcsequal(char *s1,char *s2) +BOOL strcsequal(const char *s1,const char *s2) { if (s1 == s2) return(True); if (!s1 || !s2) return(False); @@ -343,7 +343,7 @@ void strlower(char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - int skip = skip_multibyte_char( *s ); + size_t skip = skip_multibyte_char( *s ); if( skip != 0 ) s += skip; else @@ -396,7 +396,7 @@ void strupper(char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - int skip = skip_multibyte_char( *s ); + size_t skip = skip_multibyte_char( *s ); if( skip != 0 ) s += skip; else @@ -439,7 +439,7 @@ BOOL strisnormal(char *s) ****************************************************************************/ void string_replace(char *s,char oldc,char newc) { - int skip; + size_t skip; while (*s) { skip = skip_multibyte_char( *s ); @@ -458,11 +458,11 @@ void string_replace(char *s,char oldc,char newc) /******************************************************************* skip past some strings in a buffer ********************************************************************/ -char *skip_string(char *buf,int n) +char *skip_string(const char *buf,size_t n) { while (n--) buf += strlen(buf) + 1; - return(buf); + return((char *)buf); } /******************************************************************* @@ -472,7 +472,7 @@ char *skip_string(char *buf,int n) 16.oct.98, jdblair@cobaltnet.com. ********************************************************************/ -size_t str_charnum(char *s) +size_t str_charnum(const char *s) { size_t len = 0; @@ -488,7 +488,7 @@ size_t str_charnum(char *s) trim the specified elements off the front and back of a string ********************************************************************/ -BOOL trim_string(char *s,char *front,char *back) +BOOL trim_string(char *s,const char *front,const char *back) { BOOL ret = False; size_t front_len = (front && *front) ? strlen(front) : 0; @@ -584,7 +584,7 @@ BOOL trim_string(char *s,char *front,char *back) /**************************************************************************** does a string have any uppercase chars in it? ****************************************************************************/ -BOOL strhasupper(char *s) +BOOL strhasupper(const char *s) { while (*s) { @@ -615,7 +615,7 @@ BOOL strhasupper(char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - int skip = skip_multibyte_char( *s ); + size_t skip = skip_multibyte_char( *s ); if( skip != 0 ) s += skip; else { @@ -631,7 +631,7 @@ BOOL strhasupper(char *s) /**************************************************************************** does a string have any lowercase chars in it? ****************************************************************************/ -BOOL strhaslower(char *s) +BOOL strhaslower(const char *s) { while (*s) { @@ -670,7 +670,7 @@ BOOL strhaslower(char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - int skip = skip_multibyte_char( *s ); + size_t skip = skip_multibyte_char( *s ); if( skip != 0 ) s += skip; else { @@ -686,9 +686,9 @@ BOOL strhaslower(char *s) /**************************************************************************** find the number of chars in a string ****************************************************************************/ -int count_chars(char *s,char c) +size_t count_chars(const char *s,char c) { - int count=0; + size_t count=0; #if !defined(KANJI_WIN95_COMPATIBILITY) /* @@ -720,7 +720,7 @@ int count_chars(char *s,char c) { while (*s) { - int skip = skip_multibyte_char( *s ); + size_t skip = skip_multibyte_char( *s ); if( skip != 0 ) s += skip; else { @@ -739,9 +739,9 @@ int count_chars(char *s,char c) safe string copy into a known length string. maxlength does not include the terminating zero. ********************************************************************/ -char *safe_strcpy(char *dest,const char *src, int maxlength) +char *safe_strcpy(char *dest,const char *src, size_t maxlength) { - int len; + size_t len; if (!dest) { DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); @@ -770,9 +770,9 @@ char *safe_strcpy(char *dest,const char *src, int maxlength) safe string cat into a string. maxlength does not include the terminating zero. ********************************************************************/ -char *safe_strcat(char *dest, char *src, int maxlength) +char *safe_strcat(char *dest, const char *src, size_t maxlength) { - int src_len, dest_len; + size_t src_len, dest_len; if (!dest) { DEBUG(0,("ERROR: NULL dest in safe_strcat\n")); @@ -800,7 +800,7 @@ char *safe_strcat(char *dest, char *src, int maxlength) /**************************************************************************** this is a safer strcpy(), meant to prevent core dumps when nasty things happen ****************************************************************************/ -char *StrCpy(char *dest,char *src) +char *StrCpy(char *dest,const char *src) { char *d = dest; @@ -819,7 +819,7 @@ char *StrCpy(char *dest,char *src) /**************************************************************************** like strncpy but always null terminates. Make sure there is room! ****************************************************************************/ -char *StrnCpy(char *dest,const char *src,int n) +char *StrnCpy(char *dest,const char *src,size_t n) { char *d = dest; if (!dest) return(NULL); @@ -837,10 +837,10 @@ char *StrnCpy(char *dest,const char *src,int n) like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). ****************************************************************************/ -char *strncpyn(char *dest, const char *src,int n, char c) +char *strncpyn(char *dest, const char *src,size_t n, char c) { char *p; - int str_len; + size_t str_len; p = strchr(src, c); if (p == NULL) @@ -866,10 +866,10 @@ char *strncpyn(char *dest, const char *src,int n, char c) valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n" **************************************************************/ -int strhex_to_str(char *p, int len, const char *strhex) +size_t strhex_to_str(char *p, size_t len, const char *strhex) { - int i; - int num_chars = 0; + size_t i; + size_t num_chars = 0; unsigned char lonybble, hinybble; char *hexchars = "0123456789ABCDEF"; char *p1 = NULL, *p2 = NULL; @@ -935,9 +935,9 @@ static char *null_string = NULL; /**************************************************************************** set a string value, allocing the space for the string ****************************************************************************/ -BOOL string_init(char **dest,char *src) +BOOL string_init(char **dest,const char *src) { - int l; + size_t l; if (!src) src = ""; @@ -983,7 +983,7 @@ void string_free(char **s) set a string value, allocing the space for the string, and deallocating any existing space ****************************************************************************/ -BOOL string_set(char **dest,char *src) +BOOL string_set(char **dest,const char *src) { string_free(dest); @@ -999,11 +999,11 @@ insert. It may do multiple replacements. return True if a substitution was done. ****************************************************************************/ -BOOL string_sub(char *s,char *pattern,char *insert) +BOOL string_sub(char *s,const char *pattern,const char *insert) { BOOL ret = False; char *p; - int ls,lp,li; + size_t ls,lp,li; if (!insert || !pattern || !s) return(False); -- cgit From 8757254f39ab75c93e9917a6bdf99475c6a024d7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 23 Nov 1998 03:36:10 +0000 Subject: changed string_sub() to replace " ; and ` in the inserted string with _ use all_string_sub() if you don't want this. (This used to be commit a3357ab49335106674fe7a7481cd0f146d74fbe5) --- source3/lib/util_str.c | 71 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 19 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c943a854cf..dad0e85477 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -990,6 +990,7 @@ BOOL string_set(char **dest,const char *src) return(string_init(dest,src)); } + /**************************************************************************** substitute a string for a pattern in another string. Make sure there is enough room! @@ -997,31 +998,63 @@ enough room! This routine looks for pattern in s and replaces it with insert. It may do multiple replacements. -return True if a substitution was done. +any of " ; or ` in the insert string are replaced with _ ****************************************************************************/ -BOOL string_sub(char *s,const char *pattern,const char *insert) +void string_sub(char *s,const char *pattern,const char *insert) { - BOOL ret = False; - char *p; - size_t ls,lp,li; + char *p; + size_t ls,lp,li, i; + + if (!insert || !pattern || !s) return; + + ls = strlen(s); + lp = strlen(pattern); + li = strlen(insert); + + if (!*pattern) return; + + while (lp <= ls && (p = strstr(s,pattern))) { + memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp)); + for (i=0;i Date: Mon, 23 Nov 1998 03:46:51 +0000 Subject: replace ' with _ as well (This used to be commit e93491953a2555401a372de74ac2aee0cc44cb88) --- source3/lib/util_str.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index dad0e85477..5935533cee 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -998,7 +998,7 @@ enough room! This routine looks for pattern in s and replaces it with insert. It may do multiple replacements. -any of " ; or ` in the insert string are replaced with _ +any of " ; ' or ` in the insert string are replaced with _ ****************************************************************************/ void string_sub(char *s,const char *pattern,const char *insert) { @@ -1019,6 +1019,7 @@ void string_sub(char *s,const char *pattern,const char *insert) switch (insert[i]) { case '`': case '"': + case '\'': case ';': p[i] = '_'; break; -- cgit From 5694edcc3140424872d23bf80a6a865af3d04bf3 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Mon, 23 Nov 1998 22:21:19 +0000 Subject: unused variable removed (This used to be commit 16ac5c89b7417a6aa2596c5c7fbb1fa7542accfd) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 5935533cee..d01e0d8d39 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1040,7 +1040,7 @@ Use with caution! void all_string_sub(char *s,const char *pattern,const char *insert) { char *p; - size_t ls,lp,li, i; + size_t ls,lp,li; if (!insert || !pattern || !s) return; -- cgit From bfc38ff872446e0ad365c22327c779e72a81bef9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 25 Nov 1998 21:17:20 +0000 Subject: Makefile.in: Added maintainer mode fixes. aclocal.m4: Added AC_LIBTESTFUNC. configure.in: Fixed -lsecurity -lsec problems. client.c: dos_ fixes. groupdb/aliasunix.c: Dead code removal. include/includes.h: Added default PRINTCAP_NAME. lib/genrand.c: dos_ fixes. lib/replace.c: Added strtoul. lib/system.c: dos_ fixes. lib/util.c: dos_ fixes. lib/util_sid.c: Signed/unsigned fixes. lib/util_str.c: removed bad const. locking/locking_slow.c: dos_ fixes. printing/printing.c: dos_ fixes. rpc_server/srv_samr.c: Dead code removal. rpc_server/srv_sid.c: global_myworkgroup defined with wrong size AGAIN ! smbd/dir.c: dos_ fixes. smbd/open.c: dos_ fixes. smbd/oplock.c: dos_ fixes. smbd/reply.c smbd/server.c smbd/service.c smbd/uid.c: dos_ fixes. Jeremy. (This used to be commit 6acb4b68f68d516e2ac3c47e500f5600d653435e) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d01e0d8d39..a55d4cd8dc 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -458,11 +458,11 @@ void string_replace(char *s,char oldc,char newc) /******************************************************************* skip past some strings in a buffer ********************************************************************/ -char *skip_string(const char *buf,size_t n) +char *skip_string(char *buf,size_t n) { while (n--) buf += strlen(buf) + 1; - return((char *)buf); + return(buf); } /******************************************************************* -- cgit From 78314c2e327ccd67e75dbc92be497ff2852b1817 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Tue, 16 Feb 1999 18:02:50 +0000 Subject: bitmap to strings (This used to be commit ba5919bcaefa792bae503c7ab19d4b7bbf9bb954) --- source3/lib/util_str.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index a55d4cd8dc..31dc9bfd62 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1089,3 +1089,73 @@ void split_at_last_component(char *path, char *front, char sep, char *back) } } } + +/**************************************************************************** +convert a bit field to a string. if you want multiple bits to be detected +set them first, e.g SV_TYPE_ALL to be "All" or "Full Control" for ACB_INFOs. + +strings are expected to contain their own separators, although the code +below only assumes that separators are spaces. + +****************************************************************************/ +char *bit_field_to_str(uint32 type, struct field_info *bs) +{ + static fstring typestr; + int i = 0; + + typestr[0] = 0; + + if (type == 0 || bs == NULL) + { + return NULL; + } + + while (bs[i].str != NULL && type != 0) + { + if (IS_BITS_SET_ALL(bs[i].bits, type)) + { + fstrcat(typestr, bs[i].str); + type &= ~bs[i].bits; + } + i++; + } + + i = strlen(typestr)-1; + if (i > 0 && typestr[i] == ' ') + { + typestr[i] = 0; + } + + return typestr; +} + +/**************************************************************************** +convert an enumeration to a string. first item is the default. +****************************************************************************/ +char *enum_field_to_str(uint32 type, struct field_info *bs, BOOL first_default) +{ + int i = 0; + + if (bs == NULL) + { + return NULL; + } + + while (bs[i].str != NULL && type != 0) + { + if (bs[i].bits == type) + { + return bs[i].str; + } + i++; + } + + /* oops - none found */ + + if (first_default) + { + return bs[0].str; + } + + return NULL; +} -- cgit From f7f718911b28ab5d57221ea5b3d26b202ef218e4 Mon Sep 17 00:00:00 2001 From: Matthew Chapman Date: Tue, 23 Mar 1999 14:56:25 +0000 Subject: Same infinite loop bug as I fixed in SAMBA_2_0, but I just spent half an hour tracking it down in HEAD so I'm going to commit it here as well. (This used to be commit 9a482aedb2c4eb7439e2edc092642d315d2a595b) --- source3/lib/util_str.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 31dc9bfd62..142f0af4c8 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -882,16 +882,16 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) continue; } - while (!(p1 = strchr(hexchars, toupper(strhex[i])))) + if (!(p1 = strchr(hexchars, toupper(strhex[i])))) { - continue; + break; } i++; /* next hex digit */ - while (!(p2 = strchr(hexchars, toupper(strhex[i])))) + if (!(p2 = strchr(hexchars, toupper(strhex[i])))) { - continue; + break; } /* get the two nybbles */ -- cgit From 55c0c85f4de322ab0dff777dbf2b4a2c09a96886 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 8 Jul 1999 18:57:58 +0000 Subject: allow safe_strcpy() to pass 0 for max length of string, resulting in no effect. (This used to be commit 47e54d049a4de3c2154b1e5edc3234b88bcc065f) --- source3/lib/util_str.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 142f0af4c8..4c6a993777 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -743,6 +743,11 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) { size_t len; + if (maxlength == 0) + { + return dest; + } + if (!dest) { DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); return NULL; -- cgit From cba7662da1fd9ed8bd9f9969417adf1fe5f0d33b Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 7 Oct 1999 22:10:29 +0000 Subject: - added rudimentary CAP_UNICODE support because i thought it was part of a problem i was having. - added rudimentary CAP_STATUS32 support for same reason. - added hard-coded, copy-the-same-data-from-over-the-wire version of CAP_EXTENDED_SECURITY, which is a security-blob to encapsulate GSSAPI which encodes SPNEGO which is used to negotiate Kerberos or NTLMSSP. i have implemented NTLMSSP which negotiates NTLMv1 or NTLMv2 and 40-bit or 128-bit etc. i have implemented NTLMv1 / 40-bit. *whew*. (This used to be commit e5b80bd2f76fda70e41e4a9007eb035dab92ed8e) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 4c6a993777..636be164ac 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -462,7 +462,7 @@ char *skip_string(char *buf,size_t n) { while (n--) buf += strlen(buf) + 1; - return(buf); + return buf; } /******************************************************************* -- cgit From 56128244261f8e4c6e1144da66c736fbc2104665 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Mon, 25 Oct 1999 19:03:27 +0000 Subject: - typecast malloc / Realloc issues. - signed / unsigned issues. (This used to be commit c8fd555179314baf1672a23db34dc8ad9f2d02bf) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 636be164ac..43e3224df4 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -842,7 +842,7 @@ char *StrnCpy(char *dest,const char *src,size_t n) like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). ****************************************************************************/ -char *strncpyn(char *dest, const char *src,size_t n, char c) +char *strncpyn(char *dest, char *src,size_t n, char c) { char *p; size_t str_len; -- cgit From 24a069eac302069559c6347b24276e7f1a04cc91 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Sat, 20 Nov 1999 20:54:29 +0000 Subject: modified domain_client_validate to take trust account name / type. this is to pass DOMAIN_NAME$ and SEC_CHAN_DOMAIN instead of WKSTA_NAME$ and SEC_CHAN_WKSTA. modified check_domain_security to determine if domain name is own domain, and to use wksta trust account if so, otherwise check "trusting domains" parameter and use inter-domain trust account if so, otherwise return False. (This used to be commit 97ec74e1fa99d773812d2df402251fafb76b181c) --- source3/lib/util_str.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 43e3224df4..a25043df78 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1063,6 +1063,38 @@ void all_string_sub(char *s,const char *pattern,const char *insert) } } +/**************************************************************************** + splits out the front and back at a separator. +****************************************************************************/ +void split_at_first_component(char *path, char *front, char sep, char *back) +{ + char *p = strchr(path, sep); + + if (p != NULL) + { + *p = 0; + } + if (front != NULL) + { + pstrcpy(front, path); + } + if (p != NULL) + { + if (back != NULL) + { + pstrcpy(back, p+1); + } + *p = sep; + } + else + { + if (back != NULL) + { + back[0] = 0; + } + } +} + /**************************************************************************** splits out the front and back at a separator. ****************************************************************************/ @@ -1084,7 +1116,7 @@ void split_at_last_component(char *path, char *front, char sep, char *back) { pstrcpy(back, p+1); } - *p = '\\'; + *p = sep; } else { -- cgit From 3db52feb1f3b2c07ce0b06ad4a7099fa6efe3fc7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 13 Dec 1999 13:27:58 +0000 Subject: first pass at updating head branch to be to be the same as the SAMBA_2_0 branch (This used to be commit 453a822a76780063dff23526c35408866d0c0154) --- source3/lib/util_str.c | 281 ++++++++++++++++++++++++++----------------------- 1 file changed, 152 insertions(+), 129 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index a25043df78..58718f395a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -462,7 +462,7 @@ char *skip_string(char *buf,size_t n) { while (n--) buf += strlen(buf) + 1; - return buf; + return(buf); } /******************************************************************* @@ -733,21 +733,69 @@ size_t count_chars(const char *s,char c) return(count); } +/******************************************************************* +Return True if a string consists only of one particular character. +********************************************************************/ +BOOL str_is_all(const char *s,char c) +{ + if(s == NULL) + return False; + if(!*s) + return False; + +#if !defined(KANJI_WIN95_COMPATIBILITY) + /* + * For completeness we should put in equivalent code for code pages + * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but + * doubt anyone wants Samba to behave differently from Win95 and WinNT + * here. They both treat full width ascii characters as case senstive + * filenames (ie. they don't do the work we do here). + * JRA. + */ + + if(lp_client_code_page() == KANJI_CODEPAGE) + { + /* Win95 treats full width ascii characters as case sensitive. */ + while (*s) + { + if (is_shift_jis (*s)) + s += 2; + else + { + if (*s != c) + return False; + s++; + } + } + } + else +#endif /* KANJI_WIN95_COMPATIBILITY */ + { + while (*s) + { + size_t skip = skip_multibyte_char( *s ); + if( skip != 0 ) + s += skip; + else { + if (*s != c) + return False; + s++; + } + } + } + return True; +} /******************************************************************* safe string copy into a known length string. maxlength does not include the terminating zero. ********************************************************************/ + char *safe_strcpy(char *dest,const char *src, size_t maxlength) { size_t len; - if (maxlength == 0) - { - return dest; - } - if (!dest) { DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); return NULL; @@ -762,7 +810,7 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) if (len > maxlength) { DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n", - len-maxlength, src)); + (int)(len-maxlength), src)); len = maxlength; } @@ -775,6 +823,7 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) safe string cat into a string. maxlength does not include the terminating zero. ********************************************************************/ + char *safe_strcat(char *dest, const char *src, size_t maxlength) { size_t src_len, dest_len; @@ -793,7 +842,7 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) if (src_len + dest_len > maxlength) { DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", - src_len + dest_len - maxlength, src)); + (int)(src_len + dest_len - maxlength), src)); src_len = maxlength - dest_len; } @@ -802,28 +851,48 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) return dest; } -/**************************************************************************** -this is a safer strcpy(), meant to prevent core dumps when nasty things happen -****************************************************************************/ -char *StrCpy(char *dest,const char *src) +/******************************************************************* + Paranoid strcpy into a buffer of given length (includes terminating + zero. Strips out all but 'a-Z0-9' and replaces with '_'. Deliberately + does *NOT* check for multibyte characters. Don't change it ! +********************************************************************/ + +char *alpha_strcpy(char *dest, const char *src, size_t maxlength) { - char *d = dest; + size_t len, i; - /* I don't want to get lazy with these ... */ - SMB_ASSERT(dest && src); + if (!dest) { + DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n")); + return NULL; + } - if (!dest) return(NULL); - if (!src) { - *dest = 0; - return(dest); - } - while ((*d++ = *src++)) ; - return(dest); + if (!src) { + *dest = 0; + return dest; + } + + len = strlen(src); + if (len >= maxlength) + len = maxlength - 1; + + for(i = 0; i < len; i++) { + int val = (src[i] & 0xff); + if(isupper(val) ||islower(val) || isdigit(val)) + dest[i] = src[i]; + else + dest[i] = '_'; + } + + dest[i] = '\0'; + + return dest; } /**************************************************************************** -like strncpy but always null terminates. Make sure there is room! + Like strncpy but always null terminates. Make sure there is room! + The variable n should always be one less than the available size. ****************************************************************************/ + char *StrnCpy(char *dest,const char *src,size_t n) { char *d = dest; @@ -837,12 +906,11 @@ char *StrnCpy(char *dest,const char *src,size_t n) return(dest); } - /**************************************************************************** like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). ****************************************************************************/ -char *strncpyn(char *dest, char *src,size_t n, char c) +char *strncpyn(char *dest, const char *src,size_t n, char c) { char *p; size_t str_len; @@ -1003,29 +1071,42 @@ enough room! This routine looks for pattern in s and replaces it with insert. It may do multiple replacements. -any of " ; ' or ` in the insert string are replaced with _ +any of " ; ' $ or ` in the insert string are replaced with _ +if len==0 then no length check is performed ****************************************************************************/ -void string_sub(char *s,const char *pattern,const char *insert) +void string_sub(char *s,const char *pattern,const char *insert, size_t len) { char *p; - size_t ls,lp,li, i; + ssize_t ls,lp,li, i; if (!insert || !pattern || !s) return; - ls = strlen(s); - lp = strlen(pattern); - li = strlen(insert); + ls = (ssize_t)strlen(s); + lp = (ssize_t)strlen(pattern); + li = (ssize_t)strlen(insert); if (!*pattern) return; while (lp <= ls && (p = strstr(s,pattern))) { - memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp)); + if (len && (ls + (li-lp) >= len)) { + DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", + (int)(ls + (li-lp) - len), + pattern, (int)len)); + break; + } + if (li != lp) { + memmove(p+li,p+lp,strlen(p+lp)+1); + } for (i=0;i= len)) { + DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", + (int)(ls + (li-lp) - len), + pattern, (int)len)); + break; + } + if (li != lp) { + memmove(p+li,p+lp,strlen(p+lp)+1); + } memcpy(p, insert, li); s = p + li; ls += (li-lp); } } -/**************************************************************************** - splits out the front and back at a separator. -****************************************************************************/ -void split_at_first_component(char *path, char *front, char sep, char *back) -{ - char *p = strchr(path, sep); - - if (p != NULL) - { - *p = 0; - } - if (front != NULL) - { - pstrcpy(front, path); - } - if (p != NULL) - { - if (back != NULL) - { - pstrcpy(back, p+1); - } - *p = sep; - } - else - { - if (back != NULL) - { - back[0] = 0; - } - } -} - /**************************************************************************** splits out the front and back at a separator. ****************************************************************************/ @@ -1116,7 +1183,7 @@ void split_at_last_component(char *path, char *front, char sep, char *back) { pstrcpy(back, p+1); } - *p = sep; + *p = '\\'; } else { @@ -1127,72 +1194,28 @@ void split_at_last_component(char *path, char *front, char sep, char *back) } } -/**************************************************************************** -convert a bit field to a string. if you want multiple bits to be detected -set them first, e.g SV_TYPE_ALL to be "All" or "Full Control" for ACB_INFOs. - -strings are expected to contain their own separators, although the code -below only assumes that separators are spaces. +/**************************************************************************** +write an octal as a string ****************************************************************************/ -char *bit_field_to_str(uint32 type, struct field_info *bs) +char *octal_string(int i) { - static fstring typestr; - int i = 0; - - typestr[0] = 0; - - if (type == 0 || bs == NULL) - { - return NULL; - } - - while (bs[i].str != NULL && type != 0) - { - if (IS_BITS_SET_ALL(bs[i].bits, type)) - { - fstrcat(typestr, bs[i].str); - type &= ~bs[i].bits; - } - i++; - } - - i = strlen(typestr)-1; - if (i > 0 && typestr[i] == ' ') - { - typestr[i] = 0; + static char ret[64]; + if (i == -1) { + return "-1"; } - - return typestr; + slprintf(ret, sizeof(ret), "0%o", i); + return ret; } + /**************************************************************************** -convert an enumeration to a string. first item is the default. +truncate a string at a specified length ****************************************************************************/ -char *enum_field_to_str(uint32 type, struct field_info *bs, BOOL first_default) +char *string_truncate(char *s, int length) { - int i = 0; - - if (bs == NULL) - { - return NULL; + if (s && strlen(s) > length) { + s[length] = 0; } - - while (bs[i].str != NULL && type != 0) - { - if (bs[i].bits == type) - { - return bs[i].str; - } - i++; - } - - /* oops - none found */ - - if (first_default) - { - return bs[0].str; - } - - return NULL; + return s; } -- cgit From 5eae2eaebeff8819c0f840e52814e0c69907032e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 16 Jan 2000 11:07:11 +0000 Subject: make string_init() static use string_set() instead, to avoid the bug Richard discovered (This used to be commit fdcbf6b52d8373bf8f35718a9649788415c23342) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 58718f395a..b16de3efda 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1008,7 +1008,7 @@ static char *null_string = NULL; /**************************************************************************** set a string value, allocing the space for the string ****************************************************************************/ -BOOL string_init(char **dest,const char *src) +static BOOL string_init(char **dest,const char *src) { size_t l; if (!src) -- cgit From d867b86721e988dee56d5e9382b32c870ccb2790 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 26 Jan 2000 00:12:35 +0000 Subject: Second set of inline optimisation fixes from Ying Chen . Stop makeing function calls for every use of skip_multibyte_char. This function is called several *million* times during a NetBench run :-). Jeremy. (This used to be commit e5a3deba46ea2d4cb49a6c4b73edd766fe8b5a5c) --- source3/lib/util_str.c | 74 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 23 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b16de3efda..d703670860 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -343,7 +343,7 @@ void strlower(char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - size_t skip = skip_multibyte_char( *s ); + size_t skip = get_character_len( *s ); if( skip != 0 ) s += skip; else @@ -396,7 +396,7 @@ void strupper(char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - size_t skip = skip_multibyte_char( *s ); + size_t skip = get_character_len( *s ); if( skip != 0 ) s += skip; else @@ -440,17 +440,29 @@ BOOL strisnormal(char *s) void string_replace(char *s,char oldc,char newc) { size_t skip; - while (*s) - { - skip = skip_multibyte_char( *s ); - if( skip != 0 ) - s += skip; - else - { + + /* + * sbcs optimization. + */ + if(!global_is_multibyte_codepage) { + while (*s) { if (oldc == *s) *s = newc; s++; } + } else { + while (*s) + { + skip = get_character_len( *s ); + if( skip != 0 ) + s += skip; + else + { + if (oldc == *s) + *s = newc; + s++; + } + } } } @@ -476,10 +488,17 @@ size_t str_charnum(const char *s) { size_t len = 0; - while (*s != '\0') { - int skip = skip_multibyte_char(*s); - s += (skip ? skip : 1); - len++; + /* + * sbcs optimization. + */ + if(!global_is_multibyte_codepage) { + return strlen(s); + } else { + while (*s != '\0') { + int skip = get_character_len(*s); + s += (skip ? skip : 1); + len++; + } } return len; } @@ -518,7 +537,7 @@ BOOL trim_string(char *s,const char *front,const char *back) if(back_len) { - if(!is_multibyte_codepage()) + if(!global_is_multibyte_codepage) { s_len = strlen(s); while ((s_len >= back_len) && @@ -552,11 +571,20 @@ BOOL trim_string(char *s,const char *front,const char *back) size_t charcount = 0; char *mbp = s; - while(charcount < (mb_s_len - mb_back_len)) - { - size_t skip = skip_multibyte_char(*mbp); - mbp += (skip ? skip : 1); - charcount++; + /* + * sbcs optimization. + */ + if(!global_is_multibyte_codepage) { + while(charcount < (mb_s_len - mb_back_len)) { + mbp += 1; + charcount++; + } + } else { + while(charcount < (mb_s_len - mb_back_len)) { + size_t skip = skip_multibyte_char(*mbp); + mbp += (skip ? skip : 1); + charcount++; + } } /* @@ -615,7 +643,7 @@ BOOL strhasupper(const char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - size_t skip = skip_multibyte_char( *s ); + size_t skip = get_character_len( *s ); if( skip != 0 ) s += skip; else { @@ -670,7 +698,7 @@ BOOL strhaslower(const char *s) else #endif /* KANJI_WIN95_COMPATIBILITY */ { - size_t skip = skip_multibyte_char( *s ); + size_t skip = get_character_len( *s ); if( skip != 0 ) s += skip; else { @@ -720,7 +748,7 @@ size_t count_chars(const char *s,char c) { while (*s) { - size_t skip = skip_multibyte_char( *s ); + size_t skip = get_character_len( *s ); if( skip != 0 ) s += skip; else { @@ -774,7 +802,7 @@ BOOL str_is_all(const char *s,char c) { while (*s) { - size_t skip = skip_multibyte_char( *s ); + size_t skip = get_character_len( *s ); if( skip != 0 ) s += skip; else { -- cgit From 3bf8d26f7c5705f832034cf676942551ba1e1a73 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 14 Jun 2000 09:57:51 +0000 Subject: Merged parse_domain_user() from TNG. (This used to be commit f64ac9d9068901862290f7b25874156d6f0d4d73) --- source3/lib/util_str.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d703670860..41c012ba34 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1247,3 +1247,23 @@ char *string_truncate(char *s, int length) } return s; } + +/* Parse a string of the form DOMAIN/user into a domain and a user */ + +void parse_domain_user(char *domuser, fstring domain, fstring user) +{ + char *p; + char *sep = lp_winbind_separator(); + if (!sep) sep = "\\"; + p = strchr(domuser,*sep); + if (!p) p = strchr(domuser,'\\'); + if (!p) { + fstrcpy(domain,""); + fstrcpy(user, domuser); + return; + } + + fstrcpy(user, p+1); + fstrcpy(domain, domuser); + domain[PTR_DIFF(p, domuser)] = 0; +} -- cgit From 4326894f6a10c55a1b95b94023952c4e30439074 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 29 Aug 2000 14:33:39 +0000 Subject: needed to use strwicmp() in smbclient code, so I moved it to util_str.c and made it non-static --jerry (This used to be commit dfdca21bd90b9c83f195d580ec9d774f1be8f9cb) --- source3/lib/util_str.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 41c012ba34..822267f5d5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -302,6 +302,36 @@ BOOL strcsequal(const char *s1,const char *s2) return(strcmp(s1,s2)==0); } +/*************************************************************************** +Do a case-insensitive, whitespace-ignoring string compare. +***************************************************************************/ +int strwicmp(char *psz1, char *psz2) +{ + /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */ + /* appropriate value. */ + if (psz1 == psz2) + return (0); + else if (psz1 == NULL) + return (-1); + else if (psz2 == NULL) + return (1); + + /* sync the strings on first non-whitespace */ + while (1) + { + while (isspace(*psz1)) + psz1++; + while (isspace(*psz2)) + psz2++; + if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0' + || *psz2 == '\0') + break; + psz1++; + psz2++; + } + return (*psz1 - *psz2); +} + /******************************************************************* convert a string to lower case -- cgit From 23807f2b308e80a1e325c8fd2bddeec3e2e15bc5 Mon Sep 17 00:00:00 2001 From: David O'Neill Date: Thu, 4 Jan 2001 19:27:08 +0000 Subject: Changes from APPLIANCE_HEAD: source/Makefile.in - changes to ctags and etags rules that somehow got lost along the way. source/include/proto.h - make proto source/smbd/sec_ctx.c source/smbd/password.c - merge debugs for debugging user groups and NT token stuff. source/lib/util_str.c - capitalise domain name returned from parse_domain_user() source/nsswitch/wb_client.c - fix broken conditional in debug statement. source/include/rpc_secdes.h source/include/rpc_spoolss.h source/printing/nt_printing.c source/lib/util_seaccess.c - fix printer permission bugs related to ACE masks for printers. This adds mapping of generic access rights to object specific rights for NT printers. Still need to work out whether or not to ignore ACEs with certain flags set, though. See comments in util_seaccess.c:check_ace() for details. source/printing/nt_printing.c source/printing/printing.c - use PRINTER_ACCESS_ADMINISTER instead of JOB_ACCESS_ADMINISTER until we sort out printer/printjob permission stuff. (This used to be commit 1dba9c5cd1e6389734c648f6903abcb7c8d5b2f0) --- source3/lib/util_str.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 822267f5d5..e07e5ef6ad 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1296,4 +1296,5 @@ void parse_domain_user(char *domuser, fstring domain, fstring user) fstrcpy(user, p+1); fstrcpy(domain, domuser); domain[PTR_DIFF(p, domuser)] = 0; + strupper(domain); } -- cgit From f9a15ce1a69f905e94db7650f0a4805720cd9c88 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sun, 8 Apr 2001 20:22:39 +0000 Subject: Got "medieval on our ass" about adding the -1 to slprintf. Jeremy. (This used to be commit 94747b4639ed9b19f7d0fb896e43aa392a84989a) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e07e5ef6ad..03ad5a66b0 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1262,7 +1262,7 @@ char *octal_string(int i) if (i == -1) { return "-1"; } - slprintf(ret, sizeof(ret), "0%o", i); + slprintf(ret, sizeof(ret)-1, "0%o", i); return ret; } -- cgit From 78ac23f7e0f5e12c6b56adce91e356a3963c196f Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sun, 15 Apr 2001 22:21:04 +0000 Subject: Added Darwin guess. lib/util_str.c: Excellent patch from Kenichi Okuyama to speed up trim_string handling ! Jeremy. (This used to be commit 4bb63ba615c735a298a6cbda2c87242695104978) --- source3/lib/util_str.c | 181 +++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 88 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 03ad5a66b0..d09bd6a2b4 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -539,103 +539,108 @@ trim the specified elements off the front and back of a string BOOL trim_string(char *s,const char *front,const char *back) { - BOOL ret = False; - size_t front_len = (front && *front) ? strlen(front) : 0; - size_t back_len = (back && *back) ? strlen(back) : 0; - size_t s_len; - - while (front_len && strncmp(s, front, front_len) == 0) - { - char *p = s; - ret = True; - while (1) - { - if (!(*p = p[front_len])) - break; - p++; + BOOL ret = False; + size_t s_len; + size_t front_len; + size_t back_len; + char *sP; + + if ( !s ) { + return False; } - } - - /* - * We split out the multibyte code page - * case here for speed purposes. Under a - * multibyte code page we need to walk the - * string forwards only and multiple times. - * Thanks to John Blair for finding this - * one. JRA. - */ - - if(back_len) - { - if(!global_is_multibyte_codepage) - { - s_len = strlen(s); - while ((s_len >= back_len) && - (strncmp(s + s_len - back_len, back, back_len)==0)) - { - ret = True; - s[s_len - back_len] = '\0'; - s_len = strlen(s); - } + sP = s; + s_len = strlen( s ) + 1; + front_len = (front) ? strlen( front ) + 1 : 0; + back_len = (back) ? strlen( back ) + 1 : 0; + + /* + * remove "front" string from given "s", if it matches front part, + * repeatedly. + */ + if ( front && front_len > 1 ) { + while (( s_len >= front_len )&& + ( memcmp( sP, front, front_len - 1 )) == 0 ) { + ret = True; + sP += ( front_len - 1 ); + s_len -= ( front_len - 1 ); + } } - else - { - - /* - * Multibyte code page case. - * Keep going through the string, trying - * to match the 'back' string with the end - * of the string. If we get a match, truncate - * 'back' off the end of the string and - * go through the string again from the - * start. Keep doing this until we have - * gone through the string with no match - * at the string end. - */ - - size_t mb_back_len = str_charnum(back); - size_t mb_s_len = str_charnum(s); - - while(mb_s_len >= mb_back_len) - { - size_t charcount = 0; - char *mbp = s; - /* - * sbcs optimization. - */ - if(!global_is_multibyte_codepage) { - while(charcount < (mb_s_len - mb_back_len)) { - mbp += 1; - charcount++; - } - } else { - while(charcount < (mb_s_len - mb_back_len)) { - size_t skip = skip_multibyte_char(*mbp); - mbp += (skip ? skip : 1); - charcount++; - } + /* + * we'll memmove sP to s later, after we're done with + * back part removal, for minimizing copy. + */ + + + /* + * We split out the multibyte code page + * case here for speed purposes. Under a + * multibyte code page we need to walk the + * string forwards only and multiple times. + * Thanks to John Blair for finding this + * one. JRA. + */ + /* + * This JRA's comment is partly correct, but partly wrong. + * You can always check from "end" part, and if it did not match, + * it means there is no possibility of finding one. + * If you found matching point, mark them, then look from front + * if marking point suits multi-byte string rule. + * Kenichi Okuyama. + */ + + if ( back && back_len > 1 ) { + char *bP = sP + s_len - back_len; + long b_len = s_len; + + while (( b_len >= back_len )&& + ( memcmp( bP, back, back_len - 1 ) == 0 )) { + bP -= ( back_len - 1 ); + b_len -= ( back_len - 1 ); } /* - * mbp now points at mb_back_len multibyte - * characters from the end of s. + * You're here, means you ether have found match multiple times, + * or you found none. If you've found match, then bP should be + * moving. */ - - if(strcmp(mbp, back) == 0) - { - ret = True; - *mbp = '\0'; - mb_s_len = str_charnum(s); - mbp = s; + if ( bP != sP + s_len - back_len ) { + bP += ( back_len - 1 ); /* slide bP to first matching point. */ + + if( !global_is_multibyte_codepage ) { + /* simply terminate */ + (*bP) = '\0'; + s_len = b_len; + ret = True; + } else { + /* trace string from start. */ + char *cP = sP; + while ( cP < sP + s_len - back_len ) { + size_t skip; + skip = skip_multibyte_char( *cP ); + cP += ( skip ? skip : 1 ); + if ( cP == bP ) { + /* you found the match */ + (*bP) = '\0'; + ret = True; + s_len = b_len; + break; + } + while (( cP > bP )&&( bP < sP + s_len - back_len )) { + bP += ( back_len - 1 ); + b_len += ( back_len - 1 ); + } + } + } } - else - break; - } /* end while mb_s_len... */ - } /* end else .. */ - } /* end if back_len .. */ + } - return(ret); + /* if front found matching point */ + if ( sP != s ) { + /* slide string to buffer top */ + memmove( s, sP, s_len ); + } + return ret; } -- cgit From a36f9250e7c9446f3eece6d8db29fcbde99256fb Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Mon, 7 May 2001 04:32:40 +0000 Subject: Preliminary merge of winbind into HEAD. Note that this compiles and links but I haven't actually run it yet so it probably doesn't work. (-: (This used to be commit 59f95416b66db6df05289bde224de29c721978e5) --- source3/lib/util_str.c | 21 --------------------- 1 file changed, 21 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d09bd6a2b4..b517d93dd8 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1282,24 +1282,3 @@ char *string_truncate(char *s, int length) } return s; } - -/* Parse a string of the form DOMAIN/user into a domain and a user */ - -void parse_domain_user(char *domuser, fstring domain, fstring user) -{ - char *p; - char *sep = lp_winbind_separator(); - if (!sep) sep = "\\"; - p = strchr(domuser,*sep); - if (!p) p = strchr(domuser,'\\'); - if (!p) { - fstrcpy(domain,""); - fstrcpy(user, domuser); - return; - } - - fstrcpy(user, p+1); - fstrcpy(domain, domuser); - domain[PTR_DIFF(p, domuser)] = 0; - strupper(domain); -} -- cgit From e0f5d84e9d4c7eb396d48117ebb1b116e19e0700 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 10 May 2001 23:17:46 +0000 Subject: Fix for problem with "" string in trim_string(). Pointed out by Ben Winslow . Jeremy. (This used to be commit df2e306171f0a71af77dbaafc7ccbf78e27befb8) --- source3/lib/util_str.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b517d93dd8..78366fceb7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -545,9 +545,11 @@ BOOL trim_string(char *s,const char *front,const char *back) size_t back_len; char *sP; - if ( !s ) { + /* Ignore null or empty strings. */ + + if ( !s || (s[0] == '\0')) return False; - } + sP = s; s_len = strlen( s ) + 1; front_len = (front) ? strlen( front ) + 1 : 0; @@ -589,7 +591,7 @@ BOOL trim_string(char *s,const char *front,const char *back) * Kenichi Okuyama. */ - if ( back && back_len > 1 ) { + if ( back && back_len > 1 && s_len > back_len) { char *bP = sP + s_len - back_len; long b_len = s_len; -- cgit From 91febc38e32bd2f519d3254503f41b1e6014776c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 10 May 2001 23:55:33 +0000 Subject: Needs to be >=, not just >. Jeremy. (This used to be commit fa6a5bf94a3716c0554e2bcbe0eb4b0912f15604) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 78366fceb7..0f32fda63c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -591,7 +591,7 @@ BOOL trim_string(char *s,const char *front,const char *back) * Kenichi Okuyama. */ - if ( back && back_len > 1 && s_len > back_len) { + if ( back && back_len > 1 && s_len >= back_len) { char *bP = sP + s_len - back_len; long b_len = s_len; -- cgit From 91b8a8d1d21b810b6aca44ce647837669efd6dcf Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 21 Jun 2001 09:10:42 +0000 Subject: next_token() was supposed to be a reentrant replacement for strtok(), but the code suffered from bitrot and is not now reentrant. That means we can get bizarre behaviour i've fixed this by making next_token() reentrant and creating a next_token_nr() that is a small non-reentrant wrapper for those lumps of code (mostly smbclient) that have come to rely on the non-reentrant behaviour (This used to be commit 674ee2f1d12b0afc164a9e9072758fd1c5e54df7) --- source3/lib/util_str.c | 129 +++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 57 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 0f32fda63c..f1376fe0f7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -23,13 +23,6 @@ extern int DEBUGLEVEL; -static char *last_ptr=NULL; - -void set_first_token(char *ptr) -{ - last_ptr = ptr; -} - /**************************************************************************** Get the next token from a string, return False if none found handles double-quotes. @@ -38,77 +31,99 @@ Extensively modified by Andrew.Tridgell@anu.edu.au ****************************************************************************/ BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize) { - char *s; - BOOL quoted; - size_t len=1; + char *s; + BOOL quoted; + size_t len=1; - if (!ptr) ptr = &last_ptr; - if (!ptr) return(False); + if (!ptr) return(False); - s = *ptr; + s = *ptr; - /* default to simple separators */ - if (!sep) sep = " \t\n\r"; + /* default to simple separators */ + if (!sep) sep = " \t\n\r"; - /* find the first non sep char */ - while(*s && strchr(sep,*s)) s++; + /* find the first non sep char */ + while (*s && strchr(sep,*s)) s++; + + /* nothing left? */ + if (! *s) return(False); + + /* copy over the token */ + for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) { + if (*s == '\"') { + quoted = !quoted; + } else { + len++; + *buff++ = *s; + } + } + + *ptr = (*s) ? s+1 : s; + *buff = 0; + + return(True); +} - /* nothing left? */ - if (! *s) return(False); - /* copy over the token */ - for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) - { - if (*s == '\"') { - quoted = !quoted; - } else { - len++; - *buff++ = *s; - } - } - *ptr = (*s) ? s+1 : s; - *buff = 0; - last_ptr = *ptr; +/**************************************************************************** +This is like next_token but is not re-entrant and "remembers" the first +parameter so you can pass NULL. This is useful for user interface code +but beware the fact that it is not re-entrant! +****************************************************************************/ +static char *last_ptr=NULL; - return(True); +BOOL next_token_nr(char **ptr,char *buff,char *sep, size_t bufsize) +{ + BOOL ret; + if (!ptr) ptr = &last_ptr; + + ret = next_token(ptr, buff, sep, bufsize); + last_ptr = *ptr; + return ret; } +void set_first_token(char *ptr) +{ + last_ptr = ptr; +} + + /**************************************************************************** Convert list of tokens to array; dependent on above routine. Uses last_ptr from above - bit of a hack. ****************************************************************************/ char **toktocliplist(int *ctok, char *sep) { - char *s=last_ptr; - int ictok=0; - char **ret, **iret; + char *s=last_ptr; + int ictok=0; + char **ret, **iret; - if (!sep) sep = " \t\n\r"; + if (!sep) sep = " \t\n\r"; - while(*s && strchr(sep,*s)) s++; + while(*s && strchr(sep,*s)) s++; - /* nothing left? */ - if (!*s) return(NULL); + /* nothing left? */ + if (!*s) return(NULL); - do { - ictok++; - while(*s && (!strchr(sep,*s))) s++; - while(*s && strchr(sep,*s)) *s++=0; - } while(*s); - - *ctok=ictok; - s=last_ptr; - - if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL; - - while(ictok--) { - *iret++=s; - while(*s++); - while(!*s) s++; - } + do { + ictok++; + while(*s && (!strchr(sep,*s))) s++; + while(*s && strchr(sep,*s)) *s++=0; + } while(*s); + + *ctok=ictok; + s=last_ptr; + + if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL; + + while(ictok--) { + *iret++=s; + while(*s++); + while(!*s) s++; + } - return ret; + return ret; } -- cgit From 37eb0d6c74ce158b1cc268cea446b33789550048 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 23 Jun 2001 07:22:16 +0000 Subject: Added other_safe_chars to alpha_strcpy(). Needs testing but is a better fix for the problem. Jeremy. (This used to be commit e059fffd03a1382fb2b7059b6de369d9fc765a17) --- source3/lib/util_str.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f1376fe0f7..1c1b31a83c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -933,11 +933,12 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) /******************************************************************* Paranoid strcpy into a buffer of given length (includes terminating - zero. Strips out all but 'a-Z0-9' and replaces with '_'. Deliberately - does *NOT* check for multibyte characters. Don't change it ! + zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars + and replaces with '_'. Deliberately does *NOT* check for multibyte + characters. Don't change it ! ********************************************************************/ -char *alpha_strcpy(char *dest, const char *src, size_t maxlength) +char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength) { size_t len, i; @@ -955,9 +956,12 @@ char *alpha_strcpy(char *dest, const char *src, size_t maxlength) if (len >= maxlength) len = maxlength - 1; + if (!other_safe_chars) + other_safe_chars = ""; + for(i = 0; i < len; i++) { int val = (src[i] & 0xff); - if(isupper(val) ||islower(val) || isdigit(val)) + if(isupper(val) || islower(val) || isdigit(val) || strchr(other_safe_chars, val)) dest[i] = src[i]; else dest[i] = '_'; -- cgit From b3443597e2eedd2b64630ccd5ec43cf0b3c5f148 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 29 Jun 2001 01:15:28 +0000 Subject: Replaced memcpy() with memmove() to make safe_strcpy() safe for overlapping source and destination. (This used to be commit 30411d4004ce7062e73506d228ef402b99226eee) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1c1b31a83c..d52ff82b10 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -894,7 +894,7 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) len = maxlength; } - memcpy(dest, src, len); + memmove(dest, src, len); dest[len] = 0; return dest; } -- cgit From 87fbb7092b8f8b2f0db0f361c3d625e19de57cd9 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 4 Jul 2001 07:15:53 +0000 Subject: The big character set handling changeover! This commit gets rid of all our old codepage handling and replaces it with iconv. All internal strings in Samba are now in "unix" charset, which may be multi-byte. See internals.doc and my posting to samba-technical for a more complete explanation. (This used to be commit debb471267960e56005a741817ebd227ecfc512a) --- source3/lib/util_str.c | 808 +++++++++++-------------------------------------- 1 file changed, 181 insertions(+), 627 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d52ff82b10..8ff3e23443 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1,8 +1,8 @@ /* Unix SMB/Netbios implementation. - Version 1.9. + Version 3.0 Samba utility functions - Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Andrew Tridgell 1992-2001 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -83,6 +83,8 @@ BOOL next_token_nr(char **ptr,char *buff,char *sep, size_t bufsize) return ret; } +static uint16 tmpbuf[sizeof(pstring)]; + void set_first_token(char *ptr) { last_ptr = ptr; @@ -126,78 +128,15 @@ char **toktocliplist(int *ctok, char *sep) return ret; } - /******************************************************************* case insensitive string compararison ********************************************************************/ int StrCaseCmp(const char *s, const char *t) { - /* compare until we run out of string, either t or s, or find a difference */ - /* We *must* use toupper rather than tolower here due to the - asynchronous upper to lower mapping. - */ -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - int diff; - for (;;) - { - if (!*s || !*t) - return toupper (*s) - toupper (*t); - else if (is_sj_alph (*s) && is_sj_alph (*t)) - { - diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1)); - if (diff) - return diff; - s += 2; - t += 2; - } - else if (is_shift_jis (*s) && is_shift_jis (*t)) - { - diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t); - if (diff) - return diff; - diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1)); - if (diff) - return diff; - s += 2; - t += 2; - } - else if (is_shift_jis (*s)) - return 1; - else if (is_shift_jis (*t)) - return -1; - else - { - diff = toupper (*s) - toupper (*t); - if (diff) - return diff; - s++; - t++; - } - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - while (*s && *t && toupper(*s) == toupper(*t)) - { - s++; - t++; - } - - return(toupper(*s) - toupper(*t)); - } + pstring buf1, buf2; + unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1)); + unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2)); + return strcmp(buf1,buf2); } /******************************************************************* @@ -205,83 +144,10 @@ int StrCaseCmp(const char *s, const char *t) ********************************************************************/ int StrnCaseCmp(const char *s, const char *t, size_t n) { - /* compare until we run out of string, either t or s, or chars */ - /* We *must* use toupper rather than tolower here due to the - asynchronous upper to lower mapping. - */ -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - int diff; - for (;n > 0;) - { - if (!*s || !*t) - return toupper (*s) - toupper (*t); - else if (is_sj_alph (*s) && is_sj_alph (*t)) - { - diff = sj_toupper2 (*(s+1)) - sj_toupper2 (*(t+1)); - if (diff) - return diff; - s += 2; - t += 2; - n -= 2; - } - else if (is_shift_jis (*s) && is_shift_jis (*t)) - { - diff = ((int) (unsigned char) *s) - ((int) (unsigned char) *t); - if (diff) - return diff; - diff = ((int) (unsigned char) *(s+1)) - ((int) (unsigned char) *(t+1)); - if (diff) - return diff; - s += 2; - t += 2; - n -= 2; - } - else if (is_shift_jis (*s)) - return 1; - else if (is_shift_jis (*t)) - return -1; - else - { - diff = toupper (*s) - toupper (*t); - if (diff) - return diff; - s++; - t++; - n--; - } - } - return 0; - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - while (n && *s && *t && toupper(*s) == toupper(*t)) - { - s++; - t++; - n--; - } - - /* not run out of chars - strings are different lengths */ - if (n) - return(toupper(*s) - toupper(*t)); - - /* identical up to where we run out of chars, - and strings are same length */ - return(0); - } + pstring buf1, buf2; + unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1)); + unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2)); + return strncmp(buf1,buf2,n); } /******************************************************************* @@ -348,112 +214,6 @@ int strwicmp(char *psz1, char *psz2) } -/******************************************************************* - convert a string to lower case -********************************************************************/ -void strlower(char *s) -{ - while (*s) - { -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - if (is_shift_jis (*s)) - { - if (is_sj_upper (s[0], s[1])) - s[1] = sj_tolower2 (s[1]); - s += 2; - } - else if (is_kana (*s)) - { - s++; - } - else - { - if (isupper(*s)) - *s = tolower(*s); - s++; - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - size_t skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else - { - if (isupper(*s)) - *s = tolower(*s); - s++; - } - } - } -} - -/******************************************************************* - convert a string to upper case -********************************************************************/ -void strupper(char *s) -{ - while (*s) - { -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - if (is_shift_jis (*s)) - { - if (is_sj_lower (s[0], s[1])) - s[1] = sj_toupper2 (s[1]); - s += 2; - } - else if (is_kana (*s)) - { - s++; - } - else - { - if (islower(*s)) - *s = toupper(*s); - s++; - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - size_t skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else - { - if (islower(*s)) - *s = toupper(*s); - s++; - } - } - } -} - /******************************************************************* convert a string to "normal" form ********************************************************************/ @@ -471,44 +231,26 @@ check if a string is in "normal" case ********************************************************************/ BOOL strisnormal(char *s) { - extern int case_default; - if (case_default == CASE_UPPER) - return(!strhaslower(s)); - - return(!strhasupper(s)); + extern int case_default; + if (case_default == CASE_UPPER) + return(!strhaslower(s)); + + return(!strhasupper(s)); } /**************************************************************************** string replace + NOTE: oldc and newc must be 7 bit characters ****************************************************************************/ void string_replace(char *s,char oldc,char newc) { - size_t skip; - - /* - * sbcs optimization. - */ - if(!global_is_multibyte_codepage) { - while (*s) { - if (oldc == *s) - *s = newc; - s++; - } - } else { - while (*s) - { - skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else - { - if (oldc == *s) - *s = newc; - s++; - } - } - } + smb_ucs2_t *ptr; + push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + for(ptr=tmpbuf;*ptr;ptr++) { + if(*ptr==UCS2_CHAR(oldc)) *ptr = UCS2_CHAR(newc); + } + pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); } @@ -517,35 +259,20 @@ skip past some strings in a buffer ********************************************************************/ char *skip_string(char *buf,size_t n) { - while (n--) - buf += strlen(buf) + 1; - return(buf); + while (n--) + buf += strlen(buf) + 1; + return(buf); } /******************************************************************* Count the number of characters in a string. Normally this will be the same as the number of bytes in a string for single byte strings, but will be different for multibyte. - 16.oct.98, jdblair@cobaltnet.com. ********************************************************************/ - size_t str_charnum(const char *s) { - size_t len = 0; - - /* - * sbcs optimization. - */ - if(!global_is_multibyte_codepage) { - return strlen(s); - } else { - while (*s != '\0') { - int skip = get_character_len(*s); - s += (skip ? skip : 1); - len++; - } - } - return len; + push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + return strlen_w(tmpbuf); } /******************************************************************* @@ -554,110 +281,36 @@ trim the specified elements off the front and back of a string BOOL trim_string(char *s,const char *front,const char *back) { - BOOL ret = False; - size_t s_len; - size_t front_len; - size_t back_len; - char *sP; + BOOL ret = False; + size_t front_len; + size_t back_len; + size_t len; /* Ignore null or empty strings. */ + if (!s || (s[0] == '\0')) + return False; - if ( !s || (s[0] == '\0')) - return False; - - sP = s; - s_len = strlen( s ) + 1; - front_len = (front) ? strlen( front ) + 1 : 0; - back_len = (back) ? strlen( back ) + 1 : 0; - - /* - * remove "front" string from given "s", if it matches front part, - * repeatedly. - */ - if ( front && front_len > 1 ) { - while (( s_len >= front_len )&& - ( memcmp( sP, front, front_len - 1 )) == 0 ) { - ret = True; - sP += ( front_len - 1 ); - s_len -= ( front_len - 1 ); - } - } + front_len = front? strlen(front) : 0; + back_len = back? strlen(back) : 0; - /* - * we'll memmove sP to s later, after we're done with - * back part removal, for minimizing copy. - */ - - - /* - * We split out the multibyte code page - * case here for speed purposes. Under a - * multibyte code page we need to walk the - * string forwards only and multiple times. - * Thanks to John Blair for finding this - * one. JRA. - */ - /* - * This JRA's comment is partly correct, but partly wrong. - * You can always check from "end" part, and if it did not match, - * it means there is no possibility of finding one. - * If you found matching point, mark them, then look from front - * if marking point suits multi-byte string rule. - * Kenichi Okuyama. - */ - - if ( back && back_len > 1 && s_len >= back_len) { - char *bP = sP + s_len - back_len; - long b_len = s_len; - - while (( b_len >= back_len )&& - ( memcmp( bP, back, back_len - 1 ) == 0 )) { - bP -= ( back_len - 1 ); - b_len -= ( back_len - 1 ); - } + len = strlen(s); - /* - * You're here, means you ether have found match multiple times, - * or you found none. If you've found match, then bP should be - * moving. - */ - if ( bP != sP + s_len - back_len ) { - bP += ( back_len - 1 ); /* slide bP to first matching point. */ - - if( !global_is_multibyte_codepage ) { - /* simply terminate */ - (*bP) = '\0'; - s_len = b_len; - ret = True; - } else { - /* trace string from start. */ - char *cP = sP; - while ( cP < sP + s_len - back_len ) { - size_t skip; - skip = skip_multibyte_char( *cP ); - cP += ( skip ? skip : 1 ); - if ( cP == bP ) { - /* you found the match */ - (*bP) = '\0'; - ret = True; - s_len = b_len; - break; - } - while (( cP > bP )&&( bP < sP + s_len - back_len )) { - bP += ( back_len - 1 ); - b_len += ( back_len - 1 ); - } - } - } - } - } - - /* if front found matching point */ - if ( sP != s ) { - /* slide string to buffer top */ - memmove( s, sP, s_len ); - } - return ret; + if (front_len) { + while (len && strncmp(s, front, front_len)==0) { + memcpy(s, s+front_len, (len-front_len)+1); + len -= front_len; + ret=True; + } + } + + if (back_len) { + while (strncmp(s+len-back_len,back,back_len)==0) { + s[len-back_len]='\0'; + len -= back_len; + ret=True; + } + } + return ret; } @@ -666,46 +319,11 @@ does a string have any uppercase chars in it? ****************************************************************************/ BOOL strhasupper(const char *s) { - while (*s) - { -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - if (is_shift_jis (*s)) - s += 2; - else if (is_kana (*s)) - s++; - else - { - if (isupper(*s)) - return(True); - s++; - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - size_t skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else { - if (isupper(*s)) - return(True); - s++; - } - } - } - return(False); + smb_ucs2_t *ptr; + push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + for(ptr=tmpbuf;*ptr;ptr++) + if(isupper_w(*ptr)) return True; + return(False); } /**************************************************************************** @@ -713,104 +331,23 @@ does a string have any lowercase chars in it? ****************************************************************************/ BOOL strhaslower(const char *s) { - while (*s) - { -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - if (is_shift_jis (*s)) - { - if (is_sj_upper (s[0], s[1])) - return(True); - if (is_sj_lower (s[0], s[1])) - return (True); - s += 2; - } - else if (is_kana (*s)) - { - s++; - } - else - { - if (islower(*s)) - return(True); - s++; - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - size_t skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else { - if (islower(*s)) - return(True); - s++; - } - } - } - return(False); + smb_ucs2_t *ptr; + push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + for(ptr=tmpbuf;*ptr;ptr++) + if(islower_w(*ptr)) return True; + return(False); } /**************************************************************************** -find the number of chars in a string +find the number of 'c' chars in a string ****************************************************************************/ size_t count_chars(const char *s,char c) { - size_t count=0; - -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - while (*s) - { - if (is_shift_jis (*s)) - s += 2; - else - { - if (*s == c) - count++; - s++; - } - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - while (*s) - { - size_t skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else { - if (*s == c) - count++; - s++; - } - } - } - return(count); + smb_ucs2_t *ptr; + int count; + push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + for(count=0,ptr=tmpbuf;*ptr;ptr++) if(*ptr==UCS2_CHAR(c)) count++; + return(count); } /******************************************************************* @@ -819,52 +356,15 @@ Return True if a string consists only of one particular character. BOOL str_is_all(const char *s,char c) { - if(s == NULL) - return False; - if(!*s) - return False; - -#if !defined(KANJI_WIN95_COMPATIBILITY) - /* - * For completeness we should put in equivalent code for code pages - * 949 (Korean hangul) and 950 (Big5 Traditional Chinese) here - but - * doubt anyone wants Samba to behave differently from Win95 and WinNT - * here. They both treat full width ascii characters as case senstive - * filenames (ie. they don't do the work we do here). - * JRA. - */ - - if(lp_client_code_page() == KANJI_CODEPAGE) - { - /* Win95 treats full width ascii characters as case sensitive. */ - while (*s) - { - if (is_shift_jis (*s)) - s += 2; - else - { - if (*s != c) - return False; - s++; - } - } - } - else -#endif /* KANJI_WIN95_COMPATIBILITY */ - { - while (*s) - { - size_t skip = get_character_len( *s ); - if( skip != 0 ) - s += skip; - else { - if (*s != c) - return False; - s++; - } - } - } - return True; + smb_ucs2_t *ptr; + + if(s == NULL) return False; + if(!*s) return False; + + push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + for(ptr=tmpbuf;*ptr;ptr++) if(*ptr!=UCS2_CHAR(c)) return False; + + return True; } /******************************************************************* @@ -874,29 +374,29 @@ include the terminating zero. char *safe_strcpy(char *dest,const char *src, size_t maxlength) { - size_t len; + size_t len; - if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); - return NULL; - } + if (!dest) { + DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); + return NULL; + } - if (!src) { - *dest = 0; - return dest; - } + if (!src) { + *dest = 0; + return dest; + } - len = strlen(src); + len = strlen(src); - if (len > maxlength) { - DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n", - (int)(len-maxlength), src)); - len = maxlength; - } + if (len > maxlength) { + DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n", + (int)(len-maxlength), src)); + len = maxlength; + } - memmove(dest, src, len); - dest[len] = 0; - return dest; + memmove(dest, src, len); + dest[len] = 0; + return dest; } /******************************************************************* @@ -906,29 +406,29 @@ include the terminating zero. char *safe_strcat(char *dest, const char *src, size_t maxlength) { - size_t src_len, dest_len; + size_t src_len, dest_len; - if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcat\n")); - return NULL; - } - - if (!src) { - return dest; - } - - src_len = strlen(src); - dest_len = strlen(dest); + if (!dest) { + DEBUG(0,("ERROR: NULL dest in safe_strcat\n")); + return NULL; + } - if (src_len + dest_len > maxlength) { - DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", - (int)(src_len + dest_len - maxlength), src)); - src_len = maxlength - dest_len; - } - - memcpy(&dest[dest_len], src, src_len); - dest[dest_len + src_len] = 0; - return dest; + if (!src) { + return dest; + } + + src_len = strlen(src); + dest_len = strlen(dest); + + if (src_len + dest_len > maxlength) { + DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", + (int)(src_len + dest_len - maxlength), src)); + src_len = maxlength - dest_len; + } + + memcpy(&dest[dest_len], src, src_len); + dest[dest_len + src_len] = 0; + return dest; } /******************************************************************* @@ -979,15 +479,15 @@ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, si char *StrnCpy(char *dest,const char *src,size_t n) { - char *d = dest; - if (!dest) return(NULL); - if (!src) { - *dest = 0; - return(dest); - } - while (n-- && (*d++ = *src++)) ; - *d = 0; - return(dest); + char *d = dest; + if (!dest) return(NULL); + if (!src) { + *dest = 0; + return(dest); + } + while (n-- && (*d++ = *src++)) ; + *d = 0; + return(dest); } /**************************************************************************** @@ -1303,3 +803,57 @@ char *string_truncate(char *s, int length) } return s; } + + +/**************************************************************************** +strchr and strrchr are very hard to do on general multi-byte strings. +we convert via ucs2 for now +****************************************************************************/ +char *strchr_m(const char *s, char c) +{ + wpstring ws; + pstring s2; + smb_ucs2_t *p; + + push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + p = strchr_wa(ws, c); + if (!p) return NULL; + *p = 0; + pull_ucs2_pstring(s2, ws); + return (char *)(s+strlen(s2)); +} + +char *strrchr_m(const char *s, char c) +{ + wpstring ws; + pstring s2; + smb_ucs2_t *p; + + push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + p = strrchr_wa(ws, c); + if (!p) return NULL; + *p = 0; + pull_ucs2_pstring(s2, ws); + return (char *)(s+strlen(s2)); +} + +/******************************************************************* + convert a string to lower case +********************************************************************/ +void strlower_m(char *s) +{ + /* I assume that lowercased string takes the same number of bytes + * as source string even in UTF-8 encoding. (VIV) */ + unix_strlower(s,strlen(s)+1,s,strlen(s)+1); +} + +/******************************************************************* + convert a string to upper case +********************************************************************/ +void strupper_m(char *s) +{ + /* I assume that lowercased string takes the same number of bytes + * as source string even in multibyte encoding. (VIV) */ + unix_strupper(s,strlen(s)+1,s,strlen(s)+1); +} + -- cgit From 527e824293ee934ca5da0ef5424efe5ab7757248 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 4 Jul 2001 07:36:09 +0000 Subject: strchr and strrchr are macros when compiling with optimisation in gcc, so we can't redefine them. damn. (This used to be commit c41fc06376d1a2b83690612304e85010b5e5f3cf) --- source3/lib/util_str.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8ff3e23443..736229c75f 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -43,13 +43,13 @@ BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize) if (!sep) sep = " \t\n\r"; /* find the first non sep char */ - while (*s && strchr(sep,*s)) s++; + while (*s && strchr_m(sep,*s)) s++; /* nothing left? */ if (! *s) return(False); /* copy over the token */ - for (quoted = False; len < bufsize && *s && (quoted || !strchr(sep,*s)); s++) { + for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { if (*s == '\"') { quoted = !quoted; } else { @@ -103,15 +103,15 @@ char **toktocliplist(int *ctok, char *sep) if (!sep) sep = " \t\n\r"; - while(*s && strchr(sep,*s)) s++; + while(*s && strchr_m(sep,*s)) s++; /* nothing left? */ if (!*s) return(NULL); do { ictok++; - while(*s && (!strchr(sep,*s))) s++; - while(*s && strchr(sep,*s)) *s++=0; + while(*s && (!strchr_m(sep,*s))) s++; + while(*s && strchr_m(sep,*s)) *s++=0; } while(*s); *ctok=ictok; @@ -461,7 +461,7 @@ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, si for(i = 0; i < len; i++) { int val = (src[i] & 0xff); - if(isupper(val) || islower(val) || isdigit(val) || strchr(other_safe_chars, val)) + if(isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val)) dest[i] = src[i]; else dest[i] = '_'; @@ -499,7 +499,7 @@ char *strncpyn(char *dest, const char *src,size_t n, char c) char *p; size_t str_len; - p = strchr(src, c); + p = strchr_m(src, c); if (p == NULL) { DEBUG(5, ("strncpyn: separator character (%c) not found\n", c)); @@ -539,14 +539,14 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) continue; } - if (!(p1 = strchr(hexchars, toupper(strhex[i])))) + if (!(p1 = strchr_m(hexchars, toupper(strhex[i])))) { break; } i++; /* next hex digit */ - if (!(p2 = strchr(hexchars, toupper(strhex[i])))) + if (!(p2 = strchr_m(hexchars, toupper(strhex[i])))) { break; } @@ -751,7 +751,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) ****************************************************************************/ void split_at_last_component(char *path, char *front, char sep, char *back) { - char *p = strrchr(path, sep); + char *p = strrchr_m(path, sep); if (p != NULL) { @@ -806,7 +806,7 @@ char *string_truncate(char *s, int length) /**************************************************************************** -strchr and strrchr are very hard to do on general multi-byte strings. +strchr and strrchr_m are very hard to do on general multi-byte strings. we convert via ucs2 for now ****************************************************************************/ char *strchr_m(const char *s, char c) -- cgit From 6bd8340781a2a0c7fee711d8448e421001beddde Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 6 Jul 2001 11:50:45 +0000 Subject: formatting fixes (This used to be commit 67ca6cd8eb20621d3f5926e19822b479ac80cb9c) --- source3/lib/util_str.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 736229c75f..565703a800 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -133,10 +133,10 @@ char **toktocliplist(int *ctok, char *sep) ********************************************************************/ int StrCaseCmp(const char *s, const char *t) { - pstring buf1, buf2; - unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1)); - unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2)); - return strcmp(buf1,buf2); + pstring buf1, buf2; + unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1)); + unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2)); + return strcmp(buf1,buf2); } /******************************************************************* @@ -155,10 +155,10 @@ int StrnCaseCmp(const char *s, const char *t, size_t n) ********************************************************************/ BOOL strequal(const char *s1, const char *s2) { - if (s1 == s2) return(True); - if (!s1 || !s2) return(False); + if (s1 == s2) return(True); + if (!s1 || !s2) return(False); - return(StrCaseCmp(s1,s2)==0); + return(StrCaseCmp(s1,s2)==0); } /******************************************************************* -- cgit From 484a7c0341fe033fe26fe1e6b597ed1c456c39d4 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 17 Sep 2001 02:19:44 +0000 Subject: move to SAFE_FREE() (This used to be commit 60e907b7e8e1c008463a88ed2b076344278986ef) --- source3/lib/util_str.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 565703a800..8518d75a49 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -632,8 +632,7 @@ void string_free(char **s) if (!s || !(*s)) return; if (*s == null_string) *s = NULL; - if (*s) free(*s); - *s = NULL; + SAFE_FREE(*s); } /**************************************************************************** -- cgit From dc1fc3ee8ec2199bc73bb5d7ec711c6800f61d65 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 2 Oct 2001 04:29:50 +0000 Subject: Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header. (This used to be commit 2d0922b0eabfdc0aaf1d0797482fef47ed7fde8e) --- source3/lib/util_str.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8518d75a49..0e7a7c02f5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -21,8 +21,6 @@ #include "includes.h" -extern int DEBUGLEVEL; - /**************************************************************************** Get the next token from a string, return False if none found handles double-quotes. @@ -855,4 +853,3 @@ void strupper_m(char *s) * as source string even in multibyte encoding. (VIV) */ unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } - -- cgit From 9bcd133e9e7b0cfe974f273fb23409d660af8358 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 3 Oct 2001 12:18:20 +0000 Subject: switched over to a new method of handling uppercase/lowercase mappings for unicode strings. The new method relies on 3 files that are mmap'd at startup to provide the mapping tables. The upcase.dat and lowcase.dat tables should be the same on all systems. The valid.dat table says what characters are valid in 8.3 names, and differs between systems. I'm committing the japanese valid.dat here, in future we need some way of automatically installing and choosing a appropriate table. This commit also adds my mini tdb based gettext replacement in intl/lang_tdb.c. I have not enabled this yet and have not removed the old gettext code as the new code is still being looked at by Monyo. Right now the code assumes that the upcase.dat, lowcase.dat and valid.dat files are installed in the Samba lib directory. That is not a good choice, but I'll leave them there until we work out the new install directory structure for Samba 3.0. simo - please look at the isvalid_w() function and think about using it in your new mangling code. That should be the final step to correctly passing the chargen test code from monyo. (This used to be commit 1c221994f118dd542a158b2db51e07d04d0e9314) --- source3/lib/util_str.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 0e7a7c02f5..3a77098e09 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -132,8 +132,8 @@ char **toktocliplist(int *ctok, char *sep) int StrCaseCmp(const char *s, const char *t) { pstring buf1, buf2; - unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1)); - unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2)); + unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1)); + unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2)); return strcmp(buf1,buf2); } @@ -142,10 +142,10 @@ int StrCaseCmp(const char *s, const char *t) ********************************************************************/ int StrnCaseCmp(const char *s, const char *t, size_t n) { - pstring buf1, buf2; - unix_strlower(s, strlen(s)+1, buf1, sizeof(buf1)); - unix_strlower(t, strlen(t)+1, buf2, sizeof(buf2)); - return strncmp(buf1,buf2,n); + pstring buf1, buf2; + unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1)); + unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2)); + return strncmp(buf1,buf2,n); } /******************************************************************* -- cgit From 6ab678d42b46eccee080de415985a8a1e3c29dc3 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Wed, 31 Oct 2001 06:22:19 +0000 Subject: Small 'const' updates ahead of some AuthRewrite merging. (This used to be commit 3b5e72bda3263c6bdf81dfface4fae4f06b71032) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 3a77098e09..e97885ae05 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -184,7 +184,7 @@ BOOL strcsequal(const char *s1,const char *s2) /*************************************************************************** Do a case-insensitive, whitespace-ignoring string compare. ***************************************************************************/ -int strwicmp(char *psz1, char *psz2) +int strwicmp(const char *psz1, const char *psz2) { /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */ /* appropriate value. */ -- cgit From 740d6f5dd60bef72037ed5fcd7b2192af22c2e41 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 4 Nov 2001 18:26:53 +0000 Subject: a big one: - old mangle code has gone, the new one based on tdb seem resonably ok probably the valid.dat table need to be updated to treat wild chars as invalid ones (work ok without it) - a LOT of new string manipulation function for unicode, they are somewhat tested but a review would not be bad - some new function I will need for the new unix_convert function I'm writing, this will be renamed filename_convert and use only unicode strings. - charconv, I attached a comment, if someone wnat to look if I'm right or just was hacking to late in the night to make a sane one :) of course any bug is my responsibility an will be pleased to see patches if you find any. :-) Simo. (This used to be commit ee19f7efb6ea9216fc91cf112ac1afa691983e9d) --- source3/lib/util_str.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e97885ae05..dc9dbd8ed7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -245,9 +245,7 @@ void string_replace(char *s,char oldc,char newc) { smb_ucs2_t *ptr; push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(ptr=tmpbuf;*ptr;ptr++) { - if(*ptr==UCS2_CHAR(oldc)) *ptr = UCS2_CHAR(newc); - } + string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); } @@ -743,6 +741,65 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) } } +/**************************************************************************** +similar to all_string_sub but for unicode strings. +return a new allocate unicode string. +len is the number of bytes, not chars + similar to string_sub() but allows for any character to be substituted. + Use with caution! + if len==0 then no length check is performed +****************************************************************************/ + +smb_ucs2_t *all_string_sub_w(smb_ucs2_t *s, const smb_ucs2_t *pattern, + const smb_ucs2_t *insert) +{ + smb_ucs2_t *r, *rp, *sp; + size_t ls, lp, li, lt; + + if (!insert || !pattern || !*pattern || !s) return NULL; + + ls = lt = (size_t)strlen_w(s) * sizeof(smb_ucs2_t); + lp = (size_t)strlen_w(pattern) * sizeof(smb_ucs2_t); + li = (size_t)strlen_w(insert) * sizeof(smb_ucs2_t); + + if (li > lp) { + smb_ucs2_t *st = s; + int ld = li - lp; + while (sp = strstr_w(st, pattern)) { + st = sp + lp; + lt += ld; + } + } + + r = rp = (smb_ucs2_t *)malloc((lt + 1)*(sizeof(smb_ucs2_t))); + if (!r) { + DEBUG(0, ("all_string_sub_w: out of memory!\n")); + return NULL; + } + + while (sp = strstr_w(s, pattern)) { + memcpy(rp, s, sp - s); + rp += (sp - s); + memcpy(rp, insert, li); + s = sp + lp; + rp += li; + } + *rp = 0; + + return r; +} + +smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, + const char *insert) +{ + wpstring p, i; + + if (!insert || !pattern || !s) return NULL; + push_ucs2(NULL, p, pattern, sizeof(wpstring) - 1, STR_TERMINATE); + push_ucs2(NULL, i, insert, sizeof(wpstring) - 1, STR_TERMINATE); + return all_string_sub_w(s, p, i); +} + /**************************************************************************** splits out the front and back at a separator. ****************************************************************************/ @@ -813,7 +870,7 @@ char *strchr_m(const char *s, char c) smb_ucs2_t *p; push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); - p = strchr_wa(ws, c); + p = strchr_w(ws, UCS2_CHAR(c)); if (!p) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); @@ -827,7 +884,7 @@ char *strrchr_m(const char *s, char c) smb_ucs2_t *p; push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); - p = strrchr_wa(ws, c); + p = strrchr_w(ws, UCS2_CHAR(c)); if (!p) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); -- cgit From 31b91bfe4ef65630429445d9c84c1a853e47ee7c Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 14 Nov 2001 01:38:01 +0000 Subject: Removed unused variable. (This used to be commit c8c7da237dc17d9b785bc15b9b569caeeb6d283e) --- source3/lib/util_str.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index dc9dbd8ed7..4c17d1f08b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -243,7 +243,6 @@ BOOL strisnormal(char *s) ****************************************************************************/ void string_replace(char *s,char oldc,char newc) { - smb_ucs2_t *ptr; push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); -- cgit From 60906f657cb070929e96b4c81b8e24bae3a4ed61 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 18 Nov 2001 16:12:11 +0000 Subject: fixed some bugs. (This used to be commit 37edaeddce09193450b18b1b85aa41960cb39741) --- source3/lib/util_str.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 4c17d1f08b..d7e6fa0781 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -753,18 +753,18 @@ smb_ucs2_t *all_string_sub_w(smb_ucs2_t *s, const smb_ucs2_t *pattern, const smb_ucs2_t *insert) { smb_ucs2_t *r, *rp, *sp; - size_t ls, lp, li, lt; + size_t lr, lp, li, lt; if (!insert || !pattern || !*pattern || !s) return NULL; - ls = lt = (size_t)strlen_w(s) * sizeof(smb_ucs2_t); - lp = (size_t)strlen_w(pattern) * sizeof(smb_ucs2_t); - li = (size_t)strlen_w(insert) * sizeof(smb_ucs2_t); + lt = (size_t)strlen_w(s); + lp = (size_t)strlen_w(pattern); + li = (size_t)strlen_w(insert); if (li > lp) { smb_ucs2_t *st = s; int ld = li - lp; - while (sp = strstr_w(st, pattern)) { + while ((sp = strstr_w(st, pattern))) { st = sp + lp; lt += ld; } @@ -776,13 +776,18 @@ smb_ucs2_t *all_string_sub_w(smb_ucs2_t *s, const smb_ucs2_t *pattern, return NULL; } - while (sp = strstr_w(s, pattern)) { - memcpy(rp, s, sp - s); - rp += (sp - s); - memcpy(rp, insert, li); + while ((sp = strstr_w(s, pattern))) { + memcpy(rp, s, (sp - s)); + rp += ((sp - s) / sizeof(smb_ucs2_t)); + memcpy(rp, insert, (li * sizeof(smb_ucs2_t))); s = sp + lp; rp += li; } + lr = ((rp - r) / sizeof(smb_ucs2_t)); + if (lr < lt) { + memcpy(rp, s, ((lt - lr) * sizeof(smb_ucs2_t))); + rp += (lt - lr); + } *rp = 0; return r; -- cgit From e051c2c430f706835f250b10cc63e5621054b5ec Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 10 Dec 2001 00:39:01 +0000 Subject: make sid_binstring available without HAVE_ADS (This used to be commit 4a6d29768665f71b72cf48ee34ee9a9c451232f6) --- source3/lib/util_str.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d7e6fa0781..2205f5cf0d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -914,3 +914,26 @@ void strupper_m(char *s) * as source string even in multibyte encoding. (VIV) */ unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } + +/* + return a RFC2254 binary string representation of a buffer + used in LDAP filters + caller must free +*/ +char *binary_string(char *buf, int len) +{ + char *s; + int i, j; + const char *hex = "0123456789ABCDEF"; + s = malloc(len * 3 + 1); + if (!s) return NULL; + for (j=i=0;i> 4]; + s[j+2] = hex[((unsigned char)buf[i]) & 0xF]; + j += 3; + } + s[j] = 0; + return s; +} + -- cgit From 93d458c5f68a7168ce543e820906bc55a5d3a339 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 20 Dec 2001 10:02:30 +0000 Subject: fixed warnings on irix and crash bug on big endian machines (This used to be commit cc6c263993eaf0715f231fc80ca7e6e65694548b) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2205f5cf0d..14d50384c2 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -198,9 +198,9 @@ int strwicmp(const char *psz1, const char *psz2) /* sync the strings on first non-whitespace */ while (1) { - while (isspace(*psz1)) + while (isspace((int)*psz1)) psz1++; - while (isspace(*psz2)) + while (isspace((int)*psz2)) psz2++; if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0' || *psz2 == '\0') -- cgit From df3d5b3556146e2d60c1a5656b16236f96832a16 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Tue, 15 Jan 2002 01:37:12 +0000 Subject: Add constness to filenames passed to functions. (This used to be commit 8d106dc1f4a51112516d72ae68747ca6b5b904b7) --- source3/lib/util_str.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 14d50384c2..b1d50ad911 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -937,3 +937,28 @@ char *binary_string(char *buf, int len) return s; } + +/* Just a typesafety wrapper for snprintf into a pstring */ +int pstr_sprintf(pstring s, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vsnprintf(PSTR_MUTABLE(s), PSTRING_LEN, fmt, ap); + va_end(ap); + return ret; +} + + +/* Just a typesafety wrapper for snprintf into a pstring */ +int fstr_sprintf(fstring s, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vsnprintf(FSTR_MUTABLE(s), FSTRING_LEN, fmt, ap); + va_end(ap); + return ret; +} -- cgit From fed604bfa368a2bb1fed414e368d491e4c7d7005 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Wed, 16 Jan 2002 02:42:07 +0000 Subject: Roll back PSTRING_SANCTIFY patch; just leave non-controversial type and constness changes. (This used to be commit cee0ec72746122c962e6c5278a736266a7f2c424) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b1d50ad911..dbfaf179a1 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -945,7 +945,7 @@ int pstr_sprintf(pstring s, const char *fmt, ...) int ret; va_start(ap, fmt); - ret = vsnprintf(PSTR_MUTABLE(s), PSTRING_LEN, fmt, ap); + ret = vsnprintf(s, PSTRING_LEN, fmt, ap); va_end(ap); return ret; } @@ -958,7 +958,7 @@ int fstr_sprintf(fstring s, const char *fmt, ...) int ret; va_start(ap, fmt); - ret = vsnprintf(FSTR_MUTABLE(s), FSTRING_LEN, fmt, ap); + ret = vsnprintf(s, FSTRING_LEN, fmt, ap); va_end(ap); return ret; } -- cgit From 114eaabdcbacf626ccb452a2d4f695b183dd738b Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Fri, 25 Jan 2002 00:35:14 +0000 Subject: minor fixes (This used to be commit 04f492980b73800b60dde764fdeb43f2eab79624) --- source3/lib/util_str.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index dbfaf179a1..a7eeedea3d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -3,6 +3,7 @@ Version 3.0 Samba utility functions Copyright (C) Andrew Tridgell 1992-2001 + Copyright (C) Simo Sorce 2001-2002 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -749,8 +750,8 @@ len is the number of bytes, not chars if len==0 then no length check is performed ****************************************************************************/ -smb_ucs2_t *all_string_sub_w(smb_ucs2_t *s, const smb_ucs2_t *pattern, - const smb_ucs2_t *insert) +smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, + const smb_ucs2_t *insert) { smb_ucs2_t *r, *rp, *sp; size_t lr, lp, li, lt; @@ -951,7 +952,7 @@ int pstr_sprintf(pstring s, const char *fmt, ...) } -/* Just a typesafety wrapper for snprintf into a pstring */ +/* Just a typesafety wrapper for snprintf into a fstring */ int fstr_sprintf(fstring s, const char *fmt, ...) { va_list ap; -- cgit From cd68afe31256ad60748b34f7318a180cfc2127cc Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 30 Jan 2002 06:08:46 +0000 Subject: Removed version number from file header. Changed "SMB/Netbios" to "SMB/CIFS" in file header. (This used to be commit 6a58c9bd06d0d7502a24bf5ce5a2faf0a146edfa) --- source3/lib/util_str.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index a7eeedea3d..200f4ce696 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1,6 +1,5 @@ /* - Unix SMB/Netbios implementation. - Version 3.0 + Unix SMB/CIFS implementation. Samba utility functions Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Simo Sorce 2001-2002 -- cgit From 714518e550f3c6ee36fa6e3e2447b943898a2ac5 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 11 Apr 2002 09:56:38 +0000 Subject: this adds a completely new hash based mangling scheme the hash for this scheme is *much* larger (approximately 31 bits) and the code is written to be very fast, correctly handling multibyte while not doing any actual multi-byte conversions in the vast majority of cases you can select this scheme using "mangling method = hash2", although I may make it the default if it works out well. (This used to be commit bb173c1a7e2408ced967ebac40b5e3f852ccd3a1) --- source3/lib/util_str.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 200f4ce696..e3dd3124a5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -900,6 +900,16 @@ char *strrchr_m(const char *s, char c) ********************************************************************/ void strlower_m(char *s) { + /* this is quite a common operation, so we want it to be + fast. We optimise for the ascii case, knowing that all our + supported multi-byte character sets are ascii-compatible + (ie. they match for the first 128 chars) */ + while (*s && !(((unsigned char)s[0]) & 0x7F)) { + *s++ = tolower((unsigned char)*s); + } + + if (!*s) return; + /* I assume that lowercased string takes the same number of bytes * as source string even in UTF-8 encoding. (VIV) */ unix_strlower(s,strlen(s)+1,s,strlen(s)+1); @@ -910,6 +920,16 @@ void strlower_m(char *s) ********************************************************************/ void strupper_m(char *s) { + /* this is quite a common operation, so we want it to be + fast. We optimise for the ascii case, knowing that all our + supported multi-byte character sets are ascii-compatible + (ie. they match for the first 128 chars) */ + while (*s && !(((unsigned char)s[0]) & 0x7F)) { + *s++ = toupper((unsigned char)*s); + } + + if (!*s) return; + /* I assume that lowercased string takes the same number of bytes * as source string even in multibyte encoding. (VIV) */ unix_strupper(s,strlen(s)+1,s,strlen(s)+1); -- cgit From 5928c293ff5d0b864172b559cb82d1aa089d9a4a Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 11 Apr 2002 15:27:22 +0000 Subject: added strndup() for systems that don't have it (This used to be commit 7e92fb7453e4dbf1fe0c32c3dcc1e994cb95b5ea) --- source3/lib/util_str.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e3dd3124a5..6fdca658cd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -982,3 +982,22 @@ int fstr_sprintf(fstring s, const char *fmt, ...) va_end(ap); return ret; } + + +#ifndef HAVE_STRNDUP +/******************************************************************* +some platforms don't have strndup +********************************************************************/ + char *strndup(const char *s, size_t n) +{ + char *ret; + int i; + for (i=0;s[i] && i Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/util_str.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 314 insertions(+), 18 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6fdca658cd..88a72f1536 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -650,23 +650,27 @@ This routine looks for pattern in s and replaces it with insert. It may do multiple replacements. any of " ; ' $ or ` in the insert string are replaced with _ -if len==0 then no length check is performed +if len==0 then the string cannot be extended. This is different from the old +use of len==0 which was for no length checks to be done. ****************************************************************************/ -void string_sub(char *s,const char *pattern,const char *insert, size_t len) + +void string_sub(char *s,const char *pattern, const char *insert, size_t len) { char *p; ssize_t ls,lp,li, i; - if (!insert || !pattern || !s) return; + if (!insert || !pattern || !*pattern || !s) + return; ls = (ssize_t)strlen(s); lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); - if (!*pattern) return; - + if (len == 0) + len = ls; + while (lp <= ls && (p = strstr(s,pattern))) { - if (len && (ls + (li-lp) >= len)) { + if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), pattern, (int)len)); @@ -706,26 +710,98 @@ void pstring_sub(char *s,const char *pattern,const char *insert) string_sub(s, pattern, insert, sizeof(pstring)); } +/* similar to string_sub, but it will accept only allocated strings + * and may realloc them so pay attention at what you pass on no + * pointers inside strings, no pstrings or const must be passed + * as string. + */ + +char *realloc_string_sub(char *string, const char *pattern, const char *insert) +{ + char *p, *in; + char *s; + ssize_t ls,lp,li,ld, i; + + if (!insert || !pattern || !*pattern || !string || !*string) + return NULL; + + s = string; + + in = strdup(insert); + if (!in) { + DEBUG(0, ("realloc_string_sub: out of memory!\n")); + return NULL; + } + ls = (ssize_t)strlen(s); + lp = (ssize_t)strlen(pattern); + li = (ssize_t)strlen(insert); + ld = li - lp; + for (i=0;i 0) { + char *t = Realloc(string, ls + ld + 1); + if (!t) { + DEBUG(0, ("realloc_string_sub: out of memory!\n")); + SAFE_FREE(in); + return NULL; + } + string = t; + p = t + (p - s); + } + if (li != lp) { + memmove(p+li,p+lp,strlen(p+lp)+1); + } + memcpy(p, in, li); + s = p + li; + ls += ld; + } + SAFE_FREE(in); + return string; +} + /**************************************************************************** similar to string_sub() but allows for any character to be substituted. Use with caution! -if len==0 then no length check is performed +if len==0 then the string cannot be extended. This is different from the old +use of len==0 which was for no length checks to be done. ****************************************************************************/ + void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) { char *p; ssize_t ls,lp,li; - if (!insert || !pattern || !s) return; + if (!insert || !pattern || !s) + return; ls = (ssize_t)strlen(s); lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); - if (!*pattern) return; + if (!*pattern) + return; + + if (len == 0) + len = ls; while (lp <= ls && (p = strstr(s,pattern))) { - if (len && (ls + (li-lp) >= len)) { + if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), pattern, (int)len)); @@ -743,10 +819,8 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) /**************************************************************************** similar to all_string_sub but for unicode strings. return a new allocate unicode string. -len is the number of bytes, not chars similar to string_sub() but allows for any character to be substituted. Use with caution! - if len==0 then no length check is performed ****************************************************************************/ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, @@ -915,6 +989,20 @@ void strlower_m(char *s) unix_strlower(s,strlen(s)+1,s,strlen(s)+1); } +/******************************************************************* + duplicate convert a string to lower case +********************************************************************/ +char *strdup_lower(const char *s) +{ + char *t = strdup(s); + if (t == NULL) { + DEBUG(0, ("strdup_lower: Out of memory!\n")); + return NULL; + } + strlower_m(t); + return t; +} + /******************************************************************* convert a string to upper case ********************************************************************/ @@ -935,6 +1023,20 @@ void strupper_m(char *s) unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } +/******************************************************************* + convert a string to upper case +********************************************************************/ +char *strdup_upper(const char *s) +{ + char *t = strdup(s); + if (t == NULL) { + DEBUG(0, ("strdup_upper: Out of memory!\n")); + return NULL; + } + strupper_m(t); + return t; +} + /* return a RFC2254 binary string representation of a buffer used in LDAP filters @@ -991,13 +1093,207 @@ some platforms don't have strndup char *strndup(const char *s, size_t n) { char *ret; - int i; - for (i=0;s[i] && i Date: Sat, 17 Aug 2002 17:00:51 +0000 Subject: sync 3.0 branch with head (This used to be commit 3928578b52cfc949be5e0ef444fce1558d75f290) --- source3/lib/util_str.c | 165 ++++++++++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 72 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 88a72f1536..19d92eec8f 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -212,6 +212,18 @@ int strwicmp(const char *psz1, const char *psz2) } +/* Convert a string to upper case, but don't modify it */ + +char *strupper_static(const char *s) +{ + static pstring str; + + pstrcpy(str, s); + strupper(str); + + return str; +} + /******************************************************************* convert a string to "normal" form ********************************************************************/ @@ -299,7 +311,7 @@ BOOL trim_string(char *s,const char *front,const char *back) } if (back_len) { - while (strncmp(s+len-back_len,back,back_len)==0) { + while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) { s[len-back_len]='\0'; len -= back_len; ret=True; @@ -667,7 +679,7 @@ void string_sub(char *s,const char *pattern, const char *insert, size_t len) li = (ssize_t)strlen(insert); if (len == 0) - len = ls; + len = ls + 1; /* len is number of *bytes* */ while (lp <= ls && (p = strstr(s,pattern))) { if (ls + (li-lp) >= len) { @@ -798,7 +810,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) return; if (len == 0) - len = ls; + len = ls + 1; /* len is number of *bytes* */ while (lp <= ls && (p = strstr(s,pattern))) { if (ls + (li-lp) >= len) { @@ -826,7 +838,8 @@ return a new allocate unicode string. smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, const smb_ucs2_t *insert) { - smb_ucs2_t *r, *rp, *sp; + smb_ucs2_t *r, *rp; + const smb_ucs2_t *sp; size_t lr, lp, li, lt; if (!insert || !pattern || !*pattern || !s) return NULL; @@ -836,7 +849,7 @@ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, li = (size_t)strlen_w(insert); if (li > lp) { - smb_ucs2_t *st = s; + const smb_ucs2_t *st = s; int ld = li - lp; while ((sp = strstr_w(st, pattern))) { st = sp + lp; @@ -879,68 +892,59 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, } /**************************************************************************** - splits out the front and back at a separator. + Splits out the front and back at a separator. ****************************************************************************/ + void split_at_last_component(char *path, char *front, char sep, char *back) { char *p = strrchr_m(path, sep); if (p != NULL) - { *p = 0; - } + if (front != NULL) - { pstrcpy(front, path); - } - if (p != NULL) - { + + if (p != NULL) { if (back != NULL) - { pstrcpy(back, p+1); - } *p = '\\'; - } - else - { + } else { if (back != NULL) - { back[0] = 0; - } } } - /**************************************************************************** -write an octal as a string + Write an octal as a string. ****************************************************************************/ + char *octal_string(int i) { static char ret[64]; - if (i == -1) { + if (i == -1) return "-1"; - } slprintf(ret, sizeof(ret)-1, "0%o", i); return ret; } /**************************************************************************** -truncate a string at a specified length + Truncate a string at a specified length. ****************************************************************************/ + char *string_truncate(char *s, int length) { - if (s && strlen(s) > length) { + if (s && strlen(s) > length) s[length] = 0; - } return s; } - /**************************************************************************** -strchr and strrchr_m are very hard to do on general multi-byte strings. -we convert via ucs2 for now + Strchr and strrchr_m are very hard to do on general multi-byte strings. + We convert via ucs2 for now. ****************************************************************************/ + char *strchr_m(const char *s, char c) { wpstring ws; @@ -949,7 +953,8 @@ char *strchr_m(const char *s, char c) push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); p = strchr_w(ws, UCS2_CHAR(c)); - if (!p) return NULL; + if (!p) + return NULL; *p = 0; pull_ucs2_pstring(s2, ws); return (char *)(s+strlen(s2)); @@ -963,26 +968,29 @@ char *strrchr_m(const char *s, char c) push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); p = strrchr_w(ws, UCS2_CHAR(c)); - if (!p) return NULL; + if (!p) + return NULL; *p = 0; pull_ucs2_pstring(s2, ws); return (char *)(s+strlen(s2)); } /******************************************************************* - convert a string to lower case + Convert a string to lower case. ********************************************************************/ + void strlower_m(char *s) { /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) { + + while (*s && !(((unsigned char)s[0]) & 0x7F)) *s++ = tolower((unsigned char)*s); - } - if (!*s) return; + if (!*s) + return; /* I assume that lowercased string takes the same number of bytes * as source string even in UTF-8 encoding. (VIV) */ @@ -990,8 +998,9 @@ void strlower_m(char *s) } /******************************************************************* - duplicate convert a string to lower case + Duplicate convert a string to lower case. ********************************************************************/ + char *strdup_lower(const char *s) { char *t = strdup(s); @@ -1004,19 +1013,21 @@ char *strdup_lower(const char *s) } /******************************************************************* - convert a string to upper case + Convert a string to upper case. ********************************************************************/ + void strupper_m(char *s) { /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) { + + while (*s && !(((unsigned char)s[0]) & 0x7F)) *s++ = toupper((unsigned char)*s); - } - if (!*s) return; + if (!*s) + return; /* I assume that lowercased string takes the same number of bytes * as source string even in multibyte encoding. (VIV) */ @@ -1024,8 +1035,9 @@ void strupper_m(char *s) } /******************************************************************* - convert a string to upper case + Convert a string to upper case. ********************************************************************/ + char *strdup_upper(const char *s) { char *t = strdup(s); @@ -1048,7 +1060,8 @@ char *binary_string(char *buf, int len) int i, j; const char *hex = "0123456789ABCDEF"; s = malloc(len * 3 + 1); - if (!s) return NULL; + if (!s) + return NULL; for (j=i=0;i> 4]; @@ -1059,8 +1072,8 @@ char *binary_string(char *buf, int len) return s; } - /* Just a typesafety wrapper for snprintf into a pstring */ + int pstr_sprintf(pstring s, const char *fmt, ...) { va_list ap; @@ -1072,8 +1085,8 @@ int pstr_sprintf(pstring s, const char *fmt, ...) return ret; } - /* Just a typesafety wrapper for snprintf into a fstring */ + int fstr_sprintf(fstring s, const char *fmt, ...) { va_list ap; @@ -1085,18 +1098,19 @@ int fstr_sprintf(fstring s, const char *fmt, ...) return ret; } - #ifndef HAVE_STRNDUP /******************************************************************* -some platforms don't have strndup + Some platforms don't have strndup. ********************************************************************/ + char *strndup(const char *s, size_t n) { char *ret; n = strnlen(s, n); ret = malloc(n+1); - if (!ret) return NULL; + if (!ret) + return NULL; memcpy(ret, s, n); ret[n] = 0; @@ -1111,39 +1125,39 @@ some platforms don't have strnlen size_t strnlen(const char *s, size_t n) { int i; - for (i=0; s[i] && i Date: Tue, 1 Oct 2002 13:10:57 +0000 Subject: Updates from Samba HEAD: - Fix segfaults in the 'net ads' commands when no password is provided - Readd --with-ldapsam for 2.2 compatability. This conditionally compiles the old options, but the actual code is available on all ldap systems. - Fix shadow passwords (as per work with vl) - Fix sending plaintext passwords to unicode servers (again vl) - Add a bit of const to secrets.c functions - Fix some spelling and grammer by vance. - Document the -r option in smbgroupedit. There are more changes in HEAD, I'm only merging the changes I've been involved with. Andrew Bartlett (This used to be commit 83973c389355a5cc9ca74af467dfd8b5dabd2c8f) --- source3/lib/util_str.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 19d92eec8f..1b38db2c94 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -643,8 +643,8 @@ void string_free(char **s) } /**************************************************************************** -set a string value, allocing the space for the string, and deallocating any -existing space +set a string value, deallocating any existing space, and allocing the space +for the string ****************************************************************************/ BOOL string_set(char **dest,const char *src) { @@ -724,7 +724,7 @@ void pstring_sub(char *s,const char *pattern,const char *insert) /* similar to string_sub, but it will accept only allocated strings * and may realloc them so pay attention at what you pass on no - * pointers inside strings, no pstrings or const must be passed + * pointers inside strings, no pstrings or const may be passed * as string. */ @@ -830,7 +830,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) /**************************************************************************** similar to all_string_sub but for unicode strings. -return a new allocate unicode string. +return a new allocated unicode string. similar to string_sub() but allows for any character to be substituted. Use with caution! ****************************************************************************/ @@ -1203,7 +1203,7 @@ BOOL str_list_copy(char ***dest, char **src) lsize += S_LIST_ABS; rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1))); if (!rlist) { - DEBUG(0,("str_list_copy: Unable to allocate memory")); + DEBUG(0,("str_list_copy: Unable to re-allocate memory")); str_list_free(&list); return False; } @@ -1225,7 +1225,7 @@ BOOL str_list_copy(char ***dest, char **src) return True; } -/* return true if all the elemnts of the list matches exactly */ +/* return true if all the elements of the list match exactly */ BOOL str_list_compare(char **list1, char **list2) { -- cgit From f2d1f19a66ebaf9b88d23c0faa2412536cc74cda Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 1 Oct 2002 18:26:00 +0000 Subject: syncing up with HEAD. Seems to be a lot of differences creeping in (i ignored the new SAMBA stuff, but the rest of this looks like it should have been merged already). (This used to be commit 3de09e5cf1f667e410ee8b9516a956860ce7290f) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1b38db2c94..75338de4d3 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -468,7 +468,7 @@ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, si for(i = 0; i < len; i++) { int val = (src[i] & 0xff); - if(isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val)) + if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val)) dest[i] = src[i]; else dest[i] = '_'; @@ -501,7 +501,7 @@ char *StrnCpy(char *dest,const char *src,size_t n) like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). ****************************************************************************/ -char *strncpyn(char *dest, const char *src,size_t n, char c) +char *strncpyn(char *dest, const char *src, size_t n, char c) { char *p; size_t str_len; -- cgit From a0cddf2493d20fd049a5a50540f37e2becf8078c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 2 Oct 2002 19:12:14 +0000 Subject: Added const. Anal formatting fixup. Jeremy. (This used to be commit 66531969dfe0935be2c9c4d89f5bba80d862a52f) --- source3/lib/util_str.c | 397 +++++++++++++++++++++++++++---------------------- 1 file changed, 222 insertions(+), 175 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 75338de4d3..6d1f8fe023 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -22,29 +22,34 @@ #include "includes.h" /**************************************************************************** - Get the next token from a string, return False if none found - handles double-quotes. -Based on a routine by GJC@VILLAGE.COM. -Extensively modified by Andrew.Tridgell@anu.edu.au + Get the next token from a string, return False if none found. + Handles double-quotes. + Based on a routine by GJC@VILLAGE.COM. + Extensively modified by Andrew.Tridgell@anu.edu.au ****************************************************************************/ -BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize) + +BOOL next_token(char **ptr,char *buff, const char *sep, size_t bufsize) { char *s; BOOL quoted; size_t len=1; - if (!ptr) return(False); + if (!ptr) + return(False); s = *ptr; /* default to simple separators */ - if (!sep) sep = " \t\n\r"; + if (!sep) + sep = " \t\n\r"; /* find the first non sep char */ - while (*s && strchr_m(sep,*s)) s++; + while (*s && strchr_m(sep,*s)) + s++; /* nothing left? */ - if (! *s) return(False); + if (! *s) + return(False); /* copy over the token */ for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { @@ -62,19 +67,19 @@ BOOL next_token(char **ptr,char *buff,char *sep, size_t bufsize) return(True); } - - /**************************************************************************** This is like next_token but is not re-entrant and "remembers" the first parameter so you can pass NULL. This is useful for user interface code but beware the fact that it is not re-entrant! ****************************************************************************/ + static char *last_ptr=NULL; -BOOL next_token_nr(char **ptr,char *buff,char *sep, size_t bufsize) +BOOL next_token_nr(char **ptr,char *buff, const char *sep, size_t bufsize) { BOOL ret; - if (!ptr) ptr = &last_ptr; + if (!ptr) + ptr = &last_ptr; ret = next_token(ptr, buff, sep, bufsize); last_ptr = *ptr; @@ -88,47 +93,56 @@ void set_first_token(char *ptr) last_ptr = ptr; } - /**************************************************************************** -Convert list of tokens to array; dependent on above routine. -Uses last_ptr from above - bit of a hack. + Convert list of tokens to array; dependent on above routine. + Uses last_ptr from above - bit of a hack. ****************************************************************************/ -char **toktocliplist(int *ctok, char *sep) + +char **toktocliplist(int *ctok, const char *sep) { char *s=last_ptr; int ictok=0; char **ret, **iret; - if (!sep) sep = " \t\n\r"; + if (!sep) + sep = " \t\n\r"; - while(*s && strchr_m(sep,*s)) s++; + while(*s && strchr_m(sep,*s)) + s++; /* nothing left? */ - if (!*s) return(NULL); + if (!*s) + return(NULL); do { ictok++; - while(*s && (!strchr_m(sep,*s))) s++; - while(*s && strchr_m(sep,*s)) *s++=0; + while(*s && (!strchr_m(sep,*s))) + s++; + while(*s && strchr_m(sep,*s)) + *s++=0; } while(*s); *ctok=ictok; s=last_ptr; - if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL; + if (!(ret=iret=malloc(ictok*sizeof(char *)))) + return NULL; while(ictok--) { *iret++=s; - while(*s++); - while(!*s) s++; + while(*s++) + ; + while(!*s) + s++; } return ret; } /******************************************************************* - case insensitive string compararison + Case insensitive string compararison. ********************************************************************/ + int StrCaseCmp(const char *s, const char *t) { pstring buf1, buf2; @@ -138,8 +152,9 @@ int StrCaseCmp(const char *s, const char *t) } /******************************************************************* - case insensitive string compararison, length limited + Case insensitive string compararison, length limited. ********************************************************************/ + int StrnCaseCmp(const char *s, const char *t, size_t n) { pstring buf1, buf2; @@ -149,34 +164,43 @@ int StrnCaseCmp(const char *s, const char *t, size_t n) } /******************************************************************* - compare 2 strings + Compare 2 strings. ********************************************************************/ + BOOL strequal(const char *s1, const char *s2) { - if (s1 == s2) return(True); - if (!s1 || !s2) return(False); + if (s1 == s2) + return(True); + if (!s1 || !s2) + return(False); return(StrCaseCmp(s1,s2)==0); } /******************************************************************* - compare 2 strings up to and including the nth char. - ******************************************************************/ + Compare 2 strings up to and including the nth char. +******************************************************************/ + BOOL strnequal(const char *s1,const char *s2,size_t n) { - if (s1 == s2) return(True); - if (!s1 || !s2 || !n) return(False); + if (s1 == s2) + return(True); + if (!s1 || !s2 || !n) + return(False); return(StrnCaseCmp(s1,s2,n)==0); } /******************************************************************* - compare 2 strings (case sensitive) + Compare 2 strings (case sensitive). ********************************************************************/ + BOOL strcsequal(const char *s1,const char *s2) { - if (s1 == s2) return(True); - if (!s1 || !s2) return(False); + if (s1 == s2) + return(True); + if (!s1 || !s2) + return(False); return(strcmp(s1,s2)==0); } @@ -184,6 +208,7 @@ BOOL strcsequal(const char *s1,const char *s2) /*************************************************************************** Do a case-insensitive, whitespace-ignoring string compare. ***************************************************************************/ + int strwicmp(const char *psz1, const char *psz2) { /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */ @@ -196,8 +221,7 @@ int strwicmp(const char *psz1, const char *psz2) return (1); /* sync the strings on first non-whitespace */ - while (1) - { + while (1) { while (isspace((int)*psz1)) psz1++; while (isspace((int)*psz2)) @@ -212,7 +236,9 @@ int strwicmp(const char *psz1, const char *psz2) } -/* Convert a string to upper case, but don't modify it */ +/******************************************************************* + Convert a string to upper case, but don't modify it. +********************************************************************/ char *strupper_static(const char *s) { @@ -225,21 +251,23 @@ char *strupper_static(const char *s) } /******************************************************************* - convert a string to "normal" form + Convert a string to "normal" form. ********************************************************************/ + void strnorm(char *s) { - extern int case_default; - if (case_default == CASE_UPPER) - strupper(s); - else - strlower(s); + extern int case_default; + if (case_default == CASE_UPPER) + strupper(s); + else + strlower(s); } /******************************************************************* -check if a string is in "normal" case + Check if a string is in "normal" case. ********************************************************************/ -BOOL strisnormal(char *s) + +BOOL strisnormal(const char *s) { extern int case_default; if (case_default == CASE_UPPER) @@ -250,9 +278,10 @@ BOOL strisnormal(char *s) /**************************************************************************** - string replace - NOTE: oldc and newc must be 7 bit characters + String replace. + NOTE: oldc and newc must be 7 bit characters ****************************************************************************/ + void string_replace(char *s,char oldc,char newc) { push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); @@ -260,10 +289,10 @@ void string_replace(char *s,char oldc,char newc) pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); } - /******************************************************************* -skip past some strings in a buffer + Skip past some strings in a buffer. ********************************************************************/ + char *skip_string(char *buf,size_t n) { while (n--) @@ -276,6 +305,7 @@ char *skip_string(char *buf,size_t n) be the same as the number of bytes in a string for single byte strings, but will be different for multibyte. ********************************************************************/ + size_t str_charnum(const char *s) { push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); @@ -283,7 +313,7 @@ size_t str_charnum(const char *s) } /******************************************************************* -trim the specified elements off the front and back of a string + Trim the specified elements off the front and back of a string. ********************************************************************/ BOOL trim_string(char *s,const char *front,const char *back) @@ -320,40 +350,46 @@ BOOL trim_string(char *s,const char *front,const char *back) return ret; } - /**************************************************************************** -does a string have any uppercase chars in it? + Does a string have any uppercase chars in it? ****************************************************************************/ + BOOL strhasupper(const char *s) { smb_ucs2_t *ptr; push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); for(ptr=tmpbuf;*ptr;ptr++) - if(isupper_w(*ptr)) return True; + if(isupper_w(*ptr)) + return True; return(False); } /**************************************************************************** -does a string have any lowercase chars in it? + Does a string have any lowercase chars in it? ****************************************************************************/ + BOOL strhaslower(const char *s) { smb_ucs2_t *ptr; push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); for(ptr=tmpbuf;*ptr;ptr++) - if(islower_w(*ptr)) return True; + if(islower_w(*ptr)) + return True; return(False); } /**************************************************************************** -find the number of 'c' chars in a string + Find the number of 'c' chars in a string ****************************************************************************/ + size_t count_chars(const char *s,char c) { smb_ucs2_t *ptr; int count; push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(count=0,ptr=tmpbuf;*ptr;ptr++) if(*ptr==UCS2_CHAR(c)) count++; + for(count=0,ptr=tmpbuf;*ptr;ptr++) + if(*ptr==UCS2_CHAR(c)) + count++; return(count); } @@ -365,18 +401,22 @@ BOOL str_is_all(const char *s,char c) { smb_ucs2_t *ptr; - if(s == NULL) return False; - if(!*s) return False; + if(s == NULL) + return False; + if(!*s) + return False; push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(ptr=tmpbuf;*ptr;ptr++) if(*ptr!=UCS2_CHAR(c)) return False; + for(ptr=tmpbuf;*ptr;ptr++) + if(*ptr!=UCS2_CHAR(c)) + return False; return True; } /******************************************************************* -safe string copy into a known length string. maxlength does not -include the terminating zero. + Safe string copy into a known length string. maxlength does not + include the terminating zero. ********************************************************************/ char *safe_strcpy(char *dest,const char *src, size_t maxlength) @@ -407,8 +447,8 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) } /******************************************************************* -safe string cat into a string. maxlength does not -include the terminating zero. + Safe string cat into a string. maxlength does not + include the terminating zero. ********************************************************************/ char *safe_strcat(char *dest, const char *src, size_t maxlength) @@ -420,9 +460,8 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) return NULL; } - if (!src) { + if (!src) return dest; - } src_len = strlen(src); dest_len = strlen(dest); @@ -487,28 +526,30 @@ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, si char *StrnCpy(char *dest,const char *src,size_t n) { char *d = dest; - if (!dest) return(NULL); + if (!dest) + return(NULL); if (!src) { *dest = 0; return(dest); } - while (n-- && (*d++ = *src++)) ; + while (n-- && (*d++ = *src++)) + ; *d = 0; return(dest); } /**************************************************************************** -like strncpy but copies up to the character marker. always null terminates. -returns a pointer to the character marker in the source string (src). + Like strncpy but copies up to the character marker. always null terminates. + returns a pointer to the character marker in the source string (src). ****************************************************************************/ + char *strncpyn(char *dest, const char *src, size_t n, char c) { char *p; size_t str_len; p = strchr_m(src, c); - if (p == NULL) - { + if (p == NULL) { DEBUG(5, ("strncpyn: separator character (%c) not found\n", c)); return NULL; } @@ -520,7 +561,6 @@ char *strncpyn(char *dest, const char *src, size_t n, char c) return p; } - /************************************************************* Routine to get hex characters and turn them into a 16 byte array. the array can be variable length, and any non-hex-numeric @@ -530,6 +570,7 @@ char *strncpyn(char *dest, const char *src, size_t n, char c) valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n" **************************************************************/ + size_t strhex_to_str(char *p, size_t len, const char *strhex) { size_t i; @@ -538,25 +579,19 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) char *hexchars = "0123456789ABCDEF"; char *p1 = NULL, *p2 = NULL; - for (i = 0; i < len && strhex[i] != 0; i++) - { - if (strnequal(hexchars, "0x", 2)) - { + for (i = 0; i < len && strhex[i] != 0; i++) { + if (strnequal(hexchars, "0x", 2)) { i++; /* skip two chars */ continue; } if (!(p1 = strchr_m(hexchars, toupper(strhex[i])))) - { break; - } i++; /* next hex digit */ if (!(p2 = strchr_m(hexchars, toupper(strhex[i])))) - { break; - } /* get the two nybbles */ hinybble = PTR_DIFF(p1, hexchars); @@ -572,98 +607,99 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) } /**************************************************************************** -check if a string is part of a list + Check if a string is part of a list. ****************************************************************************/ + BOOL in_list(char *s,char *list,BOOL casesensitive) { - pstring tok; - char *p=list; - - if (!list) return(False); - - while (next_token(&p,tok,LIST_SEP,sizeof(tok))) { - if (casesensitive) { - if (strcmp(tok,s) == 0) - return(True); - } else { - if (StrCaseCmp(tok,s) == 0) - return(True); - } - } - return(False); + pstring tok; + char *p=list; + + if (!list) + return(False); + + while (next_token(&p,tok,LIST_SEP,sizeof(tok))) { + if (casesensitive) { + if (strcmp(tok,s) == 0) + return(True); + } else { + if (StrCaseCmp(tok,s) == 0) + return(True); + } + } + return(False); } /* this is used to prevent lots of mallocs of size 1 */ static char *null_string = NULL; /**************************************************************************** -set a string value, allocing the space for the string + Set a string value, allocing the space for the string ****************************************************************************/ + static BOOL string_init(char **dest,const char *src) { - size_t l; - if (!src) - src = ""; - - l = strlen(src); - - if (l == 0) - { - if (!null_string) { - if((null_string = (char *)malloc(1)) == NULL) { - DEBUG(0,("string_init: malloc fail for null_string.\n")); - return False; - } - *null_string = 0; - } - *dest = null_string; - } - else - { - (*dest) = (char *)malloc(l+1); - if ((*dest) == NULL) { - DEBUG(0,("Out of memory in string_init\n")); - return False; - } - - pstrcpy(*dest,src); - } - return(True); + size_t l; + if (!src) + src = ""; + + l = strlen(src); + + if (l == 0) { + if (!null_string) { + if((null_string = (char *)malloc(1)) == NULL) { + DEBUG(0,("string_init: malloc fail for null_string.\n")); + return False; + } + *null_string = 0; + } + *dest = null_string; + } else { + (*dest) = (char *)malloc(l+1); + if ((*dest) == NULL) { + DEBUG(0,("Out of memory in string_init\n")); + return False; + } + + pstrcpy(*dest,src); + } + return(True); } /**************************************************************************** -free a string value + Free a string value. ****************************************************************************/ + void string_free(char **s) { - if (!s || !(*s)) return; - if (*s == null_string) - *s = NULL; - SAFE_FREE(*s); + if (!s || !(*s)) + return; + if (*s == null_string) + *s = NULL; + SAFE_FREE(*s); } /**************************************************************************** -set a string value, deallocating any existing space, and allocing the space -for the string + Set a string value, deallocating any existing space, and allocing the space + for the string ****************************************************************************/ + BOOL string_set(char **dest,const char *src) { - string_free(dest); - - return(string_init(dest,src)); + string_free(dest); + return(string_init(dest,src)); } - /**************************************************************************** -substitute a string for a pattern in another string. Make sure there is -enough room! + Substitute a string for a pattern in another string. Make sure there is + enough room! -This routine looks for pattern in s and replaces it with -insert. It may do multiple replacements. + This routine looks for pattern in s and replaces it with + insert. It may do multiple replacements. -any of " ; ' $ or ` in the insert string are replaced with _ -if len==0 then the string cannot be extended. This is different from the old -use of len==0 which was for no length checks to be done. + Any of " ; ' $ or ` in the insert string are replaced with _ + if len==0 then the string cannot be extended. This is different from the old + use of len==0 which was for no length checks to be done. ****************************************************************************/ void string_sub(char *s,const char *pattern, const char *insert, size_t len) @@ -722,11 +758,12 @@ void pstring_sub(char *s,const char *pattern,const char *insert) string_sub(s, pattern, insert, sizeof(pstring)); } -/* similar to string_sub, but it will accept only allocated strings - * and may realloc them so pay attention at what you pass on no - * pointers inside strings, no pstrings or const may be passed - * as string. - */ +/**************************************************************************** + Similar to string_sub, but it will accept only allocated strings + and may realloc them so pay attention at what you pass on no + pointers inside strings, no pstrings or const may be passed + as string. +****************************************************************************/ char *realloc_string_sub(char *string, const char *pattern, const char *insert) { @@ -788,10 +825,10 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) } /**************************************************************************** -similar to string_sub() but allows for any character to be substituted. -Use with caution! -if len==0 then the string cannot be extended. This is different from the old -use of len==0 which was for no length checks to be done. + Similar to string_sub() but allows for any character to be substituted. + Use with caution! + if len==0 then the string cannot be extended. This is different from the old + use of len==0 which was for no length checks to be done. ****************************************************************************/ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) @@ -829,10 +866,10 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) } /**************************************************************************** -similar to all_string_sub but for unicode strings. -return a new allocated unicode string. - similar to string_sub() but allows for any character to be substituted. - Use with caution! + Similar to all_string_sub but for unicode strings. + Return a new allocated unicode string. + similar to string_sub() but allows for any character to be substituted. + Use with caution! ****************************************************************************/ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, @@ -842,7 +879,8 @@ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, const smb_ucs2_t *sp; size_t lr, lp, li, lt; - if (!insert || !pattern || !*pattern || !s) return NULL; + if (!insert || !pattern || !*pattern || !s) + return NULL; lt = (size_t)strlen_w(s); lp = (size_t)strlen_w(pattern); @@ -885,7 +923,8 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, { wpstring p, i; - if (!insert || !pattern || !s) return NULL; + if (!insert || !pattern || !s) + return NULL; push_ucs2(NULL, p, pattern, sizeof(wpstring) - 1, STR_TERMINATE); push_ucs2(NULL, i, insert, sizeof(wpstring) - 1, STR_TERMINATE); return all_string_sub_w(s, p, i); @@ -1049,11 +1088,12 @@ char *strdup_upper(const char *s) return t; } -/* - return a RFC2254 binary string representation of a buffer - used in LDAP filters - caller must free -*/ +/******************************************************************* + Return a RFC2254 binary string representation of a buffer. + Used in LDAP filters. + Caller must free. +********************************************************************/ + char *binary_string(char *buf, int len) { char *s; @@ -1072,7 +1112,9 @@ char *binary_string(char *buf, int len) return s; } -/* Just a typesafety wrapper for snprintf into a pstring */ +/******************************************************************* + Just a typesafety wrapper for snprintf into a pstring. +********************************************************************/ int pstr_sprintf(pstring s, const char *fmt, ...) { @@ -1085,7 +1127,9 @@ int pstr_sprintf(pstring s, const char *fmt, ...) return ret; } -/* Just a typesafety wrapper for snprintf into a fstring */ +/******************************************************************* + Just a typesafety wrapper for snprintf into a fstring. +********************************************************************/ int fstr_sprintf(fstring s, const char *fmt, ...) { @@ -1120,8 +1164,9 @@ int fstr_sprintf(fstring s, const char *fmt, ...) #ifndef HAVE_STRNLEN /******************************************************************* -some platforms don't have strnlen + Some platforms don't have strnlen ********************************************************************/ + size_t strnlen(const char *s, size_t n) { int i; @@ -1166,8 +1211,8 @@ char **str_list_make(const char *string, const char *sep) str_list_free(&list); SAFE_FREE(s); return NULL; - } - else list = rlist; + } else + list = rlist; memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1))); } @@ -1206,8 +1251,8 @@ BOOL str_list_copy(char ***dest, char **src) DEBUG(0,("str_list_copy: Unable to re-allocate memory")); str_list_free(&list); return False; - } - else list = rlist; + } else + list = rlist; memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1))); } @@ -1225,7 +1270,9 @@ BOOL str_list_copy(char ***dest, char **src) return True; } -/* return true if all the elements of the list match exactly */ +/*********************************************************** + Return true if all the elements of the list match exactly. +***********************************************************/ BOOL str_list_compare(char **list1, char **list2) { -- cgit From 2f194322d419350f35a48dff750066894d68eccf Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 12 Nov 2002 23:20:50 +0000 Subject: Removed global_myworkgroup, global_myname, global_myscope. Added liberal dashes of const. This is a rather large check-in, some things may break. It does compile though :-). Jeremy. (This used to be commit f755711df8f74f9b8e8c1a2b0d07d02a931eeb89) --- source3/lib/util_str.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6d1f8fe023..32efee1536 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -28,9 +28,9 @@ Extensively modified by Andrew.Tridgell@anu.edu.au ****************************************************************************/ -BOOL next_token(char **ptr,char *buff, const char *sep, size_t bufsize) +BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) { - char *s; + const char *s; BOOL quoted; size_t len=1; @@ -75,11 +75,11 @@ but beware the fact that it is not re-entrant! static char *last_ptr=NULL; -BOOL next_token_nr(char **ptr,char *buff, const char *sep, size_t bufsize) +BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) { BOOL ret; if (!ptr) - ptr = &last_ptr; + ptr = (const char **)&last_ptr; ret = next_token(ptr, buff, sep, bufsize); last_ptr = *ptr; @@ -613,7 +613,7 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) BOOL in_list(char *s,char *list,BOOL casesensitive) { pstring tok; - char *p=list; + const char *p=list; if (!list) return(False); @@ -1185,7 +1185,8 @@ int fstr_sprintf(fstring s, const char *fmt, ...) char **str_list_make(const char *string, const char *sep) { char **list, **rlist; - char *str, *s; + const char *str; + char *s; int num, lsize; pstring tok; @@ -1231,7 +1232,7 @@ char **str_list_make(const char *string, const char *sep) return list; } -BOOL str_list_copy(char ***dest, char **src) +BOOL str_list_copy(char ***dest, const char **src) { char **list, **rlist; int num, lsize; -- cgit From 39c78bf516f4db59fd3c218f67d13dd658daf558 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 11 Dec 2002 23:54:40 +0000 Subject: Fixed auth module code. Added VALGRIND defines to reduce spurious warnings. Jeremy. (This used to be commit ec4ed45563f9d8e25fcfd88840944a90b3139c3e) --- source3/lib/util_str.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 32efee1536..f3fa89b05f 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1366,3 +1366,14 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) return True; } + +#ifdef VALGRIND +size_t valgrind_strlen(const char *s) +{ + size_t count; + for(count = 0; *s++; count++) + ; + return count; +} +#endif + -- cgit From 43059acb95837fce3c8fdf5fc71b96c403e61939 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 3 Jan 2003 03:24:23 +0000 Subject: Merge from HEAD - add PRINTF_ATTRIBUTE to a few more functions. (This used to be commit 9e5297131cc53d7161aa74566f147b98e1c27aaa) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f3fa89b05f..5a1f159bdb 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1116,7 +1116,7 @@ char *binary_string(char *buf, int len) Just a typesafety wrapper for snprintf into a pstring. ********************************************************************/ -int pstr_sprintf(pstring s, const char *fmt, ...) + int pstr_sprintf(pstring s, const char *fmt, ...) { va_list ap; int ret; @@ -1131,7 +1131,7 @@ int pstr_sprintf(pstring s, const char *fmt, ...) Just a typesafety wrapper for snprintf into a fstring. ********************************************************************/ -int fstr_sprintf(fstring s, const char *fmt, ...) + int fstr_sprintf(fstring s, const char *fmt, ...) { va_list ap; int ret; -- cgit From 634c54310c92c48dd4eceec602e230a021bdcfc5 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 3 Jan 2003 08:28:12 +0000 Subject: Merge from HEAD - make Samba compile with -Wwrite-strings without additional warnings. (Adds a lot of const). Andrew Bartlett (This used to be commit 3a7458f9472432ef12c43008414925fd1ce8ea0c) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 5a1f159bdb..c519fd8497 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -576,7 +576,7 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) size_t i; size_t num_chars = 0; unsigned char lonybble, hinybble; - char *hexchars = "0123456789ABCDEF"; + const char *hexchars = "0123456789ABCDEF"; char *p1 = NULL, *p2 = NULL; for (i = 0; i < len && strhex[i] != 0; i++) { @@ -958,7 +958,7 @@ void split_at_last_component(char *path, char *front, char sep, char *back) Write an octal as a string. ****************************************************************************/ -char *octal_string(int i) +const char *octal_string(int i) { static char ret[64]; if (i == -1) -- cgit From 863e9ca2c640ce7a94acf81cff7408edc6f64e01 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 4 Jan 2003 08:48:15 +0000 Subject: Merge from HEAD - mimir's new gencache based namecache code. Andrew Bartlett (This used to be commit f79324f730c400342f445c931b0d75ff756d7cc7) --- source3/lib/util_str.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c519fd8497..b1fc53144e 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1367,6 +1367,158 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) return True; } + +#define IPSTR_LIST_SEP "," + +/** + * Add ip string representation to ipstr list. Used also + * as part of @function ipstr_list_make + * + * @param ipstr_list pointer to string containing ip list; + * MUST BE already allocated and IS reallocated if necessary + * @param ipstr_size pointer to current size of ipstr_list (might be changed + * as a result of reallocation) + * @param ip IP address which is to be added to list + * @return pointer to string appended with new ip and possibly + * reallocated to new length + **/ + +char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip) +{ + char* new_ipstr = NULL; + + /* arguments checking */ + if (!ipstr_list || !ip) return NULL; + + /* attempt to convert ip to a string and append colon separator to it */ + if (*ipstr_list) { + asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,inet_ntoa(*ip)); + SAFE_FREE(*ipstr_list); + } else { + asprintf(&new_ipstr, "%s", inet_ntoa(*ip)); + } + *ipstr_list = new_ipstr; + return *ipstr_list; +} + + +/** + * Allocate and initialise an ipstr list using ip adresses + * passed as arguments. + * + * @param ipstr_list pointer to string meant to be allocated and set + * @param ip_list array of ip addresses to place in the list + * @param ip_count number of addresses stored in ip_list + * @return pointer to allocated ip string + **/ + +char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_count) +{ + int i; + + /* arguments checking */ + if (!ip_list && !ipstr_list) return 0; + + *ipstr_list = NULL; + + /* process ip addresses given as arguments */ + for (i = 0; i < ip_count; i++) + *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]); + + return (*ipstr_list); +} + + +/** + * Parse given ip string list into array of ip addresses + * (as in_addr structures) + * + * @param ipstr ip string list to be parsed + * @param ip_list pointer to array of ip addresses which is + * allocated by this function and must be freed by caller + * @return number of succesfully parsed addresses + **/ + +int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list) +{ + fstring token_str; + int count; + + if (!ipstr_list || !ip_list) return 0; + + for (*ip_list = NULL, count = 0; + next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN); + count++) { + + struct in_addr addr; + + /* convert single token to ip address */ + if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE ) + break; + + /* prepare place for another in_addr structure */ + *ip_list = Realloc(*ip_list, (count + 1) * sizeof(struct in_addr)); + if (!*ip_list) return -1; + + (*ip_list)[count] = addr; + } + + return count; +} + + +/** + * Safely free ip string list + * + * @param ipstr_list ip string list to be freed + **/ + +void ipstr_list_free(char* ipstr_list) +{ + SAFE_FREE(ipstr_list); +} + + +/*********************************************************** + Unescape a URL encoded string, in place. +***********************************************************/ + +void rfc1738_unescape(char *buf) +{ + char *p=buf; + + while ((p=strchr_m(p,'+'))) + *p = ' '; + + p = buf; + + while (p && *p && (p=strchr_m(p,'%'))) { + int c1 = p[1]; + int c2 = p[2]; + + if (c1 >= '0' && c1 <= '9') + c1 = c1 - '0'; + else if (c1 >= 'A' && c1 <= 'F') + c1 = 10 + c1 - 'A'; + else if (c1 >= 'a' && c1 <= 'f') + c1 = 10 + c1 - 'a'; + else {p++; continue;} + + if (c2 >= '0' && c2 <= '9') + c2 = c2 - '0'; + else if (c2 >= 'A' && c2 <= 'F') + c2 = 10 + c2 - 'A'; + else if (c2 >= 'a' && c2 <= 'f') + c2 = 10 + c2 - 'a'; + else {p++; continue;} + + *p = (c1<<4) | c2; + + memmove(p+1, p+3, strlen(p+3)+1); + p++; + } +} + #ifdef VALGRIND size_t valgrind_strlen(const char *s) { -- cgit From aff13f53afe54506cdb679b355c7f9a3505c8736 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 4 Jan 2003 09:08:47 +0000 Subject: Merge from HEAD - whitespace :-) (This used to be commit 5fc90b6cf438480c9fcf8d01f0a319a52c5914cc) --- source3/lib/util_str.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b1fc53144e..7ffd71bde9 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1528,4 +1528,3 @@ size_t valgrind_strlen(const char *s) return count; } #endif - -- cgit From 99cdb462083381c88689a4e698ca48b6ed4cf5ac Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 15 Jan 2003 18:57:41 +0000 Subject: *lots of small merges form HEAD *sync up configure.in *don't build torture tools in make all *make sure to remove torture tools as part of make clean (This used to be commit 0fb724b3216eeeb97e61ff12755ca3a31bcad6ef) --- source3/lib/util_str.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7ffd71bde9..148181fddd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -308,8 +308,22 @@ char *skip_string(char *buf,size_t n) size_t str_charnum(const char *s) { - push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - return strlen_w(tmpbuf); + uint16 tmpbuf2[sizeof(pstring)]; + push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE); + return strlen_w(tmpbuf2); +} + +/******************************************************************* + Count the number of characters in a string. Normally this will + be the same as the number of bytes in a string for single byte strings, + but will be different for multibyte. +********************************************************************/ + +size_t str_ascii_charnum(const char *s) +{ + pstring tmpbuf2; + push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE); + return strlen(tmpbuf2); } /******************************************************************* @@ -655,13 +669,11 @@ static BOOL string_init(char **dest,const char *src) } *dest = null_string; } else { - (*dest) = (char *)malloc(l+1); + (*dest) = strdup(src); if ((*dest) == NULL) { DEBUG(0,("Out of memory in string_init\n")); return False; } - - pstrcpy(*dest,src); } return(True); } -- cgit From ff8ca2f88ba8835c271aa099ad9ff334c992407d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 17 Jan 2003 06:40:12 +0000 Subject: Janitorial duty... fix some undefined behaviour with increments in C. In theory a compiler could have produced complete crap for this code. (tridge). Jeremy. (This used to be commit 2b4335f06265940582f389f48dc4f87f452a2703) --- source3/lib/util_str.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 148181fddd..799bc64cc6 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1037,8 +1037,10 @@ void strlower_m(char *s) supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) - *s++ = tolower((unsigned char)*s); + while (*s && !(((unsigned char)s[0]) & 0x7F)) { + *s = tolower((unsigned char)*s); + s++; + } if (!*s) return; @@ -1074,8 +1076,10 @@ void strupper_m(char *s) supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) - *s++ = toupper((unsigned char)*s); + while (*s && !(((unsigned char)s[0]) & 0x7F)) { + *s = toupper((unsigned char)*s); + s++; + } if (!*s) return; -- cgit From 1cba0a757970ffd8b81d61c88965010968ab3eff Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 28 Jan 2003 12:07:02 +0000 Subject: Merge from HEAD: - NTLMSSP over SPENGO (sesssion-setup-and-x) cleanup and code refactor. - also consequential changes to the NTLMSSP and SPNEGO parsing functions - and the client code that uses the same functions - Add ntlm_auth, a NTLMSSP authentication interface for use by applications like Squid and Apache. - also consquential changes to use common code for base64 encode/decode. - Winbind changes to support ntlm_auth (I don't want this program to need to read smb.conf, instead getting all it's details over the pipe). - nmbd changes for fstrcat() instead of fstrcpy(). Andrew Bartlett (This used to be commit fbb46da79cf322570a7e3318100c304bbf33409e) --- source3/lib/util_str.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 799bc64cc6..2a9ee0a868 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1535,6 +1535,100 @@ void rfc1738_unescape(char *buf) } } +static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/*************************************************************************** +decode a base64 string into a DATA_BLOB - simple and slow algorithm + ***************************************************************************/ +DATA_BLOB base64_decode_data_blob(const char *s) +{ + int bit_offset, byte_offset, idx, i, n; + DATA_BLOB decoded = data_blob(s, strlen(s)+1); + unsigned char *d = decoded.data; + char *p; + + n=i=0; + + while (*s && (p=strchr_m(b64,*s))) { + idx = (int)(p - b64); + byte_offset = (i*6)/8; + bit_offset = (i*6)%8; + d[byte_offset] &= ~((1<<(8-bit_offset))-1); + if (bit_offset < 3) { + d[byte_offset] |= (idx << (2-bit_offset)); + n = byte_offset+1; + } else { + d[byte_offset] |= (idx >> (bit_offset-2)); + d[byte_offset+1] = 0; + d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF; + n = byte_offset+2; + } + s++; i++; + } + + /* fix up length */ + decoded.length = n; + return decoded; +} + +/*************************************************************************** +decode a base64 string in-place - wrapper for the above +***************************************************************************/ +void base64_decode(char *s) +{ + DATA_BLOB decoded = base64_decode_data_blob(s); + memcpy(s, decoded.data, decoded.length); + data_blob_free(&decoded); + + /* null terminate */ + s[decoded.length] = '\0'; +} + +/*************************************************************************** +encode a base64 string into a malloc()ed string caller to free. + +From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments +***************************************************************************/ +char * base64_encode_data_blob(DATA_BLOB data) +{ + int bits = 0; + int char_count = 0; + size_t out_cnt = 0; + size_t len = data.length; + size_t output_len = data.length * 2; + char *result = malloc(output_len); /* get us plenty of space */ + + while (len-- && out_cnt < (data.length * 2) - 5) { + int c = (unsigned char) *(data.data++); + bits += c; + char_count++; + if (char_count == 3) { + result[out_cnt++] = b64[bits >> 18]; + result[out_cnt++] = b64[(bits >> 12) & 0x3f]; + result[out_cnt++] = b64[(bits >> 6) & 0x3f]; + result[out_cnt++] = b64[bits & 0x3f]; + bits = 0; + char_count = 0; + } else { + bits <<= 8; + } + } + if (char_count != 0) { + bits <<= 16 - (8 * char_count); + result[out_cnt++] = b64[bits >> 18]; + result[out_cnt++] = b64[(bits >> 12) & 0x3f]; + if (char_count == 1) { + result[out_cnt++] = '='; + result[out_cnt++] = '='; + } else { + result[out_cnt++] = b64[(bits >> 6) & 0x3f]; + result[out_cnt++] = '='; + } + } + result[out_cnt] = '\0'; /* terminate */ + return result; +} + #ifdef VALGRIND size_t valgrind_strlen(const char *s) { -- cgit From 50edc1a8310c18ffc09cbd5dcc285122a638a4d7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 7 Feb 2003 04:11:36 +0000 Subject: merge from head (This used to be commit fd3216dbcbaec7d64dd24fe2af6c4156935c47e9) --- source3/lib/util_str.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2a9ee0a868..60e0e3837f 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -479,11 +479,15 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) src_len = strlen(src); dest_len = strlen(dest); - + if (src_len + dest_len > maxlength) { DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", (int)(src_len + dest_len - maxlength), src)); - src_len = maxlength - dest_len; + if (maxlength > dest_len) { + memcpy(&dest[dest_len], src, maxlength - dest_len); + } + dest[maxlength] = 0; + return NULL; } memcpy(&dest[dest_len], src, src_len); -- cgit From 17ec9642cdf29257297b56a202966863c9dac56f Mon Sep 17 00:00:00 2001 From: Jim McDonough Date: Tue, 18 Feb 2003 23:17:59 +0000 Subject: base64_decode() with heimdal libs, so I've renamed it base64_decode_inplace(). (This used to be commit d510ff85fb0dafddf3dea9412a09eeee6e70f0cb) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 60e0e3837f..21361a1862 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1578,7 +1578,7 @@ DATA_BLOB base64_decode_data_blob(const char *s) /*************************************************************************** decode a base64 string in-place - wrapper for the above ***************************************************************************/ -void base64_decode(char *s) +void base64_decode_inplace(char *s) { DATA_BLOB decoded = base64_decode_data_blob(s); memcpy(s, decoded.data, decoded.length); -- cgit From 266ec4aac04cb8666234f18baa38ff6387f40cb3 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 24 Feb 2003 03:09:08 +0000 Subject: Merge doxygen, signed/unsigned, const and other small fixes from HEAD to 3.0. Andrew Bartlett (This used to be commit 9ef0d40c3f8aef52ab321dc065264c42065bc876) --- source3/lib/util_str.c | 251 +++++++++++++++++++++++++------------------------ 1 file changed, 129 insertions(+), 122 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 21361a1862..cc4b6fe5c5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -21,13 +21,13 @@ #include "includes.h" -/**************************************************************************** - Get the next token from a string, return False if none found. - Handles double-quotes. - Based on a routine by GJC@VILLAGE.COM. - Extensively modified by Andrew.Tridgell@anu.edu.au -****************************************************************************/ - +/** + * Get the next token from a string, return False if none found. + * Handles double-quotes. + * + * Based on a routine by GJC@VILLAGE.COM. + * Extensively modified by Andrew.Tridgell@anu.edu.au + **/ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) { const char *s; @@ -67,11 +67,11 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) return(True); } -/**************************************************************************** +/** This is like next_token but is not re-entrant and "remembers" the first parameter so you can pass NULL. This is useful for user interface code but beware the fact that it is not re-entrant! -****************************************************************************/ +**/ static char *last_ptr=NULL; @@ -93,10 +93,10 @@ void set_first_token(char *ptr) last_ptr = ptr; } -/**************************************************************************** +/** Convert list of tokens to array; dependent on above routine. Uses last_ptr from above - bit of a hack. -****************************************************************************/ +**/ char **toktocliplist(int *ctok, const char *sep) { @@ -139,9 +139,9 @@ char **toktocliplist(int *ctok, const char *sep) return ret; } -/******************************************************************* +/** Case insensitive string compararison. -********************************************************************/ +**/ int StrCaseCmp(const char *s, const char *t) { @@ -151,9 +151,9 @@ int StrCaseCmp(const char *s, const char *t) return strcmp(buf1,buf2); } -/******************************************************************* +/** Case insensitive string compararison, length limited. -********************************************************************/ +**/ int StrnCaseCmp(const char *s, const char *t, size_t n) { @@ -163,9 +163,9 @@ int StrnCaseCmp(const char *s, const char *t, size_t n) return strncmp(buf1,buf2,n); } -/******************************************************************* +/** Compare 2 strings. -********************************************************************/ +**/ BOOL strequal(const char *s1, const char *s2) { @@ -177,9 +177,9 @@ BOOL strequal(const char *s1, const char *s2) return(StrCaseCmp(s1,s2)==0); } -/******************************************************************* +/** Compare 2 strings up to and including the nth char. -******************************************************************/ +**/ BOOL strnequal(const char *s1,const char *s2,size_t n) { @@ -191,9 +191,9 @@ BOOL strnequal(const char *s1,const char *s2,size_t n) return(StrnCaseCmp(s1,s2,n)==0); } -/******************************************************************* +/** Compare 2 strings (case sensitive). -********************************************************************/ +**/ BOOL strcsequal(const char *s1,const char *s2) { @@ -205,9 +205,9 @@ BOOL strcsequal(const char *s1,const char *s2) return(strcmp(s1,s2)==0); } -/*************************************************************************** +/** Do a case-insensitive, whitespace-ignoring string compare. -***************************************************************************/ +**/ int strwicmp(const char *psz1, const char *psz2) { @@ -236,9 +236,9 @@ int strwicmp(const char *psz1, const char *psz2) } -/******************************************************************* +/** Convert a string to upper case, but don't modify it. -********************************************************************/ +**/ char *strupper_static(const char *s) { @@ -250,9 +250,9 @@ char *strupper_static(const char *s) return str; } -/******************************************************************* +/** Convert a string to "normal" form. -********************************************************************/ +**/ void strnorm(char *s) { @@ -263,9 +263,9 @@ void strnorm(char *s) strlower(s); } -/******************************************************************* +/** Check if a string is in "normal" case. -********************************************************************/ +**/ BOOL strisnormal(const char *s) { @@ -277,10 +277,10 @@ BOOL strisnormal(const char *s) } -/**************************************************************************** +/** String replace. NOTE: oldc and newc must be 7 bit characters -****************************************************************************/ +**/ void string_replace(char *s,char oldc,char newc) { @@ -289,9 +289,9 @@ void string_replace(char *s,char oldc,char newc) pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); } -/******************************************************************* +/** Skip past some strings in a buffer. -********************************************************************/ +**/ char *skip_string(char *buf,size_t n) { @@ -300,11 +300,11 @@ char *skip_string(char *buf,size_t n) return(buf); } -/******************************************************************* +/** Count the number of characters in a string. Normally this will be the same as the number of bytes in a string for single byte strings, but will be different for multibyte. -********************************************************************/ +**/ size_t str_charnum(const char *s) { @@ -313,11 +313,11 @@ size_t str_charnum(const char *s) return strlen_w(tmpbuf2); } -/******************************************************************* +/** Count the number of characters in a string. Normally this will be the same as the number of bytes in a string for single byte strings, but will be different for multibyte. -********************************************************************/ +**/ size_t str_ascii_charnum(const char *s) { @@ -326,9 +326,9 @@ size_t str_ascii_charnum(const char *s) return strlen(tmpbuf2); } -/******************************************************************* +/** Trim the specified elements off the front and back of a string. -********************************************************************/ +**/ BOOL trim_string(char *s,const char *front,const char *back) { @@ -364,9 +364,9 @@ BOOL trim_string(char *s,const char *front,const char *back) return ret; } -/**************************************************************************** +/** Does a string have any uppercase chars in it? -****************************************************************************/ +**/ BOOL strhasupper(const char *s) { @@ -378,9 +378,9 @@ BOOL strhasupper(const char *s) return(False); } -/**************************************************************************** +/** Does a string have any lowercase chars in it? -****************************************************************************/ +**/ BOOL strhaslower(const char *s) { @@ -392,9 +392,9 @@ BOOL strhaslower(const char *s) return(False); } -/**************************************************************************** +/** Find the number of 'c' chars in a string -****************************************************************************/ +**/ size_t count_chars(const char *s,char c) { @@ -407,9 +407,9 @@ size_t count_chars(const char *s,char c) return(count); } -/******************************************************************* +/** Return True if a string consists only of one particular character. -********************************************************************/ +**/ BOOL str_is_all(const char *s,char c) { @@ -428,10 +428,10 @@ BOOL str_is_all(const char *s,char c) return True; } -/******************************************************************* +/** Safe string copy into a known length string. maxlength does not include the terminating zero. -********************************************************************/ +**/ char *safe_strcpy(char *dest,const char *src, size_t maxlength) { @@ -442,6 +442,14 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) return NULL; } +#ifdef DEVELOPER + /* We intentionally write out at the extremity of the destination + * string. If the destination is too short (e.g. pstrcpy into mallocd + * or fstring) then this should cause an error under a memory + * checker. */ + dest[maxlength] = '\0'; +#endif + if (!src) { *dest = 0; return dest; @@ -450,8 +458,8 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) len = strlen(src); if (len > maxlength) { - DEBUG(0,("ERROR: string overflow by %d in safe_strcpy [%.50s]\n", - (int)(len-maxlength), src)); + DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n", + (unsigned int)(len-maxlength), len, maxlength, src)); len = maxlength; } @@ -460,10 +468,10 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) return dest; } -/******************************************************************* +/** Safe string cat into a string. maxlength does not include the terminating zero. -********************************************************************/ +**/ char *safe_strcat(char *dest, const char *src, size_t maxlength) { @@ -495,12 +503,12 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) return dest; } -/******************************************************************* +/** Paranoid strcpy into a buffer of given length (includes terminating zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars and replaces with '_'. Deliberately does *NOT* check for multibyte characters. Don't change it ! -********************************************************************/ +**/ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength) { @@ -536,10 +544,10 @@ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, si return dest; } -/**************************************************************************** +/** Like strncpy but always null terminates. Make sure there is room! The variable n should always be one less than the available size. -****************************************************************************/ +**/ char *StrnCpy(char *dest,const char *src,size_t n) { @@ -556,10 +564,10 @@ char *StrnCpy(char *dest,const char *src,size_t n) return(dest); } -/**************************************************************************** +/** Like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). -****************************************************************************/ +**/ char *strncpyn(char *dest, const char *src, size_t n, char c) { @@ -579,7 +587,7 @@ char *strncpyn(char *dest, const char *src, size_t n, char c) return p; } -/************************************************************* +/** Routine to get hex characters and turn them into a 16 byte array. the array can be variable length, and any non-hex-numeric characters are skipped. "0xnn" or "0Xnn" is specially catered @@ -587,7 +595,7 @@ char *strncpyn(char *dest, const char *src, size_t n, char c) valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n" -**************************************************************/ +**/ size_t strhex_to_str(char *p, size_t len, const char *strhex) { @@ -624,9 +632,9 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) return num_chars; } -/**************************************************************************** +/** Check if a string is part of a list. -****************************************************************************/ +**/ BOOL in_list(char *s,char *list,BOOL casesensitive) { @@ -651,9 +659,9 @@ BOOL in_list(char *s,char *list,BOOL casesensitive) /* this is used to prevent lots of mallocs of size 1 */ static char *null_string = NULL; -/**************************************************************************** +/** Set a string value, allocing the space for the string -****************************************************************************/ +**/ static BOOL string_init(char **dest,const char *src) { @@ -682,9 +690,9 @@ static BOOL string_init(char **dest,const char *src) return(True); } -/**************************************************************************** +/** Free a string value. -****************************************************************************/ +**/ void string_free(char **s) { @@ -695,10 +703,10 @@ void string_free(char **s) SAFE_FREE(*s); } -/**************************************************************************** +/** Set a string value, deallocating any existing space, and allocing the space for the string -****************************************************************************/ +**/ BOOL string_set(char **dest,const char *src) { @@ -706,7 +714,7 @@ BOOL string_set(char **dest,const char *src) return(string_init(dest,src)); } -/**************************************************************************** +/** Substitute a string for a pattern in another string. Make sure there is enough room! @@ -716,7 +724,7 @@ BOOL string_set(char **dest,const char *src) Any of " ; ' $ or ` in the insert string are replaced with _ if len==0 then the string cannot be extended. This is different from the old use of len==0 which was for no length checks to be done. -****************************************************************************/ +**/ void string_sub(char *s,const char *pattern, const char *insert, size_t len) { @@ -774,12 +782,12 @@ void pstring_sub(char *s,const char *pattern,const char *insert) string_sub(s, pattern, insert, sizeof(pstring)); } -/**************************************************************************** +/** Similar to string_sub, but it will accept only allocated strings and may realloc them so pay attention at what you pass on no pointers inside strings, no pstrings or const may be passed as string. -****************************************************************************/ +**/ char *realloc_string_sub(char *string, const char *pattern, const char *insert) { @@ -840,12 +848,12 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) return string; } -/**************************************************************************** +/** Similar to string_sub() but allows for any character to be substituted. Use with caution! if len==0 then the string cannot be extended. This is different from the old use of len==0 which was for no length checks to be done. -****************************************************************************/ +**/ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) { @@ -881,12 +889,12 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) } } -/**************************************************************************** +/** Similar to all_string_sub but for unicode strings. Return a new allocated unicode string. similar to string_sub() but allows for any character to be substituted. Use with caution! -****************************************************************************/ +**/ smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, const smb_ucs2_t *insert) @@ -946,9 +954,9 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, return all_string_sub_w(s, p, i); } -/**************************************************************************** +/** Splits out the front and back at a separator. -****************************************************************************/ +**/ void split_at_last_component(char *path, char *front, char sep, char *back) { @@ -970,9 +978,9 @@ void split_at_last_component(char *path, char *front, char sep, char *back) } } -/**************************************************************************** +/** Write an octal as a string. -****************************************************************************/ +**/ const char *octal_string(int i) { @@ -984,9 +992,9 @@ const char *octal_string(int i) } -/**************************************************************************** +/** Truncate a string at a specified length. -****************************************************************************/ +**/ char *string_truncate(char *s, int length) { @@ -995,10 +1003,10 @@ char *string_truncate(char *s, int length) return s; } -/**************************************************************************** +/** Strchr and strrchr_m are very hard to do on general multi-byte strings. We convert via ucs2 for now. -****************************************************************************/ +**/ char *strchr_m(const char *s, char c) { @@ -1030,9 +1038,9 @@ char *strrchr_m(const char *s, char c) return (char *)(s+strlen(s2)); } -/******************************************************************* +/** Convert a string to lower case. -********************************************************************/ +**/ void strlower_m(char *s) { @@ -1054,9 +1062,9 @@ void strlower_m(char *s) unix_strlower(s,strlen(s)+1,s,strlen(s)+1); } -/******************************************************************* +/** Duplicate convert a string to lower case. -********************************************************************/ +**/ char *strdup_lower(const char *s) { @@ -1069,9 +1077,9 @@ char *strdup_lower(const char *s) return t; } -/******************************************************************* +/** Convert a string to upper case. -********************************************************************/ +**/ void strupper_m(char *s) { @@ -1093,9 +1101,9 @@ void strupper_m(char *s) unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } -/******************************************************************* +/** Convert a string to upper case. -********************************************************************/ +**/ char *strdup_upper(const char *s) { @@ -1108,11 +1116,11 @@ char *strdup_upper(const char *s) return t; } -/******************************************************************* +/** Return a RFC2254 binary string representation of a buffer. Used in LDAP filters. Caller must free. -********************************************************************/ +**/ char *binary_string(char *buf, int len) { @@ -1132,9 +1140,9 @@ char *binary_string(char *buf, int len) return s; } -/******************************************************************* +/** Just a typesafety wrapper for snprintf into a pstring. -********************************************************************/ +**/ int pstr_sprintf(pstring s, const char *fmt, ...) { @@ -1147,9 +1155,9 @@ char *binary_string(char *buf, int len) return ret; } -/******************************************************************* +/** Just a typesafety wrapper for snprintf into a fstring. -********************************************************************/ +**/ int fstr_sprintf(fstring s, const char *fmt, ...) { @@ -1163,9 +1171,9 @@ char *binary_string(char *buf, int len) } #ifndef HAVE_STRNDUP -/******************************************************************* +/** Some platforms don't have strndup. -********************************************************************/ +**/ char *strndup(const char *s, size_t n) { @@ -1183,9 +1191,9 @@ char *binary_string(char *buf, int len) #endif #ifndef HAVE_STRNLEN -/******************************************************************* +/** Some platforms don't have strnlen -********************************************************************/ +**/ size_t strnlen(const char *s, size_t n) { @@ -1196,9 +1204,9 @@ char *binary_string(char *buf, int len) } #endif -/*********************************************************** +/** List of Strings manipulation functions -***********************************************************/ +**/ #define S_LIST_ABS 16 /* List Allocation Block Size */ @@ -1291,10 +1299,9 @@ BOOL str_list_copy(char ***dest, const char **src) return True; } -/*********************************************************** - Return true if all the elements of the list match exactly. -***********************************************************/ - +/** + * Return true if all the elements of the list match exactly. + **/ BOOL str_list_compare(char **list1, char **list2) { int num; @@ -1499,9 +1506,9 @@ void ipstr_list_free(char* ipstr_list) } -/*********************************************************** +/** Unescape a URL encoded string, in place. -***********************************************************/ +**/ void rfc1738_unescape(char *buf) { @@ -1541,9 +1548,9 @@ void rfc1738_unescape(char *buf) static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -/*************************************************************************** -decode a base64 string into a DATA_BLOB - simple and slow algorithm - ***************************************************************************/ +/** + * Decode a base64 string into a DATA_BLOB - simple and slow algorithm + **/ DATA_BLOB base64_decode_data_blob(const char *s) { int bit_offset, byte_offset, idx, i, n; @@ -1575,9 +1582,9 @@ DATA_BLOB base64_decode_data_blob(const char *s) return decoded; } -/*************************************************************************** -decode a base64 string in-place - wrapper for the above -***************************************************************************/ +/** + * Decode a base64 string in-place - wrapper for the above + **/ void base64_decode_inplace(char *s) { DATA_BLOB decoded = base64_decode_data_blob(s); @@ -1588,11 +1595,11 @@ void base64_decode_inplace(char *s) s[decoded.length] = '\0'; } -/*************************************************************************** -encode a base64 string into a malloc()ed string caller to free. - -From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments -***************************************************************************/ +/** + * Encode a base64 string into a malloc()ed string caller to free. + * + *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments + **/ char * base64_encode_data_blob(DATA_BLOB data) { int bits = 0; -- cgit From 467f1028f44f96cffac8f81a6d921c82d9242bed Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 17 Mar 2003 22:42:01 +0000 Subject: Merge from (earlier) HEAD - doxygen. I'm not merging the current HEAD string stuff quite yet. (This used to be commit 9b8d12e081462566043bb51e9c95605609572a54) --- source3/lib/util_str.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index cc4b6fe5c5..070c59c1b2 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -164,9 +164,10 @@ int StrnCaseCmp(const char *s, const char *t, size_t n) } /** - Compare 2 strings. -**/ - + * Compare 2 strings. + * + * @note The comparison is case-insensitive. + **/ BOOL strequal(const char *s1, const char *s2) { if (s1 == s2) @@ -178,9 +179,10 @@ BOOL strequal(const char *s1, const char *s2) } /** - Compare 2 strings up to and including the nth char. -**/ - + * Compare 2 strings up to and including the nth char. + * + * @note The comparison is case-insensitive. + **/ BOOL strnequal(const char *s1,const char *s2,size_t n) { if (s1 == s2) -- cgit From d332200c254b4bbf27461a37f9655bf42faa2b3a Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 18 Mar 2003 01:48:11 +0000 Subject: Merge in the developer string options from HEAD. We need to ensure 3.0 is as stable as possible in the string department and some pain now will help later :-). Jeremy. (This used to be commit 86e3eddac698d90f4666b8492b4603a4efbbd67b) --- source3/lib/util_str.c | 98 ++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 47 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 070c59c1b2..7643c2807e 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -21,6 +21,11 @@ #include "includes.h" +#ifdef DEVELOPER +const char *global_clobber_region_function; +unsigned int global_clobber_region_line; +#endif + /** * Get the next token from a string, return False if none found. * Handles double-quotes. @@ -73,7 +78,7 @@ parameter so you can pass NULL. This is useful for user interface code but beware the fact that it is not re-entrant! **/ -static char *last_ptr=NULL; +static const char *last_ptr=NULL; BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) { @@ -410,32 +415,33 @@ size_t count_chars(const char *s,char c) } /** -Return True if a string consists only of one particular character. -**/ - -BOOL str_is_all(const char *s,char c) + * In developer builds, clobber a region of memory. + * + * If we think a string buffer is longer than it really is, this ought + * to make the failure obvious, by segfaulting (if in the heap) or by + * killing the return address (on the stack), or by trapping under a + * memory debugger. + * + * This is meant to catch possible string overflows, even if the + * actual string copied is not big enough to cause an overflow. + **/ +void clobber_region(const char *fn, unsigned int line, char *dest, size_t len) { - smb_ucs2_t *ptr; - - if(s == NULL) - return False; - if(!*s) - return False; - - push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(ptr=tmpbuf;*ptr;ptr++) - if(*ptr!=UCS2_CHAR(c)) - return False; - - return True; +#ifdef DEVELOPER + /* F1 is odd and 0xf1f1f1f1 shouldn't be a valid pointer */ + memset(dest, 0xF1, len); + global_clobber_region_function = fn; + global_clobber_region_line = line; +#endif } + /** Safe string copy into a known length string. maxlength does not include the terminating zero. **/ -char *safe_strcpy(char *dest,const char *src, size_t maxlength) +char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_t maxlength) { size_t len; @@ -444,13 +450,7 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) return NULL; } -#ifdef DEVELOPER - /* We intentionally write out at the extremity of the destination - * string. If the destination is too short (e.g. pstrcpy into mallocd - * or fstring) then this should cause an error under a memory - * checker. */ - dest[maxlength] = '\0'; -#endif + clobber_region(fn,line,dest, maxlength+1); if (!src) { *dest = 0; @@ -474,8 +474,7 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength) Safe string cat into a string. maxlength does not include the terminating zero. **/ - -char *safe_strcat(char *dest, const char *src, size_t maxlength) +char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size_t maxlength) { size_t src_len, dest_len; @@ -490,6 +489,8 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) src_len = strlen(src); dest_len = strlen(dest); + clobber_region(fn, line, dest + dest_len, maxlength + 1 - dest_len); + if (src_len + dest_len > maxlength) { DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", (int)(src_len + dest_len - maxlength), src)); @@ -499,7 +500,7 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) dest[maxlength] = 0; return NULL; } - + memcpy(&dest[dest_len], src, src_len); dest[dest_len + src_len] = 0; return dest; @@ -511,11 +512,12 @@ char *safe_strcat(char *dest, const char *src, size_t maxlength) and replaces with '_'. Deliberately does *NOT* check for multibyte characters. Don't change it ! **/ - -char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength) +char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, const char *other_safe_chars, size_t maxlength) { size_t len, i; + clobber_region(fn, line, dest, maxlength); + if (!dest) { DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n")); return NULL; @@ -550,12 +552,15 @@ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, si Like strncpy but always null terminates. Make sure there is room! The variable n should always be one less than the available size. **/ - -char *StrnCpy(char *dest,const char *src,size_t n) +char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) { char *d = dest; + + clobber_region(fn, line, dest, n+1); + if (!dest) return(NULL); + if (!src) { *dest = 0; return(dest); @@ -566,16 +571,19 @@ char *StrnCpy(char *dest,const char *src,size_t n) return(dest); } +#if 0 /** Like strncpy but copies up to the character marker. always null terminates. returns a pointer to the character marker in the source string (src). **/ -char *strncpyn(char *dest, const char *src, size_t n, char c) +static char *strncpyn(char *dest, const char *src, size_t n, char c) { char *p; size_t str_len; + clobber_region(dest, n+1); + p = strchr_m(src, c); if (p == NULL) { DEBUG(5, ("strncpyn: separator character (%c) not found\n", c)); @@ -588,6 +596,7 @@ char *strncpyn(char *dest, const char *src, size_t n, char c) return p; } +#endif /** Routine to get hex characters and turn them into a 16 byte array. @@ -898,7 +907,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) Use with caution! **/ -smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, +static smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, const smb_ucs2_t *insert) { smb_ucs2_t *r, *rp; @@ -956,11 +965,12 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, return all_string_sub_w(s, p, i); } +#if 0 /** Splits out the front and back at a separator. **/ -void split_at_last_component(char *path, char *front, char sep, char *back) +static void split_at_last_component(char *path, char *front, char sep, char *back) { char *p = strrchr_m(path, sep); @@ -979,6 +989,7 @@ void split_at_last_component(char *path, char *front, char sep, char *back) back[0] = 0; } } +#endif /** Write an octal as a string. @@ -998,7 +1009,7 @@ const char *octal_string(int i) Truncate a string at a specified length. **/ -char *string_truncate(char *s, int length) +char *string_truncate(char *s, unsigned int length) { if (s && strlen(s) > length) s[length] = 0; @@ -1157,11 +1168,12 @@ char *binary_string(char *buf, int len) return ret; } +#if 0 /** Just a typesafety wrapper for snprintf into a fstring. **/ - int fstr_sprintf(fstring s, const char *fmt, ...) +static int fstr_sprintf(fstring s, const char *fmt, ...) { va_list ap; int ret; @@ -1171,6 +1183,7 @@ char *binary_string(char *buf, int len) va_end(ap); return ret; } +#endif #ifndef HAVE_STRNDUP /** @@ -1642,12 +1655,3 @@ char * base64_encode_data_blob(DATA_BLOB data) return result; } -#ifdef VALGRIND -size_t valgrind_strlen(const char *s) -{ - size_t count; - for(count = 0; *s++; count++) - ; - return count; -} -#endif -- cgit From 84e99fe8986dac9fa95857bf15bbd633db78e7ea Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 18 Mar 2003 21:21:21 +0000 Subject: Merge mbp's HEAD changes. Jeremy. (This used to be commit da1271a95fce7fd217555fb161d4669d0b9b80e2) --- source3/lib/util_str.c | 104 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 33 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7643c2807e..5157de0d91 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1,8 +1,10 @@ /* Unix SMB/CIFS implementation. Samba utility functions + Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Simo Sorce 2001-2002 + Copyright (C) Martin Pool 2003 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,10 +23,10 @@ #include "includes.h" -#ifdef DEVELOPER -const char *global_clobber_region_function; -unsigned int global_clobber_region_line; -#endif +/** + * @file + * @brief String utilities. + **/ /** * Get the next token from a string, return False if none found. @@ -145,21 +147,79 @@ char **toktocliplist(int *ctok, const char *sep) } /** - Case insensitive string compararison. -**/ - + * Case insensitive string compararison. + * + * iconv does not directly give us a way to compare strings in + * arbitrary unix character sets -- all we can is convert and then + * compare. This is expensive. + * + * As an optimization, we do a first pass that considers only the + * prefix of the strings that is entirely 7-bit. Within this, we + * check whether they have the same value. + * + * Hopefully this will often give the answer without needing to copy. + * In particular it should speed comparisons to literal ascii strings + * or comparisons of strings that are "obviously" different. + * + * If we find a non-ascii character we fall back to converting via + * iconv. + * + * This should never be slower than convering the whole thing, and + * often faster. + * + * A different optimization would be to compare for bitwise equality + * in the binary encoding. (It would be possible thought hairy to do + * both simultaneously.) But in that case if they turn out to be + * different, we'd need to restart the whole thing. + * + * Even better is to implement strcasecmp for each encoding and use a + * function pointer. + **/ int StrCaseCmp(const char *s, const char *t) { + + const char * ps, * pt; pstring buf1, buf2; - unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1)); - unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2)); - return strcmp(buf1,buf2); + + for (ps = s, pt = t; ; ps++, pt++) { + char us, ut; + + if (!*ps && !*pt) + return 0; /* both ended */ + else if (!*ps) + return -1; /* s is a prefix */ + else if (!*pt) + return +1; /* t is a prefix */ + else if ((*ps & 0x80) || (*pt & 0x80)) + /* not ascii anymore, do it the hard way from here on in */ + break; + + us = toupper(*ps); + ut = toupper(*pt); + if (us == ut) + continue; + else if (us < ut) + return -1; + else if (us > ut) + return +1; + } + + /* TODO: Don't do this with a fixed-length buffer. This could + * still be much more efficient. */ + /* TODO: Hardcode a char-by-char comparison for UTF-8, which + * can be much faster. */ + /* TODO: Test case for this! */ + + unix_strupper(ps, strlen(ps)+1, buf1, sizeof(buf1)); + unix_strupper(pt, strlen(pt)+1, buf2, sizeof(buf2)); + + return strcmp(buf1, buf2); } + /** Case insensitive string compararison, length limited. **/ - int StrnCaseCmp(const char *s, const char *t, size_t n) { pstring buf1, buf2; @@ -414,28 +474,6 @@ size_t count_chars(const char *s,char c) return(count); } -/** - * In developer builds, clobber a region of memory. - * - * If we think a string buffer is longer than it really is, this ought - * to make the failure obvious, by segfaulting (if in the heap) or by - * killing the return address (on the stack), or by trapping under a - * memory debugger. - * - * This is meant to catch possible string overflows, even if the - * actual string copied is not big enough to cause an overflow. - **/ -void clobber_region(const char *fn, unsigned int line, char *dest, size_t len) -{ -#ifdef DEVELOPER - /* F1 is odd and 0xf1f1f1f1 shouldn't be a valid pointer */ - memset(dest, 0xF1, len); - global_clobber_region_function = fn; - global_clobber_region_line = line; -#endif -} - - /** Safe string copy into a known length string. maxlength does not include the terminating zero. -- cgit From 6c6fb121cd36c4aaeacca295fde744617e3fa61e Mon Sep 17 00:00:00 2001 From: Jim McDonough Date: Wed, 19 Mar 2003 20:50:56 +0000 Subject: use strnlen to prevent coredumps (This used to be commit 5078436d83f0fdc568d6687809c7c70dea5fd382) --- source3/lib/util_str.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 5157de0d91..d1e57ed5cf 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -495,7 +495,7 @@ char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_ return dest; } - len = strlen(src); + len = strnlen(src, maxlength+1); if (len > maxlength) { DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n", @@ -524,8 +524,8 @@ char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size if (!src) return dest; - src_len = strlen(src); - dest_len = strlen(dest); + src_len = strnlen(src, maxlength + 1); + dest_len = strnlen(dest, maxlength + 1); clobber_region(fn, line, dest + dest_len, maxlength + 1 - dest_len); -- cgit From 53beee9e5675a59c67d9ecfbaec50dca4ac01750 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 24 Mar 2003 09:54:13 +0000 Subject: (merge from HEAD) NTLM Authentication: - Add a 'privileged' mode to Winbindd. This is achieved by means of a directory under lockdir, that the admin can change the group access for. - This mode is now required to access with 'CRAP' authentication feature. - This *will* break the current SQUID helper, so I've fixed up our ntlm_auth replacement: - Update our NTLMSSP code to cope with 'datagram' mode, where we don't get a challenge. - Use this to make our ntlm_auth utility suitable for use in current Squid 2.5 servers. - Tested - works for Win2k clients, but not Win9X at present. NTLMSSP updates are needed. - Now uses fgets(), not x_fgets() to cope with Squid environment (I think somthing to do with non-blocking stdin). - Add much more robust connection code to wb_common.c - it will not connect to a server of a different protocol version, and it will automatically try and reconnect to the 'privileged' pipe if possible. - This could help with 'privileged' idmap operations etc in future. - Add a generic HEX encode routine to util_str.c, - fix a small line of dodgy C in StrnCpy_fn() - Correctly pull our 'session key' out of the info3 from th the DC. This is used in both the auth code, and in for export over the winbind pipe to ntlm_auth. - Given the user's challenge/response and access to the privileged pipe, allow external access to the 'session key'. To be used for MSCHAPv2 integration. Andrew Bartlett (This used to be commit ec071ca3dcbd3881dc08e6a8d7ac2ff0bcd57664) --- source3/lib/util_str.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d1e57ed5cf..4d955c59a7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -603,8 +603,12 @@ char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) *dest = 0; return(dest); } - while (n-- && (*d++ = *src++)) - ; + + while (n-- && (*d = *src)) { + d++; + src++; + } + *d = 0; return(dest); } @@ -681,6 +685,22 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) return num_chars; } +/** + * Routine to print a buffer as HEX digits, into an allocated string. + */ + +void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer) +{ + int i; + char *hex_buffer; + + *out_hex_buffer = smb_xmalloc((len*2)+1); + hex_buffer = *out_hex_buffer; + + for (i = 0; i < len; i++) + slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]); +} + /** Check if a string is part of a list. **/ -- cgit From 9ad1ddc793ff383c2cc05cf3b15a9eb95e7a251d Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Wed, 2 Apr 2003 00:17:03 +0000 Subject: Don't set zero length for the base64 decoded string (fixes swat auth). Andrew Bartlett (This used to be commit 7ab39cba6a97ddd0879dd968167fc7809f87de6e) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 4d955c59a7..e2f9f19f58 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1662,10 +1662,10 @@ void base64_decode_inplace(char *s) { DATA_BLOB decoded = base64_decode_data_blob(s); memcpy(s, decoded.data, decoded.length); - data_blob_free(&decoded); - /* null terminate */ s[decoded.length] = '\0'; + + data_blob_free(&decoded); } /** -- cgit From d15cd357c702aaad4093f770d557b61a4e12f3a0 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 15 Apr 2003 19:51:17 +0000 Subject: merge in metze' smbcquotas patch from HEAD (This used to be commit b6a77048886151435a4a5eeb9a04be44d397c504) --- source3/lib/util_str.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e2f9f19f58..e561d15f61 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1226,12 +1226,12 @@ char *binary_string(char *buf, int len) return ret; } -#if 0 + /** Just a typesafety wrapper for snprintf into a fstring. **/ -static int fstr_sprintf(fstring s, const char *fmt, ...) +int fstr_sprintf(fstring s, const char *fmt, ...) { va_list ap; int ret; @@ -1241,7 +1241,7 @@ static int fstr_sprintf(fstring s, const char *fmt, ...) va_end(ap); return ret; } -#endif + #ifndef HAVE_STRNDUP /** -- cgit From e8573c8fa928602fd979d5ac45c692e7464f0aad Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Mon, 12 May 2003 01:20:17 +0000 Subject: Add NT quota support. Patch from Stefan (metze) Metzemacher 1. Allows to change quota settings for shared mount points from Win2K and WinXP from Explorer properties tab 2. Disabled by default and when requested, will be probed and enabled only on Linux where it works 3. Was tested for approx. two weeks now on Linux by two independent QA teams, have not found any bugs so far Documentation to follow (This used to be commit 4bf022ce9e45be85609426762ba2644ac2031326) --- source3/lib/util_str.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e561d15f61..87a8ea2eb1 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1713,3 +1713,25 @@ char * base64_encode_data_blob(DATA_BLOB data) return result; } +/* read a SMB_BIG_UINT from a string */ +SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) +{ + + SMB_BIG_UINT val = -1; + const char *p = nptr; + + while (p && *p && isspace(*p)) + p++; +#ifdef LARGE_SMB_OFF_T + sscanf(p,"%llu",&val); +#else /* LARGE_SMB_OFF_T */ + sscanf(p,"%lu",&val); +#endif /* LARGE_SMB_OFF_T */ + if (entptr) { + while (p && *p && isdigit(*p)) + p++; + *entptr = p; + } + + return val; +} -- cgit From f51d769dd303027a3dbf46fc89a482933988e866 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 25 Jun 2003 17:41:05 +0000 Subject: large change: *) consolidates the dc location routines again (dns and netbios) get_dc_list() or get_sorted_dc_list() is the authoritative means of locating DC's again. (also inludes a flag to get_dc_list() to define if this should be a DNS only lookup or not) (however, if you set "name resolve order = hosts wins" you could still get DNS queries for domain name IFF ldap_domain2hostlist() fails. The answer? Fix your DNS setup) *) enabled DOMAIN<0x1c> lookups to be funneled through resolve_hosts resulting in a call to ldap_domain2hostlist() if lp_security() == SEC_ADS *) enables name cache for winbind ADS backend *) enable the negative connection cache for winbind ADS backend *) removes some old dead code *) consolidates some duplicate code *) moves the internal_name_resolve() to use an IP/port pair to deal with SRV RR dns replies. The namecache code also supports the IP:port syntax now as well. *) removes 'ads server' and moves the functionality back into 'password server' (which can support "hostname:port" syntax now but works fine with defaults depending on the value of lp_security()) (This used to be commit d7f7fcda425bef380441509734eca33da943c091) --- source3/lib/util_str.c | 59 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 87a8ea2eb1..af8d6aa04c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -38,6 +38,7 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) { const char *s; + char *pbuf; BOOL quoted; size_t len=1; @@ -59,17 +60,18 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) return(False); /* copy over the token */ + pbuf = buff; for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { if (*s == '\"') { quoted = !quoted; } else { len++; - *buff++ = *s; + *pbuf++ = *s; } } *ptr = (*s) ? s+1 : s; - *buff = 0; + *pbuf = 0; return(True); } @@ -1469,6 +1471,7 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) #define IPSTR_LIST_SEP "," +#define IPSTR_LIST_CHAR ',' /** * Add ip string representation to ipstr list. Used also @@ -1483,19 +1486,20 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) * reallocated to new length **/ -char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip) +char* ipstr_list_add(char** ipstr_list, const struct ip_service *service) { char* new_ipstr = NULL; /* arguments checking */ - if (!ipstr_list || !ip) return NULL; + if (!ipstr_list || !service) return NULL; /* attempt to convert ip to a string and append colon separator to it */ if (*ipstr_list) { - asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,inet_ntoa(*ip)); + asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list, IPSTR_LIST_SEP, + inet_ntoa(service->ip), service->port); SAFE_FREE(*ipstr_list); } else { - asprintf(&new_ipstr, "%s", inet_ntoa(*ip)); + asprintf(&new_ipstr, "%s:%d", inet_ntoa(service->ip), service->port); } *ipstr_list = new_ipstr; return *ipstr_list; @@ -1512,7 +1516,7 @@ char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip) * @return pointer to allocated ip string **/ -char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_count) +char* ipstr_list_make(char** ipstr_list, const struct ip_service* ip_list, int ip_count) { int i; @@ -1531,7 +1535,8 @@ char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_c /** * Parse given ip string list into array of ip addresses - * (as in_addr structures) + * (as ip_service structures) + * e.g. 192.168.1.100:389,192.168.1.78, ... * * @param ipstr ip string list to be parsed * @param ip_list pointer to array of ip addresses which is @@ -1539,28 +1544,40 @@ char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_c * @return number of succesfully parsed addresses **/ -int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list) +int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list) { fstring token_str; - int count; + size_t count; + int i; - if (!ipstr_list || !ip_list) return 0; + if (!ipstr_list || !ip_list) + return 0; + + count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; + if ( (*ip_list = (struct ip_service*)malloc(count * sizeof(struct ip_service))) == NULL ) { + DEBUG(0,("ipstr_list_parse: malloc failed for %d entries\n", count)); + return 0; + } - for (*ip_list = NULL, count = 0; - next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN); - count++) { - + for ( i=0; + next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN) && i Date: Wed, 2 Jul 2003 20:01:51 +0000 Subject: Added fix for Japanese case names in statcache - these can change size on upper casing. Based on patch from monyo@home.monyo.com. Jeremy. (This used to be commit 72e382e99b92666acdaf50a040b14aa16d48b80d) --- source3/lib/util_str.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index af8d6aa04c..ac8fccfa5a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1111,6 +1111,26 @@ char *strrchr_m(const char *s, char c) return (char *)(s+strlen(s2)); } +/*********************************************************************** + Return the equivalent of doing strrchr 'n' times - always going + backwards. +***********************************************************************/ + +char *strnrchr_m(const char *s, char c, unsigned int n) +{ + wpstring ws; + pstring s2; + smb_ucs2_t *p; + + push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + p = strnrchr_w(ws, UCS2_CHAR(c), n); + if (!p) + return NULL; + *p = 0; + pull_ucs2_pstring(s2, ws); + return (char *)(s+strlen(s2)); +} + /** Convert a string to lower case. **/ -- cgit From ce72beb2b558d86fb49063c6b1fa00e07952ce56 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 3 Jul 2003 19:11:31 +0000 Subject: Removed strupper/strlower macros that automatically map to strupper_m/strlower_m. I really want people to think about when they're using multibyte strings. Jeremy. (This used to be commit ff222716a08af65d26ad842ce4c2841cc6540959) --- source3/lib/util_str.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index ac8fccfa5a..96fbc3f124 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -314,7 +314,7 @@ char *strupper_static(const char *s) static pstring str; pstrcpy(str, s); - strupper(str); + strupper_m(str); return str; } @@ -327,9 +327,9 @@ void strnorm(char *s) { extern int case_default; if (case_default == CASE_UPPER) - strupper(s); + strupper_m(s); else - strlower(s); + strlower_m(s); } /** -- cgit From e45b66ba8a6b6fd669ce7b4f9f8f84f9c26e1575 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 19 Jul 2003 00:36:43 +0000 Subject: Fix StrCaseCmp() to avoid calling smb_panic() on invalid multibyte strings. This fix results in - we no longer use fixed-size buffers in StrCaseCmp (previously limited to a pstring) - we return strcmp(s, t) if either of the strings is invalid - for non-ascii cases, we call iconv twice, not 4 times. The basic idea with this fix is that if a string is not valid in the currnet charset, then (unless it is byte-equivilant) it cannot be case-equivilant to any other string. This should address the majority of our smb_panic() cases on this matter. It will not fix them all - we still call unix_strupper(), aka strupper_m() elsewhere, but this was being called on every file in the directory when we performed unix_convert(). Tested with the stf unit tests for this routine. Andrew Bartlett (This used to be commit 9918fa73145a22b1d7adf001f0a9cf0e1bda4136) --- source3/lib/util_str.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 96fbc3f124..7bf43de5f3 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -181,7 +181,9 @@ int StrCaseCmp(const char *s, const char *t) { const char * ps, * pt; - pstring buf1, buf2; + size_t size; + smb_ucs2_t *buffer_s, *buffer_t; + int ret; for (ps = s, pt = t; ; ps++, pt++) { char us, ut; @@ -206,16 +208,27 @@ int StrCaseCmp(const char *s, const char *t) return +1; } - /* TODO: Don't do this with a fixed-length buffer. This could - * still be much more efficient. */ - /* TODO: Hardcode a char-by-char comparison for UTF-8, which - * can be much faster. */ - /* TODO: Test case for this! */ - - unix_strupper(ps, strlen(ps)+1, buf1, sizeof(buf1)); - unix_strupper(pt, strlen(pt)+1, buf2, sizeof(buf2)); - - return strcmp(buf1, buf2); + size = convert_string_allocate(CH_UNIX, CH_UCS2, s, strlen(s), + (void **) &buffer_s); + if (size == -1) { + return strcmp(s, t); + /* Not quite the right answer, but finding the right one + under this failure case is expensive, and it's pretty close */ + } + + size = convert_string_allocate(CH_UNIX, CH_UCS2, t, strlen(t), + (void **) &buffer_t); + if (size == -1) { + SAFE_FREE(buffer_s); + return strcmp(s, t); + /* Not quite the right answer, but finding the right one + under this failure case is expensive, and it's pretty close */ + } + + ret = strcasecmp_w(buffer_s, buffer_t); + SAFE_FREE(buffer_s); + SAFE_FREE(buffer_t); + return ret; } -- cgit From 77373f1f8e3b2f61e9bbcd9fadfb83257d390cf2 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Thu, 24 Jul 2003 23:46:27 +0000 Subject: More printf fixes - size_t is long on some architectures. (This used to be commit ba4d334b822248d8ab929c9568533431603d967e) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7bf43de5f3..dca1d19593 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1588,7 +1588,7 @@ int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list) count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; if ( (*ip_list = (struct ip_service*)malloc(count * sizeof(struct ip_service))) == NULL ) { - DEBUG(0,("ipstr_list_parse: malloc failed for %d entries\n", count)); + DEBUG(0,("ipstr_list_parse: malloc failed for %l entries\n", count)); return 0; } -- cgit From 7d833de662b83f026b54a236588da27dd8899630 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 25 Jul 2003 04:24:40 +0000 Subject: More printf portability fixes. Got caught out by some gcc'isms last time. )-: (This used to be commit 59dae1da66a5eb7e128263bd578f167d8746e9f0) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index dca1d19593..bcdcb90e89 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1588,7 +1588,7 @@ int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list) count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; if ( (*ip_list = (struct ip_service*)malloc(count * sizeof(struct ip_service))) == NULL ) { - DEBUG(0,("ipstr_list_parse: malloc failed for %l entries\n", count)); + DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n", (unsigned long)count)); return 0; } -- cgit From 4b3e0268b56042eb72108dbca75ac0bb0aff2514 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sun, 27 Jul 2003 02:40:06 +0000 Subject: Use push_ucs2_allocate(), rather than convert_string_allocate() directly. Remove strdup_upper/strdup_lower from their old file, now that they have been moved to charcnv.c Note that string_replace assumes that s is a pstring. (doco change only) Andrew Bartlett (This used to be commit 6c9056029bb3dfadb244f301598e12e69493fff9) --- source3/lib/util_str.c | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index bcdcb90e89..7569a39e6a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -208,17 +208,15 @@ int StrCaseCmp(const char *s, const char *t) return +1; } - size = convert_string_allocate(CH_UNIX, CH_UCS2, s, strlen(s), - (void **) &buffer_s); - if (size == -1) { + size = push_ucs2_allocate(&buffer_s, s); + if (size == (size_t)-1) { return strcmp(s, t); /* Not quite the right answer, but finding the right one under this failure case is expensive, and it's pretty close */ } - size = convert_string_allocate(CH_UNIX, CH_UCS2, t, strlen(t), - (void **) &buffer_t); - if (size == -1) { + size = push_ucs2_allocate(&buffer_t, t); + if (size == (size_t)-1) { SAFE_FREE(buffer_s); return strcmp(s, t); /* Not quite the right answer, but finding the right one @@ -364,7 +362,7 @@ BOOL strisnormal(const char *s) NOTE: oldc and newc must be 7 bit characters **/ -void string_replace(char *s,char oldc,char newc) +void string_replace(pstring s,char oldc,char newc) { push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); @@ -1168,21 +1166,6 @@ void strlower_m(char *s) unix_strlower(s,strlen(s)+1,s,strlen(s)+1); } -/** - Duplicate convert a string to lower case. -**/ - -char *strdup_lower(const char *s) -{ - char *t = strdup(s); - if (t == NULL) { - DEBUG(0, ("strdup_lower: Out of memory!\n")); - return NULL; - } - strlower_m(t); - return t; -} - /** Convert a string to upper case. **/ @@ -1207,21 +1190,6 @@ void strupper_m(char *s) unix_strupper(s,strlen(s)+1,s,strlen(s)+1); } -/** - Convert a string to upper case. -**/ - -char *strdup_upper(const char *s) -{ - char *t = strdup(s); - if (t == NULL) { - DEBUG(0, ("strdup_upper: Out of memory!\n")); - return NULL; - } - strupper_m(t); - return t; -} - /** Return a RFC2254 binary string representation of a buffer. Used in LDAP filters. -- cgit From 0fc41a8962e86f477891e26c784ded81af75794a Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Fri, 15 Aug 2003 02:25:41 +0000 Subject: get rid of const as these things really are not const (This used to be commit 61bea183a229cc11f25c4a7cb5341faad9833d7b) --- source3/lib/util_str.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7569a39e6a..8d2b996785 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -35,9 +35,9 @@ * Based on a routine by GJC@VILLAGE.COM. * Extensively modified by Andrew.Tridgell@anu.edu.au **/ -BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) +BOOL next_token(char **ptr,char *buff, const char *sep, size_t bufsize) { - const char *s; + char *s; char *pbuf; BOOL quoted; size_t len=1; @@ -82,13 +82,13 @@ parameter so you can pass NULL. This is useful for user interface code but beware the fact that it is not re-entrant! **/ -static const char *last_ptr=NULL; +static char *last_ptr=NULL; -BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) +BOOL next_token_nr(char **ptr,char *buff, const char *sep, size_t bufsize) { BOOL ret; if (!ptr) - ptr = (const char **)&last_ptr; + ptr = (char **)&last_ptr; ret = next_token(ptr, buff, sep, bufsize); last_ptr = *ptr; -- cgit From aa39cc37dab9c4f8c3295d872bb8cc143890b378 Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Fri, 15 Aug 2003 04:42:05 +0000 Subject: get rid of more compiler warnings (This used to be commit 398bd14fc6e2f8ab2f34211270e179b8928a6669) --- source3/lib/util_str.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8d2b996785..eb1c70d412 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -35,7 +35,7 @@ * Based on a routine by GJC@VILLAGE.COM. * Extensively modified by Andrew.Tridgell@anu.edu.au **/ -BOOL next_token(char **ptr,char *buff, const char *sep, size_t bufsize) +BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) { char *s; char *pbuf; @@ -45,7 +45,7 @@ BOOL next_token(char **ptr,char *buff, const char *sep, size_t bufsize) if (!ptr) return(False); - s = *ptr; + s = (char *)*ptr; /* default to simple separators */ if (!sep) @@ -82,13 +82,13 @@ parameter so you can pass NULL. This is useful for user interface code but beware the fact that it is not re-entrant! **/ -static char *last_ptr=NULL; +static const char *last_ptr=NULL; -BOOL next_token_nr(char **ptr,char *buff, const char *sep, size_t bufsize) +BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) { BOOL ret; if (!ptr) - ptr = (char **)&last_ptr; + ptr = &last_ptr; ret = next_token(ptr, buff, sep, bufsize); last_ptr = *ptr; @@ -109,7 +109,7 @@ void set_first_token(char *ptr) char **toktocliplist(int *ctok, const char *sep) { - char *s=last_ptr; + char *s=(char *)last_ptr; int ictok=0; char **ret, **iret; @@ -132,7 +132,7 @@ char **toktocliplist(int *ctok, const char *sep) } while(*s); *ctok=ictok; - s=last_ptr; + s=(char *)last_ptr; if (!(ret=iret=malloc(ictok*sizeof(char *)))) return NULL; -- cgit From 6153bc0f5bbf67d17bcfd21c2e8581defbe3a84f Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 25 Aug 2003 20:42:24 +0000 Subject: fix bug 289; make sure to reset the offset into a string when reallocating space (This used to be commit 66dd20c7eaa66abdf99fa57475ccc4c9f978f1b5) --- source3/lib/util_str.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index eb1c70d412..c356fdcab7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -910,6 +910,7 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) while ((p = strstr(s,pattern))) { if (ld > 0) { + int offset = PTR_DIFF(s,string); char *t = Realloc(string, ls + ld + 1); if (!t) { DEBUG(0, ("realloc_string_sub: out of memory!\n")); @@ -917,7 +918,7 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) return NULL; } string = t; - p = t + (p - s); + p = t + offset + (p - s); } if (li != lp) { memmove(p+li,p+lp,strlen(p+lp)+1); -- cgit From 9c570af3330f1c59b9eb5a47139d6753a1d292e8 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 3 Sep 2003 00:56:43 +0000 Subject: Fix up overlapping memcpy -> memmove found by valgrind. Jeremy. (This used to be commit e0c1460c6b6af2b83ea205d8abeb37c71ca1d4c1) --- source3/lib/util_str.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c356fdcab7..f036d88da0 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -428,7 +428,9 @@ BOOL trim_string(char *s,const char *front,const char *back) if (front_len) { while (len && strncmp(s, front, front_len)==0) { - memcpy(s, s+front_len, (len-front_len)+1); + /* Must use memmove here as src & dest can + * easily overlap. Found by valgrind. JRA. */ + memmove(s, s+front_len, (len-front_len)+1); len -= front_len; ret=True; } -- cgit From 245fbf7efbc42530c81d5aac66681bb892c97557 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 4 Sep 2003 01:12:39 +0000 Subject: Used cachegrind to track down some bottlenecks. Removed calls to clobber_region when not compiling with developer as they were hiding speed problems. Added fast path to convert_string() when dealing with ascii -> ascii, ucs2-le to ascii and ascii to ucs2-le with values <= 0x7F. This gives a speedup of 22% on my nbench tests. Next I will do this on convert_string_allocate. Jeremy. (This used to be commit ef140d15ea0d76a3e7cdcadbfd3e917c210a9411) --- source3/lib/util_str.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f036d88da0..e1db93a131 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -503,7 +503,9 @@ char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_ return NULL; } +#ifdef DEVELOPER clobber_region(fn,line,dest, maxlength+1); +#endif if (!src) { *dest = 0; @@ -542,7 +544,9 @@ char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size src_len = strnlen(src, maxlength + 1); dest_len = strnlen(dest, maxlength + 1); +#ifdef DEVELOPER clobber_region(fn, line, dest + dest_len, maxlength + 1 - dest_len); +#endif if (src_len + dest_len > maxlength) { DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", @@ -569,7 +573,9 @@ char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, con { size_t len, i; +#ifdef DEVELOPER clobber_region(fn, line, dest, maxlength); +#endif if (!dest) { DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n")); @@ -609,7 +615,9 @@ char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) { char *d = dest; +#ifdef DEVELOPER clobber_region(fn, line, dest, n+1); +#endif if (!dest) return(NULL); @@ -639,8 +647,9 @@ static char *strncpyn(char *dest, const char *src, size_t n, char c) char *p; size_t str_len; +#ifdef DEVELOPER clobber_region(dest, n+1); - +#endif p = strchr_m(src, c); if (p == NULL) { DEBUG(5, ("strncpyn: separator character (%c) not found\n", c)); -- cgit From 08c3907f4eb9a6235ff401e39b949dbcc929a7ee Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 4 Sep 2003 23:26:13 +0000 Subject: Fastpath strchr_m for ASCII. Jeremy. (This used to be commit b3176f2ec246441dd483dc9757a487535b1656e6) --- source3/lib/util_str.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e1db93a131..34fdf75f63 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1110,6 +1110,19 @@ char *strchr_m(const char *s, char c) pstring s2; smb_ucs2_t *p; + /* this is quite a common operation, so we want it to be + fast. We optimise for the ascii case, knowing that all our + supported multi-byte character sets are ascii-compatible + (ie. they match for the first 128 chars) */ + + while (*s && !(((unsigned char)s[0]) & 0x7F)) { + if (*s == c) + return s; + } + + if (!*s) + return NULL; + push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); p = strchr_w(ws, UCS2_CHAR(c)); if (!p) -- cgit From ff78c21f51263ea7f6108acddb610bbd775efc87 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 5 Sep 2003 01:33:22 +0000 Subject: Hand optimisatinos for strrchr_m using the properties we know about MB character sets and how we use this call. Jeremy. (This used to be commit a9709700eea3bb48ab4a79d74e0b8d22dc98576f) --- source3/lib/util_str.c | 58 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 34fdf75f63..4556405b04 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1115,7 +1115,7 @@ char *strchr_m(const char *s, char c) supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) { + while (*s && (((unsigned char)s[0]) & 0x80)) { if (*s == c) return s; } @@ -1134,17 +1134,53 @@ char *strchr_m(const char *s, char c) char *strrchr_m(const char *s, char c) { - wpstring ws; - pstring s2; - smb_ucs2_t *p; + /* this is quite a common operation, so we want it to be + fast. We optimise for the ascii case, knowing that all our + supported multi-byte character sets are ascii-compatible + (ie. they match for the first 128 chars). Also, in Samba + we only search for ascii characters in 'c' and that + in all mb character sets with a compound character + containing c, if 'c' is not a match at position + p, then p[-1] > 0x7f. JRA. */ - push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); - p = strrchr_w(ws, UCS2_CHAR(c)); - if (!p) - return NULL; - *p = 0; - pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + { + size_t len = strlen(s); + const char *cp = s; + BOOL got_mb = False; + + if (len == 0) + return NULL; + cp += (len - 1); + do { + if (c == *cp) { + /* Could be a match. Part of a multibyte ? */ + if ((cp > s) && (((unsigned char)cp[-1]) & 0x80)) { + /* Yep - go slow :-( */ + got_mb = True; + break; + } + /* No - we have a match ! */ + return cp; + } + } while (cp-- != s); + if (!got_mb) + return NULL; + } + + /* String contained a non-ascii char. Slow path. */ + { + wpstring ws; + pstring s2; + smb_ucs2_t *p; + + push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + p = strrchr_w(ws, UCS2_CHAR(c)); + if (!p) + return NULL; + *p = 0; + pull_ucs2_pstring(s2, ws); + return (char *)(s+strlen(s2)); + } } /*********************************************************************** -- cgit From 5a74bdd7aaf644fc3de94b26d3c85e088211067a Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 5 Sep 2003 05:32:32 +0000 Subject: fix bug 397: use a variant of alloc_sub_basic() for string lists. (This used to be commit 62d5611df0cf86c267d7fe820822d4d019ae28bd) --- source3/lib/util_str.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 4556405b04..70567f88af 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1470,6 +1470,35 @@ void str_list_free(char ***list) SAFE_FREE(*list); } +/****************************************************************************** + version of standard_sub_basic() for string lists; uses alloc_sub_basic() + for the work + *****************************************************************************/ + +BOOL str_list_sub_basic( char **list, const char *smb_name ) +{ + char *s, *tmpstr; + + while ( *list ) { + s = *list; + tmpstr = alloc_sub_basic(smb_name, s); + if ( !tmpstr ) { + DEBUG(0,("str_list_sub_basic: alloc_sub_basic() return NULL!\n")); + return False; + } + + *list = tmpstr; + + list++; + } + + return True; +} + +/****************************************************************************** + substritute a specific pattern in a string list + *****************************************************************************/ + BOOL str_list_substitute(char **list, const char *pattern, const char *insert) { char *p, *s, *t; @@ -1525,6 +1554,7 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) } } + list++; } -- cgit From 94f59f54921174fc156fade575ca114d331b1bd8 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 5 Sep 2003 19:59:55 +0000 Subject: More tuning from cachegrind. Change most trim_string() calls to trim_char(0, as that's what they do. Fix string_replace() to fast-path ascii. Jeremy. (This used to be commit f35e9a8b909d3c74be47083ccc4a4e91a14938db) --- source3/lib/util_str.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 70567f88af..82b312e241 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -364,9 +364,27 @@ BOOL strisnormal(const char *s) void string_replace(pstring s,char oldc,char newc) { - push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); + unsigned char *p; + + /* this is quite a common operation, so we want it to be + fast. We optimise for the ascii case, knowing that all our + supported multi-byte character sets are ascii-compatible + (ie. they match for the first 128 chars) */ + + for (p = (unsigned char *)s; *p; p++) { + if (*p & 0x80) /* mb string - slow path. */ + break; + if (*p == oldc) + *p = newc; + } + + if (!*p) + return; + + /* Slow (mb) path. */ + push_ucs2(NULL, tmpbuf, p, sizeof(tmpbuf), STR_TERMINATE); string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); - pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); + pull_ucs2(NULL, p, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); } /** @@ -406,6 +424,59 @@ size_t str_ascii_charnum(const char *s) return strlen(tmpbuf2); } +BOOL trim_char(char *s,char cfront,char cback) +{ + BOOL ret = False; + char *ep; + char *fp = s; + + /* Ignore null or empty strings. */ + if (!s || (s[0] == '\0')) + return False; + + if (cfront) { + while (*fp && *fp == cfront) + fp++; + if (!*fp) { + /* We ate the string. */ + s[0] = '\0'; + return True; + } + if (fp != s) + ret = True; + } + + ep = fp + strlen(fp) - 1; + if (cback) { + /* Attempt ascii only. Bail for mb strings. */ + while ((ep >= fp) && (*ep == cback)) { + ret = True; + if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) { + /* Could be mb... bail back to tim_string. */ + char fs[2], bs[2]; + if (cfront) { + fs[0] = cfront; + fs[1] = '\0'; + } + bs[0] = cback; + bs[1] = '\0'; + return trim_string(s, cfront ? fs : NULL, bs); + } else { + ep--; + } + } + if (ep < fp) { + /* We ate the string. */ + s[0] = '\0'; + return True; + } + } + + ep[1] = '\0'; + memmove(s, fp, ep-fp+2); + return ret; +} + /** Trim the specified elements off the front and back of a string. **/ -- cgit From ec9f544561c31487b0fa2888da34b4ee0ec87c6a Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 10 Sep 2003 18:03:24 +0000 Subject: Fix stupid typo bug causing CPU spin. Spotted by Markus Ungermann Jeremy. (This used to be commit c5ed59b37be1bf779e0d0e61c31227b520430afd) --- source3/lib/util_str.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 82b312e241..57131ad873 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1189,6 +1189,7 @@ char *strchr_m(const char *s, char c) while (*s && (((unsigned char)s[0]) & 0x80)) { if (*s == c) return s; + s++; } if (!*s) -- cgit From 76d0a7ca0800c56560bd5c0a78ce790dc8be5c18 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 12 Sep 2003 21:41:18 +0000 Subject: Fix from Benjamin Riefenstahl . Revered condition meant fast-path in strchr_m was not being used. Doh ! Jeremy. (This used to be commit f23c9d36b0cd4083722012e4a94df8295f29d04c) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 57131ad873..c065bfe9db 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1186,7 +1186,7 @@ char *strchr_m(const char *s, char c) supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && (((unsigned char)s[0]) & 0x80)) { + while (*s && !(((unsigned char)s[0]) & 0x80)) { if (*s == c) return s; s++; -- cgit From cba653c30a0ed2382b7d252b5dc82a28247372b3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 13 Sep 2003 22:41:21 +0000 Subject: Fix for MacOS/X which uses STUPID BROKEN UNICODE COMPOSE CHARACTERS ! (rant off :-). Inspired by work from Benjamin Riefenstahl . Also add MacOSX/Darwin configure fixes. Jerry - can we put this in 3.0 release ? :-). Jeremy. (This used to be commit f23acb4ca5feac8ad2acfa1baf7df31283aba3ea) --- source3/lib/util_str.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c065bfe9db..15ac1639a9 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -382,6 +382,10 @@ void string_replace(pstring s,char oldc,char newc) return; /* Slow (mb) path. */ +#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS + /* With compose characters we must restart from the beginning. JRA. */ + p = s; +#endif push_ucs2(NULL, tmpbuf, p, sizeof(tmpbuf), STR_TERMINATE); string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); pull_ucs2(NULL, p, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); @@ -1175,26 +1179,31 @@ char *string_truncate(char *s, unsigned int length) We convert via ucs2 for now. **/ -char *strchr_m(const char *s, char c) +char *strchr_m(const char *src, char c) { wpstring ws; pstring s2; smb_ucs2_t *p; + const char *s; /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x80)) { + for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { if (*s == c) return s; - s++; } if (!*s) return NULL; +#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS + /* With compose characters we must restart from the beginning. JRA. */ + s = src; +#endif + push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); p = strchr_w(ws, UCS2_CHAR(c)); if (!p) -- cgit From fbb8f131c2336e921677f41e9fb8bce7406f3336 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Mon, 3 Nov 2003 14:34:25 +0000 Subject: Fix more 64-bit printf warnings. (This used to be commit 23443e3aa079710221557e18158d0ddb8ff48a36) --- source3/lib/util_str.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 15ac1639a9..b6025a362d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -590,8 +590,9 @@ char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_ len = strnlen(src, maxlength+1); if (len > maxlength) { - DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n", - (unsigned int)(len-maxlength), len, maxlength, src)); + DEBUG(0,("ERROR: string overflow by %lu (%lu - %lu) in safe_strcpy [%.50s]\n", + (unsigned long)(len-maxlength), (unsigned long)len, + (unsigned long)maxlength, src)); len = maxlength; } -- cgit From 0b5019ffc9821d4734524a819ad431700f7ea8f0 Mon Sep 17 00:00:00 2001 From: Richard Sharpe Date: Thu, 13 Nov 2003 17:30:25 +0000 Subject: Squelch some warnings with more casty-foo. (This used to be commit d165a49d860443741e57458b8a819c6d54824fc5) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b6025a362d..c244a6b34b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1194,7 +1194,7 @@ char *strchr_m(const char *src, char c) for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { if (*s == c) - return s; + return (char *)s; } if (!*s) @@ -1242,7 +1242,7 @@ char *strrchr_m(const char *s, char c) break; } /* No - we have a match ! */ - return cp; + return (char *)cp; } } while (cp-- != s); if (!got_mb) -- cgit From 8e76781ff2588c7fb6efd8c0a8d944d81eb131e6 Mon Sep 17 00:00:00 2001 From: Rafal Szczesniak Date: Tue, 18 Nov 2003 19:15:29 +0000 Subject: Useful debug message. Patch by metze. rafal (This used to be commit 8b06364b53ea01ec7a21f3fbe86afad02fe21dd8) --- source3/lib/util_str.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c244a6b34b..a560f61e7c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -695,9 +695,11 @@ char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) clobber_region(fn, line, dest, n+1); #endif - if (!dest) + if (!dest) { + DEBUG(0,("ERROR: NULL dest in StrnCpy\n")); return(NULL); - + } + if (!src) { *dest = 0; return(dest); -- cgit From a63010bae7710a4d24ebbae5f232fa792f005112 Mon Sep 17 00:00:00 2001 From: Rafal Szczesniak Date: Wed, 19 Nov 2003 22:56:02 +0000 Subject: Added useful information to debug lines. Patch by metze. rafal (This used to be commit 2eef3c7bc182bb2c0c483190570ee1a297047ad2) --- source3/lib/util_str.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index a560f61e7c..d2b64aff2e 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -574,7 +574,7 @@ char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_ size_t len; if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcpy\n")); + DEBUG(0,("ERROR: NULL dest in safe_strcpy, called from [%s][%d]\n", fn, line)); return NULL; } @@ -610,7 +610,7 @@ char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size size_t src_len, dest_len; if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcat\n")); + DEBUG(0,("ERROR: NULL dest in safe_strcat, called from [%s][%d]\n", fn, line)); return NULL; } @@ -654,7 +654,7 @@ char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, con #endif if (!dest) { - DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n")); + DEBUG(0,("ERROR: NULL dest in alpha_strcpy, called from [%s][%d]\n", fn, line)); return NULL; } @@ -696,7 +696,7 @@ char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) #endif if (!dest) { - DEBUG(0,("ERROR: NULL dest in StrnCpy\n")); + DEBUG(0,("ERROR: NULL dest in StrnCpy, called from [%s][%d]\n", fn, line)); return(NULL); } -- cgit From 11f4893145f94c85b4f1268544a84116d3a38751 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Sat, 22 Nov 2003 04:33:36 +0000 Subject: Ensure that items in a list of strings containing whitespace are written out surrounded by single quotes. This means that both double and single quotes are now used to surround strings in smb.conf. This is a slight change from the previous behavior but needed or else things like printer admin = +ntadmin, 'VALE\Domain, Admin' get written to smb.conf by SWAT. (This used to be commit 5bf91c79d620e34ac71d72c80f74e47754d49dcb) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d2b64aff2e..2928584b8a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -62,7 +62,7 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) /* copy over the token */ pbuf = buff; for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { - if (*s == '\"') { + if (*s == '\"' || *s == '\'') { quoted = !quoted; } else { len++; -- cgit From 3b386064911dce7c02e40b5cc7a971f2d4b28185 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 25 Dec 2003 09:37:41 +0000 Subject: Fix bug 916 - do not perform a + -> space substitution for squid URL encoded strings, only form input in SWAT. Andrew Bartlett (This used to be commit 8d54f5fe0c5689660f37788916b37014754ce23e) --- source3/lib/util_str.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2928584b8a..f9923bd325 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1780,11 +1780,6 @@ void rfc1738_unescape(char *buf) { char *p=buf; - while ((p=strchr_m(p,'+'))) - *p = ' '; - - p = buf; - while (p && *p && (p=strchr_m(p,'%'))) { int c1 = p[1]; int c2 = p[2]; -- cgit From e82bfa5cf6aa61b3b51db4ff2b683657635038f4 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 7 Jan 2004 23:21:36 +0000 Subject: Fix for bug #922. Fast path not called for strlower_m() and strupper_m(). From ab@samba.org (Alexander Bokovoy). Jeremy. (This used to be commit fac9e6d7125fb9edfade3c92a3cd9e1f2c60cefd) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f9923bd325..d93f39696f 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1298,7 +1298,7 @@ void strlower_m(char *s) supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) { + while (*s && !(((unsigned char)s[0]) & 0x80)) { *s = tolower((unsigned char)*s); s++; } @@ -1322,7 +1322,7 @@ void strupper_m(char *s) supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - while (*s && !(((unsigned char)s[0]) & 0x7F)) { + while (*s && !(((unsigned char)s[0]) & 0x80)) { *s = toupper((unsigned char)*s); s++; } -- cgit From aa7a675025bc63ce0300b0e5bcf4a3a3849d2833 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 23 Jan 2004 12:04:07 +0000 Subject: Fix decoding of base64. We got the length wrong when the result was not an exact multiple of 3. I also wrote a torture test and it survived some minutes of random stuff coded/decoded up to 16 MB data. But that would be a bit too embarassing to commit... :-) Volker (This used to be commit 6d22f0d8c36bea618ad0adf4eefc2f5a37cb3fda) --- source3/lib/util_str.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index d93f39696f..822ab20628 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1838,6 +1838,8 @@ DATA_BLOB base64_decode_data_blob(const char *s) s++; i++; } + if (*s == '=') n -= 1; + /* fix up length */ decoded.length = n; return decoded; -- cgit From da371e74bb3299fbf951a6b1f373daff7a2c6018 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 4 Feb 2004 20:28:51 +0000 Subject: Fix final valgrind errors with #830. Catch mb conversion error that may not terminate correctly. Jeremy. (This used to be commit 49142c6352eb3645437ef86bcedca1b1895aef60) --- source3/lib/util_str.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 822ab20628..2d1f596c97 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1293,6 +1293,8 @@ char *strnrchr_m(const char *s, char c, unsigned int n) void strlower_m(char *s) { + size_t len; + /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible @@ -1308,7 +1310,12 @@ void strlower_m(char *s) /* I assume that lowercased string takes the same number of bytes * as source string even in UTF-8 encoding. (VIV) */ - unix_strlower(s,strlen(s)+1,s,strlen(s)+1); + len = strlen(s) + 1; + errno = 0; + unix_strlower(s,len,s,len); + /* Catch mb conversion errors that may not terminate. */ + if (errno) + s[len-1] = '\0'; } /** @@ -1317,6 +1324,8 @@ void strlower_m(char *s) void strupper_m(char *s) { + size_t len; + /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible @@ -1332,7 +1341,12 @@ void strupper_m(char *s) /* I assume that lowercased string takes the same number of bytes * as source string even in multibyte encoding. (VIV) */ - unix_strupper(s,strlen(s)+1,s,strlen(s)+1); + len = strlen(s) + 1; + errno = 0; + unix_strupper(s,len,s,len); + /* Catch mb conversion errors that may not terminate. */ + if (errno) + s[len-1] = '\0'; } /** -- cgit From e3d755c5b2eff14486b80ca9a4bc7857aaf1c86c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 13 Feb 2004 22:06:25 +0000 Subject: Added Andrew Bartlett's patch to use an allocated buffer for count_chars. Jeremy. (This used to be commit cdbeb7d2ebcd4a298aabb3ed665560d219bb0d1c) --- source3/lib/util_str.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2d1f596c97..cde6b2e7a1 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -557,10 +557,16 @@ size_t count_chars(const char *s,char c) { smb_ucs2_t *ptr; int count; - push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(count=0,ptr=tmpbuf;*ptr;ptr++) + smb_ucs2_t *alloc_tmpbuf; + + if (push_ucs2_allocate(&alloc_tmpbuf, s) == (size_t)-1) { + return 0; + } + + for(count=0,ptr=alloc_tmpbuf;*ptr;ptr++) if(*ptr==UCS2_CHAR(c)) count++; + return(count); } -- cgit From 142ef0e8294fcdd5f9719a59cf83c222418f36d9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 13 Feb 2004 22:34:29 +0000 Subject: Missed SAFE_FREE (typo). Jeremy. (This used to be commit ac1d03c05bf6247541fbd6f0c3bd5b2e5b6a5212) --- source3/lib/util_str.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index cde6b2e7a1..71c8d56e40 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -557,7 +557,7 @@ size_t count_chars(const char *s,char c) { smb_ucs2_t *ptr; int count; - smb_ucs2_t *alloc_tmpbuf; + smb_ucs2_t *alloc_tmpbuf = NULL; if (push_ucs2_allocate(&alloc_tmpbuf, s) == (size_t)-1) { return 0; @@ -567,6 +567,7 @@ size_t count_chars(const char *s,char c) if(*ptr==UCS2_CHAR(c)) count++; + SAFE_FREE(alloc_tmpbuf); return(count); } -- cgit From c9b7cbbfa572512cd0348817965b99fd1df01285 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 9 Mar 2004 00:17:14 +0000 Subject: Added strstr_m() function. Use in all places where we might run into mb (should fix the mb service name problem, can't remember the bugid). Jeremy. (This used to be commit 94a272b9a881ec0004c5da2a7242b0a818da5630) --- source3/lib/util_str.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 71c8d56e40..cad0df48a4 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -919,7 +919,7 @@ void string_sub(char *s,const char *pattern, const char *insert, size_t len) if (len == 0) len = ls + 1; /* len is number of *bytes* */ - while (lp <= ls && (p = strstr(s,pattern))) { + while (lp <= ls && (p = strstr_m(s,pattern))) { if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), @@ -1004,7 +1004,7 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) } } - while ((p = strstr(s,pattern))) { + while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); char *t = Realloc(string, ls + ld + 1); @@ -1052,7 +1052,7 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) if (len == 0) len = ls + 1; /* len is number of *bytes* */ - while (lp <= ls && (p = strstr(s,pattern))) { + while (lp <= ls && (p = strstr_m(s,pattern))) { if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), @@ -1294,6 +1294,76 @@ char *strnrchr_m(const char *s, char c, unsigned int n) return (char *)(s+strlen(s2)); } +/*********************************************************************** + strstr_m - We convert via ucs2 for now. +***********************************************************************/ + +char *strstr_m(const char *src, const char *findstr) +{ + smb_ucs2_t *p; + smb_ucs2_t *src_w, *find_w; + const char *s; + char *s2; + char *retp; + + /* Samba does single character findstr calls a *lot*. */ + if (findstr[1] == '\0') + return strchr_m(src, *findstr); + + /* We optimise for the ascii case, knowing that all our + supported multi-byte character sets are ascii-compatible + (ie. they match for the first 128 chars) */ + + for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { + if (*s == *findstr) { + if (strcmp(s, findstr) == 0) { + return (char *)s; + } + } + } + + if (!*s) + return NULL; + +#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS + /* With compose characters we must restart from the beginning. JRA. */ + s = src; +#endif + + if (push_ucs2_allocate(&src_w, src) == (size_t)-1) { + DEBUG(0,("strstr_m: src malloc fail\n")); + return NULL; + } + + if (push_ucs2_allocate(&find_w, findstr) == (size_t)-1) { + SAFE_FREE(src_w); + DEBUG(0,("strstr_m: find malloc fail\n")); + return NULL; + } + + for (p = src_w; (p = strchr_w(p, *find_w)) != NULL; p++) { + if (strcmp_w(p, find_w) == 0) + break; + } + if (!p) { + SAFE_FREE(src_w); + SAFE_FREE(find_w); + return NULL; + } + *p = 0; + if (pull_ucs2_allocate(&s2, src_w) == (size_t)-1) { + SAFE_FREE(src_w); + SAFE_FREE(find_w); + DEBUG(0,("strstr_m: dest malloc fail\n")); + return NULL; + } + retp = (char *)(s+strlen(s2)); + SAFE_FREE(src_w); + SAFE_FREE(find_w); + SAFE_FREE(s2); + return retp; +} + /** Convert a string to lower case. **/ @@ -1624,7 +1694,7 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) s = *list; ls = (ssize_t)strlen(s); - while ((p = strstr(s, pattern))) { + while ((p = strstr_m(s, pattern))) { t = *list; d = p -t; if (ld) { -- cgit From 151faf6935d54efcc3b5bcf8e60419b490ac460d Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 9 Mar 2004 09:56:33 +0000 Subject: JRA's recent strstr_m work really badly broke our string_sub code. For example: strstr_m("%v foo bar", "%v") would fail... only strstr_m("foo %v", "%v") could work. I wonder what else this broke... Fix is to move to using strncmp() inside the strstr_m function. Tested on ASCII only. Andrew Bartlett (This used to be commit 44d304f84c4ba5a832d5e3848ae0d04d5438ac15) --- source3/lib/util_str.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index cad0df48a4..7f3c30f61e 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1306,6 +1306,9 @@ char *strstr_m(const char *src, const char *findstr) char *s2; char *retp; + size_t findstr_len = 0; + size_t find_w_len; + /* Samba does single character findstr calls a *lot*. */ if (findstr[1] == '\0') return strchr_m(src, *findstr); @@ -1316,7 +1319,10 @@ char *strstr_m(const char *src, const char *findstr) for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { if (*s == *findstr) { - if (strcmp(s, findstr) == 0) { + if (!findstr_len) + findstr_len = strlen(findstr); + + if (strncmp(s, findstr, findstr_len) == 0) { return (char *)s; } } @@ -1341,8 +1347,10 @@ char *strstr_m(const char *src, const char *findstr) return NULL; } + find_w_len = strlen_w(find_w); + for (p = src_w; (p = strchr_w(p, *find_w)) != NULL; p++) { - if (strcmp_w(p, find_w) == 0) + if (strncmp_w(p, find_w, find_w_len) == 0) break; } if (!p) { -- cgit From 32665c36c88d9f650baa33248275b711799fd300 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 9 Mar 2004 11:15:44 +0000 Subject: Given how core this code is, I figure it should have it's own testsuite. Big thanks to tpot and mbp for showing how easy it can be to write a simple unit test, and for providing the STF. This also changes the strstr_m() code to use strstr_w() (avoiding duplication) and fixes it so that it passes the STF. (We now always restart before doing the unicode run, until sombody can show me why the testsuite is wrong). Andrew Bartlett (This used to be commit a893a324f37e6a171719db8ffffe66df31c2dbaa) --- source3/lib/util_str.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7f3c30f61e..b8cf052862 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1309,6 +1309,11 @@ char *strstr_m(const char *src, const char *findstr) size_t findstr_len = 0; size_t find_w_len; + /* for correctness */ + if (!findstr[0]) { + return src; + } + /* Samba does single character findstr calls a *lot*. */ if (findstr[1] == '\0') return strchr_m(src, *findstr); @@ -1331,7 +1336,9 @@ char *strstr_m(const char *src, const char *findstr) if (!*s) return NULL; -#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS +#if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */ + /* 'make check' fails unless we do this */ + /* With compose characters we must restart from the beginning. JRA. */ s = src; #endif @@ -1346,18 +1353,15 @@ char *strstr_m(const char *src, const char *findstr) DEBUG(0,("strstr_m: find malloc fail\n")); return NULL; } - - find_w_len = strlen_w(find_w); - for (p = src_w; (p = strchr_w(p, *find_w)) != NULL; p++) { - if (strncmp_w(p, find_w, find_w_len) == 0) - break; - } + p = strstr_w(src_w, find_w); + if (!p) { SAFE_FREE(src_w); SAFE_FREE(find_w); return NULL; } + *p = 0; if (pull_ucs2_allocate(&s2, src_w) == (size_t)-1) { SAFE_FREE(src_w); -- cgit From 6b9dbbcd249360fb9acd61d6900baccf621c9cce Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 13 Mar 2004 02:16:21 +0000 Subject: Modified fix for bugid #784. Based on a patch from moriyama@miraclelinux.com (MORIYAMA Masayuki). Don't use nstrings to hold workgroup and netbios names. The problem with them is that MB netbios and workgroup names in unix charset (particularly utf8) may be up to 3x bigger than the name when represented in dos charset (ie. cp932). So go back to using fstrings for these but translate into nstrings (ie. 16 byte length values) for transport on the wire. Jeremy. (This used to be commit b4ea493599ab414f7828b83f40a5a8b43479ff64) --- source3/lib/util_str.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b8cf052862..2be8b7eb64 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1307,7 +1307,6 @@ char *strstr_m(const char *src, const char *findstr) char *retp; size_t findstr_len = 0; - size_t find_w_len; /* for correctness */ if (!findstr[0]) { -- cgit From 82285f2e0efe2b2c741268f8bbe80510e5b00f28 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Wed, 7 Apr 2004 09:27:50 +0000 Subject: r104: Fix ntlm_auth by adding the new strhex_to_data_blob() call. Andrew Bartlett (This used to be commit 0693b9e79fabd58491f8aaec11dbbc71fab34f80) --- source3/lib/util_str.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2be8b7eb64..e4b07a4b73 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -794,6 +794,17 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) return num_chars; } +DATA_BLOB strhex_to_data_blob(const char *strhex) +{ + DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1); + + ret_blob.length = strhex_to_str(ret_blob.data, + strlen(strhex), + strhex); + + return ret_blob; +} + /** * Routine to print a buffer as HEX digits, into an allocated string. */ -- cgit From 7af3777ab32ee220700ed3367d07ca18b2bbdd47 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 7 Apr 2004 12:43:44 +0000 Subject: r116: volker's patch for local group and group nesting (This used to be commit b393469d9581f20e4d4c52633b952ee984cca36f) --- source3/lib/util_str.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e4b07a4b73..600c830ace 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2038,3 +2038,21 @@ SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) return val; } + +void string_append(char **left, const char *right) +{ + int new_len = strlen(right) + 1; + + if (*left == NULL) { + *left = malloc(new_len); + *left[0] = '\0'; + } else { + new_len += strlen(*left); + *left = Realloc(*left, new_len); + } + + if (*left == NULL) + return; + + safe_strcat(*left, right, new_len-1); +} -- cgit From e0da56a84808c522bc7324b5d636f1cbd317a2c5 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 7 May 2004 18:37:47 +0000 Subject: r570: Remove lots of globals to handle case issues - move them to connection struct entries (as they should have been from the start). Jerry, once you've cut over to 3.0.4 release branch I'll add this to 3.0 also. - Jerry cut over :-). Jeremy. (This used to be commit 578a508509d21226ad3332fc54c3ab54cd8ae452) --- source3/lib/util_str.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 600c830ace..65ef306ed1 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -334,9 +334,8 @@ char *strupper_static(const char *s) Convert a string to "normal" form. **/ -void strnorm(char *s) +void strnorm(char *s, int case_default) { - extern int case_default; if (case_default == CASE_UPPER) strupper_m(s); else @@ -347,9 +346,8 @@ void strnorm(char *s) Check if a string is in "normal" case. **/ -BOOL strisnormal(const char *s) +BOOL strisnormal(const char *s, int case_default) { - extern int case_default; if (case_default == CASE_UPPER) return(!strhaslower(s)); -- cgit From 7e6734a0dd50c11601c60e86dee202004b2d6a90 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 8 Jun 2004 20:10:26 +0000 Subject: r1087: BUG 1221: revert old change that used single and double quotes as delimters in next_token(), and change print_parameter() to print out parm values surrounded by double quotes (instead of single quotes) (This used to be commit b0739b073a1db8b0b163726a1d181b2f05d71883) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 65ef306ed1..7c5fa11c92 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -62,7 +62,7 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) /* copy over the token */ pbuf = buff; for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { - if (*s == '\"' || *s == '\'') { + if ( *s == '\"' ) { quoted = !quoted; } else { len++; -- cgit From 0c6d7f28d6c1066cc6b3055ebf6a8fcf685ca1f6 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Thu, 22 Jul 2004 13:39:43 +0000 Subject: r1570: merging changes from 3.0.5 (This used to be commit 430cf63b9148441bce42bfb15a8045de5da108f4) --- source3/lib/util_str.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7c5fa11c92..1083076edd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1949,7 +1949,9 @@ DATA_BLOB base64_decode_data_blob(const char *s) s++; i++; } - if (*s == '=') n -= 1; + if ((n > 0) && (*s == '=')) { + n -= 1; + } /* fix up length */ decoded.length = n; @@ -1962,9 +1964,15 @@ DATA_BLOB base64_decode_data_blob(const char *s) void base64_decode_inplace(char *s) { DATA_BLOB decoded = base64_decode_data_blob(s); - memcpy(s, decoded.data, decoded.length); - /* null terminate */ - s[decoded.length] = '\0'; + + if ( decoded.length != 0 ) { + memcpy(s, decoded.data, decoded.length); + + /* null terminate */ + s[decoded.length] = '\0'; + } else { + *s = '\0'; + } data_blob_free(&decoded); } -- cgit From 804cfb20a067b4b687089dc72a8271b3abf20f31 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Wed, 25 Aug 2004 14:24:16 +0000 Subject: r2070: Let's try to overload srnlen and strndup for AIX where they are natly broken. (This used to be commit 98feb3318f54bb48ce56fc8f4721fec4967b9dd9) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1083076edd..3e6de23e55 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1502,7 +1502,7 @@ int fstr_sprintf(fstring s, const char *fmt, ...) } -#ifndef HAVE_STRNDUP +#if !defined(HAVE_STRNDUP) || defined(BROKEN_STRNDUP) /** Some platforms don't have strndup. **/ @@ -1522,7 +1522,7 @@ int fstr_sprintf(fstring s, const char *fmt, ...) } #endif -#ifndef HAVE_STRNLEN +#if !defined(HAVE_STRNLEN) || defined(BROKEN_STRNLEN) /** Some platforms don't have strnlen **/ -- cgit From 58a146033012183f44a045b6c6a35c7c74dbf7dd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 30 Aug 2004 18:27:07 +0000 Subject: r2111: Fix memleak with valid names. Jeremy. (This used to be commit 3f0707132afc66109701766528cb0bd1e1c7cd8c) --- source3/lib/util_str.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 3e6de23e55..250d6ed6a0 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1682,6 +1682,7 @@ BOOL str_list_sub_basic( char **list, const char *smb_name ) return False; } + SAFE_FREE(*list); *list = tmpstr; list++; -- cgit From c5b11b56aa3c54193f7c16356f1a944354c89eb1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 1 Sep 2004 17:39:27 +0000 Subject: r2175: Fix for #1546 from fumiya@samba.gr.jp. Preserve errno in MB strupper_m/strlower_m. Jeremy. (This used to be commit 615aa6e914e6bc3691156a3b80244fc98d8ecc56) --- source3/lib/util_str.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 250d6ed6a0..6eee62c14a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1391,6 +1391,7 @@ char *strstr_m(const char *src, const char *findstr) void strlower_m(char *s) { size_t len; + int errno_save; /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our @@ -1408,11 +1409,13 @@ void strlower_m(char *s) /* I assume that lowercased string takes the same number of bytes * as source string even in UTF-8 encoding. (VIV) */ len = strlen(s) + 1; + errno_save = errno; errno = 0; unix_strlower(s,len,s,len); /* Catch mb conversion errors that may not terminate. */ if (errno) s[len-1] = '\0'; + errno = errno_save; } /** @@ -1422,6 +1425,7 @@ void strlower_m(char *s) void strupper_m(char *s) { size_t len; + int errno_save; /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our @@ -1439,11 +1443,13 @@ void strupper_m(char *s) /* I assume that lowercased string takes the same number of bytes * as source string even in multibyte encoding. (VIV) */ len = strlen(s) + 1; + errno_save = errno; errno = 0; unix_strupper(s,len,s,len); /* Catch mb conversion errors that may not terminate. */ if (errno) s[len-1] = '\0'; + errno = errno_save; } /** -- cgit From 3ef0710fa4f487e8887a8c16714c6443009e4a47 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 15 Sep 2004 22:49:23 +0000 Subject: r2361: Fix the appalling toktocliplist() fn. Bug found by Luis Benvenutto. Jeremy. (This used to be commit d434d8e2b47bc8553ee04bd7211f622e3fcbb7fb) --- source3/lib/util_str.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6eee62c14a..65a616ad41 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -134,17 +134,20 @@ char **toktocliplist(int *ctok, const char *sep) *ctok=ictok; s=(char *)last_ptr; - if (!(ret=iret=malloc(ictok*sizeof(char *)))) + if (!(ret=iret=malloc((ictok+1)*sizeof(char *)))) return NULL; while(ictok--) { *iret++=s; - while(*s++) - ; - while(!*s) - s++; + if (ictok > 0) { + while(*s++) + ; + while(!*s) + s++; + } } + ret[*ctok] = NULL; return ret; } -- cgit From b2cd6300d7a575ea01758233ab42b3bf205d20e1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 24 Sep 2004 01:32:19 +0000 Subject: r2578: Pick up optimisation from Samba4 - thanks tridge ! - I recently found out that charaters below 0x3F are guaranteed not to occur as secondary bytes in any multi-byte character set. This allows for a very simple optimisation in strchr_m() and strrchr_m(). It might be a good idea to pick this up for Samba3. Jeremy. (This used to be commit 0465e2d23d27e535bad4652037b37041049bfa96) --- source3/lib/util_str.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 65a616ad41..2c0cae1d73 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1208,6 +1208,12 @@ char *strchr_m(const char *src, char c) smb_ucs2_t *p; const char *s; + /* characters below 0x3F are guaranteed to not appear in + non-initial position in multi-byte charsets */ + if ((c & 0xC0) == 0) { + return strchr(s, c); + } + /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible @@ -1237,6 +1243,12 @@ char *strchr_m(const char *src, char c) char *strrchr_m(const char *s, char c) { + /* characters below 0x3F are guaranteed to not appear in + non-initial position in multi-byte charsets */ + if ((c & 0xC0) == 0) { + return strrchr(s, c); + } + /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible -- cgit From bd5a0ed2f66454b2d298d7fec6a9ede2ca87aef1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 24 Sep 2004 20:06:13 +0000 Subject: r2605: Fix stupid typo in back-port of Samba4 fix. Jeremy. (This used to be commit ca9516520ffe007a7dccd4d6cbf1c5d77fc4ce29) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2c0cae1d73..a758aece4c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1211,7 +1211,7 @@ char *strchr_m(const char *src, char c) /* characters below 0x3F are guaranteed to not appear in non-initial position in multi-byte charsets */ if ((c & 0xC0) == 0) { - return strchr(s, c); + return strchr(src, c); } /* this is quite a common operation, so we want it to be -- cgit From acf9d61421faa6c0055d57fdee7db300dc5431aa Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/util_str.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index a758aece4c..c6b6570f5c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -134,7 +134,7 @@ char **toktocliplist(int *ctok, const char *sep) *ctok=ictok; s=(char *)last_ptr; - if (!(ret=iret=malloc((ictok+1)*sizeof(char *)))) + if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1))) return NULL; while(ictok--) { @@ -815,7 +815,7 @@ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer) int i; char *hex_buffer; - *out_hex_buffer = smb_xmalloc((len*2)+1); + *out_hex_buffer = SMB_XMALLOC_ARRAY(char, (len*2)+1); hex_buffer = *out_hex_buffer; for (i = 0; i < len; i++) @@ -863,7 +863,7 @@ static BOOL string_init(char **dest,const char *src) if (l == 0) { if (!null_string) { - if((null_string = (char *)malloc(1)) == NULL) { + if((null_string = (char *)SMB_MALLOC(1)) == NULL) { DEBUG(0,("string_init: malloc fail for null_string.\n")); return False; } @@ -871,7 +871,7 @@ static BOOL string_init(char **dest,const char *src) } *dest = null_string; } else { - (*dest) = strdup(src); + (*dest) = SMB_STRDUP(src); if ((*dest) == NULL) { DEBUG(0,("Out of memory in string_init\n")); return False; @@ -990,7 +990,7 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) s = string; - in = strdup(insert); + in = SMB_STRDUP(insert); if (!in) { DEBUG(0, ("realloc_string_sub: out of memory!\n")); return NULL; @@ -1019,7 +1019,7 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); - char *t = Realloc(string, ls + ld + 1); + char *t = SMB_REALLOC(string, ls + ld + 1); if (!t) { DEBUG(0, ("realloc_string_sub: out of memory!\n")); SAFE_FREE(in); @@ -1110,7 +1110,7 @@ static smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *patte } } - r = rp = (smb_ucs2_t *)malloc((lt + 1)*(sizeof(smb_ucs2_t))); + r = rp = SMB_MALLOC_ARRAY(smb_ucs2_t, lt + 1); if (!r) { DEBUG(0, ("all_string_sub_w: out of memory!\n")); return NULL; @@ -1478,7 +1478,7 @@ char *binary_string(char *buf, int len) char *s; int i, j; const char *hex = "0123456789ABCDEF"; - s = malloc(len * 3 + 1); + s = SMB_MALLOC(len * 3 + 1); if (!s) return NULL; for (j=i=0;i Date: Wed, 22 Dec 2004 22:07:04 +0000 Subject: r4334: Fix for bugid #2186 - from Buck Huppmann to prevent uninitialized creds being freed. Jeremy. (This used to be commit c3f9c81a8fcb26f7110f75b3096d5d1eb30aac13) --- source3/lib/util_str.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c6b6570f5c..6ebada94d7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1527,6 +1527,9 @@ int fstr_sprintf(fstring s, const char *fmt, ...) /** Some platforms don't have strndup. **/ +#if defined(PARANOID_MALLOC_CHECKER) +#undef strndup +#endif char *strndup(const char *s, size_t n) { @@ -1541,6 +1544,11 @@ int fstr_sprintf(fstring s, const char *fmt, ...) return ret; } + +#if defined(PARANOID_MALLOC_CHECKER) +#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY +#endif + #endif #if !defined(HAVE_STRNLEN) || defined(BROKEN_STRNLEN) -- cgit From ff909274787a92fcdb0ed36bab097f7d2ae07036 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Sat, 15 Jan 2005 03:54:03 +0000 Subject: r4746: add server support for lsa_enum_acct_rights(); last checkin for the night (This used to be commit ccdff4a998405544433aa32938963e4c37962fcc) --- source3/lib/util_str.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6ebada94d7..6b6581b4a7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2092,3 +2092,19 @@ void string_append(char **left, const char *right) safe_strcat(*left, right, new_len-1); } + +BOOL add_string_to_array(TALLOC_CTX *mem_ctx, + const char *str, const char ***strings, + int *num) +{ + char *dup_str = talloc_strdup(mem_ctx, str); + + *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings, const char *, (*num)+1); + + if ((*strings == NULL) || (dup_str == NULL)) + return False; + + (*strings)[*num] = dup_str; + *num += 1; + return True; +} -- cgit From 8ac31b503433ae6150da9523e58c338aff27e561 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 28 Jan 2005 21:55:45 +0000 Subject: r5066: A couple of small fixes from James Peach @ SGI. Jeremy. (This used to be commit 9d131e94195df79e07c8fad20e12ba1b67441a81) --- source3/lib/util_str.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6b6581b4a7..394c8e27cf 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1558,8 +1558,8 @@ int fstr_sprintf(fstring s, const char *fmt, ...) size_t strnlen(const char *s, size_t n) { - int i; - for (i=0; s[i] && i Date: Tue, 1 Feb 2005 18:24:39 +0000 Subject: r5158: BUG 2263: patch from Timur Bakeyev to guard base64_encode_data_blob() against empty blobs (This used to be commit 17239d609f63ae5bd6826e580876c27e8c92d6fa) --- source3/lib/util_str.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 394c8e27cf..f99c2d1fb3 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2016,10 +2016,16 @@ char * base64_encode_data_blob(DATA_BLOB data) { int bits = 0; int char_count = 0; - size_t out_cnt = 0; - size_t len = data.length; - size_t output_len = data.length * 2; - char *result = SMB_MALLOC(output_len); /* get us plenty of space */ + size_t out_cnt, len, output_len; + char *result; + + if (!data.length || !data.data) + return NULL; + + out_cnt = 0; + len = data.length; + output_len = data.length * 2; + result = SMB_MALLOC(output_len); /* get us plenty of space */ while (len-- && out_cnt < (data.length * 2) - 5) { int c = (unsigned char) *(data.data++); -- cgit From 0d579953042b5c361ead51f57957accb3706e3f0 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 22 Mar 2005 15:39:24 +0000 Subject: r5953: more compiler cleanups; moved SID_LIST from smb.h to privileges.c to cleanup the name space (This used to be commit 7dfafa712deb115e425c7367296400c54827a217) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f99c2d1fb3..03e9306805 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1334,7 +1334,7 @@ char *strstr_m(const char *src, const char *findstr) /* for correctness */ if (!findstr[0]) { - return src; + return (char*)src; } /* Samba does single character findstr calls a *lot*. */ -- cgit From 7c198517dab63158cd4035e879a6b75db0b54b14 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Tue, 22 Mar 2005 15:45:38 +0000 Subject: r5954: Fix some compiler warnings and add missing exclude-block in "net rpc share migrate" (found by Lars Mueller ). Guenther (This used to be commit 45a2a7bedb877745cd9677fe3124d5a2ad2c8853) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 03e9306805..6ec43a963b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -826,7 +826,7 @@ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer) Check if a string is part of a list. **/ -BOOL in_list(char *s,char *list,BOOL casesensitive) +BOOL in_list(const char *s, const char *list, BOOL casesensitive) { pstring tok; const char *p=list; -- cgit From 73d1950c010777605b1294397002cc7aa011add0 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 22 Mar 2005 16:39:09 +0000 Subject: r5956: more compile warngin fixes from the Mr. Mader (This used to be commit f3f315b14d261fa56ab040db036a6f858ac06e65) --- source3/lib/util_str.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6ec43a963b..86015b355a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -363,16 +363,16 @@ BOOL strisnormal(const char *s, int case_default) NOTE: oldc and newc must be 7 bit characters **/ -void string_replace(pstring s,char oldc,char newc) +void string_replace( pstring s, char oldc, char newc ) { - unsigned char *p; + char *p; /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our supported multi-byte character sets are ascii-compatible (ie. they match for the first 128 chars) */ - for (p = (unsigned char *)s; *p; p++) { + for (p = s; *p; p++) { if (*p & 0x80) /* mb string - slow path. */ break; if (*p == oldc) -- cgit From 93e04e941e15034c8e7aa1faedc74ce536049153 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 22 Mar 2005 18:07:58 +0000 Subject: r5961: final round of compiler warning fixes based on feedback from Jason Mader (This used to be commit 9e77da9320c900b3e437d534e31fa5ff81e9acfd) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 86015b355a..8acdab355a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -799,7 +799,7 @@ DATA_BLOB strhex_to_data_blob(const char *strhex) { DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1); - ret_blob.length = strhex_to_str(ret_blob.data, + ret_blob.length = strhex_to_str((char*)ret_blob.data, strlen(strhex), strhex); -- cgit From 5d1cb8e79edea9e8581d3c2c9dd297310cd9a98c Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 23 Mar 2005 23:26:33 +0000 Subject: r6014: rather large change set.... pulling back all recent rpc changes from trunk into 3.0. I've tested a compile and so don't think I've missed any files. But if so, just mail me and I'll clean backup in a couple of hours. Changes include \winreg, \eventlog, \svcctl, and general parse_misc.c updates. I am planning on bracketing the event code with an #ifdef ENABLE_EVENTLOG until I finish merging Marcin's changes (very soon). (This used to be commit 4e0ac63c36527cd8c52ef720cae17e84f67e7221) --- source3/lib/util_str.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8acdab355a..b13ec1f0da 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1694,6 +1694,20 @@ void str_list_free(char ***list) SAFE_FREE(*list); } +/****************************************************************************** + *****************************************************************************/ + +int str_list_count( const char **list ) +{ + int i = 0; + + /* count the number of list members */ + + for ( i=0; *list; i++, list++ ); + + return i; +} + /****************************************************************************** version of standard_sub_basic() for string lists; uses alloc_sub_basic() for the work -- cgit From 9840db418bad5a39edc4a32a1786f5e2d2c9dff8 Mon Sep 17 00:00:00 2001 From: Derrell Lipman Date: Thu, 31 Mar 2005 05:06:04 +0000 Subject: r6149: Fixes bugs #2498 and 2484. 1. using smbc_getxattr() et al, one may now request all access control entities in the ACL without getting all other NT attributes. 2. added the ability to exclude specified attributes from the result set provided by smbc_getxattr() et al, when requesting all attributes, all NT attributes, or all DOS attributes. 3. eliminated all compiler warnings, including when --enable-developer compiler flags are in use. removed -Wcast-qual flag from list, as that is specifically to force warnings in the case of casting away qualifiers. Note: In the process of eliminating compiler warnings, a few nasties were discovered. In the file libads/sasl.c, PRIVATE kerberos interfaces are being used; and in libsmb/clikrb5.c, both PRIAVE and DEPRECATED kerberos interfaces are being used. Someone who knows kerberos should look at these and determine if there is an alternate method of accomplishing the task. (This used to be commit 994694f7f26da5099f071e1381271a70407f33bb) --- source3/lib/util_str.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b13ec1f0da..12ee3dc162 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -45,7 +45,7 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) if (!ptr) return(False); - s = (char *)*ptr; + s = CONST_DISCARD(char *, *ptr); /* default to simple separators */ if (!sep) @@ -109,7 +109,7 @@ void set_first_token(char *ptr) char **toktocliplist(int *ctok, const char *sep) { - char *s=(char *)last_ptr; + char *s = CONST_DISCARD(char *, last_ptr); int ictok=0; char **ret, **iret; @@ -132,7 +132,7 @@ char **toktocliplist(int *ctok, const char *sep) } while(*s); *ctok=ictok; - s=(char *)last_ptr; + s = CONST_DISCARD(char *, last_ptr); if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1))) return NULL; @@ -1221,7 +1221,7 @@ char *strchr_m(const char *src, char c) for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { if (*s == c) - return (char *)s; + return CONST_DISCARD(char *, s); } if (!*s) @@ -1238,7 +1238,7 @@ char *strchr_m(const char *src, char c) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + return CONST_DISCARD(char *, (s+strlen(s2))); } char *strrchr_m(const char *s, char c) @@ -1275,7 +1275,7 @@ char *strrchr_m(const char *s, char c) break; } /* No - we have a match ! */ - return (char *)cp; + return CONST_DISCARD(char *, cp); } } while (cp-- != s); if (!got_mb) @@ -1294,7 +1294,7 @@ char *strrchr_m(const char *s, char c) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + return CONST_DISCARD(char *, (s+strlen(s2))); } } @@ -1315,7 +1315,7 @@ char *strnrchr_m(const char *s, char c, unsigned int n) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + return CONST_DISCARD(char *, (s+strlen(s2))); } /*********************************************************************** @@ -1334,7 +1334,7 @@ char *strstr_m(const char *src, const char *findstr) /* for correctness */ if (!findstr[0]) { - return (char*)src; + return CONST_DISCARD(char *, src); } /* Samba does single character findstr calls a *lot*. */ @@ -1351,7 +1351,7 @@ char *strstr_m(const char *src, const char *findstr) findstr_len = strlen(findstr); if (strncmp(s, findstr, findstr_len) == 0) { - return (char *)s; + return CONST_DISCARD(char *, s); } } } @@ -1392,7 +1392,7 @@ char *strstr_m(const char *src, const char *findstr) DEBUG(0,("strstr_m: dest malloc fail\n")); return NULL; } - retp = (char *)(s+strlen(s2)); + retp = CONST_DISCARD(char *, (s+strlen(s2))); SAFE_FREE(src_w); SAFE_FREE(find_w); SAFE_FREE(s2); -- cgit From f24d88cf9da46680d52b42b92bd484e7b09ce99b Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 31 May 2005 13:46:45 +0000 Subject: r7139: trying to reduce the number of diffs between trunk and 3.0; changing version to 3.0.20pre1 (This used to be commit 9727d05241574042dd3aa8844ae5c701d22e2da1) --- source3/lib/util_str.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 12ee3dc162..b13ec1f0da 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -45,7 +45,7 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) if (!ptr) return(False); - s = CONST_DISCARD(char *, *ptr); + s = (char *)*ptr; /* default to simple separators */ if (!sep) @@ -109,7 +109,7 @@ void set_first_token(char *ptr) char **toktocliplist(int *ctok, const char *sep) { - char *s = CONST_DISCARD(char *, last_ptr); + char *s=(char *)last_ptr; int ictok=0; char **ret, **iret; @@ -132,7 +132,7 @@ char **toktocliplist(int *ctok, const char *sep) } while(*s); *ctok=ictok; - s = CONST_DISCARD(char *, last_ptr); + s=(char *)last_ptr; if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1))) return NULL; @@ -1221,7 +1221,7 @@ char *strchr_m(const char *src, char c) for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { if (*s == c) - return CONST_DISCARD(char *, s); + return (char *)s; } if (!*s) @@ -1238,7 +1238,7 @@ char *strchr_m(const char *src, char c) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); - return CONST_DISCARD(char *, (s+strlen(s2))); + return (char *)(s+strlen(s2)); } char *strrchr_m(const char *s, char c) @@ -1275,7 +1275,7 @@ char *strrchr_m(const char *s, char c) break; } /* No - we have a match ! */ - return CONST_DISCARD(char *, cp); + return (char *)cp; } } while (cp-- != s); if (!got_mb) @@ -1294,7 +1294,7 @@ char *strrchr_m(const char *s, char c) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); - return CONST_DISCARD(char *, (s+strlen(s2))); + return (char *)(s+strlen(s2)); } } @@ -1315,7 +1315,7 @@ char *strnrchr_m(const char *s, char c, unsigned int n) return NULL; *p = 0; pull_ucs2_pstring(s2, ws); - return CONST_DISCARD(char *, (s+strlen(s2))); + return (char *)(s+strlen(s2)); } /*********************************************************************** @@ -1334,7 +1334,7 @@ char *strstr_m(const char *src, const char *findstr) /* for correctness */ if (!findstr[0]) { - return CONST_DISCARD(char *, src); + return (char*)src; } /* Samba does single character findstr calls a *lot*. */ @@ -1351,7 +1351,7 @@ char *strstr_m(const char *src, const char *findstr) findstr_len = strlen(findstr); if (strncmp(s, findstr, findstr_len) == 0) { - return CONST_DISCARD(char *, s); + return (char *)s; } } } @@ -1392,7 +1392,7 @@ char *strstr_m(const char *src, const char *findstr) DEBUG(0,("strstr_m: dest malloc fail\n")); return NULL; } - retp = CONST_DISCARD(char *, (s+strlen(s2))); + retp = (char *)(s+strlen(s2)); SAFE_FREE(src_w); SAFE_FREE(find_w); SAFE_FREE(s2); -- cgit From fed660877c16562265327c6093ea645cf4176b5c Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 8 Jun 2005 22:10:34 +0000 Subject: r7415: * big change -- volker's new async winbindd from trunk (This used to be commit a0ac9a8ffd4af31a0ebc423b4acbb2f043d865b8) --- source3/lib/util_str.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b13ec1f0da..f600d1704e 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2128,3 +2128,69 @@ BOOL add_string_to_array(TALLOC_CTX *mem_ctx, *num += 1; return True; } + +/* Append an sprintf'ed string. Double buffer size on demand. Usable without + * error checking in between. The indiation that something weird happened is + * string==NULL */ + +void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, + size_t *bufsize, const char *fmt, ...) +{ + va_list ap; + char *newstr; + int ret; + BOOL increased; + + /* len<0 is an internal marker that something failed */ + if (*len < 0) + goto error; + + if (*string == NULL) { + if (*bufsize == 0) + *bufsize = 128; + + if (mem_ctx != NULL) + *string = TALLOC_ARRAY(mem_ctx, char, *bufsize); + else + *string = SMB_MALLOC_ARRAY(char, *bufsize); + + if (*string == NULL) + goto error; + } + + va_start(ap, fmt); + ret = vasprintf(&newstr, fmt, ap); + va_end(ap); + + if (ret < 0) + goto error; + + increased = False; + + while ((*len)+ret >= *bufsize) { + increased = True; + *bufsize *= 2; + if (*bufsize >= (1024*1024*256)) + goto error; + } + + if (increased) { + if (mem_ctx != NULL) + *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char, + *bufsize); + else + *string = SMB_REALLOC_ARRAY(*string, char, *bufsize); + + if (*string == NULL) + goto error; + } + + StrnCpy((*string)+(*len), newstr, ret); + (*len) += ret; + free(newstr); + return; + + error: + *len = -1; + *string = NULL; +} -- cgit From e0ffbfc5587ed296d5a9e8f5ed30b6e8b2cd6fcf Mon Sep 17 00:00:00 2001 From: Jim McDonough Date: Wed, 6 Jul 2005 21:02:43 +0000 Subject: r8189: commit vampire ldif patch, mostly from Don Watson (dwatson@us.ibm.com). Yes, that's my copyright...that's just how we have to do things at big blue. Adds subcommand to vampire to allow data to be put into an ldif file instead of actually writing to the passdb. See "net rpc help vampire" for usage info. This should be added to docs as well. (This used to be commit cb5634a305256a70daa2fcbd85d9a5459b4aeaa3) --- source3/lib/util_str.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f600d1704e..42229f105a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2194,3 +2194,30 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, *len = -1; *string = NULL; } + +/* + Returns the substring from src between the first occurrence of + the char "front" and the first occurence of the char "back". + Mallocs the return string which must be freed. Not for use + with wide character strings. +*/ +char *sstring_sub(const char *src, char front, char back) +{ + char *temp1, *temp2, *temp3; + ptrdiff_t len; + + temp1 = strchr(src, front); + if (temp1 == NULL) return NULL; + temp2 = strchr(src, back); + if (temp2 == NULL) return NULL; + len = temp2 - temp1; + if (len <= 0) return NULL; + temp3 = (char*)SMB_MALLOC(len); + if (temp3 == NULL) { + DEBUG(1,("Malloc failure in sstring_sub\n")); + return NULL; + } + memcpy(temp3, temp1+1, len-1); + temp3[len-1] = '\0'; + return temp3; +} -- cgit From eb1123e5009c900c0eb741c0a075f918b16bbeab Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 15 Jul 2005 17:38:55 +0000 Subject: r8506: BUG 2853: don't strip out characters like '$' from printer names when substituting for the lpq command. (This used to be commit 2f5de718a98e56fe55d8905b12505dfc3e432544) --- source3/lib/util_str.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 42229f105a..6b91a0d625 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -916,7 +916,7 @@ BOOL string_set(char **dest,const char *src) use of len==0 which was for no length checks to be done. **/ -void string_sub(char *s,const char *pattern, const char *insert, size_t len) +void string_sub2(char *s,const char *pattern, const char *insert, size_t len, BOOL remove_unsafe_characters) { char *p; ssize_t ls,lp,li, i; @@ -951,8 +951,12 @@ void string_sub(char *s,const char *pattern, const char *insert, size_t len) case '%': case '\r': case '\n': - p[i] = '_'; - break; + if ( remove_unsafe_characters ) { + p[i] = '_'; + /* yes this break should be here since we want to + fall throw if not replacing unsafe chars */ + break; + } default: p[i] = insert[i]; } @@ -962,6 +966,11 @@ void string_sub(char *s,const char *pattern, const char *insert, size_t len) } } +void string_sub(char *s,const char *pattern, const char *insert, size_t len) +{ + string_sub2( s, pattern, insert, len, True ); +} + void fstring_sub(char *s,const char *pattern,const char *insert) { string_sub(s, pattern, insert, sizeof(fstring)); -- cgit From 0b98400cc0b8fa51f995d6cb90382b5f2526b3f5 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Thu, 21 Jul 2005 17:40:20 +0000 Subject: r8686: Revert %LOGONSERVER%-substitution. The substition is done on the client, not on the server. We now preserve this windows variable (important for vampired setups) and correctly substitute only the "%L"s in strings like: "%LOGONSERVER% %L %lOgOnSeRvEr% %L". Guenther (This used to be commit dccf777f42ce1d3f788548842fb8a606bed5708c) --- source3/lib/util_str.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6b91a0d625..1401d6d853 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -909,14 +909,15 @@ BOOL string_set(char **dest,const char *src) enough room! This routine looks for pattern in s and replaces it with - insert. It may do multiple replacements. + insert. It may do multiple replacements or just one. Any of " ; ' $ or ` in the insert string are replaced with _ if len==0 then the string cannot be extended. This is different from the old use of len==0 which was for no length checks to be done. **/ -void string_sub2(char *s,const char *pattern, const char *insert, size_t len, BOOL remove_unsafe_characters) +void string_sub2(char *s,const char *pattern, const char *insert, size_t len, + BOOL remove_unsafe_characters, BOOL replace_once) { char *p; ssize_t ls,lp,li, i; @@ -963,12 +964,20 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, BO } s = p + li; ls += (li-lp); + + if (replace_once) + break; } } +void string_sub_once(char *s, const char *pattern, const char *insert, size_t len) +{ + string_sub2( s, pattern, insert, len, True, True ); +} + void string_sub(char *s,const char *pattern, const char *insert, size_t len) { - string_sub2( s, pattern, insert, len, True ); + string_sub2( s, pattern, insert, len, True, False ); } void fstring_sub(char *s,const char *pattern,const char *insert) -- cgit From db8c38340b35574fc553b5ef7019f942699356f4 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 7 Aug 2005 20:59:28 +0000 Subject: r9198: Convert hex_encode and strhex_to_data_blob to take a talloc context. Volker (This used to be commit c7d10e2c834d8d5136e2d01dea1ad286757deddb) --- source3/lib/util_str.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1401d6d853..1252c6756b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -795,9 +795,14 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) return num_chars; } -DATA_BLOB strhex_to_data_blob(const char *strhex) +DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) { - DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1); + DATA_BLOB ret_blob; + + if (mem_ctx != NULL) + ret_blob = data_blob_talloc(mem_ctx, NULL, strlen(strhex)/2+1); + else + data_blob(NULL, strlen(strhex)/2+1); ret_blob.length = strhex_to_str((char*)ret_blob.data, strlen(strhex), @@ -810,16 +815,17 @@ DATA_BLOB strhex_to_data_blob(const char *strhex) * Routine to print a buffer as HEX digits, into an allocated string. */ -void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer) +char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len) { int i; char *hex_buffer; - *out_hex_buffer = SMB_XMALLOC_ARRAY(char, (len*2)+1); - hex_buffer = *out_hex_buffer; + hex_buffer = TALLOC_ARRAY(mem_ctx, char, (len*2)+1); for (i = 0; i < len; i++) slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]); + + return hex_buffer; } /** -- cgit From a389bf7ae590d33fa73037ac59704f6dfc40c9ee Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 7 Aug 2005 21:34:55 +0000 Subject: r9201: Ouch.... :-( (This used to be commit c78760d71ec6a364a912ba35e8a90f8005fdc018) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1252c6756b..06f9018269 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -802,7 +802,7 @@ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) if (mem_ctx != NULL) ret_blob = data_blob_talloc(mem_ctx, NULL, strlen(strhex)/2+1); else - data_blob(NULL, strlen(strhex)/2+1); + ret_blob = data_blob(NULL, strlen(strhex)/2+1); ret_blob.length = strhex_to_str((char*)ret_blob.data, strlen(strhex), -- cgit From e4edd9527595faf480b80bf2b2df0392565ea51f Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 12 Aug 2005 20:12:31 +0000 Subject: r9271: Fix problems with german umlauts - strcmp_w was broken (needs to always re-call macro on termination). Fix all other cases where this was also occurring. Jeremy. (This used to be commit 816e2fbb39b544b7f62d5351f3a8e0af63717227) --- source3/lib/util_str.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 06f9018269..712a8a18fd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -183,7 +183,7 @@ char **toktocliplist(int *ctok, const char *sep) int StrCaseCmp(const char *s, const char *t) { - const char * ps, * pt; + const char *ps, *pt; size_t size; smb_ucs2_t *buffer_s, *buffer_t; int ret; @@ -211,17 +211,17 @@ int StrCaseCmp(const char *s, const char *t) return +1; } - size = push_ucs2_allocate(&buffer_s, s); + size = push_ucs2_allocate(&buffer_s, ps); if (size == (size_t)-1) { - return strcmp(s, t); + return strcmp(ps, pt); /* Not quite the right answer, but finding the right one under this failure case is expensive, and it's pretty close */ } - size = push_ucs2_allocate(&buffer_t, t); + size = push_ucs2_allocate(&buffer_t, pt); if (size == (size_t)-1) { SAFE_FREE(buffer_s); - return strcmp(s, t); + return strcmp(ps, pt); /* Not quite the right answer, but finding the right one under this failure case is expensive, and it's pretty close */ } -- cgit From e04754b295acdea6d2ea365bb0a3935a5976a3a2 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 12 Aug 2005 22:59:35 +0000 Subject: r9282: Whitespace. Jeremy. (This used to be commit 183a4705114da7c3662b71405457123ce35b5358) --- source3/lib/util_str.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 712a8a18fd..9b14dcfaf0 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -266,12 +266,12 @@ BOOL strequal(const char *s1, const char *s2) **/ BOOL strnequal(const char *s1,const char *s2,size_t n) { - if (s1 == s2) - return(True); - if (!s1 || !s2 || !n) - return(False); + if (s1 == s2) + return(True); + if (!s1 || !s2 || !n) + return(False); - return(StrnCaseCmp(s1,s2,n)==0); + return(StrnCaseCmp(s1,s2,n)==0); } /** @@ -280,12 +280,12 @@ BOOL strnequal(const char *s1,const char *s2,size_t n) BOOL strcsequal(const char *s1,const char *s2) { - if (s1 == s2) - return(True); - if (!s1 || !s2) - return(False); + if (s1 == s2) + return(True); + if (!s1 || !s2) + return(False); - return(strcmp(s1,s2)==0); + return(strcmp(s1,s2)==0); } /** -- cgit From 28fb5b6f97c17af58ae99be98219de70ee95baba Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 18 Dec 2005 18:06:15 +0000 Subject: r12313: Introduce yet another copy of the string_sub function: talloc_string_sub. Someone with time on his hands could convert all the callers of all_string_sub to this. realloc_string_sub is *only* called from within substitute.c, it could be moved there I think. Volker (This used to be commit be6c9012da174d5d5116e5172a53bbe6486d6c38) --- source3/lib/util_str.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 9b14dcfaf0..80bb2ff2ad 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1003,7 +1003,8 @@ void pstring_sub(char *s,const char *pattern,const char *insert) as string. **/ -char *realloc_string_sub(char *string, const char *pattern, const char *insert) +char *realloc_string_sub(char *string, const char *pattern, + const char *insert) { char *p, *in; char *s; @@ -1063,6 +1064,77 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) return string; } +/* Same as string_sub, but returns a talloc'ed string */ + +char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, + const char *pattern, const char *insert) +{ + char *p, *in; + char *s; + char *string; + ssize_t ls,lp,li,ld, i; + + if (!insert || !pattern || !*pattern || !src || !*src) + return NULL; + + string = talloc_strdup(mem_ctx, src); + if (string == NULL) { + DEBUG(0, ("talloc_strdup failed\n")); + return NULL; + } + + s = string; + + in = SMB_STRDUP(insert); + if (!in) { + DEBUG(0, ("talloc_string_sub: out of memory!\n")); + return NULL; + } + ls = (ssize_t)strlen(s); + lp = (ssize_t)strlen(pattern); + li = (ssize_t)strlen(insert); + ld = li - lp; + for (i=0;i 0) { + int offset = PTR_DIFF(s,string); + char *t = TALLOC_REALLOC(mem_ctx, string, ls + ld + 1); + if (!t) { + DEBUG(0, ("talloc_string_sub: out of " + "memory!\n")); + SAFE_FREE(in); + return NULL; + } + string = t; + p = t + offset + (p - s); + } + if (li != lp) { + memmove(p+li,p+lp,strlen(p+lp)+1); + } + memcpy(p, in, li); + s = p + li; + ls += ld; + } + SAFE_FREE(in); + return string; +} + /** Similar to string_sub() but allows for any character to be substituted. Use with caution! -- cgit From 5a4881bf396e691524329bcd6aa1ae4a7f4084ec Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 27 Dec 2005 20:52:36 +0000 Subject: r12522: Try and fix bug #2926 by removing setlocale(LC_ALL, "C") and replace calls to isupper/islower/toupper/tolower with ASCII equivalents (mapping into _w variants). Jeremy. (This used to be commit c2752347eb2deeb2798c580ec7fc751a847717e9) --- source3/lib/util_str.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 80bb2ff2ad..0b02487f77 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -201,8 +201,8 @@ int StrCaseCmp(const char *s, const char *t) /* not ascii anymore, do it the hard way from here on in */ break; - us = toupper(*ps); - ut = toupper(*pt); + us = toupper_ascii(*ps); + ut = toupper_ascii(*pt); if (us == ut) continue; else if (us < ut) @@ -309,7 +309,7 @@ int strwicmp(const char *psz1, const char *psz2) psz1++; while (isspace((int)*psz2)) psz2++; - if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0' + if (toupper_ascii(*psz1) != toupper_ascii(*psz2) || *psz1 == '\0' || *psz2 == '\0') break; psz1++; @@ -680,7 +680,7 @@ char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, con for(i = 0; i < len; i++) { int val = (src[i] & 0xff); - if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val)) + if (isupper_ascii(val) || islower_ascii(val) || isdigit(val) || strchr_m(other_safe_chars, val)) dest[i] = src[i]; else dest[i] = '_'; @@ -774,12 +774,12 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) continue; } - if (!(p1 = strchr_m(hexchars, toupper(strhex[i])))) + if (!(p1 = strchr_m(hexchars, toupper_ascii(strhex[i])))) break; i++; /* next hex digit */ - if (!(p2 = strchr_m(hexchars, toupper(strhex[i])))) + if (!(p2 = strchr_m(hexchars, toupper_ascii(strhex[i])))) break; /* get the two nybbles */ @@ -1510,7 +1510,7 @@ void strlower_m(char *s) (ie. they match for the first 128 chars) */ while (*s && !(((unsigned char)s[0]) & 0x80)) { - *s = tolower((unsigned char)*s); + *s = tolower_ascii((unsigned char)*s); s++; } @@ -1544,7 +1544,7 @@ void strupper_m(char *s) (ie. they match for the first 128 chars) */ while (*s && !(((unsigned char)s[0]) & 0x80)) { - *s = toupper((unsigned char)*s); + *s = toupper_ascii((unsigned char)*s); s++; } -- cgit From 0af1500fc0bafe61019f1b2ab1d9e1d369221240 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 3 Feb 2006 22:19:41 +0000 Subject: r13316: Let the carnage begin.... Sync with trunk as off r13315 (This used to be commit 17e63ac4ed8325c0d44fe62b2442449f3298559f) --- source3/lib/util_str.c | 99 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 12 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 0b02487f77..85b5cfc90a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1667,7 +1667,7 @@ int fstr_sprintf(fstring s, const char *fmt, ...) #define S_LIST_ABS 16 /* List Allocation Block Size */ -char **str_list_make(const char *string, const char *sep) +static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, const char *sep) { char **list, **rlist; const char *str; @@ -1677,7 +1677,11 @@ char **str_list_make(const char *string, const char *sep) if (!string || !*string) return NULL; - s = SMB_STRDUP(string); + if (mem_ctx) { + s = talloc_strdup(mem_ctx, string); + } else { + s = SMB_STRDUP(string); + } if (!s) { DEBUG(0,("str_list_make: Unable to allocate memory")); return NULL; @@ -1691,32 +1695,64 @@ char **str_list_make(const char *string, const char *sep) while (next_token(&str, tok, sep, sizeof(tok))) { if (num == lsize) { lsize += S_LIST_ABS; - rlist = SMB_REALLOC_ARRAY(list, char *, lsize +1); + if (mem_ctx) { + rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *, lsize +1); + } else { + rlist = SMB_REALLOC_ARRAY(list, char *, lsize +1); + } if (!rlist) { DEBUG(0,("str_list_make: Unable to allocate memory")); str_list_free(&list); - SAFE_FREE(s); + if (mem_ctx) { + talloc_free(s); + } else { + SAFE_FREE(s); + } return NULL; } else list = rlist; memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1))); } + + if (mem_ctx) { + list[num] = talloc_strdup(mem_ctx, tok); + } else { + list[num] = SMB_STRDUP(tok); + } - list[num] = SMB_STRDUP(tok); if (!list[num]) { DEBUG(0,("str_list_make: Unable to allocate memory")); str_list_free(&list); - SAFE_FREE(s); + if (mem_ctx) { + talloc_free(s); + } else { + SAFE_FREE(s); + } return NULL; } num++; } - - SAFE_FREE(s); + + if (mem_ctx) { + talloc_free(s); + } else { + SAFE_FREE(s); + } + return list; } +char **str_list_make_talloc(TALLOC_CTX *mem_ctx, const char *string, const char *sep) +{ + return str_list_make_internal(mem_ctx, string, sep); +} + +char **str_list_make(const char *string, const char *sep) +{ + return str_list_make_internal(NULL, string, sep); +} + BOOL str_list_copy(char ***dest, const char **src) { char **list, **rlist; @@ -1778,16 +1814,35 @@ BOOL str_list_compare(char **list1, char **list2) return True; } -void str_list_free(char ***list) +static void str_list_free_internal(TALLOC_CTX *mem_ctx, char ***list) { char **tlist; if (!list || !*list) return; tlist = *list; - for(; *tlist; tlist++) - SAFE_FREE(*tlist); - SAFE_FREE(*list); + for(; *tlist; tlist++) { + if (mem_ctx) { + talloc_free(*tlist); + } else { + SAFE_FREE(*tlist); + } + } + if (mem_ctx) { + talloc_free(*tlist); + } else { + SAFE_FREE(*list); + } +} + +void str_list_free_talloc(TALLOC_CTX *mem_ctx, char ***list) +{ + str_list_free_internal(mem_ctx, list); +} + +void str_list_free(char ***list) +{ + str_list_free_internal(NULL, list); } /****************************************************************************** @@ -2317,3 +2372,23 @@ char *sstring_sub(const char *src, char front, char back) temp3[len-1] = '\0'; return temp3; } + +/******************************************************************** + Check a string for any occurrences of a specified list of invalid + characters. +********************************************************************/ + +BOOL validate_net_name( const char *name, const char *invalid_chars, int max_len ) +{ + int i; + + for ( i=0; i Date: Wed, 8 Feb 2006 15:09:09 +0000 Subject: r13393: Do not initialize the lp_svcctl_list() value since it is handled internally in services_db.c now. This prevents internal services from being listed twice (one internal and one external) when no 'svcctl list' parameter is explcitly set in smb.conf (This used to be commit 6c4ede6cee7e1d25a6357e959972e8d390c27fe3) --- source3/lib/util_str.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 85b5cfc90a..957ebd57bd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1852,6 +1852,9 @@ int str_list_count( const char **list ) { int i = 0; + if ( ! list ) + return 0; + /* count the number of list members */ for ( i=0; *list; i++, list++ ); -- cgit From fb5362c069b5b6548478b2217a0519c56d856705 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 20 Feb 2006 17:59:58 +0000 Subject: r13571: Replace all calls to talloc_free() with thye TALLOC_FREE() macro which sets the freed pointer to NULL. (This used to be commit b65be8874a2efe5a4b167448960a4fcf6bd995e2) --- source3/lib/util_str.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 957ebd57bd..e4aa5dbd51 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1704,7 +1704,7 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co DEBUG(0,("str_list_make: Unable to allocate memory")); str_list_free(&list); if (mem_ctx) { - talloc_free(s); + TALLOC_FREE(s); } else { SAFE_FREE(s); } @@ -1724,7 +1724,7 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co DEBUG(0,("str_list_make: Unable to allocate memory")); str_list_free(&list); if (mem_ctx) { - talloc_free(s); + TALLOC_FREE(s); } else { SAFE_FREE(s); } @@ -1735,7 +1735,7 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co } if (mem_ctx) { - talloc_free(s); + TALLOC_FREE(s); } else { SAFE_FREE(s); } @@ -1823,13 +1823,13 @@ static void str_list_free_internal(TALLOC_CTX *mem_ctx, char ***list) tlist = *list; for(; *tlist; tlist++) { if (mem_ctx) { - talloc_free(*tlist); + TALLOC_FREE(*tlist); } else { SAFE_FREE(*tlist); } } if (mem_ctx) { - talloc_free(*tlist); + TALLOC_FREE(*tlist); } else { SAFE_FREE(*list); } -- cgit From cab298856ab1179cdaec2ef89121f7c66c6b6d76 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Wed, 22 Feb 2006 10:28:02 +0000 Subject: r13622: Allow to rename machine accounts in a Samba Domain. This still uses the "rename user script" to do the rename of the posix machine account (this might be changed later). Fixes #2331. Guenther (This used to be commit b2eac2e6eb6ddd1bcb4ed5172e7cd64144c18d16) --- source3/lib/util_str.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e4aa5dbd51..e799556cd1 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -923,7 +923,7 @@ BOOL string_set(char **dest,const char *src) **/ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, - BOOL remove_unsafe_characters, BOOL replace_once) + BOOL remove_unsafe_characters, BOOL replace_once, BOOL allow_trailing_dollar) { char *p; ssize_t ls,lp,li, i; @@ -955,6 +955,11 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, case '\'': case ';': case '$': + /* allow a trailing $ (as in machine accounts) */ + if (allow_trailing_dollar && (i == li - 1 )) { + p[i] = insert[i]; + break; + } case '%': case '\r': case '\n': @@ -978,12 +983,12 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, void string_sub_once(char *s, const char *pattern, const char *insert, size_t len) { - string_sub2( s, pattern, insert, len, True, True ); + string_sub2( s, pattern, insert, len, True, True, False ); } void string_sub(char *s,const char *pattern, const char *insert, size_t len) { - string_sub2( s, pattern, insert, len, True, False ); + string_sub2( s, pattern, insert, len, True, False, False ); } void fstring_sub(char *s,const char *pattern,const char *insert) -- cgit From 894358a8f3e338b339b6c37233edef794b312087 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Mar 2006 06:31:04 +0000 Subject: r13915: Fixed a very interesting class of realloc() bugs found by Coverity. realloc can return NULL in one of two cases - (1) the realloc failed, (2) realloc succeeded but the new size requested was zero, in which case this is identical to a free() call. The error paths dealing with these two cases should be different, but mostly weren't. Secondly the standard idiom for dealing with realloc when you know the new size is non-zero is the following : tmp = realloc(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } However, there were *many* *many* places in Samba where we were using the old (broken) idiom of : p = realloc(p, size) if (!p) { return error; } which will leak the memory pointed to by p on realloc fail. This commit (hopefully) fixes all these cases by moving to a standard idiom of : p = SMB_REALLOC(p, size) if (!p) { return error; } Where if the realloc returns null due to the realloc failing or size == 0 we *guarentee* that the storage pointed to by p has been freed. This allows me to remove a lot of code that was dealing with the standard (more verbose) method that required a tmp pointer. This is almost always what you want. When a realloc fails you never usually want the old memory, you want to free it and get into your error processing asap. For the 11 remaining cases where we really do need to keep the old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR, which can be used as follows : tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the pointer p, even on size == 0 or realloc fail. All this is done by a hidden extra argument to Realloc(), BOOL free_old_on_error which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR macros (and their array counterparts). It remains to be seen what this will do to our Coverity bug count :-). Jeremy. (This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0) --- source3/lib/util_str.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index e799556cd1..f1ae9a472a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1049,14 +1049,13 @@ char *realloc_string_sub(char *string, const char *pattern, while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); - char *t = SMB_REALLOC(string, ls + ld + 1); - if (!t) { + string = SMB_REALLOC(string, ls + ld + 1); + if (!string) { DEBUG(0, ("realloc_string_sub: out of memory!\n")); SAFE_FREE(in); return NULL; } - string = t; - p = t + offset + (p - s); + p = string + offset + (p - s); } if (li != lp) { memmove(p+li,p+lp,strlen(p+lp)+1); @@ -1119,15 +1118,14 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); - char *t = TALLOC_REALLOC(mem_ctx, string, ls + ld + 1); - if (!t) { + string = TALLOC_REALLOC(mem_ctx, string, ls + ld + 1); + if (!string) { DEBUG(0, ("talloc_string_sub: out of " "memory!\n")); SAFE_FREE(in); return NULL; } - string = t; - p = t + offset + (p - s); + p = string + offset + (p - s); } if (li != lp) { memmove(p+li,p+lp,strlen(p+lp)+1); @@ -1703,7 +1701,9 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co if (mem_ctx) { rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *, lsize +1); } else { - rlist = SMB_REALLOC_ARRAY(list, char *, lsize +1); + /* We need to keep the old list on error so we can free the elements + if the realloc fails. */ + rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, char *, lsize +1); } if (!rlist) { DEBUG(0,("str_list_make: Unable to allocate memory")); @@ -1714,8 +1714,9 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co SAFE_FREE(s); } return NULL; - } else + } else { list = rlist; + } memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1))); } @@ -1773,7 +1774,7 @@ BOOL str_list_copy(char ***dest, const char **src) while (src[num]) { if (num == lsize) { lsize += S_LIST_ABS; - rlist = SMB_REALLOC_ARRAY(list, char *, lsize +1); + rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, char *, lsize +1); if (!rlist) { DEBUG(0,("str_list_copy: Unable to re-allocate memory")); str_list_free(&list); @@ -2266,8 +2267,9 @@ void string_append(char **left, const char *right) *left = SMB_REALLOC(*left, new_len); } - if (*left == NULL) + if (*left == NULL) { return; + } safe_strcat(*left, right, new_len-1); } @@ -2334,14 +2336,16 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, } if (increased) { - if (mem_ctx != NULL) + if (mem_ctx != NULL) { *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char, *bufsize); - else + } else { *string = SMB_REALLOC_ARRAY(*string, char, *bufsize); + } - if (*string == NULL) + if (*string == NULL) { goto error; + } } StrnCpy((*string)+(*len), newstr, ret); -- cgit From 71272fc441d7930f7ae810ee3c8f5a58385cb55c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Mar 2006 18:52:48 +0000 Subject: r13975: Re-fix Coverity #156 - I had left the hidden arg. inconsistent between Realloc and realloc_array. Jeremy. (This used to be commit 841c9b1847ae12656b827e3d35b8bf0c3f68b8b4) --- source3/lib/util_str.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f1ae9a472a..446a4a00a1 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1779,8 +1779,9 @@ BOOL str_list_copy(char ***dest, const char **src) DEBUG(0,("str_list_copy: Unable to re-allocate memory")); str_list_free(&list); return False; - } else + } else { list = rlist; + } memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1))); } -- cgit From bbf666e447132a5f6b206ddf9ca918298b756392 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Sat, 8 Apr 2006 17:25:31 +0000 Subject: r15003: patch based on code from Arkady Glabek to ensure that global memory is freed when unloading pam_winbind.so (needs more testing on non-linux platforms) (This used to be commit 1e0b79e591d70352a96e0a0487d8f394dc7b36ba) --- source3/lib/util_str.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 446a4a00a1..439cbea6d9 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -853,7 +853,7 @@ BOOL in_list(const char *s, const char *list, BOOL casesensitive) } /* this is used to prevent lots of mallocs of size 1 */ -static char *null_string = NULL; +static const char *null_string = ""; /** Set a string value, allocing the space for the string @@ -862,20 +862,14 @@ static char *null_string = NULL; static BOOL string_init(char **dest,const char *src) { size_t l; + if (!src) src = ""; l = strlen(src); if (l == 0) { - if (!null_string) { - if((null_string = (char *)SMB_MALLOC(1)) == NULL) { - DEBUG(0,("string_init: malloc fail for null_string.\n")); - return False; - } - *null_string = 0; - } - *dest = null_string; + *dest = CONST_DISCARD(char*, null_string); } else { (*dest) = SMB_STRDUP(src); if ((*dest) == NULL) { -- cgit From 34e810076df8720a145f5a619ed648c384898563 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Fri, 28 Apr 2006 14:44:43 +0000 Subject: r15305: Let winbind search by sid directly (or in windows terms: "bind to a sid"); works in all AD versions I tested. Also add "net ads sid" search tool. Guenther (This used to be commit 5557ada6943b817d28a5471c613c7291febe2ad5) --- source3/lib/util_str.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 439cbea6d9..df84fa90a5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1566,7 +1566,7 @@ void strupper_m(char *s) Caller must free. **/ -char *binary_string(char *buf, int len) +char *binary_string_rfc2254(char *buf, int len) { char *s; int i, j; @@ -1584,6 +1584,22 @@ char *binary_string(char *buf, int len) return s; } +char *binary_string(char *buf, int len) +{ + char *s; + int i, j; + const char *hex = "0123456789ABCDEF"; + s = SMB_MALLOC(len * 2 + 1); + if (!s) + return NULL; + for (j=i=0;i> 4]; + s[j+1] = hex[((unsigned char)buf[i]) & 0xF]; + j += 2; + } + s[j] = 0; + return s; +} /** Just a typesafety wrapper for snprintf into a pstring. **/ -- cgit From 0c88eedbeba3da627e712021a2811b01f323c16e Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 19 Jun 2006 23:19:24 +0000 Subject: r16375: Klocwork #1670. Jeremy. (This used to be commit 99605ce296663b7697d737fd521f0e4d8436d1f2) --- source3/lib/util_str.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index df84fa90a5..6fe1668e88 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2250,15 +2250,21 @@ SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) SMB_BIG_UINT val = -1; const char *p = nptr; - while (p && *p && isspace(*p)) + if (!p) { + *entptr = p; + return val; + } + + while (*p && isspace(*p)) p++; + #ifdef LARGE_SMB_OFF_T sscanf(p,"%llu",&val); #else /* LARGE_SMB_OFF_T */ sscanf(p,"%lu",&val); #endif /* LARGE_SMB_OFF_T */ if (entptr) { - while (p && *p && isdigit(*p)) + while (*p && isdigit(*p)) p++; *entptr = p; } -- cgit From 9220a7bb7ba0468113deae3c033504ab4ff31eb2 Mon Sep 17 00:00:00 2001 From: Derrell Lipman Date: Tue, 27 Jun 2006 03:07:02 +0000 Subject: r16552: Fix bug 3849. Added a next_token_no_ltrim() function which does not strip leading separator characters. The new function is used only where really necessary, even though it could reasonably be used in many more places, to avoid superfluous code changes. Derrell (This used to be commit d90061aa933f7d8c81973918657dd72cbc88bab5) --- source3/lib/util_str.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6fe1668e88..0248ad63ad 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -29,13 +29,18 @@ **/ /** - * Get the next token from a string, return False if none found. - * Handles double-quotes. - * + * Internal function to get the next token from a string, return False if none + * found. Handles double-quotes. This is the work horse function called by + * next_token() and next_token_no_ltrim(). + * * Based on a routine by GJC@VILLAGE.COM. * Extensively modified by Andrew.Tridgell@anu.edu.au - **/ -BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) + */ +static BOOL next_token_internal(const char **ptr, + char *buff, + const char *sep, + size_t bufsize, + int ltrim) { char *s; char *pbuf; @@ -51,8 +56,8 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) if (!sep) sep = " \t\n\r"; - /* find the first non sep char */ - while (*s && strchr_m(sep,*s)) + /* find the first non sep char, if left-trimming is requested */ + while (ltrim && *s && strchr_m(sep,*s)) s++; /* nothing left? */ @@ -76,6 +81,29 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) return(True); } +/* + * Get the next token from a string, return False if none found. Handles + * double-quotes. This version trims leading separator characters before + * looking for a token. + */ +BOOL next_token(const char **ptr, char *buff, const char *sep, size_t bufsize) +{ + return next_token_internal(ptr, buff, sep, bufsize, True); +} + +/* + * Get the next token from a string, return False if none found. Handles + * double-quotes. This version does not trim leading separator characters + * before looking for a token. + */ +BOOL next_token_no_ltrim(const char **ptr, + char *buff, + const char *sep, + size_t bufsize) +{ + return next_token_internal(ptr, buff, sep, bufsize, False); +} + /** This is like next_token but is not re-entrant and "remembers" the first parameter so you can pass NULL. This is useful for user interface code -- cgit From b000e8f08312717c123bdc800fffee0347f53d5b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 27 Jun 2006 03:59:34 +0000 Subject: r16554: Sorry, just had to change this. Don't use int when you're passing a BOOL parameter, don't use "clever" code in while statement - make things easier and clearer to understand when triggering something with an if. Jeremy. (This used to be commit b1fc2d8b99e0402c0e8fe954d9f9563dc4dc2812) --- source3/lib/util_str.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 0248ad63ad..8e0f2765eb 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -40,7 +40,7 @@ static BOOL next_token_internal(const char **ptr, char *buff, const char *sep, size_t bufsize, - int ltrim) + BOOL ltrim) { char *s; char *pbuf; @@ -57,8 +57,10 @@ static BOOL next_token_internal(const char **ptr, sep = " \t\n\r"; /* find the first non sep char, if left-trimming is requested */ - while (ltrim && *s && strchr_m(sep,*s)) - s++; + if (ltrim) { + while (*s && strchr_m(sep,*s)) + s++; + } /* nothing left? */ if (! *s) -- cgit From 35a57320f881ee9ab6e5b49247eef99f2d984cf2 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 28 Jun 2006 01:56:41 +0000 Subject: r16595: Klocwork #2067. Fix possible memleak on error exit. Jeremy. (This used to be commit 1d21a3dec9ea061ce900ad1223f7c2a43c064600) --- source3/lib/util_str.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8e0f2765eb..938fb0f47b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2402,6 +2402,9 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, error: *len = -1; + if (mem_ctx == NULL) { + SAFE_FREE(*string); + } *string = NULL; } -- cgit From fbdcf2663b56007a438ac4f0d8d82436b1bfe688 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 11 Jul 2006 18:01:26 +0000 Subject: r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need to do the upper layer directories but this is what everyone is waiting for.... Jeremy. (This used to be commit 9dafb7f48ca3e7af956b0a7d1720c2546fc4cfb8) --- source3/lib/util_str.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 938fb0f47b..2580521c3b 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -5,6 +5,7 @@ Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Simo Sorce 2001-2002 Copyright (C) Martin Pool 2003 + Copyright (C) James Peach 2006 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -1914,13 +1915,14 @@ int str_list_count( const char **list ) for the work *****************************************************************************/ -BOOL str_list_sub_basic( char **list, const char *smb_name ) +BOOL str_list_sub_basic( char **list, const char *smb_name, + const char *domain_name ) { char *s, *tmpstr; while ( *list ) { s = *list; - tmpstr = alloc_sub_basic(smb_name, s); + tmpstr = alloc_sub_basic(smb_name, domain_name, s); if ( !tmpstr ) { DEBUG(0,("str_list_sub_basic: alloc_sub_basic() return NULL!\n")); return False; @@ -2302,6 +2304,73 @@ SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) return val; } +/* Convert a size specification to a count of bytes. We accept the following + * suffixes: + * bytes if there is no suffix + * kK kibibytes + * mM mebibytes + * gG gibibytes + * tT tibibytes + * pP whatever the ISO name for petabytes is + * + * Returns 0 if the string can't be converted. + */ +SMB_OFF_T conv_str_size(const char * str) +{ + SMB_OFF_T lval; + char * end; + + if (str == NULL || *str == '\0') { + return 0; + } + +#ifdef HAVE_STRTOULL + if (sizeof(SMB_OFF_T) == 8) { + lval = strtoull(str, &end, 10 /* base */); + } else { + lval = strtoul(str, &end, 10 /* base */); + } +#else + lval = strtoul(str, &end, 10 /* base */); +#endif + + if (end == NULL || end == str) { + return 0; + } + + if (*end) { + SMB_OFF_T lval_orig = lval; + + if (strwicmp(end, "K") == 0) { + lval *= (SMB_OFF_T)1024; + } else if (strwicmp(end, "M") == 0) { + lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024); + } else if (strwicmp(end, "G") == 0) { + lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 * + (SMB_OFF_T)1024); + } else if (strwicmp(end, "T") == 0) { + lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 * + (SMB_OFF_T)1024 * (SMB_OFF_T)1024); + } else if (strwicmp(end, "P") == 0) { + lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 * + (SMB_OFF_T)1024 * (SMB_OFF_T)1024 * + (SMB_OFF_T)1024); + } else { + return 0; + } + + /* Primitive attempt to detect wrapping on platforms with + * 4-byte SMB_OFF_T. It's better to let the caller handle + * a failure than some random number. + */ + if (lval_orig <= lval) { + return 0; + } + } + + return lval; +} + void string_append(char **left, const char *right) { int new_len = strlen(right) + 1; @@ -2454,3 +2523,52 @@ BOOL validate_net_name( const char *name, const char *invalid_chars, int max_len return True; } + +/** +return the number of bytes occupied by a buffer in ASCII format +the result includes the null termination +limited by 'n' bytes +**/ +size_t ascii_len_n(const char *src, size_t n) +{ + size_t len; + + len = strnlen(src, n); + if (len+1 <= n) { + len += 1; + } + + return len; +} + +/** +return the number of bytes occupied by a buffer in CH_UTF16 format +the result includes the null termination +**/ +size_t utf16_len(const void *buf) +{ + size_t len; + + for (len = 0; SVAL(buf,len); len += 2) ; + + return len + 2; +} + +/** +return the number of bytes occupied by a buffer in CH_UTF16 format +the result includes the null termination +limited by 'n' bytes +**/ +size_t utf16_len_n(const void *src, size_t n) +{ + size_t len; + + for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ; + + if (len+2 <= n) { + len += 2; + } + + return len; +} + -- cgit From e23781b3b304d1e69ad80af5ae9c0ed8d02cf996 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 30 Jul 2006 16:36:56 +0000 Subject: r17316: More C++ warnings -- 456 left (This used to be commit 1e4ee728df7eeafc1b4d533240acb032f73b4f5c) --- source3/lib/util_str.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2580521c3b..6677d075bd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1074,7 +1074,7 @@ char *realloc_string_sub(char *string, const char *pattern, while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); - string = SMB_REALLOC(string, ls + ld + 1); + string = (char *)SMB_REALLOC(string, ls + ld + 1); if (!string) { DEBUG(0, ("realloc_string_sub: out of memory!\n")); SAFE_FREE(in); @@ -1143,7 +1143,8 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); - string = TALLOC_REALLOC(mem_ctx, string, ls + ld + 1); + string = (char *)TALLOC_REALLOC(mem_ctx, string, + ls + ld + 1); if (!string) { DEBUG(0, ("talloc_string_sub: out of " "memory!\n")); @@ -1602,7 +1603,7 @@ char *binary_string_rfc2254(char *buf, int len) char *s; int i, j; const char *hex = "0123456789ABCDEF"; - s = SMB_MALLOC(len * 3 + 1); + s = (char *)SMB_MALLOC(len * 3 + 1); if (!s) return NULL; for (j=i=0;i Date: Mon, 28 Aug 2006 01:48:04 +0000 Subject: r17862: Fix possible NULL deref (like rev 17861) found by the Stanford group. Jeremy. (This used to be commit cfd39c2804df3f67637adf223542daa208c893c7) --- source3/lib/util_str.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6677d075bd..2b647b0641 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2284,7 +2284,9 @@ SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) const char *p = nptr; if (!p) { - *entptr = p; + if (entptr) { + *entptr = p; + } return val; } -- cgit From 767820e462f43f6388aa512cff10b4ff3748e834 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 28 Aug 2006 02:27:49 +0000 Subject: r17866: Fix possible null deref - found by Stanford checker. Jeremy. (This used to be commit 84e8cc0593c20b1262965a320d89b72a3dc9b402) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2b647b0641..8639a9bc07 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2056,7 +2056,7 @@ char* ipstr_list_make(char** ipstr_list, const struct ip_service* ip_list, int i int i; /* arguments checking */ - if (!ip_list && !ipstr_list) return 0; + if (!ip_list || !ipstr_list) return 0; *ipstr_list = NULL; -- cgit From ac76c1804c7e5627d4c4841ceb825b27c4e8b4d5 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 18 Sep 2006 23:46:58 +0000 Subject: r18652: libreplace has replacements for strndup and strnlen metze (This used to be commit 9f3599a7ca636dd21c150873f395abde153ee6fd) --- source3/lib/util_str.c | 43 ------------------------------------------- 1 file changed, 43 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 8639a9bc07..4619d47388 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1663,49 +1663,6 @@ int fstr_sprintf(fstring s, const char *fmt, ...) return ret; } - -#if !defined(HAVE_STRNDUP) || defined(BROKEN_STRNDUP) -/** - Some platforms don't have strndup. -**/ -#if defined(PARANOID_MALLOC_CHECKER) -#undef strndup -#endif - - char *strndup(const char *s, size_t n) -{ - char *ret; - - n = strnlen(s, n); - ret = SMB_MALLOC(n+1); - if (!ret) - return NULL; - memcpy(ret, s, n); - ret[n] = 0; - - return ret; -} - -#if defined(PARANOID_MALLOC_CHECKER) -#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY -#endif - -#endif - -#if !defined(HAVE_STRNLEN) || defined(BROKEN_STRNLEN) -/** - Some platforms don't have strnlen -**/ - - size_t strnlen(const char *s, size_t n) -{ - size_t i; - for (i=0; i Date: Thu, 21 Sep 2006 17:00:07 +0000 Subject: r18787: Fix the strlen_m and strlen_m_term code by merging in (and using elsewhere) next_codepoint from Samba4. Jerry please test. Jeremy. (This used to be commit ece00b70a4621633f1ac9e576c4bbe332031de09) --- source3/lib/util_str.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 4619d47388..414a87a562 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1592,6 +1592,58 @@ void strupper_m(char *s) errno = errno_save; } +/** + Count the number of UCS2 characters in a string. Normally this will + be the same as the number of bytes in a string for single byte strings, + but will be different for multibyte. +**/ + +size_t strlen_m(const char *s) +{ + size_t count = 0; + + if (!s) { + return 0; + } + + while (*s && !(((uint8_t)*s) & 0x80)) { + s++; + count++; + } + + if (!*s) { + return count; + } + + while (*s) { + size_t c_size; + codepoint_t c = next_codepoint(s, &c_size); + if (c < 0x10000) { + /* Unicode char fits into 16 bits. */ + count += 1; + } else { + /* Double-width unicode char - 32 bits. */ + count += 2; + } + s += c_size; + } + + return count; +} + +/** + Count the number of UCS2 characters in a string including the null + terminator. +**/ + +size_t strlen_m_term(const char *s) +{ + if (!s) { + return 0; + } + return strlen_m(s) + 1; +} + /** Return a RFC2254 binary string representation of a buffer. Used in LDAP filters. -- cgit From 30db93664c3fc6617d568e8fea0d3a11282fb123 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 19 Nov 2006 17:07:59 +0000 Subject: r19786: My last checkin to winreg_StringBuf killed rpccli_winreg_EnumKeys against W2k3. The server requires that size==0 in the [in] name. Somehow I get the feeling that something is badly wrong here.... I did not yet recreate the gen_ndr equivalent, see next mail. Volker (This used to be commit 016ddce12005bb0829bf050e4d4851852751b3e5) --- source3/lib/util_str.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 414a87a562..fc13b75cc5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1644,6 +1644,24 @@ size_t strlen_m_term(const char *s) return strlen_m(s) + 1; } +/* + * Weird helper routine for the winreg pipe: If nothing is around, return 0, + * if a string is there, include the terminator. + */ + +size_t strlen_m_term_null(const char *s) +{ + size_t len; + if (!s) { + return 0; + } + len = strlen_m(s); + if (len == 0) { + return 0; + } + + return len+1; +} /** Return a RFC2254 binary string representation of a buffer. Used in LDAP filters. -- cgit From 63609fbb04d2ce620338b4b79e7c1abf39f08ef8 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 9 Dec 2006 02:58:18 +0000 Subject: r20090: Fix a class of bugs found by James Peach. Ensure we never mix malloc and talloc'ed contexts in the add_XX_to_array() and add_XX_to_array_unique() calls. Ensure that these calls always return False on out of memory, True otherwise and always check them. Ensure that the relevent parts of the conn struct and the nt_user_tokens are TALLOC_DESTROYED not SAFE_FREE'd. James - this should fix your crash bug in both branches. Jeremy. (This used to be commit 0ffca7559e07500bd09a64b775e230d448ce5c24) --- source3/lib/util_str.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index fc13b75cc5..cd52faa52d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2428,8 +2428,10 @@ BOOL add_string_to_array(TALLOC_CTX *mem_ctx, *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings, const char *, (*num)+1); - if ((*strings == NULL) || (dup_str == NULL)) + if ((*strings == NULL) || (dup_str == NULL)) { + *num = 0; return False; + } (*strings)[*num] = dup_str; *num += 1; -- cgit From a179d2f495e8dfb9ad30c2b7cec5349cb4e001af Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 16 Dec 2006 05:02:21 +0000 Subject: r20208: Change sprintf_append() never to use malloc, but always use a talloc context. Thanks to simo for pointing this out. Jeremy. (This used to be commit 437cb7c88833d7eab0e3c3dcf175860df74a7a38) --- source3/lib/util_str.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index cd52faa52d..ccf0af8b62 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2458,11 +2458,7 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, if (*bufsize == 0) *bufsize = 128; - if (mem_ctx != NULL) - *string = TALLOC_ARRAY(mem_ctx, char, *bufsize); - else - *string = SMB_MALLOC_ARRAY(char, *bufsize); - + *string = TALLOC_ARRAY(mem_ctx, char, *bufsize); if (*string == NULL) goto error; } @@ -2484,13 +2480,8 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, } if (increased) { - if (mem_ctx != NULL) { - *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char, - *bufsize); - } else { - *string = SMB_REALLOC_ARRAY(*string, char, *bufsize); - } - + *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char, + *bufsize); if (*string == NULL) { goto error; } @@ -2503,9 +2494,6 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, error: *len = -1; - if (mem_ctx == NULL) { - SAFE_FREE(*string); - } *string = NULL; } -- cgit From 261c004d7bf85de945a1a3956c1d8f15075bc224 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 30 Mar 2007 22:25:08 +0000 Subject: r22014: Make us pass RANDOMIPC test again :-(. This is an ugly check-in, but I've no option. Jeremy. (This used to be commit c3a565081d70b209a4f9e6e8f1859bf7194a5f74) --- source3/lib/util_str.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index ccf0af8b62..032627db94 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -424,16 +424,45 @@ void string_replace( pstring s, char oldc, char newc ) } /** - Skip past some strings in a buffer. -**/ + * Skip past some strings in a buffer - old version - no checks. + * **/ -char *skip_string(char *buf,size_t n) +char *push_skip_string(char *buf,size_t n) { while (n--) buf += strlen(buf) + 1; return(buf); } +/** + Skip past some strings in a buffer. Buffer may not be + null terminated. end_ptr points to the first byte after + then end of the buffer. +**/ + +char *skip_string(const char *base, size_t len, char *buf, size_t n) +{ + const char *end_ptr = base + len; + + if (end_ptr < base || !base || !buf || buf >= end_ptr) { + return NULL; + } + + while (n--) { + /* Skip the string */ + while (*buf) { + buf++; + if (buf >= end_ptr) { + return NULL; + } + } + /* Skip the '\0' */ + buf++; + } + + return buf; +} + /** Count the number of characters in a string. Normally this will be the same as the number of bytes in a string for single byte strings, @@ -2591,4 +2620,3 @@ size_t utf16_len_n(const void *src, size_t n) return len; } - -- cgit From 0a2cc569a1803f459f7db77d03e6e90ae30aa35d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 2 Apr 2007 20:10:21 +0000 Subject: r22045: As Volker noticed, skip_string's last argument is redundent. Remove it. Jeremy. (This used to be commit 140881cfbb59ce4a699b5900efe02bf315be7bd5) --- source3/lib/util_str.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 032627db94..457232c2b2 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -427,20 +427,19 @@ void string_replace( pstring s, char oldc, char newc ) * Skip past some strings in a buffer - old version - no checks. * **/ -char *push_skip_string(char *buf,size_t n) +char *push_skip_string(char *buf) { - while (n--) - buf += strlen(buf) + 1; + buf += strlen(buf) + 1; return(buf); } /** - Skip past some strings in a buffer. Buffer may not be + Skip past a string in a buffer. Buffer may not be null terminated. end_ptr points to the first byte after then end of the buffer. **/ -char *skip_string(const char *base, size_t len, char *buf, size_t n) +char *skip_string(const char *base, size_t len, char *buf) { const char *end_ptr = base + len; @@ -448,18 +447,15 @@ char *skip_string(const char *base, size_t len, char *buf, size_t n) return NULL; } - while (n--) { - /* Skip the string */ - while (*buf) { - buf++; - if (buf >= end_ptr) { - return NULL; - } - } - /* Skip the '\0' */ + /* Skip the string */ + while (*buf) { buf++; + if (buf >= end_ptr) { + return NULL; + } } - + /* Skip the '\0' */ + buf++; return buf; } -- cgit From d34f6bb969092166c961e328229b1b05a30f6930 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 14 May 2007 14:23:51 +0000 Subject: r22852: merge fixes for CVE-2007-2446 and CVE-2007-2447 to all branches (This used to be commit f65214be68c1a59d9598bfb9f3b19e71cc3fa07b) --- source3/lib/util_str.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 457232c2b2..1439ac6fcd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2616,3 +2616,166 @@ size_t utf16_len_n(const void *src, size_t n) return len; } + +/******************************************************************* + Add a shell escape character '\' to any character not in a known list + of characters. UNIX charset format. +*******************************************************************/ + +#define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz_/ \t.," +#define INSIDE_DQUOTE_LIST "$`\n\"\\" + +char *escape_shell_string(const char *src) +{ + size_t srclen = strlen(src); + char *ret = SMB_MALLOC((srclen * 2) + 1); + char *dest = ret; + BOOL in_s_quote = False; + BOOL in_d_quote = False; + BOOL next_escaped = False; + + if (!ret) { + return NULL; + } + + while (*src) { + size_t c_size; + codepoint_t c = next_codepoint(src, &c_size); + + if (c == INVALID_CODEPOINT) { + SAFE_FREE(ret); + return NULL; + } + + if (c_size > 1) { + memcpy(dest, src, c_size); + src += c_size; + dest += c_size; + next_escaped = False; + continue; + } + + /* + * Deal with backslash escaped state. + * This only lasts for one character. + */ + + if (next_escaped) { + *dest++ = *src++; + next_escaped = False; + continue; + } + + /* + * Deal with single quote state. The + * only thing we care about is exiting + * this state. + */ + + if (in_s_quote) { + if (*src == '\'') { + in_s_quote = False; + } + *dest++ = *src++; + continue; + } + + /* + * Deal with double quote state. The most + * complex state. We must cope with \, meaning + * possibly escape next char (depending what it + * is), ", meaning exit this state, and possibly + * add an \ escape to any unprotected character + * (listed in INSIDE_DQUOTE_LIST). + */ + + if (in_d_quote) { + if (*src == '\\') { + /* + * Next character might be escaped. + * We have to peek. Inside double + * quotes only INSIDE_DQUOTE_LIST + * characters are escaped by a \. + */ + + char nextchar; + + c = next_codepoint(&src[1], &c_size); + if (c == INVALID_CODEPOINT) { + SAFE_FREE(ret); + return NULL; + } + if (c_size > 1) { + /* + * Don't escape the next char. + * Just copy the \. + */ + *dest++ = *src++; + continue; + } + + nextchar = src[1]; + + if (nextchar && strchr(INSIDE_DQUOTE_LIST, (int)nextchar)) { + next_escaped = True; + } + *dest++ = *src++; + continue; + } + + if (*src == '\"') { + /* Exit double quote state. */ + in_d_quote = False; + *dest++ = *src++; + continue; + } + + /* + * We know the character isn't \ or ", + * so escape it if it's any of the other + * possible unprotected characters. + */ + + if (strchr(INSIDE_DQUOTE_LIST, (int)*src)) { + *dest++ = '\\'; + } + *dest++ = *src++; + continue; + } + + /* + * From here to the end of the loop we're + * not in the single or double quote state. + */ + + if (*src == '\\') { + /* Next character must be escaped. */ + next_escaped = True; + *dest++ = *src++; + continue; + } + + if (*src == '\'') { + /* Go into single quote state. */ + in_s_quote = True; + *dest++ = *src++; + continue; + } + + if (*src == '\"') { + /* Go into double quote state. */ + in_d_quote = True; + *dest++ = *src++; + continue; + } + + /* Check if we need to escape the character. */ + + if (!strchr(INCLUDE_LIST, (int)*src)) { + *dest++ = '\\'; + } + *dest++ = *src++; + } + *dest++ = '\0'; + return ret; +} -- cgit From 34bc0e188369af89f4e1680edf9766c568b8a5a8 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 5 Jun 2007 12:58:18 +0000 Subject: r23356: We missed to add the 'c' character to the list of valid ones for shell escaping. I hate this kind of bugs more than how Jeremy hates off by ones :( Simo. (This used to be commit 42d846ff870f93b7eaca316f04c12104330bbca8) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1439ac6fcd..52cdbfcedd 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2622,7 +2622,7 @@ size_t utf16_len_n(const void *src, size_t n) of characters. UNIX charset format. *******************************************************************/ -#define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijklmnopqrstuvwxyz_/ \t.," +#define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.," #define INSIDE_DQUOTE_LIST "$`\n\"\\" char *escape_shell_string(const char *src) -- cgit From 32ba5145b8c8385f56a1e8f025c7be1bc848077b Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 16 Jun 2007 11:48:11 +0000 Subject: r23518: Remove the silly assumption that string_replace requires a pstring. Jeremy, I am always very confused about the different length arguments in convert_string and friends. Can you take a look at the change in string_replace and verify it's ok? Thanks! While at it, remove the pstring limit for strhasupper and strhaslower. (This used to be commit e6e5703658aeaeb0cca5d7281095e17553001d4b) --- source3/lib/util_str.c | 63 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 52cdbfcedd..aaba7300fc 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -126,8 +126,6 @@ BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) return ret; } -static uint16 tmpbuf[sizeof(pstring)]; - void set_first_token(char *ptr) { last_ptr = ptr; @@ -394,9 +392,11 @@ BOOL strisnormal(const char *s, int case_default) NOTE: oldc and newc must be 7 bit characters **/ -void string_replace( pstring s, char oldc, char newc ) +void string_replace( char *s, char oldc, char newc ) { char *p; + smb_ucs2_t *tmp; + size_t src_len, len; /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our @@ -418,9 +418,14 @@ void string_replace( pstring s, char oldc, char newc ) /* With compose characters we must restart from the beginning. JRA. */ p = s; #endif - push_ucs2(NULL, tmpbuf, p, sizeof(tmpbuf), STR_TERMINATE); - string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); - pull_ucs2(NULL, p, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); + src_len = strlen(p); + len = push_ucs2_allocate(&tmp, p); + if (len == -1) { + return; + } + string_replace_w(tmp, UCS2_CHAR(oldc), UCS2_CHAR(newc)); + pull_ucs2(NULL, p, tmp, src_len+1, -1, STR_TERMINATE); + SAFE_FREE(tmp); } /** @@ -584,12 +589,22 @@ BOOL trim_string(char *s,const char *front,const char *back) BOOL strhasupper(const char *s) { - smb_ucs2_t *ptr; - push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(ptr=tmpbuf;*ptr;ptr++) - if(isupper_w(*ptr)) - return True; - return(False); + smb_ucs2_t *tmp, *p; + BOOL ret; + + if (push_ucs2_allocate(&tmp, s) == -1) { + return False; + } + + for(p = tmp; *p != 0; p++) { + if(isupper_w(*p)) { + break; + } + } + + ret = (*p != 0); + SAFE_FREE(tmp); + return ret; } /** @@ -598,12 +613,22 @@ BOOL strhasupper(const char *s) BOOL strhaslower(const char *s) { - smb_ucs2_t *ptr; - push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); - for(ptr=tmpbuf;*ptr;ptr++) - if(islower_w(*ptr)) - return True; - return(False); + smb_ucs2_t *tmp, *p; + BOOL ret; + + if (push_ucs2_allocate(&tmp, s) == -1) { + return False; + } + + for(p = tmp; *p != 0; p++) { + if(islower_w(*p)) { + break; + } + } + + ret = (*p != 0); + SAFE_FREE(tmp); + return ret; } /** @@ -2628,7 +2653,7 @@ size_t utf16_len_n(const void *src, size_t n) char *escape_shell_string(const char *src) { size_t srclen = strlen(src); - char *ret = SMB_MALLOC((srclen * 2) + 1); + char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1); char *dest = ret; BOOL in_s_quote = False; BOOL in_d_quote = False; -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index aaba7300fc..c1f3a620fc 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -9,7 +9,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/util_str.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c1f3a620fc..616bfd2025 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -18,8 +18,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From af3de3fa710f00e26f64db352675bfc9285f632b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 13 Sep 2007 01:10:01 +0000 Subject: r25121: Remove pstring limits from much of our string handling function. Still a few left (mainly the substitute ones). Jeremy. (This used to be commit 6552e52979a7c6954faa8b2c9c0f08b3779835c4) --- source3/lib/util_str.c | 228 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 174 insertions(+), 54 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 616bfd2025..36cd716462 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -6,6 +6,7 @@ Copyright (C) Simo Sorce 2001-2002 Copyright (C) Martin Pool 2003 Copyright (C) James Peach 2006 + Copyright (C) Jeremy Allison 1992-2007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -264,12 +265,63 @@ int StrCaseCmp(const char *s, const char *t) /** Case insensitive string compararison, length limited. **/ -int StrnCaseCmp(const char *s, const char *t, size_t n) +int StrnCaseCmp(const char *s, const char *t, size_t len) { - pstring buf1, buf2; - unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1)); - unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2)); - return strncmp(buf1,buf2,n); + size_t n = 0; + const char *ps, *pt; + size_t size; + smb_ucs2_t *buffer_s, *buffer_t; + int ret; + + for (ps = s, pt = t; n < len ; ps++, pt++, n++) { + char us, ut; + + if (!*ps && !*pt) + return 0; /* both ended */ + else if (!*ps) + return -1; /* s is a prefix */ + else if (!*pt) + return +1; /* t is a prefix */ + else if ((*ps & 0x80) || (*pt & 0x80)) + /* not ascii anymore, do it the + * hard way from here on in */ + break; + + us = toupper_ascii(*ps); + ut = toupper_ascii(*pt); + if (us == ut) + continue; + else if (us < ut) + return -1; + else if (us > ut) + return +1; + } + + if (n == len) { + return 0; + } + + size = push_ucs2_allocate(&buffer_s, ps); + if (size == (size_t)-1) { + return strncmp(ps, pt, len-n); + /* Not quite the right answer, but finding the right one + under this failure case is expensive, + and it's pretty close */ + } + + size = push_ucs2_allocate(&buffer_t, pt); + if (size == (size_t)-1) { + SAFE_FREE(buffer_s); + return strncmp(ps, pt, len-n); + /* Not quite the right answer, but finding the right one + under this failure case is expensive, + and it's pretty close */ + } + + ret = strncasecmp_w(buffer_s, buffer_t, len-n); + SAFE_FREE(buffer_s); + SAFE_FREE(buffer_t); + return ret; } /** @@ -283,7 +335,7 @@ BOOL strequal(const char *s1, const char *s2) return(True); if (!s1 || !s2) return(False); - + return(StrCaseCmp(s1,s2)==0); } @@ -298,7 +350,7 @@ BOOL strnequal(const char *s1,const char *s2,size_t n) return(True); if (!s1 || !s2 || !n) return(False); - + return(StrnCaseCmp(s1,s2,n)==0); } @@ -353,11 +405,16 @@ int strwicmp(const char *psz1, const char *psz2) char *strupper_static(const char *s) { - static pstring str; + static char *str = NULL; - pstrcpy(str, s); + if (str) { + SAFE_FREE(str); + } + str = SMB_STRDUP(s); + if (!str) { + return CONST_DISCARD(char *,s); + } strupper_m(str); - return str; } @@ -381,7 +438,7 @@ BOOL strisnormal(const char *s, int case_default) { if (case_default == CASE_UPPER) return(!strhaslower(s)); - + return(!strhasupper(s)); } @@ -394,8 +451,6 @@ BOOL strisnormal(const char *s, int case_default) void string_replace( char *s, char oldc, char newc ) { char *p; - smb_ucs2_t *tmp; - size_t src_len, len; /* this is quite a common operation, so we want it to be fast. We optimise for the ascii case, knowing that all our @@ -405,8 +460,9 @@ void string_replace( char *s, char oldc, char newc ) for (p = s; *p; p++) { if (*p & 0x80) /* mb string - slow path. */ break; - if (*p == oldc) + if (*p == oldc) { *p = newc; + } } if (!*p) @@ -417,14 +473,18 @@ void string_replace( char *s, char oldc, char newc ) /* With compose characters we must restart from the beginning. JRA. */ p = s; #endif - src_len = strlen(p); - len = push_ucs2_allocate(&tmp, p); - if (len == -1) { - return; + + while (*p) { + size_t c_size; + next_codepoint(p, &c_size); + + if (c_size == 1) { + if (*p == oldc) { + *p = newc; + } + } + p += c_size; } - string_replace_w(tmp, UCS2_CHAR(oldc), UCS2_CHAR(newc)); - pull_ucs2(NULL, p, tmp, src_len+1, -1, STR_TERMINATE); - SAFE_FREE(tmp); } /** @@ -471,9 +531,14 @@ char *skip_string(const char *base, size_t len, char *buf) size_t str_charnum(const char *s) { - uint16 tmpbuf2[sizeof(pstring)]; - push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE); - return strlen_w(tmpbuf2); + size_t ret; + smb_ucs2_t *tmpbuf2 = NULL; + if (push_ucs2_allocate(&tmpbuf2, s) == (size_t)-1) { + return 0; + } + ret = strlen_w(tmpbuf2); + SAFE_FREE(tmpbuf2); + return ret; } /** @@ -914,22 +979,38 @@ char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len) BOOL in_list(const char *s, const char *list, BOOL casesensitive) { - pstring tok; + char *tok; const char *p=list; + size_t bufsize = strlen(list); + BOOL ret = False; if (!list) return(False); - while (next_token(&p,tok,LIST_SEP,sizeof(tok))) { + /* We know a token can't be larger + * than the entire list. */ + + tok = SMB_MALLOC(bufsize+1); + if (!tok) { + return False; + } + + while (next_token(&p,tok,LIST_SEP,bufsize+1)) { if (casesensitive) { - if (strcmp(tok,s) == 0) - return(True); + if (strcmp(tok,s) == 0) { + ret = True; + break; + } } else { - if (StrCaseCmp(tok,s) == 0) - return(True); + if (StrCaseCmp(tok,s) == 0) { + ret = True; + break; + } } } - return(False); + + SAFE_FREE(tok); + return ret; } /* this is used to prevent lots of mallocs of size 1 */ @@ -1377,10 +1458,11 @@ char *string_truncate(char *s, unsigned int length) char *strchr_m(const char *src, char c) { - wpstring ws; - pstring s2; + smb_ucs2_t *ws = NULL; + char *s2 = NULL; smb_ucs2_t *p; const char *s; + char *ret; /* characters below 0x3F are guaranteed to not appear in non-initial position in multi-byte charsets */ @@ -1406,13 +1488,25 @@ char *strchr_m(const char *src, char c) s = src; #endif - push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + if (push_ucs2_allocate(&ws, s)==(size_t)-1) { + /* Wrong answer, but what can we do... */ + return strchr(src, c); + } p = strchr_w(ws, UCS2_CHAR(c)); - if (!p) + if (!p) { + SAFE_FREE(ws); return NULL; + } *p = 0; - pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + if (pull_ucs2_allocate(&s2, ws)==(size_t)-1) { + SAFE_FREE(ws); + /* Wrong answer, but what can we do... */ + return strchr(src, c); + } + ret = (char *)(s+strlen(s2)); + SAFE_FREE(ws); + SAFE_FREE(s2); + return ret; } char *strrchr_m(const char *s, char c) @@ -1458,17 +1552,30 @@ char *strrchr_m(const char *s, char c) /* String contained a non-ascii char. Slow path. */ { - wpstring ws; - pstring s2; + smb_ucs2_t *ws = NULL; + char *s2 = NULL; smb_ucs2_t *p; + char *ret; - push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + if (push_ucs2_allocate(&ws,s)==(size_t)-1) { + /* Wrong answer, but what can we do. */ + return strrchr(s, c); + } p = strrchr_w(ws, UCS2_CHAR(c)); - if (!p) + if (!p) { + SAFE_FREE(ws); return NULL; + } *p = 0; - pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + if (pull_ucs2_allocate(&s2,ws)==(size_t)-1) { + SAFE_FREE(ws); + /* Wrong answer, but what can we do. */ + return strrchr(s, c); + } + ret = (char *)(s+strlen(s2)); + SAFE_FREE(ws); + SAFE_FREE(s2); + return ret; } } @@ -1479,17 +1586,30 @@ char *strrchr_m(const char *s, char c) char *strnrchr_m(const char *s, char c, unsigned int n) { - wpstring ws; - pstring s2; + smb_ucs2_t *ws = NULL; + char *s2 = NULL; smb_ucs2_t *p; + char *ret; - push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); + if (push_ucs2_allocate(&ws,s)==(size_t)-1) { + /* Too hard to try and get right. */ + return NULL; + } p = strnrchr_w(ws, UCS2_CHAR(c), n); - if (!p) + if (!p) { + SAFE_FREE(ws); return NULL; + } *p = 0; - pull_ucs2_pstring(s2, ws); - return (char *)(s+strlen(s2)); + if (pull_ucs2_allocate(&s2,ws)==(size_t)-1) { + SAFE_FREE(ws); + /* Too hard to try and get right. */ + return NULL; + } + ret = (char *)(s+strlen(s2)); + SAFE_FREE(ws); + SAFE_FREE(s2); + return ret; } /*********************************************************************** @@ -1795,7 +1915,7 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co char *s; int num, lsize; pstring tok; - + if (!string || !*string) return NULL; if (mem_ctx) { @@ -1808,12 +1928,12 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co return NULL; } if (!sep) sep = LIST_SEP; - + num = lsize = 0; list = NULL; - + str = s; - while (next_token(&str, tok, sep, sizeof(tok))) { + while (next_token(&str, tok, sep, sizeof(tok))) { if (num == lsize) { lsize += S_LIST_ABS; if (mem_ctx) { @@ -1843,7 +1963,7 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co } else { list[num] = SMB_STRDUP(tok); } - + if (!list[num]) { DEBUG(0,("str_list_make: Unable to allocate memory")); str_list_free(&list); -- cgit From eacd3140573d1122a3785823e4003bfc6352c431 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 13 Sep 2007 22:08:59 +0000 Subject: r25138: More pstring elimination. Add a TALLOC_CTX parameter to unix_convert(). Jeremy. (This used to be commit 39c211a702e91c34c1a5a689e1b0c4530ea8a1ac) --- source3/lib/util_str.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 36cd716462..c2eeb12544 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -549,9 +549,14 @@ size_t str_charnum(const char *s) size_t str_ascii_charnum(const char *s) { - pstring tmpbuf2; - push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE); - return strlen(tmpbuf2); + size_t ret; + char *tmpbuf2 = NULL; + if (push_ascii_allocate(&tmpbuf2, s) == (size_t)-1) { + return 0; + } + ret = strlen(tmpbuf2); + SAFE_FREE(tmpbuf2); + return ret; } BOOL trim_char(char *s,char cfront,char cback) -- cgit From ad97bcf813d5f06df4711896eefc99c0c32651cc Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 15 Sep 2007 20:24:35 +0000 Subject: r25184: Fix some C++ warnings and an uninitialized variable (This used to be commit b64df8a3c504ab7749c21ffb26e4771a9a0a328f) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c2eeb12544..db5775b79f 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -995,7 +995,7 @@ BOOL in_list(const char *s, const char *list, BOOL casesensitive) /* We know a token can't be larger * than the entire list. */ - tok = SMB_MALLOC(bufsize+1); + tok = SMB_MALLOC_ARRAY(char, bufsize+1); if (!tok) { return False; } -- cgit From e5a951325a6cac8567af3a66de6d2df577508ae4 Mon Sep 17 00:00:00 2001 From: "Gerald (Jerry) Carter" Date: Wed, 10 Oct 2007 15:34:30 -0500 Subject: [GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch. (This used to be commit 5c6c8e1fe93f340005110a7833946191659d88ab) --- source3/lib/util_str.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index db5775b79f..fbd9c1ca6d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -447,7 +447,6 @@ BOOL strisnormal(const char *s, int case_default) String replace. NOTE: oldc and newc must be 7 bit characters **/ - void string_replace( char *s, char oldc, char newc ) { char *p; -- cgit From bb126d1f4908a52a442ea81323e2b47e83409868 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 17 Oct 2007 12:10:12 -0700 Subject: Reformatting fix for new coding guidelines. BOOL ->bool. Jeremy. (This used to be commit 315215e20e1e470c5077122a2e250ecb3d45ce9b) --- source3/lib/util_str.c | 584 +++++++++++++++++++++++++++---------------------- 1 file changed, 320 insertions(+), 264 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index fbd9c1ca6d..2fd22280a4 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1,23 +1,23 @@ -/* +/* Unix SMB/CIFS implementation. Samba utility functions - + Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Simo Sorce 2001-2002 Copyright (C) Martin Pool 2003 Copyright (C) James Peach 2006 Copyright (C) Jeremy Allison 1992-2007 - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -30,26 +30,26 @@ **/ /** - * Internal function to get the next token from a string, return False if none + * Internal function to get the next token from a string, return false if none * found. Handles double-quotes. This is the work horse function called by * next_token() and next_token_no_ltrim(). * - * Based on a routine by GJC@VILLAGE.COM. + * Based on a routine by GJC@VILLAGE.COM. * Extensively modified by Andrew.Tridgell@anu.edu.au */ -static BOOL next_token_internal(const char **ptr, +static bool next_token_internal(const char **ptr, char *buff, const char *sep, size_t bufsize, - BOOL ltrim) + bool ltrim) { char *s; char *pbuf; - BOOL quoted; + bool quoted; size_t len=1; if (!ptr) - return(False); + return(false); s = (char *)*ptr; @@ -62,14 +62,15 @@ static BOOL next_token_internal(const char **ptr, while (*s && strchr_m(sep,*s)) s++; } - + /* nothing left? */ if (! *s) - return(False); - + return(false); + /* copy over the token */ pbuf = buff; - for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { + for (quoted = false; len < bufsize && *s && + (quoted || !strchr_m(sep,*s)); s++) { if ( *s == '\"' ) { quoted = !quoted; } else { @@ -77,53 +78,53 @@ static BOOL next_token_internal(const char **ptr, *pbuf++ = *s; } } - - *ptr = (*s) ? s+1 : s; + + *ptr = (*s) ? s+1 : s; *pbuf = 0; - - return(True); + + return(true); } /* - * Get the next token from a string, return False if none found. Handles + * Get the next token from a string, return false if none found. Handles * double-quotes. This version trims leading separator characters before * looking for a token. */ -BOOL next_token(const char **ptr, char *buff, const char *sep, size_t bufsize) +bool next_token(const char **ptr, char *buff, const char *sep, size_t bufsize) { - return next_token_internal(ptr, buff, sep, bufsize, True); + return next_token_internal(ptr, buff, sep, bufsize, true); } /* - * Get the next token from a string, return False if none found. Handles + * Get the next token from a string, return false if none found. Handles * double-quotes. This version does not trim leading separator characters * before looking for a token. */ -BOOL next_token_no_ltrim(const char **ptr, +bool next_token_no_ltrim(const char **ptr, char *buff, const char *sep, size_t bufsize) { - return next_token_internal(ptr, buff, sep, bufsize, False); + return next_token_internal(ptr, buff, sep, bufsize, false); } /** -This is like next_token but is not re-entrant and "remembers" the first +This is like next_token but is not re-entrant and "remembers" the first parameter so you can pass NULL. This is useful for user interface code but beware the fact that it is not re-entrant! **/ static const char *last_ptr=NULL; -BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) +bool next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) { - BOOL ret; + bool ret; if (!ptr) ptr = &last_ptr; ret = next_token(ptr, buff, sep, bufsize); last_ptr = *ptr; - return ret; + return ret; } void set_first_token(char *ptr) @@ -159,14 +160,14 @@ char **toktocliplist(int *ctok, const char *sep) while(*s && strchr_m(sep,*s)) *s++=0; } while(*s); - + *ctok=ictok; s=(char *)last_ptr; - + if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1))) return NULL; - - while(ictok--) { + + while(ictok--) { *iret++=s; if (ictok > 0) { while(*s++) @@ -207,7 +208,7 @@ char **toktocliplist(int *ctok, const char *sep) * different, we'd need to restart the whole thing. * * Even better is to implement strcasecmp for each encoding and use a - * function pointer. + * function pointer. **/ int StrCaseCmp(const char *s, const char *t) { @@ -227,7 +228,8 @@ int StrCaseCmp(const char *s, const char *t) else if (!*pt) return +1; /* t is a prefix */ else if ((*ps & 0x80) || (*pt & 0x80)) - /* not ascii anymore, do it the hard way from here on in */ + /* not ascii anymore, do it the hard way + * from here on in */ break; us = toupper_ascii(*ps); @@ -242,19 +244,21 @@ int StrCaseCmp(const char *s, const char *t) size = push_ucs2_allocate(&buffer_s, ps); if (size == (size_t)-1) { - return strcmp(ps, pt); + return strcmp(ps, pt); /* Not quite the right answer, but finding the right one - under this failure case is expensive, and it's pretty close */ + under this failure case is expensive, and it's pretty + close */ } - + size = push_ucs2_allocate(&buffer_t, pt); if (size == (size_t)-1) { SAFE_FREE(buffer_s); - return strcmp(ps, pt); + return strcmp(ps, pt); /* Not quite the right answer, but finding the right one - under this failure case is expensive, and it's pretty close */ + under this failure case is expensive, and it's pretty + close */ } - + ret = strcasecmp_w(buffer_s, buffer_t); SAFE_FREE(buffer_s); SAFE_FREE(buffer_t); @@ -329,12 +333,12 @@ int StrnCaseCmp(const char *s, const char *t, size_t len) * * @note The comparison is case-insensitive. **/ -BOOL strequal(const char *s1, const char *s2) +bool strequal(const char *s1, const char *s2) { if (s1 == s2) - return(True); + return(true); if (!s1 || !s2) - return(False); + return(false); return(StrCaseCmp(s1,s2)==0); } @@ -344,12 +348,12 @@ BOOL strequal(const char *s1, const char *s2) * * @note The comparison is case-insensitive. **/ -BOOL strnequal(const char *s1,const char *s2,size_t n) +bool strnequal(const char *s1,const char *s2,size_t n) { if (s1 == s2) - return(True); + return(true); if (!s1 || !s2 || !n) - return(False); + return(false); return(StrnCaseCmp(s1,s2,n)==0); } @@ -358,13 +362,13 @@ BOOL strnequal(const char *s1,const char *s2,size_t n) Compare 2 strings (case sensitive). **/ -BOOL strcsequal(const char *s1,const char *s2) +bool strcsequal(const char *s1,const char *s2) { if (s1 == s2) - return(True); + return(true); if (!s1 || !s2) - return(False); - + return(false); + return(strcmp(s1,s2)==0); } @@ -389,8 +393,8 @@ int strwicmp(const char *psz1, const char *psz2) psz1++; while (isspace((int)*psz2)) psz2++; - if (toupper_ascii(*psz1) != toupper_ascii(*psz2) || *psz1 == '\0' - || *psz2 == '\0') + if (toupper_ascii(*psz1) != toupper_ascii(*psz2) || + *psz1 == '\0' || *psz2 == '\0') break; psz1++; psz2++; @@ -434,7 +438,7 @@ void strnorm(char *s, int case_default) Check if a string is in "normal" case. **/ -BOOL strisnormal(const char *s, int case_default) +bool strisnormal(const char *s, int case_default) { if (case_default == CASE_UPPER) return(!strhaslower(s)); @@ -558,15 +562,15 @@ size_t str_ascii_charnum(const char *s) return ret; } -BOOL trim_char(char *s,char cfront,char cback) +bool trim_char(char *s,char cfront,char cback) { - BOOL ret = False; + bool ret = false; char *ep; char *fp = s; /* Ignore null or empty strings. */ if (!s || (s[0] == '\0')) - return False; + return false; if (cfront) { while (*fp && *fp == cfront) @@ -574,17 +578,17 @@ BOOL trim_char(char *s,char cfront,char cback) if (!*fp) { /* We ate the string. */ s[0] = '\0'; - return True; + return true; } if (fp != s) - ret = True; + ret = true; } ep = fp + strlen(fp) - 1; if (cback) { /* Attempt ascii only. Bail for mb strings. */ while ((ep >= fp) && (*ep == cback)) { - ret = True; + ret = true; if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) { /* Could be mb... bail back to tim_string. */ char fs[2], bs[2]; @@ -602,7 +606,7 @@ BOOL trim_char(char *s,char cfront,char cback) if (ep < fp) { /* We ate the string. */ s[0] = '\0'; - return True; + return true; } } @@ -615,16 +619,16 @@ BOOL trim_char(char *s,char cfront,char cback) Trim the specified elements off the front and back of a string. **/ -BOOL trim_string(char *s,const char *front,const char *back) +bool trim_string(char *s,const char *front,const char *back) { - BOOL ret = False; + bool ret = false; size_t front_len; size_t back_len; size_t len; /* Ignore null or empty strings. */ if (!s || (s[0] == '\0')) - return False; + return false; front_len = front? strlen(front) : 0; back_len = back? strlen(back) : 0; @@ -637,15 +641,16 @@ BOOL trim_string(char *s,const char *front,const char *back) * easily overlap. Found by valgrind. JRA. */ memmove(s, s+front_len, (len-front_len)+1); len -= front_len; - ret=True; + ret=true; } } - + if (back_len) { - while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) { + while ((len >= back_len) && + strncmp(s+len-back_len,back,back_len)==0) { s[len-back_len]='\0'; len -= back_len; - ret=True; + ret=true; } } return ret; @@ -655,13 +660,13 @@ BOOL trim_string(char *s,const char *front,const char *back) Does a string have any uppercase chars in it? **/ -BOOL strhasupper(const char *s) +bool strhasupper(const char *s) { smb_ucs2_t *tmp, *p; - BOOL ret; + bool ret; if (push_ucs2_allocate(&tmp, s) == -1) { - return False; + return false; } for(p = tmp; *p != 0; p++) { @@ -679,13 +684,13 @@ BOOL strhasupper(const char *s) Does a string have any lowercase chars in it? **/ -BOOL strhaslower(const char *s) +bool strhaslower(const char *s) { smb_ucs2_t *tmp, *p; - BOOL ret; + bool ret; if (push_ucs2_allocate(&tmp, s) == -1) { - return False; + return false; } for(p = tmp; *p != 0; p++) { @@ -726,12 +731,17 @@ size_t count_chars(const char *s,char c) include the terminating zero. **/ -char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_t maxlength) +char *safe_strcpy_fn(const char *fn, + int line, + char *dest, + const char *src, + size_t maxlength) { size_t len; if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcpy, called from [%s][%d]\n", fn, line)); + DEBUG(0,("ERROR: NULL dest in safe_strcpy, " + "called from [%s][%d]\n", fn, line)); return NULL; } @@ -742,38 +752,44 @@ char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_ if (!src) { *dest = 0; return dest; - } + } len = strnlen(src, maxlength+1); if (len > maxlength) { - DEBUG(0,("ERROR: string overflow by %lu (%lu - %lu) in safe_strcpy [%.50s]\n", - (unsigned long)(len-maxlength), (unsigned long)len, + DEBUG(0,("ERROR: string overflow by " + "%lu (%lu - %lu) in safe_strcpy [%.50s]\n", + (unsigned long)(len-maxlength), (unsigned long)len, (unsigned long)maxlength, src)); len = maxlength; } - + memmove(dest, src, len); dest[len] = 0; return dest; -} +} /** Safe string cat into a string. maxlength does not include the terminating zero. **/ -char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size_t maxlength) +char *safe_strcat_fn(const char *fn, + int line, + char *dest, + const char *src, + size_t maxlength) { size_t src_len, dest_len; if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcat, called from [%s][%d]\n", fn, line)); + DEBUG(0,("ERROR: NULL dest in safe_strcat, " + "called from [%s][%d]\n", fn, line)); return NULL; } if (!src) return dest; - + src_len = strnlen(src, maxlength + 1); dest_len = strnlen(dest, maxlength + 1); @@ -782,7 +798,8 @@ char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size #endif if (src_len + dest_len > maxlength) { - DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n", + DEBUG(0,("ERROR: string overflow by %d " + "in safe_strcat [%.50s]\n", (int)(src_len + dest_len - maxlength), src)); if (maxlength > dest_len) { memcpy(&dest[dest_len], src, maxlength - dest_len); @@ -802,7 +819,13 @@ char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size and replaces with '_'. Deliberately does *NOT* check for multibyte characters. Don't change it ! **/ -char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, const char *other_safe_chars, size_t maxlength) + +char *alpha_strcpy_fn(const char *fn, + int line, + char *dest, + const char *src, + const char *other_safe_chars, + size_t maxlength) { size_t len, i; @@ -811,14 +834,15 @@ char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, con #endif if (!dest) { - DEBUG(0,("ERROR: NULL dest in alpha_strcpy, called from [%s][%d]\n", fn, line)); + DEBUG(0,("ERROR: NULL dest in alpha_strcpy, " + "called from [%s][%d]\n", fn, line)); return NULL; } if (!src) { *dest = 0; return dest; - } + } len = strlen(src); if (len >= maxlength) @@ -829,7 +853,8 @@ char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, con for(i = 0; i < len; i++) { int val = (src[i] & 0xff); - if (isupper_ascii(val) || islower_ascii(val) || isdigit(val) || strchr_m(other_safe_chars, val)) + if (isupper_ascii(val) || islower_ascii(val) || + isdigit(val) || strchr_m(other_safe_chars, val)) dest[i] = src[i]; else dest[i] = '_'; @@ -853,7 +878,8 @@ char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) #endif if (!dest) { - DEBUG(0,("ERROR: NULL dest in StrnCpy, called from [%s][%d]\n", fn, line)); + DEBUG(0,("ERROR: NULL dest in StrnCpy, " + "called from [%s][%d]\n", fn, line)); return(NULL); } @@ -861,7 +887,7 @@ char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n) *dest = 0; return(dest); } - + while (n-- && (*d = *src)) { d++; src++; @@ -944,7 +970,7 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) return num_chars; } -DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) +DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) { DATA_BLOB ret_blob; @@ -953,8 +979,8 @@ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) else ret_blob = data_blob(NULL, strlen(strhex)/2+1); - ret_blob.length = strhex_to_str((char*)ret_blob.data, - strlen(strhex), + ret_blob.length = strhex_to_str((char*)ret_blob.data, + strlen(strhex), strhex); return ret_blob; @@ -981,33 +1007,33 @@ char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len) Check if a string is part of a list. **/ -BOOL in_list(const char *s, const char *list, BOOL casesensitive) +bool in_list(const char *s, const char *list, bool casesensitive) { char *tok; const char *p=list; size_t bufsize = strlen(list); - BOOL ret = False; + bool ret = false; if (!list) - return(False); + return(false); /* We know a token can't be larger * than the entire list. */ tok = SMB_MALLOC_ARRAY(char, bufsize+1); if (!tok) { - return False; + return false; } while (next_token(&p,tok,LIST_SEP,bufsize+1)) { if (casesensitive) { if (strcmp(tok,s) == 0) { - ret = True; + ret = true; break; } } else { if (StrCaseCmp(tok,s) == 0) { - ret = True; + ret = true; break; } } @@ -1024,11 +1050,11 @@ static const char *null_string = ""; Set a string value, allocing the space for the string **/ -static BOOL string_init(char **dest,const char *src) +static bool string_init(char **dest,const char *src) { size_t l; - if (!src) + if (!src) src = ""; l = strlen(src); @@ -1039,10 +1065,10 @@ static BOOL string_init(char **dest,const char *src) (*dest) = SMB_STRDUP(src); if ((*dest) == NULL) { DEBUG(0,("Out of memory in string_init\n")); - return False; + return false; } } - return(True); + return(true); } /** @@ -1063,17 +1089,17 @@ void string_free(char **s) for the string **/ -BOOL string_set(char **dest,const char *src) +bool string_set(char **dest,const char *src) { string_free(dest); return(string_init(dest,src)); } /** - Substitute a string for a pattern in another string. Make sure there is + Substitute a string for a pattern in another string. Make sure there is enough room! - This routine looks for pattern in s and replaces it with + This routine looks for pattern in s and replaces it with insert. It may do multiple replacements or just one. Any of " ; ' $ or ` in the insert string are replaced with _ @@ -1081,8 +1107,9 @@ BOOL string_set(char **dest,const char *src) use of len==0 which was for no length checks to be done. **/ -void string_sub2(char *s,const char *pattern, const char *insert, size_t len, - BOOL remove_unsafe_characters, BOOL replace_once, BOOL allow_trailing_dollar) +void string_sub2(char *s,const char *pattern, const char *insert, size_t len, + bool remove_unsafe_characters, bool replace_once, + bool allow_trailing_dollar) { char *p; ssize_t ls,lp,li, i; @@ -1099,7 +1126,8 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, while (lp <= ls && (p = strstr_m(s,pattern))) { if (ls + (li-lp) >= len) { - DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", + DEBUG(0,("ERROR: string overflow by " + "%d in string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), pattern, (int)len)); break; @@ -1114,7 +1142,8 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, case '\'': case ';': case '$': - /* allow a trailing $ (as in machine accounts) */ + /* allow a trailing $ + * (as in machine accounts) */ if (allow_trailing_dollar && (i == li - 1 )) { p[i] = insert[i]; break; @@ -1124,8 +1153,9 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, case '\n': if ( remove_unsafe_characters ) { p[i] = '_'; - /* yes this break should be here since we want to - fall throw if not replacing unsafe chars */ + /* yes this break should be here + * since we want to fall throw if + * not replacing unsafe chars */ break; } default: @@ -1140,14 +1170,15 @@ void string_sub2(char *s,const char *pattern, const char *insert, size_t len, } } -void string_sub_once(char *s, const char *pattern, const char *insert, size_t len) +void string_sub_once(char *s, const char *pattern, + const char *insert, size_t len) { - string_sub2( s, pattern, insert, len, True, True, False ); + string_sub2( s, pattern, insert, len, true, true, false ); } void string_sub(char *s,const char *pattern, const char *insert, size_t len) { - string_sub2( s, pattern, insert, len, True, False, False ); + string_sub2( s, pattern, insert, len, true, false, false ); } void fstring_sub(char *s,const char *pattern,const char *insert) @@ -1204,13 +1235,14 @@ char *realloc_string_sub(char *string, const char *pattern, break; } } - + while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); string = (char *)SMB_REALLOC(string, ls + ld + 1); if (!string) { - DEBUG(0, ("realloc_string_sub: out of memory!\n")); + DEBUG(0, ("realloc_string_sub: " + "out of memory!\n")); SAFE_FREE(in); return NULL; } @@ -1273,7 +1305,7 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, break; } } - + while ((p = strstr_m(s,pattern))) { if (ld > 0) { int offset = PTR_DIFF(s,string); @@ -1299,7 +1331,7 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, } /** - Similar to string_sub() but allows for any character to be substituted. + Similar to string_sub() but allows for any character to be substituted. Use with caution! if len==0 then the string cannot be extended. This is different from the old use of len==0 which was for no length checks to be done. @@ -1319,13 +1351,14 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) if (!*pattern) return; - + if (len == 0) len = ls + 1; /* len is number of *bytes* */ - + while (lp <= ls && (p = strstr_m(s,pattern))) { if (ls + (li-lp) >= len) { - DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", + DEBUG(0,("ERROR: string overflow by " + "%d in all_string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), pattern, (int)len)); break; @@ -1346,7 +1379,8 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) Use with caution! **/ -static smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern, +static smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, + const smb_ucs2_t *pattern, const smb_ucs2_t *insert) { smb_ucs2_t *r, *rp; @@ -1409,7 +1443,8 @@ smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, Splits out the front and back at a separator. **/ -static void split_at_last_component(char *path, char *front, char sep, char *back) +static void split_at_last_component(char *path, char *front, char sep, + char *back) { char *p = strrchr_m(path, sep); @@ -1456,7 +1491,7 @@ char *string_truncate(char *s, unsigned int length) } /** - Strchr and strrchr_m are very hard to do on general multi-byte strings. + Strchr and strrchr_m are very hard to do on general multi-byte strings. We convert via ucs2 for now. **/ @@ -1533,7 +1568,7 @@ char *strrchr_m(const char *s, char c) { size_t len = strlen(s); const char *cp = s; - BOOL got_mb = False; + bool got_mb = false; if (len == 0) return NULL; @@ -1541,9 +1576,10 @@ char *strrchr_m(const char *s, char c) do { if (c == *cp) { /* Could be a match. Part of a multibyte ? */ - if ((cp > s) && (((unsigned char)cp[-1]) & 0x80)) { + if ((cp > s) && + (((unsigned char)cp[-1]) & 0x80)) { /* Yep - go slow :-( */ - got_mb = True; + got_mb = true; break; } /* No - we have a match ! */ @@ -1645,7 +1681,7 @@ char *strstr_m(const char *src, const char *findstr) for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) { if (*s == *findstr) { - if (!findstr_len) + if (!findstr_len) findstr_len = strlen(findstr); if (strncmp(s, findstr, findstr_len) == 0) { @@ -1668,7 +1704,7 @@ char *strstr_m(const char *src, const char *findstr) DEBUG(0,("strstr_m: src malloc fail\n")); return NULL; } - + if (push_ucs2_allocate(&find_w, findstr) == (size_t)-1) { SAFE_FREE(src_w); DEBUG(0,("strstr_m: find malloc fail\n")); @@ -1682,7 +1718,7 @@ char *strstr_m(const char *src, const char *findstr) SAFE_FREE(find_w); return NULL; } - + *p = 0; if (pull_ucs2_allocate(&s2, src_w) == (size_t)-1) { SAFE_FREE(src_w); @@ -1724,7 +1760,7 @@ void strlower_m(char *s) len = strlen(s) + 1; errno_save = errno; errno = 0; - unix_strlower(s,len,s,len); + unix_strlower(s,len,s,len); /* Catch mb conversion errors that may not terminate. */ if (errno) s[len-1] = '\0'; @@ -1758,7 +1794,7 @@ void strupper_m(char *s) len = strlen(s) + 1; errno_save = errno; errno = 0; - unix_strupper(s,len,s,len); + unix_strupper(s,len,s,len); /* Catch mb conversion errors that may not terminate. */ if (errno) s[len-1] = '\0'; @@ -1912,7 +1948,9 @@ int fstr_sprintf(fstring s, const char *fmt, ...) #define S_LIST_ABS 16 /* List Allocation Block Size */ -static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, const char *sep) +static char **str_list_make_internal(TALLOC_CTX *mem_ctx, + const char *string, + const char *sep) { char **list, **rlist; const char *str; @@ -1941,14 +1979,18 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co if (num == lsize) { lsize += S_LIST_ABS; if (mem_ctx) { - rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *, lsize +1); + rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list, + char *, lsize +1); } else { - /* We need to keep the old list on error so we can free the elements + /* We need to keep the old list on + * error so we can free the elements if the realloc fails. */ - rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, char *, lsize +1); + rlist =SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, + char *, lsize +1); } if (!rlist) { - DEBUG(0,("str_list_make: Unable to allocate memory")); + DEBUG(0,("str_list_make: " + "Unable to allocate memory")); str_list_free(&list); if (mem_ctx) { TALLOC_FREE(s); @@ -1959,7 +2001,8 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co } else { list = rlist; } - memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1))); + memset (&list[num], 0, + ((sizeof(char**)) * (S_LIST_ABS +1))); } if (mem_ctx) { @@ -1978,8 +2021,8 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co } return NULL; } - - num++; + + num++; } if (mem_ctx) { @@ -1991,7 +2034,9 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, co return list; } -char **str_list_make_talloc(TALLOC_CTX *mem_ctx, const char *string, const char *sep) +char **str_list_make_talloc(TALLOC_CTX *mem_ctx, + const char *string, + const char *sep) { return str_list_make_internal(mem_ctx, string, sep); } @@ -2001,72 +2046,75 @@ char **str_list_make(const char *string, const char *sep) return str_list_make_internal(NULL, string, sep); } -BOOL str_list_copy(char ***dest, const char **src) +bool str_list_copy(char ***dest, const char **src) { char **list, **rlist; int num, lsize; - + *dest = NULL; if (!src) - return False; - + return false; + num = lsize = 0; list = NULL; - + while (src[num]) { if (num == lsize) { lsize += S_LIST_ABS; - rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, char *, lsize +1); + rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, + char *, lsize +1); if (!rlist) { - DEBUG(0,("str_list_copy: Unable to re-allocate memory")); + DEBUG(0,("str_list_copy: " + "Unable to re-allocate memory")); str_list_free(&list); - return False; + return false; } else { list = rlist; } - memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1))); + memset (&list[num], 0, + ((sizeof(char **)) * (S_LIST_ABS +1))); } - + list[num] = SMB_STRDUP(src[num]); if (!list[num]) { DEBUG(0,("str_list_copy: Unable to allocate memory")); str_list_free(&list); - return False; + return false; } num++; } - + *dest = list; - return True; + return true; } /** * Return true if all the elements of the list match exactly. **/ -BOOL str_list_compare(char **list1, char **list2) +bool str_list_compare(char **list1, char **list2) { int num; - + if (!list1 || !list2) - return (list1 == list2); - + return (list1 == list2); + for (num = 0; list1[num]; num++) { if (!list2[num]) - return False; + return false; if (!strcsequal(list1[num], list2[num])) - return False; + return false; } if (list2[num]) - return False; /* if list2 has more elements than list1 fail */ - - return True; + return false; /* if list2 has more elements than list1 fail */ + + return true; } static void str_list_free_internal(TALLOC_CTX *mem_ctx, char ***list) { char **tlist; - + if (!list || !*list) return; tlist = *list; @@ -2105,59 +2153,60 @@ int str_list_count( const char **list ) return 0; /* count the number of list members */ - + for ( i=0; *list; i++, list++ ); - + return i; } /****************************************************************************** - version of standard_sub_basic() for string lists; uses alloc_sub_basic() + version of standard_sub_basic() for string lists; uses alloc_sub_basic() for the work *****************************************************************************/ - -BOOL str_list_sub_basic( char **list, const char *smb_name, + +bool str_list_sub_basic( char **list, const char *smb_name, const char *domain_name ) { char *s, *tmpstr; - + while ( *list ) { s = *list; tmpstr = alloc_sub_basic(smb_name, domain_name, s); if ( !tmpstr ) { - DEBUG(0,("str_list_sub_basic: alloc_sub_basic() return NULL!\n")); - return False; + DEBUG(0,("str_list_sub_basic: " + "alloc_sub_basic() return NULL!\n")); + return false; } SAFE_FREE(*list); *list = tmpstr; - + list++; } - return True; + return true; } /****************************************************************************** substritute a specific pattern in a string list *****************************************************************************/ - -BOOL str_list_substitute(char **list, const char *pattern, const char *insert) + +bool str_list_substitute(char **list, const char *pattern, const char *insert) { char *p, *s, *t; ssize_t ls, lp, li, ld, i, d; if (!list) - return False; + return false; if (!pattern) - return False; + return false; if (!insert) - return False; + return false; lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); ld = li -lp; - + while (*list) { s = *list; ls = (ssize_t)strlen(s); @@ -2168,8 +2217,9 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) if (ld) { t = (char *) SMB_MALLOC(ls +ld +1); if (!t) { - DEBUG(0,("str_list_substitute: Unable to allocate memory")); - return False; + DEBUG(0,("str_list_substitute: " + "Unable to allocate memory")); + return false; } memcpy(t, *list, d); memcpy(t +d +li, p +lp, ls -d -lp +1); @@ -2178,7 +2228,7 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) ls += ld; s = t +d +li; } - + for (i = 0; i < li; i++) { switch (insert[i]) { case '`': @@ -2194,14 +2244,13 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) default: t[d +i] = insert[i]; } - } + } } - - + list++; } - - return True; + + return true; } @@ -2221,10 +2270,10 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert) * reallocated to new length **/ -char* ipstr_list_add(char** ipstr_list, const struct ip_service *service) +char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) { - char* new_ipstr = NULL; - + char *new_ipstr = NULL; + /* arguments checking */ if (!ipstr_list || !service) return NULL; @@ -2234,7 +2283,8 @@ char* ipstr_list_add(char** ipstr_list, const struct ip_service *service) inet_ntoa(service->ip), service->port); SAFE_FREE(*ipstr_list); } else { - asprintf(&new_ipstr, "%s:%d", inet_ntoa(service->ip), service->port); + asprintf(&new_ipstr, "%s:%d", + inet_ntoa(service->ip), service->port); } *ipstr_list = new_ipstr; return *ipstr_list; @@ -2250,58 +2300,58 @@ char* ipstr_list_add(char** ipstr_list, const struct ip_service *service) * @param ip_count number of addresses stored in ip_list * @return pointer to allocated ip string **/ - -char* ipstr_list_make(char** ipstr_list, const struct ip_service* ip_list, int ip_count) + +char *ipstr_list_make(char **ipstr_list, + const struct ip_service * ip_list, int ip_count) { int i; - + /* arguments checking */ if (!ip_list || !ipstr_list) return 0; *ipstr_list = NULL; - + /* process ip addresses given as arguments */ for (i = 0; i < ip_count; i++) *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]); - + return (*ipstr_list); } /** * Parse given ip string list into array of ip addresses - * (as ip_service structures) + * (as ip_service structures) * e.g. 192.168.1.100:389,192.168.1.78, ... * - * @param ipstr ip string list to be parsed + * @param ipstr ip string list to be parsed * @param ip_list pointer to array of ip addresses which is * allocated by this function and must be freed by caller * @return number of succesfully parsed addresses **/ - + int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list) { fstring token_str; size_t count; int i; - if (!ipstr_list || !ip_list) + if (!ipstr_list || !ip_list) return 0; - + count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) { - DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n", (unsigned long)count)); + DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n", + (unsigned long)count)); return 0; } - - for ( i=0; - next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN) && i= 'a' && c2 <= 'f') c2 = 10 + c2 - 'a'; else {p++; continue;} - + *p = (c1<<4) | c2; memmove(p+1, p+3, strlen(p+3)+1); @@ -2428,9 +2478,11 @@ void base64_decode_inplace(char *s) /** * Encode a base64 string into a malloc()ed string caller to free. * - *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments + * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c + * with adjustments **/ -char * base64_encode_data_blob(DATA_BLOB data) + +char *base64_encode_data_blob(DATA_BLOB data) { int bits = 0; int char_count = 0; @@ -2453,27 +2505,27 @@ char * base64_encode_data_blob(DATA_BLOB data) result[out_cnt++] = b64[bits >> 18]; result[out_cnt++] = b64[(bits >> 12) & 0x3f]; result[out_cnt++] = b64[(bits >> 6) & 0x3f]; - result[out_cnt++] = b64[bits & 0x3f]; - bits = 0; - char_count = 0; - } else { - bits <<= 8; - } - } - if (char_count != 0) { - bits <<= 16 - (8 * char_count); - result[out_cnt++] = b64[bits >> 18]; - result[out_cnt++] = b64[(bits >> 12) & 0x3f]; - if (char_count == 1) { - result[out_cnt++] = '='; - result[out_cnt++] = '='; - } else { - result[out_cnt++] = b64[(bits >> 6) & 0x3f]; - result[out_cnt++] = '='; + result[out_cnt++] = b64[bits & 0x3f]; + bits = 0; + char_count = 0; + } else { + bits <<= 8; + } } - } - result[out_cnt] = '\0'; /* terminate */ - return result; + if (char_count != 0) { + bits <<= 16 - (8 * char_count); + result[out_cnt++] = b64[bits >> 18]; + result[out_cnt++] = b64[(bits >> 12) & 0x3f]; + if (char_count == 1) { + result[out_cnt++] = '='; + result[out_cnt++] = '='; + } else { + result[out_cnt++] = b64[(bits >> 6) & 0x3f]; + result[out_cnt++] = '='; + } + } + result[out_cnt] = '\0'; /* terminate */ + return result; } /* read a SMB_BIG_UINT from a string */ @@ -2482,7 +2534,7 @@ SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) SMB_BIG_UINT val = -1; const char *p = nptr; - + if (!p) { if (entptr) { *entptr = p; @@ -2494,7 +2546,7 @@ SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) p++; #ifdef LARGE_SMB_OFF_T - sscanf(p,"%llu",&val); + sscanf(p,"%llu",&val); #else /* LARGE_SMB_OFF_T */ sscanf(p,"%lu",&val); #endif /* LARGE_SMB_OFF_T */ @@ -2593,22 +2645,23 @@ void string_append(char **left, const char *right) safe_strcat(*left, right, new_len-1); } -BOOL add_string_to_array(TALLOC_CTX *mem_ctx, +bool add_string_to_array(TALLOC_CTX *mem_ctx, const char *str, const char ***strings, int *num) { char *dup_str = talloc_strdup(mem_ctx, str); - *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings, const char *, (*num)+1); + *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings, + const char *, (*num)+1); if ((*strings == NULL) || (dup_str == NULL)) { *num = 0; - return False; + return false; } (*strings)[*num] = dup_str; *num += 1; - return True; + return true; } /* Append an sprintf'ed string. Double buffer size on demand. Usable without @@ -2621,7 +2674,7 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, va_list ap; char *newstr; int ret; - BOOL increased; + bool increased; /* len<0 is an internal marker that something failed */ if (*len < 0) @@ -2643,10 +2696,10 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, if (ret < 0) goto error; - increased = False; + increased = false; while ((*len)+ret >= *bufsize) { - increased = True; + increased = true; *bufsize *= 2; if (*bufsize >= (1024*1024*256)) goto error; @@ -2702,18 +2755,20 @@ char *sstring_sub(const char *src, char front, char back) characters. ********************************************************************/ -BOOL validate_net_name( const char *name, const char *invalid_chars, int max_len ) +bool validate_net_name( const char *name, + const char *invalid_chars, + int max_len) { int i; for ( i=0; i Date: Wed, 24 Oct 2007 14:16:54 -0700 Subject: This is a large patch (sorry). Migrate from struct in_addr to struct sockaddr_storage in most places that matter (ie. not the nmbd and NetBIOS lookups). This passes make test on an IPv4 box, but I'll have to do more work/testing on IPv6 enabled boxes. This should now give us a framework for testing and finishing the IPv6 migration. It's at the state where someone with a working IPv6 setup should (theorecically) be able to type : smbclient //ipv6-address/share and have it work. Jeremy. (This used to be commit 98e154c3125d5732c37a72d74b0eb5cd7b6155fd) --- source3/lib/util_str.c | 83 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 23 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2fd22280a4..226bf826fb 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2270,27 +2270,54 @@ bool str_list_substitute(char **list, const char *pattern, const char *insert) * reallocated to new length **/ -char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) +static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) { char *new_ipstr = NULL; + char addr_buf[INET6_ADDRSTRLEN]; /* arguments checking */ - if (!ipstr_list || !service) return NULL; + if (!ipstr_list || !service) { + return NULL; + } /* attempt to convert ip to a string and append colon separator to it */ if (*ipstr_list) { - asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list, IPSTR_LIST_SEP, - inet_ntoa(service->ip), service->port); + print_sockaddr(addr_buf, + sizeof(addr_buf), + &service->ss); + if (service->ss.ss_family == AF_INET) { + /* IPv4 */ + asprintf(&new_ipstr, "%s%s%s:%d", + *ipstr_list, + IPSTR_LIST_SEP, + addr_buf, + service->port); + } else { + /* IPv6 */ + asprintf(&new_ipstr, "%s%s[%s]:%d", + *ipstr_list, + IPSTR_LIST_SEP, + addr_buf, + service->port); + } SAFE_FREE(*ipstr_list); } else { - asprintf(&new_ipstr, "%s:%d", - inet_ntoa(service->ip), service->port); + if (service->ss.ss_family == AF_INET) { + /* IPv4 */ + asprintf(&new_ipstr, "%s:%d", + addr_buf, + service->port); + } else { + /* IPv6 */ + asprintf(&new_ipstr, "[%s]:%d", + addr_buf, + service->port); + } } *ipstr_list = new_ipstr; return *ipstr_list; } - /** * Allocate and initialise an ipstr list using ip adresses * passed as arguments. @@ -2302,18 +2329,22 @@ char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) **/ char *ipstr_list_make(char **ipstr_list, - const struct ip_service * ip_list, int ip_count) + const struct ip_service *ip_list, + int ip_count) { int i; /* arguments checking */ - if (!ip_list || !ipstr_list) return 0; + if (!ip_list || !ipstr_list) { + return 0; + } *ipstr_list = NULL; /* process ip addresses given as arguments */ - for (i = 0; i < ip_count; i++) + for (i = 0; i < ip_count; i++) { *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]); + } return (*ipstr_list); } @@ -2322,7 +2353,7 @@ char *ipstr_list_make(char **ipstr_list, /** * Parse given ip string list into array of ip addresses * (as ip_service structures) - * e.g. 192.168.1.100:389,192.168.1.78, ... + * e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ... * * @param ipstr ip string list to be parsed * @param ip_list pointer to array of ip addresses which is @@ -2330,7 +2361,7 @@ char *ipstr_list_make(char **ipstr_list, * @return number of succesfully parsed addresses **/ -int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list) +int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list) { fstring token_str; size_t count; @@ -2348,27 +2379,34 @@ int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list) for ( i=0; next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN) && i Date: Thu, 25 Oct 2007 19:07:25 -0700 Subject: Fix bug in writing names into gencache as well as 2 typos where AF_INET6 was mistypes as AF_INET. JERRY YOU NEED THESE FIXES. Fixes smbclient -L localhost -U% Bugs reported by Kukks (thanks kukks). Jeremy. (This used to be commit f109f82622ca30ae2360e8300152e90b9587ffd8) --- source3/lib/util_str.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 226bf826fb..1f3aab3bce 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2280,11 +2280,12 @@ static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) return NULL; } + print_sockaddr(addr_buf, + sizeof(addr_buf), + &service->ss); + /* attempt to convert ip to a string and append colon separator to it */ if (*ipstr_list) { - print_sockaddr(addr_buf, - sizeof(addr_buf), - &service->ss); if (service->ss.ss_family == AF_INET) { /* IPv4 */ asprintf(&new_ipstr, "%s%s%s:%d", -- cgit From 2d73e86ccef0ba66309ce764ebe9df0deb076bb8 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 1 Nov 2007 17:42:35 +0100 Subject: Remove unnecessary code SAFE_FREE checks for NULL anyway, and SMB_STRDUP panics on failure (This used to be commit 4cdebda39b9b1790fc5c7df3dc81bfb46b047ad7) --- source3/lib/util_str.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1f3aab3bce..6458ae3e05 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -411,13 +411,8 @@ char *strupper_static(const char *s) { static char *str = NULL; - if (str) { - SAFE_FREE(str); - } + SAFE_FREE(str); str = SMB_STRDUP(s); - if (!str) { - return CONST_DISCARD(char *,s); - } strupper_m(str); return str; } -- cgit From 62b97b01561e332d3b566c4f70cc2601e2d7fcac Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 28 Oct 2007 19:15:08 +0100 Subject: Make base64_encode_data_blob return a talloced string (This used to be commit 5f205ab48d8ac3b7af573ea0be1ce095ab835448) --- source3/lib/util_str.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6458ae3e05..f1078c6383 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2528,7 +2528,8 @@ char *base64_encode_data_blob(DATA_BLOB data) out_cnt = 0; len = data.length; output_len = data.length * 2; - result = (char *)SMB_MALLOC(output_len); /* get us plenty of space */ + result = TALLOC_ARRAY(talloc_tos(), char, output_len); /* get us plenty of space */ + SMB_ASSERT(result != NULL); while (len-- && out_cnt < (data.length * 2) - 5) { int c = (unsigned char) *(data.data++); -- cgit From 79266500cd3f84c74b2f89ceeb15c23cedacc2b5 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 12 Nov 2007 15:02:50 -0800 Subject: Remove all pstrings from smbd/chgpasswd.c. Jeremy. (This used to be commit eaf14c701b08e9eff5b94bf57af68cb29142d7fc) --- source3/lib/util_str.c | 64 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f1078c6383..6d429e3719 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1254,22 +1254,27 @@ char *realloc_string_sub(char *string, const char *pattern, return string; } -/* Same as string_sub, but returns a talloc'ed string */ +/* + * Internal guts of talloc_string_sub and talloc_all_string_sub. + * 'filter' differentiates between them. + */ -char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, - const char *pattern, const char *insert) +static char *talloc_string_sub_internal(TALLOC_CTX *mem_ctx, const char *src, + const char *pattern, const char *insert, bool filter) { char *p, *in; char *s; char *string; ssize_t ls,lp,li,ld, i; - if (!insert || !pattern || !*pattern || !src || !*src) + if (!insert || !pattern || !*pattern || !src || !*src) { return NULL; + } string = talloc_strdup(mem_ctx, src); if (string == NULL) { - DEBUG(0, ("talloc_strdup failed\n")); + DEBUG(0, ("talloc_string_sub_internal: " + "talloc_strdup failed\n")); return NULL; } @@ -1277,27 +1282,30 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src, in = SMB_STRDUP(insert); if (!in) { - DEBUG(0, ("talloc_string_sub: out of memory!\n")); + DEBUG(0, ("talloc_string_sub_internal: ENOMEM\n")); return NULL; } ls = (ssize_t)strlen(s); lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); ld = li - lp; - for (i=0;i Date: Wed, 14 Nov 2007 16:05:42 -0800 Subject: Remove smbldap_get_single_pstring() and all pstrings from pdb_ldap.c. I don't have an LDAP passdb setup here, so I'm going to need some help on testing this. Jeremy. (This used to be commit 00760451b6c2b65f3a8a9187789ca4f270b622a2) --- source3/lib/util_str.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6d429e3719..68b06a6d90 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1187,14 +1187,17 @@ void pstring_sub(char *s,const char *pattern,const char *insert) } /** - Similar to string_sub, but it will accept only allocated strings + Similar to string_sub2, but it will accept only allocated strings and may realloc them so pay attention at what you pass on no pointers inside strings, no pstrings or const may be passed as string. **/ -char *realloc_string_sub(char *string, const char *pattern, - const char *insert) +char *realloc_string_sub2(char *string, + const char *pattern, + const char *insert, + bool remove_unsafe_characters, + bool allow_trailing_dollar) { char *p, *in; char *s; @@ -1221,10 +1224,18 @@ char *realloc_string_sub(char *string, const char *pattern, case '\'': case ';': case '$': + /* allow a trailing $ + * (as in machine accounts) */ + if (allow_trailing_dollar && (i == li - 1 )) { + break; + } case '%': case '\r': case '\n': - in[i] = '_'; + if ( remove_unsafe_characters ) { + in[i] = '_'; + break; + } default: /* ok */ break; @@ -1254,6 +1265,13 @@ char *realloc_string_sub(char *string, const char *pattern, return string; } +char *realloc_string_sub(char *string, + const char *pattern, + const char *insert) +{ + return realloc_string_sub2(string, pattern, insert, true, false); +} + /* * Internal guts of talloc_string_sub and talloc_all_string_sub. * 'filter' differentiates between them. -- cgit From 4c6b01b0ef289bb1511c30354ed41b597288c9d7 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 19 Nov 2007 18:56:22 -0800 Subject: Remove more pstring. Unify talloc_sub functions to make them a better match for replacing string_sub. Remove more unused code. Jeremy. (This used to be commit ae7885711f504f1442335f09088cbe149a7e00f9) --- source3/lib/util_str.c | 130 ++++++++++++++++--------------------------------- 1 file changed, 41 insertions(+), 89 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 68b06a6d90..f5a50b360e 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1274,11 +1274,15 @@ char *realloc_string_sub(char *string, /* * Internal guts of talloc_string_sub and talloc_all_string_sub. - * 'filter' differentiates between them. + * talloc version of string_sub2. */ -static char *talloc_string_sub_internal(TALLOC_CTX *mem_ctx, const char *src, - const char *pattern, const char *insert, bool filter) +char *talloc_string_sub2(TALLOC_CTX *mem_ctx, const char *src, + const char *pattern, + const char *insert, + bool remove_unsafe_characters, + bool replace_once, + bool allow_trailing_dollar) { char *p, *in; char *s; @@ -1291,7 +1295,7 @@ static char *talloc_string_sub_internal(TALLOC_CTX *mem_ctx, const char *src, string = talloc_strdup(mem_ctx, src); if (string == NULL) { - DEBUG(0, ("talloc_string_sub_internal: " + DEBUG(0, ("talloc_string_sub2: " "talloc_strdup failed\n")); return NULL; } @@ -1300,7 +1304,7 @@ static char *talloc_string_sub_internal(TALLOC_CTX *mem_ctx, const char *src, in = SMB_STRDUP(insert); if (!in) { - DEBUG(0, ("talloc_string_sub_internal: ENOMEM\n")); + DEBUG(0, ("talloc_string_sub2: ENOMEM\n")); return NULL; } ls = (ssize_t)strlen(s); @@ -1308,22 +1312,28 @@ static char *talloc_string_sub_internal(TALLOC_CTX *mem_ctx, const char *src, li = (ssize_t)strlen(insert); ld = li - lp; - if (filter) { - for (i=0;i lp) { - const smb_ucs2_t *st = s; - int ld = li - lp; - while ((sp = strstr_w(st, pattern))) { - st = sp + lp; - lt += ld; - } - } - - r = rp = SMB_MALLOC_ARRAY(smb_ucs2_t, lt + 1); - if (!r) { - DEBUG(0, ("all_string_sub_w: out of memory!\n")); - return NULL; - } - - while ((sp = strstr_w(s, pattern))) { - memcpy(rp, s, (sp - s)); - rp += ((sp - s) / sizeof(smb_ucs2_t)); - memcpy(rp, insert, (li * sizeof(smb_ucs2_t))); - s = sp + lp; - rp += li; - } - lr = ((rp - r) / sizeof(smb_ucs2_t)); - if (lr < lt) { - memcpy(rp, s, ((lt - lr) * sizeof(smb_ucs2_t))); - rp += (lt - lr); - } - *rp = 0; - - return r; -} - -smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern, - const char *insert) -{ - wpstring p, i; - - if (!insert || !pattern || !s) - return NULL; - push_ucs2(NULL, p, pattern, sizeof(wpstring) - 1, STR_TERMINATE); - push_ucs2(NULL, i, insert, sizeof(wpstring) - 1, STR_TERMINATE); - return all_string_sub_w(s, p, i); + return talloc_string_sub2(ctx, src, pattern, insert, + false, false, false); } #if 0 -- cgit From 810f760afd814c869df91b47ae3c5de958edb355 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 30 Nov 2007 13:09:04 -0800 Subject: Add talloc versions of all the next_token() functions. Now I can really start removing fixed length strings... Jeremy. (This used to be commit 0ae61e26547e594e94037d4474a008221e5df8cf) --- source3/lib/util_str.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f5a50b360e..886ae2a043 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -85,6 +85,73 @@ static bool next_token_internal(const char **ptr, return(true); } +static bool next_token_internal_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep, + bool ltrim) +{ + char *s; + char *pbuf; + bool quoted; + size_t len=1; + + *pp_buff = NULL; + if (!ptr) { + return(false); + } + + s = (char *)*ptr; + + /* default to simple separators */ + if (!sep) { + sep = " \t\n\r"; + } + + /* find the first non sep char, if left-trimming is requested */ + if (ltrim) { + while (*s && strchr_m(sep,*s)) + s++; + } + + /* nothing left? */ + if (!*s) { + return(false); + } + + /* Work out the length needed. */ + for (quoted = false; *s && + (quoted || !strchr_m(sep,*s)); s++) { + if (*s == '\"') { + quoted = !quoted; + } else { + len++; + } + } + + /* We started with len = 1 so we have space for the nul. */ + *pp_buff = TALLOC_ARRAY(ctx, char, len); + if (!*pp_buff) { + return false; + } + + /* copy over the token */ + pbuf = *pp_buff; + for (quoted = false; *s && + (quoted || !strchr_m(sep,*s)); s++) { + if ( *s == '\"' ) { + quoted = !quoted; + } else { + *pbuf++ = *s; + } + } + + *ptr = (*s) ? s+1 : s; + *pbuf = 0; + + return true; +} + /* * Get the next token from a string, return false if none found. Handles * double-quotes. This version trims leading separator characters before @@ -92,7 +159,15 @@ static bool next_token_internal(const char **ptr, */ bool next_token(const char **ptr, char *buff, const char *sep, size_t bufsize) { - return next_token_internal(ptr, buff, sep, bufsize, true); + return next_token_internal(ptr, buff, sep, bufsize, true); +} + +bool next_token_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true); } /* @@ -105,7 +180,15 @@ bool next_token_no_ltrim(const char **ptr, const char *sep, size_t bufsize) { - return next_token_internal(ptr, buff, sep, bufsize, false); + return next_token_internal(ptr, buff, sep, bufsize, false); +} + +bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false); } /** @@ -119,14 +202,30 @@ static const char *last_ptr=NULL; bool next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) { bool ret; - if (!ptr) + if (!ptr) { ptr = &last_ptr; + } ret = next_token(ptr, buff, sep, bufsize); last_ptr = *ptr; return ret; } +bool next_token_nr_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + bool ret; + if (!ptr) { + ptr = &last_ptr; + } + + ret = next_token_talloc(ctx, ptr, pp_buff, sep); + last_ptr = *ptr; + return ret; +} + void set_first_token(char *ptr) { last_ptr = ptr; -- cgit From d1807be93c44b5140f07de7e3c272afc65e5c08b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 30 Nov 2007 14:15:34 -0800 Subject: Fix restart after length count. Jeremy. (This used to be commit fa8115f32bfd37f75c284ff0f6906dbc2af0f40c) --- source3/lib/util_str.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 886ae2a043..a0ca03a972 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -92,6 +92,7 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx, bool ltrim) { char *s; + char *saved_s; char *pbuf; bool quoted; size_t len=1; @@ -116,9 +117,12 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx, /* nothing left? */ if (!*s) { - return(false); + return false; } + /* When restarting we need to go from here. */ + saved_s = s; + /* Work out the length needed. */ for (quoted = false; *s && (quoted || !strchr_m(sep,*s)); s++) { @@ -137,6 +141,7 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx, /* copy over the token */ pbuf = *pp_buff; + s = saved_s; for (quoted = false; *s && (quoted || !strchr_m(sep,*s)); s++) { if ( *s == '\"' ) { -- cgit From 6f46f75dfc2c80b99a6a5fb277bab456a5fd247b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 3 Dec 2007 17:17:05 -0800 Subject: Make strhex_to_str clear on string limits. Remove pstring from web/*.c Jeremy. (This used to be commit f9c8d62389f8cb47837e5360209936176537df13) --- source3/lib/util_str.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index a0ca03a972..7cd0f78439 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1034,7 +1034,7 @@ static char *strncpyn(char *dest, const char *src, size_t n, char c) **/ -size_t strhex_to_str(char *p, size_t len, const char *strhex) +size_t strhex_to_str(char *buf, size_t buf_len, const char *strhex, size_t strhex_len) { size_t i; size_t num_chars = 0; @@ -1042,7 +1042,7 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) const char *hexchars = "0123456789ABCDEF"; char *p1 = NULL, *p2 = NULL; - for (i = 0; i < len && strhex[i] != 0; i++) { + for (i = 0; i < strhex_len && strhex[i] != 0; i++) { if (strnequal(hexchars, "0x", 2)) { i++; /* skip two chars */ continue; @@ -1060,7 +1060,10 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex) hinybble = PTR_DIFF(p1, hexchars); lonybble = PTR_DIFF(p2, hexchars); - p[num_chars] = (hinybble << 4) | lonybble; + if (num_chars >= buf_len) { + break; + } + buf[num_chars] = (hinybble << 4) | lonybble; num_chars++; p1 = NULL; @@ -1079,8 +1082,9 @@ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) ret_blob = data_blob(NULL, strlen(strhex)/2+1); ret_blob.length = strhex_to_str((char*)ret_blob.data, - strlen(strhex), - strhex); + ret_blob.length, + strhex, + strlen(strhex)); return ret_blob; } -- cgit From 74dfee916f1e7f9ad01c93bda94e1fba9b89be9d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 4 Dec 2007 17:23:19 -0800 Subject: Remove tok pstring from util_str.c Do we even make rpctorture anymore ? Jeremy. (This used to be commit fecc3cc45af6145fad9a0570e6cae8422bd2443d) --- source3/lib/util_str.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7cd0f78439..01e81e48c3 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2053,7 +2053,8 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *str; char *s; int num, lsize; - pstring tok; + char *tok; + TALLOC_CTX *frame = NULL; if (!string || !*string) return NULL; @@ -2072,7 +2073,8 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, list = NULL; str = s; - while (next_token(&str, tok, sep, sizeof(tok))) { + frame = talloc_stackframe(); + while (next_token_talloc(frame, &str, &tok, sep)) { if (num == lsize) { lsize += S_LIST_ABS; if (mem_ctx) { @@ -2094,6 +2096,7 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, } else { SAFE_FREE(s); } + TALLOC_FREE(frame); return NULL; } else { list = rlist; @@ -2116,12 +2119,15 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, } else { SAFE_FREE(s); } + TALLOC_FREE(frame); return NULL; } num++; } + TALLOC_FREE(frame); + if (mem_ctx) { TALLOC_FREE(s); } else { -- cgit From 3ec5a37280c1e780785439de93522b302f324d24 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 4 Dec 2007 18:02:06 -0800 Subject: Ok, down to just the client/*.c code now. Jeremy. (This used to be commit 7d3959f81a5439800b813ef052382e67424c90cd) --- source3/lib/util_str.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 01e81e48c3..b862299519 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1289,11 +1289,6 @@ void fstring_sub(char *s,const char *pattern,const char *insert) string_sub(s, pattern, insert, sizeof(fstring)); } -void pstring_sub(char *s,const char *pattern,const char *insert) -{ - string_sub(s, pattern, insert, sizeof(pstring)); -} - /** Similar to string_sub2, but it will accept only allocated strings and may realloc them so pay attention at what you pass on no -- cgit From 78c6ee0090f4122bc25baaacb5546517ad4b7bc6 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 24 Nov 2007 17:27:54 +0100 Subject: Remove some globals (This used to be commit 31d0a846db08d845e6cdfd85def4ac1c34031e02) --- source3/lib/util_str.c | 73 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 23 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index b862299519..f26c8b8a77 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -506,21 +506,6 @@ int strwicmp(const char *psz1, const char *psz2) return (*psz1 - *psz2); } - -/** - Convert a string to upper case, but don't modify it. -**/ - -char *strupper_static(const char *s) -{ - static char *str = NULL; - - SAFE_FREE(str); - str = SMB_STRDUP(s); - strupper_m(str); - return str; -} - /** Convert a string to "normal" form. **/ @@ -1147,7 +1132,7 @@ bool in_list(const char *s, const char *list, bool casesensitive) } /* this is used to prevent lots of mallocs of size 1 */ -static const char *null_string = ""; +static const char null_string[] = ""; /** Set a string value, allocing the space for the string @@ -1561,13 +1546,17 @@ static void split_at_last_component(char *path, char *front, char sep, Write an octal as a string. **/ -const char *octal_string(int i) +char *octal_string(int i) { - static char ret[64]; - if (i == -1) - return "-1"; - slprintf(ret, sizeof(ret)-1, "0%o", i); - return ret; + char *result; + if (i == -1) { + result = talloc_strdup(talloc_tos(), "-1"); + } + else { + result = talloc_asprintf(talloc_tos(), "0%o", i); + } + SMB_ASSERT(result != NULL); + return result; } @@ -2552,7 +2541,7 @@ void rfc1738_unescape(char *buf) } } -static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /** * Decode a base64 string into a DATA_BLOB - simple and slow algorithm @@ -2860,6 +2849,44 @@ void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, *string = NULL; } +/* + * asprintf into a string and strupper_m it after that. + */ + +int asprintf_strupper_m(char **strp, const char *fmt, ...) +{ + va_list ap; + char *result; + int ret; + + va_start(ap, fmt); + ret = vasprintf(&result, fmt, ap); + va_end(ap); + + if (ret == -1) + return -1; + + strupper_m(result); + *strp = result; + return ret; +} + +char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...) +{ + va_list ap; + char *ret; + + va_start(ap, fmt); + ret = talloc_vasprintf(t, fmt, ap); + va_end(ap); + + if (ret == NULL) { + return NULL; + } + strupper_m(ret); + return ret; +} + /* Returns the substring from src between the first occurrence of the char "front" and the first occurence of the char "back". -- cgit From 9e8180b9835fc100c25ef230747f7b44ef03d685 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 7 Dec 2007 12:02:44 -0800 Subject: Remove pstrings completely except for smbctool (what does this do ?). Don't build this for now. Jeremy. (This used to be commit 46b67fd82c795d1a34a1efca9e409c0f3fa4f3a2) --- source3/lib/util_str.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f26c8b8a77..0bf4ac83b2 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1277,7 +1277,7 @@ void fstring_sub(char *s,const char *pattern,const char *insert) /** Similar to string_sub2, but it will accept only allocated strings and may realloc them so pay attention at what you pass on no - pointers inside strings, no pstrings or const may be passed + pointers inside strings, no const may be passed as string. **/ @@ -1992,21 +1992,6 @@ char *binary_string(char *buf, int len) s[j] = 0; return s; } -/** - Just a typesafety wrapper for snprintf into a pstring. -**/ - - int pstr_sprintf(pstring s, const char *fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = vsnprintf(s, PSTRING_LEN, fmt, ap); - va_end(ap); - return ret; -} - /** Just a typesafety wrapper for snprintf into a fstring. -- cgit From acf15ae730c95443681404c76b67ccfca0253d8b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 7 Dec 2007 12:26:32 -0800 Subject: Don't build rpctorture anymore - not maintained. Just remove. Remove all vestiges of pstring (except for smbctool as noted in previous commit). Jeremy (This used to be commit 4c32a22ac50ada3275d2ffba3c1aa08bee7d1549) --- source3/lib/util_str.c | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 0bf4ac83b2..2c0d86ec9a 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1515,33 +1515,6 @@ char *talloc_all_string_sub(TALLOC_CTX *ctx, false, false, false); } -#if 0 -/** - Splits out the front and back at a separator. -**/ - -static void split_at_last_component(char *path, char *front, char sep, - char *back) -{ - char *p = strrchr_m(path, sep); - - if (p != NULL) - *p = 0; - - if (front != NULL) - pstrcpy(front, path); - - if (p != NULL) { - if (back != NULL) - pstrcpy(back, p+1); - *p = '\\'; - } else { - if (back != NULL) - back[0] = 0; - } -} -#endif - /** Write an octal as a string. **/ -- cgit From 42cfffae80480eae4381902fff3f7c61f858a933 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 7 Dec 2007 17:32:32 -0800 Subject: Remove next_token - all uses must now be next_token_talloc. No more temptations to use static length strings. Jeremy. (This used to be commit ec003f39369910dee852b7cafb883ddaa321c2de) --- source3/lib/util_str.c | 110 +++++++------------------------------------------ 1 file changed, 16 insertions(+), 94 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 2c0d86ec9a..ee76e33de8 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -29,62 +29,6 @@ * @brief String utilities. **/ -/** - * Internal function to get the next token from a string, return false if none - * found. Handles double-quotes. This is the work horse function called by - * next_token() and next_token_no_ltrim(). - * - * Based on a routine by GJC@VILLAGE.COM. - * Extensively modified by Andrew.Tridgell@anu.edu.au - */ -static bool next_token_internal(const char **ptr, - char *buff, - const char *sep, - size_t bufsize, - bool ltrim) -{ - char *s; - char *pbuf; - bool quoted; - size_t len=1; - - if (!ptr) - return(false); - - s = (char *)*ptr; - - /* default to simple separators */ - if (!sep) - sep = " \t\n\r"; - - /* find the first non sep char, if left-trimming is requested */ - if (ltrim) { - while (*s && strchr_m(sep,*s)) - s++; - } - - /* nothing left? */ - if (! *s) - return(false); - - /* copy over the token */ - pbuf = buff; - for (quoted = false; len < bufsize && *s && - (quoted || !strchr_m(sep,*s)); s++) { - if ( *s == '\"' ) { - quoted = !quoted; - } else { - len++; - *pbuf++ = *s; - } - } - - *ptr = (*s) ? s+1 : s; - *pbuf = 0; - - return(true); -} - static bool next_token_internal_talloc(TALLOC_CTX *ctx, const char **ptr, char **pp_buff, @@ -111,8 +55,9 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx, /* find the first non sep char, if left-trimming is requested */ if (ltrim) { - while (*s && strchr_m(sep,*s)) + while (*s && strchr_m(sep,*s)) { s++; + } } /* nothing left? */ @@ -157,6 +102,7 @@ static bool next_token_internal_talloc(TALLOC_CTX *ctx, return true; } +#if 0 /* * Get the next token from a string, return false if none found. Handles * double-quotes. This version trims leading separator characters before @@ -166,6 +112,7 @@ bool next_token(const char **ptr, char *buff, const char *sep, size_t bufsize) { return next_token_internal(ptr, buff, sep, bufsize, true); } +#endif bool next_token_talloc(TALLOC_CTX *ctx, const char **ptr, @@ -180,13 +127,6 @@ bool next_token_talloc(TALLOC_CTX *ctx, * double-quotes. This version does not trim leading separator characters * before looking for a token. */ -bool next_token_no_ltrim(const char **ptr, - char *buff, - const char *sep, - size_t bufsize) -{ - return next_token_internal(ptr, buff, sep, bufsize, false); -} bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, const char **ptr, @@ -204,18 +144,6 @@ but beware the fact that it is not re-entrant! static const char *last_ptr=NULL; -bool next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize) -{ - bool ret; - if (!ptr) { - ptr = &last_ptr; - } - - ret = next_token(ptr, buff, sep, bufsize); - last_ptr = *ptr; - return ret; -} - bool next_token_nr_talloc(TALLOC_CTX *ctx, const char **ptr, char **pp_buff, @@ -1097,23 +1025,16 @@ char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len) bool in_list(const char *s, const char *list, bool casesensitive) { - char *tok; - const char *p=list; - size_t bufsize = strlen(list); + char *tok = NULL; bool ret = false; + TALLOC_CTX *frame; - if (!list) - return(false); - - /* We know a token can't be larger - * than the entire list. */ - - tok = SMB_MALLOC_ARRAY(char, bufsize+1); - if (!tok) { + if (!list) { return false; } - while (next_token(&p,tok,LIST_SEP,bufsize+1)) { + frame = talloc_stackframe(); + while (next_token_talloc(frame, &list, &tok,LIST_SEP)) { if (casesensitive) { if (strcmp(tok,s) == 0) { ret = true; @@ -1126,8 +1047,7 @@ bool in_list(const char *s, const char *list, bool casesensitive) } } } - - SAFE_FREE(tok); + TALLOC_FREE(frame); return ret; } @@ -2409,7 +2329,8 @@ char *ipstr_list_make(char **ipstr_list, int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list) { - fstring token_str; + TALLOC_CTX *frame; + char *token_str = NULL; size_t count; int i; @@ -2423,8 +2344,9 @@ int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list) return 0; } - for ( i=0; next_token(&ipstr_list, token_str, - IPSTR_LIST_SEP, FSTRING_LEN) && i Date: Wed, 19 Dec 2007 21:59:28 +0100 Subject: Remove next_token_nr_talloc and its associated global Only client.c and clitar.c used this, I think they should carry the static themselves. Also move the a bit funny routine toktocliplist to clitar.c, the only place where it is used. (This used to be commit 86d9412611fd99c21e15c71d30a3f95e35d8535b) --- source3/lib/util_str.c | 77 -------------------------------------------------- 1 file changed, 77 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index ee76e33de8..7e21fe1195 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -136,83 +136,6 @@ bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false); } -/** -This is like next_token but is not re-entrant and "remembers" the first -parameter so you can pass NULL. This is useful for user interface code -but beware the fact that it is not re-entrant! -**/ - -static const char *last_ptr=NULL; - -bool next_token_nr_talloc(TALLOC_CTX *ctx, - const char **ptr, - char **pp_buff, - const char *sep) -{ - bool ret; - if (!ptr) { - ptr = &last_ptr; - } - - ret = next_token_talloc(ctx, ptr, pp_buff, sep); - last_ptr = *ptr; - return ret; -} - -void set_first_token(char *ptr) -{ - last_ptr = ptr; -} - -/** - Convert list of tokens to array; dependent on above routine. - Uses last_ptr from above - bit of a hack. -**/ - -char **toktocliplist(int *ctok, const char *sep) -{ - char *s=(char *)last_ptr; - int ictok=0; - char **ret, **iret; - - if (!sep) - sep = " \t\n\r"; - - while(*s && strchr_m(sep,*s)) - s++; - - /* nothing left? */ - if (!*s) - return(NULL); - - do { - ictok++; - while(*s && (!strchr_m(sep,*s))) - s++; - while(*s && strchr_m(sep,*s)) - *s++=0; - } while(*s); - - *ctok=ictok; - s=(char *)last_ptr; - - if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1))) - return NULL; - - while(ictok--) { - *iret++=s; - if (ictok > 0) { - while(*s++) - ; - while(!*s) - s++; - } - } - - ret[*ctok] = NULL; - return ret; -} - /** * Case insensitive string compararison. * -- cgit From 980ac0984905d8c3f29dd62ed75fc9c7cb22cdd8 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 9 Jan 2008 17:32:26 -0800 Subject: Fixup hot paths - add macro for toupper (c < 0x80). This now matches 3.0.x on my micro-tests. Jeremy. (This used to be commit 329b924cba8225002ca40db26c45b31d141a0925) --- source3/lib/util_str.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7e21fe1195..3e3268104c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -24,6 +24,17 @@ #include "includes.h" +char toupper_ascii_fast_table[128] = { + 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f +}; + /** * @file * @brief String utilities. @@ -187,8 +198,8 @@ int StrCaseCmp(const char *s, const char *t) * from here on in */ break; - us = toupper_ascii(*ps); - ut = toupper_ascii(*pt); + us = toupper_ascii_fast(*ps); + ut = toupper_ascii_fast(*pt); if (us == ut) continue; else if (us < ut) @@ -246,8 +257,8 @@ int StrnCaseCmp(const char *s, const char *t, size_t len) * hard way from here on in */ break; - us = toupper_ascii(*ps); - ut = toupper_ascii(*pt); + us = toupper_ascii_fast(*ps); + ut = toupper_ascii_fast(*pt); if (us == ut) continue; else if (us < ut) @@ -1679,7 +1690,7 @@ void strupper_m(char *s) (ie. they match for the first 128 chars) */ while (*s && !(((unsigned char)s[0]) & 0x80)) { - *s = toupper_ascii((unsigned char)*s); + *s = toupper_ascii_fast((unsigned char)*s); s++; } -- cgit From 805caafd44cbc5fff49711b1a15fb64cc99f3ad3 Mon Sep 17 00:00:00 2001 From: Kai Blin Date: Sat, 19 Jan 2008 02:12:35 +0100 Subject: util_str: Don't return memory from talloc_tos(), use mem_ctx instead. (This used to be commit ab0ee6e9a6a9eee317228f0c2bde254ad9a59b85) --- source3/lib/util_str.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 3e3268104c..bcb9197141 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2415,13 +2415,13 @@ void base64_decode_inplace(char *s) } /** - * Encode a base64 string into a malloc()ed string caller to free. + * Encode a base64 string into a talloc()ed string caller to free. * * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c * with adjustments **/ -char *base64_encode_data_blob(DATA_BLOB data) +char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data) { int bits = 0; int char_count = 0; @@ -2434,7 +2434,7 @@ char *base64_encode_data_blob(DATA_BLOB data) out_cnt = 0; len = data.length; output_len = data.length * 2; - result = TALLOC_ARRAY(talloc_tos(), char, output_len); /* get us plenty of space */ + result = TALLOC_ARRAY(mem_ctx, char, output_len); /* get us plenty of space */ SMB_ASSERT(result != NULL); while (len-- && out_cnt < (data.length * 2) - 5) { -- cgit From 3955801298bbcf69afe01c212d5e8ac9812a5dcf Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 4 Feb 2008 18:49:19 +0100 Subject: Simplify str_list_xxx (This used to be commit d471dd4adb79d480c89436b2ed98f9ec6812aaa0) --- source3/lib/util_str.c | 158 +++++++++++++++---------------------------------- 1 file changed, 47 insertions(+), 111 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index bcb9197141..05be7ca1e4 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1841,95 +1841,64 @@ int fstr_sprintf(fstring s, const char *fmt, ...) #define S_LIST_ABS 16 /* List Allocation Block Size */ -static char **str_list_make_internal(TALLOC_CTX *mem_ctx, - const char *string, - const char *sep) +static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, + const char *sep) { - char **list, **rlist; + char **list; const char *str; char *s; int num, lsize; char *tok; - TALLOC_CTX *frame = NULL; if (!string || !*string) return NULL; - if (mem_ctx) { - s = talloc_strdup(mem_ctx, string); - } else { - s = SMB_STRDUP(string); + + list = TALLOC_ARRAY(mem_ctx, char *, S_LIST_ABS+1); + if (list == NULL) { + return NULL; } - if (!s) { + lsize = S_LIST_ABS; + + s = talloc_strdup(list, string); + if (s == NULL) { DEBUG(0,("str_list_make: Unable to allocate memory")); + TALLOC_FREE(list); return NULL; } if (!sep) sep = LIST_SEP; - num = lsize = 0; - list = NULL; - + num = 0; str = s; - frame = talloc_stackframe(); - while (next_token_talloc(frame, &str, &tok, sep)) { + + while (next_token_talloc(list, &str, &tok, sep)) { + if (num == lsize) { + char **tmp; + lsize += S_LIST_ABS; - if (mem_ctx) { - rlist = TALLOC_REALLOC_ARRAY(mem_ctx, list, - char *, lsize +1); - } else { - /* We need to keep the old list on - * error so we can free the elements - if the realloc fails. */ - rlist =SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, - char *, lsize +1); - } - if (!rlist) { + + tmp = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *, + lsize + 1); + if (tmp == NULL) { DEBUG(0,("str_list_make: " "Unable to allocate memory")); - str_list_free(&list); - if (mem_ctx) { - TALLOC_FREE(s); - } else { - SAFE_FREE(s); - } - TALLOC_FREE(frame); + TALLOC_FREE(list); return NULL; - } else { - list = rlist; } - memset (&list[num], 0, - ((sizeof(char**)) * (S_LIST_ABS +1))); - } - if (mem_ctx) { - list[num] = talloc_strdup(mem_ctx, tok); - } else { - list[num] = SMB_STRDUP(tok); - } + list = tmp; - if (!list[num]) { - DEBUG(0,("str_list_make: Unable to allocate memory")); - str_list_free(&list); - if (mem_ctx) { - TALLOC_FREE(s); - } else { - SAFE_FREE(s); - } - TALLOC_FREE(frame); - return NULL; + memset (&list[num], 0, + ((sizeof(char**)) * (S_LIST_ABS +1))); } - num++; + list[num] = tok; + num += 1; } - TALLOC_FREE(frame); - - if (mem_ctx) { - TALLOC_FREE(s); - } else { - SAFE_FREE(s); - } + list[num] = NULL; + TALLOC_FREE(s); return list; } @@ -1947,43 +1916,31 @@ char **str_list_make(const char *string, const char *sep) bool str_list_copy(char ***dest, const char **src) { - char **list, **rlist; - int num, lsize; + char **list; + int i, num; *dest = NULL; if (!src) return false; - num = lsize = 0; - list = NULL; + num = 0; + while (src[num] != NULL) { + num += 1; + } - while (src[num]) { - if (num == lsize) { - lsize += S_LIST_ABS; - rlist = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list, - char *, lsize +1); - if (!rlist) { - DEBUG(0,("str_list_copy: " - "Unable to re-allocate memory")); - str_list_free(&list); - return false; - } else { - list = rlist; - } - memset (&list[num], 0, - ((sizeof(char **)) * (S_LIST_ABS +1))); - } + list = TALLOC_ARRAY(NULL, char *, num+1); + if (list == NULL) { + return false; + } - list[num] = SMB_STRDUP(src[num]); - if (!list[num]) { - DEBUG(0,("str_list_copy: Unable to allocate memory")); - str_list_free(&list); + for (i=0; i Date: Mon, 4 Feb 2008 20:57:35 +0100 Subject: Always pass a TALLOC_CTX to str_list_make and str_list_copy (This used to be commit e2c9fc4cf5f0ff725330fa44f53782db65fca37e) --- source3/lib/util_str.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 05be7ca1e4..fb3392f041 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1902,19 +1902,12 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, return list; } -char **str_list_make_talloc(TALLOC_CTX *mem_ctx, - const char *string, - const char *sep) +char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep) { return str_list_make_internal(mem_ctx, string, sep); } -char **str_list_make(const char *string, const char *sep) -{ - return str_list_make_internal(NULL, string, sep); -} - -bool str_list_copy(char ***dest, const char **src) +bool str_list_copy(TALLOC_CTX *mem_ctx, char ***dest, const char **src) { char **list; int i, num; @@ -1928,7 +1921,7 @@ bool str_list_copy(char ***dest, const char **src) num += 1; } - list = TALLOC_ARRAY(NULL, char *, num+1); + list = TALLOC_ARRAY(mem_ctx, char *, num+1); if (list == NULL) { return false; } -- cgit From b361956942618ec2f7c2efc60cb190858adbc516 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 4 Feb 2008 21:05:41 +0100 Subject: str_list_free is not needed anymore (This used to be commit feddc1447d585fd108d22a36bccc576fa81197ef) --- source3/lib/util_str.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index fb3392f041..93ecad728d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1841,8 +1841,7 @@ int fstr_sprintf(fstring s, const char *fmt, ...) #define S_LIST_ABS 16 /* List Allocation Block Size */ -static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, - const char *sep) +char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep) { char **list; const char *str; @@ -1902,11 +1901,6 @@ static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, return list; } -char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep) -{ - return str_list_make_internal(mem_ctx, string, sep); -} - bool str_list_copy(TALLOC_CTX *mem_ctx, char ***dest, const char **src) { char **list; @@ -1960,16 +1954,6 @@ bool str_list_compare(char **list1, char **list2) return true; } -void str_list_free_talloc(TALLOC_CTX *mem_ctx, char ***list) -{ - TALLOC_FREE(*list); -} - -void str_list_free(char ***list) -{ - TALLOC_FREE(*list); -} - /****************************************************************************** *****************************************************************************/ -- cgit From 2a6a2288c5fae908f431bd79332554e0a23dbeed Mon Sep 17 00:00:00 2001 From: Karolin Seeger Date: Fri, 8 Feb 2008 09:28:57 +0100 Subject: Fix some typos. Karolin (This used to be commit 2bec0a1fb7857e6fb8ec15e5f597b2d4125f105b) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 93ecad728d..f631dfffee 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2171,7 +2171,7 @@ char *ipstr_list_make(char **ipstr_list, * @param ipstr ip string list to be parsed * @param ip_list pointer to array of ip addresses which is * allocated by this function and must be freed by caller - * @return number of succesfully parsed addresses + * @return number of successfully parsed addresses **/ int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list) -- cgit From 317639287886181edf08ccecad1b324e4cc55d0b Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 25 Feb 2008 15:24:49 +0100 Subject: Fix some warnings warning: ignoring return value of 'asprintf', declared with attribute warn_unused_result (This used to be commit ad37b7b0aee265a3e4d8b7552610f4b9a105434d) --- source3/lib/util_str.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index f631dfffee..cb8a100fa7 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2086,6 +2086,7 @@ static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) { char *new_ipstr = NULL; char addr_buf[INET6_ADDRSTRLEN]; + int ret; /* arguments checking */ if (!ipstr_list || !service) { @@ -2100,33 +2101,30 @@ static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) if (*ipstr_list) { if (service->ss.ss_family == AF_INET) { /* IPv4 */ - asprintf(&new_ipstr, "%s%s%s:%d", - *ipstr_list, - IPSTR_LIST_SEP, - addr_buf, - service->port); + ret = asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list, + IPSTR_LIST_SEP, addr_buf, + service->port); } else { /* IPv6 */ - asprintf(&new_ipstr, "%s%s[%s]:%d", - *ipstr_list, - IPSTR_LIST_SEP, - addr_buf, - service->port); + ret = asprintf(&new_ipstr, "%s%s[%s]:%d", *ipstr_list, + IPSTR_LIST_SEP, addr_buf, + service->port); } SAFE_FREE(*ipstr_list); } else { if (service->ss.ss_family == AF_INET) { /* IPv4 */ - asprintf(&new_ipstr, "%s:%d", - addr_buf, - service->port); + ret = asprintf(&new_ipstr, "%s:%d", addr_buf, + service->port); } else { /* IPv6 */ - asprintf(&new_ipstr, "[%s]:%d", - addr_buf, - service->port); + ret = asprintf(&new_ipstr, "[%s]:%d", addr_buf, + service->port); } } + if (ret == -1) { + return NULL; + } *ipstr_list = new_ipstr; return *ipstr_list; } -- cgit From 768a5a23e001411d26fc293624b9ab2002a90b65 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 7 Apr 2008 10:19:25 +0200 Subject: Fix bug 5375 Thanks to Moskvin for testing (This used to be commit d3c31aa36c451f0a19496cd33c0b055b466e6b09) --- source3/lib/util_str.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index cb8a100fa7..6310e2464d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1972,25 +1972,26 @@ int str_list_count( const char **list ) } /****************************************************************************** - version of standard_sub_basic() for string lists; uses alloc_sub_basic() + version of standard_sub_basic() for string lists; uses talloc_sub_basic() for the work *****************************************************************************/ bool str_list_sub_basic( char **list, const char *smb_name, const char *domain_name ) { + TALLOC_CTX *ctx = list; char *s, *tmpstr; while ( *list ) { s = *list; - tmpstr = alloc_sub_basic(smb_name, domain_name, s); + tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s); if ( !tmpstr ) { DEBUG(0,("str_list_sub_basic: " "alloc_sub_basic() return NULL!\n")); return false; } - SAFE_FREE(*list); + TALLOC_FREE(*list); *list = tmpstr; list++; -- cgit From fb37f156009611af0dd454a0fb0829a09cd638ac Mon Sep 17 00:00:00 2001 From: Tim Prouty Date: Tue, 29 Apr 2008 14:36:24 -0700 Subject: Cleanup size_t return values in callers of convert_string_allocate This patch is the second iteration of an inside-out conversion to cleanup functions in charcnv.c returning size_t == -1 to indicate failure. (This used to be commit 6b189dabc562d86dcaa685419d0cb6ea276f100d) --- source3/lib/util_str.c | 52 ++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 6310e2464d..5a08f7bc2c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -208,16 +208,14 @@ int StrCaseCmp(const char *s, const char *t) return +1; } - size = push_ucs2_allocate(&buffer_s, ps); - if (size == (size_t)-1) { + if (!push_ucs2_allocate(&buffer_s, ps, &size)) { return strcmp(ps, pt); /* Not quite the right answer, but finding the right one under this failure case is expensive, and it's pretty close */ } - size = push_ucs2_allocate(&buffer_t, pt); - if (size == (size_t)-1) { + if (!push_ucs2_allocate(&buffer_t, pt, &size)) { SAFE_FREE(buffer_s); return strcmp(ps, pt); /* Not quite the right answer, but finding the right one @@ -271,16 +269,14 @@ int StrnCaseCmp(const char *s, const char *t, size_t len) return 0; } - size = push_ucs2_allocate(&buffer_s, ps); - if (size == (size_t)-1) { + if (!push_ucs2_allocate(&buffer_s, ps, &size)) { return strncmp(ps, pt, len-n); /* Not quite the right answer, but finding the right one under this failure case is expensive, and it's pretty close */ } - size = push_ucs2_allocate(&buffer_t, pt); - if (size == (size_t)-1) { + if (!push_ucs2_allocate(&buffer_t, pt, &size)) { SAFE_FREE(buffer_s); return strncmp(ps, pt, len-n); /* Not quite the right answer, but finding the right one @@ -480,9 +476,9 @@ char *skip_string(const char *base, size_t len, char *buf) size_t str_charnum(const char *s) { - size_t ret; + size_t ret, converted_size; smb_ucs2_t *tmpbuf2 = NULL; - if (push_ucs2_allocate(&tmpbuf2, s) == (size_t)-1) { + if (!push_ucs2_allocate(&tmpbuf2, s, &converted_size)) { return 0; } ret = strlen_w(tmpbuf2); @@ -498,9 +494,9 @@ size_t str_charnum(const char *s) size_t str_ascii_charnum(const char *s) { - size_t ret; + size_t ret, converted_size; char *tmpbuf2 = NULL; - if (push_ascii_allocate(&tmpbuf2, s) == (size_t)-1) { + if (!push_ascii_allocate(&tmpbuf2, s, &converted_size)) { return 0; } ret = strlen(tmpbuf2); @@ -610,8 +606,9 @@ bool strhasupper(const char *s) { smb_ucs2_t *tmp, *p; bool ret; + size_t converted_size; - if (push_ucs2_allocate(&tmp, s) == -1) { + if (!push_ucs2_allocate(&tmp, s, &converted_size)) { return false; } @@ -634,8 +631,9 @@ bool strhaslower(const char *s) { smb_ucs2_t *tmp, *p; bool ret; + size_t converted_size; - if (push_ucs2_allocate(&tmp, s) == -1) { + if (!push_ucs2_allocate(&tmp, s, &converted_size)) { return false; } @@ -659,8 +657,9 @@ size_t count_chars(const char *s,char c) smb_ucs2_t *ptr; int count; smb_ucs2_t *alloc_tmpbuf = NULL; + size_t converted_size; - if (push_ucs2_allocate(&alloc_tmpbuf, s) == (size_t)-1) { + if (!push_ucs2_allocate(&alloc_tmpbuf, s, &converted_size)) { return 0; } @@ -1410,6 +1409,7 @@ char *strchr_m(const char *src, char c) smb_ucs2_t *p; const char *s; char *ret; + size_t converted_size; /* characters below 0x3F are guaranteed to not appear in non-initial position in multi-byte charsets */ @@ -1435,7 +1435,7 @@ char *strchr_m(const char *src, char c) s = src; #endif - if (push_ucs2_allocate(&ws, s)==(size_t)-1) { + if (!push_ucs2_allocate(&ws, s, &converted_size)) { /* Wrong answer, but what can we do... */ return strchr(src, c); } @@ -1445,7 +1445,7 @@ char *strchr_m(const char *src, char c) return NULL; } *p = 0; - if (pull_ucs2_allocate(&s2, ws)==(size_t)-1) { + if (!pull_ucs2_allocate(&s2, ws, &converted_size)) { SAFE_FREE(ws); /* Wrong answer, but what can we do... */ return strchr(src, c); @@ -1504,8 +1504,9 @@ char *strrchr_m(const char *s, char c) char *s2 = NULL; smb_ucs2_t *p; char *ret; + size_t converted_size; - if (push_ucs2_allocate(&ws,s)==(size_t)-1) { + if (!push_ucs2_allocate(&ws, s, &converted_size)) { /* Wrong answer, but what can we do. */ return strrchr(s, c); } @@ -1515,7 +1516,7 @@ char *strrchr_m(const char *s, char c) return NULL; } *p = 0; - if (pull_ucs2_allocate(&s2,ws)==(size_t)-1) { + if (!pull_ucs2_allocate(&s2, ws, &converted_size)) { SAFE_FREE(ws); /* Wrong answer, but what can we do. */ return strrchr(s, c); @@ -1538,8 +1539,9 @@ char *strnrchr_m(const char *s, char c, unsigned int n) char *s2 = NULL; smb_ucs2_t *p; char *ret; + size_t converted_size; - if (push_ucs2_allocate(&ws,s)==(size_t)-1) { + if (!push_ucs2_allocate(&ws, s, &converted_size)) { /* Too hard to try and get right. */ return NULL; } @@ -1549,7 +1551,7 @@ char *strnrchr_m(const char *s, char c, unsigned int n) return NULL; } *p = 0; - if (pull_ucs2_allocate(&s2,ws)==(size_t)-1) { + if (!pull_ucs2_allocate(&s2, ws, &converted_size)) { SAFE_FREE(ws); /* Too hard to try and get right. */ return NULL; @@ -1572,7 +1574,7 @@ char *strstr_m(const char *src, const char *findstr) char *s2; char *retp; - size_t findstr_len = 0; + size_t converted_size, findstr_len = 0; /* for correctness */ if (!findstr[0]) { @@ -1608,12 +1610,12 @@ char *strstr_m(const char *src, const char *findstr) s = src; #endif - if (push_ucs2_allocate(&src_w, src) == (size_t)-1) { + if (!push_ucs2_allocate(&src_w, src, &converted_size)) { DEBUG(0,("strstr_m: src malloc fail\n")); return NULL; } - if (push_ucs2_allocate(&find_w, findstr) == (size_t)-1) { + if (!push_ucs2_allocate(&find_w, findstr, &converted_size)) { SAFE_FREE(src_w); DEBUG(0,("strstr_m: find malloc fail\n")); return NULL; @@ -1628,7 +1630,7 @@ char *strstr_m(const char *src, const char *findstr) } *p = 0; - if (pull_ucs2_allocate(&s2, src_w) == (size_t)-1) { + if (!pull_ucs2_allocate(&s2, src_w, &converted_size)) { SAFE_FREE(src_w); SAFE_FREE(find_w); DEBUG(0,("strstr_m: dest malloc fail\n")); -- cgit From d4c5a1d504a522c0ade2bb91557e50f6252a0b1c Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Wed, 4 Jun 2008 01:29:22 +0200 Subject: util_str: add talloc_asprintf_strlower_m(). Guenther (This used to be commit 7f8b0b4d151fa4d07758b6fd7b47b0b7c07dda17) --- source3/lib/util_str.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 5a08f7bc2c..1e1108132c 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2615,6 +2615,23 @@ char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...) return ret; } +char *talloc_asprintf_strlower_m(TALLOC_CTX *t, const char *fmt, ...) +{ + va_list ap; + char *ret; + + va_start(ap, fmt); + ret = talloc_vasprintf(t, fmt, ap); + va_end(ap); + + if (ret == NULL) { + return NULL; + } + strlower_m(ret); + return ret; +} + + /* Returns the substring from src between the first occurrence of the char "front" and the first occurence of the char "back". -- cgit From a5ad7a64c6a03e1cf085b21b84af5f2d3e00b947 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 10 Jul 2008 18:12:24 +0200 Subject: Fix a segfault in base64_encode_data_blob We did not allocate enough memory for the \0 and a = at the end (This used to be commit ea110de1dc6257b78631b7d83b7bbb728180c1a1) --- source3/lib/util_str.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 1e1108132c..7cb57adbb5 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2347,7 +2347,9 @@ char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data) out_cnt = 0; len = data.length; - output_len = data.length * 2; + output_len = data.length * 2 + 4; /* Account for closing bytes. 4 is + * random but should be enough for + * the = and \0 */ result = TALLOC_ARRAY(mem_ctx, char, output_len); /* get us plenty of space */ SMB_ASSERT(result != NULL); -- cgit From 933fdd4017fb79539b23222c8f8b538e11133130 Mon Sep 17 00:00:00 2001 From: Karolin Seeger Date: Wed, 23 Jul 2008 17:07:56 +0200 Subject: talloc_string_sub2: Don't return NULL if src is empty. This fixes BUG #5635. Finished print jobs were not removed from the $PRINTER.tdb file if "printing=cups". In print_queue_update, talloc_string_sub2 is used to assemble the "lprm command". In the case of using "printing=cups", the default "lprm command" is an empty string. talloc_string_sub2 is called with this empty string and returns NULL which leads to exiting print_queue_update without doing the actual print queue update. Signed-off by Michael Adam (This used to be commit 03d66554d1bbd9d6c72a3dd5203e5305343c76b8) --- source3/lib/util_str.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 7cb57adbb5..5f26cc80f8 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -1230,7 +1230,7 @@ char *talloc_string_sub2(TALLOC_CTX *mem_ctx, const char *src, char *string; ssize_t ls,lp,li,ld, i; - if (!insert || !pattern || !*pattern || !src || !*src) { + if (!insert || !pattern || !*pattern || !src) { return NULL; } -- cgit From 9efccda1cce9be45ed856c978ddb73c3296cd2bb Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 21 Aug 2008 10:25:02 -0700 Subject: Fix bug 5698 - mixup of TALLOC/malloc. Spotted by Douglas Wegscheid . Jeremy. (This used to be commit 1295bb9787dde69b4be4acee7b66eb782afe9c42) --- source3/lib/util_str.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_str.c') diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 5f26cc80f8..9f952abf10 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -2008,6 +2008,7 @@ bool str_list_sub_basic( char **list, const char *smb_name, bool str_list_substitute(char **list, const char *pattern, const char *insert) { + TALLOC_CTX *ctx = list; char *p, *s, *t; ssize_t ls, lp, li, ld, i, d; @@ -2030,7 +2031,7 @@ bool str_list_substitute(char **list, const char *pattern, const char *insert) t = *list; d = p -t; if (ld) { - t = (char *) SMB_MALLOC(ls +ld +1); + t = TALLOC_ARRAY(ctx, char, ls +ld +1); if (!t) { DEBUG(0,("str_list_substitute: " "Unable to allocate memory")); @@ -2038,7 +2039,7 @@ bool str_list_substitute(char **list, const char *pattern, const char *insert) } memcpy(t, *list, d); memcpy(t +d +li, p +lp, ls -d -lp +1); - SAFE_FREE(*list); + TALLOC_FREE(*list); *list = t; ls += ld; s = t +d +li; -- cgit