switch TransactionalBuffer from char to uint8_t + partial parser of tags and lengths
/**
* 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 <memory>
#include <vector>
#include <array>
#include "ASN1Reader.h"
namespace relpipe {
namespace in {
namespace asn1 {
namespace lib {
/**
* Reads ASN.1 data encoded as BER (DER, CER).
*/
class BasicASN1Reader : public ASN1Reader {
private:
bool started = false;
enum class TagClass : uint8_t {
Universal = 0,
Application = 1,
ContextSpecific = 2,
Private = 3
};
enum class PC : uint8_t {
Primitive = 0,
Constructed = 1
};
class Header {
public:
TagClass tagClass;
PC pc;
uint64_t tag;
bool definiteLength;
size_t length;
};
Header readHeader() {
Header h;
memset(&h, 0, sizeof (h)); // TODO: remove, not needed
uint8_t tagByte;
read(&tagByte, 1);
h.tagClass = (TagClass) (tagByte >> 6);
h.pc = (PC) ((tagByte >> 5) & 1);
h.tag = tagByte & (0xFF >> 3);
if (h.tag > 30) throw relpipe::writer::RelpipeWriterException(L"not yet implemented, ASN.1 tag > 30"); // FIXME: higher tag values → read more bytes
uint8_t lengthByte;
read(&lengthByte, 1);
std::wcerr << L"lengthByte = " << lengthByte << std::endl;
if (lengthByte >> 7 == 0) {
h.definiteLength = true;
h.length = lengthByte;
} else if (lengthByte == 0b10000000) {
h.definiteLength = false;
h.length = 0;
} else if (lengthByte == 0xFF) {
throw relpipe::writer::RelpipeWriterException(L"ASN.1 lengthByte == 0xFF (reserved value)"); // TODO: better exception
} else {
// FIXME: longer values
throw relpipe::writer::RelpipeWriterException(L"not yet implemented, ASN.1 lengthBytes: longer value"); // TODO: better exception
}
return h;
}
void readNext() {
Header typeHeader = readHeader();
commit();
if (!started) {
handlers.writeStreamStart();
started = true;
}
handlers.writeCollectionStart(ASN1ContentHandler::CollectionType::Sequence);
handlers.writeNull();
handlers.writeBoolean(true);
handlers.writeString(ASN1ContentHandler::StringType::UTF8String, "tagClass:");
handlers.writeInteger((int64_t) typeHeader.tagClass);
handlers.writeString(ASN1ContentHandler::StringType::UTF8String, "pc:");
handlers.writeInteger((int64_t) typeHeader.pc);
handlers.writeString(ASN1ContentHandler::StringType::UTF8String, "tag:");
handlers.writeInteger((int64_t) typeHeader.tag);
handlers.writeString(ASN1ContentHandler::StringType::UTF8String, "definiteLength:");
handlers.writeBoolean(typeHeader.definiteLength);
handlers.writeString(ASN1ContentHandler::StringType::UTF8String, "length:");
handlers.writeInteger((int64_t) typeHeader.length);
handlers.writeString(ASN1ContentHandler::StringType::UTF8String, "relational pipes");
handlers.writeCollectionEnd();
}
protected:
void update() override {
while (true) readNext();
}
public:
void close() override {
if (started) handlers.writeStreamEnd();
}
};
}
}
}
}