]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
Merged from trunk
[thirdparty/squid.git] / src / Parsing.cc
1 /*
2 * DEBUG: section 03 Configuration File Parsing
3 * AUTHOR: Harvest Derived
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34 #include "cache_cf.h"
35 #include "compat/strtoll.h"
36 #include "Parsing.h"
37 #include "globals.h"
38 #include "Debug.h"
39
40 /*
41 * These functions is the same as atoi/l/f, except that they check for errors
42 */
43
44 double
45 xatof(const char *token)
46 {
47 char *end = NULL;
48 double ret = strtod(token, &end);
49
50 if (ret == 0 && end == token) {
51 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: No digits were found in the input value '" << token << "'.");
52 self_destruct();
53 }
54
55 if (*end) {
56 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid value: '" << token << "' is supposed to be a number.");
57 self_destruct();
58 }
59
60 return ret;
61 }
62
63 int
64 xatoi(const char *token)
65 {
66 int64_t input = xatoll(token, 10);
67 int ret = (int) input;
68
69 if (input != static_cast<int64_t>(ret)) {
70 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'int'.");
71 self_destruct();
72 }
73
74 return ret;
75 }
76
77 unsigned int
78 xatoui(const char *token)
79 {
80 int64_t input = xatoll(token, 10);
81 if (input < 0) {
82 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The input value '" << token << "' cannot be less than 0.");
83 self_destruct();
84 }
85
86 unsigned int ret = (unsigned int) input;
87 if (input != static_cast<int64_t>(ret)) {
88 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'unsigned int'.");
89 self_destruct();
90 }
91
92 return ret;
93 }
94
95 long
96 xatol(const char *token)
97 {
98 int64_t input = xatoll(token, 10);
99 long ret = (long) input;
100
101 if (input != static_cast<int64_t>(ret)) {
102 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'long'.");
103 self_destruct();
104 }
105
106 return ret;
107 }
108
109 int64_t
110 xatoll(const char *token, int base)
111 {
112 char *end = NULL;
113 int64_t ret = strtoll(token, &end, base);
114
115 if (end == token) {
116 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: No digits were found in the input value '" << token << "'.");
117 self_destruct();
118 }
119
120 if (*end) {
121 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid value: '" << token << "' is supposed to be a number.");
122 self_destruct();
123 }
124
125 return ret;
126 }
127
128 unsigned short
129 xatos(const char *token)
130 {
131 long port = xatol(token);
132
133 if (port < 0) {
134 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' cannot be less than 0.");
135 self_destruct();
136 }
137
138 if (port & ~0xFFFF) {
139 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'short'.");
140 self_destruct();
141 }
142
143 return port;
144 }
145
146 int64_t
147 GetInteger64(void)
148 {
149 char *token = strtok(NULL, w_space);
150
151 if (token == NULL)
152 self_destruct();
153
154 return xatoll(token, 10);
155 }
156
157 /*
158 * This function is different from others (e.g., GetInteger64, GetShort)
159 * because it supports octal and hexadecimal numbers
160 */
161 int
162 GetInteger(void)
163 {
164 char *token = strtok(NULL, w_space);
165 int i;
166
167 if (token == NULL)
168 self_destruct();
169
170 // The conversion must honor 0 and 0x prefixes, which are important for things like umask
171 int64_t ret = xatoll(token, 0);
172
173 i = (int) ret;
174 if (ret != static_cast<int64_t>(i)) {
175 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'int'.");
176 self_destruct();
177 }
178
179 return i;
180 }
181
182 /*
183 * This function is similar as GetInteger() but the token might contain
184 * the percentage symbol (%) and we check whether the value is in the range
185 * of [0, 100]
186 * So, we accept two types of input: 1. XX% or 2. XX , 0<=XX<=100
187 */
188 int
189 GetPercentage(void)
190 {
191 int p;
192 char *token = strtok(NULL, w_space);
193
194 if (!token) {
195 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: A percentage value is missing.");
196 self_destruct();
197 }
198
199 //if there is a % in the end of the digits, we remove it and go on.
200 char* end = &token[strlen(token)-1];
201 if (*end == '%') {
202 *end = '\0';
203 }
204
205 p = xatoi(token);
206
207 if (p < 0 || p > 100) {
208 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is out of range. A percentage should be within [0, 100].");
209 self_destruct();
210 }
211
212 return p;
213 }
214
215 unsigned short
216 GetShort(void)
217 {
218 char *token = strtok(NULL, w_space);
219
220 if (token == NULL)
221 self_destruct();
222
223 return xatos(token);
224 }
225
226 bool
227 StringToInt(const char *s, int &result, const char **p, int base)
228 {
229 if (s) {
230 char *ptr = 0;
231 const int h = (int) strtol(s, &ptr, base);
232
233 if (ptr != s && ptr) {
234 result = h;
235
236 if (p)
237 *p = ptr;
238
239 return true;
240 }
241 }
242
243 return false;
244 }
245
246 bool
247 StringToInt64(const char *s, int64_t &result, const char **p, int base)
248 {
249 if (s) {
250 char *ptr = 0;
251 const int64_t h = (int64_t) strtoll(s, &ptr, base);
252
253 if (ptr != s && ptr) {
254 result = h;
255
256 if (p)
257 *p = ptr;
258
259 return true;
260 }
261 }
262
263 return false;
264 }
265
266 bool
267 GetHostWithPort(char *token, Ip::Address *ipa)
268 {
269 char *t;
270 char *host;
271 char *tmp;
272 unsigned short port;
273
274 host = NULL;
275 port = 0;
276
277 if (*token == '[') {
278 /* [host]:port */
279 host = token + 1;
280 t = strchr(host, ']');
281 if (!t)
282 return false;
283 *t = '\0';
284 ++t;
285 if (*t != ':')
286 return false;
287 port = xatos(t + 1);
288 } else if ((t = strchr(token, ':'))) {
289 /* host:port */
290 host = token;
291 *t = '\0';
292 port = xatos(t + 1);
293
294 if (0 == port)
295 return false;
296 } else if (strtol(token, &tmp, 10) && !*tmp) {
297 port = xatos(token);
298 } else {
299 host = token;
300 port = 0;
301 }
302
303 if (NULL == host)
304 ipa->SetAnyAddr();
305 else if ( ipa->GetHostByName(host) ) /* dont use ipcache. Accept either FQDN or IPA. */
306 (void) 0;
307 else
308 return false;
309
310 /* port MUST be set after the IPA lookup/conversion is performed. */
311 ipa->SetPort(port);
312
313 return true;
314 }