xref: /illumos-gate/usr/src/common/util/strtoul.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.8	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #if !defined(_BOOT) && !defined(_KMDB)
34*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
35*7c478bd9Sstevel@tonic-gate #endif /* !_BOOT && !_KMDB */
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <ctype.h>
38*7c478bd9Sstevel@tonic-gate #include <limits.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	DIGIT(x)	\
43*7c478bd9Sstevel@tonic-gate 	(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	MBASE	('z' - 'a' + 1 + 10)
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * The following macro is a local version of isalnum() which limits
49*7c478bd9Sstevel@tonic-gate  * alphabetic characters to the ranges a-z and A-Z; locale dependent
50*7c478bd9Sstevel@tonic-gate  * characters will not return 1. The members of a-z and A-Z are
51*7c478bd9Sstevel@tonic-gate  * assumed to be in ascending order and contiguous
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate #define	lisalnum(x)	\
54*7c478bd9Sstevel@tonic-gate 	(isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate unsigned long
57*7c478bd9Sstevel@tonic-gate strtoul(const char *str, char **nptr, int base)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	unsigned long val;
60*7c478bd9Sstevel@tonic-gate 	int c;
61*7c478bd9Sstevel@tonic-gate 	int xx;
62*7c478bd9Sstevel@tonic-gate 	unsigned long	multmax;
63*7c478bd9Sstevel@tonic-gate 	int neg = 0;
64*7c478bd9Sstevel@tonic-gate 	const char **ptr = (const char **)nptr;
65*7c478bd9Sstevel@tonic-gate 	const unsigned char	*ustr = (const unsigned char *)str;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (ptr != (const char **)0)
68*7c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr; /* in case no number is formed */
69*7c478bd9Sstevel@tonic-gate 	if (base < 0 || base > MBASE || base == 1) {
70*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
71*7c478bd9Sstevel@tonic-gate 		return (0); /* base is invalid -- should be a fatal error */
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 	if (!isalnum(c = *ustr)) {
74*7c478bd9Sstevel@tonic-gate 		while (isspace(c))
75*7c478bd9Sstevel@tonic-gate 			c = *++ustr;
76*7c478bd9Sstevel@tonic-gate 		switch (c) {
77*7c478bd9Sstevel@tonic-gate 		case '-':
78*7c478bd9Sstevel@tonic-gate 			neg++;
79*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
80*7c478bd9Sstevel@tonic-gate 		case '+':
81*7c478bd9Sstevel@tonic-gate 			c = *++ustr;
82*7c478bd9Sstevel@tonic-gate 		}
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	if (base == 0)
85*7c478bd9Sstevel@tonic-gate 		if (c != '0')
86*7c478bd9Sstevel@tonic-gate 			base = 10;
87*7c478bd9Sstevel@tonic-gate 		else if (ustr[1] == 'x' || ustr[1] == 'X')
88*7c478bd9Sstevel@tonic-gate 			base = 16;
89*7c478bd9Sstevel@tonic-gate 		else
90*7c478bd9Sstevel@tonic-gate 			base = 8;
91*7c478bd9Sstevel@tonic-gate 	/*
92*7c478bd9Sstevel@tonic-gate 	 * for any base > 10, the digits incrementally following
93*7c478bd9Sstevel@tonic-gate 	 *	9 are assumed to be "abc...z" or "ABC...Z"
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base)
96*7c478bd9Sstevel@tonic-gate 		return (0); /* no number formed */
97*7c478bd9Sstevel@tonic-gate 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
98*7c478bd9Sstevel@tonic-gate 	    isxdigit(ustr[2]))
99*7c478bd9Sstevel@tonic-gate 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	multmax = ULONG_MAX / (unsigned long)base;
102*7c478bd9Sstevel@tonic-gate 	val = DIGIT(c);
103*7c478bd9Sstevel@tonic-gate 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
104*7c478bd9Sstevel@tonic-gate 		if (val > multmax)
105*7c478bd9Sstevel@tonic-gate 			goto overflow;
106*7c478bd9Sstevel@tonic-gate 		val *= base;
107*7c478bd9Sstevel@tonic-gate 		if (ULONG_MAX - val < xx)
108*7c478bd9Sstevel@tonic-gate 			goto overflow;
109*7c478bd9Sstevel@tonic-gate 		val += xx;
110*7c478bd9Sstevel@tonic-gate 		c = *++ustr;
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	if (ptr != (const char **)0)
113*7c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr;
114*7c478bd9Sstevel@tonic-gate 	return (neg ? -val : val);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate overflow:
117*7c478bd9Sstevel@tonic-gate 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
118*7c478bd9Sstevel@tonic-gate 		;
119*7c478bd9Sstevel@tonic-gate 	if (ptr != (const char **)0)
120*7c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr;
121*7c478bd9Sstevel@tonic-gate 	errno = ERANGE;
122*7c478bd9Sstevel@tonic-gate 	return (ULONG_MAX);
123*7c478bd9Sstevel@tonic-gate }
124