187027Sfenner/*-
287027Sfenner * Copyright (c) 1992, 1993
387027Sfenner *	The Regents of the University of California.  All rights reserved.
487027Sfenner *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
1087027Sfenner * Redistribution and use in source and binary forms, with or without
1187027Sfenner * modification, are permitted provided that the following conditions
1287027Sfenner * are met:
1387027Sfenner * 1. Redistributions of source code must retain the above copyright
1487027Sfenner *    notice, this list of conditions and the following disclaimer.
1587027Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1687027Sfenner *    notice, this list of conditions and the following disclaimer in the
1787027Sfenner *    documentation and/or other materials provided with the distribution.
18251672Semaste * 3. Neither the name of the University nor the names of its contributors
1987027Sfenner *    may be used to endorse or promote products derived from this software
2087027Sfenner *    without specific prior written permission.
2187027Sfenner *
2287027Sfenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2387027Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2487027Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2587027Sfenner * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2687027Sfenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2787027Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2887027Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2987027Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3087027Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3187027Sfenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3287027Sfenner * SUCH DAMAGE.
3387027Sfenner */
3487027Sfenner
3587027Sfenner#if defined(LIBC_SCCS) && !defined(lint)
3687027Sfennerstatic char sccsid[] = "from @(#)strtol.c	8.1 (Berkeley) 6/4/93";
3787027Sfenner#endif /* LIBC_SCCS and not lint */
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
4087027Sfenner
4187027Sfenner#include <ctype.h>
4287027Sfenner#include <errno.h>
4387027Sfenner#include <stdlib.h>
4487027Sfenner#include <inttypes.h>
45227753Stheraven#include "xlocale_private.h"
4687027Sfenner
4787027Sfenner/*
48108533Sschweikh * Convert a string to an intmax_t integer.
4987027Sfenner *
5087027Sfenner * Assumes that the upper and lower case
5187027Sfenner * alphabets and digits are each contiguous.
5287027Sfenner */
5387027Sfennerintmax_t
54227753Stheravenstrtoimax_l(const char * __restrict nptr, char ** __restrict endptr, int base,
55227753Stheraven		locale_t locale)
5687027Sfenner{
5787027Sfenner	const char *s;
5887027Sfenner	uintmax_t acc;
5987494Sache	char c;
6087027Sfenner	uintmax_t cutoff;
6187494Sache	int neg, any, cutlim;
62227753Stheraven	FIX_LOCALE(locale);
6387027Sfenner
6487027Sfenner	/*
6587027Sfenner	 * Skip white space and pick up leading +/- sign if any.
6687027Sfenner	 * If base is 0, allow 0x for hex and 0 for octal, else
6787027Sfenner	 * assume decimal; if base is already 16, allow 0x.
6887027Sfenner	 */
6987027Sfenner	s = nptr;
7087027Sfenner	do {
7187027Sfenner		c = *s++;
72227753Stheraven	} while (isspace_l((unsigned char)c, locale));
7387027Sfenner	if (c == '-') {
7487027Sfenner		neg = 1;
7587027Sfenner		c = *s++;
7687027Sfenner	} else {
7787027Sfenner		neg = 0;
7887027Sfenner		if (c == '+')
7987027Sfenner			c = *s++;
8087027Sfenner	}
8187027Sfenner	if ((base == 0 || base == 16) &&
82140536Sache	    c == '0' && (*s == 'x' || *s == 'X') &&
83140577Sache	    ((s[1] >= '0' && s[1] <= '9') ||
84140577Sache	    (s[1] >= 'A' && s[1] <= 'F') ||
85140577Sache	    (s[1] >= 'a' && s[1] <= 'f'))) {
8687027Sfenner		c = s[1];
8787027Sfenner		s += 2;
8887027Sfenner		base = 16;
8987027Sfenner	}
9087027Sfenner	if (base == 0)
9187027Sfenner		base = c == '0' ? 8 : 10;
9287027Sfenner	acc = any = 0;
9387027Sfenner	if (base < 2 || base > 36)
9487027Sfenner		goto noconv;
9587027Sfenner
9687027Sfenner	/*
9787027Sfenner	 * Compute the cutoff value between legal numbers and illegal
9887027Sfenner	 * numbers.  That is the largest legal value, divided by the
9987027Sfenner	 * base.  An input number that is greater than this value, if
10087027Sfenner	 * followed by a legal input character, is too big.  One that
10187027Sfenner	 * is equal to this value may be valid or not; the limit
10287027Sfenner	 * between valid and invalid numbers is then based on the last
10387027Sfenner	 * digit.  For instance, if the range for intmax_t is
10487027Sfenner	 * [-9223372036854775808..9223372036854775807] and the input base
10587027Sfenner	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
10687027Sfenner	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
10787027Sfenner	 * accumulated a value > 922337203685477580, or equal but the
10887027Sfenner	 * next digit is > 7 (or 8), the number is too big, and we will
10987027Sfenner	 * return a range error.
11087027Sfenner	 *
11187027Sfenner	 * Set 'any' if any `digits' consumed; make it negative to indicate
11287027Sfenner	 * overflow.
11387027Sfenner	 */
11487027Sfenner	cutoff = neg ? (uintmax_t)-(INTMAX_MIN + INTMAX_MAX) + INTMAX_MAX
11587027Sfenner	    : INTMAX_MAX;
11687027Sfenner	cutlim = cutoff % base;
11787027Sfenner	cutoff /= base;
11887027Sfenner	for ( ; ; c = *s++) {
11987494Sache		if (c >= '0' && c <= '9')
12087494Sache			c -= '0';
12187494Sache		else if (c >= 'A' && c <= 'Z')
12287494Sache			c -= 'A' - 10;
12387494Sache		else if (c >= 'a' && c <= 'z')
12487494Sache			c -= 'a' - 10;
12587027Sfenner		else
12687027Sfenner			break;
12787494Sache		if (c >= base)
12887027Sfenner			break;
12987494Sache		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
13087027Sfenner			any = -1;
13187027Sfenner		else {
13287027Sfenner			any = 1;
13387027Sfenner			acc *= base;
13487494Sache			acc += c;
13587027Sfenner		}
13687027Sfenner	}
13787027Sfenner	if (any < 0) {
13887027Sfenner		acc = neg ? INTMAX_MIN : INTMAX_MAX;
13987027Sfenner		errno = ERANGE;
14087027Sfenner	} else if (!any) {
14187027Sfennernoconv:
14287027Sfenner		errno = EINVAL;
14387027Sfenner	} else if (neg)
14487027Sfenner		acc = -acc;
14587027Sfenner	if (endptr != NULL)
14687027Sfenner		*endptr = (char *)(any ? s - 1 : nptr);
14787027Sfenner	return (acc);
14887027Sfenner}
149227753Stheravenintmax_t
150227753Stheravenstrtoimax(const char * __restrict nptr, char ** __restrict endptr, int base)
151227753Stheraven{
152227753Stheraven	return strtoimax_l(nptr, endptr, base, __get_locale());
153227753Stheraven}
154