1 /* Convert string representation of a number into an integer value.
2 
3    Copyright (C) 1991-1992, 1994-1999, 2003, 2005-2007, 2009-2020 Free Software
4    Foundation, Inc.
5 
6    NOTE: The canonical source of this file is maintained with the GNU C
7    Library.  Bugs can be reported to bug-glibc@gnu.org.
8 
9    This program is free software: you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by the
11    Free Software Foundation; either version 3 of the License, or any
12    later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
21 
22 #ifdef _LIBC
23 # define USE_NUMBER_GROUPING
24 #else
25 # include <config.h>
26 #endif
27 
28 #include <ctype.h>
29 #include <errno.h>
30 #ifndef __set_errno
31 # define __set_errno(Val) errno = (Val)
32 #endif
33 
34 #include <limits.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #ifdef USE_NUMBER_GROUPING
40 # include "../locale/localeinfo.h"
41 #endif
42 
43 /* Nonzero if we are defining 'strtoul' or 'strtoull', operating on
44    unsigned integers.  */
45 #ifndef UNSIGNED
46 # define UNSIGNED 0
47 # define INT LONG int
48 #else
49 # define INT unsigned LONG int
50 #endif
51 
52 /* Determine the name.  */
53 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
54 # if UNSIGNED
55 #  ifdef USE_WIDE_CHAR
56 #   ifdef QUAD
57 #    define strtol __wcstoull_l
58 #   else
59 #    define strtol __wcstoul_l
60 #   endif
61 #  else
62 #   ifdef QUAD
63 #    define strtol __strtoull_l
64 #   else
65 #    define strtol __strtoul_l
66 #   endif
67 #  endif
68 # else
69 #  ifdef USE_WIDE_CHAR
70 #   ifdef QUAD
71 #    define strtol __wcstoll_l
72 #   else
73 #    define strtol __wcstol_l
74 #   endif
75 #  else
76 #   ifdef QUAD
77 #    define strtol __strtoll_l
78 #   else
79 #    define strtol __strtol_l
80 #   endif
81 #  endif
82 # endif
83 #else
84 # if UNSIGNED
85 #  ifdef USE_WIDE_CHAR
86 #   ifdef QUAD
87 #    define strtol wcstoull
88 #   else
89 #    define strtol wcstoul
90 #   endif
91 #  else
92 #   ifdef QUAD
93 #    define strtol strtoull
94 #   else
95 #    define strtol strtoul
96 #   endif
97 #  endif
98 # else
99 #  ifdef USE_WIDE_CHAR
100 #   ifdef QUAD
101 #    define strtol wcstoll
102 #   else
103 #    define strtol wcstol
104 #   endif
105 #  else
106 #   ifdef QUAD
107 #    define strtol strtoll
108 #   endif
109 #  endif
110 # endif
111 #endif
112 
113 /* If QUAD is defined, we are defining 'strtoll' or 'strtoull',
114    operating on 'long long int's.  */
115 #ifdef QUAD
116 # define LONG long long
117 # define STRTOL_LONG_MIN LLONG_MIN
118 # define STRTOL_LONG_MAX LLONG_MAX
119 # define STRTOL_ULONG_MAX ULLONG_MAX
120 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
121    /* Work around gcc bug with using this constant.  */
122    static const unsigned long long int maxquad = ULLONG_MAX;
123 #  undef STRTOL_ULONG_MAX
124 #  define STRTOL_ULONG_MAX maxquad
125 # endif
126 #else
127 # define LONG long
128 # define STRTOL_LONG_MIN LONG_MIN
129 # define STRTOL_LONG_MAX LONG_MAX
130 # define STRTOL_ULONG_MAX ULONG_MAX
131 #endif
132 
133 
134 /* We use this code also for the extended locale handling where the
135    function gets as an additional argument the locale which has to be
136    used.  To access the values we have to redefine the _NL_CURRENT
137    macro.  */
138 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
139 # undef _NL_CURRENT
140 # define _NL_CURRENT(category, item) \
141   (current->values[_NL_ITEM_INDEX (item)].string)
142 # define LOCALE_PARAM , loc
143 # define LOCALE_PARAM_PROTO , __locale_t loc
144 #else
145 # define LOCALE_PARAM
146 # define LOCALE_PARAM_PROTO
147 #endif
148 
149 #ifdef USE_WIDE_CHAR
150 # include <wchar.h>
151 # include <wctype.h>
152 # define L_(Ch) L##Ch
153 # define UCHAR_TYPE wint_t
154 # define STRING_TYPE wchar_t
155 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
156 #  define ISSPACE(Ch) __iswspace_l ((Ch), loc)
157 #  define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
158 #  define TOUPPER(Ch) __towupper_l ((Ch), loc)
159 # else
160 #  define ISSPACE(Ch) iswspace (Ch)
161 #  define ISALPHA(Ch) iswalpha (Ch)
162 #  define TOUPPER(Ch) towupper (Ch)
163 # endif
164 #else
165 # define L_(Ch) Ch
166 # define UCHAR_TYPE unsigned char
167 # define STRING_TYPE char
168 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
169 #  define ISSPACE(Ch) __isspace_l ((Ch), loc)
170 #  define ISALPHA(Ch) __isalpha_l ((Ch), loc)
171 #  define TOUPPER(Ch) __toupper_l ((Ch), loc)
172 # else
173 #  define ISSPACE(Ch) isspace (Ch)
174 #  define ISALPHA(Ch) isalpha (Ch)
175 #  define TOUPPER(Ch) toupper (Ch)
176 # endif
177 #endif
178 
179 #define INTERNAL(X) INTERNAL1(X)
180 #define INTERNAL1(X) __##X##_internal
181 #define WEAKNAME(X) WEAKNAME1(X)
182 
183 #ifdef USE_NUMBER_GROUPING
184 /* This file defines a function to check for correct grouping.  */
185 # include "grouping.h"
186 #endif
187 
188 
189 
190 /* Convert NPTR to an 'unsigned long int' or 'long int' in base BASE.
191    If BASE is 0 the base is determined by the presence of a leading
192    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
193    If BASE is < 2 or > 36, it is reset to 10.
194    If ENDPTR is not NULL, a pointer to the character after the last
195    one converted is stored in *ENDPTR.  */
196 
197 INT
INTERNAL(strtol)198 INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
199                    int base, int group LOCALE_PARAM_PROTO)
200 {
201   int negative;
202   register unsigned LONG int cutoff;
203   register unsigned int cutlim;
204   register unsigned LONG int i;
205   register const STRING_TYPE *s;
206   register UCHAR_TYPE c;
207   const STRING_TYPE *save, *end;
208   int overflow;
209 
210 #ifdef USE_NUMBER_GROUPING
211 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
212   struct locale_data *current = loc->__locales[LC_NUMERIC];
213 # endif
214   /* The thousands character of the current locale.  */
215   wchar_t thousands = L'\0';
216   /* The numeric grouping specification of the current locale,
217      in the format described in <locale.h>.  */
218   const char *grouping;
219 
220   if (group)
221     {
222       grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
223       if (*grouping <= 0 || *grouping == CHAR_MAX)
224         grouping = NULL;
225       else
226         {
227           /* Figure out the thousands separator character.  */
228 # if defined _LIBC || defined _HAVE_BTOWC
229           thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
230           if (thousands == WEOF)
231             thousands = L'\0';
232 # endif
233           if (thousands == L'\0')
234             grouping = NULL;
235         }
236     }
237   else
238     grouping = NULL;
239 #endif
240 
241   if (base < 0 || base == 1 || base > 36)
242     {
243       __set_errno (EINVAL);
244       return 0;
245     }
246 
247   save = s = nptr;
248 
249   /* Skip white space.  */
250   while (ISSPACE (*s))
251     ++s;
252   if (*s == L_('\0'))
253     goto noconv;
254 
255   /* Check for a sign.  */
256   if (*s == L_('-'))
257     {
258       negative = 1;
259       ++s;
260     }
261   else if (*s == L_('+'))
262     {
263       negative = 0;
264       ++s;
265     }
266   else
267     negative = 0;
268 
269   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
270   if (*s == L_('0'))
271     {
272       if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
273         {
274           s += 2;
275           base = 16;
276         }
277       else if (base == 0)
278         base = 8;
279     }
280   else if (base == 0)
281     base = 10;
282 
283   /* Save the pointer so we can check later if anything happened.  */
284   save = s;
285 
286 #ifdef USE_NUMBER_GROUPING
287   if (group)
288     {
289       /* Find the end of the digit string and check its grouping.  */
290       end = s;
291       for (c = *end; c != L_('\0'); c = *++end)
292         if ((wchar_t) c != thousands
293             && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
294             && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
295           break;
296       if (*s == thousands)
297         end = s;
298       else
299         end = correctly_grouped_prefix (s, end, thousands, grouping);
300     }
301   else
302 #endif
303     end = NULL;
304 
305   cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
306   cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
307 
308   overflow = 0;
309   i = 0;
310   for (c = *s; c != L_('\0'); c = *++s)
311     {
312       if (s == end)
313         break;
314       if (c >= L_('0') && c <= L_('9'))
315         c -= L_('0');
316       else if (ISALPHA (c))
317         c = TOUPPER (c) - L_('A') + 10;
318       else
319         break;
320       if ((int) c >= base)
321         break;
322       /* Check for overflow.  */
323       if (i > cutoff || (i == cutoff && c > cutlim))
324         overflow = 1;
325       else
326         {
327           i *= (unsigned LONG int) base;
328           i += c;
329         }
330     }
331 
332   /* Check if anything actually happened.  */
333   if (s == save)
334     goto noconv;
335 
336   /* Store in ENDPTR the address of one character
337      past the last character we converted.  */
338   if (endptr != NULL)
339     *endptr = (STRING_TYPE *) s;
340 
341 #if !UNSIGNED
342   /* Check for a value that is within the range of
343      'unsigned LONG int', but outside the range of 'LONG int'.  */
344   if (overflow == 0
345       && i > (negative
346               ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
347               : (unsigned LONG int) STRTOL_LONG_MAX))
348     overflow = 1;
349 #endif
350 
351   if (overflow)
352     {
353       __set_errno (ERANGE);
354 #if UNSIGNED
355       return STRTOL_ULONG_MAX;
356 #else
357       return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
358 #endif
359     }
360 
361   /* Return the result of the appropriate sign.  */
362   return negative ? -i : i;
363 
364 noconv:
365   /* We must handle a special case here: the base is 0 or 16 and the
366      first two characters are '0' and 'x', but the rest are no
367      hexadecimal digits.  This is no error case.  We return 0 and
368      ENDPTR points to the 'x'.  */
369   if (endptr != NULL)
370     {
371       if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
372           && save[-2] == L_('0'))
373         *endptr = (STRING_TYPE *) &save[-1];
374       else
375         /*  There was no number to convert.  */
376         *endptr = (STRING_TYPE *) nptr;
377     }
378 
379   return 0L;
380 }
381 
382 /* External user entry point.  */
383 
384 
385 INT
386 #ifdef weak_function
387 weak_function
388 #endif
strtol(const STRING_TYPE * nptr,STRING_TYPE ** endptr,int base LOCALE_PARAM_PROTO)389 strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
390         int base LOCALE_PARAM_PROTO)
391 {
392   return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);
393 }
394