r12216: Couple of small fixes: reduce include/includes.h a bit, simplify headers
[bbaumbach/samba-autobuild/.git] / source4 / lib / util_str.c
index 95c0b84d98050cd17cc8137e27b77e9f24805917..56a1345e38279d7c2028b1ff7920fa815d2ae29c 100644 (file)
@@ -22,6 +22,9 @@
 */
 
 #include "includes.h"
+#include "system/iconv.h"
+#include "pstring.h"
+#include "lib/ldb/include/ldb.h"
 
 /**
  * @file
@@ -77,7 +80,7 @@ BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
 /**
  Case insensitive string compararison
 **/
-int StrCaseCmp(const char *s1, const char *s2)
+int strcasecmp_m(const char *s1, const char *s2)
 {
        codepoint_t c1=0, c2=0;
        size_t size1, size2;
@@ -119,7 +122,7 @@ BOOL strequal(const char *s1, const char *s2)
        if (!s1 || !s2)
                return(False);
   
-       return StrCaseCmp(s1,s2) == 0;
+       return strcasecmp_m(s1,s2) == 0;
 }
 
 /**
@@ -156,7 +159,8 @@ int strwicmp(const char *psz1, const char *psz2)
                        psz1++;
                while (isspace((int)*psz2))
                        psz2++;
-               if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
+               if (toupper((unsigned char)*psz1) != toupper((unsigned char)*psz2) 
+                   || *psz1 == '\0'
                    || *psz2 == '\0')
                        break;
                psz1++;
@@ -270,7 +274,7 @@ char *safe_strcpy(char *dest,const char *src, size_t maxlength)
 
        if (len > maxlength) {
                DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
-                        (uint_t)(len-maxlength), len, maxlength, src));
+                        (uint_t)(len-maxlength), (unsigned)len, (unsigned)maxlength, src));
                len = maxlength;
        }
       
@@ -393,6 +397,7 @@ char *StrnCpy(char *dest,const char *src,size_t n)
 
  valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
 
+
 **/
 size_t strhex_to_str(char *p, size_t len, const char *strhex)
 {
@@ -408,12 +413,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((unsigned char)strhex[i]))))
                        break;
 
                i++; /* next hex digit */
 
-               if (!(p2 = strchr_m(hexchars, toupper(strhex[i]))))
+               if (!(p2 = strchr_m(hexchars, toupper((unsigned char)strhex[i]))))
                        break;
 
                /* get the two nybbles */
@@ -472,7 +477,7 @@ BOOL in_list(const char *s, const char *list, BOOL casesensitive)
                        if (strcmp(tok,s) == 0)
                                return(True);
                } else {
-                       if (StrCaseCmp(tok,s) == 0)
+                       if (strcasecmp_m(tok,s) == 0)
                                return(True);
                }
        }
@@ -658,6 +663,52 @@ char *strrchr_m(const char *s, char c)
        return ret;
 }
 
+/*
+  return True if any (multi-byte) character is lower case
+*/
+BOOL strhaslower(const char *string)
+{
+       while (*string) {
+               size_t c_size;
+               codepoint_t s;
+               codepoint_t t;
+
+               s = next_codepoint(string, &c_size);
+               string += c_size;
+
+               t = toupper_w(s);
+
+               if (s != t) {
+                       return True; /* that means it has lower case chars */
+               }
+       }
+
+       return False;
+} 
+
+/*
+  return True if any (multi-byte) character is upper case
+*/
+BOOL strhasupper(const char *string)
+{
+       while (*string) {
+               size_t c_size;
+               codepoint_t s;
+               codepoint_t t;
+
+               s = next_codepoint(string, &c_size);
+               string += c_size;
+
+               t = tolower_w(s);
+
+               if (s != t) {
+                       return True; /* that means it has upper case chars */
+               }
+       }
+
+       return False;
+} 
+
 /**
  Convert a string to lower case, allocated with talloc
 **/
@@ -668,7 +719,7 @@ char *strlower_talloc(TALLOC_CTX *ctx, const char *src)
 
        /* this takes advantage of the fact that upper/lower can't
           change the length of a character by more than 1 byte */
