parse_number.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <stdint.h>
  36. #include <string.h>
  37. #include <stddef.h>
  38. #include <limits.h>
  39. #include <misc/debug.h>
  40. // public functions
  41. static int decode_decimal_digit (char c);
  42. static int decode_hex_digit (char c);
  43. static int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  44. static int parse_unsigned_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  45. static int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  46. static int parse_unsigned_hex_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  47. // implementation follows
  48. // decimal representation of UINTMAX_MAX
  49. static const char parse_number__uintmax_max_str[] = "18446744073709551615";
  50. // make sure UINTMAX_MAX is what we think it is
  51. static const char parse_number__uintmax_max_str_assert[(UINTMAX_MAX == UINTMAX_C(18446744073709551615)) ? 1 : -1];
  52. static int decode_decimal_digit (char c)
  53. {
  54. switch (c) {
  55. case '0': return 0;
  56. case '1': return 1;
  57. case '2': return 2;
  58. case '3': return 3;
  59. case '4': return 4;
  60. case '5': return 5;
  61. case '6': return 6;
  62. case '7': return 7;
  63. case '8': return 8;
  64. case '9': return 9;
  65. }
  66. return -1;
  67. }
  68. static int decode_hex_digit (char c)
  69. {
  70. switch (c) {
  71. case '0': return 0;
  72. case '1': return 1;
  73. case '2': return 2;
  74. case '3': return 3;
  75. case '4': return 4;
  76. case '5': return 5;
  77. case '6': return 6;
  78. case '7': return 7;
  79. case '8': return 8;
  80. case '9': return 9;
  81. case 'A': case 'a': return 10;
  82. case 'B': case 'b': return 11;
  83. case 'C': case 'c': return 12;
  84. case 'D': case 'd': return 13;
  85. case 'E': case 'e': return 14;
  86. case 'F': case 'f': return 15;
  87. }
  88. return -1;
  89. }
  90. static int parse__no_overflow (const char *str, size_t str_len, uintmax_t *out)
  91. {
  92. uintmax_t n = 0;
  93. while (str_len > 0) {
  94. if (*str < '0' || *str > '9') {
  95. return 0;
  96. }
  97. n = 10 * n + (*str - '0');
  98. str++;
  99. str_len--;
  100. }
  101. *out = n;
  102. return 1;
  103. }
  104. int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  105. {
  106. // we do not allow empty strings
  107. if (str_len == 0) {
  108. return 0;
  109. }
  110. // remove leading zeros
  111. while (str_len > 0 && *str == '0') {
  112. str++;
  113. str_len--;
  114. }
  115. // detect overflow
  116. if (str_len > sizeof(parse_number__uintmax_max_str) - 1 ||
  117. (str_len == sizeof(parse_number__uintmax_max_str) - 1 && memcmp(str, parse_number__uintmax_max_str, sizeof(parse_number__uintmax_max_str) - 1) > 0)) {
  118. return 0;
  119. }
  120. // will not overflow (but can still have invalid characters)
  121. return parse__no_overflow(str, str_len, out);
  122. }
  123. int parse_unsigned_integer (const char *str, uintmax_t *out)
  124. {
  125. return parse_unsigned_integer_bin(str, strlen(str), out);
  126. }
  127. int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  128. {
  129. uintmax_t n = 0;
  130. if (str_len == 0) {
  131. return 0;
  132. }
  133. while (str_len > 0) {
  134. int digit = decode_hex_digit(*str);
  135. if (digit < 0) {
  136. return 0;
  137. }
  138. if (n > UINTMAX_MAX / 16) {
  139. return 0;
  140. }
  141. n *= 16;
  142. if (digit > UINTMAX_MAX - n) {
  143. return 0;
  144. }
  145. n += digit;
  146. str++;
  147. str_len--;
  148. }
  149. *out = n;
  150. return 1;
  151. }
  152. int parse_unsigned_hex_integer (const char *str, uintmax_t *out)
  153. {
  154. return parse_unsigned_hex_integer_bin(str, strlen(str), out);
  155. }
  156. #endif