1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include "strtoofft.h"
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 * http://www.opengroup.org/onlinepubs/009695399/functions/strtoimax.html
35 #ifdef NEED_CURL_STRTOLL
37 /* Range tests can be used for alphanum decoding if characters are consecutive,
38 like in ASCII. Else an array is scanned. Determine this condition now. */
40 #if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
44 static const char valchars[] =
45 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
48 static int get_char(char c, int base);
51 * Emulated version of the strtoll function. This extracts a long long
52 * value from the given input string and returns it.
55 curlx_strtoll(const char *nptr, char **endptr, int base)
64 /* Skip leading whitespace. */
66 while(ISSPACE(end[0])) {
70 /* Handle the sign, if any. */
75 else if(end[0] == '+') {
78 else if(end[0] == '\0') {
79 /* We had nothing but perhaps some whitespace -- there was no number. */
86 /* Handle special beginnings, if present and allowed. */
87 if(end[0] == '0' && end[1] == 'x') {
88 if(base == 16 || base == 0) {
93 else if(end[0] == '0') {
94 if(base == 8 || base == 0) {
100 /* Matching strtol, if the base is 0 and it doesn't look like
101 * the number is octal or hex, we assume it's base 10.
107 /* Loop handling digits. */
110 for(i = get_char(end[0], base);
112 end++, i = get_char(end[0], base)) {
113 newval = base * value + i;
115 /* We've overflowed. */
131 value = CURL_OFF_T_MIN;
133 value = CURL_OFF_T_MAX;
145 * Returns the value of c in the given base, or -1 if c cannot
146 * be interpreted properly in that base (i.e., is out of range,
149 * @param c the character to interpret according to base
150 * @param base the base in which to interpret c
152 * @return the value of c in base, or -1 if c isn't in range
154 static int get_char(char c, int base)
156 #ifndef NO_RANGE_TEST
158 if(c <= '9' && c >= '0') {
161 else if(c <= 'Z' && c >= 'A') {
162 value = c - 'A' + 10;
164 else if(c <= 'z' && c >= 'a') {
165 value = c - 'a' + 10;
171 cp = memchr(valchars, c, 10 + 26 + 26);
176 value = cp - valchars;
179 value -= 26; /* Lowercase. */
188 #endif /* Only present if we need strtoll, but don't have it. */