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