diff -r 807f8543d10e -r 368ba99bb98f src/FreeformASN1ContentHandler.h --- a/src/FreeformASN1ContentHandler.h Sun Jul 25 12:09:30 2021 +0200 +++ b/src/FreeformASN1ContentHandler.h Sun Jul 25 18:22:07 2021 +0200 @@ -26,8 +26,16 @@ namespace in { namespace asn1 { +/** + * Converts arbitrary ASN.1 stream of events to a relation + * i.e. does not require any specific structures/schema. + * + * Useful especially for testing and debugging and sometimes instead of relpipe-in-asn1table, + * because this handler supports streaming and does not depend on any third-party library (no XML dependency). + */ class FreeformASN1ContentHandler : public lib::ASN1ContentHandler { private: + wstring_convert < codecvt_utf8> convertor; // ASN.1 parser works with UTF-8 std::shared_ptr writer; Configuration configuration; std::vector position; @@ -38,6 +46,8 @@ const std::vector& position; relpipe::common::type::Integer level = 0; relpipe::common::type::StringX event; + relpipe::common::type::StringX valueText; + relpipe::common::type::StringX valueBinary; Record(const Header* header, std::vector& position, relpipe::common::type::StringX event) : header(header), position(position), event(event) { } @@ -74,6 +84,19 @@ // writer->writeAttribute(&pc, typeid (pc)); writer->writeAttribute(&tagClass, typeid (tagClass)); writer->writeAttribute(&tag, typeid (tag)); + writer->writeAttribute(&r.valueText, typeid (r.valueText)); + writer->writeAttribute(&r.valueBinary, typeid (r.valueBinary)); + } + + /** + * @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: @@ -94,6 +117,8 @@ // {L"pc", relpipe::writer::TypeId::STRING}, {L"tag_class", relpipe::writer::TypeId::STRING}, {L"tag", relpipe::writer::TypeId::INTEGER}, + {L"value_text", relpipe::writer::TypeId::STRING}, + {L"value_binary", relpipe::writer::TypeId::STRING}, // TODO: OctetString data type (when available) instead of text }, true); position.push_back(-1); // TODO: null @@ -107,6 +132,7 @@ auto id = position.back() + 1; position.pop_back(); position.back() = id; + // TODO: put end event under the start parent? Record r(nullptr, position, L"stream-end"); write(r); } @@ -122,61 +148,91 @@ auto id = position.back() + 1; position.pop_back(); position.back() = id; + // TODO: put end event under the start parent? Record r(nullptr, position, L"collection-end"); write(r); } void writeBitString(const Header& header, std::vector value) override { + std::stringstream bits; + for (bool b : value) bits << (int) b; + // for (bool b : value) bits << (b ? ':' : '.'); // TODO: configurable true/false symbols? + position.back()++; Record r(&header, position, L"bit-string"); + r.valueText = convertor.from_bytes(bits.str()); write(r); } void writeBoolean(const Header& header, bool value) override { position.back()++; Record r(&header, position, L"boolean"); + r.valueText = value ? L"true" : L"false"; + r.valueBinary = value ? L"FF" : L"00"; write(r); } void writeDateTime(const Header& header, DateTime value) override { position.back()++; Record r(&header, position, L"date-time"); + r.valueText = convertor.from_bytes(value.toString()); write(r); } void writeInteger(const Header& header, Integer value) override { position.back()++; Record r(&header, position, L"integer"); + r.valueText = convertor.from_bytes(value.toString()); + r.valueBinary = convertor.from_bytes(value.toHex()); write(r); } void writeNull(const Header& header) override { position.back()++; Record r(&header, position, L"null"); + r.valueText = L"null"; write(r); } void writeOID(const Header& header, ObjectIdentifier value) override { position.back()++; Record r(&header, position, L"oid"); + r.valueText = convertor.from_bytes(value.toString()); write(r); } void writeOctetString(const Header& header, std::string value) override { + std::string text = getText(header, value); + std::stringstream hex; + hex << std::hex << std::setfill('0'); + for (uint8_t b : value) hex << std::setw(2) << (int) b; + position.back()++; Record r(&header, position, L"octet-string"); + r.valueText = convertor.from_bytes(text); + r.valueBinary = convertor.from_bytes(hex.str()); write(r); } void writeTextString(const Header& header, std::string value) override { position.back()++; Record r(&header, position, L"text-string"); + r.valueText = convertor.from_bytes(value); write(r); } void writeSpecific(const Header& header, std::string value) override { + std::stringstream hex; + hex << std::hex << std::setfill('0'); + for (uint8_t b : value) hex << std::setw(2) << (int) b; + + std::stringstream ascii; + for (uint8_t b : value) ascii << (b >= 32 && b < 127 ? (char) b : '.'); // TODO: configurable unsupported symbol? + position.back()++; Record r(&header, position, L"specific"); + r.valueText = convertor.from_bytes(ascii.str()); + r.valueBinary = convertor.from_bytes(hex.str()); write(r); }