support constructed (like a collection) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 21 Jun 2021 21:36:01 +0200
branchv_0
changeset 15 95ca127ba816
parent 14 02725d301010
child 16 bb457bb5b515
support constructed (like a collection)
src/lib/ASN1ContentHandler.h
src/lib/BasicASN1Reader.h
src/lib/GenericASN1ContentHandler.h
--- a/src/lib/ASN1ContentHandler.h	Sun Jun 20 21:06:02 2021 +0200
+++ b/src/lib/ASN1ContentHandler.h	Mon Jun 21 21:36:01 2021 +0200
@@ -28,6 +28,7 @@
 public:
 
 	enum class CollectionType : uint64_t {
+		Constructed, // TODO: special event?
 		Sequence = 16,
 		Set = 17
 	};
--- a/src/lib/BasicASN1Reader.h	Sun Jun 20 21:06:02 2021 +0200
+++ b/src/lib/BasicASN1Reader.h	Mon Jun 21 21:36:01 2021 +0200
@@ -72,6 +72,7 @@
 			if (l.definiteLength && l.length == getBytesRead() - l.start) {
 				level.pop_back();
 				handlers.writeCollectionEnd();
+				checkRemainingItems(); // multiple collections may end at the same point
 			}
 		}
 	}
@@ -123,17 +124,21 @@
 			handlers.writeStreamStart();
 			started = true;
 		}
-		
+
 		// TODO: check tagClass and pc
 
 		// TODO: constants, more types
-		if (typeHeader.tag == 0) handlers.writeCollectionEnd();
-		else if (typeHeader.tag == 16) {
+		if (typeHeader.tag == 0 && typeHeader.tagClass == TagClass::Universal && typeHeader.pc == PC::Primitive) {
+			handlers.writeCollectionEnd();
+		} else if (typeHeader.tag == 16) {
 			level.push_back({typeHeader.definiteLength, typeHeader.length, getBytesRead()}); // TODO: transaction
 			handlers.writeCollectionStart(ASN1ContentHandler::CollectionType::Sequence);
 		} else if (typeHeader.tag == 17) {
 			level.push_back({typeHeader.definiteLength, typeHeader.length, getBytesRead()}); // TODO: transaction
 			handlers.writeCollectionStart(ASN1ContentHandler::CollectionType::Set);
+		} else if (typeHeader.pc == PC::Constructed) {
+			level.push_back({typeHeader.definiteLength, typeHeader.length, getBytesRead()}); // TODO: transaction
+			handlers.writeCollectionStart(ASN1ContentHandler::CollectionType::Constructed);
 		} else if (typeHeader.tag == 5 && typeHeader.length == 0) {
 			handlers.writeNull();
 		} else if (typeHeader.tag == 1) {
@@ -190,8 +195,9 @@
 public:
 
 	void close() override {
+		checkRemainingItems();
 		// TODO: check the bytes remaining in the buffer
-		//if (started) handlers.writeStreamEnd();
+		if (started) handlers.writeStreamEnd();
 	}
 
 };
--- a/src/lib/GenericASN1ContentHandler.h	Sun Jun 20 21:06:02 2021 +0200
+++ b/src/lib/GenericASN1ContentHandler.h	Mon Jun 21 21:36:01 2021 +0200
@@ -52,6 +52,7 @@
 	void writeCollectionStart(CollectionType type) override {
 		if (type == CollectionType::Sequence) handlers.writeStartElement("sequence");
 		else if (type == CollectionType::Set) handlers.writeStartElement("set");
+		else if (type == CollectionType::Constructed) handlers.writeStartElement("constructed");
 		else handlers.writeStartElement("unknown-collection"); // TODO: exception?
 	}