strtoul.c revision 227753
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
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 *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
181573Srgrimes * 4. Neither the name of the University nor the names of its contributors
191573Srgrimes *    may be used to endorse or promote products derived from this software
201573Srgrimes *    without specific prior written permission.
211573Srgrimes *
221573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321573Srgrimes * SUCH DAMAGE.
331573Srgrimes */
341573Srgrimes
351573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
361573Srgrimesstatic char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
371573Srgrimes#endif /* LIBC_SCCS and not lint */
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdlib/strtoul.c 227753 2011-11-20 14:45:42Z theraven $");
401573Srgrimes
411573Srgrimes#include <limits.h>
421573Srgrimes#include <ctype.h>
431573Srgrimes#include <errno.h>
441573Srgrimes#include <stdlib.h>
45227753Stheraven#include "xlocale_private.h"
461573Srgrimes
471573Srgrimes/*
481573Srgrimes * Convert a string to an unsigned long integer.
491573Srgrimes *
5082975Sache * Assumes that the upper and lower case
511573Srgrimes * alphabets and digits are each contiguous.
521573Srgrimes */
531573Srgrimesunsigned long
54227753Stheravenstrtoul_l(const char * __restrict nptr, char ** __restrict endptr, int base, locale_t locale)
551573Srgrimes{
5687016Sache	const char *s;
5787016Sache	unsigned long acc;
5887494Sache	char c;
5987016Sache	unsigned long cutoff;
6087494Sache	int neg, any, cutlim;
61227753Stheraven	FIX_LOCALE(locale);
621573Srgrimes
631573Srgrimes	/*
641573Srgrimes	 * See strtol for comments as to the logic used.
651573Srgrimes	 */
6682975Sache	s = nptr;
671573Srgrimes	do {
681573Srgrimes		c = *s++;
69227753Stheraven	} while (isspace_l((unsigned char)c, locale));
701573Srgrimes	if (c == '-') {
711573Srgrimes		neg = 1;
721573Srgrimes		c = *s++;
7382975Sache	} else {
7482975Sache		neg = 0;
7582975Sache		if (c == '+')
7682975Sache			c = *s++;
7782975Sache	}
781573Srgrimes	if ((base == 0 || base == 16) &&
79140536Sache	    c == '0' && (*s == 'x' || *s == 'X') &&
80140577Sache	    ((s[1] >= '0' && s[1] <= '9') ||
81140577Sache	    (s[1] >= 'A' && s[1] <= 'F') ||
82140577Sache	    (s[1] >= 'a' && s[1] <= 'f'))) {
831573Srgrimes		c = s[1];
841573Srgrimes		s += 2;
851573Srgrimes		base = 16;
861573Srgrimes	}
871573Srgrimes	if (base == 0)
881573Srgrimes		base = c == '0' ? 8 : 10;
8982982Sache	acc = any = 0;
9087023Sfenner	if (base < 2 || base > 36)
9182975Sache		goto noconv;
9282975Sache
9382975Sache	cutoff = ULONG_MAX / base;
9482975Sache	cutlim = ULONG_MAX % base;
9582982Sache	for ( ; ; c = *s++) {
9687494Sache		if (c >= '0' && c <= '9')
9787494Sache			c -= '0';
9887494Sache		else if (c >= 'A' && c <= 'Z')
9987494Sache			c -= 'A' - 10;
10087494Sache		else if (c >= 'a' && c <= 'z')
10187494Sache			c -= 'a' - 10;
1021573Srgrimes		else
1031573Srgrimes			break;
10487494Sache		if (c >= base)
1051573Srgrimes			break;
10687494Sache		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
1071573Srgrimes			any = -1;
1081573Srgrimes		else {
1091573Srgrimes			any = 1;
1101573Srgrimes			acc *= base;
11187494Sache			acc += c;
1121573Srgrimes		}
1131573Srgrimes	}
1141573Srgrimes	if (any < 0) {
1151573Srgrimes		acc = ULONG_MAX;
1161573Srgrimes		errno = ERANGE;
11782975Sache	} else if (!any) {
11882975Sachenoconv:
11982975Sache		errno = EINVAL;
1201573Srgrimes	} else if (neg)
1211573Srgrimes		acc = -acc;
12282975Sache	if (endptr != NULL)
1231573Srgrimes		*endptr = (char *)(any ? s - 1 : nptr);
1241573Srgrimes	return (acc);
1251573Srgrimes}
126227753Stheravenunsigned long
127227753Stheravenstrtoul(const char * __restrict nptr, char ** __restrict endptr, int base)
128227753Stheraven{
129227753Stheraven	return strtoul_l(nptr, endptr, base, __get_locale());
130227753Stheraven}
131