kusano 7d535a
/*-
kusano 7d535a
 * Copyright (c) 1992, 1993
kusano 7d535a
 *	The Regents of the University of California.  All rights reserved.
kusano 7d535a
 *
kusano 7d535a
 * Redistribution and use in source and binary forms, with or without
kusano 7d535a
 * modification, are permitted provided that the following conditions
kusano 7d535a
 * are met:
kusano 7d535a
 * 1. Redistributions of source code must retain the above copyright
kusano 7d535a
 *    notice, this list of conditions and the following disclaimer.
kusano 7d535a
 * 2. Redistributions in binary form must reproduce the above copyright
kusano 7d535a
 *    notice, this list of conditions and the following disclaimer in the
kusano 7d535a
 *    documentation and/or other materials provided with the distribution.
kusano 7d535a
 * 3. All advertising materials mentioning features or use of this software
kusano 7d535a
 *    must display the following acknowledgement:
kusano 7d535a
 *	This product includes software developed by the University of
kusano 7d535a
 *	California, Berkeley and its contributors.
kusano 7d535a
 * 4. Neither the name of the University nor the names of its contributors
kusano 7d535a
 *    may be used to endorse or promote products derived from this software
kusano 7d535a
 *    without specific prior written permission.
kusano 7d535a
 *
kusano 7d535a
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
kusano 7d535a
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
kusano 7d535a
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
kusano 7d535a
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
kusano 7d535a
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
kusano 7d535a
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
kusano 7d535a
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kusano 7d535a
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
kusano 7d535a
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
kusano 7d535a
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
kusano 7d535a
 * SUCH DAMAGE.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include <limits.h></limits.h>
kusano 7d535a
#include <errno.h></errno.h>
kusano 7d535a
#include <ctype.h></ctype.h>
kusano 7d535a
#include <stdlib.h></stdlib.h>
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convert a string to an unsigned long long integer.
kusano 7d535a
 *
kusano 7d535a
 * Assumes that the upper and lower case
kusano 7d535a
 * alphabets and digits are each contiguous.
kusano 7d535a
 */
kusano 7d535a
unsigned long long
kusano 7d535a
strtoull(const char *nptr, char **endptr, int base)
kusano 7d535a
{
kusano 7d535a
	const char *s;
kusano 7d535a
	unsigned long long acc;
kusano 7d535a
	char c;
kusano 7d535a
	unsigned long long cutoff;
kusano 7d535a
	int neg, any, cutlim;
kusano 7d535a
kusano 7d535a
	/*
kusano 7d535a
	 * See strtoq for comments as to the logic used.
kusano 7d535a
	 */
kusano 7d535a
	s = nptr;
kusano 7d535a
	do {
kusano 7d535a
		c = *s++;
kusano 7d535a
	} while (isspace((unsigned char)c));
kusano 7d535a
	if (c == '-') {
kusano 7d535a
		neg = 1;
kusano 7d535a
		c = *s++;
kusano 7d535a
	} else {
kusano 7d535a
		neg = 0;
kusano 7d535a
		if (c == '+')
kusano 7d535a
			c = *s++;
kusano 7d535a
	}
kusano 7d535a
	if ((base == 0 || base == 16) &&
kusano 7d535a
	    c == '0' && (*s == 'x' || *s == 'X') &&
kusano 7d535a
	    ((s[1] >= '0' && s[1] <= '9') ||
kusano 7d535a
	    (s[1] >= 'A' && s[1] <= 'F') ||
kusano 7d535a
	    (s[1] >= 'a' && s[1] <= 'f'))) {
kusano 7d535a
		c = s[1];
kusano 7d535a
		s += 2;
kusano 7d535a
		base = 16;
kusano 7d535a
	}
kusano 7d535a
	if (base == 0)
kusano 7d535a
		base = c == '0' ? 8 : 10;
kusano 7d535a
	acc = any = 0;
kusano 7d535a
	if (base < 2 || base > 36)
kusano 7d535a
		goto noconv;
kusano 7d535a
kusano 7d535a
	cutoff = ULLONG_MAX / base;
kusano 7d535a
	cutlim = ULLONG_MAX % base;
kusano 7d535a
	for ( ; ; c = *s++) {
kusano 7d535a
		if (c >= '0' && c <= '9')
kusano 7d535a
			c -= '0';
kusano 7d535a
		else if (c >= 'A' && c <= 'Z')
kusano 7d535a
			c -= 'A' - 10;
kusano 7d535a
		else if (c >= 'a' && c <= 'z')
kusano 7d535a
			c -= 'a' - 10;
kusano 7d535a
		else
kusano 7d535a
			break;
kusano 7d535a
		if (c >= base)
kusano 7d535a
			break;
kusano 7d535a
		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
kusano 7d535a
			any = -1;
kusano 7d535a
		else {
kusano 7d535a
			any = 1;
kusano 7d535a
			acc *= base;
kusano 7d535a
			acc += c;
kusano 7d535a
		}
kusano 7d535a
	}
kusano 7d535a
	if (any < 0) {
kusano 7d535a
		acc = ULLONG_MAX;
kusano 7d535a
		errno = ERANGE;
kusano 7d535a
	} else if (!any) {
kusano 7d535a
noconv:
kusano 7d535a
		errno = EINVAL;
kusano 7d535a
	} else if (neg)
kusano 7d535a
		acc = -acc;
kusano 7d535a
	if (endptr != NULL)
kusano 7d535a
		*endptr = (char *)(any ? s - 1 : nptr);
kusano 7d535a
	return (acc);
kusano 7d535a
}