Same infinite loop bug as I fixed in SAMBA_2_0, but I just spent half
[samba.git] / source3 / lib / util_str.c
index c943a854cfa1900d0e73af543f864b6a4251d1be..142f0af4c8717b4bfa87ebf8773f0e6f73e6762a 100644 (file)
@@ -458,11 +458,11 @@ void string_replace(char *s,char oldc,char newc)
 /*******************************************************************
 skip past some strings in a buffer
 ********************************************************************/
-char *skip_string(const char *buf,size_t n)
+char *skip_string(char *buf,size_t n)
 {
   while (n--)
     buf += strlen(buf) + 1;
-  return((char *)buf);
+  return(buf);
 }
 
 /*******************************************************************
@@ -882,16 +882,16 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex)
                        continue;
                }
 
-               while (!(p1 = strchr(hexchars, toupper(strhex[i]))))
+               if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
                {
-                       continue;
+                       break;
                }
 
                i++; /* next hex digit */
 
-               while (!(p2 = strchr(hexchars, toupper(strhex[i]))))
+               if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
                {
-                       continue;
+                       break;
                }
 
                /* get the two nybbles */
@@ -990,6 +990,7 @@ BOOL string_set(char **dest,const char *src)
   return(string_init(dest,src));
 }
 
+
 /****************************************************************************
 substitute a string for a pattern in another string. Make sure there is 
 enough room!
@@ -997,31 +998,64 @@ enough room!
 This routine looks for pattern in s and replaces it with 
 insert. It may do multiple replacements.
 
-return True if a substitution was done.
+any of " ; ' or ` in the insert string are replaced with _
 ****************************************************************************/
-BOOL string_sub(char *s,const char *pattern,const char *insert)
+void string_sub(char *s,const char *pattern,const char *insert)
 {
-  BOOL ret = False;
-  char *p;
-  size_t ls,lp,li;
+       char *p;
+       size_t ls,lp,li, i;
+
+       if (!insert || !pattern || !s) return;
+
+       ls = strlen(s);
+       lp = strlen(pattern);
+       li = strlen(insert);
+
+       if (!*pattern) return;
+       
+       while (lp <= ls && (p = strstr(s,pattern))) {
+               memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
+               for (i=0;i<li;i++) {
+                       switch (insert[i]) {
+                       case '`':
+                       case '"':
+                       case '\'':
+                       case ';':
+                               p[i] = '_';
+                               break;
+                       default:
+                               p[i] = insert[i];
+                       }
+               }
+               s = p + li;
+               ls += (li-lp);
+       }
+}
 
-  if (!insert || !pattern || !s) return(False);
 
-  ls = strlen(s);
-  lp = strlen(pattern);
-  li = strlen(insert);
+/****************************************************************************
+similar to string_sub() but allows for any character to be substituted. 
+Use with caution!
+****************************************************************************/
+void all_string_sub(char *s,const char *pattern,const char *insert)
+{
+       char *p;
+       size_t ls,lp,li;
 
-  if (!*pattern) return(False);
+       if (!insert || !pattern || !s) return;
 
-  while (lp <= ls && (p = strstr(s,pattern)))
-    {
-      ret = True;
-      memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
-      memcpy(p,insert,li);
-      s = p + li;
-      ls = strlen(s);
-    }
-  return(ret);
+       ls = strlen(s);
+       lp = strlen(pattern);
+       li = strlen(insert);
+
+       if (!*pattern) return;
+       
+       while (lp <= ls && (p = strstr(s,pattern))) {
+               memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
+               memcpy(p, insert, li);
+               s = p + li;
+               ls += (li-lp);
+       }
 }
 
 /****************************************************************************
@@ -1055,3 +1089,73 @@ void split_at_last_component(char *path, char *front, char sep, char *back)
                }
        }
 }
+
+/****************************************************************************
+convert a bit field to a string.  if you want multiple bits to be detected
+set them first, e.g SV_TYPE_ALL to be "All" or "Full Control" for ACB_INFOs.
+
+strings are expected to contain their own separators, although the code
+below only assumes that separators are spaces.
+
+****************************************************************************/
+char *bit_field_to_str(uint32 type, struct field_info *bs)
+{
+       static fstring typestr;
+       int i = 0;
+
+       typestr[0] = 0;
+
+       if (type == 0 || bs == NULL)
+       {
+               return NULL;
+       }
+
+       while (bs[i].str != NULL && type != 0)
+       {
+               if (IS_BITS_SET_ALL(bs[i].bits, type))
+               {
+                       fstrcat(typestr, bs[i].str);
+                       type &= ~bs[i].bits;
+               }
+               i++;
+       }
+       
+       i = strlen(typestr)-1;
+       if (i > 0 && typestr[i] == ' ')
+       {
+               typestr[i] = 0;
+       }
+
+       return typestr;
+}
+
+/****************************************************************************
+convert an enumeration to a string.  first item is the default.
+****************************************************************************/
+char *enum_field_to_str(uint32 type, struct field_info *bs, BOOL first_default)
+{
+       int i = 0;
+
+       if (bs == NULL)
+       {
+               return NULL;
+       }
+
+       while (bs[i].str != NULL && type != 0)
+       {
+               if (bs[i].bits == type)
+               {
+                       return bs[i].str;
+               }
+               i++;
+       }
+
+       /* oops - none found */
+
+       if (first_default)
+       {
+               return bs[0].str;
+       }
+
+       return NULL;
+}