diff --git a/src/script_opt/CPP/Util.cc b/src/script_opt/CPP/Util.cc index c2a6fed195..1c64c9af21 100644 --- a/src/script_opt/CPP/Util.cc +++ b/src/script_opt/CPP/Util.cc @@ -75,4 +75,60 @@ void unlock_file(const string& fname, FILE* f) } } +string CPPEscape(const char* b, int len) + { + string res; + + for ( int i = 0; i < len; ++i ) + { + unsigned char c = b[i]; + + switch ( c ) + { + case '\a': + res += "\\a"; + break; + case '\b': + res += "\\b"; + break; + case '\f': + res += "\\f"; + break; + case '\n': + res += "\\n"; + break; + case '\r': + res += "\\r"; + break; + case '\t': + res += "\\t"; + break; + case '\v': + res += "\\v"; + break; + + case '\\': + res += "\\\\"; + break; + case '"': + res += "\\\""; + break; + + default: + if ( isprint(c) ) + res += c; + else + { + char buf[8192]; + snprintf(buf, sizeof buf, "%03o", c); + res += "\\"; + res += buf; + } + break; + } + } + + return res; + } + } // zeek::detail diff --git a/src/script_opt/CPP/Util.h b/src/script_opt/CPP/Util.h index 6ea06e6752..23e2533922 100644 --- a/src/script_opt/CPP/Util.h +++ b/src/script_opt/CPP/Util.h @@ -36,4 +36,12 @@ extern bool is_CPP_compilable(const ProfileFunc* pf, const char** reason = nullp extern void lock_file(const std::string& fname, FILE* f); extern void unlock_file(const std::string& fname, FILE* f); +// For the given byte array / string, returns a version expanded +// with escape sequences in order to represent it as a C++ string. +extern std::string CPPEscape(const char* b, int len); +inline std::string CPPEscape(const char* s) + { + return CPPEscape(s, strlen(s)); + } + } // zeek::detail