/**
* 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"
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::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;
}
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(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::stringstream hex;
hex << std::hex << std::setfill('0');
for (uint8_t b : value) hex << std::setw(2) << (int) b;
handlers.writeStartElement("octet-string", getCommonAttributes(header,{"length", std::to_string(value.size())}));
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();
}
};
}
}
}
}