Something went wrong. Try again.
Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443// SPDX-License-Identifier: GPL-2.0/* * Convert integer string representation to an integer. * If an integer doesn't fit into specified type, -E is returned. * * Integer starts with optional sign. * kstrtou*() functions do not accept sign "-". * * Radix 0 means autodetection: leading "0x" implies radix 16, * leading "0" implies radix 8, otherwise radix is 10. * Autodetection hints work after optional sign, but not before. * * If -E is returned, result is not touched. */#include <linux/ctype.h>#include <linux/errno.h>#include <linux/export.h>#include <linux/kstrtox.h>#include <linux/math64.h>#include <linux/types.h>#include <linux/uaccess.h>
#include "kstrtox.h"
noinlineconst char *_parse_integer_fixup_radix(const char *s, unsigned int *base){ if (*base == 0) { if (s[0] == '0') { if (_tolower(s[1]) == 'x' && isxdigit(s[2])) *base = 16; else *base = 8; } else *base = 10; } if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') s += 2; return s;}
/** * _parse_integer_limit - Convert integer string representation to an integer * @s: Integer string representation * @base: Radix * @p: Where to store result * @max_chars: Maximum amount of characters to convert * * Convert non-negative integer string representation in explicitly given * radix to an integer. If overflow occurs, value at @p is set to ULLONG_MAX. * * This function is the workhorse of other string conversion functions and it * is discouraged to use it explicitly. Consider kstrto*() family instead. * * Return: Number of characters consumed, maybe ORed with overflow bit */noinlineunsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p, size_t max_chars){ unsigned int rv, overflow = 0; unsigned long long res;
res = 0; for (rv = 0; rv < max_chars; rv++, s++) { unsigned int c = *s; unsigned int lc = _tolower(c); unsigned int val;
if ('0' <= c && c <= '9') val = c - '0'; else if ('a' <= lc && lc <= 'f') val = lc - 'a' + 10; else break;
if (val >= base) break; /* * Check for overflow only if we are within range of * it in the max base we support (16) */ if (unlikely(res & (~0ull << 60))) { if (check_mul_overflow(res, base, &res) || check_add_overflow(res, val, &res)) { res = ULLONG_MAX; overflow = KSTRTOX_OVERFLOW; } } else { res = res * base + val; } } *p = res; return rv | overflow;}
noinlineunsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p){ return _parse_integer_limit(s, base, p, INT_MAX);}
static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res){ unsigned long long _res; unsigned int rv;
s = _parse_integer_fixup_radix(s, &base); rv = _parse_integer(s, base, &_res); if (rv & KSTRTOX_OVERFLOW) return -ERANGE; if (rv == 0) return -EINVAL; s += rv; if (*s == '\n') s++; if (*s) return -EINVAL; *res = _res; return 0;}
/** * kstrtoull - convert a string to an unsigned long long * @s: The start of the string. The string must be null-terminated, and may also * include a single newline before its terminating null. The first character * may also be a plus sign, but not a minus sign. * @base: The number base to use. The maximum supported base is 16. If base is * given as 0, then the base of the string is automatically detected with the * conventional semantics - If it begins with 0x the number will be parsed as a * hexadecimal (case insensitive), if it otherwise begins with 0, it will be * parsed as an octal number. Otherwise it will be parsed as a decimal. * @res: Where to write the result of the conversion on success. * * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. * Preferred over simple_strtoull(). Return code must be checked. */noinlineint kstrtoull(const char *s, unsigned int base, unsigned long long *res){ if (s[0] == '+') s++; return _kstrtoull(s, base, res);}EXPORT_SYMBOL(kstrtoull);
/** * kstrtoll - convert a string to a long long * @s: The start of the string. The string must be null-terminated, and may also * include a single newline before its terminating null. The first character * may also be a plus sign or a minus sign. * @base: The number base to use. The maximum supported base is 16. If base is * given as 0, then the base of the string is automatically detected with the * conventional semantics - If it begins with 0x the number will be parsed as a * hexadecimal (case insensitive), if it otherwise begins with 0, it will be * parsed as an octal number. Otherwise it will be parsed as a decimal. * @res: Where to write the result of the conversion on success. * * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. * Preferred over simple_strtoll(). Return code must be checked. */noinlineint kstrtoll(const char *s, unsigned int base, long long *res){ unsigned long long tmp; int rv;
if (s[0] == '-') { rv = _kstrtoull(s + 1, base, &tmp); if (rv < 0) return rv; if ((long long)-tmp > 0) return -ERANGE; *res = -tmp; } else { rv = kstrtoull(s, base, &tmp); if (rv < 0) return rv; if ((long long)tmp < 0) return -ERANGE; *res = tmp; } return 0;}EXPORT_SYMBOL(kstrtoll);
/* Internal, do not use. */int _kstrtoul(const char *s, unsigned int base, unsigned long *res){ unsigned long long tmp; int rv;
rv = kstrtoull(s, base, &tmp); if (rv < 0) return rv; if (tmp != (unsigned long)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(_kstrtoul);
/* Internal, do not use. */int _kstrtol(const char *s, unsigned int base, long *res){ long long tmp; int rv;
rv = kstrtoll(s, base, &tmp); if (rv < 0) return rv; if (tmp != (long)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(_kstrtol);
/** * kstrtouint - convert a string to an unsigned int * @s: The start of the string. The string must be null-terminated, and may also * include a single newline before its terminating null. The first character * may also be a plus sign, but not a minus sign. * @base: The number base to use. The maximum supported base is 16. If base is * given as 0, then the base of the string is automatically detected with the * conventional semantics - If it begins with 0x the number will be parsed as a * hexadecimal (case insensitive), if it otherwise begins with 0, it will be * parsed as an octal number. Otherwise it will be parsed as a decimal. * @res: Where to write the result of the conversion on success. * * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. * Preferred over simple_strtoul(). Return code must be checked. */noinlineint kstrtouint(const char *s, unsigned int base, unsigned int *res){ unsigned long long tmp; int rv;
rv = kstrtoull(s, base, &tmp); if (rv < 0) return rv; if (tmp != (unsigned int)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(kstrtouint);
/** * kstrtoint - convert a string to an int * @s: The start of the string. The string must be null-terminated, and may also * include a single newline before its terminating null. The first character * may also be a plus sign or a minus sign. * @base: The number base to use. The maximum supported base is 16. If base is * given as 0, then the base of the string is automatically detected with the * conventional semantics - If it begins with 0x the number will be parsed as a * hexadecimal (case insensitive), if it otherwise begins with 0, it will be * parsed as an octal number. Otherwise it will be parsed as a decimal. * @res: Where to write the result of the conversion on success. * * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. * Preferred over simple_strtol(). Return code must be checked. */noinlineint kstrtoint(const char *s, unsigned int base, int *res){ long long tmp; int rv;
rv = kstrtoll(s, base, &tmp); if (rv < 0) return rv; if (tmp != (int)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(kstrtoint);
noinlineint kstrtou16(const char *s, unsigned int base, u16 *res){ unsigned long long tmp; int rv;
rv = kstrtoull(s, base, &tmp); if (rv < 0) return rv; if (tmp != (u16)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(kstrtou16);
noinlineint kstrtos16(const char *s, unsigned int base, s16 *res){ long long tmp; int rv;
rv = kstrtoll(s, base, &tmp); if (rv < 0) return rv; if (tmp != (s16)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(kstrtos16);
noinlineint kstrtou8(const char *s, unsigned int base, u8 *res){ unsigned long long tmp; int rv;
rv = kstrtoull(s, base, &tmp); if (rv < 0) return rv; if (tmp != (u8)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(kstrtou8);
noinlineint kstrtos8(const char *s, unsigned int base, s8 *res){ long long tmp; int rv;
rv = kstrtoll(s, base, &tmp); if (rv < 0) return rv; if (tmp != (s8)tmp) return -ERANGE; *res = tmp; return 0;}EXPORT_SYMBOL(kstrtos8);
/** * kstrtobool - convert common user inputs into boolean values * @s: input string * @res: result * * This routine returns 0 iff the first character is one of 'EeYyTt1DdNnFf0', * or [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value * pointed to by res is updated upon finding a match. */noinlineint kstrtobool(const char *s, bool *res){ if (!s) return -EINVAL;
switch (s[0]) { case 'e': case 'E': case 'y': case 'Y': case 't': case 'T': case '1': *res = true; return 0; case 'd': case 'D': case 'n': case 'N': case 'f': case 'F': case '0': *res = false; return 0; case 'o': case 'O': switch (s[1]) { case 'n': case 'N': *res = true; return 0; case 'f': case 'F': *res = false; return 0; default: break; } break; default: break; }
return -EINVAL;}EXPORT_SYMBOL(kstrtobool);
/* * Since "base" would be a nonsense argument, this open-codes the * _from_user helper instead of using the helper macro below. */int kstrtobool_from_user(const char __user *s, size_t count, bool *res){ /* Longest string needed to differentiate, newline, terminator */ char buf[4];
count = min(count, sizeof(buf) - 1); if (copy_from_user(buf, s, count)) return -EFAULT; buf[count] = '\0'; return kstrtobool(buf, res);}EXPORT_SYMBOL(kstrtobool_from_user);
#define kstrto_from_user(f, g, type) \int f(const char __user *s, size_t count, unsigned int base, type *res) \{ \ /* sign, base 2 representation, newline, terminator */ \ char buf[1 + sizeof(type) * 8 + 1 + 1]; \ \ count = min(count, sizeof(buf) - 1); \ if (copy_from_user(buf, s, count)) \ return -EFAULT; \ buf[count] = '\0'; \ return g(buf, base, res); \} \EXPORT_SYMBOL(f)
kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);kstrto_from_user(kstrtol_from_user, kstrtol, long);kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);kstrto_from_user(kstrtoint_from_user, kstrtoint, int);kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);