aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-04-12 11:01:36 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-04-12 11:01:36 +0000
commitdc854bdfcff4f07dd70479484c4137f8d1a1320b (patch)
treeb5cbc9921cccb2796bb761f9fc113f7290f6bbd8
parent19a9f06eea66a8dfa3c7fa2585d98f39bced3bda (diff)
downloadlibmicrohttpd-dc854bdfcff4f07dd70479484c4137f8d1a1320b.tar.gz
libmicrohttpd-dc854bdfcff4f07dd70479484c4137f8d1a1320b.zip
mhd_str: refactored MHD_str_to_uint64() and MHD_str_to_uint64_n_() for simplification, more
error checking and optimization
-rw-r--r--src/microhttpd/connection.c6
-rw-r--r--src/microhttpd/mhd_str.c99
-rw-r--r--src/microhttpd/mhd_str.h27
3 files changed, 49 insertions, 83 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index 10ac43bf..6e75ebd5 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -2100,9 +2100,9 @@ parse_connection_headers (struct MHD_Connection *connection)
2100 MHD_HTTP_HEADER_CONTENT_LENGTH); 2100 MHD_HTTP_HEADER_CONTENT_LENGTH);
2101 if (NULL != clen) 2101 if (NULL != clen)
2102 { 2102 {
2103 if (!MHD_str_to_uint64_ (clen, &connection->remaining_upload_size, 2103 end = clen + MHD_str_to_uint64_ (clen,
2104 &end) || 2104 &connection->remaining_upload_size);
2105 ('\0' != *end) ) 2105 if ( (clen == end) || ('\0' != *end) )
2106 { 2106 {
2107 connection->remaining_upload_size = 0; 2107 connection->remaining_upload_size = 0;
2108#ifdef HAVE_MESSAGES 2108#ifdef HAVE_MESSAGES
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index 82b12311..7a8dbd93 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -237,47 +237,33 @@ MHD_str_equal_caseless_n_ (const char * const str1, const char * const str2, siz
237 * Conversion stopped at first non-digit character. 237 * Conversion stopped at first non-digit character.
238 * @param str string to convert 238 * @param str string to convert
239 * @param out_val pointer to uint64_t to store result of conversion 239 * @param out_val pointer to uint64_t to store result of conversion
240 * @param next_char pointer to store pointer to character next to last 240 * @return non-zero number of characters processed on succeed,
241 * converted digit, ignored if NULL 241 * zero if no digit is found, resulting value is larger
242 * @return non-zero if conversion succeed; zero if no digit is found, 242 * then possible to store in uint64_t or @a out_val is NULL
243 * value is larger then possible to store in uint64_t or
244 * @a out_val or @a str is NULL
245 */ 243 */
246int 244size_t
247MHD_str_to_uint64_ (const char * str, uint64_t * out_val, const char ** next_char) 245MHD_str_to_uint64_ (const char * str, uint64_t * out_val)
248{ 246{
247 const char * const start = str;
249 uint64_t res; 248 uint64_t res;
250 if (!str || !isasciidigit(str[0])) 249 if (!str || !out_val || !isasciidigit(str[0]))
251 { 250 return 0;
252 if (next_char)
253 *next_char = str;
254 return 0;
255 }
256 251
257 res = 0; 252 res = 0;
258 do 253 do
259 { 254 {
260 const int digit = str[0] - '0'; 255 const int digit = (unsigned char)(*str) - '0';
261 if ( (res < (UINT64_MAX / 10)) || 256 if ( (res > (UINT64_MAX / 10)) ||
262 (res == (UINT64_MAX / 10) && digit <= (UINT64_MAX % 10)) ) 257 (res == (UINT64_MAX / 10) && digit > (UINT64_MAX % 10)) )
263 { 258 return 0;
264 res *= 10; 259
265 res += digit; 260 res *= 10;
266 } 261 res += digit;
267 else
268 {
269 if (next_char)
270 *next_char = str;
271 return 0;
272 }
273 str++; 262 str++;
274 } while (isasciidigit (str[0])); 263 } while (isasciidigit (*str));
275 264
276 *out_val = res; 265 *out_val = res;
277 if (next_char) 266 return str - start;
278 *next_char = str;
279
280 return !0;
281} 267}
282 268
283 269
@@ -287,49 +273,34 @@ MHD_str_to_uint64_ (const char * str, uint64_t * out_val, const char ** next_cha
287 * Conversion stopped at first non-digit character or after @a maxlen 273 * Conversion stopped at first non-digit character or after @a maxlen
288 * digits. 274 * digits.
289 * @param str string to convert 275 * @param str string to convert
276 * @param maxlen maximum number of characters to process
290 * @param out_val pointer to uint64_t to store result of conversion 277 * @param out_val pointer to uint64_t to store result of conversion
291 * @param next_char pointer to store pointer to character next to last 278 * @return non-zero number of characters processed on succeed,
292 * converted digit, ignored if NULL 279 * zero if no digit is found, resulting value is larger
293 * @return non-zero if conversion succeed; zero if no digit is found, 280 * then possible to store in uint64_t or @a out_val is NULL
294 * value is larger then possible to store in uint64_t or
295 * @a out_val is NULL
296 */ 281 */
297int 282size_t
298MHD_str_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val, 283MHD_str_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val)
299 const char ** next_char)
300{ 284{
301 uint64_t res; 285 uint64_t res;
302 size_t i; 286 size_t i;
303 if (!str || !maxlen || !isasciidigit (str[0])) 287 if (!str || !maxlen || !out_val || !isasciidigit (str[0]))
304 { 288 return 0;
305 if (next_char)
306 *next_char = str;
307 return 0;
308 }
309 289
310 res = 0; 290 res = 0;
311 i = 0; 291 i = 0;
312 do 292 do
313 { 293 {
314 const int digit = str[i] - '0'; 294 const int digit = (unsigned char)str[i] - '0';
315 if ( (res < (UINT64_MAX / 10)) || 295 if ( (res > (UINT64_MAX / 10)) ||
316 (res == (UINT64_MAX / 10) && digit <= (UINT64_MAX % 10)) ) 296 (res == (UINT64_MAX / 10) && digit > (UINT64_MAX % 10)) )
317 { 297 return 0;
318 res *= 10;
319 res += digit;
320 }
321 else
322 {
323 if (next_char)
324 *next_char = str + i;
325 return 0;
326 }
327
328 } while(i < maxlen && isasciidigit(str[i]));
329 298
330 *out_val = res; 299 res *= 10;
331 if (next_char) 300 res += digit;
332 *next_char = str + i; 301 i++;
302 } while(i < maxlen && isasciidigit(str[i]));
333 303
334 return !0; 304 *out_val= res;
305 return i;
335} 306}
diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h
index e14a308e..845e6389 100644
--- a/src/microhttpd/mhd_str.h
+++ b/src/microhttpd/mhd_str.h
@@ -65,16 +65,13 @@ MHD_str_equal_caseless_n_ (const char * const str1,
65 * Conversion stopped at first non-digit character. 65 * Conversion stopped at first non-digit character.
66 * @param str string to convert 66 * @param str string to convert
67 * @param out_val pointer to uint64_t to store result of conversion 67 * @param out_val pointer to uint64_t to store result of conversion
68 * @param next_char pointer to store pointer to character next to last 68 * @return non-zero number of characters processed on succeed,
69 * converted digit, ignored if NULL 69 * zero if no digit is found, resulting value is larger
70 * @return non-zero if conversion succeed; zero if no digit is found, 70 * then possible to store in uint64_t or @a out_val is NULL
71 * value is larger then possible to store in uint64_t or
72 * @a out_val is NULL
73 */ 71 */
74int 72size_t
75MHD_str_to_uint64_ (const char * str, 73MHD_str_to_uint64_ (const char * str,
76 uint64_t * out_val, 74 uint64_t * out_val);
77 const char ** next_char);
78 75
79/** 76/**
80 * Convert not more then @a maxlen decimal US-ASCII digits in string to 77 * Convert not more then @a maxlen decimal US-ASCII digits in string to
@@ -82,17 +79,15 @@ MHD_str_to_uint64_ (const char * str,
82 * Conversion stopped at first non-digit character or after @a maxlen 79 * Conversion stopped at first non-digit character or after @a maxlen
83 * digits. 80 * digits.
84 * @param str string to convert 81 * @param str string to convert
82 * @param maxlen maximum number of characters to process
85 * @param out_val pointer to uint64_t to store result of conversion 83 * @param out_val pointer to uint64_t to store result of conversion
86 * @param next_char pointer to store pointer to character next to last 84 * @return non-zero number of characters processed on succeed,
87 * converted digit, ignored if NULL 85 * zero if no digit is found, resulting value is larger
88 * @return non-zero if conversion succeed; zero if no digit is found, 86 * then possible to store in uint64_t or @a out_val is NULL
89 * value is larger then possible to store in uint64_t or
90 * @a out_val is NULL
91 */ 87 */
92int 88size_t
93MHD_str_to_uint64_n_ (const char * str, 89MHD_str_to_uint64_n_ (const char * str,
94 size_t maxlen, 90 size_t maxlen,
95 uint64_t * out_val, 91 uint64_t * out_val);
96 const char ** next_char);
97 92
98#endif /* MHD_STR_H */ 93#endif /* MHD_STR_H */