1 | n/a | |
---|
2 | n/a | #include "Python.h" |
---|
3 | n/a | |
---|
4 | n/a | #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE) |
---|
5 | n/a | #define _SGI_MP_SOURCE |
---|
6 | n/a | #endif |
---|
7 | n/a | |
---|
8 | n/a | /* strtol and strtoul, renamed to avoid conflicts */ |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | #include <ctype.h> |
---|
12 | n/a | #ifdef HAVE_ERRNO_H |
---|
13 | n/a | #include <errno.h> |
---|
14 | n/a | #endif |
---|
15 | n/a | |
---|
16 | n/a | /* Static overflow check values for bases 2 through 36. |
---|
17 | n/a | * smallmax[base] is the largest unsigned long i such that |
---|
18 | n/a | * i * base doesn't overflow unsigned long. |
---|
19 | n/a | */ |
---|
20 | n/a | static const unsigned long smallmax[] = { |
---|
21 | n/a | 0, /* bases 0 and 1 are invalid */ |
---|
22 | n/a | 0, |
---|
23 | n/a | ULONG_MAX / 2, |
---|
24 | n/a | ULONG_MAX / 3, |
---|
25 | n/a | ULONG_MAX / 4, |
---|
26 | n/a | ULONG_MAX / 5, |
---|
27 | n/a | ULONG_MAX / 6, |
---|
28 | n/a | ULONG_MAX / 7, |
---|
29 | n/a | ULONG_MAX / 8, |
---|
30 | n/a | ULONG_MAX / 9, |
---|
31 | n/a | ULONG_MAX / 10, |
---|
32 | n/a | ULONG_MAX / 11, |
---|
33 | n/a | ULONG_MAX / 12, |
---|
34 | n/a | ULONG_MAX / 13, |
---|
35 | n/a | ULONG_MAX / 14, |
---|
36 | n/a | ULONG_MAX / 15, |
---|
37 | n/a | ULONG_MAX / 16, |
---|
38 | n/a | ULONG_MAX / 17, |
---|
39 | n/a | ULONG_MAX / 18, |
---|
40 | n/a | ULONG_MAX / 19, |
---|
41 | n/a | ULONG_MAX / 20, |
---|
42 | n/a | ULONG_MAX / 21, |
---|
43 | n/a | ULONG_MAX / 22, |
---|
44 | n/a | ULONG_MAX / 23, |
---|
45 | n/a | ULONG_MAX / 24, |
---|
46 | n/a | ULONG_MAX / 25, |
---|
47 | n/a | ULONG_MAX / 26, |
---|
48 | n/a | ULONG_MAX / 27, |
---|
49 | n/a | ULONG_MAX / 28, |
---|
50 | n/a | ULONG_MAX / 29, |
---|
51 | n/a | ULONG_MAX / 30, |
---|
52 | n/a | ULONG_MAX / 31, |
---|
53 | n/a | ULONG_MAX / 32, |
---|
54 | n/a | ULONG_MAX / 33, |
---|
55 | n/a | ULONG_MAX / 34, |
---|
56 | n/a | ULONG_MAX / 35, |
---|
57 | n/a | ULONG_MAX / 36, |
---|
58 | n/a | }; |
---|
59 | n/a | |
---|
60 | n/a | /* maximum digits that can't ever overflow for bases 2 through 36, |
---|
61 | n/a | * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)]. |
---|
62 | n/a | * Note that this is pessimistic if sizeof(long) > 4. |
---|
63 | n/a | */ |
---|
64 | n/a | #if SIZEOF_LONG == 4 |
---|
65 | n/a | static const int digitlimit[] = { |
---|
66 | n/a | 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */ |
---|
67 | n/a | 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */ |
---|
68 | n/a | 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */ |
---|
69 | n/a | 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */ |
---|
70 | n/a | #elif SIZEOF_LONG == 8 |
---|
71 | n/a | /* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */ |
---|
72 | n/a | static const int digitlimit[] = { |
---|
73 | n/a | 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */ |
---|
74 | n/a | 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */ |
---|
75 | n/a | 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */ |
---|
76 | n/a | 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */ |
---|
77 | n/a | #else |
---|
78 | n/a | #error "Need table for SIZEOF_LONG" |
---|
79 | n/a | #endif |
---|
80 | n/a | |
---|
81 | n/a | /* |
---|
82 | n/a | ** strtoul |
---|
83 | n/a | ** This is a general purpose routine for converting |
---|
84 | n/a | ** an ascii string to an integer in an arbitrary base. |
---|
85 | n/a | ** Leading white space is ignored. If 'base' is zero |
---|
86 | n/a | ** it looks for a leading 0b, 0o or 0x to tell which |
---|
87 | n/a | ** base. If these are absent it defaults to 10. |
---|
88 | n/a | ** Base must be 0 or between 2 and 36 (inclusive). |
---|
89 | n/a | ** If 'ptr' is non-NULL it will contain a pointer to |
---|
90 | n/a | ** the end of the scan. |
---|
91 | n/a | ** Errors due to bad pointers will probably result in |
---|
92 | n/a | ** exceptions - we don't check for them. |
---|
93 | n/a | */ |
---|
94 | n/a | unsigned long |
---|
95 | n/a | PyOS_strtoul(const char *str, char **ptr, int base) |
---|
96 | n/a | { |
---|
97 | n/a | unsigned long result = 0; /* return value of the function */ |
---|
98 | n/a | int c; /* current input character */ |
---|
99 | n/a | int ovlimit; /* required digits to overflow */ |
---|
100 | n/a | |
---|
101 | n/a | /* skip leading white space */ |
---|
102 | n/a | while (*str && Py_ISSPACE(Py_CHARMASK(*str))) |
---|
103 | n/a | ++str; |
---|
104 | n/a | |
---|
105 | n/a | /* check for leading 0b, 0o or 0x for auto-base or base 16 */ |
---|
106 | n/a | switch (base) { |
---|
107 | n/a | case 0: /* look for leading 0b, 0o or 0x */ |
---|
108 | n/a | if (*str == '0') { |
---|
109 | n/a | ++str; |
---|
110 | n/a | if (*str == 'x' || *str == 'X') { |
---|
111 | n/a | /* there must be at least one digit after 0x */ |
---|
112 | n/a | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) { |
---|
113 | n/a | if (ptr) |
---|
114 | n/a | *ptr = (char *)str; |
---|
115 | n/a | return 0; |
---|
116 | n/a | } |
---|
117 | n/a | ++str; |
---|
118 | n/a | base = 16; |
---|
119 | n/a | } else if (*str == 'o' || *str == 'O') { |
---|
120 | n/a | /* there must be at least one digit after 0o */ |
---|
121 | n/a | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) { |
---|
122 | n/a | if (ptr) |
---|
123 | n/a | *ptr = (char *)str; |
---|
124 | n/a | return 0; |
---|
125 | n/a | } |
---|
126 | n/a | ++str; |
---|
127 | n/a | base = 8; |
---|
128 | n/a | } else if (*str == 'b' || *str == 'B') { |
---|
129 | n/a | /* there must be at least one digit after 0b */ |
---|
130 | n/a | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) { |
---|
131 | n/a | if (ptr) |
---|
132 | n/a | *ptr = (char *)str; |
---|
133 | n/a | return 0; |
---|
134 | n/a | } |
---|
135 | n/a | ++str; |
---|
136 | n/a | base = 2; |
---|
137 | n/a | } else { |
---|
138 | n/a | /* skip all zeroes... */ |
---|
139 | n/a | while (*str == '0') |
---|
140 | n/a | ++str; |
---|
141 | n/a | while (Py_ISSPACE(Py_CHARMASK(*str))) |
---|
142 | n/a | ++str; |
---|
143 | n/a | if (ptr) |
---|
144 | n/a | *ptr = (char *)str; |
---|
145 | n/a | return 0; |
---|
146 | n/a | } |
---|
147 | n/a | } |
---|
148 | n/a | else |
---|
149 | n/a | base = 10; |
---|
150 | n/a | break; |
---|
151 | n/a | |
---|
152 | n/a | /* even with explicit base, skip leading 0? prefix */ |
---|
153 | n/a | case 16: |
---|
154 | n/a | if (*str == '0') { |
---|
155 | n/a | ++str; |
---|
156 | n/a | if (*str == 'x' || *str == 'X') { |
---|
157 | n/a | /* there must be at least one digit after 0x */ |
---|
158 | n/a | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) { |
---|
159 | n/a | if (ptr) |
---|
160 | n/a | *ptr = (char *)str; |
---|
161 | n/a | return 0; |
---|
162 | n/a | } |
---|
163 | n/a | ++str; |
---|
164 | n/a | } |
---|
165 | n/a | } |
---|
166 | n/a | break; |
---|
167 | n/a | case 8: |
---|
168 | n/a | if (*str == '0') { |
---|
169 | n/a | ++str; |
---|
170 | n/a | if (*str == 'o' || *str == 'O') { |
---|
171 | n/a | /* there must be at least one digit after 0o */ |
---|
172 | n/a | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) { |
---|
173 | n/a | if (ptr) |
---|
174 | n/a | *ptr = (char *)str; |
---|
175 | n/a | return 0; |
---|
176 | n/a | } |
---|
177 | n/a | ++str; |
---|
178 | n/a | } |
---|
179 | n/a | } |
---|
180 | n/a | break; |
---|
181 | n/a | case 2: |
---|
182 | n/a | if(*str == '0') { |
---|
183 | n/a | ++str; |
---|
184 | n/a | if (*str == 'b' || *str == 'B') { |
---|
185 | n/a | /* there must be at least one digit after 0b */ |
---|
186 | n/a | if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) { |
---|
187 | n/a | if (ptr) |
---|
188 | n/a | *ptr = (char *)str; |
---|
189 | n/a | return 0; |
---|
190 | n/a | } |
---|
191 | n/a | ++str; |
---|
192 | n/a | } |
---|
193 | n/a | } |
---|
194 | n/a | break; |
---|
195 | n/a | } |
---|
196 | n/a | |
---|
197 | n/a | /* catch silly bases */ |
---|
198 | n/a | if (base < 2 || base > 36) { |
---|
199 | n/a | if (ptr) |
---|
200 | n/a | *ptr = (char *)str; |
---|
201 | n/a | return 0; |
---|
202 | n/a | } |
---|
203 | n/a | |
---|
204 | n/a | /* skip leading zeroes */ |
---|
205 | n/a | while (*str == '0') |
---|
206 | n/a | ++str; |
---|
207 | n/a | |
---|
208 | n/a | /* base is guaranteed to be in [2, 36] at this point */ |
---|
209 | n/a | ovlimit = digitlimit[base]; |
---|
210 | n/a | |
---|
211 | n/a | /* do the conversion until non-digit character encountered */ |
---|
212 | n/a | while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) { |
---|
213 | n/a | if (ovlimit > 0) /* no overflow check required */ |
---|
214 | n/a | result = result * base + c; |
---|
215 | n/a | else { /* requires overflow check */ |
---|
216 | n/a | unsigned long temp_result; |
---|
217 | n/a | |
---|
218 | n/a | if (ovlimit < 0) /* guaranteed overflow */ |
---|
219 | n/a | goto overflowed; |
---|
220 | n/a | |
---|
221 | n/a | /* there could be an overflow */ |
---|
222 | n/a | /* check overflow just from shifting */ |
---|
223 | n/a | if (result > smallmax[base]) |
---|
224 | n/a | goto overflowed; |
---|
225 | n/a | |
---|
226 | n/a | result *= base; |
---|
227 | n/a | |
---|
228 | n/a | /* check overflow from the digit's value */ |
---|
229 | n/a | temp_result = result + c; |
---|
230 | n/a | if (temp_result < result) |
---|
231 | n/a | goto overflowed; |
---|
232 | n/a | |
---|
233 | n/a | result = temp_result; |
---|
234 | n/a | } |
---|
235 | n/a | |
---|
236 | n/a | ++str; |
---|
237 | n/a | --ovlimit; |
---|
238 | n/a | } |
---|
239 | n/a | |
---|
240 | n/a | /* set pointer to point to the last character scanned */ |
---|
241 | n/a | if (ptr) |
---|
242 | n/a | *ptr = (char *)str; |
---|
243 | n/a | |
---|
244 | n/a | return result; |
---|
245 | n/a | |
---|
246 | n/a | overflowed: |
---|
247 | n/a | if (ptr) { |
---|
248 | n/a | /* spool through remaining digit characters */ |
---|
249 | n/a | while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base) |
---|
250 | n/a | ++str; |
---|
251 | n/a | *ptr = (char *)str; |
---|
252 | n/a | } |
---|
253 | n/a | errno = ERANGE; |
---|
254 | n/a | return (unsigned long)-1; |
---|
255 | n/a | } |
---|
256 | n/a | |
---|
257 | n/a | /* Checking for overflow in PyOS_strtol is a PITA; see comments |
---|
258 | n/a | * about PY_ABS_LONG_MIN in longobject.c. |
---|
259 | n/a | */ |
---|
260 | n/a | #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
---|
261 | n/a | |
---|
262 | n/a | long |
---|
263 | n/a | PyOS_strtol(const char *str, char **ptr, int base) |
---|
264 | n/a | { |
---|
265 | n/a | long result; |
---|
266 | n/a | unsigned long uresult; |
---|
267 | n/a | char sign; |
---|
268 | n/a | |
---|
269 | n/a | while (*str && Py_ISSPACE(Py_CHARMASK(*str))) |
---|
270 | n/a | str++; |
---|
271 | n/a | |
---|
272 | n/a | sign = *str; |
---|
273 | n/a | if (sign == '+' || sign == '-') |
---|
274 | n/a | str++; |
---|
275 | n/a | |
---|
276 | n/a | uresult = PyOS_strtoul(str, ptr, base); |
---|
277 | n/a | |
---|
278 | n/a | if (uresult <= (unsigned long)LONG_MAX) { |
---|
279 | n/a | result = (long)uresult; |
---|
280 | n/a | if (sign == '-') |
---|
281 | n/a | result = -result; |
---|
282 | n/a | } |
---|
283 | n/a | else if (sign == '-' && uresult == PY_ABS_LONG_MIN) { |
---|
284 | n/a | result = LONG_MIN; |
---|
285 | n/a | } |
---|
286 | n/a | else { |
---|
287 | n/a | errno = ERANGE; |
---|
288 | n/a | result = LONG_MAX; |
---|
289 | n/a | } |
---|
290 | n/a | return result; |
---|
291 | n/a | } |
---|