xref: /openbsd-src/lib/libc/stdlib/strtoll.c (revision 6022c2c21c00ed222314d1a82f05f5778bfe1e32)
1*6022c2c2Smillert /*	$OpenBSD: strtoll.c,v 1.10 2017/07/06 16:23:11 millert Exp $ */
2b252b5f6Sschwarze /*
3c2f18a70Smillert  * Copyright (c) 1992 The Regents of the University of California.
4c2f18a70Smillert  * All rights reserved.
5c2f18a70Smillert  *
6c2f18a70Smillert  * Redistribution and use in source and binary forms, with or without
7c2f18a70Smillert  * modification, are permitted provided that the following conditions
8c2f18a70Smillert  * are met:
9c2f18a70Smillert  * 1. Redistributions of source code must retain the above copyright
10c2f18a70Smillert  *    notice, this list of conditions and the following disclaimer.
11c2f18a70Smillert  * 2. Redistributions in binary form must reproduce the above copyright
12c2f18a70Smillert  *    notice, this list of conditions and the following disclaimer in the
13c2f18a70Smillert  *    documentation and/or other materials provided with the distribution.
146580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
15c2f18a70Smillert  *    may be used to endorse or promote products derived from this software
16c2f18a70Smillert  *    without specific prior written permission.
17c2f18a70Smillert  *
18c2f18a70Smillert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19c2f18a70Smillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20c2f18a70Smillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21c2f18a70Smillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22c2f18a70Smillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23c2f18a70Smillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24c2f18a70Smillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25c2f18a70Smillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26c2f18a70Smillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27c2f18a70Smillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28c2f18a70Smillert  * SUCH DAMAGE.
29c2f18a70Smillert  */
30c2f18a70Smillert 
31c2f18a70Smillert #include <sys/types.h>
32c2f18a70Smillert 
33c2f18a70Smillert #include <ctype.h>
34c2f18a70Smillert #include <errno.h>
35c2f18a70Smillert #include <limits.h>
36c2f18a70Smillert #include <stdlib.h>
37c2f18a70Smillert 
38c2f18a70Smillert /*
39c2f18a70Smillert  * Convert a string to a long long.
40c2f18a70Smillert  *
41c2f18a70Smillert  * Ignores `locale' stuff.  Assumes that the upper and lower case
42c2f18a70Smillert  * alphabets and digits are each contiguous.
43c2f18a70Smillert  */
44c2f18a70Smillert long long
strtoll(const char * nptr,char ** endptr,int base)45d8bc04e4Spat strtoll(const char *nptr, char **endptr, int base)
46c2f18a70Smillert {
47c2f18a70Smillert 	const char *s;
48c2f18a70Smillert 	long long acc, cutoff;
49c2f18a70Smillert 	int c;
50c2f18a70Smillert 	int neg, any, cutlim;
51c2f18a70Smillert 
52c2f18a70Smillert 	/*
53b252b5f6Sschwarze 	 * Ensure that base is between 2 and 36 inclusive, or the special
54b252b5f6Sschwarze 	 * value of 0.
55b252b5f6Sschwarze 	 */
56b252b5f6Sschwarze 	if (base < 0 || base == 1 || base > 36) {
57b252b5f6Sschwarze 		if (endptr != 0)
58b252b5f6Sschwarze 			*endptr = (char *)nptr;
59b252b5f6Sschwarze 		errno = EINVAL;
60b252b5f6Sschwarze 		return 0;
61b252b5f6Sschwarze 	}
62b252b5f6Sschwarze 
63b252b5f6Sschwarze 	/*
64c2f18a70Smillert 	 * Skip white space and pick up leading +/- sign if any.
65c2f18a70Smillert 	 * If base is 0, allow 0x for hex and 0 for octal, else
66c2f18a70Smillert 	 * assume decimal; if base is already 16, allow 0x.
67c2f18a70Smillert 	 */
68c2f18a70Smillert 	s = nptr;
69c2f18a70Smillert 	do {
70c2f18a70Smillert 		c = (unsigned char) *s++;
71c2f18a70Smillert 	} while (isspace(c));
72c2f18a70Smillert 	if (c == '-') {
73c2f18a70Smillert 		neg = 1;
74c2f18a70Smillert 		c = *s++;
75c2f18a70Smillert 	} else {
76c2f18a70Smillert 		neg = 0;
77c2f18a70Smillert 		if (c == '+')
78c2f18a70Smillert 			c = *s++;
79c2f18a70Smillert 	}
80*6022c2c2Smillert 	if ((base == 0 || base == 16) && c == '0' &&
81*6022c2c2Smillert 	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
82c2f18a70Smillert 		c = s[1];
83c2f18a70Smillert 		s += 2;
84c2f18a70Smillert 		base = 16;
85c2f18a70Smillert 	}
86c2f18a70Smillert 	if (base == 0)
87c2f18a70Smillert 		base = c == '0' ? 8 : 10;
88c2f18a70Smillert 
89c2f18a70Smillert 	/*
90c2f18a70Smillert 	 * Compute the cutoff value between legal numbers and illegal
91c2f18a70Smillert 	 * numbers.  That is the largest legal value, divided by the
92c2f18a70Smillert 	 * base.  An input number that is greater than this value, if
93c2f18a70Smillert 	 * followed by a legal input character, is too big.  One that
94c2f18a70Smillert 	 * is equal to this value may be valid or not; the limit
95c2f18a70Smillert 	 * between valid and invalid numbers is then based on the last
96c2f18a70Smillert 	 * digit.  For instance, if the range for long longs is
97c2f18a70Smillert 	 * [-9223372036854775808..9223372036854775807] and the input base
98c2f18a70Smillert 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
99c2f18a70Smillert 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
100c2f18a70Smillert 	 * accumulated a value > 922337203685477580, or equal but the
101c2f18a70Smillert 	 * next digit is > 7 (or 8), the number is too big, and we will
102c2f18a70Smillert 	 * return a range error.
103c2f18a70Smillert 	 *
104c2f18a70Smillert 	 * Set any if any `digits' consumed; make it negative to indicate
105c2f18a70Smillert 	 * overflow.
106c2f18a70Smillert 	 */
107c2f18a70Smillert 	cutoff = neg ? LLONG_MIN : LLONG_MAX;
108c2f18a70Smillert 	cutlim = cutoff % base;
109c2f18a70Smillert 	cutoff /= base;
110c2f18a70Smillert 	if (neg) {
111c2f18a70Smillert 		if (cutlim > 0) {
112c2f18a70Smillert 			cutlim -= base;
113c2f18a70Smillert 			cutoff += 1;
114c2f18a70Smillert 		}
115c2f18a70Smillert 		cutlim = -cutlim;
116c2f18a70Smillert 	}
117c2f18a70Smillert 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
118c2f18a70Smillert 		if (isdigit(c))
119c2f18a70Smillert 			c -= '0';
120c2f18a70Smillert 		else if (isalpha(c))
121c2f18a70Smillert 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
122c2f18a70Smillert 		else
123c2f18a70Smillert 			break;
124c2f18a70Smillert 		if (c >= base)
125c2f18a70Smillert 			break;
126c2f18a70Smillert 		if (any < 0)
127c2f18a70Smillert 			continue;
128c2f18a70Smillert 		if (neg) {
129c2f18a70Smillert 			if (acc < cutoff || (acc == cutoff && c > cutlim)) {
130c2f18a70Smillert 				any = -1;
131c2f18a70Smillert 				acc = LLONG_MIN;
132c2f18a70Smillert 				errno = ERANGE;
133c2f18a70Smillert 			} else {
134c2f18a70Smillert 				any = 1;
135c2f18a70Smillert 				acc *= base;
136c2f18a70Smillert 				acc -= c;
137c2f18a70Smillert 			}
138c2f18a70Smillert 		} else {
139c2f18a70Smillert 			if (acc > cutoff || (acc == cutoff && c > cutlim)) {
140c2f18a70Smillert 				any = -1;
141c2f18a70Smillert 				acc = LLONG_MAX;
142c2f18a70Smillert 				errno = ERANGE;
143c2f18a70Smillert 			} else {
144c2f18a70Smillert 				any = 1;
145c2f18a70Smillert 				acc *= base;
146c2f18a70Smillert 				acc += c;
147c2f18a70Smillert 			}
148c2f18a70Smillert 		}
149c2f18a70Smillert 	}
150c2f18a70Smillert 	if (endptr != 0)
151c2f18a70Smillert 		*endptr = (char *) (any ? s - 1 : nptr);
152c2f18a70Smillert 	return (acc);
153c2f18a70Smillert }
1540d943ef0Sguenther DEF_STRONG(strtoll);
155c2f18a70Smillert 
1560d943ef0Sguenther __weak_alias(strtoq, strtoll);
157