parse_number.h 5.9 KB

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