From 29d2b2ccd06cdd831a7fac66928bfa612003945c Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sat, 1 Jan 2022 16:44:56 -0500 Subject: Print announce error response (#11878) Fix HTTPFetch caller and request ID to 64 bits Check that allocated caller ID is not DISCARD Print body if serverlist request returns error Don't print control characters from HTTP responses Document special HTTPFetch caller IDs Allow unicode to be printed --- src/util/string.cpp | 16 ++++++++++++++++ src/util/string.h | 8 ++++++++ 2 files changed, 24 insertions(+) (limited to 'src/util') diff --git a/src/util/string.cpp b/src/util/string.cpp index 8be5e320a..bc4664997 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -887,3 +887,19 @@ std::string sanitizeDirName(const std::string &str, const std::string &optional_ return wide_to_utf8(safe_name); } + + +void safe_print_string(std::ostream &os, const std::string &str) +{ + std::ostream::fmtflags flags = os.flags(); + os << std::hex; + for (const char c : str) { + if (IS_ASCII_PRINTABLE_CHAR(c) || IS_UTF8_MULTB_START(c) || + IS_UTF8_MULTB_INNER(c) || c == '\n' || c == '\t') { + os << c; + } else { + os << '<' << std::setw(2) << (int)c << '>'; + } + } + os.setf(flags); +} diff --git a/src/util/string.h b/src/util/string.h index bca998f56..8a9e83f22 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -753,3 +753,11 @@ inline irr::core::stringw utf8_to_stringw(const std::string &input) * 2. Remove 'unsafe' characters from the name by replacing them with '_' */ std::string sanitizeDirName(const std::string &str, const std::string &optional_prefix); + +/** + * Prints a sanitized version of a string without control characters. + * '\t' and '\n' are allowed, as are UTF-8 control characters (e.g. RTL). + * ASCII control characters are replaced with their hex encoding in angle + * brackets (e.g. "a\x1eb" -> "a<1e>b"). + */ +void safe_print_string(std::ostream &os, const std::string &str); -- cgit v1.2.3