diff options
Diffstat (limited to 'Utilities/cmcurl/lib/strtoofft.c')
-rw-r--r-- | Utilities/cmcurl/lib/strtoofft.c | 85 |
1 files changed, 54 insertions, 31 deletions
diff --git a/Utilities/cmcurl/lib/strtoofft.c b/Utilities/cmcurl/lib/strtoofft.c index 3ab1bfd..03a97e8 100644 --- a/Utilities/cmcurl/lib/strtoofft.c +++ b/Utilities/cmcurl/lib/strtoofft.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -18,10 +18,10 @@ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * - * $Id$ ***************************************************************************/ -#include "setup.h" +#include "curl_setup.h" + #include "strtoofft.h" /* @@ -33,9 +33,17 @@ */ #ifdef NEED_CURL_STRTOLL -#include <stdlib.h> -#include <ctype.h> -#include <errno.h> + +/* Range tests can be used for alphanum decoding if characters are consecutive, + like in ASCII. Else an array is scanned. Determine this condition now. */ + +#if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25 + +#define NO_RANGE_TEST + +static const char valchars[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +#endif static int get_char(char c, int base); @@ -55,35 +63,35 @@ curlx_strtoll(const char *nptr, char **endptr, int base) /* Skip leading whitespace. */ end = (char *)nptr; - while (ISSPACE(end[0])) { + while(ISSPACE(end[0])) { end++; } /* Handle the sign, if any. */ - if (end[0] == '-') { + if(end[0] == '-') { is_negative = 1; end++; } - else if (end[0] == '+') { + else if(end[0] == '+') { end++; } - else if (end[0] == '\0') { + else if(end[0] == '\0') { /* We had nothing but perhaps some whitespace -- there was no number. */ - if (endptr) { + if(endptr) { *endptr = end; } return 0; } /* Handle special beginnings, if present and allowed. */ - if (end[0] == '0' && end[1] == 'x') { - if (base == 16 || base == 0) { + if(end[0] == '0' && end[1] == 'x') { + if(base == 16 || base == 0) { end += 2; base = 16; } } - else if (end[0] == '0') { - if (base == 8 || base == 0) { + else if(end[0] == '0') { + if(base == 8 || base == 0) { end++; base = 8; } @@ -92,18 +100,18 @@ curlx_strtoll(const char *nptr, char **endptr, int base) /* 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. */ - if (base == 0) { + if(base == 0) { base = 10; } /* Loop handling digits. */ value = 0; overflow = 0; - for (i = get_char(end[0], base); - i != -1; - end++, i = get_char(end[0], base)) { + for(i = get_char(end[0], base); + i != -1; + end++, i = get_char(end[0], base)) { newval = base * value + i; - if (newval < value) { + if(newval < value) { /* We've overflowed. */ overflow = 1; break; @@ -112,22 +120,22 @@ curlx_strtoll(const char *nptr, char **endptr, int base) value = newval; } - if (!overflow) { - if (is_negative) { + if(!overflow) { + if(is_negative) { /* Fix the sign. */ value *= -1; } } else { - if (is_negative) - value = CURL_LLONG_MIN; + if(is_negative) + value = CURL_OFF_T_MIN; else - value = CURL_LLONG_MAX; + value = CURL_OFF_T_MAX; - errno = ERANGE; + SET_ERRNO(ERANGE); } - if (endptr) + if(endptr) *endptr = end; return value; @@ -145,18 +153,33 @@ curlx_strtoll(const char *nptr, char **endptr, int base) */ static int get_char(char c, int base) { +#ifndef NO_RANGE_TEST int value = -1; - if (c <= '9' && c >= '0') { + if(c <= '9' && c >= '0') { value = c - '0'; } - else if (c <= 'Z' && c >= 'A') { + else if(c <= 'Z' && c >= 'A') { value = c - 'A' + 10; } - else if (c <= 'z' && c >= 'a') { + else if(c <= 'z' && c >= 'a') { value = c - 'a' + 10; } +#else + const char * cp; + int value; + + cp = memchr(valchars, c, 10 + 26 + 26); + + if(!cp) + return -1; + + value = cp - valchars; + + if(value >= 10 + 26) + value -= 26; /* Lowercase. */ +#endif - if (value >= base) { + if(value >= base) { value = -1; } |