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