libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit d3519acbbe19e693c09271d5c1186273a3273469
parent f586f2e7c1ebb471a76e24315783412dce2d0fec
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Mon, 10 Jul 2023 13:40:10 +0300

perf_replies: moved one function to separate header

Diffstat:
Msrc/tools/Makefile.am | 2+-
Asrc/tools/mhd_tool_str_to_uint.h | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/tools/perf_replies.c | 46+++++-----------------------------------------
3 files changed, 75 insertions(+), 42 deletions(-)

diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am @@ -36,4 +36,4 @@ endif perf_replies_SOURCES = \ - perf_replies.c + perf_replies.c mhd_tool_str_to_uint.h diff --git a/src/tools/mhd_tool_str_to_uint.h b/src/tools/mhd_tool_str_to_uint.h @@ -0,0 +1,69 @@ +/* + This file is part of GNU libmicrohttpd + Copyright (C) 2023 Evgeny Grin (Karlson2k) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file tools/mhd_tool_str_to_uint.c + * @brief Implementation of HTTP server optimised for fast replies + * based on MHD. + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_TOOL_STR_TO_UINT_H_ +#define MHD_TOOL_STR_TO_UINT_H_ + +#include <stddef.h> + +/** + * Convert decimal string to unsigned int. + * Function stops at the end of the string or on first non-digit character. + * @param str the string to convert + * @param[out] value the pointer to put the result + * @return return the number of digits converted or + * zero if no digits found or result would overflow the output + * variable (the output set to UINT_MAX in this case). + */ +static size_t +mhd_tool_str_to_uint (const char *str, unsigned int *value) +{ + size_t i; + unsigned int v = 0; + *value = 0; + + for (i = 0; 0 != str[i]; ++i) + { + const char chr = str[i]; + unsigned int digit; + if (('0' > chr) || ('9' < chr)) + break; + digit = (unsigned char) (chr - '0'); + if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v)) + { + /* Overflow */ + *value = 0U - 1; + return 0; + } + v *= 10; + v += digit; + } + *value = v; + return i; +} + + +#endif /* MHD_TOOL_STR_TO_UINT_H_ */ diff --git a/src/tools/perf_replies.c b/src/tools/perf_replies.c @@ -26,7 +26,7 @@ */ /** - * @file examples/perf_replies.c + * @file tools/perf_replies.c * @brief Implementation of HTTP server optimised for fast replies * based on MHD. * @author Karlson2k (Evgeny Grin) @@ -38,6 +38,7 @@ #include <stdint.h> #include "mhd_options.h" #include "microhttpd.h" +#include "mhd_tool_str_to_uint.h" #if defined(MHD_REAL_CPU_COUNT) #if MHD_REAL_CPU_COUNT == 0 @@ -106,43 +107,6 @@ set_self_name (int argc, char *const *argv) } -/** - * Convert decimal string to unsigned int. - * Function stops at the end of the string or on first non-digit character. - * @param str the string to convert - * @param[out] value the pointer to put the result - * @return return the number of digits converted or - * zero if no digits found or result would overflow the output - * variable (the output set to UINT_MAX in this case). - */ -static size_t -str_to_uint (const char *str, unsigned int *value) -{ - size_t i; - unsigned int v = 0; - *value = 0; - - for (i = 0; 0 != str[i]; ++i) - { - const char chr = str[i]; - unsigned int digit; - if (('0' > chr) || ('9' < chr)) - break; - digit = (unsigned char) (chr - '0'); - if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v)) - { - /* Overflow */ - *value = 0U - 1; - return 0; - } - v *= 10; - v += digit; - } - *value = v; - return i; -} - - #if defined (HAVE_POPEN) && defined(HAVE_PCLOSE) /** * Read the command output as a number and return the number. @@ -175,7 +139,7 @@ get_cmd_out_as_number (const char *cmd) { size_t digits_found; unsigned int out_value; - digits_found = str_to_uint (buf, &out_value); + digits_found = mhd_tool_str_to_uint (buf, &out_value); if (0 != digits_found) { if ((0 == buf[digits_found]) @@ -302,7 +266,7 @@ get_param_value (const char *param_name, const char *param_tail, value_str = next_param; if (NULL != value_str) - digits = str_to_uint (value_str, param_value); + digits = mhd_tool_str_to_uint (value_str, param_value); else digits = 0; @@ -1033,7 +997,7 @@ process_params (int argc, char *const *argv) /* Process the port number */ unsigned int read_port; size_t num_digits; - num_digits = str_to_uint (p, &read_port); + num_digits = mhd_tool_str_to_uint (p, &read_port); if (0 != p[num_digits]) { fprintf (stderr, "Error in specified port number: %s\n", p);