aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_str.c
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 /src/microhttpd/mhd_str.c
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
Diffstat (limited to 'src/microhttpd/mhd_str.c')
-rw-r--r--src/microhttpd/mhd_str.c99
1 files changed, 35 insertions, 64 deletions
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}