-       dest = talloc(ctx, 2*(strlen(src))+1);
+       dest = talloc_size(ctx, 2*(strlen(src))+1);
        if (dest == NULL) {
                return NULL;
        }
@@ -700,10 +751,14 @@ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
 {
        size_t size=0;
        char *dest;
+       
+       if (!src) {
+               return NULL;
+       }
 
        /* this takes advantage of the fact that upper/lower can't
           change the length of a character by more than 1 byte */
-       dest = talloc(ctx, 2*(strlen(src))+1);
+       dest = talloc_size(ctx, 2*(strlen(src))+1);
        if (dest == NULL) {
                return NULL;
        }
@@ -755,7 +810,7 @@ void strlower_m(char *s)
                c_size2 = push_codepoint(d, tolower_w(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_w(c), c_size, c_size2));
+                                c, tolower_w(c), (int)c_size, (int)c_size2));
                        smb_panic("codepoint expansion in strlower_m\n");
                }
                s += c_size;
@@ -791,7 +846,7 @@ void strupper_m(char *s)
                c_size2 = push_codepoint(d, toupper_w(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_w(c), c_size, c_size2));
+                                c, toupper_w(c), (int)c_size, (int)c_size2));
                        smb_panic("codepoint expansion in strupper_m\n");
                }
                s += c_size;
@@ -849,29 +904,6 @@ size_t strlen_m_term(const char *s)
        return strlen_m(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<len;i++) {
-               s[j] = '\\';
-               s[j+1] = hex[((uint8_t)buf[i]) >> 4];
-               s[j+2] = hex[((uint8_t)buf[i]) & 0xF];
-               j += 3;
-       }
-       s[j] = 0;
-       return s;
-}
-
 /**
  Unescape a URL encoded string, in place.
 **/
@@ -915,37 +947,11 @@ void rfc1738_unescape(char *buf)
 /**
  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
  **/
-DATA_BLOB base64_decode_data_blob(const char *s)
+DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
 {
-       const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-       int bit_offset, byte_offset, idx, i, n;
-       DATA_BLOB decoded = data_blob(s, strlen(s)+1);
-       uint8_t *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;
+       DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
+       ret.length = ldb_base64_decode(ret.data);
+       return ret;
 }
 
 /**
@@ -953,58 +959,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);
-       data_blob_free(&decoded);
-
-       /* null terminate */
-       s[decoded.length] = '\0';
+       ldb_base64_decode(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
+ * Encode a base64 string into a talloc()ed string caller to free.
  **/
-char * base64_encode_data_blob(DATA_BLOB data)
+char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
 {
-       const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-       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 = (uint8_t) *(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;
+       return ldb_base64_encode(mem_ctx, data.data, data.length);
 }
 
 #ifdef VALGRIND
@@ -1029,7 +992,7 @@ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
        if (!s || !*s) {
                return talloc_strdup(mem_ctx, "");
        }
-       ret = talloc(mem_ctx, strlen(s)+2);
+       ret = talloc_size(mem_ctx, strlen(s)+2);
        if (!ret) {
                return ret;
        }
@@ -1056,7 +1019,7 @@ BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
 {
        char *dup_str = talloc_strdup(mem_ctx, str);
 
-       *strings = talloc_realloc_p(mem_ctx,
+       *strings = talloc_realloc(mem_ctx,
                                    *strings,
                                    const char *, ((*num)+1));
 
@@ -1119,6 +1082,7 @@ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
                {'A', FILE_ATTRIBUTE_ARCHIVE},
                {'H', FILE_ATTRIBUTE_HIDDEN},
                {'S', FILE_ATTRIBUTE_SYSTEM},
+               {'N', FILE_ATTRIBUTE_NORMAL},
                {'R', FILE_ATTRIBUTE_READONLY},
                {'d', FILE_ATTRIBUTE_DEVICE},
                {'t', FILE_ATTRIBUTE_TEMPORARY},
@@ -1131,7 +1095,7 @@ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
        };
        char *ret;
 
-       ret = talloc(mem_ctx, ARRAY_SIZE(attr_strs)+1);
+       ret = talloc_size(mem_ctx, ARRAY_SIZE(attr_strs)+1);
        if (!ret) {
                return NULL;
        }
@@ -1146,4 +1110,3 @@ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
 
        return ret;
 }
-