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