aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2023-07-10 13:40:10 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2023-07-10 13:40:10 +0300
commitd3519acbbe19e693c09271d5c1186273a3273469 (patch)
treeea031e03294aebea13a35fefa25b969ef4d5c364
parentf586f2e7c1ebb471a76e24315783412dce2d0fec (diff)
downloadlibmicrohttpd-d3519acbbe19e693c09271d5c1186273a3273469.tar.gz
libmicrohttpd-d3519acbbe19e693c09271d5c1186273a3273469.zip
perf_replies: moved one function to separate header
-rw-r--r--src/tools/Makefile.am2
-rw-r--r--src/tools/mhd_tool_str_to_uint.h69
-rw-r--r--src/tools/perf_replies.c46
3 files changed, 75 insertions, 42 deletions
diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
index 427c659f..d9064969 100644
--- a/src/tools/Makefile.am
+++ b/src/tools/Makefile.am
@@ -36,4 +36,4 @@ endif
36 36
37 37
38perf_replies_SOURCES = \ 38perf_replies_SOURCES = \
39 perf_replies.c 39 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
new file mode 100644
index 00000000..5fc91b58
--- /dev/null
+++ b/src/tools/mhd_tool_str_to_uint.h
@@ -0,0 +1,69 @@
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.c
22 * @brief Implementation of HTTP server optimised for fast replies
23 * based on MHD.
24 * @author Karlson2k (Evgeny Grin)
25 */
26
27#ifndef MHD_TOOL_STR_TO_UINT_H_
28#define MHD_TOOL_STR_TO_UINT_H_
29
30#include <stddef.h>
31
32/**
33 * Convert decimal string to unsigned int.
34 * Function stops at the end of the string or on first non-digit character.
35 * @param str the string to convert
36 * @param[out] value the pointer to put the result
37 * @return return the number of digits converted or
38 * zero if no digits found or result would overflow the output
39 * variable (the output set to UINT_MAX in this case).
40 */
41static size_t
42mhd_tool_str_to_uint (const char *str, unsigned int *value)
43{
44 size_t i;
45 unsigned int v = 0;
46 *value = 0;
47
48 for (i = 0; 0 != str[i]; ++i)
49 {
50 const char chr = str[i];
51 unsigned int digit;
52 if (('0' > chr) || ('9' < chr))
53 break;
54 digit = (unsigned char) (chr - '0');
55 if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v))
56 {
57 /* Overflow */
58 *value = 0U - 1;
59 return 0;
60 }
61 v *= 10;
62 v += digit;
63 }
64 *value = v;
65 return i;
66}
67
68
69#endif /* MHD_TOOL_STR_TO_UINT_H_ */
diff --git a/src/tools/perf_replies.c b/src/tools/perf_replies.c
index 49a7a1f2..1b2fc533 100644
--- a/src/tools/perf_replies.c
+++ b/src/tools/perf_replies.c
@@ -26,7 +26,7 @@
26*/ 26*/
27 27
28/** 28/**
29 * @file examples/perf_replies.c 29 * @file tools/perf_replies.c
30 * @brief Implementation of HTTP server optimised for fast replies 30 * @brief Implementation of HTTP server optimised for fast replies
31 * based on MHD. 31 * based on MHD.
32 * @author Karlson2k (Evgeny Grin) 32 * @author Karlson2k (Evgeny Grin)
@@ -38,6 +38,7 @@
38#include <stdint.h> 38#include <stdint.h>
39#include "mhd_options.h" 39#include "mhd_options.h"
40#include "microhttpd.h" 40#include "microhttpd.h"
41#include "mhd_tool_str_to_uint.h"
41 42
42#if defined(MHD_REAL_CPU_COUNT) 43#if defined(MHD_REAL_CPU_COUNT)
43#if MHD_REAL_CPU_COUNT == 0 44#if MHD_REAL_CPU_COUNT == 0
@@ -106,43 +107,6 @@ set_self_name (int argc, char *const *argv)
106} 107}
107 108
108 109
109/**
110 * Convert decimal string to unsigned int.
111 * Function stops at the end of the string or on first non-digit character.
112 * @param str the string to convert
113 * @param[out] value the pointer to put the result
114 * @return return the number of digits converted or
115 * zero if no digits found or result would overflow the output
116 * variable (the output set to UINT_MAX in this case).
117 */
118static size_t
119str_to_uint (const char *str, unsigned int *value)
120{
121 size_t i;
122 unsigned int v = 0;
123 *value = 0;
124
125 for (i = 0; 0 != str[i]; ++i)
126 {
127 const char chr = str[i];
128 unsigned int digit;
129 if (('0' > chr) || ('9' < chr))
130 break;
131 digit = (unsigned char) (chr - '0');
132 if ((((0U - 1) / 10) < v) || ((v * 10 + digit) < v))
133 {
134 /* Overflow */
135 *value = 0U - 1;
136 return 0;
137 }
138 v *= 10;
139 v += digit;
140 }
141 *value = v;
142 return i;
143}
144
145
146#if defined (HAVE_POPEN) && defined(HAVE_PCLOSE) 110#if defined (HAVE_POPEN) && defined(HAVE_PCLOSE)
147/** 111/**
148 * Read the command output as a number and return the number. 112 * Read the command output as a number and return the number.
@@ -175,7 +139,7 @@ get_cmd_out_as_number (const char *cmd)
175 { 139 {
176 size_t digits_found; 140 size_t digits_found;
177 unsigned int out_value; 141 unsigned int out_value;
178 digits_found = str_to_uint (buf, &out_value); 142 digits_found = mhd_tool_str_to_uint (buf, &out_value);
179 if (0 != digits_found) 143 if (0 != digits_found)
180 { 144 {
181 if ((0 == buf[digits_found]) 145 if ((0 == buf[digits_found])
@@ -302,7 +266,7 @@ get_param_value (const char *param_name, const char *param_tail,
302 value_str = next_param; 266 value_str = next_param;
303 267
304 if (NULL != value_str) 268 if (NULL != value_str)
305 digits = str_to_uint (value_str, param_value); 269 digits = mhd_tool_str_to_uint (value_str, param_value);
306 else 270 else
307 digits = 0; 271 digits = 0;
308 272
@@ -1033,7 +997,7 @@ process_params (int argc, char *const *argv)
1033 /* Process the port number */ 997 /* Process the port number */
1034 unsigned int read_port; 998 unsigned int read_port;
1035 size_t num_digits; 999 size_t num_digits;
1036 num_digits = str_to_uint (p, &read_port); 1000 num_digits = mhd_tool_str_to_uint (p, &read_port);
1037 if (0 != p[num_digits]) 1001 if (0 != p[num_digits])
1038 { 1002 {
1039 fprintf (stderr, "Error in specified port number: %s\n", p); 1003 fprintf (stderr, "Error in specified port number: %s\n", p);