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 inline char _tolower(const char c
)
27 static int _kstrtoull(const char *s
, unsigned int base
, unsigned long long *res
)
29 unsigned long long acc
;
34 if (_tolower(s
[1]) == 'x' && isxdigit(s
[2]))
41 if (base
== 16 && s
[0] == '0' && _tolower(s
[1]) == 'x')
49 if ('0' <= *s
&& *s
<= '9')
51 else if ('a' <= _tolower(*s
) && _tolower(*s
) <= 'f')
52 val
= _tolower(*s
) - 'a' + 10;
53 else if (*s
== '\n' && *(s
+ 1) == '\0')
60 if (acc
> div_u64(ULLONG_MAX
- val
, base
))
62 acc
= acc
* base
+ val
;
73 int kstrtoull(const char *s
, unsigned int base
, unsigned long long *res
)
77 return _kstrtoull(s
, base
, res
);
79 EXPORT_SYMBOL(kstrtoull
);
81 int kstrtoll(const char *s
, unsigned int base
, long long *res
)
83 unsigned long long tmp
;
87 rv
= _kstrtoull(s
+ 1, base
, &tmp
);
90 if ((long long)(-tmp
) >= 0)
94 rv
= kstrtoull(s
, base
, &tmp
);
97 if ((long long)tmp
< 0)
103 EXPORT_SYMBOL(kstrtoll
);
105 /* Internal, do not use. */
106 int _kstrtoul(const char *s
, unsigned int base
, unsigned long *res
)
108 unsigned long long tmp
;
111 rv
= kstrtoull(s
, base
, &tmp
);
114 if (tmp
!= (unsigned long long)(unsigned long)tmp
)
119 EXPORT_SYMBOL(_kstrtoul
);
121 /* Internal, do not use. */
122 int _kstrtol(const char *s
, unsigned int base
, long *res
)
127 rv
= kstrtoll(s
, base
, &tmp
);
130 if (tmp
!= (long long)(long)tmp
)
135 EXPORT_SYMBOL(_kstrtol
);
137 int kstrtouint(const char *s
, unsigned int base
, unsigned int *res
)
139 unsigned long long tmp
;
142 rv
= kstrtoull(s
, base
, &tmp
);
145 if (tmp
!= (unsigned long long)(unsigned int)tmp
)
150 EXPORT_SYMBOL(kstrtouint
);
152 int kstrtoint(const char *s
, unsigned int base
, int *res
)
157 rv
= kstrtoll(s
, base
, &tmp
);
160 if (tmp
!= (long long)(int)tmp
)
165 EXPORT_SYMBOL(kstrtoint
);
167 int kstrtou16(const char *s
, unsigned int base
, u16
*res
)
169 unsigned long long tmp
;
172 rv
= kstrtoull(s
, base
, &tmp
);
175 if (tmp
!= (unsigned long long)(u16
)tmp
)
180 EXPORT_SYMBOL(kstrtou16
);
182 int kstrtos16(const char *s
, unsigned int base
, s16
*res
)
187 rv
= kstrtoll(s
, base
, &tmp
);
190 if (tmp
!= (long long)(s16
)tmp
)
195 EXPORT_SYMBOL(kstrtos16
);
197 int kstrtou8(const char *s
, unsigned int base
, u8
*res
)
199 unsigned long long tmp
;
202 rv
= kstrtoull(s
, base
, &tmp
);
205 if (tmp
!= (unsigned long long)(u8
)tmp
)
210 EXPORT_SYMBOL(kstrtou8
);
212 int kstrtos8(const char *s
, unsigned int base
, s8
*res
)
217 rv
= kstrtoll(s
, base
, &tmp
);
220 if (tmp
!= (long long)(s8
)tmp
)
225 EXPORT_SYMBOL(kstrtos8
);
227 #define kstrto_from_user(f, g, type) \
228 int f(const char __user *s, size_t count, unsigned int base, type *res) \
230 /* sign, base 2 representation, newline, terminator */ \
231 char buf[1 + sizeof(type) * 8 + 1 + 1]; \
233 count = min(count, sizeof(buf) - 1); \
234 if (copy_from_user(buf, s, count)) \
237 return g(buf, base, res); \
241 kstrto_from_user(kstrtoull_from_user
, kstrtoull
, unsigned long long);
242 kstrto_from_user(kstrtoll_from_user
, kstrtoll
, long long);
243 kstrto_from_user(kstrtoul_from_user
, kstrtoul
, unsigned long);
244 kstrto_from_user(kstrtol_from_user
, kstrtol
, long);
245 kstrto_from_user(kstrtouint_from_user
, kstrtouint
, unsigned int);
246 kstrto_from_user(kstrtoint_from_user
, kstrtoint
, int);
247 kstrto_from_user(kstrtou16_from_user
, kstrtou16
, u16
);
248 kstrto_from_user(kstrtos16_from_user
, kstrtos16
, s16
);
249 kstrto_from_user(kstrtou8_from_user
, kstrtou8
, u8
);
250 kstrto_from_user(kstrtos8_from_user
, kstrtos8
, s8
);