Blame port/strtoull.c

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