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. if (rv == 0)
  89. return -EINVAL;
  90. s += rv;
  91. if (*s == '\n')
  92. s++;
  93. if (*s)
  94. return -EINVAL;
  95. *res = _res;
  96. return 0;
  97. }
  98. /**
  99. * kstrtoull - convert a string to an unsigned long long
  100. * @s: The start of the string. The string must be null-terminated, and may also
  101. * include a single newline before its terminating null. The first character
  102. * may also be a plus sign, but not a minus sign.
  103. * @base: The number base to use. The maximum supported base is 16. If base is
  104. * given as 0, then the base of the string is automatically detected with the
  105. * conventional semantics - If it begins with 0x the number will be parsed as a
  106. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  107. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  108. * @res: Where to write the result of the conversion on success.
  109. *
  110. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  111. * Used as a replacement for the obsolete simple_strtoull. Return code must
  112. * be checked.
  113. */
  114. int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  115. {
  116. if (s[0] == '+')
  117. s++;
  118. return _kstrtoull(s, base, res);
  119. }
  120. EXPORT_SYMBOL(kstrtoull);
  121. /**
  122. * kstrtoll - convert a string to a long long
  123. * @s: The start of the string. The string must be null-terminated, and may also
  124. * include a single newline before its terminating null. The first character
  125. * may also be a plus sign or a minus sign.
  126. * @base: The number base to use. The maximum supported base is 16. If base is
  127. * given as 0, then the base of the string is automatically detected with the
  128. * conventional semantics - If it begins with 0x the number will be parsed as a
  129. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  130. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  131. * @res: Where to write the result of the conversion on success.
  132. *
  133. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  134. * Used as a replacement for the obsolete simple_strtoull. Return code must
  135. * be checked.
  136. */
  137. int kstrtoll(const char *s, unsigned int base, long long *res)
  138. {
  139. unsigned long long tmp;
  140. int rv;
  141. if (s[0] == '-') {
  142. rv = _kstrtoull(s + 1, base, &tmp);
  143. if (rv < 0)
  144. return rv;
  145. if ((long long)(-tmp) >= 0)
  146. return -ERANGE;
  147. *res = -tmp;
  148. } else {
  149. rv = kstrtoull(s, base, &tmp);
  150. if (rv < 0)
  151. return rv;
  152. if ((long long)tmp < 0)
  153. return -ERANGE;
  154. *res = tmp;
  155. }
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(kstrtoll);
  159. /* Internal, do not use. */
  160. int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
  161. {
  162. unsigned long long tmp;
  163. int rv;
  164. rv = kstrtoull(s, base, &tmp);
  165. if (rv < 0)
  166. return rv;
  167. if (tmp != (unsigned long long)(unsigned long)tmp)
  168. return -ERANGE;
  169. *res = tmp;
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(_kstrtoul);
  173. /* Internal, do not use. */
  174. int _kstrtol(const char *s, unsigned int base, long *res)
  175. {
  176. long long tmp;
  177. int rv;
  178. rv = kstrtoll(s, base, &tmp);
  179. if (rv < 0)
  180. return rv;
  181. if (tmp != (long long)(long)tmp)
  182. return -ERANGE;
  183. *res = tmp;
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(_kstrtol);
  187. /**
  188. * kstrtouint - convert a string to an unsigned int
  189. * @s: The start of the string. The string must be null-terminated, and may also
  190. * include a single newline before its terminating null. The first character
  191. * may also be a plus sign, but not a minus sign.
  192. * @base: The number base to use. The maximum supported base is 16. If base is
  193. * given as 0, then the base of the string is automatically detected with the
  194. * conventional semantics - If it begins with 0x the number will be parsed as a
  195. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  196. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  197. * @res: Where to write the result of the conversion on success.
  198. *
  199. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  200. * Used as a replacement for the obsolete simple_strtoull. Return code must
  201. * be checked.
  202. */
  203. int kstrtouint(const char *s, unsigned int base, unsigned int *res)
  204. {
  205. unsigned long long tmp;
  206. int rv;
  207. rv = kstrtoull(s, base, &tmp);
  208. if (rv < 0)
  209. return rv;
  210. if (tmp != (unsigned long long)(unsigned int)tmp)
  211. return -ERANGE;
  212. *res = tmp;
  213. return 0;
  214. }
  215. EXPORT_SYMBOL(kstrtouint);
  216. /**
  217. * kstrtoint - convert a string to an int
  218. * @s: The start of the string. The string must be null-terminated, and may also
  219. * include a single newline before its terminating null. The first character
  220. * may also be a plus sign or a minus sign.
  221. * @base: The number base to use. The maximum supported base is 16. If base is
  222. * given as 0, then the base of the string is automatically detected with the
  223. * conventional semantics - If it begins with 0x the number will be parsed as a
  224. * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
  225. * parsed as an octal number. Otherwise it will be parsed as a decimal.
  226. * @res: Where to write the result of the conversion on success.
  227. *
  228. * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
  229. * Used as a replacement for the obsolete simple_strtoull. Return code must
  230. * be checked.
  231. */
  232. int kstrtoint(const char *s, unsigned int base, int *res)
  233. {
  234. long long tmp;
  235. int rv;
  236. rv = kstrtoll(s, base, &tmp);
  237. if (rv < 0)
  238. return rv;
  239. if (tmp != (long long)(int)tmp)
  240. return -ERANGE;
  241. *res = tmp;
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(kstrtoint);
  245. int kstrtou16(const char *s, unsigned int base, u16 *res)
  246. {
  247. unsigned long long tmp;
  248. int rv;
  249. rv = kstrtoull(s, base, &tmp);
  250. if (rv < 0)
  251. return rv;
  252. if (tmp != (unsigned long long)(u16)tmp)
  253. return -ERANGE;
  254. *res = tmp;
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(kstrtou16);
  258. int kstrtos16(const char *s, unsigned int base, s16 *res)
  259. {
  260. long long tmp;
  261. int rv;
  262. rv = kstrtoll(s, base, &tmp);
  263. if (rv < 0)
  264. return rv;
  265. if (tmp != (long long)(s16)tmp)
  266. return -ERANGE;
  267. *res = tmp;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(kstrtos16);
  271. int kstrtou8(const char *s, unsigned int base, u8 *res)
  272. {
  273. unsigned long long tmp;
  274. int rv;
  275. rv = kstrtoull(s, base, &tmp);
  276. if (rv < 0)
  277. return rv;
  278. if (tmp != (unsigned long long)(u8)tmp)
  279. return -ERANGE;
  280. *res = tmp;
  281. return 0;
  282. }
  283. EXPORT_SYMBOL(kstrtou8);
  284. int kstrtos8(const char *s, unsigned int base, s8 *res)
  285. {
  286. long long tmp;
  287. int rv;
  288. rv = kstrtoll(s, base, &tmp);
  289. if (rv < 0)
  290. return rv;
  291. if (tmp != (long long)(s8)tmp)
  292. return -ERANGE;
  293. *res = tmp;
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(kstrtos8);
  297. #define kstrto_from_user(f, g, type) \
  298. int f(const char __user *s, size_t count, unsigned int base, type *res) \
  299. { \
  300. /* sign, base 2 representation, newline, terminator */ \
  301. char buf[1 + sizeof(type) * 8 + 1 + 1]; \
  302. \
  303. count = min(count, sizeof(buf) - 1); \
  304. if (copy_from_user(buf, s, count)) \
  305. return -EFAULT; \
  306. buf[count] = '\0'; \
  307. return g(buf, base, res); \
  308. } \
  309. EXPORT_SYMBOL(f)
  310. kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
  311. kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
  312. kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
  313. kstrto_from_user(kstrtol_from_user, kstrtol, long);
  314. kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
  315. kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
  316. kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
  317. kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
  318. kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
  319. kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);