1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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 ***************************************************************************/
24 #include "strtoofft.h"
29 * In the ISO C standard (IEEE Std 1003.1), there is a strtoimax() function we
30 * could use in case strtoll() doesn't exist... See
31 * http://www.opengroup.org/onlinepubs/009695399/functions/strtoimax.html
34 #ifdef NEED_CURL_STRTOLL
39 /* Range tests can be used for alphanum decoding if characters are consecutive,
40 like in ASCII. Else an array is scanned. Determine this condition now. */
42 #if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
47 static const char valchars[] =
48 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
51 static int get_char(char c, int base);
54 * Emulated version of the strtoll function. This extracts a long long
55 * value from the given input string and returns it.
58 curlx_strtoll(const char *nptr, char **endptr, int base)
67 /* Skip leading whitespace. */
69 while(ISSPACE(end[0])) {
73 /* Handle the sign, if any. */
78 else if(end[0] == '+') {
81 else if(end[0] == '\0') {
82 /* We had nothing but perhaps some whitespace -- there was no number. */
89 /* Handle special beginnings, if present and allowed. */
90 if(end[0] == '0' && end[1] == 'x') {
91 if(base == 16 || base == 0) {
96 else if(end[0] == '0') {
97 if(base == 8 || base == 0) {
103 /* Matching strtol, if the base is 0 and it doesn't look like
104 * the number is octal or hex, we assume it's base 10.
110 /* Loop handling digits. */
113 for (i = get_char(end[0], base);
115 end++, i = get_char(end[0], base)) {
116 newval = base * value + i;
118 /* We've overflowed. */
134 value = CURL_OFF_T_MIN;
136 value = CURL_OFF_T_MAX;
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,
152 * @param c the character to interpret according to base
153 * @param base the base in which to interpret c
155 * @return the value of c in base, or -1 if c isn't in range
157 static int get_char(char c, int base)
159 #ifndef NO_RANGE_TEST
161 if(c <= '9' && c >= '0') {
164 else if(c <= 'Z' && c >= 'A') {
165 value = c - 'A' + 10;
167 else if(c <= 'z' && c >= 'a') {
168 value = c - 'a' + 10;
174 cp = memchr(valchars, c, 10 + 26 + 26);
179 value = cp - valchars;
182 value -= 26; /* Lowercase. */
191 #endif /* Only present if we need strtoll, but don't have it. */