kstrtox.c 11 KB

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