format specific/unknown/unparsed/raw values in ASN1ContentHandler instead of in ASN1Reader v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 18 Jul 2021 12:37:40 +0200
branchv_0
changeset 35 bf2c546f49d1
parent 34 c15dc8762bdb
child 36 caed9146f72f
format specific/unknown/unparsed/raw values in ASN1ContentHandler instead of in ASN1Reader
src/lib/ASN1ContentHandler.h
src/lib/BasicASN1Reader.h
src/lib/GenericASN1ContentHandler.h
src/lib/ValidatingASN1ContentHandler.h
--- a/src/lib/ASN1ContentHandler.h	Sat Jul 17 20:38:03 2021 +0200
+++ b/src/lib/ASN1ContentHandler.h	Sun Jul 18 12:37:40 2021 +0200
@@ -291,6 +291,15 @@
 	// OID-IRI
 	// Relative OID-IRI
 
+	/**
+	 * Specific value that was not parsed.
+	 * May be processed in a generic way (as binary data or ASCII or UTF-8 string, when possible)
+	 * or according to given application, context or private specification.
+	 * 
+	 * @param value original raw data
+	 */
+	virtual void writeSpecific(const Header& header, std::string value) = 0;
+
 };
 
 class ASN1ContentHandlerProxy : public ASN1ContentHandler {
@@ -350,6 +359,10 @@
 		handlers.forward(&ASN1ContentHandler::writeDateTime, header, value);
 	}
 
+	void writeSpecific(const Header& header, std::string value) override {
+		handlers.forward(&ASN1ContentHandler::writeSpecific, header, value);
+	}
+
 };
 
 
--- a/src/lib/BasicASN1Reader.h	Sat Jul 17 20:38:03 2021 +0200
+++ b/src/lib/BasicASN1Reader.h	Sun Jul 18 12:37:40 2021 +0200
@@ -253,24 +253,10 @@
 		} else {
 			// TODO: do not skip, parse
 			// TODO: check available bytes before allocating buffer
-			std::vector<uint8_t> temp(typeHeader.length, 0);
-			read(temp.data(), typeHeader.length);
-			// TODO: recover transaction?
-
-			std::stringstream description;
-			description << "UNKNOWN:"
-					<< " tag = " << typeHeader.tag
-					<< " tagClass = " << (int) typeHeader.tagClass
-					<< " pc = " << (int) typeHeader.pc
-					<< " length = " << typeHeader.length
-					<< " definite = " << (typeHeader.definiteLength ? "true" : "false")
-					<< " hex = ";
-			description << std::hex << std::setfill('0');
-			for (uint8_t b : temp) description << std::setw(2) << (int) b;
-			description << " ascii = ";
-			for (uint8_t b : temp) description << (b >= 32 && b < 127 ? (char) b : '.');
-			// TODO: special event for unparsed? (instead of a text string)
-			handlers->writeTextString(typeHeader, description.str());
+			std::string s;
+			s.resize(typeHeader.length);
+			read((uint8_t*) s.data(), typeHeader.length);
+			handlers->writeSpecific(typeHeader, s);
 		}
 
 		commit();
--- a/src/lib/GenericASN1ContentHandler.h	Sat Jul 17 20:38:03 2021 +0200
+++ b/src/lib/GenericASN1ContentHandler.h	Sun Jul 18 12:37:40 2021 +0200
@@ -117,6 +117,20 @@
 		handlers.writeEndElement();
 	}
 
+	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?
+
+		handlers.writeStartElement("specific",{"length", std::to_string(value.size()), "hex", hex.str()});
+		handlers.writeCharacters(ascii.str());
+		handlers.writeEndElement();
+	}
+
+
 };
 
 }
--- a/src/lib/ValidatingASN1ContentHandler.h	Sat Jul 17 20:38:03 2021 +0200
+++ b/src/lib/ValidatingASN1ContentHandler.h	Sun Jul 18 12:37:40 2021 +0200
@@ -94,6 +94,9 @@
 	void writeTextString(const Header& header, std::string value) override {
 	}
 
+	void writeSpecific(const Header& header, std::string value) override {
+	}
+
 };
 
 }