11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*	$OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $	*/
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*-
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 1992 The Regents of the University of California.
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. Neither the name of the University nor the names of its contributors
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    may be used to endorse or promote products derived from this software
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    without specific prior written permission.
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <ctype.h>
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <errno.h>
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <inttypes.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Convert a string to a uintmax_t.
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Ignores `locale' stuff.  Assumes that the upper and lower case
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * alphabets and digits are each contiguous.
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectuintmax_t
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstrtoumax(const char *nptr, char **endptr, int base)
441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	const char *s;
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	uintmax_t acc, cutoff;
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	int c;
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	int neg, any, cutlim;
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	/*
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 * See strtoq for comments as to the logic used.
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	 */
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	s = nptr;
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	do {
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		c = (unsigned char) *s++;
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	} while (isspace(c));
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (c == '-') {
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		neg = 1;
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		c = *s++;
60e734769276045c0cb89d4620fdd4ef35a0e6c335André Goddard Rosa	} else {
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		neg = 0;
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (c == '+')
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			c = *s++;
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if ((base == 0 || base == 16) &&
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    c == '0' && (*s == 'x' || *s == 'X')) {
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		c = s[1];
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		s += 2;
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		base = 16;
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (base == 0)
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		base = c == '0' ? 8 : 10;
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* BIONIC: avoid division and modulo for common cases */
751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define  CASE_BASE(x)                            \
761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            case x: cutoff = UINTMAX_MAX / x;    \
771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	            cutlim = UINTMAX_MAX % x;    \
781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		    break
79e734769276045c0cb89d4620fdd4ef35a0e6c335André Goddard Rosa
801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch (base) {
811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        CASE_BASE(8);
821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	CASE_BASE(10);
831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	CASE_BASE(16);
841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	default:
851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    cutoff = UINTMAX_MAX / base;
861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    cutlim = UINTMAX_MAX % base;
871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
88e734769276045c0cb89d4620fdd4ef35a0e6c335André Goddard Rosa
891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (isdigit(c))
911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			c -= '0';
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		else if (isalpha(c))
931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		else
951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			break;
961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (c >= base)
971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			break;
981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (any < 0)
991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			continue;
1001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if (acc > cutoff || (acc == cutoff && c > cutlim)) {
1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			any = -1;
1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			acc = UINTMAX_MAX;
1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			errno = ERANGE;
1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		} else {
1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			any = 1;
1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			acc *= (uintmax_t)base;
1071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project			acc += c;
1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		}
1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
1101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (neg && any > 0)
1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		acc = -acc;
1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (endptr != 0)
1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		*endptr = (char *) (any ? s - 1 : nptr);
1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return (acc);
1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
116