messaging: Factor out messaging_dispatch_waiters
[samba.git] / source3 / lib / util_str.c
index f6d9f3425f74cc9dfb99d989c4b17ba37d11eed7..48e434f777ee548593759ea5c8917fa44de24b3a 100644 (file)
@@ -25,7 +25,7 @@
 #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,
@@ -55,12 +55,12 @@ bool strnequal(const char *s1,const char *s2,size_t n)
  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);
 }
 
 /**
@@ -414,8 +414,6 @@ static bool unix_strlower(const char *src, size_t srclen, char *dest, size_t des
        if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE, src, srclen,
                                   (void **)(void *)&buffer, &size))
        {
-               smb_panic("failed to create UCS2 buffer");
-               /* NOTREACHED. Yet. */
                return false;
        }
        if (!strlower_w(buffer) && (dest == src)) {
@@ -462,10 +460,11 @@ _PUBLIC_ void strlower_m(char *s)
  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
@@ -478,18 +477,20 @@ void strlower_m(char *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)
@@ -547,10 +548,10 @@ _PUBLIC_ void strupper_m(char *s)
  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
@@ -558,23 +559,22 @@ void strupper_m(char *s)
           (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;
 }
 
 /**
@@ -848,7 +848,7 @@ uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
        while (*p && isspace(*p))
                p++;
 
-       sscanf(p,"%"PRIu64,&val);
+       sscanf(p,"%"SCNu64,&val);
        if (entptr) {
                while (*p && isdigit(*p))
                        p++;
@@ -909,65 +909,6 @@ uint64_t conv_str_size(const char * str)
        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(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.
  */
@@ -985,7 +926,11 @@ int asprintf_strupper_m(char **strp, const char *fmt, ...)
        if (ret == -1)
                return -1;
 
-       strupper_m(result);
+       if (!strupper_m(result)) {
+               SAFE_FREE(result);
+               return -1;
+       }
+
        *strp = result;
        return ret;
 }
@@ -1002,7 +947,10 @@ char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...)
        if (ret == NULL) {
                return NULL;
        }
-       strupper_m(ret);
+       if (!strupper_m(ret)) {
+               TALLOC_FREE(ret);
+               return NULL;
+       }
        return ret;
 }
 
@@ -1018,7 +966,10 @@ char *talloc_asprintf_strlower_m(TALLOC_CTX *t, const char *fmt, ...)
        if (ret == NULL) {
                return NULL;
        }
-       strlower_m(ret);
+       if (!strlower_m(ret)) {
+               TALLOC_FREE(ret);
+               return NULL;
+       }
        return ret;
 }
 
@@ -1213,70 +1164,41 @@ char *escape_shell_string(const char *src)
        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(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;
+       memcpy(dst, dir, dirlen);
+       dst[dirlen] = '/';
+       memcpy(dst+dirlen+1, name, namelen+1);
+       *pdst = dst;
+       return len;
 }