152845Sphk/*-
252845Sphk * Copyright (c) 1990, 1993
352845Sphk *	The Regents of the University of California.  All rights reserved.
452845Sphk *
552845Sphk * This code is derived from software contributed to Berkeley by
652845Sphk * Chris Torek.
752845Sphk *
852845Sphk * Redistribution and use in source and binary forms, with or without
952845Sphk * modification, are permitted provided that the following conditions
1052845Sphk * are met:
1152845Sphk * 1. Redistributions of source code must retain the above copyright
1252845Sphk *    notice, this list of conditions and the following disclaimer.
1352845Sphk * 2. Redistributions in binary form must reproduce the above copyright
1452845Sphk *    notice, this list of conditions and the following disclaimer in the
1552845Sphk *    documentation and/or other materials provided with the distribution.
1652845Sphk * 4. Neither the name of the University nor the names of its contributors
1752845Sphk *    may be used to endorse or promote products derived from this software
1852845Sphk *    without specific prior written permission.
1952845Sphk *
2052845Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2152845Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2252845Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2352845Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2452845Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2552845Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2652845Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2752845Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2852845Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2952845Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3052845Sphk * SUCH DAMAGE.
3152845Sphk */
3252845Sphk
33116189Sobrien#include <sys/cdefs.h>
34116189Sobrien__FBSDID("$FreeBSD$");
35116189Sobrien
3652845Sphk#include <sys/param.h>
3752845Sphk#include <sys/systm.h>
3852845Sphk#include <sys/ctype.h>
39114216Skan#include <sys/limits.h>
4052845Sphk
4152845Sphk/*
4252845Sphk * Convert a string to a quad integer.
4352845Sphk *
4452845Sphk * Ignores `locale' stuff.  Assumes that the upper and lower case
4552845Sphk * alphabets and digits are each contiguous.
4652845Sphk */
4752845Sphkquad_t
4853648Sarchiestrtoq(const char *nptr, char **endptr, int base)
4952845Sphk{
5052845Sphk	const char *s;
5152845Sphk	u_quad_t acc;
5252845Sphk	unsigned char c;
5352845Sphk	u_quad_t qbase, cutoff;
5452845Sphk	int neg, any, cutlim;
5552845Sphk
5652845Sphk	/*
5752845Sphk	 * Skip white space and pick up leading +/- sign if any.
5852845Sphk	 * If base is 0, allow 0x for hex and 0 for octal, else
5952845Sphk	 * assume decimal; if base is already 16, allow 0x.
6052845Sphk	 */
6152845Sphk	s = nptr;
6252845Sphk	do {
6352845Sphk		c = *s++;
6452845Sphk	} while (isspace(c));
6552845Sphk	if (c == '-') {
6652845Sphk		neg = 1;
6752845Sphk		c = *s++;
6852845Sphk	} else {
6952845Sphk		neg = 0;
7052845Sphk		if (c == '+')
7152845Sphk			c = *s++;
7252845Sphk	}
7352845Sphk	if ((base == 0 || base == 16) &&
7452845Sphk	    c == '0' && (*s == 'x' || *s == 'X')) {
7552845Sphk		c = s[1];
7652845Sphk		s += 2;
7752845Sphk		base = 16;
7852845Sphk	}
7952845Sphk	if (base == 0)
8052845Sphk		base = c == '0' ? 8 : 10;
8152845Sphk
8252845Sphk	/*
8352845Sphk	 * Compute the cutoff value between legal numbers and illegal
8452845Sphk	 * numbers.  That is the largest legal value, divided by the
8552845Sphk	 * base.  An input number that is greater than this value, if
8652845Sphk	 * followed by a legal input character, is too big.  One that
8752845Sphk	 * is equal to this value may be valid or not; the limit
8852845Sphk	 * between valid and invalid numbers is then based on the last
8952845Sphk	 * digit.  For instance, if the range for quads is
9052845Sphk	 * [-9223372036854775808..9223372036854775807] and the input base
9152845Sphk	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
9252845Sphk	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
9352845Sphk	 * accumulated a value > 922337203685477580, or equal but the
9452845Sphk	 * next digit is > 7 (or 8), the number is too big, and we will
9552845Sphk	 * return a range error.
9652845Sphk	 *
9752845Sphk	 * Set any if any `digits' consumed; make it negative to indicate
9852845Sphk	 * overflow.
9952845Sphk	 */
10052845Sphk	qbase = (unsigned)base;
10152845Sphk	cutoff = neg ? (u_quad_t)-(QUAD_MIN + QUAD_MAX) + QUAD_MAX : QUAD_MAX;
10252845Sphk	cutlim = cutoff % qbase;
10352845Sphk	cutoff /= qbase;
10452845Sphk	for (acc = 0, any = 0;; c = *s++) {
10552845Sphk		if (!isascii(c))
10652845Sphk			break;
10752845Sphk		if (isdigit(c))
10852845Sphk			c -= '0';
10952845Sphk		else if (isalpha(c))
11052845Sphk			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
11152845Sphk		else
11252845Sphk			break;
11352845Sphk		if (c >= base)
11452845Sphk			break;
11552845Sphk		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
11652845Sphk			any = -1;
11752845Sphk		else {
11852845Sphk			any = 1;
11952845Sphk			acc *= qbase;
12052845Sphk			acc += c;
12152845Sphk		}
12252845Sphk	}
12352845Sphk	if (any < 0) {
12452845Sphk		acc = neg ? QUAD_MIN : QUAD_MAX;
12552845Sphk	} else if (neg)
12652845Sphk		acc = -acc;
12752845Sphk	if (endptr != 0)
12854006Sarchie		*((const char **)endptr) = any ? s - 1 : nptr;
12952845Sphk	return (acc);
13052845Sphk}
131