# HG changeset patch # User František Kučera # Date 1626908774 -7200 # Node ID 6ef41443211ea262c68916710a6b17ab3157bcee # Parent 44ee477875ec5ec296616b1087790ee6a143b720 add @text attribute to octet-string if bytes are ASCII text diff -r 44ee477875ec -r 6ef41443211e src/lib/GenericASN1ContentHandler.h --- a/src/lib/GenericASN1ContentHandler.h Thu Jul 22 00:47:47 2021 +0200 +++ b/src/lib/GenericASN1ContentHandler.h Thu Jul 22 01:06:14 2021 +0200 @@ -60,6 +60,17 @@ return attributes; } + /** + * @param header + * @param value an octet string + * @return original value if octet string was a text string; otherwise an empty string + */ + const std::string getText(const Header& header, const std::string& value) { + // TODO: support also UTF-8 + for (uint8_t b : value) if (b < 32 || b >= 127) return ""; + return value; + } + public: void addHandler(std::shared_ptr handler) { @@ -112,10 +123,17 @@ } void writeOctetString(const Header& header, std::string value) override { + std::vector attributes = getCommonAttributes(header,{"length", std::to_string(value.size())}); + std::string text = getText(header, value); + if (text.size()) { + attributes.push_back("text"); + attributes.push_back(text); + } + std::stringstream hex; hex << std::hex << std::setfill('0'); for (uint8_t b : value) hex << std::setw(2) << (int) b; - handlers.writeStartElement("octet-string", getCommonAttributes(header,{"length", std::to_string(value.size())})); + handlers.writeStartElement("octet-string", attributes); handlers.writeCharacters(hex.str()); handlers.writeEndElement(); }