*/
#include "includes.h"
+#include "lib/param/loadparm.h"
-const char toupper_ascii_fast_table[128] = {
+static const 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,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
};
-/**
- * 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;
- size_t size;
- smb_ucs2_t *buffer_s, *buffer_t;
- int ret;
-
- 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_ascii_fast(*ps);
- ut = toupper_ascii_fast(*pt);
- if (us == ut)
- continue;
- else if (us < ut)
- return -1;
- else if (us > ut)
- return +1;
- }
-
- if (!push_ucs2_talloc(talloc_tos(), &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 */
- }
-
- if (!push_ucs2_talloc(talloc_tos(), &buffer_t, pt, &size)) {
- TALLOC_FREE(buffer_s);
- 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 */
- }
-
- ret = strcasecmp_w(buffer_s, buffer_t);
- TALLOC_FREE(buffer_s);
- TALLOC_FREE(buffer_t);
- return ret;
-}
-
-
-/**
- Case insensitive string compararison, length limited.
-**/
-int StrnCaseCmp(const char *s, const char *t, size_t len)
-{
- 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_fast(*ps);
- ut = toupper_ascii_fast(*pt);
- if (us == ut)
- continue;
- else if (us < ut)
- return -1;
- else if (us > ut)
- return +1;
- }
-
- if (n == len) {
- return 0;
- }
-
- if (!push_ucs2_talloc(talloc_tos(), &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 */
- }
-
- if (!push_ucs2_talloc(talloc_tos(), &buffer_t, pt, &size)) {
- TALLOC_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);
- TALLOC_FREE(buffer_s);
- TALLOC_FREE(buffer_t);
- return ret;
-}
-
-/**
- * Compare 2 strings.
- *
- * @note The comparison is case-insensitive.
- **/
-bool strequal(const char *s1, const 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.
*
if (!s1 || !s2 || !n)
return(false);
- return(StrnCaseCmp(s1,s2,n)==0);
+ return(strncasecmp_m(s1,s2,n)==0);
}
/**
Convert a string to "normal" form.
**/
-void strnorm(char *s, int case_default)
+bool strnorm(char *s, int case_default)
{
if (case_default == CASE_UPPER)
- strupper_m(s);
+ return strupper_m(s);
else
- strlower_m(s);
+ return strlower_m(s);
}
/**
return ret;
}
-/**
- Safe string copy into a known length string. maxlength does not
- include the terminating zero.
-**/
-
-char *safe_strcpy_fn(char *dest,
- const char *src,
- size_t maxlength)
-{
- size_t len;
-
- if (!dest) {
- smb_panic("ERROR: NULL dest in safe_strcpy");
- }
-
- 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,
- (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(char *dest,
- const char *src,
- size_t maxlength)
-{
- size_t src_len, dest_len;
-
- if (!dest) {
- smb_panic("ERROR: NULL dest in safe_strcat");
- }
-
- if (!src)
- return dest;
-
- src_len = strnlen(src, maxlength + 1);
- dest_len = strnlen(dest, maxlength + 1);
-
- 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));
- if (maxlength > dest_len) {
- memcpy(&dest[dest_len], src, maxlength - dest_len);
- }
- dest[maxlength] = 0;
- return NULL;
- }
-
- memcpy(&dest[dest_len], src, src_len);
- dest[dest_len + src_len] = 0;
- 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)
-{
- char *d = dest;
-
- if (!dest) {
- smb_panic("ERROR: NULL dest in StrnCpy");
- }
-
- if (!src) {
- *dest = 0;
- return(dest);
- }
-
- while (n-- && (*d = *src)) {
- d++;
- src++;
- }
-
- *d = 0;
- return(dest);
-}
-
/**
Check if a string is part of a list.
**/
break;
}
} else {
- if (StrCaseCmp(tok,s) == 0) {
+ if (strcasecmp_m(tok,s) == 0) {
ret = true;
break;
}
return ret;
}
-/* this is used to prevent lots of mallocs of size 1 */
-static const char null_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) {
- *dest = CONST_DISCARD(char*, null_string);
- } else {
- (*dest) = SMB_STRDUP(src);
- if ((*dest) == NULL) {
- DEBUG(0,("Out of memory in string_init\n"));
- return false;
- }
- }
- return(true);
-}
-
-/**
- Free a string value.
-**/
-
-void string_free(char **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
-**/
-
-bool string_set(char **dest,const char *src)
-{
- string_free(dest);
- return(string_init(dest,src));
-}
-
-void fstring_sub(char *s,const char *pattern,const char *insert)
-{
- string_sub(s, pattern, insert, sizeof(fstring));
-}
-
-/**
- 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 const may be passed
- as string.
-**/
-
-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;
- ssize_t ls,lp,li,ld, i;
-
- if (!insert || !pattern || !*pattern || !string || !*string)
- return NULL;
-
- s = string;
-
- in = SMB_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<li;i++) {
- switch (in[i]) {
- case '$':
- /* allow a trailing $
- * (as in machine accounts) */
- if (allow_trailing_dollar && (i == li - 1 )) {
- break;
- }
- case '`':
- case '"':
- case '\'':
- case ';':
- case '%':
- case '\r':
- case '\n':
- if ( remove_unsafe_characters ) {
- in[i] = '_';
- break;
- }
- default:
- /* ok */
- 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"));
- SAFE_FREE(in);
- return NULL;
- }
- p = string + 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;
-}
-
-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.
* talloc version of string_sub2.
s = string;
- in = SMB_STRDUP(insert);
+ in = talloc_strdup(mem_ctx, insert);
if (!in) {
DEBUG(0, ("talloc_string_sub2: ENOMEM\n"));
return NULL;
if (allow_trailing_dollar && (i == li - 1 )) {
break;
}
+
+ FALL_THROUGH;
case '`':
case '"':
case '\'':
in[i] = '_';
break;
}
+
+ FALL_THROUGH;
default:
/* ok */
break;
if (!string) {
DEBUG(0, ("talloc_string_sub: out of "
"memory!\n"));
- SAFE_FREE(in);
+ TALLOC_FREE(in);
return NULL;
}
p = string + offset + (p - s);
break;
}
}
- SAFE_FREE(in);
+ TALLOC_FREE(in);
return string;
}
/* Too hard to try and get right. */
return NULL;
}
- ret = (char *)(s+strlen(s2));
+ ret = discard_const_p(char, (s+strlen(s2)));
TALLOC_FREE(ws);
TALLOC_FREE(s2);
return ret;
if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE, src, srclen,
(void **)(void *)&buffer, &size))
{
- smb_panic("failed to create UCS2 buffer");
+ return false;
}
if (!strlower_w(buffer) && (dest == src)) {
TALLOC_FREE(buffer);
- return srclen;
+ return true;
}
ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
TALLOC_FREE(buffer);
return ret;
}
+#if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
+
+/**
+ Convert a string to lower case.
+**/
+_PUBLIC_ void strlower_m(char *s)
+{
+ char *d;
+ struct smb_iconv_handle *iconv_handle;
+
+ iconv_handle = get_iconv_handle();
+
+ d = s;
+
+ while (*s) {
+ size_t c_size, c_size2;
+ codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
+ c_size2 = push_codepoint_handle(iconv_handle, d, tolower_m(c));
+ if (c_size2 > c_size) {
+ DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
+ c, tolower_m(c), (int)c_size, (int)c_size2));
+ smb_panic("codepoint expansion in strlower_m\n");
+ }
+ s += c_size;
+ d += c_size2;
+ }
+ *d = 0;
+}
+
+#endif
/**
Convert a string to lower case.
**/
-void strlower_m(char *s)
+bool strlower_m(char *s)
{
size_t len;
int errno_save;
+ bool ret = false;
/* this is quite a common operation, so we want it to be
fast. We optimise for the ascii case, knowing that all our
(ie. they match for the first 128 chars) */
while (*s && !(((unsigned char)s[0]) & 0x80)) {
- *s = tolower_ascii((unsigned char)*s);
+ *s = tolower_m((unsigned char)*s);
s++;
}
if (!*s)
- return;
+ return true;
/* 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);
+ ret = unix_strlower(s,len,s,len);
/* Catch mb conversion errors that may not terminate. */
- if (errno)
+ if (errno) {
s[len-1] = '\0';
+ }
errno = errno_save;
+ return ret;
}
static bool unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
bool ret;
if (!push_ucs2_talloc(talloc_tos(), &buffer, src, &size)) {
- return (size_t)-1;
+ return false;
}
if (!strupper_w(buffer) && (dest == src)) {
TALLOC_FREE(buffer);
- return srclen;
+ return true;
}
ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
return ret;
}
+#if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
+
+/**
+ Convert a string to UPPER case.
+**/
+_PUBLIC_ void strupper_m(char *s)
+{
+ char *d;
+ struct smb_iconv_handle *iconv_handle;
+
+ iconv_handle = get_iconv_handle();
+
+ d = s;
+
+ while (*s) {
+ size_t c_size, c_size2;
+ codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
+ c_size2 = push_codepoint_handle(iconv_handle, d, toupper_m(c));
+ if (c_size2 > c_size) {
+ DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
+ c, toupper_m(c), (int)c_size, (int)c_size2));
+ smb_panic("codepoint expansion in strupper_m\n");
+ }
+ s += c_size;
+ d += c_size2;
+ }
+ *d = 0;
+}
+
+#endif
+
/**
Convert a string to upper case.
**/
-void strupper_m(char *s)
+bool strupper_m(char *s)
{
size_t len;
- int errno_save;
+ bool ret = false;
/* this is quite a common operation, so we want it to be
fast. We optimise for the ascii case, knowing that all our
(ie. they match for the first 128 chars) */
while (*s && !(((unsigned char)s[0]) & 0x80)) {
- *s = toupper_ascii_fast((unsigned char)*s);
+ *s = toupper_ascii_fast_table[(unsigned char)s[0]];
s++;
}
if (!*s)
- return;
+ return true;
- /* I assume that lowercased string takes the same number of bytes
+ /* I assume that uppercased 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);
+ ret = unix_strupper(s,len,s,len);
/* Catch mb conversion errors that may not terminate. */
- if (errno)
+ if (!ret) {
s[len-1] = '\0';
- errno = errno_save;
+ }
+ return ret;
}
/**
#define S_LIST_ABS 16 /* List Allocation Block Size */
-/******************************************************************************
- 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 = 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;
- }
-
- TALLOC_FREE(*list);
- *list = tmpstr;
-
- list++;
- }
-
- return true;
-}
-
/******************************************************************************
substitute a specific pattern in a string list
*****************************************************************************/
t = *list;
d = p -t;
if (ld) {
- t = TALLOC_ARRAY(ctx, char, ls +ld +1);
+ t = talloc_array(ctx, char, ls +ld +1);
if (!t) {
DEBUG(0,("str_list_substitute: "
"Unable to allocate memory"));
while (*p && isspace(*p))
p++;
- sscanf(p,"%"PRIu64,&val);
+ sscanf(p,"%"SCNu64,&val);
if (entptr) {
while (*p && isdigit(*p))
p++;
*
* Returns 0 if the string can't be converted.
*/
-SMB_OFF_T conv_str_size(const char * str)
+uint64_t conv_str_size(const char * str)
{
- SMB_OFF_T lval_orig;
- SMB_OFF_T lval;
+ uint64_t lval;
char * end;
+ int error = 0;
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
+ lval = strtoull_err(str, &end, 10, &error);
- if (end == NULL || end == str) {
+ if (end == NULL || end == str || error != 0) {
return 0;
}
return lval;
}
- lval_orig = lval;
-
if (strwicmp(end, "K") == 0) {
- lval *= (SMB_OFF_T)1024;
+ lval *= 1024ULL;
} else if (strwicmp(end, "M") == 0) {
- lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024);
+ lval *= (1024ULL * 1024ULL);
} else if (strwicmp(end, "G") == 0) {
- lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
- (SMB_OFF_T)1024);
+ lval *= (1024ULL * 1024ULL *
+ 1024ULL);
} else if (strwicmp(end, "T") == 0) {
- lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
- (SMB_OFF_T)1024 * (SMB_OFF_T)1024);
+ lval *= (1024ULL * 1024ULL *
+ 1024ULL * 1024ULL);
} 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);
+ lval *= (1024ULL * 1024ULL *
+ 1024ULL * 1024ULL *
+ 1024ULL);
} 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;
}
-/* 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;
-
- *string = TALLOC_ARRAY(mem_ctx, 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) {
- *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char,
- *bufsize);
- if (*string == NULL) {
- goto error;
- }
- }
-
- StrnCpy((*string)+(*len), newstr, ret);
- (*len) += ret;
- free(newstr);
- return;
-
- error:
- *len = -1;
- *string = NULL;
-}
-
/*
* asprintf into a string and strupper_m it after that.
*/
if (ret == -1)
return -1;
- strupper_m(result);
+ if (!strupper_m(result)) {
+ SAFE_FREE(result);
+ return -1;
+ }
+
*strp = result;
return ret;
}
if (ret == NULL) {
return NULL;
}
- strupper_m(ret);
+ if (!strupper_m(ret)) {
+ TALLOC_FREE(ret);
+ return NULL;
+ }
return ret;
}
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".
- 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"));
+ if (!strlower_m(ret)) {
+ TALLOC_FREE(ret);
return NULL;
}
- memcpy(temp3, temp1+1, len-1);
- temp3[len-1] = '\0';
- return temp3;
+ return ret;
}
+
/********************************************************************
Check a string for any occurrences of a specified list of invalid
characters.
return ret;
}
-/***************************************************
- str_list_make, v3 version. The v4 version does not
- look at quoted strings with embedded blanks, so
- do NOT merge this function please!
-***************************************************/
-
-#define S_LIST_ABS 16 /* List Allocation Block Size */
+/*
+ * This routine improves performance for operations temporarily acting on a
+ * full path. It is equivalent to the much more expensive
+ *
+ * talloc_asprintf(talloc_tos(), "%s/%s", dir, name)
+ *
+ * This actually does make a difference in metadata-heavy workloads (i.e. the
+ * "standard" client.txt nbench run.
+ */
-char **str_list_make_v3(TALLOC_CTX *mem_ctx, const char *string,
- const char *sep)
+ssize_t full_path_tos(const char *dir, const char *name,
+ char *tmpbuf, size_t tmpbuf_len,
+ char **pdst, char **to_free)
{
- char **list;
- const char *str;
- char *s, *tok;
- int num, lsize;
+ size_t dirlen, namelen, len;
+ char *dst;
- if (!string || !*string)
- return NULL;
+ dirlen = strlen(dir);
+ namelen = strlen(name);
+ len = dirlen + namelen + 1;
- list = TALLOC_ARRAY(mem_ctx, char *, S_LIST_ABS+1);
- if (list == NULL) {
- return NULL;
- }
- 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 = 0;
- str = s;
-
- while (next_token_talloc(list, &str, &tok, sep)) {
-
- if (num == lsize) {
- char **tmp;
-
- lsize += S_LIST_ABS;
-
- tmp = TALLOC_REALLOC_ARRAY(mem_ctx, list, char *,
- lsize + 1);
- if (tmp == NULL) {
- DEBUG(0,("str_list_make: "
- "Unable to allocate memory"));
- TALLOC_FREE(list);
- return NULL;
- }
-
- list = tmp;
-
- memset (&list[num], 0,
- ((sizeof(char**)) * (S_LIST_ABS +1)));
+ if (len < tmpbuf_len) {
+ dst = tmpbuf;
+ *to_free = NULL;
+ } else {
+ dst = talloc_array(talloc_tos(), char, len+1);
+ if (dst == NULL) {
+ return -1;
}
-
- list[num] = tok;
- num += 1;
+ *to_free = dst;
}
- list[num] = NULL;
-
- TALLOC_FREE(s);
- return list;
-}
-
-char *sanitize_username(TALLOC_CTX *mem_ctx, const char *username)
-{
- fstring tmp;
-
- alpha_strcpy(tmp, username, ". _-$", sizeof(tmp));
- return talloc_strdup(mem_ctx, tmp);
+ memcpy(dst, dir, dirlen);
+ dst[dirlen] = '/';
+ memcpy(dst+dirlen+1, name, namelen+1);
+ *pdst = dst;
+ return len;
}