util: strhex_to_str() fixed to handle '0x' correctly
[ira/wip.git] / lib / util / util.c
index 4e2a5aab0981cd990a95e4a7cc21a17163f928c6..fd0e6b8d799c717b0b704bdc95ac0f3b6dd4e84c 100644 (file)
@@ -133,8 +133,13 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid,
                        umask(old_umask);
                        return false;
                }
-               if ((st.st_uid != uid) || 
-                   ((st.st_mode & 0777) != dir_perms)) {
+               if (st.st_uid != uid && !uwrap_enabled()) {
+                       DEBUG(0, ("invalid ownership on directory "
+                                 "%s\n", dname));
+                       umask(old_umask);
+                       return false;
+               }
+               if ((st.st_mode & 0777) != dir_perms) {
                        DEBUG(0, ("invalid permissions on directory "
                                  "%s\n", dname));
                        umask(old_umask);
@@ -145,37 +150,6 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid,
 }       
 
 
-/**
- Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
- else
-  if SYSV use O_NDELAY
-  if BSD use FNDELAY
-**/
-
-_PUBLIC_ int set_blocking(int fd, bool set)
-{
-       int val;
-#ifdef O_NONBLOCK
-#define FLAG_TO_SET O_NONBLOCK
-#else
-#ifdef SYSV
-#define FLAG_TO_SET O_NDELAY
-#else /* BSD */
-#define FLAG_TO_SET FNDELAY
-#endif
-#endif
-
-       if((val = fcntl(fd, F_GETFL, 0)) == -1)
-               return -1;
-       if(set) /* Turn blocking on - ie. clear nonblock flag */
-               val &= ~FLAG_TO_SET;
-       else
-               val |= FLAG_TO_SET;
-       return fcntl( fd, F_SETFL, val);
-#undef FLAG_TO_SET
-}
-
-
 /**
  Sleep for a specified number of milliseconds.
 **/
@@ -192,33 +166,30 @@ _PUBLIC_ void msleep(unsigned int t)
 }
 
 /**
- Get my own name, return in malloc'ed storage.
+ Get my own name, return in talloc'ed storage.
 **/
 
-_PUBLIC_ char *get_myname(void)
+_PUBLIC_ char *get_myname(TALLOC_CTX *ctx)
 {
-       char *hostname;
        char *p;
-
-       hostname = (char *)malloc(MAXHOSTNAMELEN+1);
-       *hostname = 0;
+       char hostname[HOST_NAME_MAX];
 
        /* get my host name */
-       if (gethostname(hostname, MAXHOSTNAMELEN+1) == -1) {
+       if (gethostname(hostname, sizeof(hostname)) == -1) {
                DEBUG(0,("gethostname failed\n"));
                return NULL;
-       } 
+       }
 
        /* Ensure null termination. */
-       hostname[MAXHOSTNAMELEN] = '\0';
+       hostname[sizeof(hostname)-1] = '\0';
 
        /* split off any parts after an initial . */
-       p = strchr(hostname, '.');
-
-       if (p != NULL)
+       p = strchr_m(hostname, '.');
+       if (p) {
                *p = 0;
-       
-       return hostname;
+       }
+
+       return talloc_strdup(ctx, hostname);
 }
 
 /**
@@ -299,15 +270,13 @@ static void _dump_data(int level, const uint8_t *buf, int len,
                       bool omit_zero_bytes)
 {
        int i=0;
-       const uint8_t empty[16];
+       static const uint8_t empty[16] = { 0, };
        bool skipped = false;
 
        if (len<=0) return;
 
        if (!DEBUGLVL(level)) return;
 
-       memset(&empty, '\0', 16);
-
        for (i=0;i<len;) {
 
                if (i%16 == 0) {
@@ -544,21 +513,6 @@ void *malloc_array(size_t el_size, unsigned int count)
        return realloc_array(NULL, el_size, count, false);
 }
 
-_PUBLIC_ void *talloc_check_name_abort(const void *ptr, const char *name)
-{
-        void *result;
-
-        result = talloc_check_name(ptr, name);
-        if (result != NULL)
-                return result;
-
-        DEBUG(0, ("Talloc type mismatch, expected %s, got %s\n",
-                  name, talloc_get_name(ptr)));
-        smb_panic("talloc type mismatch");
-        /* Keep the compiler happy */
-        return NULL;
-}
-
 /**
  Trim the specified elements off the front and back of a string.
 **/
@@ -625,18 +579,18 @@ _PUBLIC_ _PURE_ size_t count_chars(const char *s, char c)
 **/
 _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t strhex_len)
 {
-       size_t i;
+       size_t i = 0;
        size_t num_chars = 0;
        uint8_t   lonybble, hinybble;
        const char     *hexchars = "0123456789ABCDEF";
        char           *p1 = NULL, *p2 = NULL;
 
-       for (i = 0; i < strhex_len && strhex[i] != 0; i++) {
-               if (strncasecmp(hexchars, "0x", 2) == 0) {
-                       i++; /* skip two chars */
-                       continue;
-               }
+       /* skip leading 0x prefix */
+       if (strncasecmp(strhex, "0x", 2) == 0) {
+               i += 2; /* skip two chars */
+       }
 
+       for (; i < strhex_len && strhex[i] != 0; i++) {
                if (!(p1 = strchr(hexchars, toupper((unsigned char)strhex[i]))))
                        break;
 
@@ -701,10 +655,14 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_
        char *hex_buffer;
 
        hex_buffer = talloc_array(mem_ctx, char, (len*2)+1);
+       if (!hex_buffer) {
+               return NULL;
+       }
 
        for (i = 0; i < len; i++)
                slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
 
+       talloc_set_name_const(hex_buffer, hex_buffer);
        return hex_buffer;
 }
 
@@ -835,4 +793,104 @@ _PUBLIC_ size_t utf16_len_n(const void *src, size_t n)
        return len;
 }
 
+/**
+ * @file
+ * @brief String utilities.
+ **/
+
+static bool next_token_internal_talloc(TALLOC_CTX *ctx,
+                               const char **ptr,
+                                char **pp_buff,
+                                const char *sep,
+                                bool ltrim)
+{
+       const char *s;
+       const char *saved_s;
+       char *pbuf;
+       bool quoted;
+       size_t len=1;
+
+       *pp_buff = NULL;
+       if (!ptr) {
+               return(false);
+       }
+
+       s = *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;
+       }
+
+       /* 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++) {
+               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;
+       s = saved_s;
+       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;
+}
+
+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);
+}
+
+/*
+ * 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_talloc(TALLOC_CTX *ctx,
+                       const char **ptr,
+                       char **pp_buff,
+                       const char *sep)
+{
+       return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
+}
+