2 * Convert integer string representation to an integer.
3 * If an integer doesn't fit into specified type, -E is returned.
5 * Integer starts with optional sign.
6 * kstrtou*() functions do not accept sign "-".
8 * Radix 0 means autodetection: leading "0x" implies radix 16,
9 * leading "0" implies radix 8, otherwise radix is 10.
10 * Autodetection hints work after optional sign, but not before.
12 * If -E is returned, result is not touched.
14 #include <linux/ctype.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <asm/uaccess.h>
22 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
24 unsigned long long acc;
29 if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
36 if (base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
44 if ('0' <= *s && *s <= '9')
46 else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
47 val = _tolower(*s) - 'a' + 10;
48 else if (*s == '\n' && *(s + 1) == '\0')
55 if (acc > div_u64(ULLONG_MAX - val, base))
57 acc = acc * base + val;
68 int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
72 return _kstrtoull(s, base, res);
74 EXPORT_SYMBOL(kstrtoull);
76 int kstrtoll(const char *s, unsigned int base, long long *res)
78 unsigned long long tmp;
82 rv = _kstrtoull(s + 1, base, &tmp);
85 if ((long long)(-tmp) >= 0)
89 rv = kstrtoull(s, base, &tmp);
92 if ((long long)tmp < 0)
98 EXPORT_SYMBOL(kstrtoll);
100 /* Internal, do not use. */
101 int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
103 unsigned long long tmp;
106 rv = kstrtoull(s, base, &tmp);
109 if (tmp != (unsigned long long)(unsigned long)tmp)
114 EXPORT_SYMBOL(_kstrtoul);
116 /* Internal, do not use. */
117 int _kstrtol(const char *s, unsigned int base, long *res)
122 rv = kstrtoll(s, base, &tmp);
125 if (tmp != (long long)(long)tmp)
130 EXPORT_SYMBOL(_kstrtol);
132 int kstrtouint(const char *s, unsigned int base, unsigned int *res)
134 unsigned long long tmp;
137 rv = kstrtoull(s, base, &tmp);
140 if (tmp != (unsigned long long)(unsigned int)tmp)
145 EXPORT_SYMBOL(kstrtouint);
147 int kstrtoint(const char *s, unsigned int base, int *res)
152 rv = kstrtoll(s, base, &tmp);
155 if (tmp != (long long)(int)tmp)
160 EXPORT_SYMBOL(kstrtoint);
162 int kstrtou16(const char *s, unsigned int base, u16 *res)
164 unsigned long long tmp;
167 rv = kstrtoull(s, base, &tmp);
170 if (tmp != (unsigned long long)(u16)tmp)
175 EXPORT_SYMBOL(kstrtou16);
177 int kstrtos16(const char *s, unsigned int base, s16 *res)
182 rv = kstrtoll(s, base, &tmp);
185 if (tmp != (long long)(s16)tmp)
190 EXPORT_SYMBOL(kstrtos16);
192 int kstrtou8(const char *s, unsigned int base, u8 *res)
194 unsigned long long tmp;
197 rv = kstrtoull(s, base, &tmp);
200 if (tmp != (unsigned long long)(u8)tmp)
205 EXPORT_SYMBOL(kstrtou8);
207 int kstrtos8(const char *s, unsigned int base, s8 *res)
212 rv = kstrtoll(s, base, &tmp);
215 if (tmp != (long long)(s8)tmp)
220 EXPORT_SYMBOL(kstrtos8);
222 #define kstrto_from_user(f, g, type) \
223 int f(const char __user *s, size_t count, unsigned int base, type *res) \
225 /* sign, base 2 representation, newline, terminator */ \
226 char buf[1 + sizeof(type) * 8 + 1 + 1]; \
228 count = min(count, sizeof(buf) - 1); \
229 if (copy_from_user(buf, s, count)) \
232 return g(buf, base, res); \
236 kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
237 kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
238 kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
239 kstrto_from_user(kstrtol_from_user, kstrtol, long);
240 kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
241 kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
242 kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
243 kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
244 kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
245 kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);