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