Source
void zbx_strquote_alloc_opt(char **str, size_t *str_alloc, size_t *str_offset, const char *value_str, int option)
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/* Has to be rewritten to avoid malloc */
char *zbx_string_replace(const char *str, const char *sub_str1, const char *sub_str2)
{
char *t, *new_str = NULL;
const char *p, *q, *r;
long len, diff, count = 0;
assert(str);
assert(sub_str1);
assert(sub_str2);
len = (long)strlen(sub_str1);
/* count the number of occurrences of sub_str1 */
for ( p=str; (p = strstr(p, sub_str1)); p+=len, count++ );
if (0 == count)
return zbx_strdup(NULL, str);
diff = (long)strlen(sub_str2) - len;
/* allocate new memory */
new_str = (char *)zbx_malloc(new_str, (strlen(str) + (size_t)(count * diff) + 1) * sizeof(char));
for (q=str,t=new_str,p=str; (p = strstr(p, sub_str1)); )
{
/* copy until next occurrence of sub_str1 */
for ( ; q < p; *t++ = *q++);
q += len;
p = q;
for ( r = sub_str2; (*t++ = *r++); );
--t;
}
/* copy the tail of str */
for( ; *q ; *t++ = *q++ );
*t = '\0';
return new_str;
}
int zbx_is_ascii_string(const char *str)
{