src/lib/GenericASN1ContentHandler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 22 Jul 2021 20:01:03 +0200
branchv_0
changeset 40 85b6f13f1088
parent 39 6ef41443211e
child 41 12acb6c02d32
permissions -rw-r--r--
configuration: --parser-option encoding, parse-encapsulated, root-name, tree-style, tree-with-namespaces

/**
 * Relational pipes
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include <sstream>
#include <iomanip>

#include "ASN1ContentHandler.h"
#include "XMLContentHandler.h"
#include "uri.h"

namespace relpipe {
namespace in {
namespace asn1 {
namespace lib {

/**
 * Converts ASN.1 events to XML SAX events.
 * 
 * Does not need to know anything about the model/schema – works with generic events (nodes).
 */
class GenericASN1ContentHandler : public ASN1ContentHandler {
private:
	XMLContentHandlerProxy handlers;

	std::string rootName = "asn1";
	bool treeWithNamespaces = false;

	/**
	 * TODO: use a common method
	 */
	bool parseBoolean(const std::string& value) {
		if (value == "true") return true;
		else if (value == "false") return false;
		else throw std::invalid_argument(std::string("Unable to parse boolean value: ") + value + " (expecting true or false)");
	}

	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;
	}

	/**
	 * @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:

	void addHandler(std::shared_ptr<XMLContentHandler> handler) {
		handlers.addHandler(handler);
	}

	virtual ~GenericASN1ContentHandler() {
	}

	bool setOption(const std::string& uri, const std::string& value) override {
		if (uri == xml::RootName) rootName = value;
		else if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value);
		else if (uri == xml::TreeStyle && value == "standard"); // the only style currently supported
		else if (uri == xml::TreeStyle && value == "literal") throw std::invalid_argument("Tree style 'literal' is not yet supported"); // will require ASN.1 schema, might be implemented in another class
		else if (uri == xml::TreeStyle) throw std::invalid_argument("Unsupported tree-style: " + value);
		else return false;

		return true;
	}

	void writeStreamStart() override {
		if (treeWithNamespaces) handlers.writeStartElement(rootName,{"xmlns", xml::XMLNS}); // TODO: actual namespace instead of a mere attribute
		else handlers.writeStartElement(rootName);
	}

	void writeStreamEnd() override {
		handlers.writeEndElement();
	}

	void writeCollectionStart(const Header& header) override {
		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 {
		handlers.writeEndElement();
	}

	void writeBoolean(const Header& header, bool value) override {
		handlers.writeStartElement("boolean", getCommonAttributes(header));
		handlers.writeCharacters(value ? "true" : "false");
		handlers.writeEndElement();
	}

	void writeNull(const Header& header) override {
		handlers.writeStartElement("null", getCommonAttributes(header));
		handlers.writeEndElement();
	}

	void writeInteger(const Header& header, Integer value) override {
		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", getCommonAttributes(header));
		handlers.writeCharacters(value);
		handlers.writeEndElement();
	}

	void writeOctetString(const Header& header, std::string value) override {
		std::vector<std::string> attributes = getCommonAttributes(header,{"length", std::to_string(value.size())});
		std::string text = getText(header, value);
		if (text.size()) {
			attributes.push_back("text");
			attributes.push_back(text);
		}

		std::stringstream hex;
		hex << std::hex << std::setfill('0');
		for (uint8_t b : value) hex << std::setw(2) << (int) b;
		handlers.writeStartElement("octet-string", attributes);
		handlers.writeCharacters(hex.str());
		handlers.writeEndElement();
	}

	void writeBitString(const Header& header, std::vector<bool> value) override {
		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", 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", 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", getCommonAttributes(header));
		handlers.writeCharacters(value.toString());
		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", getCommonAttributes(header,{"length", std::to_string(value.size()), "hex", hex.str()}));
		handlers.writeCharacters(ascii.str());
		handlers.writeEndElement();
	}


};

}
}
}
}