src/FreeformASN1ContentHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Dec 2021 21:14:48 +0100
branchv_0
changeset 11 6282949e3672
parent 9 7d309accc639
permissions -rw-r--r--
Added tag v0.18 for changeset db8429c641c6

/**
 * 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 <relpipe/writer/RelationalWriter.h>
#include "lib/ASN1ContentHandler.h"
#include "Configuration.h"

namespace relpipe {
namespace in {
namespace asn1 {

namespace freeformOption {
static const char* BitStringSymbol0 = "bit-string-symbol-0";
static const char* BitStringSymbol1 = "bit-string-symbol-1";
}

/**
 * Converts arbitrary ASN.1 stream of events to a relation
 * i.e. does not require any specific structures/schema.
 *
 * Useful especially for testing and debugging and sometimes instead of relpipe-in-asn1table,
 * because this handler supports streaming and does not depend on any third-party library (no XML dependency).
 */
class FreeformASN1ContentHandler : public lib::ASN1ContentHandler {
private:
	wstring_convert < codecvt_utf8<wchar_t>> convertor; // ASN.1 parser works with UTF-8
	std::shared_ptr<relpipe::writer::RelationalWriter> writer;
	Configuration configuration;
	std::vector<relpipe::common::type::Integer> position;

	std::string bitStringSymbol0 = "0";
	std::string bitStringSymbol1 = "1";

	class Record {
	public:
		const Header* header;
		const std::vector<relpipe::common::type::Integer>& position;
		relpipe::common::type::Integer level = 0;
		relpipe::common::type::StringX event;
		relpipe::common::type::StringX valueText;
		relpipe::common::type::StringX valueBinary;

		Record(const Header* header, std::vector<relpipe::common::type::Integer>& position, relpipe::common::type::StringX event) : header(header), position(position), event(event) {
		}

		virtual ~Record() {
		}
	};

	void write(const Record& r) {
		relpipe::common::type::Integer tag = r.header ? r.header->tag : -1; // TODO: null + possible integer overflow
		relpipe::common::type::Integer id = r.position.back();
		relpipe::common::type::Integer parent = r.position[r.position.size() - 2];
		relpipe::common::type::Integer level = r.position.size() - 2;

		relpipe::common::type::StringX pc;
		relpipe::common::type::StringX tagClass;

		if (r.header) {
			if (r.header->pc == PC::Primitive) pc = L"primitive";
			else if (r.header->pc == PC::Constructed) pc = L"primitive";
			else throw std::logic_error("Unexpected PC: " + std::to_string((uint8_t) r.header->pc)); // should never happen

			if (r.header->tagClass == TagClass::Universal) tagClass = L"universal";
			else if (r.header->tagClass == TagClass::ContextSpecific) tagClass = L"context";
			else if (r.header->tagClass == TagClass::Application) tagClass = L"application";
			else if (r.header->tagClass == TagClass::Private) tagClass = L"private";
			else throw std::logic_error("Unexpected TagClass: " + std::to_string((uint8_t) r.header->tagClass)); // should never happen
		}

		writer->writeAttribute(&id, typeid (id));
		writer->writeAttribute(&parent, typeid (parent));
		writer->writeAttribute(&level, typeid (level));
		writer->writeAttribute(&r.event, typeid (r.event));
		// writer->writeAttribute(&pc, typeid (pc));
		writer->writeAttribute(&tagClass, typeid (tagClass));
		writer->writeAttribute(&tag, typeid (tag));
		writer->writeAttribute(&r.valueText, typeid (r.valueText));
		writer->writeAttribute(&r.valueBinary, typeid (r.valueBinary));
	}

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

	FreeformASN1ContentHandler(std::shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
	}

	bool setOption(const std::string& uri, const std::string& value) override {
		if (uri == freeformOption::BitStringSymbol0) bitStringSymbol0 = value;
		else if (uri == freeformOption::BitStringSymbol1) bitStringSymbol1 = value;
		else return false;

		return true;
	}

	void writeStreamStart() override {
		writer->startRelation(configuration.relation,{
			{L"id", relpipe::writer::TypeId::INTEGER},
			{L"parent", relpipe::writer::TypeId::INTEGER},
			{L"level", relpipe::writer::TypeId::INTEGER},
			{L"event", relpipe::writer::TypeId::STRING},
			// {L"pc", relpipe::writer::TypeId::STRING},
			{L"tag_class", relpipe::writer::TypeId::STRING},
			{L"tag", relpipe::writer::TypeId::INTEGER},
			{L"value_text", relpipe::writer::TypeId::STRING},
			{L"value_binary", relpipe::writer::TypeId::STRING}, // TODO: OctetString data type (when available) instead of text
		}, true);

		position.push_back(-1); // TODO: null
		position.push_back(1);
		Record r(nullptr, position, L"stream-start");
		write(r);
		position.push_back(position.back());
	}

	void writeStreamEnd() override {
		Record r(nullptr, position, L"stream-end");
		write(r);

		auto id = position.back() + 1;
		position.pop_back();
		position.back() = id;
	}

	void writeCollectionStart(const Header& header) override {
		position.back()++;
		Record r(&header, position, L"collection-start");
		write(r);
		position.push_back(position.back());
	}

	void writeCollectionEnd() override {
		Record r(nullptr, position, L"collection-end");
		write(r);

		auto id = position.back() + 1;
		position.pop_back();
		position.back() = id;
	}

	void writeBitString(const Header& header, std::vector<bool> value) override {
		std::stringstream bits;
		for (bool b : value) bits << (b ? bitStringSymbol1 : bitStringSymbol0);

		position.back()++;
		Record r(&header, position, L"bit-string");
		r.valueText = convertor.from_bytes(bits.str());
		write(r);
	}

	void writeBoolean(const Header& header, bool value) override {
		position.back()++;
		Record r(&header, position, L"boolean");
		r.valueText = value ? L"true" : L"false";
		r.valueBinary = value ? L"FF" : L"00";
		write(r);
	}

	void writeDateTime(const Header& header, DateTime value) override {
		position.back()++;
		Record r(&header, position, L"date-time");
		r.valueText = convertor.from_bytes(value.toString());
		write(r);
	}

	void writeInteger(const Header& header, Integer value) override {
		position.back()++;
		Record r(&header, position, L"integer");
		r.valueText = convertor.from_bytes(value.toString());
		r.valueBinary = convertor.from_bytes(value.toHex());
		write(r);
	}

	void writeNull(const Header& header) override {
		position.back()++;
		Record r(&header, position, L"null");
		r.valueText = L"null";
		write(r);
	}

	void writeOID(const Header& header, ObjectIdentifier value) override {
		position.back()++;
		Record r(&header, position, L"oid");
		r.valueText = convertor.from_bytes(value.toString());
		write(r);
	}

	void writeOctetString(const Header& header, std::string value) override {
		std::string text = getText(header, value);
		std::stringstream hex;
		hex << std::hex << std::setfill('0');
		for (uint8_t b : value) hex << std::setw(2) << (int) b;

		position.back()++;
		Record r(&header, position, L"octet-string");
		r.valueText = convertor.from_bytes(text);
		r.valueBinary = convertor.from_bytes(hex.str());
		write(r);
	}

	void writeTextString(const Header& header, std::string value) override {
		position.back()++;
		Record r(&header, position, L"text-string");
		r.valueText = convertor.from_bytes(value);
		write(r);
	}

	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?

		position.back()++;
		Record r(&header, position, L"specific");
		r.valueText = convertor.from_bytes(ascii.str());
		r.valueBinary = convertor.from_bytes(hex.str());
		write(r);
	}



};

}
}
}