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