1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/* $NetBSD: _strtoul.h,v 1.1 2008/08/20 12:42:26 joerg Exp $ */
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c) 1990, 1993
6219019Sgabor *	The Regents of the University of California.  All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor * 3. Neither the name of the University nor the names of its contributors
17219019Sgabor *    may be used to endorse or promote products derived from this software
18219019Sgabor *    without specific prior written permission.
19219019Sgabor *
20219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30219019Sgabor * SUCH DAMAGE.
31219019Sgabor *
32219019Sgabor * Original version ID:
33219019Sgabor * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
34219019Sgabor */
35219019Sgabor
36219019Sgabor/*
37219019Sgabor * function template for strtoul, strtoull and strtoumax.
38219019Sgabor *
39219019Sgabor * parameters:
40219019Sgabor *	_FUNCNAME  : function name
41219019Sgabor *      __UINT     : return type
42219019Sgabor *      __UINT_MAX : upper limit of the return type
43219019Sgabor */
44219019Sgabor
45219019Sgabor__UINT
46219019Sgabor_FUNCNAME(const char *nptr, char **endptr, int base)
47219019Sgabor{
48219019Sgabor	const char *s;
49219019Sgabor	__UINT acc, cutoff;
50219019Sgabor	unsigned char c;
51219019Sgabor	int any, cutlim, i, neg;
52219019Sgabor
53219019Sgabor	/* check base value */
54219019Sgabor	if (base && (base < 2 || base > 36)) {
55219019Sgabor#if !defined(_KERNEL) && !defined(_STANDALONE)
56219019Sgabor		errno = EINVAL;
57219019Sgabor		return (0);
58219019Sgabor#else
59219019Sgabor		panic("%s: invalid base %d", __func__, base);
60219019Sgabor#endif
61219019Sgabor	}
62219019Sgabor
63219019Sgabor	/*
64219019Sgabor	 * Skip white space and pick up leading +/- sign if any.
65219019Sgabor	 * If base is 0, allow 0x for hex and 0 for octal, else
66219019Sgabor	 * assume decimal; if base is already 16, allow 0x.
67219019Sgabor	 */
68219019Sgabor	s = nptr;
69219019Sgabor	do {
70219019Sgabor		c = *s++;
71219019Sgabor	} while (isspace(c));
72219019Sgabor	if (c == '-') {
73219019Sgabor		neg = 1;
74219019Sgabor		c = *s++;
75219019Sgabor	} else {
76219019Sgabor		neg = 0;
77219019Sgabor		if (c == '+')
78219019Sgabor			c = *s++;
79219019Sgabor	}
80219019Sgabor	if ((base == 0 || base == 16) &&
81219019Sgabor	    c == '0' && (*s == 'x' || *s == 'X')) {
82219019Sgabor		c = s[1];
83219019Sgabor		s += 2;
84219019Sgabor		base = 16;
85219019Sgabor	}
86219019Sgabor	if (base == 0)
87219019Sgabor		base = (c == '0' ? 8 : 10);
88219019Sgabor
89219019Sgabor	/*
90219019Sgabor	 * See strtol for comments as to the logic used.
91219019Sgabor	 */
92219019Sgabor	cutoff = __UINT_MAX / (__UINT)base;
93219019Sgabor	cutlim = (int)(__UINT_MAX % (__UINT)base);
94219019Sgabor	for (acc = 0, any = 0;; c = *s++) {
95219019Sgabor		if (isdigit(c))
96219019Sgabor			i = c - '0';
97219019Sgabor		else if (isalpha(c))
98219019Sgabor			i = c - (isupper(c) ? 'A' - 10 : 'a' - 10);
99219019Sgabor		else
100219019Sgabor			break;
101219019Sgabor		if (i >= base)
102219019Sgabor			break;
103219019Sgabor		if (any < 0)
104219019Sgabor			continue;
105219019Sgabor		if (acc > cutoff || (acc == cutoff && i > cutlim)) {
106219019Sgabor			acc = __UINT_MAX;
107219019Sgabor#if !defined(_KERNEL) && !defined(_STANDALONE)
108219019Sgabor			any = -1;
109219019Sgabor			errno = ERANGE;
110219019Sgabor#else
111219019Sgabor			any = 0;
112219019Sgabor			break;
113219019Sgabor#endif
114219019Sgabor		} else {
115219019Sgabor			any = 1;
116219019Sgabor			acc *= (__UINT)base;
117219019Sgabor			acc += i;
118219019Sgabor		}
119219019Sgabor	}
120219019Sgabor	if (neg && any > 0)
121219019Sgabor		acc = -acc;
122219019Sgabor	if (endptr != NULL)
123219019Sgabor		/* LINTED interface specification */
124219019Sgabor		*endptr = __DECONST(void *, any ? s - 1 : nptr);
125219019Sgabor	return (acc);
126219019Sgabor}
127