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 std::string outputFilename;
static enum class MODULE { static enum class MODULE {
ENCRYPT, ENCRYPTION,
DECRYPT, DECRYPTION,
HASH, HASH,
GENERATE_KEY GENERATE_KEY
} activeModule; } activeModule;

View File

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

View File

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

View File

@ -1,6 +1,6 @@
#include "DataIngestionLayer.h" #include "DataIngestionLayer.h"
#include "CommandlineInterface.h" #include "CommandlineInterface.h"
#include "Configuration.h" #include "Bases.h"
#include <iostream> #include <iostream>
#include <istream> #include <istream>
#include <fstream> #include <fstream>
@ -53,20 +53,27 @@ void DataIngestionLayer::Init() {
break; 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; initialized = true;
reachedEof = false; reachedEof = false;
// Temporary return;
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;
} while(n_last_bytes_read == 64); void DataIngestionLayer::Destruct() {
if (Configuration::inputFrom == Configuration::INPUT_FROM::FILE) {
ifs.close();
}
return; return;
} }
@ -77,6 +84,8 @@ void DataIngestionLayer::ReadBlock() {
} }
if (!reachedEof) { if (!reachedEof) {
// This should really account for iobase recoding!
// Create buffer to read into // Create buffer to read into
char buf[Block::BLOCK_SIZE]; char buf[Block::BLOCK_SIZE];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
@ -95,7 +104,7 @@ void DataIngestionLayer::ReadBlock() {
// Construct a Block from this buf // Construct a Block from this buf
Block block; Block block;
block.FromByteString(std::string(buf)); block.FromByteString(std::string(buf, sizeof(buf)));
// Enqueue it // Enqueue it
blocks.emplace(block); blocks.emplace(block);
@ -109,9 +118,24 @@ bool DataIngestionLayer::ReachedEOF() {
} }
bool DataIngestionLayer::IsBlockReady() { 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; return blocks.size() > 0;
} }
bool DataIngestionLayer::IsFinished() {
return (reachedEof) && (blocks.size() == 0);
}
Block DataIngestionLayer::GetNextBlock() { Block DataIngestionLayer::GetNextBlock() {
if (!IsBlockReady()) { if (!IsBlockReady()) {
throw std::runtime_error("Attempted to get the next block, but there are none left!"); 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; std::istringstream DataIngestionLayer::iss;
bool DataIngestionLayer::reachedEof = false; bool DataIngestionLayer::reachedEof = false;
bool DataIngestionLayer::initialized = false; bool DataIngestionLayer::initialized = false;
Configuration::IOBASE_FORMAT DataIngestionLayer::inFormat;
std::queue<Block> DataIngestionLayer::blocks; std::queue<Block> DataIngestionLayer::blocks;