xref: /freebsd/lib/libc/stdlib/strtoimax.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
19c5cbc30SBill Fenner /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49c5cbc30SBill Fenner  * Copyright (c) 1992, 1993
59c5cbc30SBill Fenner  *	The Regents of the University of California.  All rights reserved.
69c5cbc30SBill Fenner  *
73c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
85b5fa75aSEd Maste  *
93c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall  *
129c5cbc30SBill Fenner  * Redistribution and use in source and binary forms, with or without
139c5cbc30SBill Fenner  * modification, are permitted provided that the following conditions
149c5cbc30SBill Fenner  * are met:
159c5cbc30SBill Fenner  * 1. Redistributions of source code must retain the above copyright
169c5cbc30SBill Fenner  *    notice, this list of conditions and the following disclaimer.
179c5cbc30SBill Fenner  * 2. Redistributions in binary form must reproduce the above copyright
189c5cbc30SBill Fenner  *    notice, this list of conditions and the following disclaimer in the
199c5cbc30SBill Fenner  *    documentation and/or other materials provided with the distribution.
20580b4d18SEd Maste  * 3. Neither the name of the University nor the names of its contributors
219c5cbc30SBill Fenner  *    may be used to endorse or promote products derived from this software
229c5cbc30SBill Fenner  *    without specific prior written permission.
239c5cbc30SBill Fenner  *
249c5cbc30SBill Fenner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259c5cbc30SBill Fenner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269c5cbc30SBill Fenner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279c5cbc30SBill Fenner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289c5cbc30SBill Fenner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299c5cbc30SBill Fenner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309c5cbc30SBill Fenner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319c5cbc30SBill Fenner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329c5cbc30SBill Fenner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339c5cbc30SBill Fenner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349c5cbc30SBill Fenner  * SUCH DAMAGE.
359c5cbc30SBill Fenner  */
369c5cbc30SBill Fenner 
379c5cbc30SBill Fenner #include <ctype.h>
389c5cbc30SBill Fenner #include <errno.h>
399c5cbc30SBill Fenner #include <stdlib.h>
409c5cbc30SBill Fenner #include <inttypes.h>
413c87aa1dSDavid Chisnall #include "xlocale_private.h"
429c5cbc30SBill Fenner 
439c5cbc30SBill Fenner /*
449d5abbddSJens Schweikhardt  * Convert a string to an intmax_t integer.
459c5cbc30SBill Fenner  *
469c5cbc30SBill Fenner  * Assumes that the upper and lower case
479c5cbc30SBill Fenner  * alphabets and digits are each contiguous.
489c5cbc30SBill Fenner  */
499c5cbc30SBill Fenner intmax_t
strtoimax_l(const char * __restrict nptr,char ** __restrict endptr,int base,locale_t locale)503c87aa1dSDavid Chisnall strtoimax_l(const char * __restrict nptr, char ** __restrict endptr, int base,
513c87aa1dSDavid Chisnall 		locale_t locale)
529c5cbc30SBill Fenner {
539c5cbc30SBill Fenner 	const char *s;
549c5cbc30SBill Fenner 	uintmax_t acc;
55f34b139cSAndrey A. Chernov 	char c;
569c5cbc30SBill Fenner 	uintmax_t cutoff;
57f34b139cSAndrey A. Chernov 	int neg, any, cutlim;
583c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
599c5cbc30SBill Fenner 
609c5cbc30SBill Fenner 	/*
619c5cbc30SBill Fenner 	 * Skip white space and pick up leading +/- sign if any.
629c5cbc30SBill Fenner 	 * If base is 0, allow 0x for hex and 0 for octal, else
639c5cbc30SBill Fenner 	 * assume decimal; if base is already 16, allow 0x.
649c5cbc30SBill Fenner 	 */
659c5cbc30SBill Fenner 	s = nptr;
669c5cbc30SBill Fenner 	do {
679c5cbc30SBill Fenner 		c = *s++;
683c87aa1dSDavid Chisnall 	} while (isspace_l((unsigned char)c, locale));
699c5cbc30SBill Fenner 	if (c == '-') {
709c5cbc30SBill Fenner 		neg = 1;
719c5cbc30SBill Fenner 		c = *s++;
729c5cbc30SBill Fenner 	} else {
739c5cbc30SBill Fenner 		neg = 0;
749c5cbc30SBill Fenner 		if (c == '+')
759c5cbc30SBill Fenner 			c = *s++;
769c5cbc30SBill Fenner 	}
779c5cbc30SBill Fenner 	if ((base == 0 || base == 16) &&
782571c7f7SAndrey A. Chernov 	    c == '0' && (*s == 'x' || *s == 'X') &&
79db0e25eeSAndrey A. Chernov 	    ((s[1] >= '0' && s[1] <= '9') ||
802571c7f7SAndrey A. Chernov 	    (s[1] >= 'A' && s[1] <= 'F') ||
81db0e25eeSAndrey A. Chernov 	    (s[1] >= 'a' && s[1] <= 'f'))) {
829c5cbc30SBill Fenner 		c = s[1];
839c5cbc30SBill Fenner 		s += 2;
849c5cbc30SBill Fenner 		base = 16;
859c5cbc30SBill Fenner 	}
86*d9dc1603SDag-Erling Smørgrav 	if ((base == 0 || base == 2) &&
87*d9dc1603SDag-Erling Smørgrav 	    c == '0' && (*s == 'b' || *s == 'B') &&
88*d9dc1603SDag-Erling Smørgrav 	    (s[1] >= '0' && s[1] <= '1')) {
89*d9dc1603SDag-Erling Smørgrav 		c = s[1];
90*d9dc1603SDag-Erling Smørgrav 		s += 2;
91*d9dc1603SDag-Erling Smørgrav 		base = 2;
92*d9dc1603SDag-Erling Smørgrav 	}
939c5cbc30SBill Fenner 	if (base == 0)
949c5cbc30SBill Fenner 		base = c == '0' ? 8 : 10;
959c5cbc30SBill Fenner 	acc = any = 0;
969c5cbc30SBill Fenner 	if (base < 2 || base > 36)
979c5cbc30SBill Fenner 		goto noconv;
989c5cbc30SBill Fenner 
999c5cbc30SBill Fenner 	/*
1009c5cbc30SBill Fenner 	 * Compute the cutoff value between legal numbers and illegal
1019c5cbc30SBill Fenner 	 * numbers.  That is the largest legal value, divided by the
1029c5cbc30SBill Fenner 	 * base.  An input number that is greater than this value, if
1039c5cbc30SBill Fenner 	 * followed by a legal input character, is too big.  One that
1049c5cbc30SBill Fenner 	 * is equal to this value may be valid or not; the limit
1059c5cbc30SBill Fenner 	 * between valid and invalid numbers is then based on the last
1069c5cbc30SBill Fenner 	 * digit.  For instance, if the range for intmax_t is
1079c5cbc30SBill Fenner 	 * [-9223372036854775808..9223372036854775807] and the input base
1089c5cbc30SBill Fenner 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
1099c5cbc30SBill Fenner 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
1109c5cbc30SBill Fenner 	 * accumulated a value > 922337203685477580, or equal but the
1119c5cbc30SBill Fenner 	 * next digit is > 7 (or 8), the number is too big, and we will
1129c5cbc30SBill Fenner 	 * return a range error.
1139c5cbc30SBill Fenner 	 *
1149c5cbc30SBill Fenner 	 * Set 'any' if any `digits' consumed; make it negative to indicate
1159c5cbc30SBill Fenner 	 * overflow.
1169c5cbc30SBill Fenner 	 */
1179c5cbc30SBill Fenner 	cutoff = neg ? (uintmax_t)-(INTMAX_MIN + INTMAX_MAX) + INTMAX_MAX
1189c5cbc30SBill Fenner 	    : INTMAX_MAX;
1199c5cbc30SBill Fenner 	cutlim = cutoff % base;
1209c5cbc30SBill Fenner 	cutoff /= base;
1219c5cbc30SBill Fenner 	for ( ; ; c = *s++) {
122f34b139cSAndrey A. Chernov 		if (c >= '0' && c <= '9')
123f34b139cSAndrey A. Chernov 			c -= '0';
124f34b139cSAndrey A. Chernov 		else if (c >= 'A' && c <= 'Z')
125f34b139cSAndrey A. Chernov 			c -= 'A' - 10;
126f34b139cSAndrey A. Chernov 		else if (c >= 'a' && c <= 'z')
127f34b139cSAndrey A. Chernov 			c -= 'a' - 10;
1289c5cbc30SBill Fenner 		else
1299c5cbc30SBill Fenner 			break;
130f34b139cSAndrey A. Chernov 		if (c >= base)
1319c5cbc30SBill Fenner 			break;
132f34b139cSAndrey A. Chernov 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1339c5cbc30SBill Fenner 			any = -1;
1349c5cbc30SBill Fenner 		else {
1359c5cbc30SBill Fenner 			any = 1;
1369c5cbc30SBill Fenner 			acc *= base;
137f34b139cSAndrey A. Chernov 			acc += c;
1389c5cbc30SBill Fenner 		}
1399c5cbc30SBill Fenner 	}
1409c5cbc30SBill Fenner 	if (any < 0) {
1419c5cbc30SBill Fenner 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
1429c5cbc30SBill Fenner 		errno = ERANGE;
1439c5cbc30SBill Fenner 	} else if (!any) {
1449c5cbc30SBill Fenner noconv:
1459c5cbc30SBill Fenner 		errno = EINVAL;
1469c5cbc30SBill Fenner 	} else if (neg)
1479c5cbc30SBill Fenner 		acc = -acc;
1489c5cbc30SBill Fenner 	if (endptr != NULL)
1499c5cbc30SBill Fenner 		*endptr = (char *)(any ? s - 1 : nptr);
1509c5cbc30SBill Fenner 	return (acc);
1519c5cbc30SBill Fenner }
1523c87aa1dSDavid Chisnall intmax_t
strtoimax(const char * __restrict nptr,char ** __restrict endptr,int base)1533c87aa1dSDavid Chisnall strtoimax(const char * __restrict nptr, char ** __restrict endptr, int base)
1543c87aa1dSDavid Chisnall {
1553c87aa1dSDavid Chisnall 	return strtoimax_l(nptr, endptr, base, __get_locale());
1563c87aa1dSDavid Chisnall }
157