strtol_l.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* Convert string representing a number to integer value, using given locale.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #ifdef _LIBC
  20. # define USE_NUMBER_GROUPING
  21. # define HAVE_LIMITS_H
  22. #endif
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #ifndef __set_errno
  26. # define __set_errno(Val) errno = (Val)
  27. #endif
  28. #ifdef HAVE_LIMITS_H
  29. # include <limits.h>
  30. #endif
  31. #include <stddef.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <locale.h>
  35. #include <stdint.h>
  36. #include <bits/wordsize.h>
  37. #ifdef USE_NUMBER_GROUPING
  38. # include "../locale/localeinfo.h"
  39. #endif
  40. /* Nonzero if we are defining `strtoul' or `strtoull', operating on
  41. unsigned integers. */
  42. #ifndef UNSIGNED
  43. # define UNSIGNED 0
  44. # define INT LONG int
  45. #else
  46. # define INT unsigned LONG int
  47. #endif
  48. /* Determine the name. */
  49. #if UNSIGNED
  50. # ifdef USE_WIDE_CHAR
  51. # ifdef QUAD
  52. # define strtol_l wcstoull_l
  53. # else
  54. # define strtol_l wcstoul_l
  55. # endif
  56. # else
  57. # ifdef QUAD
  58. # define strtol_l strtoull_l
  59. # else
  60. # define strtol_l strtoul_l
  61. # endif
  62. # endif
  63. #else
  64. # ifdef USE_WIDE_CHAR
  65. # ifdef QUAD
  66. # define strtol_l wcstoll_l
  67. # else
  68. # define strtol_l wcstol_l
  69. # endif
  70. # else
  71. # ifdef QUAD
  72. # define strtol_l strtoll_l
  73. # else
  74. # define strtol_l strtol_l
  75. # endif
  76. # endif
  77. #endif
  78. #define __strtol_l __strtol_l2(strtol_l)
  79. #define __strtol_l2(name) __strtol_l3(name)
  80. #define __strtol_l3(name) __##name
  81. /* If QUAD is defined, we are defining `strtoll' or `strtoull',
  82. operating on `long long int's. */
  83. #ifdef QUAD
  84. # define LONG long long
  85. # define STRTOL_LONG_MIN LONG_LONG_MIN
  86. # define STRTOL_LONG_MAX LONG_LONG_MAX
  87. # define STRTOL_ULONG_MAX ULONG_LONG_MAX
  88. #else
  89. # define LONG long
  90. # ifndef ULONG_MAX
  91. # define ULONG_MAX ((unsigned long int) ~(unsigned long int) 0)
  92. # endif
  93. # ifndef LONG_MAX
  94. # define LONG_MAX ((long int) (ULONG_MAX >> 1))
  95. # endif
  96. # define STRTOL_LONG_MIN LONG_MIN
  97. # define STRTOL_LONG_MAX LONG_MAX
  98. # define STRTOL_ULONG_MAX ULONG_MAX
  99. #endif
  100. /* We use this code for the extended locale handling where the
  101. function gets as an additional argument the locale which has to be
  102. used. To access the values we have to redefine the _NL_CURRENT and
  103. _NL_CURRENT_WORD macros. */
  104. #undef _NL_CURRENT
  105. #define _NL_CURRENT(category, item) \
  106. (current->values[_NL_ITEM_INDEX (item)].string)
  107. #undef _NL_CURRENT_WORD
  108. #define _NL_CURRENT_WORD(category, item) \
  109. ((uint32_t) current->values[_NL_ITEM_INDEX (item)].word)
  110. #if defined _LIBC || defined HAVE_WCHAR_H
  111. # include <wchar.h>
  112. #endif
  113. #ifdef USE_WIDE_CHAR
  114. # include <wctype.h>
  115. # define L_(Ch) L##Ch
  116. # define UCHAR_TYPE wint_t
  117. # define STRING_TYPE wchar_t
  118. # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
  119. # define ISALPHA(Ch) __iswalpha_l ((Ch), _nl_C_locobj_ptr)
  120. # define TOUPPER(Ch) __towupper_l ((Ch), _nl_C_locobj_ptr)
  121. #else
  122. # if defined _LIBC \
  123. || defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
  124. # define IN_CTYPE_DOMAIN(c) 1
  125. # else
  126. # define IN_CTYPE_DOMAIN(c) isascii(c)
  127. # endif
  128. # define L_(Ch) Ch
  129. # define UCHAR_TYPE unsigned char
  130. # define STRING_TYPE char
  131. # define ISSPACE(Ch) __isspace_l ((Ch), loc)
  132. # define ISALPHA(Ch) __isalpha_l ((Ch), _nl_C_locobj_ptr)
  133. # define TOUPPER(Ch) __toupper_l ((Ch), _nl_C_locobj_ptr)
  134. #endif
  135. #define INTERNAL(X) INTERNAL1(X)
  136. #define INTERNAL1(X) __##X##_internal
  137. #define WEAKNAME(X) WEAKNAME1(X)
  138. #ifdef USE_NUMBER_GROUPING
  139. /* This file defines a function to check for correct grouping. */
  140. # include "grouping.h"
  141. #endif
  142. /* Define tables of maximum values and remainders in order to detect
  143. overflow. Do this at compile-time in order to avoid the runtime
  144. overhead of the division. */
  145. extern const unsigned long __strtol_ul_max_tab[] attribute_hidden;
  146. extern const unsigned char __strtol_ul_rem_tab[] attribute_hidden;
  147. #if defined(QUAD) && __WORDSIZE == 32
  148. extern const unsigned long long __strtol_ull_max_tab[] attribute_hidden;
  149. extern const unsigned char __strtol_ull_rem_tab[] attribute_hidden;
  150. #endif
  151. #define DEF(TYPE, NAME) \
  152. const TYPE NAME[] attribute_hidden = \
  153. { \
  154. F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9), F(10), \
  155. F(11), F(12), F(13), F(14), F(15), F(16), F(17), F(18), F(19), F(20), \
  156. F(21), F(22), F(23), F(24), F(25), F(26), F(27), F(28), F(29), F(30), \
  157. F(31), F(32), F(33), F(34), F(35), F(36) \
  158. }
  159. #if !UNSIGNED && !defined (USE_WIDE_CHAR) && !defined (QUAD)
  160. # define F(X) ULONG_MAX / X
  161. DEF (unsigned long, __strtol_ul_max_tab);
  162. # undef F
  163. # define F(X) ULONG_MAX % X
  164. DEF (unsigned char, __strtol_ul_rem_tab);
  165. # undef F
  166. #endif
  167. #if !UNSIGNED && !defined (USE_WIDE_CHAR) && defined (QUAD) \
  168. && __WORDSIZE == 32
  169. # define F(X) ULONG_LONG_MAX / X
  170. DEF (unsigned long long, __strtol_ull_max_tab);
  171. # undef F
  172. # define F(X) ULONG_LONG_MAX % X
  173. DEF (unsigned char, __strtol_ull_rem_tab);
  174. # undef F
  175. #endif
  176. #undef DEF
  177. /* Define some more readable aliases for these arrays which correspond
  178. to how they'll be used in the function below. */
  179. #define jmax_tab __strtol_ul_max_tab
  180. #if defined(QUAD) && __WORDSIZE == 32
  181. # define cutoff_tab __strtol_ull_max_tab
  182. # define cutlim_tab __strtol_ull_rem_tab
  183. #else
  184. # define cutoff_tab __strtol_ul_max_tab
  185. # define cutlim_tab __strtol_ul_rem_tab
  186. #endif
  187. /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
  188. If BASE is 0 the base is determined by the presence of a leading
  189. zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
  190. If BASE is < 2 or > 36, it is reset to 10.
  191. If ENDPTR is not NULL, a pointer to the character after the last
  192. one converted is stored in *ENDPTR. */
  193. INT
  194. INTERNAL (__strtol_l) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  195. int base, int group, locale_t loc)
  196. {
  197. int negative;
  198. unsigned LONG int cutoff;
  199. unsigned int cutlim;
  200. unsigned LONG int i;
  201. const STRING_TYPE *s;
  202. UCHAR_TYPE c;
  203. const STRING_TYPE *save, *end;
  204. int overflow;
  205. #ifndef USE_WIDE_CHAR
  206. size_t cnt;
  207. #endif
  208. #ifdef USE_NUMBER_GROUPING
  209. struct __locale_data *current = loc->__locales[LC_NUMERIC];
  210. /* The thousands character of the current locale. */
  211. # ifdef USE_WIDE_CHAR
  212. wchar_t thousands = L'\0';
  213. # else
  214. const char *thousands = NULL;
  215. size_t thousands_len = 0;
  216. # endif
  217. /* The numeric grouping specification of the current locale,
  218. in the format described in <locale.h>. */
  219. const char *grouping;
  220. if (__glibc_unlikely (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. # ifdef USE_WIDE_CHAR
  229. # ifdef _LIBC
  230. thousands = _NL_CURRENT_WORD (LC_NUMERIC,
  231. _NL_NUMERIC_THOUSANDS_SEP_WC);
  232. # endif
  233. if (thousands == L'\0')
  234. grouping = NULL;
  235. # else
  236. # ifdef _LIBC
  237. thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
  238. # endif
  239. if (*thousands == '\0')
  240. {
  241. thousands = NULL;
  242. grouping = NULL;
  243. }
  244. # endif
  245. }
  246. }
  247. else
  248. grouping = NULL;
  249. #endif
  250. if (base < 0 || base == 1 || base > 36)
  251. {
  252. __set_errno (EINVAL);
  253. return 0;
  254. }
  255. save = s = nptr;
  256. /* Skip white space. */
  257. while (ISSPACE (*s))
  258. ++s;
  259. if (__glibc_unlikely (*s == L_('\0')))
  260. goto noconv;
  261. /* Check for a sign. */
  262. negative = 0;
  263. if (*s == L_('-'))
  264. {
  265. negative = 1;
  266. ++s;
  267. }
  268. else if (*s == L_('+'))
  269. ++s;
  270. /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
  271. if (*s == L_('0'))
  272. {
  273. if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
  274. {
  275. s += 2;
  276. base = 16;
  277. }
  278. else if (base == 0)
  279. base = 8;
  280. }
  281. else if (base == 0)
  282. base = 10;
  283. /* Save the pointer so we can check later if anything happened. */
  284. save = s;
  285. #ifdef USE_NUMBER_GROUPING
  286. if (base != 10)
  287. grouping = NULL;
  288. if (__glibc_unlikely (grouping != NULL))
  289. {
  290. # ifndef USE_WIDE_CHAR
  291. thousands_len = strlen (thousands);
  292. # endif
  293. /* Find the end of the digit string and check its grouping. */
  294. end = s;
  295. if (
  296. # ifdef USE_WIDE_CHAR
  297. *s != thousands
  298. # else
  299. ({ for (cnt = 0; cnt < thousands_len; ++cnt)
  300. if (thousands[cnt] != end[cnt])
  301. break;
  302. cnt < thousands_len; })
  303. # endif
  304. )
  305. {
  306. for (c = *end; c != L_('\0'); c = *++end)
  307. if (((STRING_TYPE) c < L_('0') || (STRING_TYPE) c > L_('9'))
  308. # ifdef USE_WIDE_CHAR
  309. && (wchar_t) c != thousands
  310. # else
  311. && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
  312. if (thousands[cnt] != end[cnt])
  313. break;
  314. cnt < thousands_len; })
  315. # endif
  316. && (!ISALPHA (c)
  317. || (int) (TOUPPER (c) - L_('A') + 10) >= base))
  318. break;
  319. # ifdef USE_WIDE_CHAR
  320. end = __correctly_grouped_prefixwc (s, end, thousands, grouping);
  321. # else
  322. end = __correctly_grouped_prefixmb (s, end, thousands, grouping);
  323. # endif
  324. }
  325. }
  326. else
  327. #endif
  328. end = NULL;
  329. /* Avoid runtime division; lookup cutoff and limit. */
  330. cutoff = cutoff_tab[base - 2];
  331. cutlim = cutlim_tab[base - 2];
  332. overflow = 0;
  333. i = 0;
  334. c = *s;
  335. if (sizeof (long int) != sizeof (LONG int))
  336. {
  337. unsigned long int j = 0;
  338. unsigned long int jmax = jmax_tab[base - 2];
  339. for (;c != L_('\0'); c = *++s)
  340. {
  341. if (s == end)
  342. break;
  343. if (c >= L_('0') && c <= L_('9'))
  344. c -= L_('0');
  345. #ifdef USE_NUMBER_GROUPING
  346. # ifdef USE_WIDE_CHAR
  347. else if (grouping && (wchar_t) c == thousands)
  348. continue;
  349. # else
  350. else if (thousands_len)
  351. {
  352. for (cnt = 0; cnt < thousands_len; ++cnt)
  353. if (thousands[cnt] != s[cnt])
  354. break;
  355. if (cnt == thousands_len)
  356. {
  357. s += thousands_len - 1;
  358. continue;
  359. }
  360. if (ISALPHA (c))
  361. c = TOUPPER (c) - L_('A') + 10;
  362. else
  363. break;
  364. }
  365. # endif
  366. #endif
  367. else if (ISALPHA (c))
  368. c = TOUPPER (c) - L_('A') + 10;
  369. else
  370. break;
  371. if ((int) c >= base)
  372. break;
  373. /* Note that we never can have an overflow. */
  374. else if (j >= jmax)
  375. {
  376. /* We have an overflow. Now use the long representation. */
  377. i = (unsigned LONG int) j;
  378. goto use_long;
  379. }
  380. else
  381. j = j * (unsigned long int) base + c;
  382. }
  383. i = (unsigned LONG int) j;
  384. }
  385. else
  386. for (;c != L_('\0'); c = *++s)
  387. {
  388. if (s == end)
  389. break;
  390. if (c >= L_('0') && c <= L_('9'))
  391. c -= L_('0');
  392. #ifdef USE_NUMBER_GROUPING
  393. # ifdef USE_WIDE_CHAR
  394. else if (grouping && (wchar_t) c == thousands)
  395. continue;
  396. # else
  397. else if (thousands_len)
  398. {
  399. for (cnt = 0; cnt < thousands_len; ++cnt)
  400. if (thousands[cnt] != s[cnt])
  401. break;
  402. if (cnt == thousands_len)
  403. {
  404. s += thousands_len - 1;
  405. continue;
  406. }
  407. if (ISALPHA (c))
  408. c = TOUPPER (c) - L_('A') + 10;
  409. else
  410. break;
  411. }
  412. # endif
  413. #endif
  414. else if (ISALPHA (c))
  415. c = TOUPPER (c) - L_('A') + 10;
  416. else
  417. break;
  418. if ((int) c >= base)
  419. break;
  420. /* Check for overflow. */
  421. if (i > cutoff || (i == cutoff && c > cutlim))
  422. overflow = 1;
  423. else
  424. {
  425. use_long:
  426. i *= (unsigned LONG int) base;
  427. i += c;
  428. }
  429. }
  430. /* Check if anything actually happened. */
  431. if (s == save)
  432. goto noconv;
  433. /* Store in ENDPTR the address of one character
  434. past the last character we converted. */
  435. if (endptr != NULL)
  436. *endptr = (STRING_TYPE *) s;
  437. #if !UNSIGNED
  438. /* Check for a value that is within the range of
  439. `unsigned LONG int', but outside the range of `LONG int'. */
  440. if (overflow == 0
  441. && i > (negative
  442. ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
  443. : (unsigned LONG int) STRTOL_LONG_MAX))
  444. overflow = 1;
  445. #endif
  446. if (__glibc_unlikely (overflow))
  447. {
  448. __set_errno (ERANGE);
  449. #if UNSIGNED
  450. return STRTOL_ULONG_MAX;
  451. #else
  452. return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
  453. #endif
  454. }
  455. /* Return the result of the appropriate sign. */
  456. return negative ? -i : i;
  457. noconv:
  458. /* We must handle a special case here: the base is 0 or 16 and the
  459. first two characters are '0' and 'x', but the rest are no
  460. hexadecimal digits. This is no error case. We return 0 and
  461. ENDPTR points to the `x`. */
  462. if (endptr != NULL)
  463. {
  464. if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
  465. && save[-2] == L_('0'))
  466. *endptr = (STRING_TYPE *) &save[-1];
  467. else
  468. /* There was no number to convert. */
  469. *endptr = (STRING_TYPE *) nptr;
  470. }
  471. return 0L;
  472. }
  473. #if defined _LIBC && !defined USE_WIDE_CHAR
  474. libc_hidden_def (INTERNAL (__strtol_l))
  475. #endif
  476. /* External user entry point. */
  477. #if _LIBC - 0 == 0
  478. /* Prototype. */
  479. extern INT __strtol_l (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  480. int base);
  481. #endif
  482. INT
  483. #ifdef weak_function
  484. weak_function
  485. #endif
  486. __strtol_l (const STRING_TYPE *nptr, STRING_TYPE **endptr,
  487. int base, locale_t loc)
  488. {
  489. return INTERNAL (__strtol_l) (nptr, endptr, base, 0, loc);
  490. }
  491. libc_hidden_def (__strtol_l)
  492. weak_alias (__strtol_l, strtol_l)