Source
/*
** 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/>.
**/
/******************************************************************************
* *
* Purpose: replaces unsafe characters with a '%' followed by two hexadecimal *
* digits (the only allowed exception is a space character that can *
* be replaced with a plus (+) sign or with %20).to url encode *
* *
* Parameters: source - [IN] the value to encode *
* result - [OUT] encoded string *
* *
******************************************************************************/
void zbx_url_encode(const char *source, char **result)
{
char *target, *buffer;
const char *hex = "0123456789ABCDEF";
buffer = (char *)zbx_malloc(NULL, strlen(source) * 3 + 1);
target = buffer;
while ('\0' != *source)
{
if (0 == isalnum(*source) && NULL == strchr("-._~", *source))
{
/* Percent-encoding */
*target++ = '%';
*target++ = hex[(unsigned char)*source >> 4];
*target++ = hex[(unsigned char)*source & 15];
}
else
*target++ = *source;
source++;
}
*target = '\0';
zbx_free(*result);
*result = buffer;
}
/******************************************************************************
* *
* Purpose: replaces URL escape sequences ('+' or '%' followed by two *
* hexadecimal digits) with matching characters. *
* *
* Parameters: source - [IN] the value to decode *
* result - [OUT] decoded string *