Adds functions limitTo and toHexIfNeed into utils.cc

Those will be used in order to make the debug and audit logs
readable.
This commit is contained in:
Felipe Zimmerle 2015-09-15 16:07:03 -03:00
parent 97214edf6e
commit 8772daec4d
2 changed files with 31 additions and 0 deletions

View File

@ -992,6 +992,35 @@ int urldecode_uni_nonstrict_inplace_ex(Assay *assay, unsigned char *input,
return count;
}
std::string limitTo(int amount, const std::string &str) {
std::string ret;
if (str.length() > amount) {
ret.assign(str, 0, amount);
ret = ret + " (" + std::to_string(str.length() - amount) + " " \
"characters omitted)";
return ret;
}
return str;
}
std::string toHexIfNeeded(const std::string &str) {
std::stringstream res;
size_t pos;
for (int i = 0; i < str.size(); i++) {
int c = str.at(i);
if (c < 33 || c > 126) {
res << "\\x" << std::setw(2) << std::setfill('0') << std::hex << c;
} else {
res << str.at(i);
}
}
return res.str();
}
} // namespace ModSecurity

View File

@ -45,6 +45,8 @@ namespace ModSecurity {
int urldecode_uni_nonstrict_inplace_ex(Assay *assay, unsigned char *input,
int64_t input_len, int *changed);
std::string phase_name(int x);
std::string limitTo(int amount, const std::string &str);
std::string toHexIfNeeded(const std::string &str);
} // namespace ModSecurity
#define SRC_UTILS_H_