2 Unix SMB/CIFS implementation.
3 Samba utility functions
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
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.
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.
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/>.
26 #ifndef SAMBA_UTIL_CORE_ONLY
27 #include "charset/charset.h"
29 #include "charset_compat.h"
31 #include "substitute.h"
35 * @brief Substitute utilities.
39 Substitute a string for a pattern in another string. Make sure there is
42 This routine looks for pattern in s and replaces it with
43 insert. It may do multiple replacements or just one.
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.
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
)
57 if (!insert
|| !pattern
|| !*pattern
|| !s
)
65 len
= ls
+ 1; /* len is number of *bytes* */
67 while (lp
<= ls
&& (p
= strstr_m(s
,pattern
))) {
68 if (ls
+ li
- lp
>= len
) {
69 DBG_ERR("ERROR: string overflow by "
70 "%zu in string_sub(%.50s, %zu)\n",
71 ls
+ li
- lp
+ 1 - len
,
77 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
83 * (as in machine accounts) */
84 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
96 if ( remove_unsafe_characters
) {
98 /* yes this break should be here
99 * since we want to fall throw if
100 * not replacing unsafe chars */
116 void string_sub(char *s
,const char *pattern
, const char *insert
, size_t len
)
118 string_sub2( s
, pattern
, insert
, len
, true, false, false );
122 Similar to string_sub() but allows for any character to be substituted.
124 if len==0 then the string cannot be extended. This is different from the old
125 use of len==0 which was for no length checks to be done.
128 _PUBLIC_
void all_string_sub(char *s
,const char *pattern
,const char *insert
, size_t len
)
133 if (!insert
|| !pattern
|| !s
)
137 lp
= strlen(pattern
);
144 len
= ls
+ 1; /* len is number of *bytes* */
146 while (lp
<= ls
&& (p
= strstr_m(s
,pattern
))) {
147 if (ls
+ li
- lp
>= len
) {
148 DBG_ERR("ERROR: string overflow by "
149 "%zu in all_string_sub(%.50s, %zu)\n",
150 ls
+ li
- lp
+ 1 - len
,
156 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
158 memcpy(p
, insert
, li
);
165 * Internal guts of talloc_string_sub and talloc_all_string_sub.
166 * talloc version of string_sub2.
169 char *talloc_string_sub2(TALLOC_CTX
*mem_ctx
, const char *src
,
172 bool remove_unsafe_characters
,
174 bool allow_trailing_dollar
)
179 ssize_t ls
,lp
,li
,ld
, i
;
181 if (!insert
|| !pattern
|| !*pattern
|| !src
) {
185 string
= talloc_strdup(mem_ctx
, src
);
186 if (string
== NULL
) {
187 DEBUG(0, ("talloc_string_sub2: "
188 "talloc_strdup failed\n"));
194 in
= talloc_strdup(mem_ctx
, insert
);
196 DEBUG(0, ("talloc_string_sub2: ENOMEM\n"));
200 ls
= (ssize_t
)strlen(s
);
201 lp
= (ssize_t
)strlen(pattern
);
202 li
= (ssize_t
)strlen(insert
);
208 /* allow a trailing $
209 * (as in machine accounts) */
210 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
222 if (remove_unsafe_characters
) {
234 while ((p
= strstr_m(s
,pattern
))) {
236 int offset
= PTR_DIFF(s
,string
);
237 string
= (char *)talloc_realloc_size(mem_ctx
, string
,
240 DEBUG(0, ("talloc_string_sub: out of "
245 p
= string
+ offset
+ (p
- s
);
248 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
262 /* Same as string_sub, but returns a talloc'ed string */
264 char *talloc_string_sub(TALLOC_CTX
*mem_ctx
,
269 return talloc_string_sub2(mem_ctx
, src
, pattern
, insert
,
273 char *talloc_all_string_sub(TALLOC_CTX
*ctx
,
278 return talloc_string_sub2(ctx
, src
, pattern
, insert
,
279 false, false, false);