xref: /freebsd-src/lib/libc/iconv/_strtoul.h (revision d9dc1603d6e48cca84cad3ebe859129131b8387c)
1ad30f8e7SGabor Kovesdan /* $NetBSD: _strtoul.h,v 1.1 2008/08/20 12:42:26 joerg Exp $ */
2ad30f8e7SGabor Kovesdan 
3ad30f8e7SGabor Kovesdan /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
6ad30f8e7SGabor Kovesdan  * Copyright (c) 1990, 1993
7ad30f8e7SGabor Kovesdan  *	The Regents of the University of California.  All rights reserved.
8ad30f8e7SGabor Kovesdan  *
9ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan  * are met:
12ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan  * 3. Neither the name of the University nor the names of its contributors
18ad30f8e7SGabor Kovesdan  *    may be used to endorse or promote products derived from this software
19ad30f8e7SGabor Kovesdan  *    without specific prior written permission.
20ad30f8e7SGabor Kovesdan  *
21ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
32ad30f8e7SGabor Kovesdan  *
33ad30f8e7SGabor Kovesdan  * Original version ID:
34ad30f8e7SGabor Kovesdan  * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
35ad30f8e7SGabor Kovesdan  */
36ad30f8e7SGabor Kovesdan 
37ad30f8e7SGabor Kovesdan /*
38ad30f8e7SGabor Kovesdan  * function template for strtoul, strtoull and strtoumax.
39ad30f8e7SGabor Kovesdan  *
40ad30f8e7SGabor Kovesdan  * parameters:
41ad30f8e7SGabor Kovesdan  *	_FUNCNAME  : function name
42ad30f8e7SGabor Kovesdan  *      __UINT     : return type
43ad30f8e7SGabor Kovesdan  *      __UINT_MAX : upper limit of the return type
44ad30f8e7SGabor Kovesdan  */
45ad30f8e7SGabor Kovesdan 
46ad30f8e7SGabor Kovesdan __UINT
_FUNCNAME(const char * nptr,char ** endptr,int base)47ad30f8e7SGabor Kovesdan _FUNCNAME(const char *nptr, char **endptr, int base)
48ad30f8e7SGabor Kovesdan {
49ad30f8e7SGabor Kovesdan 	const char *s;
50ad30f8e7SGabor Kovesdan 	__UINT acc, cutoff;
51ad30f8e7SGabor Kovesdan 	unsigned char c;
52ad30f8e7SGabor Kovesdan 	int any, cutlim, i, neg;
53ad30f8e7SGabor Kovesdan 
54ad30f8e7SGabor Kovesdan 	/* check base value */
55ad30f8e7SGabor Kovesdan 	if (base && (base < 2 || base > 36)) {
56ad30f8e7SGabor Kovesdan #if !defined(_KERNEL) && !defined(_STANDALONE)
57ad30f8e7SGabor Kovesdan 		errno = EINVAL;
58ad30f8e7SGabor Kovesdan 		return (0);
59ad30f8e7SGabor Kovesdan #else
60ad30f8e7SGabor Kovesdan 		panic("%s: invalid base %d", __func__, base);
61ad30f8e7SGabor Kovesdan #endif
62ad30f8e7SGabor Kovesdan 	}
63ad30f8e7SGabor Kovesdan 
64ad30f8e7SGabor Kovesdan 	/*
65ad30f8e7SGabor Kovesdan 	 * Skip white space and pick up leading +/- sign if any.
66ad30f8e7SGabor Kovesdan 	 * If base is 0, allow 0x for hex and 0 for octal, else
67ad30f8e7SGabor Kovesdan 	 * assume decimal; if base is already 16, allow 0x.
68ad30f8e7SGabor Kovesdan 	 */
69ad30f8e7SGabor Kovesdan 	s = nptr;
70ad30f8e7SGabor Kovesdan 	do {
71ad30f8e7SGabor Kovesdan 		c = *s++;
72ad30f8e7SGabor Kovesdan 	} while (isspace(c));
73ad30f8e7SGabor Kovesdan 	if (c == '-') {
74ad30f8e7SGabor Kovesdan 		neg = 1;
75ad30f8e7SGabor Kovesdan 		c = *s++;
76ad30f8e7SGabor Kovesdan 	} else {
77ad30f8e7SGabor Kovesdan 		neg = 0;
78ad30f8e7SGabor Kovesdan 		if (c == '+')
79ad30f8e7SGabor Kovesdan 			c = *s++;
80ad30f8e7SGabor Kovesdan 	}
81ad30f8e7SGabor Kovesdan 	if ((base == 0 || base == 16) &&
82b8b6bef4SDag-Erling Smørgrav 	    c == '0' && (*s == 'x' || *s == 'X') &&
83b8b6bef4SDag-Erling Smørgrav 	    ((s[1] >= '0' && s[1] <= '9') ||
84b8b6bef4SDag-Erling Smørgrav 	    (s[1] >= 'A' && s[1] <= 'F') ||
85b8b6bef4SDag-Erling Smørgrav 	    (s[1] >= 'a' && s[1] <= 'f'))) {
86ad30f8e7SGabor Kovesdan 		c = s[1];
87ad30f8e7SGabor Kovesdan 		s += 2;
88ad30f8e7SGabor Kovesdan 		base = 16;
89ad30f8e7SGabor Kovesdan 	}
90*d9dc1603SDag-Erling Smørgrav 	if ((base == 0 || base == 2) &&
91*d9dc1603SDag-Erling Smørgrav 	    c == '0' && (*s == 'b' || *s == 'B') &&
92*d9dc1603SDag-Erling Smørgrav 	    (s[1] >= '0' && s[1] <= '1')) {
93*d9dc1603SDag-Erling Smørgrav 		c = s[1];
94*d9dc1603SDag-Erling Smørgrav 		s += 2;
95*d9dc1603SDag-Erling Smørgrav 		base = 2;
96*d9dc1603SDag-Erling Smørgrav 	}
97ad30f8e7SGabor Kovesdan 	if (base == 0)
98ad30f8e7SGabor Kovesdan 		base = (c == '0' ? 8 : 10);
99ad30f8e7SGabor Kovesdan 
100ad30f8e7SGabor Kovesdan 	/*
101ad30f8e7SGabor Kovesdan 	 * See strtol for comments as to the logic used.
102ad30f8e7SGabor Kovesdan 	 */
103ad30f8e7SGabor Kovesdan 	cutoff = __UINT_MAX / (__UINT)base;
104ad30f8e7SGabor Kovesdan 	cutlim = (int)(__UINT_MAX % (__UINT)base);
105ad30f8e7SGabor Kovesdan 	for (acc = 0, any = 0;; c = *s++) {
106ad30f8e7SGabor Kovesdan 		if (isdigit(c))
107ad30f8e7SGabor Kovesdan 			i = c - '0';
108ad30f8e7SGabor Kovesdan 		else if (isalpha(c))
109ad30f8e7SGabor Kovesdan 			i = c - (isupper(c) ? 'A' - 10 : 'a' - 10);
110ad30f8e7SGabor Kovesdan 		else
111ad30f8e7SGabor Kovesdan 			break;
112ad30f8e7SGabor Kovesdan 		if (i >= base)
113ad30f8e7SGabor Kovesdan 			break;
114ad30f8e7SGabor Kovesdan 		if (any < 0)
115ad30f8e7SGabor Kovesdan 			continue;
116ad30f8e7SGabor Kovesdan 		if (acc > cutoff || (acc == cutoff && i > cutlim)) {
117ad30f8e7SGabor Kovesdan 			acc = __UINT_MAX;
118ad30f8e7SGabor Kovesdan #if !defined(_KERNEL) && !defined(_STANDALONE)
119ad30f8e7SGabor Kovesdan 			any = -1;
120ad30f8e7SGabor Kovesdan 			errno = ERANGE;
121ad30f8e7SGabor Kovesdan #else
122ad30f8e7SGabor Kovesdan 			any = 0;
123ad30f8e7SGabor Kovesdan 			break;
124ad30f8e7SGabor Kovesdan #endif
125ad30f8e7SGabor Kovesdan 		} else {
126ad30f8e7SGabor Kovesdan 			any = 1;
127ad30f8e7SGabor Kovesdan 			acc *= (__UINT)base;
128ad30f8e7SGabor Kovesdan 			acc += i;
129ad30f8e7SGabor Kovesdan 		}
130ad30f8e7SGabor Kovesdan 	}
131ad30f8e7SGabor Kovesdan 	if (neg && any > 0)
132ad30f8e7SGabor Kovesdan 		acc = -acc;
133ad30f8e7SGabor Kovesdan 	if (endptr != NULL)
134ad30f8e7SGabor Kovesdan 		/* LINTED interface specification */
135ad30f8e7SGabor Kovesdan 		*endptr = __DECONST(void *, any ? s - 1 : nptr);
136ad30f8e7SGabor Kovesdan 	return (acc);
137ad30f8e7SGabor Kovesdan }
138