kstrtox.c 11 KB

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