parse_number.h 8.4 KB

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