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