xref: /xnu-10002.81.5/iokit/Kernel/IOStringFuncs.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions /*
30*5e3eaea3SApple OSS Distributions  *      Copyright (c) 1995 NeXT Computer, Inc.  All rights reserved.
31*5e3eaea3SApple OSS Distributions  *
32*5e3eaea3SApple OSS Distributions  * strol.c - The functions strtol() & strtoul() are exported as public API
33*5e3eaea3SApple OSS Distributions  *           via the header file ~driverkit/generalFuncs.h
34*5e3eaea3SApple OSS Distributions  *
35*5e3eaea3SApple OSS Distributions  * HISTORY
36*5e3eaea3SApple OSS Distributions  * 25-Oct-1995    Dean Reece at NeXT
37*5e3eaea3SApple OSS Distributions  *      Created based on BSD4.4's strtol.c & strtoul.c.
38*5e3eaea3SApple OSS Distributions  *      Removed dependency on _ctype_ by static versions of isupper()...
39*5e3eaea3SApple OSS Distributions  *      Added support for "0b101..." binary constants.
40*5e3eaea3SApple OSS Distributions  *      Commented out references to errno.
41*5e3eaea3SApple OSS Distributions  */
42*5e3eaea3SApple OSS Distributions 
43*5e3eaea3SApple OSS Distributions /*
44*5e3eaea3SApple OSS Distributions  * Copyright (c) 1990, 1993
45*5e3eaea3SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
46*5e3eaea3SApple OSS Distributions  *
47*5e3eaea3SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
48*5e3eaea3SApple OSS Distributions  * modification, are permitted provided that the following conditions
49*5e3eaea3SApple OSS Distributions  * are met:
50*5e3eaea3SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
51*5e3eaea3SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
52*5e3eaea3SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
53*5e3eaea3SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
54*5e3eaea3SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
55*5e3eaea3SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
56*5e3eaea3SApple OSS Distributions  *    must display the following acknowledgement:
57*5e3eaea3SApple OSS Distributions  *	This product includes software developed by the University of
58*5e3eaea3SApple OSS Distributions  *	California, Berkeley and its contributors.
59*5e3eaea3SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
60*5e3eaea3SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
61*5e3eaea3SApple OSS Distributions  *    without specific prior written permission.
62*5e3eaea3SApple OSS Distributions  *
63*5e3eaea3SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64*5e3eaea3SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65*5e3eaea3SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66*5e3eaea3SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67*5e3eaea3SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68*5e3eaea3SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69*5e3eaea3SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70*5e3eaea3SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71*5e3eaea3SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72*5e3eaea3SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73*5e3eaea3SApple OSS Distributions  * SUCH DAMAGE.
74*5e3eaea3SApple OSS Distributions  */
75*5e3eaea3SApple OSS Distributions 
76*5e3eaea3SApple OSS Distributions /*
77*5e3eaea3SApple OSS Distributions  #include <string.h>
78*5e3eaea3SApple OSS Distributions  #include <stdlib.h>
79*5e3eaea3SApple OSS Distributions  #include <limits.h>
80*5e3eaea3SApple OSS Distributions  */
81*5e3eaea3SApple OSS Distributions #include <sys/types.h>
82*5e3eaea3SApple OSS Distributions #include <machine/limits.h>
83*5e3eaea3SApple OSS Distributions 
84*5e3eaea3SApple OSS Distributions 
85*5e3eaea3SApple OSS Distributions long strtol(const char *nptr, char **endptr, int base);
86*5e3eaea3SApple OSS Distributions unsigned long strtoul(const char *nptr, char **endptr, int base);
87*5e3eaea3SApple OSS Distributions quad_t strtoq(const char *nptr, char **endptr, int base);
88*5e3eaea3SApple OSS Distributions u_quad_t strtouq(const char *nptr, char **endptr, int base);
89*5e3eaea3SApple OSS Distributions 
90*5e3eaea3SApple OSS Distributions 
91*5e3eaea3SApple OSS Distributions typedef int BOOL;
92*5e3eaea3SApple OSS Distributions 
93*5e3eaea3SApple OSS Distributions static inline BOOL
isupper(char c)94*5e3eaea3SApple OSS Distributions isupper(char c)
95*5e3eaea3SApple OSS Distributions {
96*5e3eaea3SApple OSS Distributions 	return c >= 'A' && c <= 'Z';
97*5e3eaea3SApple OSS Distributions }
98*5e3eaea3SApple OSS Distributions 
99*5e3eaea3SApple OSS Distributions static inline BOOL
isalpha(char c)100*5e3eaea3SApple OSS Distributions isalpha(char c)
101*5e3eaea3SApple OSS Distributions {
102*5e3eaea3SApple OSS Distributions 	return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
103*5e3eaea3SApple OSS Distributions }
104*5e3eaea3SApple OSS Distributions 
105*5e3eaea3SApple OSS Distributions 
106*5e3eaea3SApple OSS Distributions static inline BOOL
isspace(char c)107*5e3eaea3SApple OSS Distributions isspace(char c)
108*5e3eaea3SApple OSS Distributions {
109*5e3eaea3SApple OSS Distributions 	return c == ' ' || c == '\t' || c == '\n' || c == '\12';
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions 
112*5e3eaea3SApple OSS Distributions static inline BOOL
isdigit(char c)113*5e3eaea3SApple OSS Distributions isdigit(char c)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions 	return c >= '0' && c <= '9';
116*5e3eaea3SApple OSS Distributions }
117*5e3eaea3SApple OSS Distributions 
118*5e3eaea3SApple OSS Distributions /*
119*5e3eaea3SApple OSS Distributions  * Convert a string to a long integer.
120*5e3eaea3SApple OSS Distributions  *
121*5e3eaea3SApple OSS Distributions  * Ignores `locale' stuff.  Assumes that the upper and lower case
122*5e3eaea3SApple OSS Distributions  * alphabets and digits are each contiguous.
123*5e3eaea3SApple OSS Distributions  */
124*5e3eaea3SApple OSS Distributions long
strtol(const char * nptr,char ** endptr,int base)125*5e3eaea3SApple OSS Distributions strtol(const char *nptr, char **endptr, int base)
126*5e3eaea3SApple OSS Distributions {
127*5e3eaea3SApple OSS Distributions 	const char *s = nptr;
128*5e3eaea3SApple OSS Distributions 	unsigned long acc;
129*5e3eaea3SApple OSS Distributions 	char c;
130*5e3eaea3SApple OSS Distributions 	unsigned long cutoff;
131*5e3eaea3SApple OSS Distributions 	int neg = 0, any, cutlim;
132*5e3eaea3SApple OSS Distributions 
133*5e3eaea3SApple OSS Distributions 	/*
134*5e3eaea3SApple OSS Distributions 	 * Skip white space and pick up leading +/- sign if any.
135*5e3eaea3SApple OSS Distributions 	 * If base is 0, allow 0x for hex and 0 for octal, else
136*5e3eaea3SApple OSS Distributions 	 * assume decimal; if base is already 16, allow 0x.
137*5e3eaea3SApple OSS Distributions 	 */
138*5e3eaea3SApple OSS Distributions 	do {
139*5e3eaea3SApple OSS Distributions 		c = *s++;
140*5e3eaea3SApple OSS Distributions 	} while (isspace(c));
141*5e3eaea3SApple OSS Distributions 	if (c == '-') {
142*5e3eaea3SApple OSS Distributions 		neg = 1;
143*5e3eaea3SApple OSS Distributions 		c = *s++;
144*5e3eaea3SApple OSS Distributions 	} else if (c == '+') {
145*5e3eaea3SApple OSS Distributions 		c = *s++;
146*5e3eaea3SApple OSS Distributions 	}
147*5e3eaea3SApple OSS Distributions 	if ((base == 0 || base == 16) &&
148*5e3eaea3SApple OSS Distributions 	    c == '0' && (*s == 'x' || *s == 'X')) {
149*5e3eaea3SApple OSS Distributions 		c = s[1];
150*5e3eaea3SApple OSS Distributions 		s += 2;
151*5e3eaea3SApple OSS Distributions 		base = 16;
152*5e3eaea3SApple OSS Distributions 	} else if ((base == 0 || base == 2) &&
153*5e3eaea3SApple OSS Distributions 	    c == '0' && (*s == 'b' || *s == 'B')) {
154*5e3eaea3SApple OSS Distributions 		c = s[1];
155*5e3eaea3SApple OSS Distributions 		s += 2;
156*5e3eaea3SApple OSS Distributions 		base = 2;
157*5e3eaea3SApple OSS Distributions 	}
158*5e3eaea3SApple OSS Distributions 	if (base == 0) {
159*5e3eaea3SApple OSS Distributions 		base = c == '0' ? 8 : 10;
160*5e3eaea3SApple OSS Distributions 	}
161*5e3eaea3SApple OSS Distributions 
162*5e3eaea3SApple OSS Distributions 	/*
163*5e3eaea3SApple OSS Distributions 	 * Compute the cutoff value between legal numbers and illegal
164*5e3eaea3SApple OSS Distributions 	 * numbers.  That is the largest legal value, divided by the
165*5e3eaea3SApple OSS Distributions 	 * base.  An input number that is greater than this value, if
166*5e3eaea3SApple OSS Distributions 	 * followed by a legal input character, is too big.  One that
167*5e3eaea3SApple OSS Distributions 	 * is equal to this value may be valid or not; the limit
168*5e3eaea3SApple OSS Distributions 	 * between valid and invalid numbers is then based on the last
169*5e3eaea3SApple OSS Distributions 	 * digit.  For instance, if the range for longs is
170*5e3eaea3SApple OSS Distributions 	 * [-2147483648..2147483647] and the input base is 10,
171*5e3eaea3SApple OSS Distributions 	 * cutoff will be set to 214748364 and cutlim to either
172*5e3eaea3SApple OSS Distributions 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
173*5e3eaea3SApple OSS Distributions 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
174*5e3eaea3SApple OSS Distributions 	 * the number is too big, and we will return a range error.
175*5e3eaea3SApple OSS Distributions 	 *
176*5e3eaea3SApple OSS Distributions 	 * Set any if any `digits' consumed; make it negative to indicate
177*5e3eaea3SApple OSS Distributions 	 * overflow.
178*5e3eaea3SApple OSS Distributions 	 */
179*5e3eaea3SApple OSS Distributions 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
180*5e3eaea3SApple OSS Distributions 	cutlim = ((int)(cutoff % (unsigned long)base));
181*5e3eaea3SApple OSS Distributions 	cutoff /= (unsigned long)base;
182*5e3eaea3SApple OSS Distributions 	for (acc = 0, any = 0;; c = *s++) {
183*5e3eaea3SApple OSS Distributions 		if (isdigit(c)) {
184*5e3eaea3SApple OSS Distributions 			c -= '0';
185*5e3eaea3SApple OSS Distributions 		} else if (isalpha(c)) {
186*5e3eaea3SApple OSS Distributions 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
187*5e3eaea3SApple OSS Distributions 		} else {
188*5e3eaea3SApple OSS Distributions 			break;
189*5e3eaea3SApple OSS Distributions 		}
190*5e3eaea3SApple OSS Distributions 		if (c >= base) {
191*5e3eaea3SApple OSS Distributions 			break;
192*5e3eaea3SApple OSS Distributions 		}
193*5e3eaea3SApple OSS Distributions 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
194*5e3eaea3SApple OSS Distributions 			any = -1;
195*5e3eaea3SApple OSS Distributions 		} else {
196*5e3eaea3SApple OSS Distributions 			any = 1;
197*5e3eaea3SApple OSS Distributions 			acc *= base;
198*5e3eaea3SApple OSS Distributions 			acc += c;
199*5e3eaea3SApple OSS Distributions 		}
200*5e3eaea3SApple OSS Distributions 	}
201*5e3eaea3SApple OSS Distributions 	if (any < 0) {
202*5e3eaea3SApple OSS Distributions 		acc = neg ? LONG_MIN : LONG_MAX;
203*5e3eaea3SApple OSS Distributions //		errno = ERANGE;
204*5e3eaea3SApple OSS Distributions 	} else if (neg) {
205*5e3eaea3SApple OSS Distributions 		acc = -acc;
206*5e3eaea3SApple OSS Distributions 	}
207*5e3eaea3SApple OSS Distributions 	if (endptr != 0) {
208*5e3eaea3SApple OSS Distributions 		if (any) {
209*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
210*5e3eaea3SApple OSS Distributions 		} else {
211*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
212*5e3eaea3SApple OSS Distributions 		}
213*5e3eaea3SApple OSS Distributions 	}
214*5e3eaea3SApple OSS Distributions 	return acc;
215*5e3eaea3SApple OSS Distributions }
216*5e3eaea3SApple OSS Distributions 
217*5e3eaea3SApple OSS Distributions unsigned long
strtoul(const char * nptr,char ** endptr,int base)218*5e3eaea3SApple OSS Distributions strtoul(const char *nptr, char **endptr, int base)
219*5e3eaea3SApple OSS Distributions {
220*5e3eaea3SApple OSS Distributions 	const char *s = nptr;
221*5e3eaea3SApple OSS Distributions 	unsigned long acc;
222*5e3eaea3SApple OSS Distributions 	char c;
223*5e3eaea3SApple OSS Distributions 	unsigned long cutoff;
224*5e3eaea3SApple OSS Distributions 	int neg = 0, any, cutlim;
225*5e3eaea3SApple OSS Distributions 
226*5e3eaea3SApple OSS Distributions 	/*
227*5e3eaea3SApple OSS Distributions 	 * See strtol for comments as to the logic used.
228*5e3eaea3SApple OSS Distributions 	 */
229*5e3eaea3SApple OSS Distributions 	do {
230*5e3eaea3SApple OSS Distributions 		c = *s++;
231*5e3eaea3SApple OSS Distributions 	} while (isspace(c));
232*5e3eaea3SApple OSS Distributions 	if (c == '-') {
233*5e3eaea3SApple OSS Distributions 		neg = 1;
234*5e3eaea3SApple OSS Distributions 		c = *s++;
235*5e3eaea3SApple OSS Distributions 	} else if (c == '+') {
236*5e3eaea3SApple OSS Distributions 		c = *s++;
237*5e3eaea3SApple OSS Distributions 	}
238*5e3eaea3SApple OSS Distributions 	if ((base == 0 || base == 16) &&
239*5e3eaea3SApple OSS Distributions 	    c == '0' && (*s == 'x' || *s == 'X')) {
240*5e3eaea3SApple OSS Distributions 		c = s[1];
241*5e3eaea3SApple OSS Distributions 		s += 2;
242*5e3eaea3SApple OSS Distributions 		base = 16;
243*5e3eaea3SApple OSS Distributions 	} else if ((base == 0 || base == 2) &&
244*5e3eaea3SApple OSS Distributions 	    c == '0' && (*s == 'b' || *s == 'B')) {
245*5e3eaea3SApple OSS Distributions 		c = s[1];
246*5e3eaea3SApple OSS Distributions 		s += 2;
247*5e3eaea3SApple OSS Distributions 		base = 2;
248*5e3eaea3SApple OSS Distributions 	}
249*5e3eaea3SApple OSS Distributions 	if (base == 0) {
250*5e3eaea3SApple OSS Distributions 		base = c == '0' ? 8 : 10;
251*5e3eaea3SApple OSS Distributions 	}
252*5e3eaea3SApple OSS Distributions 	cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
253*5e3eaea3SApple OSS Distributions 	cutlim = ((int)((unsigned long)ULONG_MAX % (unsigned long)base));
254*5e3eaea3SApple OSS Distributions 	for (acc = 0, any = 0;; c = *s++) {
255*5e3eaea3SApple OSS Distributions 		if (isdigit(c)) {
256*5e3eaea3SApple OSS Distributions 			c -= '0';
257*5e3eaea3SApple OSS Distributions 		} else if (isalpha(c)) {
258*5e3eaea3SApple OSS Distributions 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
259*5e3eaea3SApple OSS Distributions 		} else {
260*5e3eaea3SApple OSS Distributions 			break;
261*5e3eaea3SApple OSS Distributions 		}
262*5e3eaea3SApple OSS Distributions 		if (c >= base) {
263*5e3eaea3SApple OSS Distributions 			break;
264*5e3eaea3SApple OSS Distributions 		}
265*5e3eaea3SApple OSS Distributions 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
266*5e3eaea3SApple OSS Distributions 			any = -1;
267*5e3eaea3SApple OSS Distributions 		} else {
268*5e3eaea3SApple OSS Distributions 			any = 1;
269*5e3eaea3SApple OSS Distributions 			acc *= base;
270*5e3eaea3SApple OSS Distributions 			acc += c;
271*5e3eaea3SApple OSS Distributions 		}
272*5e3eaea3SApple OSS Distributions 	}
273*5e3eaea3SApple OSS Distributions 	if (any < 0) {
274*5e3eaea3SApple OSS Distributions 		acc = ULONG_MAX;
275*5e3eaea3SApple OSS Distributions //		errno = ERANGE;
276*5e3eaea3SApple OSS Distributions 	} else if (neg) {
277*5e3eaea3SApple OSS Distributions 		acc = -acc;
278*5e3eaea3SApple OSS Distributions 	}
279*5e3eaea3SApple OSS Distributions 	if (endptr != 0) {
280*5e3eaea3SApple OSS Distributions 		if (any) {
281*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
282*5e3eaea3SApple OSS Distributions 		} else {
283*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
284*5e3eaea3SApple OSS Distributions 		}
285*5e3eaea3SApple OSS Distributions 	}
286*5e3eaea3SApple OSS Distributions 
287*5e3eaea3SApple OSS Distributions 	return acc;
288*5e3eaea3SApple OSS Distributions }
289*5e3eaea3SApple OSS Distributions 
290*5e3eaea3SApple OSS Distributions /*
291*5e3eaea3SApple OSS Distributions  * Convert a string to a quad integer.
292*5e3eaea3SApple OSS Distributions  *
293*5e3eaea3SApple OSS Distributions  * Ignores `locale' stuff.  Assumes that the upper and lower case
294*5e3eaea3SApple OSS Distributions  * alphabets and digits are each contiguous.
295*5e3eaea3SApple OSS Distributions  */
296*5e3eaea3SApple OSS Distributions quad_t
strtoq(const char * nptr,char ** endptr,int base)297*5e3eaea3SApple OSS Distributions strtoq(const char *nptr, char **endptr, int base)
298*5e3eaea3SApple OSS Distributions {
299*5e3eaea3SApple OSS Distributions 	const char *s;
300*5e3eaea3SApple OSS Distributions 	u_quad_t acc;
301*5e3eaea3SApple OSS Distributions 	char c;
302*5e3eaea3SApple OSS Distributions 	u_quad_t qbase, cutoff;
303*5e3eaea3SApple OSS Distributions 	int neg, any, cutlim;
304*5e3eaea3SApple OSS Distributions 
305*5e3eaea3SApple OSS Distributions 	/*
306*5e3eaea3SApple OSS Distributions 	 * Skip white space and pick up leading +/- sign if any.
307*5e3eaea3SApple OSS Distributions 	 * If base is 0, allow 0x for hex and 0 for octal, else
308*5e3eaea3SApple OSS Distributions 	 * assume decimal; if base is already 16, allow 0x.
309*5e3eaea3SApple OSS Distributions 	 */
310*5e3eaea3SApple OSS Distributions 	s = nptr;
311*5e3eaea3SApple OSS Distributions 	do {
312*5e3eaea3SApple OSS Distributions 		c = *s++;
313*5e3eaea3SApple OSS Distributions 	} while (isspace(c));
314*5e3eaea3SApple OSS Distributions 	if (c == '-') {
315*5e3eaea3SApple OSS Distributions 		neg = 1;
316*5e3eaea3SApple OSS Distributions 		c = *s++;
317*5e3eaea3SApple OSS Distributions 	} else {
318*5e3eaea3SApple OSS Distributions 		neg = 0;
319*5e3eaea3SApple OSS Distributions 		if (c == '+') {
320*5e3eaea3SApple OSS Distributions 			c = *s++;
321*5e3eaea3SApple OSS Distributions 		}
322*5e3eaea3SApple OSS Distributions 	}
323*5e3eaea3SApple OSS Distributions 	if ((base == 0 || base == 16) &&
324*5e3eaea3SApple OSS Distributions 	    c == '0' && (*s == 'x' || *s == 'X')) {
325*5e3eaea3SApple OSS Distributions 		c = s[1];
326*5e3eaea3SApple OSS Distributions 		s += 2;
327*5e3eaea3SApple OSS Distributions 		base = 16;
328*5e3eaea3SApple OSS Distributions 	}
329*5e3eaea3SApple OSS Distributions 	if (base == 0) {
330*5e3eaea3SApple OSS Distributions 		base = c == '0' ? 8 : 10;
331*5e3eaea3SApple OSS Distributions 	}
332*5e3eaea3SApple OSS Distributions 
333*5e3eaea3SApple OSS Distributions 	/*
334*5e3eaea3SApple OSS Distributions 	 * Compute the cutoff value between legal numbers and illegal
335*5e3eaea3SApple OSS Distributions 	 * numbers.  That is the largest legal value, divided by the
336*5e3eaea3SApple OSS Distributions 	 * base.  An input number that is greater than this value, if
337*5e3eaea3SApple OSS Distributions 	 * followed by a legal input character, is too big.  One that
338*5e3eaea3SApple OSS Distributions 	 * is equal to this value may be valid or not; the limit
339*5e3eaea3SApple OSS Distributions 	 * between valid and invalid numbers is then based on the last
340*5e3eaea3SApple OSS Distributions 	 * digit.  For instance, if the range for quads is
341*5e3eaea3SApple OSS Distributions 	 * [-9223372036854775808..9223372036854775807] and the input base
342*5e3eaea3SApple OSS Distributions 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
343*5e3eaea3SApple OSS Distributions 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
344*5e3eaea3SApple OSS Distributions 	 * accumulated a value > 922337203685477580, or equal but the
345*5e3eaea3SApple OSS Distributions 	 * next digit is > 7 (or 8), the number is too big, and we will
346*5e3eaea3SApple OSS Distributions 	 * return a range error.
347*5e3eaea3SApple OSS Distributions 	 *
348*5e3eaea3SApple OSS Distributions 	 * Set any if any `digits' consumed; make it negative to indicate
349*5e3eaea3SApple OSS Distributions 	 * overflow.
350*5e3eaea3SApple OSS Distributions 	 */
351*5e3eaea3SApple OSS Distributions 	qbase = (unsigned)base;
352*5e3eaea3SApple OSS Distributions 	cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
353*5e3eaea3SApple OSS Distributions 	cutlim = ((int)(cutoff % qbase));
354*5e3eaea3SApple OSS Distributions 	cutoff /= qbase;
355*5e3eaea3SApple OSS Distributions 	for (acc = 0, any = 0;; c = *s++) {
356*5e3eaea3SApple OSS Distributions 		if (isdigit(c)) {
357*5e3eaea3SApple OSS Distributions 			c -= '0';
358*5e3eaea3SApple OSS Distributions 		} else if (isalpha(c)) {
359*5e3eaea3SApple OSS Distributions 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
360*5e3eaea3SApple OSS Distributions 		} else {
361*5e3eaea3SApple OSS Distributions 			break;
362*5e3eaea3SApple OSS Distributions 		}
363*5e3eaea3SApple OSS Distributions 		if (c >= base) {
364*5e3eaea3SApple OSS Distributions 			break;
365*5e3eaea3SApple OSS Distributions 		}
366*5e3eaea3SApple OSS Distributions 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
367*5e3eaea3SApple OSS Distributions 			any = -1;
368*5e3eaea3SApple OSS Distributions 		} else {
369*5e3eaea3SApple OSS Distributions 			any = 1;
370*5e3eaea3SApple OSS Distributions 			acc *= qbase;
371*5e3eaea3SApple OSS Distributions 			acc += c;
372*5e3eaea3SApple OSS Distributions 		}
373*5e3eaea3SApple OSS Distributions 	}
374*5e3eaea3SApple OSS Distributions 	if (any < 0) {
375*5e3eaea3SApple OSS Distributions 		acc = neg ? QUAD_MIN : QUAD_MAX;
376*5e3eaea3SApple OSS Distributions //		errno = ERANGE;
377*5e3eaea3SApple OSS Distributions 	} else if (neg) {
378*5e3eaea3SApple OSS Distributions 		acc = -acc;
379*5e3eaea3SApple OSS Distributions 	}
380*5e3eaea3SApple OSS Distributions 	if (endptr != 0) {
381*5e3eaea3SApple OSS Distributions 		if (any) {
382*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
383*5e3eaea3SApple OSS Distributions 		} else {
384*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
385*5e3eaea3SApple OSS Distributions 		}
386*5e3eaea3SApple OSS Distributions 	}
387*5e3eaea3SApple OSS Distributions 
388*5e3eaea3SApple OSS Distributions 	return acc;
389*5e3eaea3SApple OSS Distributions }
390*5e3eaea3SApple OSS Distributions 
391*5e3eaea3SApple OSS Distributions 
392*5e3eaea3SApple OSS Distributions /*
393*5e3eaea3SApple OSS Distributions  * Convert a string to an unsigned quad integer.
394*5e3eaea3SApple OSS Distributions  *
395*5e3eaea3SApple OSS Distributions  * Ignores `locale' stuff.  Assumes that the upper and lower case
396*5e3eaea3SApple OSS Distributions  * alphabets and digits are each contiguous.
397*5e3eaea3SApple OSS Distributions  */
398*5e3eaea3SApple OSS Distributions u_quad_t
strtouq(const char * nptr,char ** endptr,int base)399*5e3eaea3SApple OSS Distributions strtouq(const char *nptr,
400*5e3eaea3SApple OSS Distributions     char **endptr,
401*5e3eaea3SApple OSS Distributions     int base)
402*5e3eaea3SApple OSS Distributions {
403*5e3eaea3SApple OSS Distributions 	const char *s = nptr;
404*5e3eaea3SApple OSS Distributions 	u_quad_t acc;
405*5e3eaea3SApple OSS Distributions 	char c;
406*5e3eaea3SApple OSS Distributions 	u_quad_t qbase, cutoff;
407*5e3eaea3SApple OSS Distributions 	int neg, any, cutlim;
408*5e3eaea3SApple OSS Distributions 
409*5e3eaea3SApple OSS Distributions 	/*
410*5e3eaea3SApple OSS Distributions 	 * See strtoq for comments as to the logic used.
411*5e3eaea3SApple OSS Distributions 	 */
412*5e3eaea3SApple OSS Distributions 	s = nptr;
413*5e3eaea3SApple OSS Distributions 	do {
414*5e3eaea3SApple OSS Distributions 		c = *s++;
415*5e3eaea3SApple OSS Distributions 	} while (isspace(c));
416*5e3eaea3SApple OSS Distributions 	if (c == '-') {
417*5e3eaea3SApple OSS Distributions 		neg = 1;
418*5e3eaea3SApple OSS Distributions 		c = *s++;
419*5e3eaea3SApple OSS Distributions 	} else {
420*5e3eaea3SApple OSS Distributions 		neg = 0;
421*5e3eaea3SApple OSS Distributions 		if (c == '+') {
422*5e3eaea3SApple OSS Distributions 			c = *s++;
423*5e3eaea3SApple OSS Distributions 		}
424*5e3eaea3SApple OSS Distributions 	}
425*5e3eaea3SApple OSS Distributions 	if ((base == 0 || base == 16) &&
426*5e3eaea3SApple OSS Distributions 	    c == '0' && (*s == 'x' || *s == 'X')) {
427*5e3eaea3SApple OSS Distributions 		c = s[1];
428*5e3eaea3SApple OSS Distributions 		s += 2;
429*5e3eaea3SApple OSS Distributions 		base = 16;
430*5e3eaea3SApple OSS Distributions 	}
431*5e3eaea3SApple OSS Distributions 	if (base == 0) {
432*5e3eaea3SApple OSS Distributions 		base = c == '0' ? 8 : 10;
433*5e3eaea3SApple OSS Distributions 	}
434*5e3eaea3SApple OSS Distributions 	qbase = (unsigned)base;
435*5e3eaea3SApple OSS Distributions 	cutoff = (u_quad_t)UQUAD_MAX / qbase;
436*5e3eaea3SApple OSS Distributions 	cutlim = ((int)((u_quad_t)UQUAD_MAX % qbase));
437*5e3eaea3SApple OSS Distributions 	for (acc = 0, any = 0;; c = *s++) {
438*5e3eaea3SApple OSS Distributions 		if (isdigit(c)) {
439*5e3eaea3SApple OSS Distributions 			c -= '0';
440*5e3eaea3SApple OSS Distributions 		} else if (isalpha(c)) {
441*5e3eaea3SApple OSS Distributions 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
442*5e3eaea3SApple OSS Distributions 		} else {
443*5e3eaea3SApple OSS Distributions 			break;
444*5e3eaea3SApple OSS Distributions 		}
445*5e3eaea3SApple OSS Distributions 		if (c >= base) {
446*5e3eaea3SApple OSS Distributions 			break;
447*5e3eaea3SApple OSS Distributions 		}
448*5e3eaea3SApple OSS Distributions 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
449*5e3eaea3SApple OSS Distributions 			any = -1;
450*5e3eaea3SApple OSS Distributions 		} else {
451*5e3eaea3SApple OSS Distributions 			any = 1;
452*5e3eaea3SApple OSS Distributions 			acc *= qbase;
453*5e3eaea3SApple OSS Distributions 			acc += c;
454*5e3eaea3SApple OSS Distributions 		}
455*5e3eaea3SApple OSS Distributions 	}
456*5e3eaea3SApple OSS Distributions 	if (any < 0) {
457*5e3eaea3SApple OSS Distributions 		acc = UQUAD_MAX;
458*5e3eaea3SApple OSS Distributions //		errno = ERANGE;
459*5e3eaea3SApple OSS Distributions 	} else if (neg) {
460*5e3eaea3SApple OSS Distributions 		acc = -acc;
461*5e3eaea3SApple OSS Distributions 	}
462*5e3eaea3SApple OSS Distributions 	if (endptr != 0) {
463*5e3eaea3SApple OSS Distributions 		if (any) {
464*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
465*5e3eaea3SApple OSS Distributions 		} else {
466*5e3eaea3SApple OSS Distributions 			*endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
467*5e3eaea3SApple OSS Distributions 		}
468*5e3eaea3SApple OSS Distributions 	}
469*5e3eaea3SApple OSS Distributions 
470*5e3eaea3SApple OSS Distributions 	return acc;
471*5e3eaea3SApple OSS Distributions }
472