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