strtoumax.c revision 140536
187027Sfenner/*-
287027Sfenner * Copyright (c) 1992, 1993
387027Sfenner *	The Regents of the University of California.  All rights reserved.
487027Sfenner *
587027Sfenner * Redistribution and use in source and binary forms, with or without
687027Sfenner * modification, are permitted provided that the following conditions
787027Sfenner * are met:
887027Sfenner * 1. Redistributions of source code must retain the above copyright
987027Sfenner *    notice, this list of conditions and the following disclaimer.
1087027Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1187027Sfenner *    notice, this list of conditions and the following disclaimer in the
1287027Sfenner *    documentation and/or other materials provided with the distribution.
1387027Sfenner * 3. All advertising materials mentioning features or use of this software
1487027Sfenner *    must display the following acknowledgement:
1587027Sfenner *	This product includes software developed by the University of
1687027Sfenner *	California, Berkeley and its contributors.
1787027Sfenner * 4. Neither the name of the University nor the names of its contributors
1887027Sfenner *    may be used to endorse or promote products derived from this software
1987027Sfenner *    without specific prior written permission.
2087027Sfenner *
2187027Sfenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2287027Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2387027Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2487027Sfenner * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2587027Sfenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2687027Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2787027Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2887027Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2987027Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3087027Sfenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3187027Sfenner * SUCH DAMAGE.
3287027Sfenner */
3387027Sfenner
3487027Sfenner#if defined(LIBC_SCCS) && !defined(lint)
3587027Sfennerstatic char sccsid[] = "from @(#)strtoul.c	8.1 (Berkeley) 6/4/93";
3687027Sfenner#endif /* LIBC_SCCS and not lint */
3792986Sobrien#include <sys/cdefs.h>
3892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdlib/strtoumax.c 140536 2005-01-21 00:42:13Z ache $");
3987027Sfenner
4087027Sfenner#include <ctype.h>
4187027Sfenner#include <errno.h>
4287027Sfenner#include <stdlib.h>
4387027Sfenner#include <inttypes.h>
4487027Sfenner
4587027Sfenner/*
4687027Sfenner * Convert a string to a uintmax_t integer.
4787027Sfenner *
4887027Sfenner * Assumes that the upper and lower case
4987027Sfenner * alphabets and digits are each contiguous.
5087027Sfenner */
5187027Sfenneruintmax_t
52103012Stjrstrtoumax(const char * __restrict nptr, char ** __restrict endptr, int base)
5387027Sfenner{
5487027Sfenner	const char *s;
5587027Sfenner	uintmax_t acc;
5687494Sache	char c;
5787027Sfenner	uintmax_t cutoff;
5887494Sache	int neg, any, cutlim;
5987027Sfenner
6087027Sfenner	/*
6187027Sfenner	 * See strtoimax for comments as to the logic used.
6287027Sfenner	 */
6387027Sfenner	s = nptr;
6487027Sfenner	do {
6587027Sfenner		c = *s++;
6687494Sache	} while (isspace((unsigned char)c));
6787027Sfenner	if (c == '-') {
6887027Sfenner		neg = 1;
6987027Sfenner		c = *s++;
7087027Sfenner	} else {
7187027Sfenner		neg = 0;
7287027Sfenner		if (c == '+')
7387027Sfenner			c = *s++;
7487027Sfenner	}
7587027Sfenner	if ((base == 0 || base == 16) &&
76140536Sache	    c == '0' && (*s == 'x' || *s == 'X') &&
77140536Sache	    ((s[1] >= 'a' && s[1] <= 'f') ||
78140536Sache	     (s[1] >= 'A' && s[1] <= 'F') ||
79140536Sache	     (s[1] >= '0' && s[1] <= '9'))
80140536Sache	   ) {
8187027Sfenner		c = s[1];
8287027Sfenner		s += 2;
8387027Sfenner		base = 16;
8487027Sfenner	}
8587027Sfenner	if (base == 0)
8687027Sfenner		base = c == '0' ? 8 : 10;
8787027Sfenner	acc = any = 0;
8887027Sfenner	if (base < 2 || base > 36)
8987027Sfenner		goto noconv;
9087027Sfenner
9187027Sfenner	cutoff = UINTMAX_MAX / base;
9287027Sfenner	cutlim = UINTMAX_MAX % base;
9387027Sfenner	for ( ; ; c = *s++) {
9487494Sache		if (c >= '0' && c <= '9')
9587494Sache			c -= '0';
9687494Sache		else if (c >= 'A' && c <= 'Z')
9787494Sache			c -= 'A' - 10;
9887494Sache		else if (c >= 'a' && c <= 'z')
9987494Sache			c -= 'a' - 10;
10087027Sfenner		else
10187027Sfenner			break;
10287494Sache		if (c >= base)
10387027Sfenner			break;
10487494Sache		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
10587027Sfenner			any = -1;
10687027Sfenner		else {
10787027Sfenner			any = 1;
10887027Sfenner			acc *= base;
10987494Sache			acc += c;
11087027Sfenner		}
11187027Sfenner	}
11287027Sfenner	if (any < 0) {
11387027Sfenner		acc = UINTMAX_MAX;
11487027Sfenner		errno = ERANGE;
11587027Sfenner	} else if (!any) {
11687027Sfennernoconv:
11787027Sfenner		errno = EINVAL;
11887027Sfenner	} else if (neg)
11987027Sfenner		acc = -acc;
12087027Sfenner	if (endptr != NULL)
12187027Sfenner		*endptr = (char *)(any ? s - 1 : nptr);
12287027Sfenner	return (acc);
12387027Sfenner}
124