Changeset 988 for vendor/current/lib/util/substitute.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/lib/util/substitute.c
r414 r988 22 22 */ 23 23 24 #include "includes.h" 24 #include "replace.h" 25 #include "debug.h" 26 #ifndef SAMBA_UTIL_CORE_ONLY 27 #include "charset/charset.h" 28 #else 29 #include "charset_compat.h" 30 #endif 31 #include "substitute.h" 25 32 26 33 /** … … 30 37 31 38 /** 32 Substitute a string for a pattern in another string. Make sure there is 39 Substitute a string for a pattern in another string. Make sure there is 33 40 enough room! 34 41 35 This routine looks for pattern in s and replaces it with 36 insert. It may do multiple replacements .42 This routine looks for pattern in s and replaces it with 43 insert. It may do multiple replacements or just one. 37 44 38 45 Any of " ; ' $ or ` in the insert string are replaced with _ … … 41 48 **/ 42 49 43 _PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_t len) 50 static void string_sub2(char *s,const char *pattern, const char *insert, size_t len, 51 bool remove_unsafe_characters, bool replace_once, 52 bool allow_trailing_dollar) 44 53 { 45 54 char *p; … … 56 65 len = ls + 1; /* len is number of *bytes* */ 57 66 58 while (lp <= ls && (p = strstr (s,pattern))) {67 while (lp <= ls && (p = strstr_m(s,pattern))) { 59 68 if (ls + (li-lp) >= len) { 60 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", 69 DEBUG(0,("ERROR: string overflow by " 70 "%d in string_sub(%.50s, %d)\n", 61 71 (int)(ls + (li-lp) - len), 62 72 pattern, (int)len)); … … 68 78 for (i=0;i<li;i++) { 69 79 switch (insert[i]) { 80 case '$': 81 /* allow a trailing $ 82 * (as in machine accounts) */ 83 if (allow_trailing_dollar && (i == li - 1 )) { 84 p[i] = insert[i]; 85 break; 86 } 70 87 case '`': 71 88 case '"': 72 89 case '\'': 73 90 case ';': 74 case '$':75 91 case '%': 76 92 case '\r': 77 93 case '\n': 78 p[i] = '_'; 79 break; 94 if ( remove_unsafe_characters ) { 95 p[i] = '_'; 96 /* yes this break should be here 97 * since we want to fall throw if 98 * not replacing unsafe chars */ 99 break; 100 } 80 101 default: 81 102 p[i] = insert[i]; … … 84 105 s = p + li; 85 106 ls += (li-lp); 86 } 107 108 if (replace_once) 109 break; 110 } 111 } 112 113 void string_sub_once(char *s, const char *pattern, 114 const char *insert, size_t len) 115 { 116 string_sub2( s, pattern, insert, len, true, true, false ); 117 } 118 119 void string_sub(char *s,const char *pattern, const char *insert, size_t len) 120 { 121 string_sub2( s, pattern, insert, len, true, false, false ); 87 122 } 88 123 … … 119 154 return NULL; 120 155 121 SMB_ASSERT(ret[len] == '\0'); 156 if (ret[len] != '\0') { 157 DEBUG(0,("Internal error at %s(%d): string not terminated\n", 158 __FILE__, __LINE__)); 159 abort(); 160 } 122 161 123 162 talloc_set_name_const(ret, ret); … … 147 186 if (!*pattern) 148 187 return; 149 188 150 189 if (len == 0) 151 190 len = ls + 1; /* len is number of *bytes* */ 152 153 while (lp <= ls && (p = strstr (s,pattern))) {191 192 while (lp <= ls && (p = strstr_m(s,pattern))) { 154 193 if (ls + (li-lp) >= len) { 155 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n", 194 DEBUG(0,("ERROR: string overflow by " 195 "%d in all_string_sub(%.50s, %d)\n", 156 196 (int)(ls + (li-lp) - len), 157 197 pattern, (int)len));
Note:
See TracChangeset
for help on using the changeset viewer.