kstrtox.c 11 KB

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