parse_number.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file parse_number.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Numeric string parsing.
  25. */
  26. #ifndef BADVPN_MISC_PARSE_NUMBER_H
  27. #define BADVPN_MISC_PARSE_NUMBER_H
  28. #include <inttypes.h>
  29. #include <string.h>
  30. #include <stddef.h>
  31. #include <misc/debug.h>
  32. static int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  33. static int parse_unsigned_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  34. static int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out) WARN_UNUSED;
  35. static int parse_unsigned_hex_integer (const char *str, uintmax_t *out) WARN_UNUSED;
  36. static int decode_hex_digit (char c)
  37. {
  38. switch (c) {
  39. case '0': return 0;
  40. case '1': return 1;
  41. case '2': return 2;
  42. case '3': return 3;
  43. case '4': return 4;
  44. case '5': return 5;
  45. case '6': return 6;
  46. case '7': return 7;
  47. case '8': return 8;
  48. case '9': return 9;
  49. case 'A': case 'a': return 10;
  50. case 'B': case 'b': return 11;
  51. case 'C': case 'c': return 12;
  52. case 'D': case 'd': return 13;
  53. case 'E': case 'e': return 14;
  54. case 'F': case 'f': return 15;
  55. }
  56. return -1;
  57. }
  58. int parse_unsigned_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  59. {
  60. uintmax_t n = 0;
  61. if (str_len == 0) {
  62. return 0;
  63. }
  64. while (str_len > 0) {
  65. if (*str < '0' || *str > '9') {
  66. return 0;
  67. }
  68. int digit = *str - '0';
  69. if (n > UINTMAX_MAX / 10) {
  70. return 0;
  71. }
  72. n *= 10;
  73. if (digit > UINTMAX_MAX - n) {
  74. return 0;
  75. }
  76. n += digit;
  77. str++;
  78. str_len--;
  79. }
  80. *out = n;
  81. return 1;
  82. }
  83. int parse_unsigned_integer (const char *str, uintmax_t *out)
  84. {
  85. return parse_unsigned_integer_bin(str, strlen(str), out);
  86. }
  87. int parse_unsigned_hex_integer_bin (const char *str, size_t str_len, uintmax_t *out)
  88. {
  89. uintmax_t n = 0;
  90. if (str_len == 0) {
  91. return 0;
  92. }
  93. while (str_len > 0) {
  94. int digit = decode_hex_digit(*str);
  95. if (digit < 0) {
  96. return 0;
  97. }
  98. if (n > UINTMAX_MAX / 16) {
  99. return 0;
  100. }
  101. n *= 16;
  102. if (digit > UINTMAX_MAX - n) {
  103. return 0;
  104. }
  105. n += digit;
  106. str++;
  107. str_len--;
  108. }
  109. *out = n;
  110. return 1;
  111. }
  112. int parse_unsigned_hex_integer (const char *str, uintmax_t *out)
  113. {
  114. return parse_unsigned_hex_integer_bin(str, strlen(str), out);
  115. }
  116. #endif