From 095beb64a0752317a9a1d9777284a6c9343417cc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 18 May 2020 09:51:22 +0200 Subject: Add sensitize_strlit() for sanitizing C string literals --- libbutl/utility.ixx | 28 ++++++++++++++++++++++++++++ libbutl/utility.mxx | 8 ++++++++ 2 files changed, 36 insertions(+) (limited to 'libbutl') diff --git a/libbutl/utility.ixx b/libbutl/utility.ixx index 27ef7fb..72fbc5b 100644 --- a/libbutl/utility.ixx +++ b/libbutl/utility.ixx @@ -219,6 +219,34 @@ namespace butl return sanitize_identifier (std::string (s)); } + inline void + sanitize_strlit (const std::string& s, std::string& o) + { + for (size_t i (0), j;; i = j + 1) + { + j = s.find_first_of ("\\\"\n", i); + o.append (s.c_str () + i, (j == std::string::npos ? s.size () : j) - i); + + if (j == std::string::npos) + break; + + switch (s[j]) + { + case '\\': o += "\\\\"; break; + case '"': o += "\\\""; break; + case '\n': o += "\\n"; break; + } + } + } + + inline std::string + sanitize_strlit (const std::string& s) + { + std::string r; + sanitize_strlit (s, r); + return r; + } + inline bool eof (std::istream& is) { diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx index b84e731..251471a 100644 --- a/libbutl/utility.mxx +++ b/libbutl/utility.mxx @@ -195,6 +195,14 @@ LIBBUTL_MODEXPORT namespace butl std::string sanitize_identifier (std::string&&); std::string sanitize_identifier (const std::string&); + // Sanitize a string (e.g., a path) to be a valid C string literal by + // escaping backslahes, double-quotes, and newlines. + // + // Note that in the second version the result is appended to out. + // + std::string sanitize_strlit (const std::string&); + void sanitize_strlit (const std::string&, std::string& out); + // Return true if the string is a valid UTF-8 encoded byte string and, // optionally, its decoded codepoints belong to the specified types or // codepoint whitelist. -- cgit v1.1