parse_number.h 6.9 KB

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