2
0

strtoofft.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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() doesn't 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. int is_negative = 0;
  72. int overflow;
  73. int i;
  74. curl_off_t value = 0;
  75. curl_off_t newval;
  76. /* Skip leading whitespace. */
  77. end = (char *)nptr;
  78. while(ISBLANK(end[0])) {
  79. end++;
  80. }
  81. /* Handle the sign, if any. */
  82. if(end[0] == '-') {
  83. is_negative = 1;
  84. end++;
  85. }
  86. else if(end[0] == '+') {
  87. end++;
  88. }
  89. else if(end[0] == '\0') {
  90. /* We had nothing but perhaps some whitespace -- there was no number. */
  91. if(endptr) {
  92. *endptr = end;
  93. }
  94. return 0;
  95. }
  96. /* Handle special beginnings, if present and allowed. */
  97. if(end[0] == '0' && end[1] == 'x') {
  98. if(base == 16 || base == 0) {
  99. end += 2;
  100. base = 16;
  101. }
  102. }
  103. else if(end[0] == '0') {
  104. if(base == 8 || base == 0) {
  105. end++;
  106. base = 8;
  107. }
  108. }
  109. /* Matching strtol, if the base is 0 and it doesn't look like
  110. * the number is octal or hex, we assume it's base 10.
  111. */
  112. if(base == 0) {
  113. base = 10;
  114. }
  115. /* Loop handling digits. */
  116. value = 0;
  117. overflow = 0;
  118. for(i = get_char(end[0], base);
  119. i != -1;
  120. end++, i = get_char(end[0], base)) {
  121. newval = base * value + i;
  122. if(newval < value) {
  123. /* We've overflowed. */
  124. overflow = 1;
  125. break;
  126. }
  127. else
  128. value = newval;
  129. }
  130. if(!overflow) {
  131. if(is_negative) {
  132. /* Fix the sign. */
  133. value *= -1;
  134. }
  135. }
  136. else {
  137. if(is_negative)
  138. value = CURL_OFF_T_MIN;
  139. else
  140. value = CURL_OFF_T_MAX;
  141. errno = ERANGE;
  142. }
  143. if(endptr)
  144. *endptr = end;
  145. return value;
  146. }
  147. /**
  148. * Returns the value of c in the given base, or -1 if c cannot
  149. * be interpreted properly in that base (i.e., is out of range,
  150. * is a null, etc.).
  151. *
  152. * @param c the character to interpret according to base
  153. * @param base the base in which to interpret c
  154. *
  155. * @return the value of c in base, or -1 if c isn't in range
  156. */
  157. static int get_char(char c, int base)
  158. {
  159. #ifndef NO_RANGE_TEST
  160. int value = -1;
  161. if(c <= '9' && c >= '0') {
  162. value = c - '0';
  163. }
  164. else if(c <= 'Z' && c >= 'A') {
  165. value = c - 'A' + 10;
  166. }
  167. else if(c <= 'z' && c >= 'a') {
  168. value = c - 'a' + 10;
  169. }
  170. #else
  171. const char *cp;
  172. int value;
  173. cp = memchr(valchars, c, 10 + 26 + 26);
  174. if(!cp)
  175. return -1;
  176. value = cp - valchars;
  177. if(value >= 10 + 26)
  178. value -= 26; /* Lowercase. */
  179. #endif
  180. if(value >= base) {
  181. value = -1;
  182. }
  183. return value;
  184. }
  185. #endif /* Only present if we need strtoll, but don't have it. */
  186. /*
  187. * Parse a *positive* up to 64 bit number written in ascii.
  188. */
  189. CURLofft curlx_strtoofft(const char *str, char **endp, int base,
  190. curl_off_t *num)
  191. {
  192. char *end;
  193. curl_off_t number;
  194. errno = 0;
  195. *num = 0; /* clear by default */
  196. DEBUGASSERT(base); /* starting now, avoid base zero */
  197. while(*str && ISBLANK(*str))
  198. str++;
  199. if(('-' == *str) || (ISSPACE(*str))) {
  200. if(endp)
  201. *endp = (char *)str; /* didn't actually move */
  202. return CURL_OFFT_INVAL; /* nothing parsed */
  203. }
  204. number = strtooff(str, &end, base);
  205. if(endp)
  206. *endp = end;
  207. if(errno == ERANGE)
  208. /* overflow/underflow */
  209. return CURL_OFFT_FLOW;
  210. else if(str == end)
  211. /* nothing parsed */
  212. return CURL_OFFT_INVAL;
  213. *num = number;
  214. return CURL_OFFT_OK;
  215. }