parse_number.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file parse_number.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Numeric string parsing.
  32. */
  33. #ifndef BADVPN_MISC_PARSE_NUMBER_H
  34. #define BADVPN_MISC_PARSE_NUMBER_H
  35. #include <inttypes.h>
  36. #include <string.h>
  37. #include <stddef.h>
  38. #include <misc/debug.h>
  39. static int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  40. static int parse_unsigned_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  41. static int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  42. static int parse_unsigned_hex_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  43. static int decode_hex_digit (char c)
  44. {
  45. switch (c) {
  46. case '0': return 0;
  47. case '1': return 1;
  48. case '2': return 2;
  49. case '3': return 3;
  50. case '4': return 4;
  51. case '5': return 5;
  52. case '6': return 6;
  53. case '7': return 7;
  54. case '8': return 8;
  55. case '9': return 9;
  56. case 'A': case 'a': return 10;
  57. case 'B': case 'b': return 11;
  58. case 'C': case 'c': return 12;
  59. case 'D': case 'd': return 13;
  60. case 'E': case 'e': return 14;
  61. case 'F': case 'f': return 15;
  62. }
  63. return -1;
  64. }
  65. int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  66. {
  67. uintmax_t n = 0;
  68. if (str_len == 0) {
  69. return 0;
  70. }
  71. while (str_len > 0) {
  72. if (*str < '0' || *str > '9') {
  73. return 0;
  74. }
  75. int digit = *str - '0';
  76. if (n > UINTMAX_MAX / 10) {
  77. return 0;
  78. }
  79. n *= 10;
  80. if (digit > UINTMAX_MAX - n) {
  81. return 0;
  82. }
  83. n += digit;
  84. str++;
  85. str_len--;
  86. }
  87. *out = n;
  88. return 1;
  89. }
  90. int parse_unsigned_integer (const char *str, uintmax_t *out)
  91. {
  92. return parse_unsigned_integer_bin(str, strlen(str), out);
  93. }
  94. int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  95. {
  96. uintmax_t n = 0;
  97. if (str_len == 0) {
  98. return 0;
  99. }
  100. while (str_len > 0) {
  101. int digit = decode_hex_digit(*str);
  102. if (digit < 0) {
  103. return 0;
  104. }
  105. if (n > UINTMAX_MAX / 16) {
  106. return 0;
  107. }
  108. n *= 16;
  109. if (digit > UINTMAX_MAX - n) {
  110. return 0;
  111. }
  112. n += digit;
  113. str++;
  114. str_len--;
  115. }
  116. *out = n;
  117. return 1;
  118. }
  119. int parse_unsigned_hex_integer (const char *str, uintmax_t *out)
  120. {
  121. return parse_unsigned_hex_integer_bin(str, strlen(str), out);
  122. }
  123. #endif