kstrtox.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. int overflow;
  50. res = 0;
  51. rv = 0;
  52. overflow = 0;
  53. while (*s) {
  54. unsigned int val;
  55. if ('0' <= *s && *s <= '9')
  56. val = *s - '0';
  57. else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
  58. val = _tolower(*s) - 'a' + 10;
  59. else
  60. break;
  61. if (val >= base)
  62. break;
  63. /*
  64. * Check for overflow only if we are within range of
  65. * it in the max base we support (16)
  66. */
  67. if (unlikely(res & (~0ull << 60))) {
  68. if (res > div_u64(ULLONG_MAX - val, base))
  69. overflow = 1;
  70. }
  71. res = res * base + val;
  72. rv++;
  73. s++;
  74. }
  75. *p = res;
  76. if (overflow)
  77. rv |= KSTRTOX_OVERFLOW;
  78. return rv;
  79. }
  80. static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  81. {
  82. unsigned long long _res;
  83. unsigned int rv;
  84. s = _parse_integer_fixup_radix(s, &base);
  85. rv = _parse_integer(s, base, &_res);
  86. if (rv & KSTRTOX_OVERFLOW)
  87. return -ERANGE;
  88. rv &= ~KSTRTOX_OVERFLOW;
  89. if (rv == 0)
  90. return -EINVAL;
  91. s += rv;
  92. if (*s == '\n')
  93. s++;
  94. if (*s)
  95. return -EINVAL;
  96. *res = _res;
  97. return 0;
  98. }
  99. /**
  100. * kstrtoull - convert a string to an unsigned long long
  101. * @s: The start of the string. The string must be null-terminated, and may also
  102. * include a single newline before its terminating null. The first character
  103. * may also be a plus sign, but not a minus sign.
  104. * @base: The number base to use. The maximum supported base is 16. If base is
  105. * given as 0, then the base of the string is automatically detected with the
  106. * conventional semantics - If it begins with 0x the number will be parsed as a
  107. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  108. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  109. * @res: Where to write the result of the conversion on success.
  110. *
  111. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  112. * Used as a replacement for the obsolete simple_strtoull. Return code must
  113. * be checked.
  114. */
  115. int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  116. {
  117. if (s[0] == '+')
  118. s++;
  119. return _kstrtoull(s, base, res);
  120. }
  121. EXPORT_SYMBOL(kstrtoull);
  122. /**
  123. * kstrtoll - convert a string to a long long
  124. * @s: The start of the string. The string must be null-terminated, and may also
  125. * include a single newline before its terminating null. The first character
  126. * may also be a plus sign or a minus sign.
  127. * @base: The number base to use. The maximum supported base is 16. If base is
  128. * given as 0, then the base of the string is automatically detected with the
  129. * conventional semantics - If it begins with 0x the number will be parsed as a
  130. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  131. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  132. * @res: Where to write the result of the conversion on success.
  133. *
  134. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  135. * Used as a replacement for the obsolete simple_strtoull. Return code must
  136. * be checked.
  137. */
  138. int kstrtoll(const char *s, unsigned int base, long long *res)
  139. {
  140. unsigned long long tmp;
  141. int rv;
  142. if (s[0] == '-') {
  143. rv = _kstrtoull(s + 1, base, &tmp);
  144. if (rv < 0)
  145. return rv;
  146. if ((long long)(-tmp) >= 0)
  147. return -ERANGE;
  148. *res = -tmp;
  149. } else {
  150. rv = kstrtoull(s, base, &tmp);
  151. if (rv < 0)
  152. return rv;
  153. if ((long long)tmp < 0)
  154. return -ERANGE;
  155. *res = tmp;
  156. }
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(kstrtoll);
  160. /* Internal, do not use. */
  161. int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
  162. {
  163. unsigned long long tmp;
  164. int rv;
  165. rv = kstrtoull(s, base, &tmp);
  166. if (rv < 0)
  167. return rv;
  168. if (tmp != (unsigned long long)(unsigned long)tmp)
  169. return -ERANGE;
  170. *res = tmp;
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(_kstrtoul);
  174. /* Internal, do not use. */
  175. int _kstrtol(const char *s, unsigned int base, long *res)
  176. {
  177. long long tmp;
  178. int rv;
  179. rv = kstrtoll(s, base, &tmp);
  180. if (rv < 0)
  181. return rv;
  182. if (tmp != (long long)(long)tmp)
  183. return -ERANGE;
  184. *res = tmp;
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(_kstrtol);
  188. /**
  189. * kstrtouint - convert a string to an unsigned int
  190. * @s: The start of the string. The string must be null-terminated, and may also
  191. * include a single newline before its terminating null. The first character
  192. * may also be a plus sign, but not a minus sign.
  193. * @base: The number base to use. The maximum supported base is 16. If base is
  194. * given as 0, then the base of the string is automatically detected with the
  195. * conventional semantics - If it begins with 0x the number will be parsed as a
  196. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  197. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  198. * @res: Where to write the result of the conversion on success.
  199. *
  200. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  201. * Used as a replacement for the obsolete simple_strtoull. Return code must
  202. * be checked.
  203. */
  204. int kstrtouint(const char *s, unsigned int base, unsigned int *res)
  205. {
  206. unsigned long long tmp;
  207. int rv;
  208. rv = kstrtoull(s, base, &tmp);
  209. if (rv < 0)
  210. return rv;
  211. if (tmp != (unsigned long long)(unsigned int)tmp)
  212. return -ERANGE;
  213. *res = tmp;
  214. return 0;
  215. }
  216. EXPORT_SYMBOL(kstrtouint);
  217. /**
  218. * kstrtoint - convert a string to an int
  219. * @s: The start of the string. The string must be null-terminated, and may also
  220. * include a single newline before its terminating null. The first character
  221. * may also be a plus sign or a minus sign.
  222. * @base: The number base to use. The maximum supported base is 16. If base is
  223. * given as 0, then the base of the string is automatically detected with the
  224. * conventional semantics - If it begins with 0x the number will be parsed as a
  225. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  226. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  227. * @res: Where to write the result of the conversion on success.
  228. *
  229. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  230. * Used as a replacement for the obsolete simple_strtoull. Return code must
  231. * 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 != (long long)(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 != (unsigned long long)(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 != (long long)(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 != (unsigned long long)(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 != (long long)(s8)tmp)
  293. return -ERANGE;
  294. *res = tmp;
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(kstrtos8);
  298. #define kstrto_from_user(f, g, type) \
  299. int f(const char __user *s, size_t count, unsigned int base, type *res) \
  300. { \
  301. /* sign, base 2 representation, newline, terminator */ \
  302. char buf[1 + sizeof(type) * 8 + 1 + 1]; \
  303. \
  304. count = min(count, sizeof(buf) - 1); \
  305. if (copy_from_user(buf, s, count)) \
  306. return -EFAULT; \
  307. buf[count] = '\0'; \
  308. return g(buf, base, res); \
  309. } \
  310. EXPORT_SYMBOL(f)
  311. kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
  312. kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
  313. kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
  314. kstrto_from_user(kstrtol_from_user, kstrtol, long);
  315. kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
  316. kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
  317. kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
  318. kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
  319. kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
  320. kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);