# HG changeset patch # User František Kučera # Date 1626907667 -7200 # Node ID 44ee477875ec5ec296616b1087790ee6a143b720 # Parent 0845ca3636b6e0dcb0823bc268b0bc0b8163aa74 common attributes in XML diff -r 0845ca3636b6 -r 44ee477875ec src/lib/GenericASN1ContentHandler.h --- a/src/lib/GenericASN1ContentHandler.h Sun Jul 18 22:43:16 2021 +0200 +++ b/src/lib/GenericASN1ContentHandler.h Thu Jul 22 00:47:47 2021 +0200 @@ -16,6 +16,7 @@ */ #pragma once +#include #include #include @@ -35,6 +36,30 @@ class GenericASN1ContentHandler : public ASN1ContentHandler { private: XMLContentHandlerProxy handlers; + + std::vector getCommonAttributes(const Header& header, std::vector attributes = {}) { + std::string tag = std::to_string(header.tag); + std::string tagClass = std::to_string((uint64_t) header.tagClass); + std::string pc = std::to_string((uint8_t) header.pc); + + if (header.tagClass == TagClass::Universal) tagClass = "universal"; + else if (header.tagClass == TagClass::ContextSpecific) tagClass = "context"; + else if (header.tagClass == TagClass::Application) tagClass = "application"; + else if (header.tagClass == TagClass::Private) tagClass = "private"; + + // if (header.pc == PC::Constructed) pc = "constructed"; + // else if (header.pc == PC::Primitive) pc = "primitive"; + + attributes.push_back("tag"); + attributes.push_back(tag); + // attributes.push_back("pc"); + // attributes.push_back(pc); + attributes.push_back("class"); + attributes.push_back(tagClass); + + return attributes; + } + public: void addHandler(std::shared_ptr handler) { @@ -53,10 +78,10 @@ } void writeCollectionStart(const Header& header) override { - if (header.tag == UniversalType::Sequence) handlers.writeStartElement("sequence"); - else if (header.tag == UniversalType::Set) handlers.writeStartElement("set"); - else if (header.tag == UniversalType::OctetString || header.tag == UniversalType::BitString) handlers.writeStartElement("encapsulated",{"type", std::to_string(header.tag)}); // TODO: type name, better attributes - else handlers.writeStartElement("constructed"); + if (header.tag == UniversalType::Sequence) handlers.writeStartElement("sequence", getCommonAttributes(header)); + else if (header.tag == UniversalType::Set) handlers.writeStartElement("set", getCommonAttributes(header)); + else if (header.tag == UniversalType::OctetString || header.tag == UniversalType::BitString) handlers.writeStartElement("encapsulated", getCommonAttributes(header)); + else handlers.writeStartElement("constructed", getCommonAttributes(header)); } void writeCollectionEnd() override { @@ -64,24 +89,24 @@ } void writeBoolean(const Header& header, bool value) override { - handlers.writeStartElement("boolean"); + handlers.writeStartElement("boolean", getCommonAttributes(header)); handlers.writeCharacters(value ? "true" : "false"); handlers.writeEndElement(); } void writeNull(const Header& header) override { - handlers.writeStartElement("null"); + handlers.writeStartElement("null", getCommonAttributes(header)); handlers.writeEndElement(); } void writeInteger(const Header& header, Integer value) override { - handlers.writeStartElement("integer",{"hex", value.toHex()}); + handlers.writeStartElement("integer", getCommonAttributes(header,{"hex", value.toHex()})); handlers.writeCharacters(value.toString()); handlers.writeEndElement(); } void writeTextString(const Header& header, std::string value) override { - handlers.writeStartElement("text-string",{"type", std::to_string(header.tag)}); // TODO: type name, better attributes + handlers.writeStartElement("text-string", getCommonAttributes(header)); handlers.writeCharacters(value); handlers.writeEndElement(); } @@ -90,7 +115,7 @@ std::stringstream hex; hex << std::hex << std::setfill('0'); for (uint8_t b : value) hex << std::setw(2) << (int) b; - handlers.writeStartElement("octet-string",{"length", std::to_string(value.size())}); + handlers.writeStartElement("octet-string", getCommonAttributes(header,{"length", std::to_string(value.size())})); handlers.writeCharacters(hex.str()); handlers.writeEndElement(); } @@ -99,20 +124,20 @@ std::stringstream bits; for (bool b : value) bits << (int) b; // for (bool b : value) bits << (b ? ':' : '.'); // TODO: configurable true/false symbols? - handlers.writeStartElement("bit-string",{"length", std::to_string(value.size())}); + handlers.writeStartElement("bit-string", getCommonAttributes(header,{"length", std::to_string(value.size())})); handlers.writeCharacters(bits.str()); handlers.writeEndElement(); } void writeOID(const Header& header, ObjectIdentifier value) override { // TODO: optionally expand into separate elements with additional metadata - handlers.writeStartElement("oid"); + handlers.writeStartElement("oid", getCommonAttributes(header)); // TODO: {"length", std::to_string(value.size())} handlers.writeCharacters(value.toString()); handlers.writeEndElement(); } void writeDateTime(const Header& header, DateTime value) override { - handlers.writeStartElement("date-time"); + handlers.writeStartElement("date-time", getCommonAttributes(header)); handlers.writeCharacters(value.toString()); handlers.writeEndElement(); } @@ -125,7 +150,7 @@ std::stringstream ascii; for (uint8_t b : value) ascii << (b >= 32 && b < 127 ? (char) b : '.'); // TODO: configurable unsupported symbol? - handlers.writeStartElement("specific",{"length", std::to_string(value.size()), "hex", hex.str()}); + handlers.writeStartElement("specific", getCommonAttributes(header,{"length", std::to_string(value.size()), "hex", hex.str()})); handlers.writeCharacters(ascii.str()); handlers.writeEndElement(); }