kstrtox.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  100. {
  101. if (s[0] == '+')
  102. s++;
  103. return _kstrtoull(s, base, res);
  104. }
  105. EXPORT_SYMBOL(kstrtoull);
  106. int kstrtoll(const char *s, unsigned int base, long long *res)
  107. {
  108. unsigned long long tmp;
  109. int rv;
  110. if (s[0] == '-') {
  111. rv = _kstrtoull(s + 1, base, &tmp);
  112. if (rv < 0)
  113. return rv;
  114. if ((long long)(-tmp) >= 0)
  115. return -ERANGE;
  116. *res = -tmp;
  117. } else {
  118. rv = kstrtoull(s, base, &tmp);
  119. if (rv < 0)
  120. return rv;
  121. if ((long long)tmp < 0)
  122. return -ERANGE;
  123. *res = tmp;
  124. }
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(kstrtoll);
  128. /* Internal, do not use. */
  129. int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
  130. {
  131. unsigned long long tmp;
  132. int rv;
  133. rv = kstrtoull(s, base, &tmp);
  134. if (rv < 0)
  135. return rv;
  136. if (tmp != (unsigned long long)(unsigned long)tmp)
  137. return -ERANGE;
  138. *res = tmp;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(_kstrtoul);
  142. /* Internal, do not use. */
  143. int _kstrtol(const char *s, unsigned int base, long *res)
  144. {
  145. long long tmp;
  146. int rv;
  147. rv = kstrtoll(s, base, &tmp);
  148. if (rv < 0)
  149. return rv;
  150. if (tmp != (long long)(long)tmp)
  151. return -ERANGE;
  152. *res = tmp;
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(_kstrtol);
  156. int kstrtouint(const char *s, unsigned int base, unsigned int *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 int)tmp)
  164. return -ERANGE;
  165. *res = tmp;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(kstrtouint);
  169. int kstrtoint(const char *s, unsigned int base, int *res)
  170. {
  171. long long tmp;
  172. int rv;
  173. rv = kstrtoll(s, base, &tmp);
  174. if (rv < 0)
  175. return rv;
  176. if (tmp != (long long)(int)tmp)
  177. return -ERANGE;
  178. *res = tmp;
  179. return 0;
  180. }
  181. EXPORT_SYMBOL(kstrtoint);
  182. int kstrtou16(const char *s, unsigned int base, u16 *res)
  183. {
  184. unsigned long long tmp;
  185. int rv;
  186. rv = kstrtoull(s, base, &tmp);
  187. if (rv < 0)
  188. return rv;
  189. if (tmp != (unsigned long long)(u16)tmp)
  190. return -ERANGE;
  191. *res = tmp;
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(kstrtou16);
  195. int kstrtos16(const char *s, unsigned int base, s16 *res)
  196. {
  197. long long tmp;
  198. int rv;
  199. rv = kstrtoll(s, base, &tmp);
  200. if (rv < 0)
  201. return rv;
  202. if (tmp != (long long)(s16)tmp)
  203. return -ERANGE;
  204. *res = tmp;
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(kstrtos16);
  208. int kstrtou8(const char *s, unsigned int base, u8 *res)
  209. {
  210. unsigned long long tmp;
  211. int rv;
  212. rv = kstrtoull(s, base, &tmp);
  213. if (rv < 0)
  214. return rv;
  215. if (tmp != (unsigned long long)(u8)tmp)
  216. return -ERANGE;
  217. *res = tmp;
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(kstrtou8);
  221. int kstrtos8(const char *s, unsigned int base, s8 *res)
  222. {
  223. long long tmp;
  224. int rv;
  225. rv = kstrtoll(s, base, &tmp);
  226. if (rv < 0)
  227. return rv;
  228. if (tmp != (long long)(s8)tmp)
  229. return -ERANGE;
  230. *res = tmp;
  231. return 0;
  232. }
  233. EXPORT_SYMBOL(kstrtos8);
  234. #define kstrto_from_user(f, g, type) \
  235. int f(const char __user *s, size_t count, unsigned int base, type *res) \
  236. { \
  237. /* sign, base 2 representation, newline, terminator */ \
  238. char buf[1 + sizeof(type) * 8 + 1 + 1]; \
  239. \
  240. count = min(count, sizeof(buf) - 1); \
  241. if (copy_from_user(buf, s, count)) \
  242. return -EFAULT; \
  243. buf[count] = '\0'; \
  244. return g(buf, base, res); \
  245. } \
  246. EXPORT_SYMBOL(f)
  247. kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
  248. kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
  249. kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
  250. kstrto_from_user(kstrtol_from_user, kstrtol, long);
  251. kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
  252. kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
  253. kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
  254. kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
  255. kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
  256. kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);