parse_number.h 6.1 KB

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