kstrtox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include "kstrtox.h"
  23. noinline
  24. const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
  25. {
  26. if (*base == 0) {
  27. if (s[0] == '0') {
  28. if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
  29. *base = 16;
  30. else
  31. *base = 8;
  32. } else
  33. *base = 10;
  34. }
  35. if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
  36. s += 2;
  37. return s;
  38. }
  39. /*
  40. * Convert non-negative integer string representation in explicitly given radix
  41. * to an integer. A maximum of max_chars characters will be converted.
  42. *
  43. * Return number of characters consumed maybe or-ed with overflow bit.
  44. * If overflow occurs, result integer (incorrect) is still returned.
  45. *
  46. * Don't you dare use this function.
  47. */
  48. noinline
  49. unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p,
  50. size_t max_chars)
  51. {
  52. unsigned long long res;
  53. unsigned int rv;
  54. res = 0;
  55. rv = 0;
  56. while (max_chars--) {
  57. unsigned int c = *s;
  58. unsigned int lc = c | 0x20; /* don't tolower() this line */
  59. unsigned int val;
  60. if ('0' <= c && c <= '9')
  61. val = c - '0';
  62. else if ('a' <= lc && lc <= 'f')
  63. val = lc - 'a' + 10;
  64. else
  65. break;
  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. noinline
  84. unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
  85. {
  86. return _parse_integer_limit(s, base, p, INT_MAX);
  87. }
  88. static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  89. {
  90. unsigned long long _res;
  91. unsigned int rv;
  92. s = _parse_integer_fixup_radix(s, &base);
  93. rv = _parse_integer(s, base, &_res);
  94. if (rv & KSTRTOX_OVERFLOW)
  95. return -ERANGE;
  96. if (rv == 0)
  97. return -EINVAL;
  98. s += rv;
  99. if (*s == '\n')
  100. s++;
  101. if (*s)
  102. return -EINVAL;
  103. *res = _res;
  104. return 0;
  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. noinline
  122. int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  123. {
  124. if (s[0] == '+')
  125. s++;
  126. return _kstrtoull(s, base, res);
  127. }
  128. EXPORT_SYMBOL(kstrtoull);
  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. noinline
  145. int kstrtoll(const char *s, unsigned int base, long long *res)
  146. {
  147. unsigned long long tmp;
  148. int rv;
  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. /* Internal, do not use. */
  168. int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
  169. {
  170. unsigned long long tmp;
  171. int rv;
  172. rv = kstrtoull(s, base, &tmp);
  173. if (rv < 0)
  174. return rv;
  175. if (tmp != (unsigned long)tmp)
  176. return -ERANGE;
  177. *res = tmp;
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(_kstrtoul);
  181. /* Internal, do not use. */
  182. int _kstrtol(const char *s, unsigned int base, long *res)
  183. {
  184. long long tmp;
  185. int rv;
  186. rv = kstrtoll(s, base, &tmp);
  187. if (rv < 0)
  188. return rv;
  189. if (tmp != (long)tmp)
  190. return -ERANGE;
  191. *res = tmp;
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(_kstrtol);
  195. /**
  196. * kstrtouint - convert a string to an unsigned int
  197. * @s: The start of the string. The string must be null-terminated, and may also
  198. * include a single newline before its terminating null. The first character
  199. * may also be a plus sign, but not a minus sign.
  200. * @base: The number base to use. The maximum supported base is 16. If base is
  201. * given as 0, then the base of the string is automatically detected with the
  202. * conventional semantics - If it begins with 0x the number will be parsed as a
  203. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  204. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  205. * @res: Where to write the result of the conversion on success.
  206. *
  207. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  208. * Preferred over simple_strtoul(). Return code must be checked.
  209. */
  210. noinline
  211. int kstrtouint(const char *s, unsigned int base, unsigned int *res)
  212. {
  213. unsigned long long tmp;
  214. int rv;
  215. rv = kstrtoull(s, base, &tmp);
  216. if (rv < 0)
  217. return rv;
  218. if (tmp != (unsigned int)tmp)
  219. return -ERANGE;
  220. *res = tmp;
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(kstrtouint);
  224. /**
  225. * kstrtoint - convert a string to an int
  226. * @s: The start of the string. The string must be null-terminated, and may also
  227. * include a single newline before its terminating null. The first character
  228. * may also be a plus sign or a minus sign.
  229. * @base: The number base to use. The maximum supported base is 16. If base is
  230. * given as 0, then the base of the string is automatically detected with the
  231. * conventional semantics - If it begins with 0x the number will be parsed as a
  232. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  233. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  234. * @res: Where to write the result of the conversion on success.
  235. *
  236. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  237. * Preferred over simple_strtol(). Return code must be checked.
  238. */
  239. noinline
  240. int kstrtoint(const char *s, unsigned int base, int *res)
  241. {
  242. long long tmp;
  243. int rv;
  244. rv = kstrtoll(s, base, &tmp);
  245. if (rv < 0)
  246. return rv;
  247. if (tmp != (int)tmp)
  248. return -ERANGE;
  249. *res = tmp;
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(kstrtoint);
  253. noinline
  254. int kstrtou16(const char *s, unsigned int base, u16 *res)
  255. {
  256. unsigned long long tmp;
  257. int rv;
  258. rv = kstrtoull(s, base, &tmp);
  259. if (rv < 0)
  260. return rv;
  261. if (tmp != (u16)tmp)
  262. return -ERANGE;
  263. *res = tmp;
  264. return 0;
  265. }
  266. EXPORT_SYMBOL(kstrtou16);
  267. noinline
  268. int kstrtos16(const char *s, unsigned int base, s16 *res)
  269. {
  270. long long tmp;
  271. int rv;
  272. rv = kstrtoll(s, base, &tmp);
  273. if (rv < 0)
  274. return rv;
  275. if (tmp != (s16)tmp)
  276. return -ERANGE;
  277. *res = tmp;
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(kstrtos16);
  281. noinline
  282. int kstrtou8(const char *s, unsigned int base, u8 *res)
  283. {
  284. unsigned long long tmp;
  285. int rv;
  286. rv = kstrtoull(s, base, &tmp);
  287. if (rv < 0)
  288. return rv;
  289. if (tmp != (u8)tmp)
  290. return -ERANGE;
  291. *res = tmp;
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(kstrtou8);
  295. noinline
  296. int kstrtos8(const char *s, unsigned int base, s8 *res)
  297. {
  298. long long tmp;
  299. int rv;
  300. rv = kstrtoll(s, base, &tmp);
  301. if (rv < 0)
  302. return rv;
  303. if (tmp != (s8)tmp)
  304. return -ERANGE;
  305. *res = tmp;
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(kstrtos8);
  309. /**
  310. * kstrtobool - convert common user inputs into boolean values
  311. * @s: input string
  312. * @res: result
  313. *
  314. * This routine returns 0 iff the first character is one of 'YyTt1NnFf0', or
  315. * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
  316. * pointed to by res is updated upon finding a match.
  317. */
  318. noinline
  319. int kstrtobool(const char *s, bool *res)
  320. {
  321. if (!s)
  322. return -EINVAL;
  323. switch (s[0]) {
  324. case 'y':
  325. case 'Y':
  326. case 't':
  327. case 'T':
  328. case '1':
  329. *res = true;
  330. return 0;
  331. case 'n':
  332. case 'N':
  333. case 'f':
  334. case 'F':
  335. case '0':
  336. *res = false;
  337. return 0;
  338. case 'o':
  339. case 'O':
  340. switch (s[1]) {
  341. case 'n':
  342. case 'N':
  343. *res = true;
  344. return 0;
  345. case 'f':
  346. case 'F':
  347. *res = false;
  348. return 0;
  349. default:
  350. break;
  351. }
  352. break;
  353. default:
  354. break;
  355. }
  356. return -EINVAL;
  357. }
  358. EXPORT_SYMBOL(kstrtobool);
  359. /*
  360. * Since "base" would be a nonsense argument, this open-codes the
  361. * _from_user helper instead of using the helper macro below.
  362. */
  363. int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
  364. {
  365. /* Longest string needed to differentiate, newline, terminator */
  366. char buf[4];
  367. count = min(count, sizeof(buf) - 1);
  368. if (copy_from_user(buf, s, count))
  369. return -EFAULT;
  370. buf[count] = '\0';
  371. return kstrtobool(buf, res);
  372. }
  373. EXPORT_SYMBOL(kstrtobool_from_user);
  374. #define kstrto_from_user(f, g, type) \
  375. int f(const char __user *s, size_t count, unsigned int base, type *res) \
  376. { \
  377. /* sign, base 2 representation, newline, terminator */ \
  378. char buf[1 + sizeof(type) * 8 + 1 + 1]; \
  379. \
  380. count = min(count, sizeof(buf) - 1); \
  381. if (copy_from_user(buf, s, count)) \
  382. return -EFAULT; \
  383. buf[count] = '\0'; \
  384. return g(buf, base, res); \
  385. } \
  386. EXPORT_SYMBOL(f)
  387. kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
  388. kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
  389. kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
  390. kstrto_from_user(kstrtol_from_user, kstrtol, long);
  391. kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
  392. kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
  393. kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
  394. kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
  395. kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
  396. kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);