Git init
[external/curl.git] / lib / strtoofft.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, 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 http://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
23 #include "setup.h"
24 #include "strtoofft.h"
25
26 /*
27  * NOTE:
28  *
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
32  */
33
34 #ifdef NEED_CURL_STRTOLL
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <errno.h>
38
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. */
41
42 #if('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
43 #include <string.h>
44
45 #define NO_RANGE_TEST
46
47 static const char valchars[] =
48             "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
49 #endif
50
51 static int get_char(char c, int base);
52
53 /**
54  * Emulated version of the strtoll function.  This extracts a long long
55  * value from the given input string and returns it.
56  */
57 curl_off_t
58 curlx_strtoll(const char *nptr, char **endptr, int base)
59 {
60   char *end;
61   int is_negative = 0;
62   int overflow;
63   int i;
64   curl_off_t value = 0;
65   curl_off_t newval;
66
67   /* Skip leading whitespace. */
68   end = (char *)nptr;
69   while(ISSPACE(end[0])) {
70     end++;
71   }
72
73   /* Handle the sign, if any. */
74   if(end[0] == '-') {
75     is_negative = 1;
76     end++;
77   }
78   else if(end[0] == '+') {
79     end++;
80   }
81   else if(end[0] == '\0') {
82     /* We had nothing but perhaps some whitespace -- there was no number. */
83     if(endptr) {
84       *endptr = end;
85     }
86     return 0;
87   }
88
89   /* Handle special beginnings, if present and allowed. */
90   if(end[0] == '0' && end[1] == 'x') {
91     if(base == 16 || base == 0) {
92       end += 2;
93       base = 16;
94     }
95   }
96   else if(end[0] == '0') {
97     if(base == 8 || base == 0) {
98       end++;
99       base = 8;
100     }
101   }
102
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.
105    */
106   if(base == 0) {
107     base = 10;
108   }
109
110   /* Loop handling digits. */
111   value = 0;
112   overflow = 0;
113   for (i = get_char(end[0], base);
114        i != -1;
115        end++, i = get_char(end[0], base)) {
116     newval = base * value + i;
117     if(newval < value) {
118       /* We've overflowed. */
119       overflow = 1;
120       break;
121     }
122     else
123       value = newval;
124   }
125
126   if(!overflow) {
127     if(is_negative) {
128       /* Fix the sign. */
129       value *= -1;
130     }
131   }
132   else {
133     if(is_negative)
134       value = CURL_OFF_T_MIN;
135     else
136       value = CURL_OFF_T_MAX;
137
138     SET_ERRNO(ERANGE);
139   }
140
141   if(endptr)
142     *endptr = end;
143
144   return value;
145 }
146
147 /**
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,
150  * is a null, etc.).
151  *
152  * @param c     the character to interpret according to base
153  * @param base  the base in which to interpret c
154  *
155  * @return  the value of c in base, or -1 if c isn't in range
156  */
157 static int get_char(char c, int base)
158 {
159 #ifndef NO_RANGE_TEST
160   int value = -1;
161   if(c <= '9' && c >= '0') {
162     value = c - '0';
163   }
164   else if(c <= 'Z' && c >= 'A') {
165     value = c - 'A' + 10;
166   }
167   else if(c <= 'z' && c >= 'a') {
168     value = c - 'a' + 10;
169   }
170 #else
171   const char * cp;
172   int value;
173
174   cp = memchr(valchars, c, 10 + 26 + 26);
175
176   if(!cp)
177     return -1;
178
179   value = cp - valchars;
180
181   if(value >= 10 + 26)
182     value -= 26;                /* Lowercase. */
183 #endif
184
185   if(value >= base) {
186     value = -1;
187   }
188
189   return value;
190 }
191 #endif  /* Only present if we need strtoll, but don't have it. */