Patrick Monnerat fixed curl_easy_escape() and curlx_strtoll() to work on
[platform/upstream/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  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25 #include "strtoofft.h"
26
27 /*
28  * NOTE:
29  *
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
33  */
34
35 #ifdef NEED_CURL_STRTOLL
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <errno.h>
39
40 /* Range tests can be used for alphanum decoding if characters are consecutive,
41    like in ASCII. Else an array is scanned. Determine this condition now. */
42
43 #if ('9' - '0') != 9 || ('Z' - 'A') != 25 || ('z' - 'a') != 25
44 #include <string.h>
45
46 #define NO_RANGE_TEST
47
48 static const char valchars[] =
49             "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
50 #endif
51
52 static int get_char(char c, int base);
53
54 /**
55  * Emulated version of the strtoll function.  This extracts a long long
56  * value from the given input string and returns it.
57  */
58 curl_off_t
59 curlx_strtoll(const char *nptr, char **endptr, int base)
60 {
61   char *end;
62   int is_negative = 0;
63   int overflow;
64   int i;
65   curl_off_t value = 0;
66   curl_off_t newval;
67
68   /* Skip leading whitespace. */
69   end = (char *)nptr;
70   while (ISSPACE(end[0])) {
71     end++;
72   }
73
74   /* Handle the sign, if any. */
75   if (end[0] == '-') {
76     is_negative = 1;
77     end++;
78   }
79   else if (end[0] == '+') {
80     end++;
81   }
82   else if (end[0] == '\0') {
83     /* We had nothing but perhaps some whitespace -- there was no number. */
84     if (endptr) {
85       *endptr = end;
86     }
87     return 0;
88   }
89
90   /* Handle special beginnings, if present and allowed. */
91   if (end[0] == '0' && end[1] == 'x') {
92     if (base == 16 || base == 0) {
93       end += 2;
94       base = 16;
95     }
96   }
97   else if (end[0] == '0') {
98     if (base == 8 || base == 0) {
99       end++;
100       base = 8;
101     }
102   }
103
104   /* Matching strtol, if the base is 0 and it doesn't look like
105    * the number is octal or hex, we assume it's base 10.
106    */
107   if (base == 0) {
108     base = 10;
109   }
110
111   /* Loop handling digits. */
112   value = 0;
113   overflow = 0;
114   for (i = get_char(end[0], base);
115        i != -1;
116        end++, i = get_char(end[0], base)) {
117     newval = base * value + i;
118     if (newval < value) {
119       /* We've overflowed. */
120       overflow = 1;
121       break;
122     }
123     else
124       value = newval;
125   }
126
127   if (!overflow) {
128     if (is_negative) {
129       /* Fix the sign. */
130       value *= -1;
131     }
132   }
133   else {
134     if (is_negative)
135       value = CURL_LLONG_MIN;
136     else
137       value = CURL_LLONG_MAX;
138
139     SET_ERRNO(ERANGE);
140   }
141
142   if (endptr)
143     *endptr = end;
144
145   return value;
146 }
147
148 /**
149  * Returns the value of c in the given base, or -1 if c cannot
150  * be interpreted properly in that base (i.e., is out of range,
151  * is a null, etc.).
152  *
153  * @param c     the character to interpret according to base
154  * @param base  the base in which to interpret c
155  *
156  * @return  the value of c in base, or -1 if c isn't in range
157  */
158 static int get_char(char c, int base)
159 {
160 #ifndef NO_RANGE_TEST
161   int value = -1;
162   if (c <= '9' && c >= '0') {
163     value = c - '0';
164   }
165   else if (c <= 'Z' && c >= 'A') {
166     value = c - 'A' + 10;
167   }
168   else if (c <= 'z' && c >= 'a') {
169     value = c - 'a' + 10;
170   }
171 #else
172   const char * cp;
173   int value;
174
175   cp = memchr(valchars, c, 10 + 26 + 26);
176
177   if (!cp)
178     return -1;
179
180   value = cp - valchars;
181
182   if (value >= 10 + 26)
183     value -= 26;                /* Lowercase. */
184 #endif
185
186   if (value >= base) {
187     value = -1;
188   }
189
190   return value;
191 }
192 #endif  /* Only present if we need strtoll, but don't have it. */