]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/StdLib/NumericInt.c
058ad04959ba30c8fa3ccc9488d7e431814cd76d
[mirror_edk2.git] / StdLib / LibC / StdLib / NumericInt.c
1 /** @file
2 Integer Numeric Conversion Functions.
3
4 The atoi, atol, and atoll functions convert the initial portion of the string
5 pointed to by nptr to int, long int, and long long int representation,
6 respectively. They are equivalent to:
7 - atoi: (int)strtol(nptr, (char **)NULL, 10)
8 - atol: strtol(nptr, (char **)NULL, 10)
9 - atoll: strtoll(nptr, (char **)NULL, 10)
10
11 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
12 This program and the accompanying materials are licensed and made available under
13 the terms and conditions of the BSD License that accompanies this distribution.
14 The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php.
16
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 **/
20 #include <Uefi.h>
21 #include <Library/BaseLib.h>
22
23 #include <LibConfig.h>
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdlib.h>
29
30 /** The atoi function converts the initial portion of the string pointed to by
31 nptr to int representation. Except for the behavior on error, it is
32 equivalent to:
33 - (int)strtol(nptr, (char **)NULL, 10)
34
35 @return The atoi function returns the converted value.
36 **/
37 int
38 atoi(const char *nptr)
39 {
40 int Retval;
41 BOOLEAN Negative = FALSE;
42
43 while(isspace((const unsigned char)*nptr)) ++nptr; // Skip leading spaces
44
45 if(*nptr == '+') {
46 Negative = FALSE;
47 ++nptr;
48 }
49 else if(*nptr == '-') {
50 Negative = TRUE;
51 ++nptr;
52 }
53 Retval = (int)AsciiStrDecimalToUintn(nptr);
54 if(Negative) {
55 Retval = -Retval;
56 }
57 return Retval;
58 }
59
60 /** The atol function converts the initial portion of the string pointed to by
61 nptr to long int representation. Except for the behavior on error, it is
62 equivalent to:
63 - strtol(nptr, (char **)NULL, 10)
64
65 @return The atol function returns the converted value.
66 **/
67 long int
68 atol(const char *nptr)
69 {
70 long int Retval;
71 BOOLEAN Negative = FALSE;
72
73 while(isspace(*nptr)) ++nptr; // Skip leading spaces
74
75 if(*nptr == '+') {
76 Negative = FALSE;
77 ++nptr;
78 }
79 else if(*nptr == '-') {
80 Negative = TRUE;
81 ++nptr;
82 }
83 Retval = (long int)AsciiStrDecimalToUint64(nptr);
84 if(Negative) {
85 Retval = -Retval;
86 }
87 return Retval;
88 }
89
90 /** The atoll function converts the initial portion of the string pointed to by
91 nptr to long long int representation. Except for the behavior on error, it
92 is equivalent to:
93 - strtoll(nptr, (char **)NULL, 10)
94
95 @return The atoll function returns the converted value.
96 **/
97 long long int
98 atoll(const char *nptr)
99 {
100 long long int Retval;
101 BOOLEAN Negative = FALSE;
102
103 while(isspace(*nptr)) ++nptr; // Skip leading spaces
104
105 if(*nptr == '+') {
106 Negative = FALSE;
107 ++nptr;
108 }
109 else if(*nptr == '-') {
110 Negative = TRUE;
111 ++nptr;
112 }
113 Retval = (long long int)AsciiStrDecimalToUint64(nptr);
114 if(Negative) {
115 Retval = -Retval;
116 }
117 return Retval;
118 }
119
120 static int
121 Digit2Val( int c)
122 {
123 if(__isHexLetter(c)) { /* If c is one of [A-Fa-f]... */
124 c = toupper(c) - 7; // Adjust so 'A' is ('9' + 1)
125 }
126 return c - '0'; // Value returned is between 0 and 35, inclusive.
127 }
128
129 /** The strtol, strtoll, strtoul, and strtoull functions convert the initial
130 portion of the string pointed to by nptr to long int, long long int,
131 unsigned long int, and unsigned long long int representation, respectively.
132 First, they decompose the input string into three parts: an initial,
133 possibly empty, sequence of white-space characters (as specified by the
134 isspace function), a subject sequence resembling an integer represented in
135 some radix determined by the value of base, and a final string of one or
136 more unrecognized characters, including the terminating null character of
137 the input string. Then, they attempt to convert the subject sequence to an
138 integer, and return the result.
139
140 If the value of base is zero, the expected form of the subject sequence is
141 that of an integer constant, optionally preceded
142 by a plus or minus sign, but not including an integer suffix. If the value
143 of base is between 2 and 36 (inclusive), the expected form of the subject
144 sequence is a sequence of letters and digits representing an integer with
145 the radix specified by base, optionally preceded by a plus or minus sign,
146 but not including an integer suffix. The letters from a (or A) through z
147 (or Z) are ascribed the values 10 through 35; only letters and digits whose
148 ascribed values are less than that of base are permitted. If the value of
149 base is 16, the characters 0x or 0X may optionally precede the sequence of
150 letters and digits, following the sign if present.
151
152 The subject sequence is defined as the longest initial subsequence of the
153 input string, starting with the first non-white-space character, that is of
154 the expected form. The subject sequence contains no characters if the input
155 string is empty or consists entirely of white space, or if the first
156 non-white-space character is other than a sign or a permissible letter or digit.
157
158 If the subject sequence has the expected form and the value of base is
159 zero, the sequence of characters starting with the first digit is
160 interpreted as an integer constant. If the subject sequence has the
161 expected form and the value of base is between 2 and 36, it is used as the
162 base for conversion, ascribing to each letter its value as given above. If
163 the subject sequence begins with a minus sign, the value resulting from the
164 conversion is negated (in the return type). A pointer to the final string
165 is stored in the object pointed to by endptr, provided that endptr is
166 not a null pointer.
167
168 In other than the "C" locale, additional locale-specific subject sequence
169 forms may be accepted.
170
171 If the subject sequence is empty or does not have the expected form, no
172 conversion is performed; the value of nptr is stored in the object pointed
173 to by endptr, provided that endptr is not a null pointer.
174
175 @return The strtol, strtoll, strtoul, and strtoull functions return the
176 converted value, if any. If no conversion could be performed, zero
177 is returned. If the correct value is outside the range of
178 representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
179 ULONG_MAX, or ULLONG_MAX is returned (according to the return type
180 and sign of the value, if any), and the value of the macro ERANGE
181 is stored in errno.
182 **/
183 long
184 strtol(const char * __restrict nptr, char ** __restrict endptr, int base)
185 {
186 long Result = 0;
187 long Previous;
188 int temp;
189 BOOLEAN Negative = FALSE;
190
191 if((base < 0) || (base == 1) || (base > 36)) {
192 *endptr = NULL;
193 return 0;
194 }
195 // Skip leading spaces.
196 while(isspace(*nptr)) ++nptr;
197
198 // Process Subject sequence: optional sign followed by digits.
199 if(*nptr == '+') {
200 Negative = FALSE;
201 ++nptr;
202 }
203 else if(*nptr == '-') {
204 Negative = TRUE;
205 ++nptr;
206 }
207 if( (base == 16) && (*nptr == '0') && (toupper(nptr[1]) == 'X')) {
208 nptr += 2;
209 }
210 while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {
211 Previous = Result;
212 Result = (Result * base) + (long int)temp;
213 if( Result <= Previous) { // Detect Overflow
214 if(Negative) {
215 Result = LONG_MIN;
216 }
217 else {
218 Result = LONG_MAX;
219 }
220 Negative = FALSE;
221 errno = ERANGE;
222 break;
223 }
224 ++nptr;
225 }
226 if(Negative) {
227 Result = -Result;
228 }
229
230 // Save pointer to final sequence
231 if( endptr != NULL) {
232 *endptr = (char *)nptr;
233 }
234 return Result;
235 }
236
237 /** The strtoul function converts the initial portion of the string pointed to
238 by nptr to unsigned long int representation.
239
240 See the description for strtol for more information.
241
242 @return The strtoul function returns the converted value, if any. If no
243 conversion could be performed, zero is returned. If the correct
244 value is outside the range of representable values, ULONG_MAX is
245 returned and the value of the macro ERANGE is stored in errno.
246 **/
247 unsigned long
248 strtoul(const char * __restrict nptr, char ** __restrict endptr, int base)
249 {
250 unsigned long Result = 0;
251 unsigned long Previous;
252 int temp;
253
254 if((base < 0) || (base == 1) || (base > 36)) {
255 *endptr = NULL;
256 return 0;
257 }
258 // Skip leading spaces.
259 while(isspace(*nptr)) ++nptr;
260
261 // Process Subject sequence: optional + sign followed by digits.
262 if(*nptr == '+') {
263 ++nptr;
264 }
265 if( (base == 16) && (*nptr == '0') && (toupper(nptr[1]) == 'X')) {
266 nptr += 2;
267 }
268 while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {
269 Previous = Result;
270 Result = (Result * base) + (unsigned long)temp;
271 if( Result < Previous) { // If we overflowed
272 Result = ULONG_MAX;
273 errno = ERANGE;
274 break;
275 }
276 ++nptr;
277 }
278
279 // Save pointer to final sequence
280 if( endptr != NULL) {
281 *endptr = (char *)nptr;
282 }
283 return Result;
284 }
285
286 /** The strtoll function converts the initial portion of the string pointed to
287 by nptr to long long int representation.
288
289 See the description for strtol for more information.
290
291 @return The strtoll function returns the converted value, if any. If no
292 conversion could be performed, zero is returned. If the correct
293 value is outside the range of representable values, LLONG_MIN or
294 LLONG_MAX is returned (according to the sign of the value, if any),
295 and the value of the macro ERANGE is stored in errno.
296 **/
297 long long
298 strtoll(const char * __restrict nptr, char ** __restrict endptr, int base)
299 {
300 long long Result = 0;
301 long long Previous;
302 int temp;
303 BOOLEAN Negative = FALSE;
304
305 if((base < 0) || (base == 1) || (base > 36)) {
306 *endptr = NULL;
307 return 0;
308 }
309 // Skip leading spaces.
310 while(isspace(*nptr)) ++nptr;
311
312 // Process Subject sequence: optional sign followed by digits.
313 if(*nptr == '+') {
314 Negative = FALSE;
315 ++nptr;
316 }
317 else if(*nptr == '-') {
318 Negative = TRUE;
319 ++nptr;
320 }
321 if( (base == 16) && (*nptr == '0') && (toupper(nptr[1]) == 'X')) {
322 nptr += 2;
323 }
324 while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {
325 Previous = Result;
326 Result = (Result * base) + (long long int)temp;
327 if( Result <= Previous) { // Detect Overflow
328 if(Negative) {
329 Result = LLONG_MIN;
330 }
331 else {
332 Result = LLONG_MAX;
333 }
334 Negative = FALSE;
335 errno = ERANGE;
336 break;
337 }
338 ++nptr;
339 }
340 if(Negative) {
341 Result = -Result;
342 }
343
344 // Save pointer to final sequence
345 if( endptr != NULL) {
346 *endptr = (char *)nptr;
347 }
348 return Result;
349 }
350
351 /** The strtoull function converts the initial portion of the string pointed to
352 by nptr to unsigned long long int representation.
353
354 See the description for strtol for more information.
355
356 @return The strtoull function returns the converted value, if any. If no
357 conversion could be performed, zero is returned. If the correct
358 value is outside the range of representable values, ULLONG_MAX is
359 returned and the value of the macro ERANGE is stored in errno.
360 **/
361 unsigned long long
362 strtoull(const char * __restrict nptr, char ** __restrict endptr, int base)
363 {
364 unsigned long long Result = 0;
365 unsigned long long Previous;
366 int temp;
367
368 if((base < 0) || (base == 1) || (base > 36)) {
369 *endptr = NULL;
370 return 0;
371 }
372 // Skip leading spaces.
373 while(isspace(*nptr)) ++nptr;
374
375 // Process Subject sequence: optional + sign followed by digits.
376 if(*nptr == '+') {
377 ++nptr;
378 }
379 if( (base == 16) && (*nptr == '0') && (toupper(nptr[1]) == 'X')) {
380 nptr += 2;
381 }
382 while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {
383 Previous = Result;
384 Result = (Result * base) + (unsigned long long)temp;
385 if( Result < Previous) { // If we overflowed
386 Result = ULLONG_MAX;
387 errno = ERANGE;
388 break;
389 }
390 ++nptr;
391 }
392
393 // Save pointer to final sequence
394 if( endptr != NULL) {
395 *endptr = (char *)nptr;
396 }
397 return Result;
398 }