blob: 720144075c1ea06b659ab7a15512ca6b3a1bba24 [file] [log] [blame]
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -07001/*
2 * Convert integer string representation to an integer.
3 * If an integer doesn't fit into specified type, -E is returned.
4 *
5 * Integer starts with optional sign.
6 * kstrtou*() functions do not accept sign "-".
7 *
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.
11 *
12 * If -E is returned, result is not touched.
13 */
14#include <linux/ctype.h>
15#include <linux/errno.h>
16#include <linux/kernel.h>
17#include <linux/math64.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050018#include <linux/export.h>
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070019#include <linux/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070021#include "kstrtox.h"
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070022
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070023const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070024{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070025 if (*base == 0) {
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070026 if (s[0] == '0') {
27 if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070028 *base = 16;
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070029 else
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070030 *base = 8;
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070031 } else
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070032 *base = 10;
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070033 }
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070034 if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070035 s += 2;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070036 return s;
37}
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070038
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070039/*
40 * Convert non-negative integer string representation in explicitly given radix
41 * to an integer.
42 * Return number of characters consumed maybe or-ed with overflow bit.
43 * If overflow occurs, result integer (incorrect) is still returned.
44 *
45 * Don't you dare use this function.
46 */
David Howells690d1372012-02-09 15:48:20 +000047unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070048{
David Howells690d1372012-02-09 15:48:20 +000049 unsigned long long res;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070050 unsigned int rv;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070051
David Howells690d1372012-02-09 15:48:20 +000052 res = 0;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070053 rv = 0;
Alexey Dobriyan512750e2017-07-10 15:51:38 -070054 while (1) {
Alexey Dobriyanbe5f3c72017-07-10 15:51:41 -070055 unsigned int c = *s;
56 unsigned int lc = c | 0x20; /* don't tolower() this line */
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070057 unsigned int val;
58
Alexey Dobriyanbe5f3c72017-07-10 15:51:41 -070059 if ('0' <= c && c <= '9')
60 val = c - '0';
61 else if ('a' <= lc && lc <= 'f')
62 val = lc - 'a' + 10;
Alexey Dobriyan78be9592011-04-14 15:22:02 -070063 else
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070064 break;
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070065
66 if (val >= base)
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070067 break;
David Howells690d1372012-02-09 15:48:20 +000068 /*
69 * Check for overflow only if we are within range of
70 * it in the max base we support (16)
71 */
72 if (unlikely(res & (~0ull << 60))) {
73 if (res > div_u64(ULLONG_MAX - val, base))
Alexey Dobriyan8cfd56d2016-10-11 13:51:32 -070074 rv |= KSTRTOX_OVERFLOW;
David Howells690d1372012-02-09 15:48:20 +000075 }
76 res = res * base + val;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070077 rv++;
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070078 s++;
79 }
David Howells690d1372012-02-09 15:48:20 +000080 *p = res;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070081 return rv;
82}
83
84static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
85{
86 unsigned long long _res;
87 unsigned int rv;
88
89 s = _parse_integer_fixup_radix(s, &base);
90 rv = _parse_integer(s, base, &_res);
91 if (rv & KSTRTOX_OVERFLOW)
92 return -ERANGE;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070093 if (rv == 0)
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -070094 return -EINVAL;
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070095 s += rv;
96 if (*s == '\n')
97 s++;
98 if (*s)
99 return -EINVAL;
100 *res = _res;
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -0700101 return 0;
102}
103
Eldad Zack4c925d62012-12-17 16:03:04 -0800104/**
105 * kstrtoull - convert a string to an unsigned long long
106 * @s: The start of the string. The string must be null-terminated, and may also
107 * include a single newline before its terminating null. The first character
108 * may also be a plus sign, but not a minus sign.
109 * @base: The number base to use. The maximum supported base is 16. If base is
110 * given as 0, then the base of the string is automatically detected with the
111 * conventional semantics - If it begins with 0x the number will be parsed as a
112 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
113 * parsed as an octal number. Otherwise it will be parsed as a decimal.
114 * @res: Where to write the result of the conversion on success.
115 *
116 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
117 * Used as a replacement for the obsolete simple_strtoull. Return code must
118 * be checked.
119 */
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -0700120int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
121{
122 if (s[0] == '+')
123 s++;
124 return _kstrtoull(s, base, res);
125}
126EXPORT_SYMBOL(kstrtoull);
127
Eldad Zack4c925d62012-12-17 16:03:04 -0800128/**
129 * kstrtoll - convert a string to a long long
130 * @s: The start of the string. The string must be null-terminated, and may also
131 * include a single newline before its terminating null. The first character
132 * may also be a plus sign or a minus sign.
133 * @base: The number base to use. The maximum supported base is 16. If base is
134 * given as 0, then the base of the string is automatically detected with the
135 * conventional semantics - If it begins with 0x the number will be parsed as a
136 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
137 * parsed as an octal number. Otherwise it will be parsed as a decimal.
138 * @res: Where to write the result of the conversion on success.
139 *
140 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
141 * Used as a replacement for the obsolete simple_strtoull. Return code must
142 * be checked.
143 */
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -0700144int kstrtoll(const char *s, unsigned int base, long long *res)
145{
146 unsigned long long tmp;
147 int rv;
148
149 if (s[0] == '-') {
150 rv = _kstrtoull(s + 1, base, &tmp);
151 if (rv < 0)
152 return rv;
Alexey Dobriyan2d2e4712015-09-09 15:36:17 -0700153 if ((long long)-tmp > 0)
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -0700154 return -ERANGE;
155 *res = -tmp;
156 } else {
157 rv = kstrtoull(s, base, &tmp);
158 if (rv < 0)
159 return rv;
160 if ((long long)tmp < 0)
161 return -ERANGE;
162 *res = tmp;
163 }
164 return 0;
165}
166EXPORT_SYMBOL(kstrtoll);
167
168/* Internal, do not use. */
169int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
170{
171 unsigned long long tmp;
172 int rv;
173
174 rv = kstrtoull(s, base, &tmp);
175 if (rv < 0)
176 return rv;
177 if (tmp != (unsigned long long)(unsigned long)tmp)
178 return -ERANGE;
179 *res = tmp;
180 return 0;
181}
182EXPORT_SYMBOL(_kstrtoul);
183
184/* Internal, do not use. */
185int _kstrtol(const char *s, unsigned int base, long *res)
186{
187 long long tmp;
188 int rv;
189
190 rv = kstrtoll(s, base, &tmp);
191 if (rv < 0)
192 return rv;
193 if (tmp != (long long)(long)tmp)
194 return -ERANGE;
195 *res = tmp;
196 return 0;
197}
198EXPORT_SYMBOL(_kstrtol);
199
Eldad Zack4c925d62012-12-17 16:03:04 -0800200/**
201 * kstrtouint - convert a string to an unsigned int
202 * @s: The start of the string. The string must be null-terminated, and may also
203 * include a single newline before its terminating null. The first character
204 * may also be a plus sign, but not a minus sign.
205 * @base: The number base to use. The maximum supported base is 16. If base is
206 * given as 0, then the base of the string is automatically detected with the
207 * conventional semantics - If it begins with 0x the number will be parsed as a
208 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
209 * parsed as an octal number. Otherwise it will be parsed as a decimal.
210 * @res: Where to write the result of the conversion on success.
211 *
212 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
213 * Used as a replacement for the obsolete simple_strtoull. Return code must
214 * be checked.
215 */
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -0700216int kstrtouint(const char *s, unsigned int base, unsigned int *res)
217{
218 unsigned long long tmp;
219 int rv;
220
221 rv = kstrtoull(s, base, &tmp);
222 if (rv < 0)
223 return rv;
224 if (tmp != (unsigned long long)(unsigned int)tmp)
225 return -ERANGE;
226 *res = tmp;
227 return 0;
228}
229EXPORT_SYMBOL(kstrtouint);
230
Eldad Zack4c925d62012-12-17 16:03:04 -0800231/**
232 * kstrtoint - convert a string to an int
233 * @s: The start of the string. The string must be null-terminated, and may also
234 * include a single newline before its terminating null. The first character
235 * may also be a plus sign or a minus sign.
236 * @base: The number base to use. The maximum supported base is 16. If base is
237 * given as 0, then the base of the string is automatically detected with the
238 * conventional semantics - If it begins with 0x the number will be parsed as a
239 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
240 * parsed as an octal number. Otherwise it will be parsed as a decimal.
241 * @res: Where to write the result of the conversion on success.
242 *
243 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
244 * Used as a replacement for the obsolete simple_strtoull. Return code must
245 * be checked.
246 */
Alexey Dobriyan33ee3b22011-03-22 16:34:40 -0700247int kstrtoint(const char *s, unsigned int base, int *res)
248{
249 long long tmp;
250 int rv;
251
252 rv = kstrtoll(s, base, &tmp);
253 if (rv < 0)
254 return rv;
255 if (tmp != (long long)(int)tmp)
256 return -ERANGE;
257 *res = tmp;
258 return 0;
259}
260EXPORT_SYMBOL(kstrtoint);
261
262int kstrtou16(const char *s, unsigned int base, u16 *res)
263{
264 unsigned long long tmp;
265 int rv;
266
267 rv = kstrtoull(s, base, &tmp);
268 if (rv < 0)
269 return rv;
270 if (tmp != (unsigned long long)(u16)tmp)
271 return -ERANGE;
272 *res = tmp;
273 return 0;
274}
275EXPORT_SYMBOL(kstrtou16);
276
277int kstrtos16(const char *s, unsigned int base, s16 *res)
278{
279 long long tmp;
280 int rv;
281
282 rv = kstrtoll(s, base, &tmp);
283 if (rv < 0)
284 return rv;
285 if (tmp != (long long)(s16)tmp)
286 return -ERANGE;
287 *res = tmp;
288 return 0;
289}
290EXPORT_SYMBOL(kstrtos16);
291
292int kstrtou8(const char *s, unsigned int base, u8 *res)
293{
294 unsigned long long tmp;
295 int rv;
296
297 rv = kstrtoull(s, base, &tmp);
298 if (rv < 0)
299 return rv;
300 if (tmp != (unsigned long long)(u8)tmp)
301 return -ERANGE;
302 *res = tmp;
303 return 0;
304}
305EXPORT_SYMBOL(kstrtou8);
306
307int kstrtos8(const char *s, unsigned int base, s8 *res)
308{
309 long long tmp;
310 int rv;
311
312 rv = kstrtoll(s, base, &tmp);
313 if (rv < 0)
314 return rv;
315 if (tmp != (long long)(s8)tmp)
316 return -ERANGE;
317 *res = tmp;
318 return 0;
319}
320EXPORT_SYMBOL(kstrtos8);
Alexey Dobriyanc196e322011-05-24 17:13:31 -0700321
Kees Cookef951592016-03-17 14:22:50 -0700322/**
323 * kstrtobool - convert common user inputs into boolean values
324 * @s: input string
325 * @res: result
326 *
Kees Cooka81a5a12016-03-17 14:22:57 -0700327 * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
328 * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
329 * pointed to by res is updated upon finding a match.
Kees Cookef951592016-03-17 14:22:50 -0700330 */
331int kstrtobool(const char *s, bool *res)
332{
333 if (!s)
334 return -EINVAL;
335
336 switch (s[0]) {
337 case 'y':
338 case 'Y':
339 case '1':
340 *res = true;
341 return 0;
342 case 'n':
343 case 'N':
344 case '0':
345 *res = false;
346 return 0;
Kees Cooka81a5a12016-03-17 14:22:57 -0700347 case 'o':
348 case 'O':
349 switch (s[1]) {
350 case 'n':
351 case 'N':
352 *res = true;
353 return 0;
354 case 'f':
355 case 'F':
356 *res = false;
357 return 0;
358 default:
359 break;
360 }
Kees Cookef951592016-03-17 14:22:50 -0700361 default:
362 break;
363 }
364
365 return -EINVAL;
366}
367EXPORT_SYMBOL(kstrtobool);
368
369/*
370 * Since "base" would be a nonsense argument, this open-codes the
371 * _from_user helper instead of using the helper macro below.
372 */
373int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
374{
375 /* Longest string needed to differentiate, newline, terminator */
376 char buf[4];
377
378 count = min(count, sizeof(buf) - 1);
379 if (copy_from_user(buf, s, count))
380 return -EFAULT;
381 buf[count] = '\0';
382 return kstrtobool(buf, res);
383}
384EXPORT_SYMBOL(kstrtobool_from_user);
385
Alexey Dobriyanc196e322011-05-24 17:13:31 -0700386#define kstrto_from_user(f, g, type) \
387int f(const char __user *s, size_t count, unsigned int base, type *res) \
388{ \
389 /* sign, base 2 representation, newline, terminator */ \
390 char buf[1 + sizeof(type) * 8 + 1 + 1]; \
391 \
392 count = min(count, sizeof(buf) - 1); \
393 if (copy_from_user(buf, s, count)) \
394 return -EFAULT; \
395 buf[count] = '\0'; \
396 return g(buf, base, res); \
397} \
398EXPORT_SYMBOL(f)
399
400kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
401kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
402kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
403kstrto_from_user(kstrtol_from_user, kstrtol, long);
404kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
405kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
406kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
407kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
408kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
409kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);