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