Blame port/strtoull.c

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