/**
* 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 {
class FreeformASN1ContentHandler : public lib::ASN1ContentHandler {
private:
std::shared_ptr<relpipe::writer::RelationalWriter> writer;
Configuration configuration;
std::vector<relpipe::common::type::Integer> position;
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;
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));
}
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 {
return false;
}
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},
}, 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 {
auto id = position.back() + 1;
position.pop_back();
position.back() = id;
Record r(nullptr, position, L"stream-end");
write(r);
}
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 {
auto id = position.back() + 1;
position.pop_back();
position.back() = id;
Record r(nullptr, position, L"collection-end");
write(r);
}
void writeBitString(const Header& header, std::vector<bool> value) override {
position.back()++;
Record r(&header, position, L"bit-string");
write(r);
}
void writeBoolean(const Header& header, bool value) override {
position.back()++;
Record r(&header, position, L"boolean");
write(r);
}
void writeDateTime(const Header& header, DateTime value) override {
position.back()++;
Record r(&header, position, L"date-time");
write(r);
}
void writeInteger(const Header& header, Integer value) override {
position.back()++;
Record r(&header, position, L"integer");
write(r);
}
void writeNull(const Header& header) override {
position.back()++;
Record r(&header, position, L"null");
write(r);
}
void writeOID(const Header& header, ObjectIdentifier value) override {
position.back()++;
Record r(&header, position, L"oid");
write(r);
}
void writeOctetString(const Header& header, std::string value) override {
position.back()++;
Record r(&header, position, L"octet-string");
write(r);
}
void writeTextString(const Header& header, std::string value) override {
position.back()++;
Record r(&header, position, L"text-string");
write(r);
}
void writeSpecific(const Header& header, std::string value) override {
position.back()++;
Record r(&header, position, L"specific");
write(r);
}
};
}
}
}