kstrtox.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/module.h>
  19. #include <linux/types.h>
  20. #include <asm/uaccess.h>
  21. static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  22. {
  23. unsigned long long acc;
  24. int ok;
  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. acc = 0;
  37. ok = 0;
  38. while (*s) {
  39. unsigned int val;
  40. if ('0' <= *s && *s <= '9')
  41. val = *s - '0';
  42. else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
  43. val = _tolower(*s) - 'a' + 10;
  44. else if (*s == '\n' && *(s + 1) == '\0')
  45. break;
  46. else
  47. return -EINVAL;
  48. if (val >= base)
  49. return -EINVAL;
  50. if (acc > div_u64(ULLONG_MAX - val, base))
  51. return -ERANGE;
  52. acc = acc * base + val;
  53. ok = 1;
  54. s++;
  55. }
  56. if (!ok)
  57. return -EINVAL;
  58. *res = acc;
  59. return 0;
  60. }
  61. int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
  62. {
  63. if (s[0] == '+')
  64. s++;
  65. return _kstrtoull(s, base, res);
  66. }
  67. EXPORT_SYMBOL(kstrtoull);
  68. int kstrtoll(const char *s, unsigned int base, long long *res)
  69. {
  70. unsigned long long tmp;
  71. int rv;
  72. if (s[0] == '-') {
  73. rv = _kstrtoull(s + 1, base, &tmp);
  74. if (rv < 0)
  75. return rv;
  76. if ((long long)(-tmp) >= 0)
  77. return -ERANGE;
  78. *res = -tmp;
  79. } else {
  80. rv = kstrtoull(s, base, &tmp);
  81. if (rv < 0)
  82. return rv;
  83. if ((long long)tmp < 0)
  84. return -ERANGE;
  85. *res = tmp;
  86. }
  87. return 0;
  88. }
  89. EXPORT_SYMBOL(kstrtoll);
  90. /* Internal, do not use. */
  91. int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
  92. {
  93. unsigned long long tmp;
  94. int rv;
  95. rv = kstrtoull(s, base, &tmp);
  96. if (rv < 0)
  97. return rv;
  98. if (tmp != (unsigned long long)(unsigned long)tmp)
  99. return -ERANGE;
  100. *res = tmp;
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(_kstrtoul);
  104. /* Internal, do not use. */
  105. int _kstrtol(const char *s, unsigned int base, long *res)
  106. {
  107. long long tmp;
  108. int rv;
  109. rv = kstrtoll(s, base, &tmp);
  110. if (rv < 0)
  111. return rv;
  112. if (tmp != (long long)(long)tmp)
  113. return -ERANGE;
  114. *res = tmp;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(_kstrtol);
  118. int kstrtouint(const char *s, unsigned int base, unsigned int *res)
  119. {
  120. unsigned long long tmp;
  121. int rv;
  122. rv = kstrtoull(s, base, &tmp);
  123. if (rv < 0)
  124. return rv;
  125. if (tmp != (unsigned long long)(unsigned int)tmp)
  126. return -ERANGE;
  127. *res = tmp;
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(kstrtouint);
  131. int kstrtoint(const char *s, unsigned int base, int *res)
  132. {
  133. long long tmp;
  134. int rv;
  135. rv = kstrtoll(s, base, &tmp);
  136. if (rv < 0)
  137. return rv;
  138. if (tmp != (long long)(int)tmp)
  139. return -ERANGE;
  140. *res = tmp;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(kstrtoint);
  144. int kstrtou16(const char *s, unsigned int base, u16 *res)
  145. {
  146. unsigned long long tmp;
  147. int rv;
  148. rv = kstrtoull(s, base, &tmp);
  149. if (rv < 0)
  150. return rv;
  151. if (tmp != (unsigned long long)(u16)tmp)
  152. return -ERANGE;
  153. *res = tmp;
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(kstrtou16);
  157. int kstrtos16(const char *s, unsigned int base, s16 *res)
  158. {
  159. long long tmp;
  160. int rv;
  161. rv = kstrtoll(s, base, &tmp);
  162. if (rv < 0)
  163. return rv;
  164. if (tmp != (long long)(s16)tmp)
  165. return -ERANGE;
  166. *res = tmp;
  167. return 0;
  168. }
  169. EXPORT_SYMBOL(kstrtos16);
  170. int kstrtou8(const char *s, unsigned int base, u8 *res)
  171. {
  172. unsigned long long tmp;
  173. int rv;
  174. rv = kstrtoull(s, base, &tmp);
  175. if (rv < 0)
  176. return rv;
  177. if (tmp != (unsigned long long)(u8)tmp)
  178. return -ERANGE;
  179. *res = tmp;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(kstrtou8);
  183. int kstrtos8(const char *s, unsigned int base, s8 *res)
  184. {
  185. long long tmp;
  186. int rv;
  187. rv = kstrtoll(s, base, &tmp);
  188. if (rv < 0)
  189. return rv;
  190. if (tmp != (long long)(s8)tmp)
  191. return -ERANGE;
  192. *res = tmp;
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(kstrtos8);
  196. #define kstrto_from_user(f, g, type) \
  197. int f(const char __user *s, size_t count, unsigned int base, type *res) \
  198. { \
  199. /* sign, base 2 representation, newline, terminator */ \
  200. char buf[1 + sizeof(type) * 8 + 1 + 1]; \
  201. \
  202. count = min(count, sizeof(buf) - 1); \
  203. if (copy_from_user(buf, s, count)) \
  204. return -EFAULT; \
  205. buf[count] = '\0'; \
  206. return g(buf, base, res); \
  207. } \
  208. EXPORT_SYMBOL(f)
  209. kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
  210. kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
  211. kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
  212. kstrto_from_user(kstrtol_from_user, kstrtol, long);
  213. kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
  214. kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
  215. kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
  216. kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
  217. kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
  218. kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);