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