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