| 1 |
/* |
| 2 |
This file is part of GNU libmicrohttpd |
| 3 |
Copyright (C) 2023 Evgeny Grin (Karlson2k) |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library; if not, write to the Free Software |
| 17 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
/** |
| 21 |
* @file tools/mhd_tool_str_to_uint.h |
| 22 |
* @brief Function to decode the value of decimal string number. |
| 23 |
* @author Karlson2k (Evgeny Grin) |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef MHD_TOOL_STR_TO_UINT_H_ |
| 27 |
#define MHD_TOOL_STR_TO_UINT_H_ 1 |
| 28 |
|
| 29 |
#include <stddef.h> |
| 30 |
|
| 31 |
/** |
| 32 |
* Convert decimal string to unsigned int. |
| 33 |
* Function stops at the end of the string or on first non-digit character. |
| 34 |
* @param str the string to convert |
| 35 |
* @param[out] value the pointer to put the result |
| 36 |
* @return return the number of digits converted or |
| 37 |
* zero if no digits found or result would overflow the output |
| 38 |
* variable (the output set to UINT_MAX in this case). |
| 39 |
*/ |
| 40 |
static size_t |
| 41 |
mhd_tool_str_to_uint (const char *str, unsigned int *value) |
| 42 |
{ |
| 43 |
size_t i; |
| 44 |
unsigned int v = 0; |
| 45 |
*value = 0; |
| 46 |
|
| 47 |
for (i = 0; 0 != str[i]; ++i) |
| 48 |
{ |
| 49 |
const char chr = str[i]; |
| 50 |
unsigned int digit; |
| 51 |
if (('0' > chr) || ('9' < chr)) |
| 52 |
break; |
| 53 |
digit = (unsigned char) (chr - '0'); |
| 54 |
if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v)) |
| 55 |
{ |
| 56 |
/* Overflow */ |
| 57 |
*value = 0U - 1; |
| 58 |
return 0; |
| 59 |
} |
| 60 |
v *= 10; |
| 61 |
v += digit; |
| 62 |
} |
| 63 |
*value = v; |
| 64 |
return i; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
#endif /* MHD_TOOL_STR_TO_UINT_H_ */ |