lib/util Re-merge the string_sub() and all_string_sub() from source3
authorAndrew Bartlett <abartlet@samba.org>
Fri, 29 Apr 2011 03:20:51 +0000 (13:20 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 29 Apr 2011 06:38:15 +0000 (16:38 +1000)
Andrew Bartlett

lib/util/substitute.c
lib/util/util.h
lib/util/wscript_build
source3/Makefile.in
source3/include/proto.h
source3/lib/util_str.c

index 32945a721340529a5d7c41d8f70bf9f01fc12b71..500d12777f9a5c908de5d6727839a0999d7f11cf 100644 (file)
  **/
 
 /**
- Substitute a string for a pattern in another string. Make sure there is 
+ Substitute a string for a pattern in another string. Make sure there is
  enough room!
 
- This routine looks for pattern in s and replaces it with 
- insert. It may do multiple replacements.
+ This routine looks for pattern in s and replaces it with
+ insert. It may do multiple replacements or just one.
 
  Any of " ; ' $ or ` in the insert string are replaced with _
  if len==0 then the string cannot be extended. This is different from the old
  use of len==0 which was for no length checks to be done.
 **/
 
-_PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_t len)
+static void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
+                       bool remove_unsafe_characters, bool replace_once,
+                       bool allow_trailing_dollar)
 {
        char *p;
        ssize_t ls, lp, li, i;
@@ -55,9 +57,10 @@ _PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_
        if (len == 0)
                len = ls + 1; /* len is number of *bytes* */
 
-       while (lp <= ls && (p = strstr(s, pattern))) {
+       while (lp <= ls && (p = strstr_m(s,pattern))) {
                if (ls + (li-lp) >= len) {
-                       DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", 
+                       DEBUG(0,("ERROR: string overflow by "
+                               "%d in string_sub(%.50s, %d)\n",
                                 (int)(ls + (li-lp) - len),
                                 pattern, (int)len));
                        break;
@@ -67,25 +70,50 @@ _PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_
                }
                for (i=0;i<li;i++) {
                        switch (insert[i]) {
+                       case '$':
+                               /* allow a trailing $
+                                * (as in machine accounts) */
+                               if (allow_trailing_dollar && (i == li - 1 )) {
+                                       p[i] = insert[i];
+                                       break;
+                               }
                        case '`':
                        case '"':
                        case '\'':
                        case ';':
-                       case '$':
                        case '%':
                        case '\r':
                        case '\n':
-                               p[i] = '_';
-                               break;
+                               if ( remove_unsafe_characters ) {
+                                       p[i] = '_';
+                                       /* yes this break should be here
+                                        * since we want to fall throw if
+                                        * not replacing unsafe chars */
+                                       break;
+                               }
                        default:
                                p[i] = insert[i];
                        }
                }
                s = p + li;
                ls += (li-lp);
+
+               if (replace_once)
+                       break;
        }
 }
 
+void string_sub_once(char *s, const char *pattern,
+               const char *insert, size_t len)
+{
+       string_sub2( s, pattern, insert, len, true, true, false );
+}
+
+void string_sub(char *s,const char *pattern, const char *insert, size_t len)
+{
+       string_sub2( s, pattern, insert, len, true, false, false );
+}
+
 /**
  * Talloc'ed version of string_sub
  */
@@ -146,13 +174,14 @@ _PUBLIC_ void all_string_sub(char *s,const char *pattern,const char *insert, siz
 
        if (!*pattern)
                return;
-       
+
        if (len == 0)
                len = ls + 1; /* len is number of *bytes* */
-       
-       while (lp <= ls && (p = strstr(s,pattern))) {
+
+       while (lp <= ls && (p = strstr_m(s,pattern))) {
                if (ls + (li-lp) >= len) {
-                       DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", 
+                       DEBUG(0,("ERROR: string overflow by "
+                               "%d in all_string_sub(%.50s, %d)\n",
                                 (int)(ls + (li-lp) - len),
                                 pattern, (int)len));
                        break;
index 45779912f3105f08c4bac7bbae818d5fc36cd934..8bbaa0e393ec1c2ce2110ca25eb9805f4fcef4f4 100644 (file)
@@ -284,6 +284,8 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_
 **/
 _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len);
 
+_PUBLIC_ void string_sub_once(char *s, const char *pattern,
+                             const char *insert, size_t len);
 
 _PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, 
                                const char *pattern, const char *insert);
index f1bb9e74935b2b8199e738786381941c660b0a79..561dcc4379acf4294bf798393996caa3a6b45f5a 100755 (executable)
@@ -5,11 +5,11 @@ common_util_sources = '''talloc_stack.c smb_threads.c xfile.c data_blob.c
                     genrand.c fsusage.c blocking.c become_daemon.c
                     signal.c system.c params.c util.c util_id.c util_net.c
                     util_strlist.c idtree.c debug.c fault.c base64.c
-                    util_str_common.c'''
+                    util_str_common.c substitute.c'''
 
 common_util_headers = 'debug.h'
 common_util_public_deps = 'talloc pthread LIBCRYPTO'
-s4_util_sources = '''dprintf.c ms_fnmatch.c parmlist.c substitute.c util_str.c'''
+s4_util_sources = '''dprintf.c ms_fnmatch.c parmlist.c util_str.c'''
 s4_util_deps = 'DYNCONFIG'
 s4_util_public_deps = 'talloc CHARSET execinfo uid_wrapper'
 s4_util_public_headers = 'attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h dlinklist.h util.h'
index 7c5da0971f572801a2e76e586ac220ce8ddc9148..9746a215088036bc3a5535628175a9060e325ea4 100644 (file)
@@ -461,7 +461,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \
          ../lib/util/charset/util_unistr_w.o ../lib/util/charset/codepoints.o ../lib/util/charset/util_str.o lib/util_file.o \
          lib/util.o lib/util_cmdline.o lib/util_names.o \
          lib/util_sock.o lib/sock_exec.o lib/util_sec.o \
-         lib/substitute.o lib/dbwrap_util.o \
+         lib/substitute.o ../lib/util/substitute.o lib/dbwrap_util.o \
          lib/ms_fnmatch.o lib/errmap_unix.o \
          lib/tallocmsg.o lib/dmallocmsg.o \
          libsmb/clisigning.o libsmb/smb_signing.o \
index 058356e0d617c36e71631ddbcd4014b6fc718d6c..c9552d75542975a257f28c1f4251158ed2374f60 100644 (file)
@@ -960,12 +960,6 @@ char *StrnCpy(char *dest,const char *src,size_t n);
 bool in_list(const char *s, const char *list, bool casesensitive);
 void string_free(char **s);
 bool string_set(char **dest,const char *src);
-void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
-                bool remove_unsafe_characters, bool replace_once,
-                bool allow_trailing_dollar);
-void string_sub_once(char *s, const char *pattern,
-               const char *insert, size_t len);
-void string_sub(char *s,const char *pattern, const char *insert, size_t len);
 void fstring_sub(char *s,const char *pattern,const char *insert);
 char *realloc_string_sub2(char *string,
                        const char *pattern,
index 554f6a689be7c8f2e39be2227e5ed923f24e4e8e..07a058925dd07426780d639f9e8cb1fd6e29b7f8 100644 (file)
@@ -508,92 +508,6 @@ 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!
-
- This routine looks for pattern in s and replaces it with
- insert. It may do multiple replacements or just one.
-
- Any of " ; ' $ or ` in the insert string are replaced with _
- if len==0 then the string cannot be extended. This is different from the old
- use of len==0 which was for no length checks to be done.
-**/
-
-void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
-                bool remove_unsafe_characters, bool replace_once,
-                bool allow_trailing_dollar)
-{
-       char *p;
-       ssize_t ls,lp,li, i;
-
-       if (!insert || !pattern || !*pattern || !s)
-               return;
-
-       ls = (ssize_t)strlen(s);
-       lp = (ssize_t)strlen(pattern);
-       li = (ssize_t)strlen(insert);
-
-       if (len == 0)
-               len = ls + 1; /* len is number of *bytes* */
-
-       while (lp <= ls && (p = strstr_m(s,pattern))) {
-               if (ls + (li-lp) >= len) {
-                       DEBUG(0,("ERROR: string overflow by "
-                               "%d in string_sub(%.50s, %d)\n",
-                                (int)(ls + (li-lp) - len),
-                                pattern, (int)len));
-                       break;
-               }
-               if (li != lp) {
-                       memmove(p+li,p+lp,strlen(p+lp)+1);
-               }
-               for (i=0;i<li;i++) {
-                       switch (insert[i]) {
-                       case '$':
-                               /* allow a trailing $
-                                * (as in machine accounts) */
-                               if (allow_trailing_dollar && (i == li - 1 )) {
-                                       p[i] = insert[i];
-                                       break;
-                               }
-                       case '`':
-                       case '"':
-                       case '\'':
-                       case ';':
-                       case '%':
-                       case '\r':
-                       case '\n':
-                               if ( remove_unsafe_characters ) {
-                                       p[i] = '_';
-                                       /* yes this break should be here
-                                        * since we want to fall throw if
-                                        * not replacing unsafe chars */
-                                       break;
-                               }
-                       default:
-                               p[i] = insert[i];
-                       }
-               }
-               s = p + li;
-               ls += (li-lp);
-
-               if (replace_once)
-                       break;
-       }
-}
-
-void string_sub_once(char *s, const char *pattern,
-               const char *insert, size_t len)
-{
-       string_sub2( s, pattern, insert, len, true, true, false );
-}
-
-void string_sub(char *s,const char *pattern, const char *insert, size_t len)
-{
-       string_sub2( s, pattern, insert, len, true, false, false );
-}
-
 void fstring_sub(char *s,const char *pattern,const char *insert)
 {
        string_sub(s, pattern, insert, sizeof(fstring));
@@ -789,48 +703,6 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx,
                        true, false, false);
 }
 
-/**
- Similar to string_sub() but allows for any character to be substituted.
- Use with caution!
- if len==0 then the string cannot be extended. This is different from the old
- use of len==0 which was for no length checks to be done.
-**/
-
-void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
-{
-       char *p;
-       ssize_t ls,lp,li;
-
-       if (!insert || !pattern || !s)
-               return;
-
-       ls = (ssize_t)strlen(s);
-       lp = (ssize_t)strlen(pattern);
-       li = (ssize_t)strlen(insert);
-
-       if (!*pattern)
-               return;
-
-       if (len == 0)
-               len = ls + 1; /* len is number of *bytes* */
-
-       while (lp <= ls && (p = strstr_m(s,pattern))) {
-               if (ls + (li-lp) >= len) {
-                       DEBUG(0,("ERROR: string overflow by "
-                               "%d in all_string_sub(%.50s, %d)\n",
-                                (int)(ls + (li-lp) - len),
-                                pattern, (int)len));
-                       break;
-               }
-               if (li != lp) {
-                       memmove(p+li,p+lp,strlen(p+lp)+1);
-               }
-               memcpy(p, insert, li);
-               s = p + li;
-               ls += (li-lp);
-       }
-}
-
 char *talloc_all_string_sub(TALLOC_CTX *ctx,
                                const char *src,
                                const char *pattern,