From f979b9c187256cd969df6f00882c37f91e39a612 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Tue, 31 May 2022 15:32:17 +0200 Subject: [PATCH] Add additional methods to data ingestion layer --- GCryptCLI/include/Configuration.h | 4 +-- GCryptCLI/include/DataIngestionLayer.h | 12 ++++++- GCryptCLI/src/Configuration.cpp | 8 ++--- GCryptCLI/src/DataIngestionLayer.cpp | 47 ++++++++++++++++++++------ 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/GCryptCLI/include/Configuration.h b/GCryptCLI/include/Configuration.h index 5851372..1b87f3c 100644 --- a/GCryptCLI/include/Configuration.h +++ b/GCryptCLI/include/Configuration.h @@ -31,8 +31,8 @@ class Configuration { static std::string outputFilename; static enum class MODULE { - ENCRYPT, - DECRYPT, + ENCRYPTION, + DECRYPTION, HASH, GENERATE_KEY } activeModule; diff --git a/GCryptCLI/include/DataIngestionLayer.h b/GCryptCLI/include/DataIngestionLayer.h index 9bd7238..53338b7 100644 --- a/GCryptCLI/include/DataIngestionLayer.h +++ b/GCryptCLI/include/DataIngestionLayer.h @@ -4,6 +4,7 @@ #include #include #include +#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! diff --git a/GCryptCLI/src/Configuration.cpp b/GCryptCLI/src/Configuration.cpp index c91c2f6..92c263b 100644 --- a/GCryptCLI/src/Configuration.cpp +++ b/GCryptCLI/src/Configuration.cpp @@ -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; diff --git a/GCryptCLI/src/DataIngestionLayer.cpp b/GCryptCLI/src/DataIngestionLayer.cpp index f4d6eec..39c585f 100644 --- a/GCryptCLI/src/DataIngestionLayer.cpp +++ b/GCryptCLI/src/DataIngestionLayer.cpp @@ -1,6 +1,6 @@ #include "DataIngestionLayer.h" #include "CommandlineInterface.h" -#include "Configuration.h" +#include "Bases.h" #include #include #include @@ -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 DataIngestionLayer::blocks;