kstrtox.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_KSTRTOX_H
  3. #define _LINUX_KSTRTOX_H
  4. #include <linux/compiler.h>
  5. #include <linux/types.h>
  6. /* Internal, do not use. */
  7. int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
  8. int __must_check _kstrtol(const char *s, unsigned int base, long *res);
  9. int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
  10. int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
  11. /**
  12. * kstrtoul - convert a string to an unsigned long
  13. * @s: The start of the string. The string must be null-terminated, and may also
  14. * include a single newline before its terminating null. The first character
  15. * may also be a plus sign, but not a minus sign.
  16. * @base: The number base to use. The maximum supported base is 16. If base is
  17. * given as 0, then the base of the string is automatically detected with the
  18. * conventional semantics - If it begins with 0x the number will be parsed as a
  19. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  20. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  21. * @res: Where to write the result of the conversion on success.
  22. *
  23. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  24. * Preferred over simple_strtoul(). Return code must be checked.
  25. */
  26. static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
  27. {
  28. /*
  29. * We want to shortcut function call, but
  30. * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
  31. */
  32. if (sizeof(unsigned long) == sizeof(unsigned long long) &&
  33. __alignof__(unsigned long) == __alignof__(unsigned long long))
  34. return kstrtoull(s, base, (unsigned long long *)res);
  35. else
  36. return _kstrtoul(s, base, res);
  37. }
  38. /**
  39. * kstrtol - convert a string to a long
  40. * @s: The start of the string. The string must be null-terminated, and may also
  41. * include a single newline before its terminating null. The first character
  42. * may also be a plus sign or a minus sign.
  43. * @base: The number base to use. The maximum supported base is 16. If base is
  44. * given as 0, then the base of the string is automatically detected with the
  45. * conventional semantics - If it begins with 0x the number will be parsed as a
  46. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  47. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  48. * @res: Where to write the result of the conversion on success.
  49. *
  50. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  51. * Preferred over simple_strtol(). Return code must be checked.
  52. */
  53. static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
  54. {
  55. /*
  56. * We want to shortcut function call, but
  57. * __builtin_types_compatible_p(long, long long) = 0.
  58. */
  59. if (sizeof(long) == sizeof(long long) &&
  60. __alignof__(long) == __alignof__(long long))
  61. return kstrtoll(s, base, (long long *)res);
  62. else
  63. return _kstrtol(s, base, res);
  64. }
  65. int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
  66. int __must_check kstrtoint(const char *s, unsigned int base, int *res);
  67. static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
  68. {
  69. return kstrtoull(s, base, res);
  70. }
  71. static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
  72. {
  73. return kstrtoll(s, base, res);
  74. }
  75. static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
  76. {
  77. return kstrtouint(s, base, res);
  78. }
  79. static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
  80. {
  81. return kstrtoint(s, base, res);
  82. }
  83. int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
  84. int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
  85. int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
  86. int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
  87. int __must_check kstrtobool(const char *s, bool *res);
  88. int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
  89. int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
  90. int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
  91. int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
  92. int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
  93. int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
  94. int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
  95. int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
  96. int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
  97. int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
  98. int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
  99. static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
  100. {
  101. return kstrtoull_from_user(s, count, base, res);
  102. }
  103. static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
  104. {
  105. return kstrtoll_from_user(s, count, base, res);
  106. }
  107. static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
  108. {
  109. return kstrtouint_from_user(s, count, base, res);
  110. }
  111. static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
  112. {
  113. return kstrtoint_from_user(s, count, base, res);
  114. }
  115. /*
  116. * Use kstrto<foo> instead.
  117. *
  118. * NOTE: simple_strto<foo> does not check for the range overflow and,
  119. * depending on the input, may give interesting results.
  120. *
  121. * Use these functions if and only if you cannot use kstrto<foo>, because
  122. * the conversion ends on the first non-digit character, which may be far
  123. * beyond the supported range. It might be useful to parse the strings like
  124. * 10x50 or 12:21 without altering original string or temporary buffer in use.
  125. * Keep in mind above caveat.
  126. */
  127. extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  128. extern long simple_strtol(const char *,char **,unsigned int);
  129. extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
  130. extern long long simple_strtoll(const char *,char **,unsigned int);
  131. static inline int strtobool(const char *s, bool *res)
  132. {
  133. return kstrtobool(s, res);
  134. }
  135. #endif /* _LINUX_KSTRTOX_H */