15af32e75SAxel Dörfler /*-
2*bc328a43SAugustin Cavalier * SPDX-License-Identifier: BSD-3-Clause
3*bc328a43SAugustin Cavalier *
45af32e75SAxel Dörfler * Copyright (c) 1992, 1993
55af32e75SAxel Dörfler * The Regents of the University of California. All rights reserved.
65af32e75SAxel Dörfler *
7*bc328a43SAugustin Cavalier * Copyright (c) 2011 The FreeBSD Foundation
8*bc328a43SAugustin Cavalier *
9*bc328a43SAugustin Cavalier * Portions of this software were developed by David Chisnall
10*bc328a43SAugustin Cavalier * under sponsorship from the FreeBSD Foundation.
11*bc328a43SAugustin Cavalier *
125af32e75SAxel Dörfler * Redistribution and use in source and binary forms, with or without
135af32e75SAxel Dörfler * modification, are permitted provided that the following conditions
145af32e75SAxel Dörfler * are met:
155af32e75SAxel Dörfler * 1. Redistributions of source code must retain the above copyright
165af32e75SAxel Dörfler * notice, this list of conditions and the following disclaimer.
175af32e75SAxel Dörfler * 2. Redistributions in binary form must reproduce the above copyright
185af32e75SAxel Dörfler * notice, this list of conditions and the following disclaimer in the
195af32e75SAxel Dörfler * documentation and/or other materials provided with the distribution.
20*bc328a43SAugustin Cavalier * 3. Neither the name of the University nor the names of its contributors
215af32e75SAxel Dörfler * may be used to endorse or promote products derived from this software
225af32e75SAxel Dörfler * without specific prior written permission.
235af32e75SAxel Dörfler *
245af32e75SAxel Dörfler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
255af32e75SAxel Dörfler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
265af32e75SAxel Dörfler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
275af32e75SAxel Dörfler * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
285af32e75SAxel Dörfler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
295af32e75SAxel Dörfler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
305af32e75SAxel Dörfler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
315af32e75SAxel Dörfler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
325af32e75SAxel Dörfler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
335af32e75SAxel Dörfler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
345af32e75SAxel Dörfler * SUCH DAMAGE.
355af32e75SAxel Dörfler */
365af32e75SAxel Dörfler
375af32e75SAxel Dörfler
385af32e75SAxel Dörfler #include <limits.h>
395af32e75SAxel Dörfler #include <errno.h>
405af32e75SAxel Dörfler #include <ctype.h>
415af32e75SAxel Dörfler #include <stdlib.h>
425af32e75SAxel Dörfler
435af32e75SAxel Dörfler /*
445af32e75SAxel Dörfler * Convert a string to a long long integer.
455af32e75SAxel Dörfler *
465af32e75SAxel Dörfler * Assumes that the upper and lower case
475af32e75SAxel Dörfler * alphabets and digits are each contiguous.
485af32e75SAxel Dörfler */
495af32e75SAxel Dörfler long long
strtoll(const char * __restrict nptr,char ** __restrict endptr,int base)505af32e75SAxel Dörfler strtoll(const char * __restrict nptr, char ** __restrict endptr, int base)
515af32e75SAxel Dörfler {
525af32e75SAxel Dörfler const char *s;
535af32e75SAxel Dörfler unsigned long long acc;
545af32e75SAxel Dörfler char c;
555af32e75SAxel Dörfler unsigned long long cutoff;
565af32e75SAxel Dörfler int neg, any, cutlim;
575af32e75SAxel Dörfler
585af32e75SAxel Dörfler /*
595af32e75SAxel Dörfler * Skip white space and pick up leading +/- sign if any.
60*bc328a43SAugustin Cavalier * If base is 0, allow 0b for binary, 0x for hex, and 0 for
61*bc328a43SAugustin Cavalier * octal, else assume decimal; if base is already 2, allow
62*bc328a43SAugustin Cavalier * 0b; if base is already 16, allow 0x.
635af32e75SAxel Dörfler */
645af32e75SAxel Dörfler s = nptr;
655af32e75SAxel Dörfler do {
665af32e75SAxel Dörfler c = *s++;
675af32e75SAxel Dörfler } while (isspace((unsigned char)c));
685af32e75SAxel Dörfler if (c == '-') {
695af32e75SAxel Dörfler neg = 1;
705af32e75SAxel Dörfler c = *s++;
715af32e75SAxel Dörfler } else {
725af32e75SAxel Dörfler neg = 0;
735af32e75SAxel Dörfler if (c == '+')
745af32e75SAxel Dörfler c = *s++;
755af32e75SAxel Dörfler }
765af32e75SAxel Dörfler if ((base == 0 || base == 16) &&
77*bc328a43SAugustin Cavalier c == '0' && (*s == 'x' || *s == 'X') &&
78*bc328a43SAugustin Cavalier ((s[1] >= '0' && s[1] <= '9') ||
79*bc328a43SAugustin Cavalier (s[1] >= 'A' && s[1] <= 'F') ||
80*bc328a43SAugustin Cavalier (s[1] >= 'a' && s[1] <= 'f'))) {
815af32e75SAxel Dörfler c = s[1];
825af32e75SAxel Dörfler s += 2;
835af32e75SAxel Dörfler base = 16;
845af32e75SAxel Dörfler }
85*bc328a43SAugustin Cavalier if ((base == 0 || base == 2) &&
86*bc328a43SAugustin Cavalier c == '0' && (*s == 'b' || *s == 'B') &&
87*bc328a43SAugustin Cavalier (s[1] >= '0' && s[1] <= '1')) {
88*bc328a43SAugustin Cavalier c = s[1];
89*bc328a43SAugustin Cavalier s += 2;
90*bc328a43SAugustin Cavalier base = 2;
91*bc328a43SAugustin Cavalier }
925af32e75SAxel Dörfler if (base == 0)
935af32e75SAxel Dörfler base = c == '0' ? 8 : 10;
945af32e75SAxel Dörfler acc = any = 0;
955af32e75SAxel Dörfler if (base < 2 || base > 36)
965af32e75SAxel Dörfler goto noconv;
975af32e75SAxel Dörfler
985af32e75SAxel Dörfler /*
995af32e75SAxel Dörfler * Compute the cutoff value between legal numbers and illegal
1005af32e75SAxel Dörfler * numbers. That is the largest legal value, divided by the
1015af32e75SAxel Dörfler * base. An input number that is greater than this value, if
1025af32e75SAxel Dörfler * followed by a legal input character, is too big. One that
1035af32e75SAxel Dörfler * is equal to this value may be valid or not; the limit
1045af32e75SAxel Dörfler * between valid and invalid numbers is then based on the last
1055af32e75SAxel Dörfler * digit. For instance, if the range for quads is
1065af32e75SAxel Dörfler * [-9223372036854775808..9223372036854775807] and the input base
1075af32e75SAxel Dörfler * is 10, cutoff will be set to 922337203685477580 and cutlim to
1085af32e75SAxel Dörfler * either 7 (neg==0) or 8 (neg==1), meaning that if we have
1095af32e75SAxel Dörfler * accumulated a value > 922337203685477580, or equal but the
1105af32e75SAxel Dörfler * next digit is > 7 (or 8), the number is too big, and we will
1115af32e75SAxel Dörfler * return a range error.
1125af32e75SAxel Dörfler *
1135af32e75SAxel Dörfler * Set 'any' if any `digits' consumed; make it negative to indicate
1145af32e75SAxel Dörfler * overflow.
1155af32e75SAxel Dörfler */
116*bc328a43SAugustin Cavalier cutoff = neg ? (unsigned long long)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX
117*bc328a43SAugustin Cavalier : LLONG_MAX;
1185af32e75SAxel Dörfler cutlim = cutoff % base;
1195af32e75SAxel Dörfler cutoff /= base;
1205af32e75SAxel Dörfler for ( ; ; c = *s++) {
1215af32e75SAxel Dörfler if (c >= '0' && c <= '9')
1225af32e75SAxel Dörfler c -= '0';
1235af32e75SAxel Dörfler else if (c >= 'A' && c <= 'Z')
1245af32e75SAxel Dörfler c -= 'A' - 10;
1255af32e75SAxel Dörfler else if (c >= 'a' && c <= 'z')
1265af32e75SAxel Dörfler c -= 'a' - 10;
1275af32e75SAxel Dörfler else
1285af32e75SAxel Dörfler break;
1295af32e75SAxel Dörfler if (c >= base)
1305af32e75SAxel Dörfler break;
1315af32e75SAxel Dörfler if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1325af32e75SAxel Dörfler any = -1;
1335af32e75SAxel Dörfler else {
1345af32e75SAxel Dörfler any = 1;
1355af32e75SAxel Dörfler acc *= base;
1365af32e75SAxel Dörfler acc += c;
1375af32e75SAxel Dörfler }
1385af32e75SAxel Dörfler }
1395af32e75SAxel Dörfler if (any < 0) {
140*bc328a43SAugustin Cavalier acc = neg ? LLONG_MIN : LLONG_MAX;
141*bc328a43SAugustin Cavalier errno = ERANGE;
1425af32e75SAxel Dörfler } else if (!any) {
1435af32e75SAxel Dörfler noconv:
144*bc328a43SAugustin Cavalier errno = EINVAL;
1455af32e75SAxel Dörfler } else if (neg)
1465af32e75SAxel Dörfler acc = -acc;
1475af32e75SAxel Dörfler if (endptr != NULL)
1485af32e75SAxel Dörfler *endptr = (char *)(any ? s - 1 : nptr);
149*bc328a43SAugustin Cavalier return (acc);
1505af32e75SAxel Dörfler }
1515af32e75SAxel Dörfler
1525af32e75SAxel Dörfler
153*bc328a43SAugustin Cavalier #ifdef __HAIKU__
1545af32e75SAxel Dörfler long long __strtoll_internal(const char *number, char **_end, int base, int group);
1555af32e75SAxel Dörfler
1565af32e75SAxel Dörfler long long
__strtoll_internal(const char * number,char ** _end,int base,int group)1575af32e75SAxel Dörfler __strtoll_internal(const char *number, char **_end, int base, int group)
1585af32e75SAxel Dörfler {
1595af32e75SAxel Dörfler // ToDo: group is currently not supported!
1605af32e75SAxel Dörfler (void)group;
1615af32e75SAxel Dörfler
1625af32e75SAxel Dörfler return strtoll(number, _end, base);
1635af32e75SAxel Dörfler }
164*bc328a43SAugustin Cavalier #endif
165