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