1 /* Copyright (c) 2002,2004,2005 Joerg Wunsch
2    Copyright (c) 2008  Dmitry Xmelkov
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 are met:
7 
8    * Redistributions of source code must retain the above copyright
9      notice, this list of conditions and the following disclaimer.
10 
11    * Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the
14      distribution.
15 
16    * Neither the name of the copyright holders nor the names of
17      contributors may be used to endorse or promote products derived
18      from this software without specific prior written permission.
19 
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #include "stdio_private.h"
34 #include <inttypes.h>
35 
36 #if defined(_HAVE_BUILTIN_MUL_OVERFLOW) && defined(_HAVE_BUILTIN_ADD_OVERFLOW) && !defined(strtoi_signed)
37 #define USE_OVERFLOW
38 #endif
39 
40 strtoi_type
strtoi(const char * __restrict nptr,char ** __restrict endptr,int ibase)41 strtoi(const char *__restrict nptr, char **__restrict endptr, int ibase)
42 {
43     unsigned int base = ibase;
44 
45     /* Check for invalid base value */
46     if (base > 36 || base == 1) {
47         errno = EINVAL;
48         if (endptr)
49             *endptr = (char *) nptr;
50         return 0;
51     }
52 
53 #define FLAG_NEG        0x1     /* Negative. Must be 1 for ucutoff below */
54 #define FLAG_OFLOW      0x2     /* Value overflow */
55 
56     const unsigned char *s = (const unsigned char *) nptr;
57     strtoi_type val = 0;
58     unsigned char flags = 0;
59     unsigned int i;
60 
61     /* Skip leading spaces */
62     do {
63         i = *s++;
64     } while (isspace(i));
65 
66     /* Parse a leading sign */
67     switch (i) {
68     case '-':
69         flags = FLAG_NEG;
70 	__PICOLIBC_FALLTHROUGH;
71     case '+':
72         i = *s++;
73     }
74 
75     /* Leading '0' digit -- check for base indication */
76     if (i == '0') {
77         if (TOLOWER(*s) == 'x' && ((base | 16) == 16)) {
78             base = 16;
79             /* Parsed the '0' */
80             nptr = (const char *) s;
81             i = s[1];
82             s += 2;
83 	} else if (base == 0) {
84             base = 8;
85         }
86     } else if (base == 0) {
87         base = 10;
88     }
89 
90 #ifndef USE_OVERFLOW
91     /* Compute values used to detect overflow. */
92 #ifdef strtoi_signed
93     /* works because strtoi_min = (strtoi_type) ((strtoi_utype) strtoi_max + 1) */
94     strtoi_utype ucutoff = (strtoi_utype) strtoi_max + flags;
95     strtoi_type cutoff = ucutoff / base;
96     unsigned int cutlim = ucutoff % base;
97 #else
98     strtoi_type cutoff = strtoi_max / base;
99     unsigned int cutlim = strtoi_max % base;
100 #endif
101 #endif
102 
103     for(;;) {
104         i = digit_to_val(i);
105         /* detect invalid char */
106         if (i >= base)
107             break;
108 
109         /* Add the new digit, checking for overflow */
110 #ifdef USE_OVERFLOW
111         /*
112          * This isn't used for signed values as it's tricky and
113          * generates larger code. Yes, it avoids doing the divmod
114          * above, but we'll assume an app doing math with signed
115          * values will probably end up doing a divide somewhere
116          */
117         if (__builtin_mul_overflow(val, (strtoi_type) base, &val) ||
118             __builtin_add_overflow(val, (strtoi_type) i, &val))
119         {
120             flags |= FLAG_OFLOW;
121         }
122 #else
123         if (val > cutoff || (val == cutoff && i > cutlim))
124             flags |= FLAG_OFLOW;
125         val = val * (strtoi_type) base + (strtoi_type) i;
126 #endif
127         /* Parsed another digit */
128         nptr = (const char *) s;
129         i = *s++;
130     }
131 
132     /* Mark the end of the parsed region */
133     if (endptr != NULL)
134         *endptr = (char *) nptr;
135 
136     if (flags & FLAG_NEG)
137         val = -val;
138 
139     if (flags & FLAG_OFLOW) {
140 #ifdef strtoi_signed
141         val = (strtoi_type) ucutoff;
142 #else
143         val = strtoi_max;
144 #endif
145         errno = ERANGE;
146     }
147 
148     return val;
149 }
150