# HG changeset patch # User František Kučera # Date 1612654417 -3600 # Node ID 47b677177365c5fccc75aad6ed4a18f6a78d9963 # Parent a3794fe5ea4b4dddfb994efc7b3c41403064f382 format binary data as HEX diff -r a3794fe5ea4b -r 47b677177365 src/XMLDocumentConstructor.h --- a/src/XMLDocumentConstructor.h Sat Feb 06 20:32:26 2021 +0100 +++ b/src/XMLDocumentConstructor.h Sun Feb 07 00:33:37 2021 +0100 @@ -61,7 +61,22 @@ return result; } - std::string fetchBody(std::shared_ptr body) { + std::string toHex(const std::string& value) { + static const char* const hexSymbols = "0123456789abcdef"; + size_t length = value.length(); + + std::string result; + result.reserve(3 * length - 1); + for (size_t i = 0; i < length; i++) { + const unsigned char ch = value[i]; + result.push_back(hexSymbols[ch >> 4]); + result.push_back(hexSymbols[ch & 15]); + if (i < length - 1) result.push_back(' '); + } + return result; + } + + std::string fetchBodyText(std::shared_ptr body) { std::stringstream result; vmime::utility::outputStreamAdapter resultAdapter(result); @@ -77,13 +92,24 @@ return result.str(); } + std::string fetchBodyBinary(std::shared_ptr body) { + std::stringstream result; + vmime::utility::outputStreamAdapter binaryResultAdapter(result); + body->getContents()->extract(binaryResultAdapter); + binaryResultAdapter.flush(); + return toHex(result.str()); + } + void appendBody(xmlpp::Element* element, std::shared_ptr body) { element->set_attribute("content-type", format(body->getContentType())); // element->set_attribute("content-type-charset", body->getCharset().getName()); // element->set_attribute("content-transfer-encoding", body->getEncoding().getName()); if (body->getPartCount() == 0) { - element->add_child_cdata(fetchBody(body)); + if (body->getContentType().getType() == "text") element->add_child_cdata(fetchBodyText(body)); + else element->add_child_text(fetchBodyBinary(body)); + // TODO: if content is valid XML, import it in the DOM tree instead of pasting as a nested text/cdata + // TODO: optional trim of long data } else { for (auto part : body->getPartList()) { xmlpp::Element* partElement = element->add_child("part");