From 0b424905738f231c4d8b1e8b27e6104f2406987f Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 8 Oct 2016 16:52:42 +0200 Subject: [PATCH] avoid unprintable characters for StringDataType and escape special chars for JSON, formattting --- src/lib/ebus/datatype.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index bcb36036..c778b172 100644 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -158,26 +158,36 @@ result_t StringDataType::readSymbols(SymbolString& input, const bool isMaster, incr = -1; } - if (outputFormat & OF_JSON) + if (outputFormat & OF_JSON) { output << '"'; + } + output << setfill('0') << (m_isHex ? hex : dec); for (size_t offset = start, i = 0; i < count; offset += incr, i++) { ch = input[baseOffset + offset]; if (m_isHex) { if (i > 0) output << ' '; - output << setw(2) << hex << setfill('0') << static_cast(ch); + output << setw(2) << static_cast(ch); } else { - if (ch < 0x20) - ch = (unsigned char)m_replacement; - if (ch == 0x00) + if (ch == 0x00) { terminated = true; - else if (!terminated) - output << setw(0) << dec << static_cast(ch); + } else if (!terminated) { + if (ch < 0x20) { + ch = (unsigned char)m_replacement; + } else if (!isprint(ch)) { + ch = '?'; + } else if (outputFormat & OF_JSON) { + if (ch == '"' || ch == '\\') { + output << '\\'; // escape + } + } + output << static_cast(ch); + } } } - if (outputFormat & OF_JSON) + if (outputFormat & OF_JSON) { output << '"'; - + } return RESULT_OK; }