aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_str.c
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-04-12 14:36:11 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-04-12 14:36:11 +0000
commit095d09c986c895f197d32a8aef67f9f42efcdebe (patch)
tree612709af43dcfcd53f7d312235156de144179d2c /src/microhttpd/mhd_str.c
parent36d82b1a0d34dc1b436abeeae9984b101718086c (diff)
downloadlibmicrohttpd-095d09c986c895f197d32a8aef67f9f42efcdebe.tar.gz
libmicrohttpd-095d09c986c895f197d32a8aef67f9f42efcdebe.zip
mhd_str: added MHD_strx_to_uint32_(), MHD_strx_to_uint32_n_(), MHD_strx_to_uint64_() and
MHD_strx_to_uint64_n_() functions
Diffstat (limited to 'src/microhttpd/mhd_str.c')
-rw-r--r--src/microhttpd/mhd_str.c164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index f54b45ed..be8249fb 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -437,3 +437,167 @@ MHD_strx_to_sizet_n_ (const char * str, size_t maxlen, size_t * out_val)
437 *out_val = res; 437 *out_val = res;
438 return i; 438 return i;
439} 439}
440
441
442/**
443 * Convert hexadecimal US-ASCII digits in string to number in uint32_t.
444 * Conversion stopped at first non-digit character.
445 * @param str string to convert
446 * @param out_val pointer to uint32_t to store result of conversion
447 * @return non-zero number of characters processed on succeed,
448 * zero if no digit is found, resulting value is larger
449 * then possible to store in uint32_t or @a out_val is NULL
450 */
451size_t
452MHD_strx_to_uint32_ (const char * str, uint32_t * out_val)
453{
454 const char * const start = str;
455 uint32_t res;
456 int digit;
457 if (!str || !out_val)
458 return 0;
459
460 res = 0;
461 digit = toxdigitvalue (*str);
462 while (digit >= 0)
463 {
464 if ( (res < (UINT32_MAX / 16)) ||
465 (res == (UINT32_MAX / 16) && digit <= (UINT32_MAX % 16)) )
466 {
467 res *= 16;
468 res += digit;
469 }
470 else
471 return 0;
472 str++;
473 digit = toxdigitvalue (*str);
474 }
475
476 if (str - start > 0)
477 *out_val = res;
478 return str - start;
479}
480
481
482/**
483 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
484 * to number in uint32_t.
485 * Conversion stopped at first non-digit character or after @a maxlen
486 * digits.
487 * @param str string to convert
488 * @param maxlen maximum number of characters to process
489 * @param out_val pointer to uint32_t to store result of conversion
490 * @param next_char pointer to store pointer to character next to last
491 * converted digit, ignored if NULL
492 * @return non-zero number of characters processed on succeed,
493 * zero if no digit is found, resulting value is larger
494 * then possible to store in uint32_t or @a out_val is NULL
495 */
496size_t
497MHD_strx_to_uint32_n_ (const char * str, size_t maxlen, uint32_t * out_val)
498{
499 size_t i;
500 uint32_t res;
501 int digit;
502 if (!str || !out_val)
503 return 0;
504
505 res = 0;
506 i = 0;
507 while (i < maxlen && (digit = toxdigitvalue (str[i])) >= 0)
508 {
509 if ( (res > (UINT32_MAX / 16)) ||
510 (res == (UINT32_MAX / 16) && digit > (UINT32_MAX % 16)) )
511 return 0;
512
513 res *= 16;
514 res += digit;
515 i++;
516 }
517
518 if (i)
519 *out_val = res;
520 return i;
521}
522
523
524/**
525 * Convert hexadecimal US-ASCII digits in string to number in uint64_t.
526 * Conversion stopped at first non-digit character.
527 * @param str string to convert
528 * @param out_val pointer to uint64_t to store result of conversion
529 * @return non-zero number of characters processed on succeed,
530 * zero if no digit is found, resulting value is larger
531 * then possible to store in uint64_t or @a out_val is NULL
532 */
533size_t
534MHD_strx_to_uint64_ (const char * str, uint64_t * out_val)
535{
536 const char * const start = str;
537 uint64_t res;
538 int digit;
539 if (!str || !out_val)
540 return 0;
541
542 res = 0;
543 digit = toxdigitvalue (*str);
544 while (digit >= 0)
545 {
546 if ( (res < (UINT64_MAX / 16)) ||
547 (res == (UINT64_MAX / 16) && digit <= (UINT64_MAX % 16)) )
548 {
549 res *= 16;
550 res += digit;
551 }
552 else
553 return 0;
554 str++;
555 digit = toxdigitvalue (*str);
556 }
557
558 if (str - start > 0)
559 *out_val = res;
560 return str - start;
561}
562
563
564/**
565 * Convert not more then @a maxlen hexadecimal US-ASCII digits in string
566 * to number in uint64_t.
567 * Conversion stopped at first non-digit character or after @a maxlen
568 * digits.
569 * @param str string to convert
570 * @param maxlen maximum number of characters to process
571 * @param out_val pointer to uint64_t to store result of conversion
572 * @param next_char pointer to store pointer to character next to last
573 * converted digit, ignored if NULL
574 * @return non-zero number of characters processed on succeed,
575 * zero if no digit is found, resulting value is larger
576 * then possible to store in uint64_t or @a out_val is NULL
577 */
578size_t
579MHD_strx_to_uint64_n_ (const char * str, size_t maxlen, uint64_t * out_val)
580{
581 size_t i;
582 uint64_t res;
583 int digit;
584 if (!str || !out_val)
585 return 0;
586
587 res = 0;
588 i = 0;
589 while (i < maxlen && (digit = toxdigitvalue (str[i])) >= 0)
590 {
591 if ( (res > (UINT64_MAX / 16)) ||
592 (res == (UINT64_MAX / 16) && digit > (UINT64_MAX % 16)) )
593 return 0;
594
595 res *= 16;
596 res += digit;
597 i++;
598 }
599
600 if (i)
601 *out_val = res;
602 return i;
603}