Add additional methods to data ingestion layer

This commit is contained in:
Leonetienne 2022-05-31 15:32:17 +02:00
parent 6a14b7b428
commit f979b9c187
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
4 changed files with 53 additions and 18 deletions

View File

@ -31,8 +31,8 @@ class Configuration {
static std::string outputFilename;
static enum class MODULE {
ENCRYPT,
DECRYPT,
ENCRYPTION,
DECRYPTION,
HASH,
GENERATE_KEY
} activeModule;

View File

@ -4,6 +4,7 @@
#include <iosfwd>
#include <queue>
#include <GCrypt/Block.h>
#include "Configuration.h"
using namespace Leonetienne::GCrypt;
@ -15,6 +16,9 @@ namespace IO {
//! Will initialize the ingestion
static void Init();
//! Will destruct the ingestion layer (like, closing file handles)
static void Destruct();
//! Will attempt to read a data block.
//! Requires Init() to have been called
static void ReadBlock();
@ -22,15 +26,21 @@ namespace IO {
//! Have we read in all available blocks?
static bool ReachedEOF();
//! Will return true if there is at least one block to be GetBlock()'ed
//! Returns true if there are blocks to be fetched (GetNextBlock())
static bool IsBlockReady();
//! Will return the next block in the queue
static Block GetNextBlock();
//! Returns true, if EOF is reached, and there are no more blocks to fetch (GetNextBlock())
static bool IsFinished();
private:
static std::istream* in;
// The format to read data in
static Configuration::IOBASE_FORMAT inFormat;
// We have to hold on to a reference to a filestream,
// even if we're always just reading from in.
// We still have to CLOSE the file handle afterwards!

View File

@ -12,11 +12,11 @@ void Configuration::Parse() {
void Configuration::DecideModule() {
if (CommandlineInterface::Get().HasParam("--encrypt")) {
activeModule = MODULE::ENCRYPT;
activeModule = MODULE::ENCRYPTION;
return;
}
else if (CommandlineInterface::Get().HasParam("--decrypt")) {
activeModule = MODULE::DECRYPT;
activeModule = MODULE::DECRYPTION;
return;
}
else if (CommandlineInterface::Get().HasParam("--hash")) {
@ -102,7 +102,7 @@ void Configuration::DecideIOBaseFormat() {
// If we are encrypting or hashing,
if (
(activeModule == MODULE::ENCRYPT) ||
(activeModule == MODULE::ENCRYPTION) ||
(activeModule == MODULE::HASH)
) {
// and input comes from a parameter,
@ -125,7 +125,7 @@ void Configuration::DecideIOBaseFormat() {
}
// Else, if we are decrypting,
else if (activeModule == MODULE::DECRYPT) {
else if (activeModule == MODULE::DECRYPTION) {
// and input comes from a parameter, we'll assume base-16.
if (inputFrom == INPUT_FROM::PARAMETER) {
iobaseFormat = IOBASE_FORMAT::BASE_16;

View File

@ -1,6 +1,6 @@
#include "DataIngestionLayer.h"
#include "CommandlineInterface.h"
#include "Configuration.h"
#include "Bases.h"
#include <iostream>
#include <istream>
#include <fstream>
@ -53,20 +53,27 @@ void DataIngestionLayer::Init() {
break;
}
// Determine which iobase format to read in
// If we are decrypting, input is formatted.
if (Configuration::activeModule == Configuration::MODULE::DECRYPTION) {
inFormat = Configuration::iobaseFormat;
}
// If we are doing anything else, input is raw bytes.
else {
inFormat = Configuration::IOBASE_FORMAT::BASE_BYTES;
}
initialized = true;
reachedEof = false;
// Temporary
std::size_t n_last_bytes_read = 0;
char buf[64];
do {
memset(buf, 0, sizeof(buf));
in->read(buf, sizeof(buf));
n_last_bytes_read = in->gcount();
std::cout << buf;
return;
}
} while(n_last_bytes_read == 64);
void DataIngestionLayer::Destruct() {
if (Configuration::inputFrom == Configuration::INPUT_FROM::FILE) {
ifs.close();
}
return;
}
@ -77,6 +84,8 @@ void DataIngestionLayer::ReadBlock() {
}
if (!reachedEof) {
// This should really account for iobase recoding!
// Create buffer to read into
char buf[Block::BLOCK_SIZE];
memset(buf, 0, sizeof(buf));
@ -95,7 +104,7 @@ void DataIngestionLayer::ReadBlock() {
// Construct a Block from this buf
Block block;
block.FromByteString(std::string(buf));
block.FromByteString(std::string(buf, sizeof(buf)));
// Enqueue it
blocks.emplace(block);
@ -109,9 +118,24 @@ bool DataIngestionLayer::ReachedEOF() {
}
bool DataIngestionLayer::IsBlockReady() {
// We're not ready, if we haven't reached EOF, if we should puffer
// the input.
if (
(CommandlineInterface::Get().HasParam("--puffer-input")) &&
(!reachedEof)
) {
return false;
}
// If we're not puffering, just return whether or not
// we have any blocks...
return blocks.size() > 0;
}
bool DataIngestionLayer::IsFinished() {
return (reachedEof) && (blocks.size() == 0);
}
Block DataIngestionLayer::GetNextBlock() {
if (!IsBlockReady()) {
throw std::runtime_error("Attempted to get the next block, but there are none left!");
@ -128,5 +152,6 @@ std::ifstream DataIngestionLayer::ifs;
std::istringstream DataIngestionLayer::iss;
bool DataIngestionLayer::reachedEof = false;
bool DataIngestionLayer::initialized = false;
Configuration::IOBASE_FORMAT DataIngestionLayer::inFormat;
std::queue<Block> DataIngestionLayer::blocks;