1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2003  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/*
19258945Sroberto * Copyright (c) 1990, 1993
20258945Sroberto *	The Regents of the University of California.  All rights reserved.
21258945Sroberto *
22258945Sroberto * Redistribution and use in source and binary forms, with or without
23258945Sroberto * modification, are permitted provided that the following conditions
24258945Sroberto * are met:
25258945Sroberto * 1. Redistributions of source code must retain the above copyright
26258945Sroberto *    notice, this list of conditions and the following disclaimer.
27258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
28258945Sroberto *    notice, this list of conditions and the following disclaimer in the
29258945Sroberto *    documentation and/or other materials provided with the distribution.
30258945Sroberto * 3. All advertising materials mentioning features or use of this software
31258945Sroberto *    must display the following acknowledgement:
32258945Sroberto *	This product includes software developed by the University of
33258945Sroberto *	California, Berkeley and its contributors.
34258945Sroberto * 4. Neither the name of the University nor the names of its contributors
35258945Sroberto *    may be used to endorse or promote products derived from this software
36258945Sroberto *    without specific prior written permission.
37258945Sroberto *
38258945Sroberto * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41258945Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42258945Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43258945Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44258945Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45258945Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46258945Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47258945Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48258945Sroberto * SUCH DAMAGE.
49258945Sroberto */
50258945Sroberto
51258945Sroberto/*! \file */
52258945Sroberto#if defined(LIBC_SCCS) && !defined(lint)
53258945Srobertostatic char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
54258945Sroberto#endif /* LIBC_SCCS and not lint */
55258945Sroberto
56258945Sroberto/* $Id: strtoul.c,v 1.7 2007/06/19 23:47:17 tbox Exp $ */
57258945Sroberto
58258945Sroberto#include <config.h>
59258945Sroberto
60258945Sroberto#include <limits.h>
61258945Sroberto#include <ctype.h>
62258945Sroberto#include <errno.h>
63258945Sroberto
64258945Sroberto#include <isc/stdlib.h>
65258945Sroberto#include <isc/util.h>
66258945Sroberto
67258945Sroberto/*!
68258945Sroberto * Convert a string to an unsigned long integer.
69258945Sroberto *
70258945Sroberto * Ignores `locale' stuff.  Assumes that the upper and lower case
71258945Sroberto * alphabets and digits are each contiguous.
72258945Sroberto */
73258945Srobertounsigned long
74258945Srobertoisc_strtoul(const char *nptr, char **endptr, int base) {
75258945Sroberto	const char *s = nptr;
76258945Sroberto	unsigned long acc;
77258945Sroberto	unsigned char c;
78258945Sroberto	unsigned long cutoff;
79258945Sroberto	int neg = 0, any, cutlim;
80258945Sroberto
81258945Sroberto	/*
82258945Sroberto	 * See strtol for comments as to the logic used.
83258945Sroberto	 */
84258945Sroberto	do {
85258945Sroberto		c = *s++;
86258945Sroberto	} while (isspace(c));
87258945Sroberto	if (c == '-') {
88258945Sroberto		neg = 1;
89258945Sroberto		c = *s++;
90258945Sroberto	} else if (c == '+')
91258945Sroberto		c = *s++;
92258945Sroberto	if ((base == 0 || base == 16) &&
93258945Sroberto	    c == '0' && (*s == 'x' || *s == 'X')) {
94258945Sroberto		c = s[1];
95258945Sroberto		s += 2;
96258945Sroberto		base = 16;
97258945Sroberto	}
98258945Sroberto	if (base == 0)
99258945Sroberto		base = c == '0' ? 8 : 10;
100258945Sroberto	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
101258945Sroberto	cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
102258945Sroberto	for (acc = 0, any = 0;; c = *s++) {
103258945Sroberto		if (!isascii(c))
104258945Sroberto			break;
105258945Sroberto		if (isdigit(c))
106258945Sroberto			c -= '0';
107258945Sroberto		else if (isalpha(c))
108258945Sroberto			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
109258945Sroberto		else
110258945Sroberto			break;
111258945Sroberto		if (c >= base)
112258945Sroberto			break;
113258945Sroberto		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
114258945Sroberto			any = -1;
115258945Sroberto		else {
116258945Sroberto			any = 1;
117258945Sroberto			acc *= base;
118258945Sroberto			acc += c;
119258945Sroberto		}
120258945Sroberto	}
121258945Sroberto	if (any < 0) {
122258945Sroberto		acc = ULONG_MAX;
123258945Sroberto		errno = ERANGE;
124258945Sroberto	} else if (neg)
125258945Sroberto		acc = -acc;
126258945Sroberto	if (endptr != 0)
127258945Sroberto		DE_CONST(any ? s - 1 : nptr, *endptr);
128258945Sroberto	return (acc);
129258945Sroberto}
130