strtoofft.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include <errno.h>
  25. #include "curl_setup.h"
  26. #include "strtoofft.h"
  27. /*
  28. * NOTE:
  29. *
  30. * In the ISO C standard (IEEE Std 1003.1), there is a strtoimax() function we
  31. * could use in case strtoll() does not exist... See
  32. * https://www.opengroup.org/onlinepubs/009695399/functions/strtoimax.html
  33. */
  34. #if (SIZEOF_CURL_OFF_T > SIZEOF_LONG)
  35. # ifdef HAVE_STRTOLL
  36. # define strtooff strtoll
  37. # else
  38. # if defined(_MSC_VER) && (_MSC_VER >= 1300) && (_INTEGRAL_MAX_BITS >= 64)
  39. # if defined(_SAL_VERSION)
  40. _Check_return_ _CRTIMP __int64 __cdecl _strtoi64(
  41. _In_z_ const char *_String,
  42. _Out_opt_ _Deref_post_z_ char **_EndPtr, _In_ int _Radix);
  43. # else
  44. _CRTIMP __int64 __cdecl _strtoi64(const char *_String,
  45. char **_EndPtr, int _Radix);
  46. # endif
  47. # define strtooff _strtoi64
  48. # else
  49. # define PRIVATE_STRTOOFF 1
  50. # endif
  51. # endif
  52. #else
  53. # define strtooff strtol
  54. #endif
  55. #ifdef PRIVATE_STRTOOFF
  56. /* Range tests can be used for alphanum decoding if characters are consecutive,
  57. like in ASCII. Else an array is scanned. Determine this condition now. */
  58. #if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
  59. #define NO_RANGE_TEST
  60. static const char valchars[] =
  61. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  62. #endif
  63. static int get_char(char c, int base);
  64. /**
  65. * Custom version of the strtooff function. This extracts a curl_off_t
  66. * value from the given input string and returns it.
  67. */
  68. static curl_off_t strtooff(const char *nptr, char **endptr, int base)
  69. {
  70. char *end;
  71. bool is_negative = FALSE;
  72. bool overflow = FALSE;
  73. int i;
  74. curl_off_t value = 0;
  75. /* Skip leading whitespace. */
  76. end = (char *)nptr;
  77. while(ISBLANK(end[0])) {
  78. end++;
  79. }
  80. /* Handle the sign, if any. */
  81. if(end[0] == '-') {
  82. is_negative = TRUE;
  83. end++;
  84. }
  85. else if(end[0] == '+') {
  86. end++;
  87. }
  88. else if(end[0] == '\0') {
  89. /* We had nothing but perhaps some whitespace -- there was no number. */
  90. if(endptr) {
  91. *endptr = end;
  92. }
  93. return 0;
  94. }
  95. /* Handle special beginnings, if present and allowed. */
  96. if(end[0] == '0' && end[1] == 'x') {
  97. if(base == 16 || base == 0) {
  98. end += 2;
  99. base = 16;
  100. }
  101. }
  102. else if(end[0] == '0') {
  103. if(base == 8 || base == 0) {
  104. end++;
  105. base = 8;
  106. }
  107. }
  108. /* Matching strtol, if the base is 0 and it does not look like
  109. * the number is octal or hex, we assume it is base 10.
  110. */
  111. if(base == 0) {
  112. base = 10;
  113. }
  114. /* Loop handling digits. */
  115. for(i = get_char(end[0], base);
  116. i != -1;
  117. end++, i = get_char(end[0], base)) {
  118. if(value > (CURL_OFF_T_MAX - i) / base) {
  119. overflow = TRUE;
  120. break;
  121. }
  122. value = base * value + i;
  123. }
  124. if(!overflow) {
  125. if(is_negative) {
  126. /* Fix the sign. */
  127. value *= -1;
  128. }
  129. }
  130. else {
  131. if(is_negative)
  132. value = CURL_OFF_T_MIN;
  133. else
  134. value = CURL_OFF_T_MAX;
  135. errno = ERANGE;
  136. }
  137. if(endptr)
  138. *endptr = end;
  139. return value;
  140. }
  141. /**
  142. * Returns the value of c in the given base, or -1 if c cannot
  143. * be interpreted properly in that base (i.e., is out of range,
  144. * is a null, etc.).
  145. *
  146. * @param c the character to interpret according to base
  147. * @param base the base in which to interpret c
  148. *
  149. * @return the value of c in base, or -1 if c is not in range
  150. */
  151. static int get_char(char c, int base)
  152. {
  153. #ifndef NO_RANGE_TEST
  154. int value = -1;
  155. if(c <= '9' && c >= '0') {
  156. value = c - '0';
  157. }
  158. else if(c <= 'Z' && c >= 'A') {
  159. value = c - 'A' + 10;
  160. }
  161. else if(c <= 'z' && c >= 'a') {
  162. value = c - 'a' + 10;
  163. }
  164. #else
  165. const char *cp;
  166. int value;
  167. cp = memchr(valchars, c, 10 + 26 + 26);
  168. if(!cp)
  169. return -1;
  170. value = cp - valchars;
  171. if(value >= 10 + 26)
  172. value -= 26; /* Lowercase. */
  173. #endif
  174. if(value >= base) {
  175. value = -1;
  176. }
  177. return value;
  178. }
  179. #endif /* Only present if we need strtoll, but do not have it. */
  180. /*
  181. * Parse a *positive* up to 64-bit number written in ascii.
  182. */
  183. CURLofft curlx_strtoofft(const char *str, char **endp, int base,
  184. curl_off_t *num)
  185. {
  186. char *end = NULL;
  187. curl_off_t number;
  188. errno = 0;
  189. *num = 0; /* clear by default */
  190. DEBUGASSERT(base); /* starting now, avoid base zero */
  191. while(*str && ISBLANK(*str))
  192. str++;
  193. if(('-' == *str) || (ISSPACE(*str))) {
  194. if(endp)
  195. *endp = (char *)str; /* did not actually move */
  196. return CURL_OFFT_INVAL; /* nothing parsed */
  197. }
  198. number = strtooff(str, &end, base);
  199. if(endp)
  200. *endp = end;
  201. if(errno == ERANGE)
  202. /* overflow/underflow */
  203. return CURL_OFFT_FLOW;
  204. else if(str == end)
  205. /* nothing parsed */
  206. return CURL_OFFT_INVAL;
  207. *num = number;
  208. return CURL_OFFT_OK;
  209. }