src/lib/GenericASN1ContentHandler.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 02 Jul 2021 00:42:01 +0200
branchv_0
changeset 23 8941a679299f
parent 21 705036445672
child 26 e39de9b8b3a1
permissions -rw-r--r--
BitString and OctetString support

/**
 * 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 <sstream>
#include <iomanip>

#include "ASN1ContentHandler.h"
#include "XMLContentHandler.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;
public:

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

	virtual ~GenericASN1ContentHandler() {
	}

	void writeStreamStart() override {
		handlers.writeStartElement("asn1");
	}

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

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

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

	void writeBoolean(bool value) override {
		handlers.writeStartElement("boolean");
		handlers.writeCharacters(value ? "true" : "false");
		handlers.writeEndElement();
	}

	void writeNull() override {
		handlers.writeStartElement("null");
		handlers.writeEndElement();
	}

	void writeInteger(Integer value) override {
		handlers.writeStartElement("integer",{"hex", value.toHex()});
		handlers.writeCharacters(value.toString());
		handlers.writeEndElement();
	}

	void writeTextString(StringType type, std::string value) override {
		handlers.writeStartElement("text-string",{"type", std::to_string((uint64_t) type)}); // TODO: type name, better attributes
		handlers.writeCharacters(value);
		handlers.writeEndElement();
	}

	void writeOctetString(std::string value) override {
		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.writeCharacters(hex.str());
		handlers.writeEndElement();
	}

	void writeBitString(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",{"length", std::to_string(value.size())});
		handlers.writeCharacters(bits.str());
		handlers.writeEndElement();
	}

	void writeOID(ObjectIdentifier value) override {
		// TODO: optionally expand into separate elements with additional metadata
		handlers.writeStartElement("oid");
		handlers.writeCharacters(value.toString());
		handlers.writeEndElement();
	}

	void writeDateTime(DateTimeType type, DateTime value) override {
		handlers.writeStartElement("date-time");
		handlers.writeCharacters(value.toString());
		handlers.writeEndElement();
	}

};

}
}
}
}