187027Sfenner/*-
287027Sfenner * Copyright (c) 1992, 1993
387027Sfenner *	The Regents of the University of California.  All rights reserved.
487027Sfenner *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
1087027Sfenner * Redistribution and use in source and binary forms, with or without
1187027Sfenner * modification, are permitted provided that the following conditions
1287027Sfenner * are met:
1387027Sfenner * 1. Redistributions of source code must retain the above copyright
1487027Sfenner *    notice, this list of conditions and the following disclaimer.
1587027Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1687027Sfenner *    notice, this list of conditions and the following disclaimer in the
1787027Sfenner *    documentation and/or other materials provided with the distribution.
18251672Semaste * 3. Neither the name of the University nor the names of its contributors
1987027Sfenner *    may be used to endorse or promote products derived from this software
2087027Sfenner *    without specific prior written permission.
2187027Sfenner *
2287027Sfenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2387027Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2487027Sfenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2587027Sfenner * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2687027Sfenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2787027Sfenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2887027Sfenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2987027Sfenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3087027Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3187027Sfenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3287027Sfenner * SUCH DAMAGE.
3387027Sfenner */
3487027Sfenner
3587027Sfenner#if defined(LIBC_SCCS) && !defined(lint)
3687027Sfennerstatic char sccsid[] = "from @(#)strtoul.c	8.1 (Berkeley) 6/4/93";
3787027Sfenner#endif /* LIBC_SCCS and not lint */
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
4087027Sfenner
4187027Sfenner#include <ctype.h>
4287027Sfenner#include <errno.h>
4387027Sfenner#include <stdlib.h>
4487027Sfenner#include <inttypes.h>
45227753Stheraven#include "xlocale_private.h"
4687027Sfenner
4787027Sfenner/*
4887027Sfenner * Convert a string to a uintmax_t integer.
4987027Sfenner *
5087027Sfenner * Assumes that the upper and lower case
5187027Sfenner * alphabets and digits are each contiguous.
5287027Sfenner */
5387027Sfenneruintmax_t
54227753Stheravenstrtoumax_l(const char * __restrict nptr, char ** __restrict endptr, int base,
55227753Stheraven		locale_t locale)
5687027Sfenner{
5787027Sfenner	const char *s;
5887027Sfenner	uintmax_t acc;
5987494Sache	char c;
6087027Sfenner	uintmax_t cutoff;
6187494Sache	int neg, any, cutlim;
62227753Stheraven	FIX_LOCALE(locale);
6387027Sfenner
6487027Sfenner	/*
6587027Sfenner	 * See strtoimax for comments as to the logic used.
6687027Sfenner	 */
6787027Sfenner	s = nptr;
6887027Sfenner	do {
6987027Sfenner		c = *s++;
70227753Stheraven	} while (isspace_l((unsigned char)c, locale));
7187027Sfenner	if (c == '-') {
7287027Sfenner		neg = 1;
7387027Sfenner		c = *s++;
7487027Sfenner	} else {
7587027Sfenner		neg = 0;
7687027Sfenner		if (c == '+')
7787027Sfenner			c = *s++;
7887027Sfenner	}
7987027Sfenner	if ((base == 0 || base == 16) &&
80140536Sache	    c == '0' && (*s == 'x' || *s == 'X') &&
81140577Sache	    ((s[1] >= '0' && s[1] <= '9') ||
82140577Sache	    (s[1] >= 'A' && s[1] <= 'F') ||
83140577Sache	    (s[1] >= 'a' && s[1] <= 'f'))) {
8487027Sfenner		c = s[1];
8587027Sfenner		s += 2;
8687027Sfenner		base = 16;
8787027Sfenner	}
8887027Sfenner	if (base == 0)
8987027Sfenner		base = c == '0' ? 8 : 10;
9087027Sfenner	acc = any = 0;
9187027Sfenner	if (base < 2 || base > 36)
9287027Sfenner		goto noconv;
9387027Sfenner
9487027Sfenner	cutoff = UINTMAX_MAX / base;
9587027Sfenner	cutlim = UINTMAX_MAX % base;
9687027Sfenner	for ( ; ; c = *s++) {
9787494Sache		if (c >= '0' && c <= '9')
9887494Sache			c -= '0';
9987494Sache		else if (c >= 'A' && c <= 'Z')
10087494Sache			c -= 'A' - 10;
10187494Sache		else if (c >= 'a' && c <= 'z')
10287494Sache			c -= 'a' - 10;
10387027Sfenner		else
10487027Sfenner			break;
10587494Sache		if (c >= base)
10687027Sfenner			break;
10787494Sache		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
10887027Sfenner			any = -1;
10987027Sfenner		else {
11087027Sfenner			any = 1;
11187027Sfenner			acc *= base;
11287494Sache			acc += c;
11387027Sfenner		}
11487027Sfenner	}
11587027Sfenner	if (any < 0) {
11687027Sfenner		acc = UINTMAX_MAX;
11787027Sfenner		errno = ERANGE;
11887027Sfenner	} else if (!any) {
11987027Sfennernoconv:
12087027Sfenner		errno = EINVAL;
12187027Sfenner	} else if (neg)
12287027Sfenner		acc = -acc;
12387027Sfenner	if (endptr != NULL)
12487027Sfenner		*endptr = (char *)(any ? s - 1 : nptr);
12587027Sfenner	return (acc);
12687027Sfenner}
127227753Stheravenuintmax_t
128227753Stheravenstrtoumax(const char * __restrict nptr, char ** __restrict endptr, int base)
129227753Stheraven{
130227753Stheraven	return strtoumax_l(nptr, endptr, base, __get_locale());
131227753Stheraven}
132