common attributes in XML v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 22 Jul 2021 00:47:47 +0200
branchv_0
changeset 38 44ee477875ec
parent 37 0845ca3636b6
child 39 6ef41443211e
common attributes in XML
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 <string>
 #include <sstream>
 #include <iomanip>
 
@@ -35,6 +36,30 @@
 class GenericASN1ContentHandler : public ASN1ContentHandler {
 private:
 	XMLContentHandlerProxy handlers;
+
+	std::vector<std::string> getCommonAttributes(const Header& header, std::vector<std::string> 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<XMLContentHandler> 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();
 	}