1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*0Sstevel@tonic-gate * All rights reserved.
4*0Sstevel@tonic-gate * Copyright (c) 1992
5*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
6*0Sstevel@tonic-gate *
7*0Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
8*0Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
9*0Sstevel@tonic-gate * the sendmail distribution.
10*0Sstevel@tonic-gate */
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate #include <sm/gen.h>
15*0Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: strto.c,v 1.18 2001/12/30 04:59:37 gshapiro Exp $")
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate #include <sys/param.h>
18*0Sstevel@tonic-gate #include <sys/types.h>
19*0Sstevel@tonic-gate #include <stdlib.h>
20*0Sstevel@tonic-gate #include <ctype.h>
21*0Sstevel@tonic-gate #include <errno.h>
22*0Sstevel@tonic-gate #include <sm/limits.h>
23*0Sstevel@tonic-gate #include <sm/conf.h>
24*0Sstevel@tonic-gate #include <sm/string.h>
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate ** SM_STRTOLL -- Convert a string to a (signed) long long integer.
28*0Sstevel@tonic-gate **
29*0Sstevel@tonic-gate ** Ignores `locale' stuff. Assumes that the upper and lower case
30*0Sstevel@tonic-gate ** alphabets and digits are each contiguous.
31*0Sstevel@tonic-gate **
32*0Sstevel@tonic-gate ** Parameters:
33*0Sstevel@tonic-gate ** nptr -- string containing number
34*0Sstevel@tonic-gate ** endptr -- location of first invalid character
35*0Sstevel@tonic-gate ** base -- numeric base that 'nptr' number is based in
36*0Sstevel@tonic-gate **
37*0Sstevel@tonic-gate ** Returns:
38*0Sstevel@tonic-gate ** Failure: on underflow LLONG_MIN is returned; on overflow
39*0Sstevel@tonic-gate ** LLONG_MAX is returned and errno is set.
40*0Sstevel@tonic-gate ** When 'endptr' == '\0' then the entire string 'nptr'
41*0Sstevel@tonic-gate ** was valid.
42*0Sstevel@tonic-gate ** Success: returns the converted number
43*0Sstevel@tonic-gate */
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate LONGLONG_T
46*0Sstevel@tonic-gate sm_strtoll(nptr, endptr, base)
47*0Sstevel@tonic-gate const char *nptr;
48*0Sstevel@tonic-gate char **endptr;
49*0Sstevel@tonic-gate register int base;
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate register bool neg;
52*0Sstevel@tonic-gate register const char *s;
53*0Sstevel@tonic-gate register LONGLONG_T acc, cutoff;
54*0Sstevel@tonic-gate register int c;
55*0Sstevel@tonic-gate register int any, cutlim;
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate ** Skip white space and pick up leading +/- sign if any.
59*0Sstevel@tonic-gate ** If base is 0, allow 0x for hex and 0 for octal, else
60*0Sstevel@tonic-gate ** assume decimal; if base is already 16, allow 0x.
61*0Sstevel@tonic-gate */
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate s = nptr;
64*0Sstevel@tonic-gate do
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate c = (unsigned char) *s++;
67*0Sstevel@tonic-gate } while (isascii(c) && isspace(c));
68*0Sstevel@tonic-gate if (c == '-')
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate neg = true;
71*0Sstevel@tonic-gate c = *s++;
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate else
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate neg = false;
76*0Sstevel@tonic-gate if (c == '+')
77*0Sstevel@tonic-gate c = *s++;
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate if ((base == 0 || base == 16) &&
80*0Sstevel@tonic-gate c == '0' && (*s == 'x' || *s == 'X'))
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate c = s[1];
83*0Sstevel@tonic-gate s += 2;
84*0Sstevel@tonic-gate base = 16;
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate if (base == 0)
87*0Sstevel@tonic-gate base = c == '0' ? 8 : 10;
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate /*
90*0Sstevel@tonic-gate ** Compute the cutoff value between legal numbers and illegal
91*0Sstevel@tonic-gate ** numbers. That is the largest legal value, divided by the
92*0Sstevel@tonic-gate ** base. An input number that is greater than this value, if
93*0Sstevel@tonic-gate ** followed by a legal input character, is too big. One that
94*0Sstevel@tonic-gate ** is equal to this value may be valid or not; the limit
95*0Sstevel@tonic-gate ** between valid and invalid numbers is then based on the last
96*0Sstevel@tonic-gate ** digit. For instance, if the range for long-long's is
97*0Sstevel@tonic-gate ** [-9223372036854775808..9223372036854775807] and the input base
98*0Sstevel@tonic-gate ** is 10, cutoff will be set to 922337203685477580 and cutlim to
99*0Sstevel@tonic-gate ** either 7 (!neg) or 8 (neg), meaning that if we have
100*0Sstevel@tonic-gate ** accumulated a value > 922337203685477580, or equal but the
101*0Sstevel@tonic-gate ** next digit is > 7 (or 8), the number is too big, and we will
102*0Sstevel@tonic-gate ** return a range error.
103*0Sstevel@tonic-gate **
104*0Sstevel@tonic-gate ** Set any if any `digits' consumed; make it negative to indicate
105*0Sstevel@tonic-gate ** overflow.
106*0Sstevel@tonic-gate */
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate cutoff = neg ? LLONG_MIN : LLONG_MAX;
109*0Sstevel@tonic-gate cutlim = cutoff % base;
110*0Sstevel@tonic-gate cutoff /= base;
111*0Sstevel@tonic-gate if (neg)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate if (cutlim > 0)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate cutlim -= base;
116*0Sstevel@tonic-gate cutoff += 1;
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate cutlim = -cutlim;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate for (acc = 0, any = 0;; c = (unsigned char) *s++)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate if (isascii(c) && isdigit(c))
123*0Sstevel@tonic-gate c -= '0';
124*0Sstevel@tonic-gate else if (isascii(c) && isalpha(c))
125*0Sstevel@tonic-gate c -= isupper(c) ? 'A' - 10 : 'a' - 10;
126*0Sstevel@tonic-gate else
127*0Sstevel@tonic-gate break;
128*0Sstevel@tonic-gate if (c >= base)
129*0Sstevel@tonic-gate break;
130*0Sstevel@tonic-gate if (any < 0)
131*0Sstevel@tonic-gate continue;
132*0Sstevel@tonic-gate if (neg)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate if (acc < cutoff || (acc == cutoff && c > cutlim))
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate any = -1;
137*0Sstevel@tonic-gate acc = LLONG_MIN;
138*0Sstevel@tonic-gate errno = ERANGE;
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate else
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate any = 1;
143*0Sstevel@tonic-gate acc *= base;
144*0Sstevel@tonic-gate acc -= c;
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate else
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate if (acc > cutoff || (acc == cutoff && c > cutlim))
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate any = -1;
152*0Sstevel@tonic-gate acc = LLONG_MAX;
153*0Sstevel@tonic-gate errno = ERANGE;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate else
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate any = 1;
158*0Sstevel@tonic-gate acc *= base;
159*0Sstevel@tonic-gate acc += c;
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate if (endptr != 0)
164*0Sstevel@tonic-gate *endptr = (char *) (any ? s - 1 : nptr);
165*0Sstevel@tonic-gate return acc;
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate ** SM_STRTOULL -- Convert a string to an unsigned long long integer.
170*0Sstevel@tonic-gate **
171*0Sstevel@tonic-gate ** Ignores `locale' stuff. Assumes that the upper and lower case
172*0Sstevel@tonic-gate ** alphabets and digits are each contiguous.
173*0Sstevel@tonic-gate **
174*0Sstevel@tonic-gate ** Parameters:
175*0Sstevel@tonic-gate ** nptr -- string containing (unsigned) number
176*0Sstevel@tonic-gate ** endptr -- location of first invalid character
177*0Sstevel@tonic-gate ** base -- numeric base that 'nptr' number is based in
178*0Sstevel@tonic-gate **
179*0Sstevel@tonic-gate ** Returns:
180*0Sstevel@tonic-gate ** Failure: on overflow ULLONG_MAX is returned and errno is set.
181*0Sstevel@tonic-gate ** When 'endptr' == '\0' then the entire string 'nptr'
182*0Sstevel@tonic-gate ** was valid.
183*0Sstevel@tonic-gate ** Success: returns the converted number
184*0Sstevel@tonic-gate */
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate ULONGLONG_T
sm_strtoull(nptr,endptr,base)187*0Sstevel@tonic-gate sm_strtoull(nptr, endptr, base)
188*0Sstevel@tonic-gate const char *nptr;
189*0Sstevel@tonic-gate char **endptr;
190*0Sstevel@tonic-gate register int base;
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate register const char *s;
193*0Sstevel@tonic-gate register ULONGLONG_T acc, cutoff;
194*0Sstevel@tonic-gate register int c;
195*0Sstevel@tonic-gate register bool neg;
196*0Sstevel@tonic-gate register int any, cutlim;
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate /* See sm_strtoll for comments as to the logic used. */
199*0Sstevel@tonic-gate s = nptr;
200*0Sstevel@tonic-gate do
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate c = (unsigned char) *s++;
203*0Sstevel@tonic-gate } while (isascii(c) && isspace(c));
204*0Sstevel@tonic-gate neg = (c == '-');
205*0Sstevel@tonic-gate if (neg)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate c = *s++;
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate else
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate if (c == '+')
212*0Sstevel@tonic-gate c = *s++;
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate if ((base == 0 || base == 16) &&
215*0Sstevel@tonic-gate c == '0' && (*s == 'x' || *s == 'X'))
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate c = s[1];
218*0Sstevel@tonic-gate s += 2;
219*0Sstevel@tonic-gate base = 16;
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate if (base == 0)
222*0Sstevel@tonic-gate base = c == '0' ? 8 : 10;
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate cutoff = ULLONG_MAX / (ULONGLONG_T)base;
225*0Sstevel@tonic-gate cutlim = ULLONG_MAX % (ULONGLONG_T)base;
226*0Sstevel@tonic-gate for (acc = 0, any = 0;; c = (unsigned char) *s++)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate if (isascii(c) && isdigit(c))
229*0Sstevel@tonic-gate c -= '0';
230*0Sstevel@tonic-gate else if (isascii(c) && isalpha(c))
231*0Sstevel@tonic-gate c -= isupper(c) ? 'A' - 10 : 'a' - 10;
232*0Sstevel@tonic-gate else
233*0Sstevel@tonic-gate break;
234*0Sstevel@tonic-gate if (c >= base)
235*0Sstevel@tonic-gate break;
236*0Sstevel@tonic-gate if (any < 0)
237*0Sstevel@tonic-gate continue;
238*0Sstevel@tonic-gate if (acc > cutoff || (acc == cutoff && c > cutlim))
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate any = -1;
241*0Sstevel@tonic-gate acc = ULLONG_MAX;
242*0Sstevel@tonic-gate errno = ERANGE;
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate else
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate any = 1;
247*0Sstevel@tonic-gate acc *= (ULONGLONG_T)base;
248*0Sstevel@tonic-gate acc += c;
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate if (neg && any > 0)
252*0Sstevel@tonic-gate acc = -((LONGLONG_T) acc);
253*0Sstevel@tonic-gate if (endptr != 0)
254*0Sstevel@tonic-gate *endptr = (char *) (any ? s - 1 : nptr);
255*0Sstevel@tonic-gate return acc;
256*0Sstevel@tonic-gate }
257