2004-01-06 06:39:46 +08:00
|
|
|
/***************************************************************************
|
2004-10-08 17:39:37 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2004-01-06 06:39:46 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2004-01-06 06:39:46 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2004-10-08 17:39:37 +08:00
|
|
|
*
|
2004-01-06 06:39:46 +08:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2004-01-06 06:39:46 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2017-08-15 05:33:23 +08:00
|
|
|
#include <errno.h>
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 23:23:27 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "strtoofft.h"
|
2004-01-06 06:39:46 +08:00
|
|
|
|
2005-08-12 04:42:02 +08:00
|
|
|
/*
|
|
|
|
* NOTE:
|
|
|
|
*
|
|
|
|
* In the ISO C standard (IEEE Std 1003.1), there is a strtoimax() function we
|
|
|
|
* could use in case strtoll() doesn't exist... See
|
2017-08-09 03:22:34 +08:00
|
|
|
* https://www.opengroup.org/onlinepubs/009695399/functions/strtoimax.html
|
2005-08-12 04:42:02 +08:00
|
|
|
*/
|
|
|
|
|
2017-08-12 21:54:06 +08:00
|
|
|
#if (SIZEOF_CURL_OFF_T > SIZEOF_LONG)
|
2017-08-15 05:33:23 +08:00
|
|
|
# ifdef HAVE_STRTOLL
|
|
|
|
# define strtooff strtoll
|
|
|
|
# else
|
|
|
|
# if defined(_MSC_VER) && (_MSC_VER >= 1300) && (_INTEGRAL_MAX_BITS >= 64)
|
|
|
|
# if defined(_SAL_VERSION)
|
|
|
|
_Check_return_ _CRTIMP __int64 __cdecl _strtoi64(
|
|
|
|
_In_z_ const char *_String,
|
|
|
|
_Out_opt_ _Deref_post_z_ char **_EndPtr, _In_ int _Radix);
|
|
|
|
# else
|
|
|
|
_CRTIMP __int64 __cdecl _strtoi64(const char *_String,
|
|
|
|
char **_EndPtr, int _Radix);
|
|
|
|
# endif
|
|
|
|
# define strtooff _strtoi64
|
|
|
|
# else
|
2017-08-25 17:09:46 +08:00
|
|
|
# define PRIVATE_STRTOOFF 1
|
2017-08-15 05:33:23 +08:00
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define strtooff strtol
|
|
|
|
#endif
|
|
|
|
|
2017-08-25 17:09:46 +08:00
|
|
|
#ifdef PRIVATE_STRTOOFF
|
2004-01-06 06:39:46 +08:00
|
|
|
|
2007-08-05 04:47:59 +08:00
|
|
|
/* Range tests can be used for alphanum decoding if characters are consecutive,
|
|
|
|
like in ASCII. Else an array is scanned. Determine this condition now. */
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
#if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
|
2007-08-05 04:47:59 +08:00
|
|
|
|
|
|
|
#define NO_RANGE_TEST
|
|
|
|
|
|
|
|
static const char valchars[] =
|
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
#endif
|
|
|
|
|
2004-01-06 06:39:46 +08:00
|
|
|
static int get_char(char c, int base);
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:33:23 +08:00
|
|
|
* Custom version of the strtooff function. This extracts a curl_off_t
|
2004-01-06 06:39:46 +08:00
|
|
|
* value from the given input string and returns it.
|
|
|
|
*/
|
2017-08-15 05:33:23 +08:00
|
|
|
static curl_off_t strtooff(const char *nptr, char **endptr, int base)
|
2004-01-06 06:39:46 +08:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
int is_negative = 0;
|
|
|
|
int overflow;
|
|
|
|
int i;
|
2004-01-22 22:31:46 +08:00
|
|
|
curl_off_t value = 0;
|
|
|
|
curl_off_t newval;
|
2004-01-06 06:39:46 +08:00
|
|
|
|
|
|
|
/* Skip leading whitespace. */
|
|
|
|
end = (char *)nptr;
|
2022-09-06 05:21:15 +08:00
|
|
|
while(ISBLANK(end[0])) {
|
2004-01-06 06:39:46 +08:00
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the sign, if any. */
|
2007-11-05 17:45:09 +08:00
|
|
|
if(end[0] == '-') {
|
2004-01-06 06:39:46 +08:00
|
|
|
is_negative = 1;
|
|
|
|
end++;
|
2004-01-22 22:37:06 +08:00
|
|
|
}
|
2007-11-05 17:45:09 +08:00
|
|
|
else if(end[0] == '+') {
|
2004-01-06 06:39:46 +08:00
|
|
|
end++;
|
2004-01-22 22:37:06 +08:00
|
|
|
}
|
2007-11-05 17:45:09 +08:00
|
|
|
else if(end[0] == '\0') {
|
2004-01-06 06:39:46 +08:00
|
|
|
/* We had nothing but perhaps some whitespace -- there was no number. */
|
2007-11-05 17:45:09 +08:00
|
|
|
if(endptr) {
|
2004-01-06 06:39:46 +08:00
|
|
|
*endptr = end;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle special beginnings, if present and allowed. */
|
2007-11-05 17:45:09 +08:00
|
|
|
if(end[0] == '0' && end[1] == 'x') {
|
|
|
|
if(base == 16 || base == 0) {
|
2004-01-06 06:39:46 +08:00
|
|
|
end += 2;
|
|
|
|
base = 16;
|
|
|
|
}
|
2004-01-22 22:37:06 +08:00
|
|
|
}
|
2007-11-05 17:45:09 +08:00
|
|
|
else if(end[0] == '0') {
|
|
|
|
if(base == 8 || base == 0) {
|
2004-01-06 06:39:46 +08:00
|
|
|
end++;
|
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Matching strtol, if the base is 0 and it doesn't look like
|
|
|
|
* the number is octal or hex, we assume it's base 10.
|
|
|
|
*/
|
2007-11-05 17:45:09 +08:00
|
|
|
if(base == 0) {
|
2004-01-06 06:39:46 +08:00
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop handling digits. */
|
|
|
|
value = 0;
|
|
|
|
overflow = 0;
|
2011-04-20 21:17:42 +08:00
|
|
|
for(i = get_char(end[0], base);
|
|
|
|
i != -1;
|
|
|
|
end++, i = get_char(end[0], base)) {
|
2004-01-06 06:39:46 +08:00
|
|
|
newval = base * value + i;
|
2007-11-05 17:45:09 +08:00
|
|
|
if(newval < value) {
|
2004-01-06 06:39:46 +08:00
|
|
|
/* We've overflowed. */
|
|
|
|
overflow = 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-01-22 22:37:06 +08:00
|
|
|
else
|
|
|
|
value = newval;
|
2004-01-06 06:39:46 +08:00
|
|
|
}
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(!overflow) {
|
|
|
|
if(is_negative) {
|
2004-01-06 06:39:46 +08:00
|
|
|
/* Fix the sign. */
|
|
|
|
value *= -1;
|
|
|
|
}
|
2004-01-22 22:37:06 +08:00
|
|
|
}
|
|
|
|
else {
|
2007-11-05 17:45:09 +08:00
|
|
|
if(is_negative)
|
2010-05-30 03:28:16 +08:00
|
|
|
value = CURL_OFF_T_MIN;
|
2004-01-22 22:37:06 +08:00
|
|
|
else
|
2010-05-30 03:28:16 +08:00
|
|
|
value = CURL_OFF_T_MAX;
|
2004-01-06 06:39:46 +08:00
|
|
|
|
2017-06-19 12:52:38 +08:00
|
|
|
errno = ERANGE;
|
2004-01-06 06:39:46 +08:00
|
|
|
}
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(endptr)
|
2004-01-06 06:39:46 +08:00
|
|
|
*endptr = end;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of c in the given base, or -1 if c cannot
|
|
|
|
* be interpreted properly in that base (i.e., is out of range,
|
|
|
|
* is a null, etc.).
|
|
|
|
*
|
|
|
|
* @param c the character to interpret according to base
|
|
|
|
* @param base the base in which to interpret c
|
|
|
|
*
|
|
|
|
* @return the value of c in base, or -1 if c isn't in range
|
|
|
|
*/
|
2004-01-22 22:37:06 +08:00
|
|
|
static int get_char(char c, int base)
|
|
|
|
{
|
2007-08-05 04:47:59 +08:00
|
|
|
#ifndef NO_RANGE_TEST
|
2004-01-22 22:37:06 +08:00
|
|
|
int value = -1;
|
2007-11-05 17:45:09 +08:00
|
|
|
if(c <= '9' && c >= '0') {
|
2004-01-22 22:37:06 +08:00
|
|
|
value = c - '0';
|
|
|
|
}
|
2007-11-05 17:45:09 +08:00
|
|
|
else if(c <= 'Z' && c >= 'A') {
|
2004-01-22 22:37:06 +08:00
|
|
|
value = c - 'A' + 10;
|
|
|
|
}
|
2007-11-05 17:45:09 +08:00
|
|
|
else if(c <= 'z' && c >= 'a') {
|
2004-01-22 22:37:06 +08:00
|
|
|
value = c - 'a' + 10;
|
|
|
|
}
|
2007-08-05 04:47:59 +08:00
|
|
|
#else
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *cp;
|
2007-08-05 04:47:59 +08:00
|
|
|
int value;
|
|
|
|
|
|
|
|
cp = memchr(valchars, c, 10 + 26 + 26);
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(!cp)
|
2007-08-05 04:47:59 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
value = cp - valchars;
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(value >= 10 + 26)
|
2007-08-05 04:47:59 +08:00
|
|
|
value -= 26; /* Lowercase. */
|
|
|
|
#endif
|
2004-01-06 06:39:46 +08:00
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(value >= base) {
|
2004-01-22 22:37:06 +08:00
|
|
|
value = -1;
|
|
|
|
}
|
2004-01-06 06:39:46 +08:00
|
|
|
|
2004-01-22 22:37:06 +08:00
|
|
|
return value;
|
2004-01-06 06:39:46 +08:00
|
|
|
}
|
|
|
|
#endif /* Only present if we need strtoll, but don't have it. */
|
2017-08-15 05:33:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a *positive* up to 64 bit number written in ascii.
|
|
|
|
*/
|
|
|
|
CURLofft curlx_strtoofft(const char *str, char **endp, int base,
|
|
|
|
curl_off_t *num)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
curl_off_t number;
|
|
|
|
errno = 0;
|
|
|
|
*num = 0; /* clear by default */
|
2022-11-17 15:41:44 +08:00
|
|
|
DEBUGASSERT(base); /* starting now, avoid base zero */
|
2017-10-06 03:45:51 +08:00
|
|
|
|
2022-09-06 05:21:15 +08:00
|
|
|
while(*str && ISBLANK(*str))
|
2017-08-15 05:33:23 +08:00
|
|
|
str++;
|
2022-09-09 18:46:01 +08:00
|
|
|
if(('-' == *str) || (ISSPACE(*str))) {
|
2017-08-15 05:33:23 +08:00
|
|
|
if(endp)
|
|
|
|
*endp = (char *)str; /* didn't actually move */
|
|
|
|
return CURL_OFFT_INVAL; /* nothing parsed */
|
|
|
|
}
|
|
|
|
number = strtooff(str, &end, base);
|
|
|
|
if(endp)
|
|
|
|
*endp = end;
|
|
|
|
if(errno == ERANGE)
|
|
|
|
/* overflow/underflow */
|
|
|
|
return CURL_OFFT_FLOW;
|
|
|
|
else if(str == end)
|
|
|
|
/* nothing parsed */
|
|
|
|
return CURL_OFFT_INVAL;
|
|
|
|
|
|
|
|
*num = number;
|
|
|
|
return CURL_OFFT_OK;
|
|
|
|
}
|