xref: /openbmc/linux/lib/kstrtox.c (revision e6dec923)
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>
18 #include <linux/export.h>
19 #include <linux/types.h>
20 #include <linux/uaccess.h>
21 #include "kstrtox.h"
22 
23 const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
24 {
25 	if (*base == 0) {
26 		if (s[0] == '0') {
27 			if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
28 				*base = 16;
29 			else
30 				*base = 8;
31 		} else
32 			*base = 10;
33 	}
34 	if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
35 		s += 2;
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  */
47 unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
48 {
49 	unsigned long long res;
50 	unsigned int rv;
51 
52 	res = 0;
53 	rv = 0;
54 	while (1) {
55 		unsigned int c = *s;
56 		unsigned int lc = c | 0x20; /* don't tolower() this line */
57 		unsigned int val;
58 
59 		if ('0' <= c && c <= '9')
60 			val = c - '0';
61 		else if ('a' <= lc && lc <= 'f')
62 			val = lc - 'a' + 10;
63 		else
64 			break;
65 
66 		if (val >= base)
67 			break;
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))
74 				rv |= KSTRTOX_OVERFLOW;
75 		}
76 		res = res * base + val;
77 		rv++;
78 		s++;
79 	}
80 	*p = res;
81 	return rv;
82 }
83 
84 static 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;
93 	if (rv == 0)
94 		return -EINVAL;
95 	s += rv;
96 	if (*s == '\n')
97 		s++;
98 	if (*s)
99 		return -EINVAL;
100 	*res = _res;
101 	return 0;
102 }
103 
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  */
120 int 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 }
126 EXPORT_SYMBOL(kstrtoull);
127 
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  */
144 int 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;
153 		if ((long long)-tmp > 0)
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 }
166 EXPORT_SYMBOL(kstrtoll);
167 
168 /* Internal, do not use. */
169 int _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 }
182 EXPORT_SYMBOL(_kstrtoul);
183 
184 /* Internal, do not use. */
185 int _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 }
198 EXPORT_SYMBOL(_kstrtol);
199 
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  */
216 int 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 }
229 EXPORT_SYMBOL(kstrtouint);
230 
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  */
247 int 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 }
260 EXPORT_SYMBOL(kstrtoint);
261 
262 int 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 }
275 EXPORT_SYMBOL(kstrtou16);
276 
277 int 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 }
290 EXPORT_SYMBOL(kstrtos16);
291 
292 int 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 }
305 EXPORT_SYMBOL(kstrtou8);
306 
307 int 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 }
320 EXPORT_SYMBOL(kstrtos8);
321 
322 /**
323  * kstrtobool - convert common user inputs into boolean values
324  * @s: input string
325  * @res: result
326  *
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.
330  */
331 int 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;
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 		}
361 	default:
362 		break;
363 	}
364 
365 	return -EINVAL;
366 }
367 EXPORT_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  */
373 int 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 }
384 EXPORT_SYMBOL(kstrtobool_from_user);
385 
386 #define kstrto_from_user(f, g, type)					\
387 int 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 }									\
398 EXPORT_SYMBOL(f)
399 
400 kstrto_from_user(kstrtoull_from_user,	kstrtoull,	unsigned long long);
401 kstrto_from_user(kstrtoll_from_user,	kstrtoll,	long long);
402 kstrto_from_user(kstrtoul_from_user,	kstrtoul,	unsigned long);
403 kstrto_from_user(kstrtol_from_user,	kstrtol,	long);
404 kstrto_from_user(kstrtouint_from_user,	kstrtouint,	unsigned int);
405 kstrto_from_user(kstrtoint_from_user,	kstrtoint,	int);
406 kstrto_from_user(kstrtou16_from_user,	kstrtou16,	u16);
407 kstrto_from_user(kstrtos16_from_user,	kstrtos16,	s16);
408 kstrto_from_user(kstrtou8_from_user,	kstrtou8,	u8);
409 kstrto_from_user(kstrtos8_from_user,	kstrtos8,	s8);
410