xref: /freebsd/sys/libkern/strtoq.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
11a4e73baSPoul-Henning Kamp /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
41a4e73baSPoul-Henning Kamp  * Copyright (c) 1990, 1993
51a4e73baSPoul-Henning Kamp  *	The Regents of the University of California.  All rights reserved.
61a4e73baSPoul-Henning Kamp  *
71a4e73baSPoul-Henning Kamp  * This code is derived from software contributed to Berkeley by
81a4e73baSPoul-Henning Kamp  * Chris Torek.
91a4e73baSPoul-Henning Kamp  *
101a4e73baSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
111a4e73baSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
121a4e73baSPoul-Henning Kamp  * are met:
131a4e73baSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
141a4e73baSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
151a4e73baSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
161a4e73baSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
171a4e73baSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
191a4e73baSPoul-Henning Kamp  *    may be used to endorse or promote products derived from this software
201a4e73baSPoul-Henning Kamp  *    without specific prior written permission.
211a4e73baSPoul-Henning Kamp  *
221a4e73baSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231a4e73baSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241a4e73baSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251a4e73baSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261a4e73baSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271a4e73baSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281a4e73baSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291a4e73baSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301a4e73baSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311a4e73baSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321a4e73baSPoul-Henning Kamp  * SUCH DAMAGE.
331a4e73baSPoul-Henning Kamp  */
341a4e73baSPoul-Henning Kamp 
351a4e73baSPoul-Henning Kamp #include <sys/param.h>
361a4e73baSPoul-Henning Kamp #include <sys/systm.h>
371a4e73baSPoul-Henning Kamp #include <sys/ctype.h>
38104a9b7eSAlexander Kabaev #include <sys/limits.h>
391a4e73baSPoul-Henning Kamp 
401a4e73baSPoul-Henning Kamp /*
411a4e73baSPoul-Henning Kamp  * Convert a string to a quad integer.
421a4e73baSPoul-Henning Kamp  *
431a4e73baSPoul-Henning Kamp  * Ignores `locale' stuff.  Assumes that the upper and lower case
441a4e73baSPoul-Henning Kamp  * alphabets and digits are each contiguous.
451a4e73baSPoul-Henning Kamp  */
461a4e73baSPoul-Henning Kamp quad_t
strtoq(const char * nptr,char ** endptr,int base)4725792ef3SArchie Cobbs strtoq(const char *nptr, char **endptr, int base)
481a4e73baSPoul-Henning Kamp {
491a4e73baSPoul-Henning Kamp 	const char *s;
501a4e73baSPoul-Henning Kamp 	u_quad_t acc;
511a4e73baSPoul-Henning Kamp 	unsigned char c;
521a4e73baSPoul-Henning Kamp 	u_quad_t qbase, cutoff;
531a4e73baSPoul-Henning Kamp 	int neg, any, cutlim;
541a4e73baSPoul-Henning Kamp 
551a4e73baSPoul-Henning Kamp 	/*
561a4e73baSPoul-Henning Kamp 	 * Skip white space and pick up leading +/- sign if any.
571a4e73baSPoul-Henning Kamp 	 * If base is 0, allow 0x for hex and 0 for octal, else
581a4e73baSPoul-Henning Kamp 	 * assume decimal; if base is already 16, allow 0x.
591a4e73baSPoul-Henning Kamp 	 */
601a4e73baSPoul-Henning Kamp 	s = nptr;
611a4e73baSPoul-Henning Kamp 	do {
621a4e73baSPoul-Henning Kamp 		c = *s++;
631a4e73baSPoul-Henning Kamp 	} while (isspace(c));
641a4e73baSPoul-Henning Kamp 	if (c == '-') {
651a4e73baSPoul-Henning Kamp 		neg = 1;
661a4e73baSPoul-Henning Kamp 		c = *s++;
671a4e73baSPoul-Henning Kamp 	} else {
681a4e73baSPoul-Henning Kamp 		neg = 0;
691a4e73baSPoul-Henning Kamp 		if (c == '+')
701a4e73baSPoul-Henning Kamp 			c = *s++;
711a4e73baSPoul-Henning Kamp 	}
721a4e73baSPoul-Henning Kamp 	if ((base == 0 || base == 16) &&
731a4e73baSPoul-Henning Kamp 	    c == '0' && (*s == 'x' || *s == 'X')) {
741a4e73baSPoul-Henning Kamp 		c = s[1];
751a4e73baSPoul-Henning Kamp 		s += 2;
761a4e73baSPoul-Henning Kamp 		base = 16;
771a4e73baSPoul-Henning Kamp 	}
781a4e73baSPoul-Henning Kamp 	if (base == 0)
791a4e73baSPoul-Henning Kamp 		base = c == '0' ? 8 : 10;
801a4e73baSPoul-Henning Kamp 
811a4e73baSPoul-Henning Kamp 	/*
821a4e73baSPoul-Henning Kamp 	 * Compute the cutoff value between legal numbers and illegal
831a4e73baSPoul-Henning Kamp 	 * numbers.  That is the largest legal value, divided by the
841a4e73baSPoul-Henning Kamp 	 * base.  An input number that is greater than this value, if
851a4e73baSPoul-Henning Kamp 	 * followed by a legal input character, is too big.  One that
861a4e73baSPoul-Henning Kamp 	 * is equal to this value may be valid or not; the limit
871a4e73baSPoul-Henning Kamp 	 * between valid and invalid numbers is then based on the last
881a4e73baSPoul-Henning Kamp 	 * digit.  For instance, if the range for quads is
891a4e73baSPoul-Henning Kamp 	 * [-9223372036854775808..9223372036854775807] and the input base
901a4e73baSPoul-Henning Kamp 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
911a4e73baSPoul-Henning Kamp 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
921a4e73baSPoul-Henning Kamp 	 * accumulated a value > 922337203685477580, or equal but the
931a4e73baSPoul-Henning Kamp 	 * next digit is > 7 (or 8), the number is too big, and we will
941a4e73baSPoul-Henning Kamp 	 * return a range error.
951a4e73baSPoul-Henning Kamp 	 *
961a4e73baSPoul-Henning Kamp 	 * Set any if any `digits' consumed; make it negative to indicate
971a4e73baSPoul-Henning Kamp 	 * overflow.
981a4e73baSPoul-Henning Kamp 	 */
991a4e73baSPoul-Henning Kamp 	qbase = (unsigned)base;
1001a4e73baSPoul-Henning Kamp 	cutoff = neg ? (u_quad_t)-(QUAD_MIN + QUAD_MAX) + QUAD_MAX : QUAD_MAX;
1011a4e73baSPoul-Henning Kamp 	cutlim = cutoff % qbase;
1021a4e73baSPoul-Henning Kamp 	cutoff /= qbase;
1031a4e73baSPoul-Henning Kamp 	for (acc = 0, any = 0;; c = *s++) {
1041a4e73baSPoul-Henning Kamp 		if (!isascii(c))
1051a4e73baSPoul-Henning Kamp 			break;
1061a4e73baSPoul-Henning Kamp 		if (isdigit(c))
1071a4e73baSPoul-Henning Kamp 			c -= '0';
1081a4e73baSPoul-Henning Kamp 		else if (isalpha(c))
1091a4e73baSPoul-Henning Kamp 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
1101a4e73baSPoul-Henning Kamp 		else
1111a4e73baSPoul-Henning Kamp 			break;
1121a4e73baSPoul-Henning Kamp 		if (c >= base)
1131a4e73baSPoul-Henning Kamp 			break;
1141a4e73baSPoul-Henning Kamp 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1151a4e73baSPoul-Henning Kamp 			any = -1;
1161a4e73baSPoul-Henning Kamp 		else {
1171a4e73baSPoul-Henning Kamp 			any = 1;
1181a4e73baSPoul-Henning Kamp 			acc *= qbase;
1191a4e73baSPoul-Henning Kamp 			acc += c;
1201a4e73baSPoul-Henning Kamp 		}
1211a4e73baSPoul-Henning Kamp 	}
1221a4e73baSPoul-Henning Kamp 	if (any < 0) {
1231a4e73baSPoul-Henning Kamp 		acc = neg ? QUAD_MIN : QUAD_MAX;
1241a4e73baSPoul-Henning Kamp 	} else if (neg)
1251a4e73baSPoul-Henning Kamp 		acc = -acc;
126b85f65afSPedro F. Giffuni 	if (endptr != NULL)
1270b77a417SDimitry Andric 		*endptr = __DECONST(char *, any ? s - 1 : nptr);
1281a4e73baSPoul-Henning Kamp 	return (acc);
1291a4e73baSPoul-Henning Kamp }
130