1 |
|
---|
2 | #include "Python.h"
|
---|
3 |
|
---|
4 | #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE)
|
---|
5 | #define _SGI_MP_SOURCE
|
---|
6 | #endif
|
---|
7 |
|
---|
8 | /* Convert a possibly signed character to a nonnegative int */
|
---|
9 | /* XXX This assumes characters are 8 bits wide */
|
---|
10 | #ifdef __CHAR_UNSIGNED__
|
---|
11 | #define Py_CHARMASK(c) (c)
|
---|
12 | #else
|
---|
13 | #define Py_CHARMASK(c) ((c) & 0xff)
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | /* strtol and strtoul, renamed to avoid conflicts */
|
---|
17 |
|
---|
18 |
|
---|
19 | #include <ctype.h>
|
---|
20 | #ifdef HAVE_ERRNO_H
|
---|
21 | #include <errno.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | /* Static overflow check values for bases 2 through 36.
|
---|
25 | * smallmax[base] is the largest unsigned long i such that
|
---|
26 | * i * base doesn't overflow unsigned long.
|
---|
27 | */
|
---|
28 | static unsigned long smallmax[] = {
|
---|
29 | 0, /* bases 0 and 1 are invalid */
|
---|
30 | 0,
|
---|
31 | ULONG_MAX / 2,
|
---|
32 | ULONG_MAX / 3,
|
---|
33 | ULONG_MAX / 4,
|
---|
34 | ULONG_MAX / 5,
|
---|
35 | ULONG_MAX / 6,
|
---|
36 | ULONG_MAX / 7,
|
---|
37 | ULONG_MAX / 8,
|
---|
38 | ULONG_MAX / 9,
|
---|
39 | ULONG_MAX / 10,
|
---|
40 | ULONG_MAX / 11,
|
---|
41 | ULONG_MAX / 12,
|
---|
42 | ULONG_MAX / 13,
|
---|
43 | ULONG_MAX / 14,
|
---|
44 | ULONG_MAX / 15,
|
---|
45 | ULONG_MAX / 16,
|
---|
46 | ULONG_MAX / 17,
|
---|
47 | ULONG_MAX / 18,
|
---|
48 | ULONG_MAX / 19,
|
---|
49 | ULONG_MAX / 20,
|
---|
50 | ULONG_MAX / 21,
|
---|
51 | ULONG_MAX / 22,
|
---|
52 | ULONG_MAX / 23,
|
---|
53 | ULONG_MAX / 24,
|
---|
54 | ULONG_MAX / 25,
|
---|
55 | ULONG_MAX / 26,
|
---|
56 | ULONG_MAX / 27,
|
---|
57 | ULONG_MAX / 28,
|
---|
58 | ULONG_MAX / 29,
|
---|
59 | ULONG_MAX / 30,
|
---|
60 | ULONG_MAX / 31,
|
---|
61 | ULONG_MAX / 32,
|
---|
62 | ULONG_MAX / 33,
|
---|
63 | ULONG_MAX / 34,
|
---|
64 | ULONG_MAX / 35,
|
---|
65 | ULONG_MAX / 36,
|
---|
66 | };
|
---|
67 |
|
---|
68 | /* maximum digits that can't ever overflow for bases 2 through 36,
|
---|
69 | * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)].
|
---|
70 | * Note that this is pessimistic if sizeof(long) > 4.
|
---|
71 | */
|
---|
72 | #if SIZEOF_LONG == 4
|
---|
73 | static int digitlimit[] = {
|
---|
74 | 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
|
---|
75 | 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
|
---|
76 | 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
|
---|
77 | 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
|
---|
78 | #elif SIZEOF_LONG == 8
|
---|
79 | /* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
|
---|
80 | static int digitlimit[] = {
|
---|
81 | 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
|
---|
82 | 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
|
---|
83 | 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
|
---|
84 | 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
|
---|
85 | #else
|
---|
86 | #error "Need table for SIZEOF_LONG"
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | /*
|
---|
90 | ** strtoul
|
---|
91 | ** This is a general purpose routine for converting
|
---|
92 | ** an ascii string to an integer in an arbitrary base.
|
---|
93 | ** Leading white space is ignored. If 'base' is zero
|
---|
94 | ** it looks for a leading 0, 0x or 0X to tell which
|
---|
95 | ** base. If these are absent it defaults to 10.
|
---|
96 | ** Base must be 0 or between 2 and 36 (inclusive).
|
---|
97 | ** If 'ptr' is non-NULL it will contain a pointer to
|
---|
98 | ** the end of the scan.
|
---|
99 | ** Errors due to bad pointers will probably result in
|
---|
100 | ** exceptions - we don't check for them.
|
---|
101 | */
|
---|
102 | unsigned long
|
---|
103 | PyOS_strtoul(register char *str, char **ptr, int base)
|
---|
104 | {
|
---|
105 | register unsigned long result = 0; /* return value of the function */
|
---|
106 | register int c; /* current input character */
|
---|
107 | register int ovlimit; /* required digits to overflow */
|
---|
108 |
|
---|
109 | /* skip leading white space */
|
---|
110 | while (*str && isspace(Py_CHARMASK(*str)))
|
---|
111 | ++str;
|
---|
112 |
|
---|
113 | /* check for leading 0 or 0x for auto-base or base 16 */
|
---|
114 | switch (base) {
|
---|
115 | case 0: /* look for leading 0, 0x or 0X */
|
---|
116 | if (*str == '0') {
|
---|
117 | ++str;
|
---|
118 | if (*str == 'x' || *str == 'X') {
|
---|
119 | ++str;
|
---|
120 | base = 16;
|
---|
121 | }
|
---|
122 | else
|
---|
123 | base = 8;
|
---|
124 | }
|
---|
125 | else
|
---|
126 | base = 10;
|
---|
127 | break;
|
---|
128 |
|
---|
129 | case 16: /* skip leading 0x or 0X */
|
---|
130 | if (*str == '0') {
|
---|
131 | ++str;
|
---|
132 | if (*str == 'x' || *str == 'X')
|
---|
133 | ++str;
|
---|
134 | }
|
---|
135 | break;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* catch silly bases */
|
---|
139 | if (base < 2 || base > 36) {
|
---|
140 | if (ptr)
|
---|
141 | *ptr = str;
|
---|
142 | return 0;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* skip leading zeroes */
|
---|
146 | while (*str == '0')
|
---|
147 | ++str;
|
---|
148 |
|
---|
149 | /* base is guaranteed to be in [2, 36] at this point */
|
---|
150 | ovlimit = digitlimit[base];
|
---|
151 |
|
---|
152 | /* do the conversion until non-digit character encountered */
|
---|
153 | while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
|
---|
154 | if (ovlimit > 0) /* no overflow check required */
|
---|
155 | result = result * base + c;
|
---|
156 | else { /* requires overflow check */
|
---|
157 | register unsigned long temp_result;
|
---|
158 |
|
---|
159 | if (ovlimit < 0) /* guaranteed overflow */
|
---|
160 | goto overflowed;
|
---|
161 |
|
---|
162 | /* there could be an overflow */
|
---|
163 | /* check overflow just from shifting */
|
---|
164 | if (result > smallmax[base])
|
---|
165 | goto overflowed;
|
---|
166 |
|
---|
167 | result *= base;
|
---|
168 |
|
---|
169 | /* check overflow from the digit's value */
|
---|
170 | temp_result = result + c;
|
---|
171 | if (temp_result < result)
|
---|
172 | goto overflowed;
|
---|
173 |
|
---|
174 | result = temp_result;
|
---|
175 | }
|
---|
176 |
|
---|
177 | ++str;
|
---|
178 | --ovlimit;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* set pointer to point to the last character scanned */
|
---|
182 | if (ptr)
|
---|
183 | *ptr = str;
|
---|
184 |
|
---|
185 | return result;
|
---|
186 |
|
---|
187 | overflowed:
|
---|
188 | if (ptr) {
|
---|
189 | /* spool through remaining digit characters */
|
---|
190 | while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
|
---|
191 | ++str;
|
---|
192 | *ptr = str;
|
---|
193 | }
|
---|
194 | errno = ERANGE;
|
---|
195 | return (unsigned long)-1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* Checking for overflow in PyOS_strtol is a PITA since C doesn't define
|
---|
199 | * anything about what happens when a signed integer operation overflows,
|
---|
200 | * and some compilers think they're doing you a favor by being "clever"
|
---|
201 | * then. Python assumes a 2's-complement representation, so that the bit
|
---|
202 | * pattern for the largest postive signed long is LONG_MAX, and for
|
---|
203 | * the smallest negative signed long is LONG_MAX + 1.
|
---|
204 | */
|
---|
205 |
|
---|
206 | long
|
---|
207 | PyOS_strtol(char *str, char **ptr, int base)
|
---|
208 | {
|
---|
209 | long result;
|
---|
210 | unsigned long uresult;
|
---|
211 | char sign;
|
---|
212 |
|
---|
213 | while (*str && isspace(Py_CHARMASK(*str)))
|
---|
214 | str++;
|
---|
215 |
|
---|
216 | sign = *str;
|
---|
217 | if (sign == '+' || sign == '-')
|
---|
218 | str++;
|
---|
219 |
|
---|
220 | uresult = PyOS_strtoul(str, ptr, base);
|
---|
221 |
|
---|
222 | if (uresult <= (unsigned long)LONG_MAX) {
|
---|
223 | result = (long)uresult;
|
---|
224 | if (sign == '-')
|
---|
225 | result = -result;
|
---|
226 | }
|
---|
227 | else if (sign == '-' && uresult == (unsigned long)LONG_MAX + 1) {
|
---|
228 | assert(LONG_MIN == -LONG_MAX-1);
|
---|
229 | result = LONG_MIN;
|
---|
230 | }
|
---|
231 | else {
|
---|
232 | errno = ERANGE;
|
---|
233 | result = LONG_MAX;
|
---|
234 | }
|
---|
235 | return result;
|
---|
236 | }
|
---|