xref: /netbsd-src/common/lib/libc/stdlib/strtoll.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: strtoll.c,v 1.1 2006/10/08 03:14:55 thorpej 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.1 2006/10/08 03:14:55 thorpej 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 #ifdef __GNUC__
99 	/* This outrageous construct just to shut up a GCC warning. */
100 	(void) &acc; (void) &cutoff;
101 #endif
102 
103 	/*
104 	 * Skip white space and pick up leading +/- sign if any.
105 	 * If base is 0, allow 0x for hex and 0 for octal, else
106 	 * assume decimal; if base is already 16, allow 0x.
107 	 */
108 	s = nptr;
109 	do {
110 		c = (unsigned char) *s++;
111 	} while (isspace(c));
112 	if (c == '-') {
113 		neg = 1;
114 		c = *s++;
115 	} else {
116 		neg = 0;
117 		if (c == '+')
118 			c = *s++;
119 	}
120 	if ((base == 0 || base == 16) &&
121 	    c == '0' && (*s == 'x' || *s == 'X')) {
122 		c = s[1];
123 		s += 2;
124 		base = 16;
125 	}
126 	if (base == 0)
127 		base = c == '0' ? 8 : 10;
128 
129 	/*
130 	 * Compute the cutoff value between legal numbers and illegal
131 	 * numbers.  That is the largest legal value, divided by the
132 	 * base.  An input number that is greater than this value, if
133 	 * followed by a legal input character, is too big.  One that
134 	 * is equal to this value may be valid or not; the limit
135 	 * between valid and invalid numbers is then based on the last
136 	 * digit.  For instance, if the range for long longs is
137 	 * [-9223372036854775808..9223372036854775807] and the input base
138 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
139 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
140 	 * accumulated a value > 922337203685477580, or equal but the
141 	 * next digit is > 7 (or 8), the number is too big, and we will
142 	 * return a range error.
143 	 *
144 	 * Set any if any `digits' consumed; make it negative to indicate
145 	 * overflow.
146 	 */
147 	cutoff = neg ? LLONG_MIN : LLONG_MAX;
148 	cutlim = (int)(cutoff % base);
149 	cutoff /= base;
150 	if (neg) {
151 		if (cutlim > 0) {
152 			cutlim -= base;
153 			cutoff += 1;
154 		}
155 		cutlim = -cutlim;
156 	}
157 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
158 		if (isdigit(c))
159 			c -= '0';
160 		else if (isalpha(c)) {
161 #if defined(_KERNEL) || defined(_STANDALONE)
162 			c = toupper(c) - 'A' + 10;
163 #else
164 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
165 #endif
166 		} else
167 			break;
168 		if (c >= base)
169 			break;
170 		if (any < 0)
171 			continue;
172 		if (neg) {
173 			if (acc < cutoff || (acc == cutoff && c > cutlim)) {
174 #if defined(_KERNEL) || defined(_STANDALONE)
175 				if (endptr)
176 					*endptr = __UNCONST(nptr);
177 				return (LLONG_MIN);
178 #else
179 				any = -1;
180 				acc = LLONG_MIN;
181 				errno = ERANGE;
182 #endif
183 			} else {
184 				any = 1;
185 				acc *= base;
186 				acc -= c;
187 			}
188 		} else {
189 			if (acc > cutoff || (acc == cutoff && c > cutlim)) {
190 #if defined(_KERNEL) || defined(_STANDALONE)
191 				if (endptr)
192 					*endptr = __UNCONST(nptr);
193 				return (LLONG_MAX);
194 #else
195 				any = -1;
196 				acc = LLONG_MAX;
197 				errno = ERANGE;
198 #endif
199 			} else {
200 				any = 1;
201 				acc *= base;
202 				acc += c;
203 			}
204 		}
205 	}
206 	if (endptr != 0)
207 		*endptr = __UNCONST(any ? s - 1 : nptr);
208 	return (acc);
209 }
210 #endif
211