kstrtox.c 5.1 KB

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