^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Convert integer string representation to an integer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * If an integer doesn't fit into specified type, -E is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Integer starts with optional sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * kstrtou*() functions do not accept sign "-".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Radix 0 means autodetection: leading "0x" implies radix 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * leading "0" implies radix 8, otherwise radix is 10.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Autodetection hints work after optional sign, but not before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * If -E is returned, result is not touched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "kstrtox.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (*base == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (s[0] == '0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *base = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *base = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *base = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) s += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Convert non-negative integer string representation in explicitly given radix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * to an integer. A maximum of max_chars characters will be converted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Return number of characters consumed maybe or-ed with overflow bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * If overflow occurs, result integer (incorrect) is still returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Don't you dare use this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) size_t max_chars)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long long res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) while (max_chars--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned int c = *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned int lc = c | 0x20; /* don't tolower() this line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if ('0' <= c && c <= '9')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) val = c - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) else if ('a' <= lc && lc <= 'f')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) val = lc - 'a' + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (val >= base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * Check for overflow only if we are within range of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * it in the max base we support (16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (unlikely(res & (~0ull << 60))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (res > div_u64(ULLONG_MAX - val, base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) rv |= KSTRTOX_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) res = res * base + val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) rv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *p = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return _parse_integer_limit(s, base, p, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned long long _res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) s = _parse_integer_fixup_radix(s, &base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rv = _parse_integer(s, base, &_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (rv & KSTRTOX_OVERFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (rv == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) s += rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (*s == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) *res = _res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * kstrtoull - convert a string to an unsigned long long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @s: The start of the string. The string must be null-terminated, and may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * include a single newline before its terminating null. The first character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * may also be a plus sign, but not a minus sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @base: The number base to use. The maximum supported base is 16. If base is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * given as 0, then the base of the string is automatically detected with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * conventional semantics - If it begins with 0x the number will be parsed as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * parsed as an octal number. Otherwise it will be parsed as a decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @res: Where to write the result of the conversion on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Preferred over simple_strtoull(). Return code must be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (s[0] == '+')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return _kstrtoull(s, base, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) EXPORT_SYMBOL(kstrtoull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * kstrtoll - convert a string to a long long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * @s: The start of the string. The string must be null-terminated, and may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * include a single newline before its terminating null. The first character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * may also be a plus sign or a minus sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @base: The number base to use. The maximum supported base is 16. If base is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * given as 0, then the base of the string is automatically detected with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * conventional semantics - If it begins with 0x the number will be parsed as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * parsed as an octal number. Otherwise it will be parsed as a decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @res: Where to write the result of the conversion on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Preferred over simple_strtoll(). Return code must be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int kstrtoll(const char *s, unsigned int base, long long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (s[0] == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) rv = _kstrtoull(s + 1, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if ((long long)-tmp > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *res = -tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) rv = kstrtoull(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if ((long long)tmp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) EXPORT_SYMBOL(kstrtoll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Internal, do not use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) rv = kstrtoull(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (tmp != (unsigned long)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) EXPORT_SYMBOL(_kstrtoul);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* Internal, do not use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int _kstrtol(const char *s, unsigned int base, long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) rv = kstrtoll(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (tmp != (long)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) EXPORT_SYMBOL(_kstrtol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * kstrtouint - convert a string to an unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * @s: The start of the string. The string must be null-terminated, and may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * include a single newline before its terminating null. The first character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * may also be a plus sign, but not a minus sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @base: The number base to use. The maximum supported base is 16. If base is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * given as 0, then the base of the string is automatically detected with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * conventional semantics - If it begins with 0x the number will be parsed as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * parsed as an octal number. Otherwise it will be parsed as a decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @res: Where to write the result of the conversion on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * Preferred over simple_strtoul(). Return code must be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int kstrtouint(const char *s, unsigned int base, unsigned int *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) unsigned long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) rv = kstrtoull(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (tmp != (unsigned int)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) EXPORT_SYMBOL(kstrtouint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * kstrtoint - convert a string to an int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * @s: The start of the string. The string must be null-terminated, and may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * include a single newline before its terminating null. The first character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * may also be a plus sign or a minus sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * @base: The number base to use. The maximum supported base is 16. If base is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * given as 0, then the base of the string is automatically detected with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * conventional semantics - If it begins with 0x the number will be parsed as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * parsed as an octal number. Otherwise it will be parsed as a decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @res: Where to write the result of the conversion on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Preferred over simple_strtol(). Return code must be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int kstrtoint(const char *s, unsigned int base, int *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) rv = kstrtoll(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (tmp != (int)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) EXPORT_SYMBOL(kstrtoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int kstrtou16(const char *s, unsigned int base, u16 *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) unsigned long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) rv = kstrtoull(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (tmp != (u16)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) EXPORT_SYMBOL(kstrtou16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int kstrtos16(const char *s, unsigned int base, s16 *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) rv = kstrtoll(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (tmp != (s16)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) EXPORT_SYMBOL(kstrtos16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int kstrtou8(const char *s, unsigned int base, u8 *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) rv = kstrtoull(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (tmp != (u8)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) EXPORT_SYMBOL(kstrtou8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int kstrtos8(const char *s, unsigned int base, s8 *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) rv = kstrtoll(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (tmp != (s8)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) EXPORT_SYMBOL(kstrtos8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * kstrtobool - convert common user inputs into boolean values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * @s: input string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * @res: result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * pointed to by res is updated upon finding a match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int kstrtobool(const char *s, bool *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) switch (s[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) case 'y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) case 'Y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) case '1':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) case 'N':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) case '0':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) *res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) case 'o':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case 'O':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) switch (s[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) case 'N':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) *res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) case 'F':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) *res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) EXPORT_SYMBOL(kstrtobool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * Since "base" would be a nonsense argument, this open-codes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * _from_user helper instead of using the helper macro below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* Longest string needed to differentiate, newline, terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) char buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) count = min(count, sizeof(buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (copy_from_user(buf, s, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) buf[count] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return kstrtobool(buf, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) EXPORT_SYMBOL(kstrtobool_from_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #define kstrto_from_user(f, g, type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) int f(const char __user *s, size_t count, unsigned int base, type *res) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* sign, base 2 representation, newline, terminator */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) char buf[1 + sizeof(type) * 8 + 1 + 1]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) count = min(count, sizeof(buf) - 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (copy_from_user(buf, s, count)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return -EFAULT; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) buf[count] = '\0'; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return g(buf, base, res); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) EXPORT_SYMBOL(f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) kstrto_from_user(kstrtol_from_user, kstrtol, long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);