]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/kstrtox.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / lib / kstrtox.c
CommitLineData
33ee3b2e
AD
1/*
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>
8bc3bcc9 18#include <linux/export.h>
33ee3b2e 19#include <linux/types.h>
7c0f6ba6 20#include <linux/uaccess.h>
1dff46d6 21#include "kstrtox.h"
33ee3b2e 22
1dff46d6 23const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
33ee3b2e 24{
1dff46d6 25 if (*base == 0) {
33ee3b2e
AD
26 if (s[0] == '0') {
27 if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
1dff46d6 28 *base = 16;
33ee3b2e 29 else
1dff46d6 30 *base = 8;
33ee3b2e 31 } else
1dff46d6 32 *base = 10;
33ee3b2e 33 }
1dff46d6 34 if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
33ee3b2e 35 s += 2;
1dff46d6
AD
36 return s;
37}
38
39/*
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 */
690d137f 47unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
1dff46d6 48{
690d137f 49 unsigned long long res;
1dff46d6 50 unsigned int rv;
33ee3b2e 51
690d137f 52 res = 0;
1dff46d6 53 rv = 0;
512750ef 54 while (1) {
be5f3c77
AD
55 unsigned int c = *s;
56 unsigned int lc = c | 0x20; /* don't tolower() this line */
33ee3b2e
AD
57 unsigned int val;
58
be5f3c77
AD
59 if ('0' <= c && c <= '9')
60 val = c - '0';
61 else if ('a' <= lc && lc <= 'f')
62 val = lc - 'a' + 10;
78be959e 63 else
1dff46d6 64 break;
33ee3b2e
AD
65
66 if (val >= base)
1dff46d6 67 break;
690d137f
DH
68 /*
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))
8cfd56d4 74 rv |= KSTRTOX_OVERFLOW;
690d137f
DH
75 }
76 res = res * base + val;
1dff46d6 77 rv++;
33ee3b2e
AD
78 s++;
79 }
690d137f 80 *p = res;
1dff46d6
AD
81 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;
1dff46d6
AD
93 if (rv == 0)
94 return -EINVAL;
95 s += rv;
96 if (*s == '\n')
97 s++;
98 if (*s)
33ee3b2e 99 return -EINVAL;
1dff46d6 100 *res = _res;
33ee3b2e
AD
101 return 0;
102}
103
4c925d60
EZ
104/**
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 */
33ee3b2e
AD
120int 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
4c925d60
EZ
128/**
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 */
33ee3b2e
AD
144int 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;
2d2e4715 153 if ((long long)-tmp > 0)
33ee3b2e
AD
154 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
4c925d60
EZ
200/**
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 */
33ee3b2e
AD
216int 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
4c925d60
EZ
231/**
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 */
33ee3b2e
AD
247int 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);
c196e32a 321
ef951599
KC
322/**
323 * kstrtobool - convert common user inputs into boolean values
324 * @s: input string
325 * @res: result
326 *
a81a5a17
KC
327 * 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.
ef951599
KC
330 */
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;
a81a5a17
KC
347 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 }
ef951599
KC
361 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
c196e32a
AD
386#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);