1*5ba6b03cSchristos /*-
2*5ba6b03cSchristos  * Copyright (c) 2014 The Regents of the University of California.
3*5ba6b03cSchristos  * All rights reserved.
4*5ba6b03cSchristos  *
5*5ba6b03cSchristos  * Redistribution and use in source and binary forms, with or without
6*5ba6b03cSchristos  * modification, are permitted provided that the following conditions
7*5ba6b03cSchristos  * are met:
8*5ba6b03cSchristos  * 1. Redistributions of source code must retain the above copyright
9*5ba6b03cSchristos  *    notice, this list of conditions and the following disclaimer.
10*5ba6b03cSchristos  * 2. Redistributions in binary form must reproduce the above copyright
11*5ba6b03cSchristos  *    notice, this list of conditions and the following disclaimer in the
12*5ba6b03cSchristos  *    documentation and/or other materials provided with the distribution.
13*5ba6b03cSchristos  * 3. [rescinded 22 July 1999]
14*5ba6b03cSchristos  * 4. Neither the name of the University nor the names of its contributors
15*5ba6b03cSchristos  *    may be used to endorse or promote products derived from this software
16*5ba6b03cSchristos  *    without specific prior written permission.
17*5ba6b03cSchristos  *
18*5ba6b03cSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*5ba6b03cSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*5ba6b03cSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*5ba6b03cSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*5ba6b03cSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*5ba6b03cSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*5ba6b03cSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*5ba6b03cSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*5ba6b03cSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*5ba6b03cSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*5ba6b03cSchristos  * SUCH DAMAGE.
29*5ba6b03cSchristos  */
30*5ba6b03cSchristos 
31*5ba6b03cSchristos /*
32*5ba6b03cSchristos 
33*5ba6b03cSchristos @deftypefn Supplemental {long long int} strtoll (const char *@var{string}, @
34*5ba6b03cSchristos   char **@var{endptr}, int @var{base})
35*5ba6b03cSchristos @deftypefnx Supplemental {unsigned long long int} strtoul (@
36*5ba6b03cSchristos   const char *@var{string}, char **@var{endptr}, int @var{base})
37*5ba6b03cSchristos 
38*5ba6b03cSchristos The @code{strtoll} function converts the string in @var{string} to a
39*5ba6b03cSchristos long long integer value according to the given @var{base}, which must be
40*5ba6b03cSchristos between 2 and 36 inclusive, or be the special value 0.  If @var{base}
41*5ba6b03cSchristos is 0, @code{strtoll} will look for the prefixes @code{0} and @code{0x}
42*5ba6b03cSchristos to indicate bases 8 and 16, respectively, else default to base 10.
43*5ba6b03cSchristos When the base is 16 (either explicitly or implicitly), a prefix of
44*5ba6b03cSchristos @code{0x} is allowed.  The handling of @var{endptr} is as that of
45*5ba6b03cSchristos @code{strtod} above.  The @code{strtoull} function is the same, except
46*5ba6b03cSchristos that the converted value is unsigned.
47*5ba6b03cSchristos 
48*5ba6b03cSchristos @end deftypefn
49*5ba6b03cSchristos 
50*5ba6b03cSchristos */
51*5ba6b03cSchristos 
52*5ba6b03cSchristos #ifdef HAVE_CONFIG_H
53*5ba6b03cSchristos #include "config.h"
54*5ba6b03cSchristos #endif
55*5ba6b03cSchristos #ifdef HAVE_LIMITS_H
56*5ba6b03cSchristos #include <limits.h>
57*5ba6b03cSchristos #endif
58*5ba6b03cSchristos #ifdef HAVE_SYS_PARAM_H
59*5ba6b03cSchristos #include <sys/param.h>
60*5ba6b03cSchristos #endif
61*5ba6b03cSchristos #include <errno.h>
62*5ba6b03cSchristos #ifdef NEED_DECLARATION_ERRNO
63*5ba6b03cSchristos extern int errno;
64*5ba6b03cSchristos #endif
65*5ba6b03cSchristos #include "safe-ctype.h"
66*5ba6b03cSchristos 
67*5ba6b03cSchristos #ifdef HAVE_LONG_LONG
68*5ba6b03cSchristos 
69*5ba6b03cSchristos __extension__
70*5ba6b03cSchristos typedef unsigned long long ullong_type;
71*5ba6b03cSchristos 
72*5ba6b03cSchristos __extension__
73*5ba6b03cSchristos typedef long long llong_type;
74*5ba6b03cSchristos 
75*5ba6b03cSchristos /* FIXME: It'd be nice to configure around these, but the include files are too
76*5ba6b03cSchristos    painful.  These macros should at least be more portable than hardwired hex
77*5ba6b03cSchristos    constants. */
78*5ba6b03cSchristos 
79*5ba6b03cSchristos #ifndef ULLONG_MAX
80*5ba6b03cSchristos #define ULLONG_MAX (~(ullong_type)0) /* 0xFFFFFFFFFFFFFFFF */
81*5ba6b03cSchristos #endif
82*5ba6b03cSchristos 
83*5ba6b03cSchristos #ifndef LLONG_MAX
84*5ba6b03cSchristos #define LLONG_MAX ((llong_type)(ULLONG_MAX >> 1)) /* 0x7FFFFFFFFFFFFFFF */
85*5ba6b03cSchristos #endif
86*5ba6b03cSchristos 
87*5ba6b03cSchristos #ifndef LLONG_MIN
88*5ba6b03cSchristos #define LLONG_MIN (~LLONG_MAX) /* 0x8000000000000000 */
89*5ba6b03cSchristos #endif
90*5ba6b03cSchristos 
91*5ba6b03cSchristos /*
92*5ba6b03cSchristos  * Convert a string to a long long integer.
93*5ba6b03cSchristos  *
94*5ba6b03cSchristos  * Ignores `locale' stuff.  Assumes that the upper and lower case
95*5ba6b03cSchristos  * alphabets and digits are each contiguous.
96*5ba6b03cSchristos  */
97*5ba6b03cSchristos llong_type
strtoll(const char * nptr,char ** endptr,register int base)98*5ba6b03cSchristos strtoll(const char *nptr, char **endptr, register int base)
99*5ba6b03cSchristos {
100*5ba6b03cSchristos 	register const char *s = nptr;
101*5ba6b03cSchristos 	register ullong_type acc;
102*5ba6b03cSchristos 	register int c;
103*5ba6b03cSchristos 	register ullong_type cutoff;
104*5ba6b03cSchristos 	register int neg = 0, any, cutlim;
105*5ba6b03cSchristos 
106*5ba6b03cSchristos 	/*
107*5ba6b03cSchristos 	 * Skip white space and pick up leading +/- sign if any.
108*5ba6b03cSchristos 	 * If base is 0, allow 0x for hex and 0 for octal, else
109*5ba6b03cSchristos 	 * assume decimal; if base is already 16, allow 0x.
110*5ba6b03cSchristos 	 */
111*5ba6b03cSchristos 	do {
112*5ba6b03cSchristos 		c = *s++;
113*5ba6b03cSchristos 	} while (ISSPACE(c));
114*5ba6b03cSchristos 	if (c == '-') {
115*5ba6b03cSchristos 		neg = 1;
116*5ba6b03cSchristos 		c = *s++;
117*5ba6b03cSchristos 	} else if (c == '+')
118*5ba6b03cSchristos 		c = *s++;
119*5ba6b03cSchristos 	if ((base == 0 || base == 16) &&
120*5ba6b03cSchristos 	    c == '0' && (*s == 'x' || *s == 'X')) {
121*5ba6b03cSchristos 		c = s[1];
122*5ba6b03cSchristos 		s += 2;
123*5ba6b03cSchristos 		base = 16;
124*5ba6b03cSchristos 	}
125*5ba6b03cSchristos 	if (base == 0)
126*5ba6b03cSchristos 		base = c == '0' ? 8 : 10;
127*5ba6b03cSchristos 
128*5ba6b03cSchristos 	/*
129*5ba6b03cSchristos 	 * Compute the cutoff value between legal numbers and illegal
130*5ba6b03cSchristos 	 * numbers.  That is the largest legal value, divided by the
131*5ba6b03cSchristos 	 * base.  An input number that is greater than this value, if
132*5ba6b03cSchristos 	 * followed by a legal input character, is too big.  One that
133*5ba6b03cSchristos 	 * is equal to this value may be valid or not; the limit
134*5ba6b03cSchristos 	 * between valid and invalid numbers is then based on the last
135*5ba6b03cSchristos 	 * digit.  For instance, if the range for longs is
136*5ba6b03cSchristos 	 * [-2147483648..2147483647] and the input base is 10,
137*5ba6b03cSchristos 	 * cutoff will be set to 214748364 and cutlim to either
138*5ba6b03cSchristos 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
139*5ba6b03cSchristos 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
140*5ba6b03cSchristos 	 * the number is too big, and we will return a range error.
141*5ba6b03cSchristos 	 *
142*5ba6b03cSchristos 	 * Set any if any `digits' consumed; make it negative to indicate
143*5ba6b03cSchristos 	 * overflow.
144*5ba6b03cSchristos 	 */
145*5ba6b03cSchristos 	cutoff = neg ? -(ullong_type)LLONG_MIN : LLONG_MAX;
146*5ba6b03cSchristos 	cutlim = cutoff % (ullong_type)base;
147*5ba6b03cSchristos 	cutoff /= (ullong_type)base;
148*5ba6b03cSchristos 	for (acc = 0, any = 0;; c = *s++) {
149*5ba6b03cSchristos 		if (ISDIGIT(c))
150*5ba6b03cSchristos 			c -= '0';
151*5ba6b03cSchristos 		else if (ISALPHA(c))
152*5ba6b03cSchristos 			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
153*5ba6b03cSchristos 		else
154*5ba6b03cSchristos 			break;
155*5ba6b03cSchristos 		if (c >= base)
156*5ba6b03cSchristos 			break;
157*5ba6b03cSchristos 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
158*5ba6b03cSchristos 			any = -1;
159*5ba6b03cSchristos 		else {
160*5ba6b03cSchristos 			any = 1;
161*5ba6b03cSchristos 			acc *= base;
162*5ba6b03cSchristos 			acc += c;
163*5ba6b03cSchristos 		}
164*5ba6b03cSchristos 	}
165*5ba6b03cSchristos 	if (any < 0) {
166*5ba6b03cSchristos 		acc = neg ? LLONG_MIN : LLONG_MAX;
167*5ba6b03cSchristos 		errno = ERANGE;
168*5ba6b03cSchristos 	} else if (neg)
169*5ba6b03cSchristos 		acc = -acc;
170*5ba6b03cSchristos 	if (endptr != 0)
171*5ba6b03cSchristos 		*endptr = (char *) (any ? s - 1 : nptr);
172*5ba6b03cSchristos 	return (acc);
173*5ba6b03cSchristos }
174*5ba6b03cSchristos 
175*5ba6b03cSchristos #endif /* ifdef HAVE_LONG_LONG */
176