1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/* A wrapper around strtol() and strtoul() to correct some
* "out of bounds" cases that don't work well on at least UTS.
* If a value is Larger than the max, strto[u]l should return
* the max value, and set errno to ERANGE
* The same if a value is smaller than the min value (only
* relevant for strtol(); not strtoul()), except the minimum
* value is returned (and errno == ERANGE).
*/
#include <ctype.h>
#include <string.h>
#include <sys/errno.h>
#include <stdlib.h>
extern int errno;
#undef I32
#undef U32
#define I32 int
#define U32 unsigned int
struct base_info {
char *ValidChars;
char *Ulong_max_str;
char *Long_max_str;
char *Long_min_str; /* Absolute value */
int Ulong_max_str_len;
int Long_max_str_len;
int Long_min_str_len; /* Absolute value */
U32 Ulong_max;
I32 Long_max;
I32 Long_min; /* NOT Absolute value */
};
static struct base_info Base_info[37];
static struct base_info Base_info_16 = {
"0123456789abcdefABCDEF",
"4294967295", "2147483648" /* <== ABS VAL */ , "2147483647",
10, 10, 10,
4294967295, 2147483647, - 2147483648,
};
static struct base_info Base_info_10 = {
"0123456789",
"4294967295", "2147483648" /* <== ABS VAL */ , "2147483647",
10, 10, 10,
4294967295, 2147483647, - 2147483648,
};
/* Used eventually (if this is fully developed) to hold info
* for processing bases 2-36. So that we can just plug the
* base in as a selector for its info, we sacrifice
* Base_info[0] and Base_info[1] (unless they are used
* at some point for special information).
*/
/* This may be replaced later by something more universal */
static void
init_Base_info()
{
if(Base_info[10].ValidChars) return;
Base_info[10] = Base_info_10;
Base_info[16] = Base_info_16;
}
unsigned int
strtoul_wrap32(char *s, char **pEnd, int base)
{
int Len;
int isNegated = 0;
char *sOrig = s;
init_Base_info();
while(*s && isspace(*s)) ++s;
if(*s == '-') {
++isNegated;
++s;
while(*s && isspace(*s)) ++s;
}
if(base == 0) {
if(*s == '0') {
if(s[1] == 'x' || s[1] == 'X') {
s += 2;
base = 16;
} else {
++s;
base = 8;
}
} else if(isdigit(*s)) {
base = 10;
}
}
if(base != 10) {
return strtoul(sOrig, pEnd, base);
}
Len = strspn(s, Base_info[base].ValidChars);
if(Len > Base_info[base].Ulong_max_str_len
||
(Len == Base_info[base].Ulong_max_str_len
&&
strncmp(Base_info[base].Ulong_max_str, s, Len) < 0)
) {
/* In case isNegated is set - what to do?? */
/* Mightn't we say a negative number is ERANGE for strtoul? */
errno = ERANGE;
return Base_info[base].Ulong_max;
}
return strtoul(sOrig, pEnd, base);
}
int
strtol_wrap32(char *s, char **pEnd, int base)
{
int Len;
int isNegated = 0;
char *sOrig = s;
init_Base_info();
while(*s && isspace(*s)) ++s;
if(*s == '-') {
++isNegated;
++s;
while(*s && isspace(*s)) ++s;
}
if(base == 0) {
if(*s == '0') {
if(s[1] == 'x' || s[1] == 'X') {
s += 2;
base = 16;
} else {
++s;
base = 8;
}
} else if(isdigit(*s)) {
base = 10;
}
}
if(base != 10) {
return strtol(sOrig, pEnd, base);
}
Len = strspn(s, Base_info[base].ValidChars);
if(Len > Base_info[base].Long_max_str_len
||
(!isNegated && Len == Base_info[base].Long_max_str_len
&&
strncmp(Base_info[base].Long_max_str, s, Len) < 0)
||
(isNegated && Len == Base_info[base].Long_min_str_len
&&
strncmp(Base_info[base].Long_min_str, s, Len) < 0)
) {
/* In case isNegated is set - what to do?? */
/* Mightn't we say a negative number is ERANGE for strtol? */
errno = ERANGE;
return(isNegated ? Base_info[base].Long_min
:
Base_info[base].Long_min);
}
return strtol(sOrig, pEnd, base);
}
|