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