Source
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/******************************************************************************
* *
* Purpose: check if the string is unsigned integer within the specified *
* range and optionally store it into value parameter *
* *
* Parameters: str - [IN] string to check *
* n - [IN] string length or ZBX_MAX_UINT64_LEN *
* value - [OUT] a pointer to output buffer where the converted *
* value is to be written (optional, can be NULL) *
* size - [IN] size of the output buffer (optional) *
* min - [IN] the minimum acceptable value *
* max - [IN] the maximum acceptable value *
* *
* Return value: SUCCEED - the string is unsigned integer *
* FAIL - the string is not a number or its value is outside *
* the 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++;
}