blob: f83a317deebdca127486449082ff79933bc0cd14 [file] [log] [blame]
Elliott Hughes1921dce2017-12-19 10:27:27 -08001/*
2 * Copyright (c) 1992 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <ctype.h>
31#include <errno.h>
32#include <inttypes.h>
33#include <limits.h>
34#include <stdlib.h>
35
36template <typename T, T Min, T Max> T StrToI(const char* nptr, char** endptr, int base) {
37 // Ensure that base is between 2 and 36 inclusive, or the special value of 0.
38 if (base < 0 || base == 1 || base > 36) {
39 if (endptr != nullptr) *endptr = const_cast<char*>(nptr);
40 errno = EINVAL;
41 return 0;
42 }
43
44 // Skip white space and pick up leading +/- sign if any.
45 // If base is 0, allow 0x for hex and 0 for octal, else
46 // assume decimal; if base is already 16, allow 0x.
47 const char* s = nptr;
48 int c;
49 do {
50 c = static_cast<unsigned char>(*s++);
51 } while (isspace(c));
52 int neg;
53 if (c == '-') {
54 neg = 1;
55 c = *s++;
56 } else {
57 neg = 0;
58 if (c == '+') c = *s++;
59 }
60 if ((base == 0 || base == 16) && c == '0' &&
61 (*s == 'x' || *s == 'X') && isxdigit(static_cast<unsigned char>(s[1]))) {
62 c = s[1];
63 s += 2;
64 base = 16;
65 }
66 if (base == 0) base = (c == '0') ? 8 : 10;
67
68 // Compute the cutoff value between legal numbers and illegal
69 // numbers. That is the largest legal value, divided by the
70 // base. An input number that is greater than this value, if
71 // followed by a legal input character, is too big. One that
72 // is equal to this value may be valid or not; the limit
73 // between valid and invalid numbers is then based on the last
74 // digit. For instance, if the range for intmax_t is
75 // [-9223372036854775808..9223372036854775807] and the input base
76 // is 10, cutoff will be set to 922337203685477580 and cutlim to
77 // either 7 (neg==0) or 8 (neg==1), meaning that if we have
78 // accumulated a value > 922337203685477580, or equal but the
79 // next digit is > 7 (or 8), the number is too big, and we will
80 // return a range error.
81 T cutoff = neg ? Min : Max;
82 int cutlim = cutoff % base;
83 cutoff /= base;
84 if (neg) {
85 if (cutlim > 0) {
86 cutlim -= base;
87 cutoff += 1;
88 }
89 cutlim = -cutlim;
90 }
91
92 // Set `any` if any digits consumed; make it negative to indicate overflow.
93 int any = 0;
94 T acc = 0;
95 for (; ; c = static_cast<unsigned char>(*s++)) {
96 if (isdigit(c)) {
97 c -= '0';
98 } else if (isalpha(c)) {
99 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
100 } else {
101 break;
102 }
103 if (c >= base) break;
104 if (any < 0) continue;
105 if (neg) {
106 if (acc < cutoff || (acc == cutoff && c > cutlim)) {
107 any = -1;
108 acc = Min;
109 errno = ERANGE;
110 } else {
111 any = 1;
112 acc *= base;
113 acc -= c;
114 }
115 } else {
116 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
117 any = -1;
118 acc = Max;
119 errno = ERANGE;
120 } else {
121 any = 1;
122 acc *= base;
123 acc += c;
124 }
125 }
126 }
127 if (endptr != nullptr) *endptr = const_cast<char*>(any ? s - 1 : nptr);
128 return acc;
129}
130
131template <typename T, T Max> T StrToU(const char* nptr, char** endptr, int base) {
132 if (base < 0 || base == 1 || base > 36) {
133 if (endptr != nullptr) *endptr = const_cast<char*>(nptr);
134 errno = EINVAL;
135 return 0;
136 }
137
138 const char* s = nptr;
139 int c;
140 do {
141 c = static_cast<unsigned char>(*s++);
142 } while (isspace(c));
143 int neg;
144 if (c == '-') {
145 neg = 1;
146 c = *s++;
147 } else {
148 neg = 0;
149 if (c == '+') c = *s++;
150 }
151 if ((base == 0 || base == 16) && c == '0' &&
152 (*s == 'x' || *s == 'X') && isxdigit(static_cast<unsigned char>(s[1]))) {
153 c = s[1];
154 s += 2;
155 base = 16;
156 }
157 if (base == 0) base = (c == '0') ? 8 : 10;
158
159 T cutoff = Max / static_cast<T>(base);
160 int cutlim = Max % static_cast<T>(base);
161 T acc = 0;
162 int any = 0;
163 for (; ; c = static_cast<unsigned char>(*s++)) {
164 if (isdigit(c)) {
165 c -= '0';
166 } else if (isalpha(c)) {
167 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
168 } else {
169 break;
170 }
171 if (c >= base) break;
172 if (any < 0) continue;
173 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
174 any = -1;
175 acc = Max;
176 errno = ERANGE;
177 } else {
178 any = 1;
179 acc *= static_cast<T>(base);
180 acc += c;
181 }
182 }
183 if (neg && any > 0) acc = -acc;
184 if (endptr != nullptr) *endptr = const_cast<char*>(any ? s - 1 : nptr);
185 return acc;
186}
187
188int atoi(const char* s) {
189 return strtol(s, nullptr, 10);
190}
191
192long atol(const char* s) {
193 return strtol(s, nullptr, 10);
194}
195
196long long atoll(const char* s) {
197 return strtoll(s, nullptr, 10);
198}
199
200intmax_t strtoimax(const char* s, char** end, int base) {
201 return StrToI<intmax_t, INTMAX_MIN, INTMAX_MAX>(s, end, base);
202}
203
204long strtol(const char* s, char** end, int base) {
205 return StrToI<long, LONG_MIN, LONG_MAX>(s, end, base);
206}
207
208long long strtoll(const char* s, char** end, int base) {
209 return StrToI<long long, LLONG_MIN, LLONG_MAX>(s, end, base);
210}
211
212// Public API since L, but not in any header.
213extern "C" long long strtoq(const char* s, char** end, int base) {
214 return strtoll(s, end, base);
215}
216
217unsigned long strtoul(const char* s, char** end, int base) {
218 return StrToU<unsigned long, ULONG_MAX>(s, end, base);
219}
220
221unsigned long long strtoull(const char* s, char** end, int base) {
222 return StrToU<unsigned long long, ULLONG_MAX>(s, end, base);
223}
224
225uintmax_t strtoumax(const char* s, char** end, int base) {
226 return StrToU<uintmax_t, UINTMAX_MAX>(s, end, base);
227}
228
229// Public API since L, but not in any header.
230extern "C" unsigned long long strtouq(const char* s, char** end, int base) {
231 return strtoull(s, end, base);
232}