staging: brcm80211: removed remains of assert mechanism in fullmac
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / kstrtox.c
blob2dbae88090ac3232f591f98289521fa3c9f3b3dd
1 /*
2 * Convert integer string representation to an integer.
3 * If an integer doesn't fit into specified type, -E is returned.
5 * Integer starts with optional sign.
6 * kstrtou*() functions do not accept sign "-".
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.
12 * If -E is returned, result is not touched.
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>
22 static inline char _tolower(const char c)
24 return c | 0x20;
27 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
29 unsigned long long acc;
30 int ok;
32 if (base == 0) {
33 if (s[0] == '0') {
34 if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
35 base = 16;
36 else
37 base = 8;
38 } else
39 base = 10;
41 if (base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
42 s += 2;
44 acc = 0;
45 ok = 0;
46 while (*s) {
47 unsigned int val;
49 if ('0' <= *s && *s <= '9')
50 val = *s - '0';
51 else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
52 val = _tolower(*s) - 'a' + 10;
53 else if (*s == '\n' && *(s + 1) == '\0')
54 break;
55 else
56 return -EINVAL;
58 if (val >= base)
59 return -EINVAL;
60 if (acc > div_u64(ULLONG_MAX - val, base))
61 return -ERANGE;
62 acc = acc * base + val;
63 ok = 1;
65 s++;
67 if (!ok)
68 return -EINVAL;
69 *res = acc;
70 return 0;
73 int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
75 if (s[0] == '+')
76 s++;
77 return _kstrtoull(s, base, res);
79 EXPORT_SYMBOL(kstrtoull);
81 int kstrtoll(const char *s, unsigned int base, long long *res)
83 unsigned long long tmp;
84 int rv;
86 if (s[0] == '-') {
87 rv = _kstrtoull(s + 1, base, &tmp);
88 if (rv < 0)
89 return rv;
90 if ((long long)(-tmp) >= 0)
91 return -ERANGE;
92 *res = -tmp;
93 } else {
94 rv = kstrtoull(s, base, &tmp);
95 if (rv < 0)
96 return rv;
97 if ((long long)tmp < 0)
98 return -ERANGE;
99 *res = tmp;
101 return 0;
103 EXPORT_SYMBOL(kstrtoll);
105 /* Internal, do not use. */
106 int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
108 unsigned long long tmp;
109 int rv;
111 rv = kstrtoull(s, base, &tmp);
112 if (rv < 0)
113 return rv;
114 if (tmp != (unsigned long long)(unsigned long)tmp)
115 return -ERANGE;
116 *res = tmp;
117 return 0;
119 EXPORT_SYMBOL(_kstrtoul);
121 /* Internal, do not use. */
122 int _kstrtol(const char *s, unsigned int base, long *res)
124 long long tmp;
125 int rv;
127 rv = kstrtoll(s, base, &tmp);
128 if (rv < 0)
129 return rv;
130 if (tmp != (long long)(long)tmp)
131 return -ERANGE;
132 *res = tmp;
133 return 0;
135 EXPORT_SYMBOL(_kstrtol);
137 int kstrtouint(const char *s, unsigned int base, unsigned int *res)
139 unsigned long long tmp;
140 int rv;
142 rv = kstrtoull(s, base, &tmp);
143 if (rv < 0)
144 return rv;
145 if (tmp != (unsigned long long)(unsigned int)tmp)
146 return -ERANGE;
147 *res = tmp;
148 return 0;
150 EXPORT_SYMBOL(kstrtouint);
152 int kstrtoint(const char *s, unsigned int base, int *res)
154 long long tmp;
155 int rv;
157 rv = kstrtoll(s, base, &tmp);
158 if (rv < 0)
159 return rv;
160 if (tmp != (long long)(int)tmp)
161 return -ERANGE;
162 *res = tmp;
163 return 0;
165 EXPORT_SYMBOL(kstrtoint);
167 int kstrtou16(const char *s, unsigned int base, u16 *res)
169 unsigned long long tmp;
170 int rv;
172 rv = kstrtoull(s, base, &tmp);
173 if (rv < 0)
174 return rv;
175 if (tmp != (unsigned long long)(u16)tmp)
176 return -ERANGE;
177 *res = tmp;
178 return 0;
180 EXPORT_SYMBOL(kstrtou16);
182 int kstrtos16(const char *s, unsigned int base, s16 *res)
184 long long tmp;
185 int rv;
187 rv = kstrtoll(s, base, &tmp);
188 if (rv < 0)
189 return rv;
190 if (tmp != (long long)(s16)tmp)
191 return -ERANGE;
192 *res = tmp;
193 return 0;
195 EXPORT_SYMBOL(kstrtos16);
197 int kstrtou8(const char *s, unsigned int base, u8 *res)
199 unsigned long long tmp;
200 int rv;
202 rv = kstrtoull(s, base, &tmp);
203 if (rv < 0)
204 return rv;
205 if (tmp != (unsigned long long)(u8)tmp)
206 return -ERANGE;
207 *res = tmp;
208 return 0;
210 EXPORT_SYMBOL(kstrtou8);
212 int kstrtos8(const char *s, unsigned int base, s8 *res)
214 long long tmp;
215 int rv;
217 rv = kstrtoll(s, base, &tmp);
218 if (rv < 0)
219 return rv;
220 if (tmp != (long long)(s8)tmp)
221 return -ERANGE;
222 *res = tmp;
223 return 0;
225 EXPORT_SYMBOL(kstrtos8);
227 #define kstrto_from_user(f, g, type) \
228 int f(const char __user *s, size_t count, unsigned int base, type *res) \
230 /* sign, base 2 representation, newline, terminator */ \
231 char buf[1 + sizeof(type) * 8 + 1 + 1]; \
233 count = min(count, sizeof(buf) - 1); \
234 if (copy_from_user(buf, s, count)) \
235 return -EFAULT; \
236 buf[count] = '\0'; \
237 return g(buf, base, res); \
239 EXPORT_SYMBOL(f)
241 kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long);
242 kstrto_from_user(kstrtoll_from_user, kstrtoll, long long);
243 kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long);
244 kstrto_from_user(kstrtol_from_user, kstrtol, long);
245 kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int);
246 kstrto_from_user(kstrtoint_from_user, kstrtoint, int);
247 kstrto_from_user(kstrtou16_from_user, kstrtou16, u16);
248 kstrto_from_user(kstrtos16_from_user, kstrtos16, s16);
249 kstrto_from_user(kstrtou8_from_user, kstrtou8, u8);
250 kstrto_from_user(kstrtos8_from_user, kstrtos8, s8);