]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 03 Configuration File Parsing */
13 #include "compat/strtoll.h"
14 #include "ConfigParser.h"
15 #include "debug/Stream.h"
18 #include "sbuf/Stream.h"
21 * These functions is the same as atoi/l/f, except that they check for errors
25 xatof(const char *token
)
28 double ret
= strtod(token
, &end
);
30 if (ret
== 0 && end
== token
) {
31 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: No digits were found in the input value '" << token
<< "'.");
36 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: Invalid value: '" << token
<< "' is supposed to be a number.");
44 xatoi(const char *token
)
46 int64_t input
= xatoll(token
, 10);
47 int ret
= (int) input
;
49 if (input
!= static_cast<int64_t>(ret
)) {
50 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'int'.");
58 xatoui(const char *token
, char eov
)
60 int64_t input
= xatoll(token
, 10, eov
);
62 throw TextException(ToSBuf("the input value '", token
, "' cannot be less than 0"), Here());
64 unsigned int ret
= (unsigned int) input
;
65 if (input
!= static_cast<int64_t>(ret
))
66 throw TextException(ToSBuf("the value '", token
, "' is larger than the type 'unsigned int'"), Here());
72 xatol(const char *token
)
74 int64_t input
= xatoll(token
, 10);
75 long ret
= (long) input
;
77 if (input
!= static_cast<int64_t>(ret
)) {
78 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'long'.");
86 xatoll(const char *token
, int base
, char eov
)
89 int64_t ret
= strtoll(token
, &end
, base
);
92 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: No digits were found in the input value '" << token
<< "'.");
97 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: Invalid value: '" << token
<< "' is supposed to be a number.");
105 xatoull(const char *token
, int base
, char eov
)
107 const auto number
= xatoll(token
, base
, eov
);
109 throw TextException(ToSBuf("the input value '", token
, "' cannot be less than 0"), Here());
110 return static_cast<uint64_t>(number
);
114 xatos(const char *token
)
116 long port
= xatol(token
);
119 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' cannot be less than 0.");
123 if (port
& ~0xFFFF) {
124 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'short'.");
134 char *token
= ConfigParser::NextToken();
137 return -1; // not reachable
140 return xatoll(token
, 10);
144 * This function is different from others (e.g., GetInteger64, GetShort)
145 * because it supports octal and hexadecimal numbers
150 char *token
= ConfigParser::NextToken();
155 return -1; // not reachable
158 // The conversion must honor 0 and 0x prefixes, which are important for things like umask
159 int64_t ret
= xatoll(token
, 0);
162 if (ret
!= static_cast<int64_t>(i
)) {
163 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'int'.");
171 * This function is similar as GetInteger() but the token might contain
172 * the percentage symbol (%) and we check whether the value is in the range
174 * So, we accept two types of input: 1. XX% or 2. XX , 0<=XX<=100
175 * unless the limit parameter is set to false.
178 GetPercentage(bool limit
)
180 char *token
= ConfigParser::NextToken();
183 debugs(3, DBG_CRITICAL
, "FATAL: A percentage value is missing.");
185 return 0.0; // not reachable
188 //if there is a % in the end of the digits, we remove it and go on.
189 char* end
= &token
[strlen(token
)-1];
194 int p
= xatoi(token
);
196 if (p
< 0 || (limit
&& p
> 100)) {
197 debugs(3, DBG_CRITICAL
, "FATAL: The value '" << token
<< "' is out of range. A percentage should be within [0, 100].");
201 return static_cast<double>(p
) / 100.0;
207 char *token
= ConfigParser::NextToken();
210 return 0; // not reachable
217 StringToInt(const char *s
, int &result
, const char **p
, int base
)
221 const int h
= (int) strtol(s
, &ptr
, base
);
223 if (ptr
!= s
&& ptr
) {
237 StringToInt64(const char *s
, int64_t &result
, const char **p
, int base
)
241 const int64_t h
= (int64_t) strtoll(s
, &ptr
, base
);
243 if (ptr
!= s
&& ptr
) {
257 GetHostWithPort(char *token
, Ip::Address
*ipa
)
270 t
= strchr(host
, ']');
278 } else if ((t
= strchr(token
, ':'))) {
286 } else if (strtol(token
, &tmp
, 10) && !*tmp
) {
295 else if (ipa
->GetHostByName(host
)) /* do not use ipcache. Accept either FQDN or IPA. */
300 /* port MUST be set after the IPA lookup/conversion is performed. */