lib:util: Add FALL_THROUGH statements in substitute.c
[samba.git] / lib / util / substitute.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    
5    Copyright (C) Andrew Tridgell 1992-2001
6    Copyright (C) Simo Sorce      2001-2002
7    Copyright (C) Martin Pool     2003
8    Copyright (C) James Peach     2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
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"
32
33 /**
34  * @file
35  * @brief Substitute utilities.
36  **/
37
38 /**
39  Substitute a string for a pattern in another string. Make sure there is
40  enough room!
41
42  This routine looks for pattern in s and replaces it with
43  insert. It may do multiple replacements or just one.
44
45  Any of " ; ' $ or ` in the insert string are replaced with _
46  if len==0 then the string cannot be extended. This is different from the old
47  use of len==0 which was for no length checks to be done.
48 **/
49
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)
53 {
54         char *p;
55         ssize_t ls, lp, li, i;
56
57         if (!insert || !pattern || !*pattern || !s)
58                 return;
59
60         ls = (ssize_t)strlen(s);
61         lp = (ssize_t)strlen(pattern);
62         li = (ssize_t)strlen(insert);
63
64         if (len == 0)
65                 len = ls + 1; /* len is number of *bytes* */
66
67         while (lp <= ls && (p = strstr_m(s,pattern))) {
68                 if (ls + (li-lp) >= len) {
69                         DEBUG(0,("ERROR: string overflow by "
70                                 "%d in string_sub(%.50s, %d)\n",
71                                  (int)(ls + (li-lp) - len),
72                                  pattern, (int)len));
73                         break;
74                 }
75                 if (li != lp) {
76                         memmove(p+li,p+lp,strlen(p+lp)+1);
77                 }
78                 for (i=0;i<li;i++) {
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                                 }
87                                 FALL_THROUGH;
88                         case '`':
89                         case '"':
90                         case '\'':
91                         case ';':
92                         case '%':
93                         case '\r':
94                         case '\n':
95                                 if ( remove_unsafe_characters ) {
96                                         p[i] = '_';
97                                         /* yes this break should be here
98                                          * since we want to fall throw if
99                                          * not replacing unsafe chars */
100                                         break;
101                                 }
102                                 FALL_THROUGH;
103                         default:
104                                 p[i] = insert[i];
105                         }
106                 }
107                 s = p + li;
108                 ls += (li-lp);
109
110                 if (replace_once)
111                         break;
112         }
113 }
114
115 void string_sub_once(char *s, const char *pattern,
116                 const char *insert, size_t len)
117 {
118         string_sub2( s, pattern, insert, len, true, true, false );
119 }
120
121 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
122 {
123         string_sub2( s, pattern, insert, len, true, false, false );
124 }
125
126 /**
127  * Talloc'ed version of string_sub
128  */
129 _PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, 
130                                 const char *pattern, const char *insert)
131 {
132         const char *p;
133         char *ret;
134         size_t len, alloc_len;
135
136         if (insert == NULL || pattern == NULL || !*pattern || s == NULL)
137                 return NULL;
138
139         /* determine length needed */
140         len = strlen(s);
141         
142         for (p = strstr(s, pattern); p != NULL; 
143              p = strstr(p+strlen(pattern), pattern)) {
144                 len += strlen(insert) - strlen(pattern);
145         }
146
147         alloc_len = MAX(len, strlen(s))+1;
148         ret = talloc_array(mem_ctx, char, alloc_len);
149         if (ret == NULL)
150                 return NULL;
151         strncpy(ret, s, alloc_len);
152         string_sub(ret, pattern, insert, alloc_len);
153
154         ret = talloc_realloc(mem_ctx, ret, char, len+1);
155         if (ret == NULL)
156                 return NULL;
157
158         if (ret[len] != '\0') {
159                 DEBUG(0,("Internal error at %s(%d): string not terminated\n",
160                          __FILE__, __LINE__));
161                 abort();
162         }
163
164         talloc_set_name_const(ret, ret);
165
166         return ret;
167 }
168
169 /**
170  Similar to string_sub() but allows for any character to be substituted. 
171  Use with caution!
172  if len==0 then the string cannot be extended. This is different from the old
173  use of len==0 which was for no length checks to be done.
174 **/
175
176 _PUBLIC_ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
177 {
178         char *p;
179         ssize_t ls,lp,li;
180
181         if (!insert || !pattern || !s)
182                 return;
183
184         ls = (ssize_t)strlen(s);
185         lp = (ssize_t)strlen(pattern);
186         li = (ssize_t)strlen(insert);
187
188         if (!*pattern)
189                 return;
190
191         if (len == 0)
192                 len = ls + 1; /* len is number of *bytes* */
193
194         while (lp <= ls && (p = strstr_m(s,pattern))) {
195                 if (ls + (li-lp) >= len) {
196                         DEBUG(0,("ERROR: string overflow by "
197                                 "%d in all_string_sub(%.50s, %d)\n",
198                                  (int)(ls + (li-lp) - len),
199                                  pattern, (int)len));
200                         break;
201                 }
202                 if (li != lp) {
203                         memmove(p+li,p+lp,strlen(p+lp)+1);
204                 }
205                 memcpy(p, insert, li);
206                 s = p + li;
207                 ls += (li-lp);
208         }
209 }