diff --git a/src/input/readers/Ascii.cc b/src/input/readers/Ascii.cc index 297f8a7136..aaa124f0c1 100644 --- a/src/input/readers/Ascii.cc +++ b/src/input/readers/Ascii.cc @@ -438,6 +438,8 @@ bool Ascii::DoUpdate() if ( ! getline(splitstream, s, separator[0]) ) break; + s = get_unescaped_string(s); + stringfields[pos] = s; pos++; } diff --git a/src/util.cc b/src/util.cc index cd367cf825..544ba1b573 100644 --- a/src/util.cc +++ b/src/util.cc @@ -42,6 +42,46 @@ #include "Net.h" #include "Reporter.h" +/** + * Takes a string, unescapes all characters that are escaped as hex codes + * (\x##) and turns them into the equivalent ascii-codes. Returns a string + * containing no escaped values + * + * @param str string to unescape + * @return A str::string without escaped characters. + */ +std::string get_unescaped_string(const std::string& str) + { + char* buf = new char [str.length() + 1]; // it will at most have the same length as str. + char* bufpos = buf; + size_t pos = 0; + + while ( pos < str.length() ) + { + if ( str[pos] == '\\' && str[pos+1] == 'x' && + isxdigit(str[pos+2]) && isxdigit(str[pos+3]) ) + { + *bufpos = (decode_hex(str[pos+2]) << 4) + + decode_hex(str[pos+3]); + + pos += 4; + bufpos++; + } + else + { + *bufpos = str[pos]; + bufpos++; + pos++; + } + } + + *bufpos = 0; + + string outstring (buf, bufpos - buf); + delete [] buf; + return outstring; + } + /** * Takes a string, escapes characters into equivalent hex codes (\x##), and * returns a string containing all escaped values. @@ -53,25 +93,25 @@ * @return A std::string containing a list of escaped hex values of the form * \x## */ std::string get_escaped_string(const std::string& str, bool escape_all) -{ - char tbuf[16]; - string esc = ""; + { + char tbuf[16]; + string esc = ""; - for ( size_t i = 0; i < str.length(); ++i ) - { - char c = str[i]; + for ( size_t i = 0; i < str.length(); ++i ) + { + char c = str[i]; - if ( escape_all || isspace(c) || ! isascii(c) || ! isprint(c) ) - { - snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]); - esc += tbuf; + if ( escape_all || isspace(c) || ! isascii(c) || ! isprint(c) ) + { + snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]); + esc += tbuf; + } + else + esc += c; } - else - esc += c; - } - return esc; -} + return esc; + } char* copy_string(const char* s) { diff --git a/src/util.h b/src/util.h index a695c6df6a..fc4b60792b 100644 --- a/src/util.h +++ b/src/util.h @@ -90,6 +90,7 @@ void delete_each(T* t) delete *it; } +std::string get_unescaped_string(const std::string& str); std::string get_escaped_string(const std::string& str, bool escape_all); extern char* copy_string(const char* s);