strtoofft.c 5.7 KB

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