xref: /openbsd-src/lib/libc/stdlib/strtoull.c (revision 6022c2c21c00ed222314d1a82f05f5778bfe1e32)
1*6022c2c2Smillert /*	$OpenBSD: strtoull.c,v 1.9 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 an unsigned long long.
40c2f18a70Smillert  *
41c2f18a70Smillert  * Ignores `locale' stuff.  Assumes that the upper and lower case
42c2f18a70Smillert  * alphabets and digits are each contiguous.
43c2f18a70Smillert  */
44c2f18a70Smillert unsigned long long
strtoull(const char * nptr,char ** endptr,int base)45d8bc04e4Spat strtoull(const char *nptr, char **endptr, int base)
46c2f18a70Smillert {
47c2f18a70Smillert 	const char *s;
48c2f18a70Smillert 	unsigned long long acc, cutoff;
49c2f18a70Smillert 	int c;
50c2f18a70Smillert 	int neg, any, cutlim;
51c2f18a70Smillert 
52c2f18a70Smillert 	/*
53b252b5f6Sschwarze 	 * See strtoll for comments as to the logic used.
54c2f18a70Smillert 	 */
55b252b5f6Sschwarze 	if (base < 0 || base == 1 || base > 36) {
56b252b5f6Sschwarze 		if (endptr != 0)
57b252b5f6Sschwarze 			*endptr = (char *)nptr;
58b252b5f6Sschwarze 		errno = EINVAL;
59b252b5f6Sschwarze 		return 0;
60b252b5f6Sschwarze 	}
61b252b5f6Sschwarze 
62c2f18a70Smillert 	s = nptr;
63c2f18a70Smillert 	do {
64c2f18a70Smillert 		c = (unsigned char) *s++;
65c2f18a70Smillert 	} while (isspace(c));
66c2f18a70Smillert 	if (c == '-') {
67c2f18a70Smillert 		neg = 1;
68c2f18a70Smillert 		c = *s++;
69c2f18a70Smillert 	} else {
70c2f18a70Smillert 		neg = 0;
71c2f18a70Smillert 		if (c == '+')
72c2f18a70Smillert 			c = *s++;
73c2f18a70Smillert 	}
74*6022c2c2Smillert 	if ((base == 0 || base == 16) && c == '0' &&
75*6022c2c2Smillert 	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
76c2f18a70Smillert 		c = s[1];
77c2f18a70Smillert 		s += 2;
78c2f18a70Smillert 		base = 16;
79c2f18a70Smillert 	}
80c2f18a70Smillert 	if (base == 0)
81c2f18a70Smillert 		base = c == '0' ? 8 : 10;
82c2f18a70Smillert 
83c2f18a70Smillert 	cutoff = ULLONG_MAX / (unsigned long long)base;
84c2f18a70Smillert 	cutlim = ULLONG_MAX % (unsigned long long)base;
85c2f18a70Smillert 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
86c2f18a70Smillert 		if (isdigit(c))
87c2f18a70Smillert 			c -= '0';
88c2f18a70Smillert 		else if (isalpha(c))
89c2f18a70Smillert 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
90c2f18a70Smillert 		else
91c2f18a70Smillert 			break;
92c2f18a70Smillert 		if (c >= base)
93c2f18a70Smillert 			break;
94c2f18a70Smillert 		if (any < 0)
95c2f18a70Smillert 			continue;
96c2f18a70Smillert 		if (acc > cutoff || (acc == cutoff && c > cutlim)) {
97c2f18a70Smillert 			any = -1;
98c2f18a70Smillert 			acc = ULLONG_MAX;
99c2f18a70Smillert 			errno = ERANGE;
100c2f18a70Smillert 		} else {
101c2f18a70Smillert 			any = 1;
102c2f18a70Smillert 			acc *= (unsigned long long)base;
103c2f18a70Smillert 			acc += c;
104c2f18a70Smillert 		}
105c2f18a70Smillert 	}
106c2f18a70Smillert 	if (neg && any > 0)
107c2f18a70Smillert 		acc = -acc;
108c2f18a70Smillert 	if (endptr != 0)
109c2f18a70Smillert 		*endptr = (char *) (any ? s - 1 : nptr);
110c2f18a70Smillert 	return (acc);
111c2f18a70Smillert }
1120d943ef0Sguenther DEF_STRONG(strtoull);
113c2f18a70Smillert 
1140d943ef0Sguenther __weak_alias(strtouq, strtoull);
115