1 | /* SPDX-License-Identifier: GPL-2.0 */ |
---|---|

2 | #ifndef _LINUX_KSTRTOX_H |

3 | #define _LINUX_KSTRTOX_H |

4 | |

5 | #include <linux/compiler.h> |

6 | #include <linux/types.h> |

7 | |

8 | /* Internal, do not use. */ |

9 | int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); |

10 | int __must_check _kstrtol(const char *s, unsigned int base, long *res); |

11 | |

12 | int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); |

13 | int __must_check kstrtoll(const char *s, unsigned int base, long long *res); |

14 | |

15 | /** |

16 | * kstrtoul - convert a string to an unsigned long |

17 | * @s: The start of the string. The string must be null-terminated, and may also |

18 | * include a single newline before its terminating null. The first character |

19 | * may also be a plus sign, but not a minus sign. |

20 | * @base: The number base to use. The maximum supported base is 16. If base is |

21 | * given as 0, then the base of the string is automatically detected with the |

22 | * conventional semantics - If it begins with 0x the number will be parsed as a |

23 | * hexadecimal (case insensitive), if it otherwise begins with 0, it will be |

24 | * parsed as an octal number. Otherwise it will be parsed as a decimal. |

25 | * @res: Where to write the result of the conversion on success. |

26 | * |

27 | * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. |

28 | * Preferred over simple_strtoul(). Return code must be checked. |

29 | */ |

30 | static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) |

31 | { |

32 | /* |

33 | * We want to shortcut function call, but |

34 | * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. |

35 | */ |

36 | if (sizeof(unsigned long) == sizeof(unsigned long long) && |

37 | __alignof__(unsigned long) == __alignof__(unsigned long long)) |

38 | return kstrtoull(s, base, res: (unsigned long long *)res); |

39 | else |

40 | return _kstrtoul(s, base, res); |

41 | } |

42 | |

43 | /** |

44 | * kstrtol - convert a string to a long |

45 | * @s: The start of the string. The string must be null-terminated, and may also |

46 | * include a single newline before its terminating null. The first character |

47 | * may also be a plus sign or a minus sign. |

48 | * @base: The number base to use. The maximum supported base is 16. If base is |

49 | * given as 0, then the base of the string is automatically detected with the |

50 | * conventional semantics - If it begins with 0x the number will be parsed as a |

51 | * hexadecimal (case insensitive), if it otherwise begins with 0, it will be |

52 | * parsed as an octal number. Otherwise it will be parsed as a decimal. |

53 | * @res: Where to write the result of the conversion on success. |

54 | * |

55 | * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. |

56 | * Preferred over simple_strtol(). Return code must be checked. |

57 | */ |

58 | static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) |

59 | { |

60 | /* |

61 | * We want to shortcut function call, but |

62 | * __builtin_types_compatible_p(long, long long) = 0. |

63 | */ |

64 | if (sizeof(long) == sizeof(long long) && |

65 | __alignof__(long) == __alignof__(long long)) |

66 | return kstrtoll(s, base, res: (long long *)res); |

67 | else |

68 | return _kstrtol(s, base, res); |

69 | } |

70 | |

71 | int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); |

72 | int __must_check kstrtoint(const char *s, unsigned int base, int *res); |

73 | |

74 | static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) |

75 | { |

76 | return kstrtoull(s, base, res); |

77 | } |

78 | |

79 | static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) |

80 | { |

81 | return kstrtoll(s, base, res); |

82 | } |

83 | |

84 | static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) |

85 | { |

86 | return kstrtouint(s, base, res); |

87 | } |

88 | |

89 | static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) |

90 | { |

91 | return kstrtoint(s, base, res); |

92 | } |

93 | |

94 | int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); |

95 | int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); |

96 | int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); |

97 | int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); |

98 | int __must_check kstrtobool(const char *s, bool *res); |

99 | |

100 | int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); |

101 | int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); |

102 | int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); |

103 | int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); |

104 | int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); |

105 | int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); |

106 | int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); |

107 | int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); |

108 | int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); |

109 | int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); |

110 | int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); |

111 | |

112 | static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) |

113 | { |

114 | return kstrtoull_from_user(s, count, base, res); |

115 | } |

116 | |

117 | static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) |

118 | { |

119 | return kstrtoll_from_user(s, count, base, res); |

120 | } |

121 | |

122 | static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) |

123 | { |

124 | return kstrtouint_from_user(s, count, base, res); |

125 | } |

126 | |

127 | static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) |

128 | { |

129 | return kstrtoint_from_user(s, count, base, res); |

130 | } |

131 | |

132 | /* |

133 | * Use kstrto<foo> instead. |

134 | * |

135 | * NOTE: simple_strto<foo> does not check for the range overflow and, |

136 | * depending on the input, may give interesting results. |

137 | * |

138 | * Use these functions if and only if you cannot use kstrto<foo>, because |

139 | * the conversion ends on the first non-digit character, which may be far |

140 | * beyond the supported range. It might be useful to parse the strings like |

141 | * 10x50 or 12:21 without altering original string or temporary buffer in use. |

142 | * Keep in mind above caveat. |

143 | */ |

144 | |

145 | extern unsigned long simple_strtoul(const char *,char **,unsigned int); |

146 | extern long simple_strtol(const char *,char **,unsigned int); |

147 | extern unsigned long long simple_strtoull(const char *,char **,unsigned int); |

148 | extern long long simple_strtoll(const char *,char **,unsigned int); |

149 | |

150 | #endif /* _LINUX_KSTRTOX_H */ |

151 |