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 
35 #if defined(_HAVE_BUILTIN_MUL_OVERFLOW) && defined(_HAVE_BUILTIN_ADD_OVERFLOW) && !defined(strtoi_signed)
36 #define USE_OVERFLOW
37 #endif
38 
39 strtoi_type
strtoi(const char * __restrict nptr,char ** __restrict endptr,int ibase)40 strtoi(const char *__restrict nptr, char **__restrict endptr, int ibase)
41 {
42     unsigned int base = ibase;
43 
44     /* Check for invalid base value */
45     if (base > 36 || base == 1) {
46         errno = EINVAL;
47         if (endptr)
48             *endptr = (char *) nptr;
49         return 0;
50     }
51 
52 #define FLAG_NEG        0x1     /* Negative. Must be 1 for ucutoff below */
53 #define FLAG_OFLOW      0x2     /* Value overflow */
54 
55     const unsigned char *s = (const unsigned char *) nptr;
56     strtoi_type val = 0;
57     unsigned char flags = 0;
58     unsigned int i;
59 
60     /* Skip leading spaces */
61     do {
62         i = *s++;
63     } while (isspace(i));
64 
65     /* Parse a leading sign */
66     switch (i) {
67     case '-':
68         flags = FLAG_NEG;
69 	__PICOLIBC_FALLTHROUGH;
70     case '+':
71         i = *s++;
72     }
73 
74     /* Leading '0' digit -- check for base indication */
75     if (i == '0') {
76         if (TOLOWER(*s) == 'x' && ((base | 16) == 16)) {
77             base = 16;
78             /* Parsed the '0' */
79             nptr = (const char *) s;
80             i = s[1];
81             s += 2;
82 	} else if (base == 0) {
83             base = 8;
84         }
85     } else if (base == 0) {
86         base = 10;
87     }
88 
89 #ifndef USE_OVERFLOW
90     /* Compute values used to detect overflow. */
91 #ifdef strtoi_signed
92     /* works because strtoi_min = (strtoi_type) ((strtoi_utype) strtoi_max + 1) */
93     strtoi_utype ucutoff = (strtoi_utype) strtoi_max + flags;
94     strtoi_type cutoff = ucutoff / base;
95     unsigned int cutlim = ucutoff % base;
96 #else
97     strtoi_type cutoff = strtoi_max / base;
98     unsigned int cutlim = strtoi_max % base;
99 #endif
100 #endif
101 
102     for(;;) {
103         i = digit_to_val(i);
104         /* detect invalid char */
105         if (i >= base)
106             break;
107 
108         /* Add the new digit, checking for overflow */
109 #ifdef USE_OVERFLOW
110         /*
111          * This isn't used for signed values as it's tricky and
112          * generates larger code. Yes, it avoids doing the divmod
113          * above, but we'll assume an app doing math with signed
114          * values will probably end up doing a divide somewhere
115          */
116         if (__builtin_mul_overflow(val, (strtoi_type) base, &val) ||
117             __builtin_add_overflow(val, (strtoi_type) i, &val))
118         {
119             flags |= FLAG_OFLOW;
120         }
121 #else
122         if (val > cutoff || (val == cutoff && i > cutlim))
123             flags |= FLAG_OFLOW;
124         val = val * (strtoi_type) base + (strtoi_type) i;
125 #endif
126         /* Parsed another digit */
127         nptr = (const char *) s;
128         i = *s++;
129     }
130 
131     /* Mark the end of the parsed region */
132     if (endptr != NULL)
133         *endptr = (char *) nptr;
134 
135     if (flags & FLAG_NEG)
136         val = -val;
137 
138     if (flags & FLAG_OFLOW) {
139 #ifdef strtoi_signed
140         val = (strtoi_type) ucutoff;
141 #else
142         val = strtoi_max;
143 #endif
144         errno = ERANGE;
145     }
146 
147     return val;
148 }
149