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: Checks if the string is an unsigned integer within the specified *
* range and optionally stores it into the value parameter. *
* *
* Parameters: str - [IN] string to check *
* n - [IN] string length or ZBX_MAX_UINT64_LEN *
* value - [OUT] pointer to output buffer where converted value *
* is to be written (optional, can be NULL) *
* size - [IN] size of output buffer (optional) *
* min - [IN] minimum acceptable value *
* max - [IN] maximum acceptable value *
* *
* Return value: SUCCEED - string is unsigned integer *
* FAIL - string is not number or its value is outside *
* specified range *
* *
******************************************************************************/
int zbx_is_uint_n_range(const char *str, size_t n, void *value, size_t size, zbx_uint64_t min, zbx_uint64_t max)
{
zbx_uint64_t value_uint64 = 0, c;
const zbx_uint64_t max_uint64 = ~__UINT64_C(0);
if ('\0' == *str || 0 == n || sizeof(zbx_uint64_t) < size || (0 == size && NULL != value))
return FAIL;
while ('\0' != *str && 0 < n--)
{
if (0 == isdigit(*str))
return FAIL; /* not a digit */
c = (zbx_uint64_t)(unsigned char)(*str - '0');
if ((max_uint64 - c) / 10 < value_uint64)
return FAIL; /* maximum value exceeded */
value_uint64 = value_uint64 * 10 + c;
str++;
}
if (min > value_uint64 || value_uint64 > max)
return FAIL;
if (NULL != value)
{
/* On little endian architecture the output value will be stored starting from the first bytes */