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>
21 static inline char _tolower(const char c)
26 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
28 unsigned long long acc;
33 if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
40 if (base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
48 if ('0' <= *s && *s <= '9')
50 else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
51 val = _tolower(*s) - 'a' + 10;
52 else if (*s == '\n') {
62 if (acc > div_u64(ULLONG_MAX - val, base))
64 acc = acc * base + val;
75 int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
79 return _kstrtoull(s, base, res);
81 EXPORT_SYMBOL(kstrtoull);
83 int kstrtoll(const char *s, unsigned int base, long long *res)
85 unsigned long long tmp;
89 rv = _kstrtoull(s + 1, base, &tmp);
92 if ((long long)(-tmp) >= 0)
96 rv = kstrtoull(s, base, &tmp);
99 if ((long long)tmp < 0)
105 EXPORT_SYMBOL(kstrtoll);
107 /* Internal, do not use. */
108 int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
110 unsigned long long tmp;
113 rv = kstrtoull(s, base, &tmp);
116 if (tmp != (unsigned long long)(unsigned long)tmp)
121 EXPORT_SYMBOL(_kstrtoul);
123 /* Internal, do not use. */
124 int _kstrtol(const char *s, unsigned int base, long *res)
129 rv = kstrtoll(s, base, &tmp);
132 if (tmp != (long long)(long)tmp)
137 EXPORT_SYMBOL(_kstrtol);
139 int kstrtouint(const char *s, unsigned int base, unsigned int *res)
141 unsigned long long tmp;
144 rv = kstrtoull(s, base, &tmp);
147 if (tmp != (unsigned long long)(unsigned int)tmp)
152 EXPORT_SYMBOL(kstrtouint);
154 int kstrtoint(const char *s, unsigned int base, int *res)
159 rv = kstrtoll(s, base, &tmp);
162 if (tmp != (long long)(int)tmp)
167 EXPORT_SYMBOL(kstrtoint);
169 int kstrtou16(const char *s, unsigned int base, u16 *res)
171 unsigned long long tmp;
174 rv = kstrtoull(s, base, &tmp);
177 if (tmp != (unsigned long long)(u16)tmp)
182 EXPORT_SYMBOL(kstrtou16);
184 int kstrtos16(const char *s, unsigned int base, s16 *res)
189 rv = kstrtoll(s, base, &tmp);
192 if (tmp != (long long)(s16)tmp)
197 EXPORT_SYMBOL(kstrtos16);
199 int kstrtou8(const char *s, unsigned int base, u8 *res)
201 unsigned long long tmp;
204 rv = kstrtoull(s, base, &tmp);
207 if (tmp != (unsigned long long)(u8)tmp)
212 EXPORT_SYMBOL(kstrtou8);
214 int kstrtos8(const char *s, unsigned int base, s8 *res)
219 rv = kstrtoll(s, base, &tmp);
222 if (tmp != (long long)(s8)tmp)
227 EXPORT_SYMBOL(kstrtos8);