strtoimax.c revision 302408
1300081Simp/*-
2300081Simp * Copyright (c) 1992, 1993
3300081Simp *	The Regents of the University of California.  All rights reserved.
4300081Simp *
5300081Simp * Copyright (c) 2011 The FreeBSD Foundation
6300081Simp * All rights reserved.
7300081Simp * Portions of this software were developed by David Chisnall
8300081Simp * under sponsorship from the FreeBSD Foundation.
9300081Simp *
10300081Simp * Redistribution and use in source and binary forms, with or without
11300081Simp * modification, are permitted provided that the following conditions
12300081Simp * are met:
13300081Simp * 1. Redistributions of source code must retain the above copyright
14300081Simp *    notice, this list of conditions and the following disclaimer.
15300081Simp * 2. Redistributions in binary form must reproduce the above copyright
16300081Simp *    notice, this list of conditions and the following disclaimer in the
17300081Simp *    documentation and/or other materials provided with the distribution.
18300081Simp * 3. Neither the name of the University nor the names of its contributors
19300081Simp *    may be used to endorse or promote products derived from this software
20300081Simp *    without specific prior written permission.
21300081Simp *
22300081Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23300081Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24300081Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25300081Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26300081Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27300081Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28300081Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29329099Skevans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30329011Skevans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31329011Skevans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32300081Simp * SUCH DAMAGE.
33300081Simp */
34329011Skevans
35329099Skevans#if defined(LIBC_SCCS) && !defined(lint)
36329011Skevansstatic char sccsid[] = "from @(#)strtol.c	8.1 (Berkeley) 6/4/93";
37300081Simp#endif /* LIBC_SCCS and not lint */
38329099Skevans#include <sys/cdefs.h>
39329099Skevans__FBSDID("$FreeBSD: stable/11/lib/libc/stdlib/strtoimax.c 251672 2013-06-13 00:19:30Z emaste $");
40329099Skevans
41329099Skevans#include <ctype.h>
42329099Skevans#include <errno.h>
43329099Skevans#include <stdlib.h>
44329099Skevans#include <inttypes.h>
45329099Skevans#include "xlocale_private.h"
46329099Skevans
47329099Skevans/*
48329099Skevans * Convert a string to an intmax_t integer.
49329099Skevans *
50329099Skevans * Assumes that the upper and lower case
51329099Skevans * alphabets and digits are each contiguous.
52329099Skevans */
53329099Skevansintmax_t
54329099Skevansstrtoimax_l(const char * __restrict nptr, char ** __restrict endptr, int base,
55329099Skevans		locale_t locale)
56329099Skevans{
57329099Skevans	const char *s;
58329099Skevans	uintmax_t acc;
59329099Skevans	char c;
60329099Skevans	uintmax_t cutoff;
61329099Skevans	int neg, any, cutlim;
62329099Skevans	FIX_LOCALE(locale);
63329099Skevans
64329099Skevans	/*
65329099Skevans	 * Skip white space and pick up leading +/- sign if any.
66329099Skevans	 * If base is 0, allow 0x for hex and 0 for octal, else
67329099Skevans	 * assume decimal; if base is already 16, allow 0x.
68329099Skevans	 */
69329099Skevans	s = nptr;
70329099Skevans	do {
71329099Skevans		c = *s++;
72329099Skevans	} while (isspace_l((unsigned char)c, locale));
73329099Skevans	if (c == '-') {
74329099Skevans		neg = 1;
75329099Skevans		c = *s++;
76329099Skevans	} else {
77329099Skevans		neg = 0;
78329099Skevans		if (c == '+')
79329099Skevans			c = *s++;
80329099Skevans	}
81329099Skevans	if ((base == 0 || base == 16) &&
82329099Skevans	    c == '0' && (*s == 'x' || *s == 'X') &&
83329099Skevans	    ((s[1] >= '0' && s[1] <= '9') ||
84329099Skevans	    (s[1] >= 'A' && s[1] <= 'F') ||
85329099Skevans	    (s[1] >= 'a' && s[1] <= 'f'))) {
86329099Skevans		c = s[1];
87329099Skevans		s += 2;
88329099Skevans		base = 16;
89329099Skevans	}
90329099Skevans	if (base == 0)
91329099Skevans		base = c == '0' ? 8 : 10;
92329099Skevans	acc = any = 0;
93329099Skevans	if (base < 2 || base > 36)
94329099Skevans		goto noconv;
95329099Skevans
96329099Skevans	/*
97329099Skevans	 * Compute the cutoff value between legal numbers and illegal
98329099Skevans	 * numbers.  That is the largest legal value, divided by the
99329099Skevans	 * base.  An input number that is greater than this value, if
100329099Skevans	 * followed by a legal input character, is too big.  One that
101329099Skevans	 * is equal to this value may be valid or not; the limit
102329099Skevans	 * between valid and invalid numbers is then based on the last
103329099Skevans	 * digit.  For instance, if the range for intmax_t is
104329099Skevans	 * [-9223372036854775808..9223372036854775807] and the input base
105329099Skevans	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
106329099Skevans	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
107329099Skevans	 * accumulated a value > 922337203685477580, or equal but the
108329099Skevans	 * next digit is > 7 (or 8), the number is too big, and we will
109329099Skevans	 * return a range error.
110329099Skevans	 *
111329099Skevans	 * Set 'any' if any `digits' consumed; make it negative to indicate
112329099Skevans	 * overflow.
113329099Skevans	 */
114329099Skevans	cutoff = neg ? (uintmax_t)-(INTMAX_MIN + INTMAX_MAX) + INTMAX_MAX
115329099Skevans	    : INTMAX_MAX;
116329099Skevans	cutlim = cutoff % base;
117346472Skevans	cutoff /= base;
118329099Skevans	for ( ; ; c = *s++) {
119329099Skevans		if (c >= '0' && c <= '9')
120329099Skevans			c -= '0';
121346472Skevans		else if (c >= 'A' && c <= 'Z')
122346472Skevans			c -= 'A' - 10;
123329099Skevans		else if (c >= 'a' && c <= 'z')
124329099Skevans			c -= 'a' - 10;
125329099Skevans		else
126329099Skevans			break;
127329099Skevans		if (c >= base)
128329099Skevans			break;
129329099Skevans		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
130329099Skevans			any = -1;
131329099Skevans		else {
132329099Skevans			any = 1;
133329099Skevans			acc *= base;
134329099Skevans			acc += c;
135329099Skevans		}
136329099Skevans	}
137329099Skevans	if (any < 0) {
138329099Skevans		acc = neg ? INTMAX_MIN : INTMAX_MAX;
139329099Skevans		errno = ERANGE;
140329099Skevans	} else if (!any) {
141329099Skevansnoconv:
142329099Skevans		errno = EINVAL;
143329099Skevans	} else if (neg)
144329099Skevans		acc = -acc;
145329099Skevans	if (endptr != NULL)
146329099Skevans		*endptr = (char *)(any ? s - 1 : nptr);
147329099Skevans	return (acc);
148329099Skevans}
149329099Skevansintmax_t
150329099Skevansstrtoimax(const char * __restrict nptr, char ** __restrict endptr, int base)
151329099Skevans{
152329099Skevans	return strtoimax_l(nptr, endptr, base, __get_locale());
153329099Skevans}
154329099Skevans