]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / Parsing.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 03 Configuration File Parsing */
10
11 #include "squid.h"
12 #include "cache_cf.h"
13 #include "compat/strtoll.h"
14 #include "ConfigParser.h"
15 #include "Debug.h"
16 #include "globals.h"
17 #include "Parsing.h"
18
19 /*
20 * These functions is the same as atoi/l/f, except that they check for errors
21 */
22
23 double
24 xatof(const char *token)
25 {
26 char *end = NULL;
27 double ret = strtod(token, &end);
28
29 if (ret == 0 && end == token) {
30 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: No digits were found in the input value '" << token << "'.");
31 self_destruct();
32 }
33
34 if (*end) {
35 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid value: '" << token << "' is supposed to be a number.");
36 self_destruct();
37 }
38
39 return ret;
40 }
41
42 int
43 xatoi(const char *token)
44 {
45 int64_t input = xatoll(token, 10);
46 int ret = (int) input;
47
48 if (input != static_cast<int64_t>(ret)) {
49 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'int'.");
50 self_destruct();
51 }
52
53 return ret;
54 }
55
56 unsigned int
57 xatoui(const char *token, char eov)
58 {
59 int64_t input = xatoll(token, 10, eov);
60 if (input < 0) {
61 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The input value '" << token << "' cannot be less than 0.");
62 self_destruct();
63 }
64
65 unsigned int ret = (unsigned int) input;
66 if (input != static_cast<int64_t>(ret)) {
67 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'unsigned int'.");
68 self_destruct();
69 }
70
71 return ret;
72 }
73
74 long
75 xatol(const char *token)
76 {
77 int64_t input = xatoll(token, 10);
78 long ret = (long) input;
79
80 if (input != static_cast<int64_t>(ret)) {
81 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'long'.");
82 self_destruct();
83 }
84
85 return ret;
86 }
87
88 int64_t
89 xatoll(const char *token, int base, char eov)
90 {
91 char *end = NULL;
92 int64_t ret = strtoll(token, &end, base);
93
94 if (end == token) {
95 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: No digits were found in the input value '" << token << "'.");
96 self_destruct();
97 }
98
99 if (*end != eov) {
100 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: Invalid value: '" << token << "' is supposed to be a number.");
101 self_destruct();
102 }
103
104 return ret;
105 }
106
107 unsigned short
108 xatos(const char *token)
109 {
110 long port = xatol(token);
111
112 if (port < 0) {
113 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' cannot be less than 0.");
114 self_destruct();
115 }
116
117 if (port & ~0xFFFF) {
118 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'short'.");
119 self_destruct();
120 }
121
122 return port;
123 }
124
125 int64_t
126 GetInteger64(void)
127 {
128 char *token = ConfigParser::NextToken();
129
130 if (token == NULL)
131 self_destruct();
132
133 return xatoll(token, 10);
134 }
135
136 /*
137 * This function is different from others (e.g., GetInteger64, GetShort)
138 * because it supports octal and hexadecimal numbers
139 */
140 int
141 GetInteger(void)
142 {
143 char *token = ConfigParser::NextToken();
144 int i;
145
146 if (token == NULL)
147 self_destruct();
148
149 // The conversion must honor 0 and 0x prefixes, which are important for things like umask
150 int64_t ret = xatoll(token, 0);
151
152 i = (int) ret;
153 if (ret != static_cast<int64_t>(i)) {
154 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is larger than the type 'int'.");
155 self_destruct();
156 }
157
158 return i;
159 }
160
161 /*
162 * This function is similar as GetInteger() but the token might contain
163 * the percentage symbol (%) and we check whether the value is in the range
164 * of [0, 100]
165 * So, we accept two types of input: 1. XX% or 2. XX , 0<=XX<=100
166 */
167 int
168 GetPercentage(void)
169 {
170 char *token = ConfigParser::NextToken();
171
172 if (!token) {
173 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: A percentage value is missing.");
174 self_destruct();
175 }
176
177 //if there is a % in the end of the digits, we remove it and go on.
178 char* end = &token[strlen(token)-1];
179 if (*end == '%') {
180 *end = '\0';
181 }
182
183 int p = xatoi(token);
184
185 if (p < 0 || p > 100) {
186 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: The value '" << token << "' is out of range. A percentage should be within [0, 100].");
187 self_destruct();
188 }
189
190 return p;
191 }
192
193 unsigned short
194 GetShort(void)
195 {
196 char *token = ConfigParser::NextToken();
197
198 if (token == NULL)
199 self_destruct();
200
201 return xatos(token);
202 }
203
204 bool
205 StringToInt(const char *s, int &result, const char **p, int base)
206 {
207 if (s) {
208 char *ptr = 0;
209 const int h = (int) strtol(s, &ptr, base);
210
211 if (ptr != s && ptr) {
212 result = h;
213
214 if (p)
215 *p = ptr;
216
217 return true;
218 }
219 }
220
221 return false;
222 }
223
224 bool
225 StringToInt64(const char *s, int64_t &result, const char **p, int base)
226 {
227 if (s) {
228 char *ptr = 0;
229 const int64_t h = (int64_t) strtoll(s, &ptr, base);
230
231 if (ptr != s && ptr) {
232 result = h;
233
234 if (p)
235 *p = ptr;
236
237 return true;
238 }
239 }
240
241 return false;
242 }
243
244 bool
245 GetHostWithPort(char *token, Ip::Address *ipa)
246 {
247 char *t;
248 char *host;
249 char *tmp;
250 unsigned short port;
251
252 host = NULL;
253 port = 0;
254
255 if (*token == '[') {
256 /* [host]:port */
257 host = token + 1;
258 t = strchr(host, ']');
259 if (!t)
260 return false;
261 *t = '\0';
262 ++t;
263 if (*t != ':')
264 return false;
265 port = xatos(t + 1);
266 } else if ((t = strchr(token, ':'))) {
267 /* host:port */
268 host = token;
269 *t = '\0';
270 port = xatos(t + 1);
271
272 if (0 == port)
273 return false;
274 } else if (strtol(token, &tmp, 10) && !*tmp) {
275 port = xatos(token);
276 } else {
277 host = token;
278 port = 0;
279 }
280
281 if (NULL == host)
282 ipa->setAnyAddr();
283 else if ( ipa->GetHostByName(host) ) /* dont use ipcache. Accept either FQDN or IPA. */
284 (void) 0;
285 else
286 return false;
287
288 /* port MUST be set after the IPA lookup/conversion is performed. */
289 ipa->port(port);
290
291 return true;
292 }
293