parse_number.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <misc/debug.h>
  39. static int decode_decimal_digit (char c);
  40. static int decode_hex_digit (char c);
  41. static int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  42. static int parse_unsigned_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  43. static int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  44. static int parse_unsigned_hex_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  45. static int decode_decimal_digit (char c)
  46. {
  47. switch (c) {
  48. case '0': return 0;
  49. case '1': return 1;
  50. case '2': return 2;
  51. case '3': return 3;
  52. case '4': return 4;
  53. case '5': return 5;
  54. case '6': return 6;
  55. case '7': return 7;
  56. case '8': return 8;
  57. case '9': return 9;
  58. }
  59. return -1;
  60. }
  61. static int decode_hex_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. case 'A': case 'a': return 10;
  75. case 'B': case 'b': return 11;
  76. case 'C': case 'c': return 12;
  77. case 'D': case 'd': return 13;
  78. case 'E': case 'e': return 14;
  79. case 'F': case 'f': return 15;
  80. }
  81. return -1;
  82. }
  83. int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  84. {
  85. uintmax_t n = 0;
  86. if (str_len == 0) {
  87. return 0;
  88. }
  89. while (str_len > 0) {
  90. if (*str < '0' || *str > '9') {
  91. return 0;
  92. }
  93. int digit = *str - '0';
  94. if (n > UINTMAX_MAX / 10) {
  95. return 0;
  96. }
  97. n *= 10;
  98. if (digit > UINTMAX_MAX - n) {
  99. return 0;
  100. }
  101. n += digit;
  102. str++;
  103. str_len--;
  104. }
  105. *out = n;
  106. return 1;
  107. }
  108. int parse_unsigned_integer (const char *str, uintmax_t *out)
  109. {
  110. return parse_unsigned_integer_bin(str, strlen(str), out);
  111. }
  112. int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  113. {
  114. uintmax_t n = 0;
  115. if (str_len == 0) {
  116. return 0;
  117. }
  118. while (str_len > 0) {
  119. int digit = decode_hex_digit(*str);
  120. if (digit < 0) {
  121. return 0;
  122. }
  123. if (n > UINTMAX_MAX / 16) {
  124. return 0;
  125. }
  126. n *= 16;
  127. if (digit > UINTMAX_MAX - n) {
  128. return 0;
  129. }
  130. n += digit;
  131. str++;
  132. str_len--;
  133. }
  134. *out = n;
  135. return 1;
  136. }
  137. int parse_unsigned_hex_integer (const char *str, uintmax_t *out)
  138. {
  139. return parse_unsigned_hex_integer_bin(str, strlen(str), out);
  140. }
  141. #endif