1*12904384SSimon J. Gerraty /* $NetBSD: _strtol.h,v 1.11 2017/07/06 21:08:44 joerg Exp $ */
2*12904384SSimon J. Gerraty
3*12904384SSimon J. Gerraty /*-
4*12904384SSimon J. Gerraty * Copyright (c) 1990, 1993
5*12904384SSimon J. Gerraty * The Regents of the University of California. All rights reserved.
6*12904384SSimon J. Gerraty *
7*12904384SSimon J. Gerraty * Redistribution and use in source and binary forms, with or without
8*12904384SSimon J. Gerraty * modification, are permitted provided that the following conditions
9*12904384SSimon J. Gerraty * are met:
10*12904384SSimon J. Gerraty * 1. Redistributions of source code must retain the above copyright
11*12904384SSimon J. Gerraty * notice, this list of conditions and the following disclaimer.
12*12904384SSimon J. Gerraty * 2. Redistributions in binary form must reproduce the above copyright
13*12904384SSimon J. Gerraty * notice, this list of conditions and the following disclaimer in the
14*12904384SSimon J. Gerraty * documentation and/or other materials provided with the distribution.
15*12904384SSimon J. Gerraty * 3. Neither the name of the University nor the names of its contributors
16*12904384SSimon J. Gerraty * may be used to endorse or promote products derived from this software
17*12904384SSimon J. Gerraty * without specific prior written permission.
18*12904384SSimon J. Gerraty *
19*12904384SSimon J. Gerraty * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*12904384SSimon J. Gerraty * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*12904384SSimon J. Gerraty * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*12904384SSimon J. Gerraty * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*12904384SSimon J. Gerraty * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*12904384SSimon J. Gerraty * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*12904384SSimon J. Gerraty * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*12904384SSimon J. Gerraty * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*12904384SSimon J. Gerraty * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*12904384SSimon J. Gerraty * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*12904384SSimon J. Gerraty * SUCH DAMAGE.
30*12904384SSimon J. Gerraty *
31*12904384SSimon J. Gerraty * Original version ID:
32*12904384SSimon J. Gerraty * NetBSD: src/lib/libc/locale/_wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp
33*12904384SSimon J. Gerraty */
34*12904384SSimon J. Gerraty
35*12904384SSimon J. Gerraty /*
36*12904384SSimon J. Gerraty * function template for strtol, strtoll and strtoimax.
37*12904384SSimon J. Gerraty *
38*12904384SSimon J. Gerraty * parameters:
39*12904384SSimon J. Gerraty * _FUNCNAME : function name
40*12904384SSimon J. Gerraty * __INT : return type
41*12904384SSimon J. Gerraty * __INT_MIN : lower limit of the return type
42*12904384SSimon J. Gerraty * __INT_MAX : upper limit of the return type
43*12904384SSimon J. Gerraty */
44*12904384SSimon J. Gerraty #if defined(_KERNEL) || defined(_STANDALONE) || defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
45*12904384SSimon J. Gerraty __INT
_FUNCNAME(const char * nptr,char ** endptr,int base)46*12904384SSimon J. Gerraty _FUNCNAME(const char *nptr, char **endptr, int base)
47*12904384SSimon J. Gerraty #else
48*12904384SSimon J. Gerraty #include <locale.h>
49*12904384SSimon J. Gerraty #include "setlocale_local.h"
50*12904384SSimon J. Gerraty #define INT_FUNCNAME_(pre, name, post) pre ## name ## post
51*12904384SSimon J. Gerraty #define INT_FUNCNAME(pre, name, post) INT_FUNCNAME_(pre, name, post)
52*12904384SSimon J. Gerraty
53*12904384SSimon J. Gerraty static __INT
54*12904384SSimon J. Gerraty INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr,
55*12904384SSimon J. Gerraty int base, locale_t loc)
56*12904384SSimon J. Gerraty #endif
57*12904384SSimon J. Gerraty {
58*12904384SSimon J. Gerraty const char *s;
59*12904384SSimon J. Gerraty __INT acc, cutoff;
60*12904384SSimon J. Gerraty unsigned char c;
61*12904384SSimon J. Gerraty int i, neg, any, cutlim;
62*12904384SSimon J. Gerraty
63*12904384SSimon J. Gerraty _DIAGASSERT(nptr != NULL);
64*12904384SSimon J. Gerraty /* endptr may be NULL */
65*12904384SSimon J. Gerraty
66*12904384SSimon J. Gerraty /* check base value */
67*12904384SSimon J. Gerraty if (base && (base < 2 || base > 36)) {
68*12904384SSimon J. Gerraty #if !defined(_KERNEL) && !defined(_STANDALONE)
69*12904384SSimon J. Gerraty errno = EINVAL;
70*12904384SSimon J. Gerraty if (endptr != NULL)
71*12904384SSimon J. Gerraty /* LINTED interface specification */
72*12904384SSimon J. Gerraty *endptr = __UNCONST(nptr);
73*12904384SSimon J. Gerraty return 0;
74*12904384SSimon J. Gerraty #else
75*12904384SSimon J. Gerraty panic("%s: invalid base %d", __func__, base);
76*12904384SSimon J. Gerraty #endif
77*12904384SSimon J. Gerraty }
78*12904384SSimon J. Gerraty
79*12904384SSimon J. Gerraty /*
80*12904384SSimon J. Gerraty * Skip white space and pick up leading +/- sign if any.
81*12904384SSimon J. Gerraty * If base is 0, allow 0x for hex and 0 for octal, else
82*12904384SSimon J. Gerraty * assume decimal; if base is already 16, allow 0x.
83*12904384SSimon J. Gerraty */
84*12904384SSimon J. Gerraty s = nptr;
85*12904384SSimon J. Gerraty #if defined(_KERNEL) || defined(_STANDALONE) || \
86*12904384SSimon J. Gerraty defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)
87*12904384SSimon J. Gerraty do {
88*12904384SSimon J. Gerraty c = *s++;
89*12904384SSimon J. Gerraty } while (isspace(c));
90*12904384SSimon J. Gerraty #else
91*12904384SSimon J. Gerraty do {
92*12904384SSimon J. Gerraty c = *s++;
93*12904384SSimon J. Gerraty } while (isspace_l(c, loc));
94*12904384SSimon J. Gerraty #endif
95*12904384SSimon J. Gerraty if (c == '-') {
96*12904384SSimon J. Gerraty neg = 1;
97*12904384SSimon J. Gerraty c = *s++;
98*12904384SSimon J. Gerraty } else {
99*12904384SSimon J. Gerraty neg = 0;
100*12904384SSimon J. Gerraty if (c == '+')
101*12904384SSimon J. Gerraty c = *s++;
102*12904384SSimon J. Gerraty }
103*12904384SSimon J. Gerraty if ((base == 0 || base == 16) &&
104*12904384SSimon J. Gerraty c == '0' && (*s == 'x' || *s == 'X') &&
105*12904384SSimon J. Gerraty ((s[1] >= '0' && s[1] <= '9') ||
106*12904384SSimon J. Gerraty (s[1] >= 'a' && s[1] <= 'f') ||
107*12904384SSimon J. Gerraty (s[1] >= 'A' && s[1] <= 'F'))) {
108*12904384SSimon J. Gerraty c = s[1];
109*12904384SSimon J. Gerraty s += 2;
110*12904384SSimon J. Gerraty base = 16;
111*12904384SSimon J. Gerraty #if 0
112*12904384SSimon J. Gerraty } else if ((base == 0 || base == 2) &&
113*12904384SSimon J. Gerraty c == '0' && (*s == 'b' || *s == 'B') &&
114*12904384SSimon J. Gerraty (s[1] >= '0' && s[1] <= '1')) {
115*12904384SSimon J. Gerraty c = s[1];
116*12904384SSimon J. Gerraty s += 2;
117*12904384SSimon J. Gerraty base = 2;
118*12904384SSimon J. Gerraty #endif
119*12904384SSimon J. Gerraty } else if (base == 0)
120*12904384SSimon J. Gerraty base = (c == '0' ? 8 : 10);
121*12904384SSimon J. Gerraty
122*12904384SSimon J. Gerraty /*
123*12904384SSimon J. Gerraty * Compute the cutoff value between legal numbers and illegal
124*12904384SSimon J. Gerraty * numbers. That is the largest legal value, divided by the
125*12904384SSimon J. Gerraty * base. An input number that is greater than this value, if
126*12904384SSimon J. Gerraty * followed by a legal input character, is too big. One that
127*12904384SSimon J. Gerraty * is equal to this value may be valid or not; the limit
128*12904384SSimon J. Gerraty * between valid and invalid numbers is then based on the last
129*12904384SSimon J. Gerraty * digit. For instance, if the range for longs is
130*12904384SSimon J. Gerraty * [-2147483648..2147483647] and the input base is 10,
131*12904384SSimon J. Gerraty * cutoff will be set to 214748364 and cutlim to either
132*12904384SSimon J. Gerraty * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
133*12904384SSimon J. Gerraty * a value > 214748364, or equal but the next digit is > 7 (or 8),
134*12904384SSimon J. Gerraty * the number is too big, and we will return a range error.
135*12904384SSimon J. Gerraty *
136*12904384SSimon J. Gerraty * Set any if any `digits' consumed; make it negative to indicate
137*12904384SSimon J. Gerraty * overflow.
138*12904384SSimon J. Gerraty */
139*12904384SSimon J. Gerraty cutoff = (__INT)(neg ? __INT_MIN : __INT_MAX);
140*12904384SSimon J. Gerraty cutlim = (int)(cutoff % base);
141*12904384SSimon J. Gerraty cutoff /= base;
142*12904384SSimon J. Gerraty if (neg) {
143*12904384SSimon J. Gerraty if (cutlim > 0) {
144*12904384SSimon J. Gerraty cutlim -= base;
145*12904384SSimon J. Gerraty cutoff += 1;
146*12904384SSimon J. Gerraty }
147*12904384SSimon J. Gerraty cutlim = -cutlim;
148*12904384SSimon J. Gerraty }
149*12904384SSimon J. Gerraty for (acc = 0, any = 0;; c = *s++) {
150*12904384SSimon J. Gerraty if (c >= '0' && c <= '9')
151*12904384SSimon J. Gerraty i = c - '0';
152*12904384SSimon J. Gerraty else if (c >= 'a' && c <= 'z')
153*12904384SSimon J. Gerraty i = (c - 'a') + 10;
154*12904384SSimon J. Gerraty else if (c >= 'A' && c <= 'Z')
155*12904384SSimon J. Gerraty i = (c - 'A') + 10;
156*12904384SSimon J. Gerraty else
157*12904384SSimon J. Gerraty break;
158*12904384SSimon J. Gerraty if (i >= base)
159*12904384SSimon J. Gerraty break;
160*12904384SSimon J. Gerraty if (any < 0)
161*12904384SSimon J. Gerraty continue;
162*12904384SSimon J. Gerraty if (neg) {
163*12904384SSimon J. Gerraty if (acc < cutoff || (acc == cutoff && i > cutlim)) {
164*12904384SSimon J. Gerraty acc = __INT_MIN;
165*12904384SSimon J. Gerraty #if !defined(_KERNEL) && !defined(_STANDALONE)
166*12904384SSimon J. Gerraty any = -1;
167*12904384SSimon J. Gerraty errno = ERANGE;
168*12904384SSimon J. Gerraty #else
169*12904384SSimon J. Gerraty any = 0;
170*12904384SSimon J. Gerraty break;
171*12904384SSimon J. Gerraty #endif
172*12904384SSimon J. Gerraty } else {
173*12904384SSimon J. Gerraty any = 1;
174*12904384SSimon J. Gerraty acc *= base;
175*12904384SSimon J. Gerraty acc -= i;
176*12904384SSimon J. Gerraty }
177*12904384SSimon J. Gerraty } else {
178*12904384SSimon J. Gerraty if (acc > cutoff || (acc == cutoff && i > cutlim)) {
179*12904384SSimon J. Gerraty acc = __INT_MAX;
180*12904384SSimon J. Gerraty #if !defined(_KERNEL) && !defined(_STANDALONE)
181*12904384SSimon J. Gerraty any = -1;
182*12904384SSimon J. Gerraty errno = ERANGE;
183*12904384SSimon J. Gerraty #else
184*12904384SSimon J. Gerraty any = 0;
185*12904384SSimon J. Gerraty break;
186*12904384SSimon J. Gerraty #endif
187*12904384SSimon J. Gerraty } else {
188*12904384SSimon J. Gerraty any = 1;
189*12904384SSimon J. Gerraty acc *= base;
190*12904384SSimon J. Gerraty acc += i;
191*12904384SSimon J. Gerraty }
192*12904384SSimon J. Gerraty }
193*12904384SSimon J. Gerraty }
194*12904384SSimon J. Gerraty if (endptr != NULL)
195*12904384SSimon J. Gerraty /* LINTED interface specification */
196*12904384SSimon J. Gerraty *endptr = __UNCONST(any ? s - 1 : nptr);
197*12904384SSimon J. Gerraty return(acc);
198*12904384SSimon J. Gerraty }
199*12904384SSimon J. Gerraty
200*12904384SSimon J. Gerraty #if !defined(_KERNEL) && !defined(_STANDALONE) && \
201*12904384SSimon J. Gerraty !defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)
202*12904384SSimon J. Gerraty __INT
_FUNCNAME(const char * nptr,char ** endptr,int base)203*12904384SSimon J. Gerraty _FUNCNAME(const char *nptr, char **endptr, int base)
204*12904384SSimon J. Gerraty {
205*12904384SSimon J. Gerraty return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale());
206*12904384SSimon J. Gerraty }
207*12904384SSimon J. Gerraty
208*12904384SSimon J. Gerraty __INT
209*12904384SSimon J. Gerraty INT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc)
210*12904384SSimon J. Gerraty {
211*12904384SSimon J. Gerraty return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc);
212*12904384SSimon J. Gerraty }
213*12904384SSimon J. Gerraty #endif
214