xref: /netbsd-src/common/lib/libc/stdlib/strtoll.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: strtoll.c,v 1.2 2006/12/18 00:41:54 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #if !defined(_KERNEL) && !defined(_STANDALONE)
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "from: @(#)strtoq.c	8.1 (Berkeley) 6/4/93";
41 #else
42 __RCSID("$NetBSD: strtoll.c,v 1.2 2006/12/18 00:41:54 christos Exp $");
43 #endif
44 #endif /* LIBC_SCCS and not lint */
45 
46 #ifdef _LIBC
47 #include "namespace.h"
48 #endif
49 
50 #include <assert.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <limits.h>
54 #include <stdlib.h>
55 
56 #ifdef _LIBC
57 #ifdef __weak_alias
58 __weak_alias(strtoll, _strtoll)
59 #endif
60 #endif
61 
62 #else /* !_KERNEL && !_STANDALONE */
63 #include <sys/param.h>
64 #include <lib/libkern/libkern.h>
65 #define	isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r')
66 #define	isdigit(x) ((x) >= '0' && (x) <= '9')
67 #define	isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
68 #define	toupper(x) ((x) & ~0x20)
69 #endif
70 
71 #if !HAVE_STRTOLL
72 /*
73  * Convert a string to a long long integer.
74  *
75  * Ignores `locale' stuff.  Assumes that the upper and lower case
76  * alphabets and digits are each contiguous.
77  */
78 /* LONGLONG */
79 long long int
80 #ifdef _LIBC
81 _strtoll(nptr, endptr, base)
82 #else
83 strtoll(nptr, endptr, base)
84 #endif
85 	const char *nptr;
86 	char **endptr;
87 	int base;
88 {
89 	const char *s;
90 	/* LONGLONG */
91 	long long int acc, cutoff;
92 	int c;
93 	int neg, any, cutlim;
94 
95 	_DIAGASSERT(nptr != NULL);
96 	/* endptr may be NULL */
97 
98 	/*
99 	 * Skip white space and pick up leading +/- sign if any.
100 	 * If base is 0, allow 0x for hex and 0 for octal, else
101 	 * assume decimal; if base is already 16, allow 0x.
102 	 */
103 	s = nptr;
104 	do {
105 		c = (unsigned char) *s++;
106 	} while (isspace(c));
107 	if (c == '-') {
108 		neg = 1;
109 		c = *s++;
110 	} else {
111 		neg = 0;
112 		if (c == '+')
113 			c = *s++;
114 	}
115 	if ((base == 0 || base == 16) &&
116 	    c == '0' && (*s == 'x' || *s == 'X')) {
117 		c = s[1];
118 		s += 2;
119 		base = 16;
120 	}
121 	if (base == 0)
122 		base = c == '0' ? 8 : 10;
123 
124 	/*
125 	 * Compute the cutoff value between legal numbers and illegal
126 	 * numbers.  That is the largest legal value, divided by the
127 	 * base.  An input number that is greater than this value, if
128 	 * followed by a legal input character, is too big.  One that
129 	 * is equal to this value may be valid or not; the limit
130 	 * between valid and invalid numbers is then based on the last
131 	 * digit.  For instance, if the range for long longs is
132 	 * [-9223372036854775808..9223372036854775807] and the input base
133 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
134 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
135 	 * accumulated a value > 922337203685477580, or equal but the
136 	 * next digit is > 7 (or 8), the number is too big, and we will
137 	 * return a range error.
138 	 *
139 	 * Set any if any `digits' consumed; make it negative to indicate
140 	 * overflow.
141 	 */
142 	cutoff = neg ? LLONG_MIN : LLONG_MAX;
143 	cutlim = (int)(cutoff % base);
144 	cutoff /= base;
145 	if (neg) {
146 		if (cutlim > 0) {
147 			cutlim -= base;
148 			cutoff += 1;
149 		}
150 		cutlim = -cutlim;
151 	}
152 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
153 		if (isdigit(c))
154 			c -= '0';
155 		else if (isalpha(c)) {
156 #if defined(_KERNEL) || defined(_STANDALONE)
157 			c = toupper(c) - 'A' + 10;
158 #else
159 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
160 #endif
161 		} else
162 			break;
163 		if (c >= base)
164 			break;
165 		if (any < 0)
166 			continue;
167 		if (neg) {
168 			if (acc < cutoff || (acc == cutoff && c > cutlim)) {
169 #if defined(_KERNEL) || defined(_STANDALONE)
170 				if (endptr)
171 					*endptr = __UNCONST(nptr);
172 				return (LLONG_MIN);
173 #else
174 				any = -1;
175 				acc = LLONG_MIN;
176 				errno = ERANGE;
177 #endif
178 			} else {
179 				any = 1;
180 				acc *= base;
181 				acc -= c;
182 			}
183 		} else {
184 			if (acc > cutoff || (acc == cutoff && c > cutlim)) {
185 #if defined(_KERNEL) || defined(_STANDALONE)
186 				if (endptr)
187 					*endptr = __UNCONST(nptr);
188 				return (LLONG_MAX);
189 #else
190 				any = -1;
191 				acc = LLONG_MAX;
192 				errno = ERANGE;
193 #endif
194 			} else {
195 				any = 1;
196 				acc *= base;
197 				acc += c;
198 			}
199 		}
200 	}
201 	if (endptr != 0)
202 		*endptr = __UNCONST(any ? s - 1 : nptr);
203 	return (acc);
204 }
205 #endif
206