Add two more memory-debug smbcontrol messages: these ones should
[ira/wip.git] / source3 / lib / util_str.c
index 8518d75a49b55869f45b737b4ae7ef186f1186e3..14d50384c28070efda32bee5e4f2b48a4d5a5db5 100644 (file)
@@ -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. 
@@ -134,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);
 }
 
@@ -144,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);
 }
 
 /*******************************************************************
@@ -186,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. */
@@ -200,9 +198,9 @@ int strwicmp(char *psz1, 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')
@@ -245,11 +243,8 @@ 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);
-       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);
 }
 
@@ -745,6 +740,70 @@ 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  lr, lp, li, lt;
+
+       if (!insert || !pattern || !*pattern || !s) return NULL;
+
+       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))) {
+                       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) / 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);
+}
+
 /****************************************************************************
  splits out the front and back at a separator.
 ****************************************************************************/
@@ -815,7 +874,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);
@@ -829,7 +888,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);
@@ -856,3 +915,25 @@ void strupper_m(char *s)
        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<len;i++) {
+               s[j] = '\\';
+               s[j+1] = hex[((unsigned char)buf[i]) >> 4];
+               s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
+               j += 3;
+       }
+       s[j] = 0;
+       return s;
+}
+