From 0ab87de23cf6bffd0b4fddea2030f6b04dcacdb3 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Tue, 31 May 2022 17:44:30 +0200 Subject: [PATCH] More streamlined handling of io bases --- GCryptCLI/include/Configuration.h | 10 +++- GCryptCLI/include/DataIngestionLayer.h | 3 - GCryptCLI/include/DataOutputLayer.h | 3 - GCryptCLI/src/Configuration.cpp | 79 ++++++++++++++++---------- GCryptCLI/src/DataIngestionLayer.cpp | 11 ---- GCryptCLI/src/DataOutputLayer.cpp | 17 +----- 6 files changed, 59 insertions(+), 64 deletions(-) diff --git a/GCryptCLI/include/Configuration.h b/GCryptCLI/include/Configuration.h index 1b87f3c..6af4f3e 100644 --- a/GCryptCLI/include/Configuration.h +++ b/GCryptCLI/include/Configuration.h @@ -25,7 +25,9 @@ class Configuration { BASE_64, BASE_UWU, BASE_UGH - } iobaseFormat; + } + formatIn, + formatOut; static std::string inputFilename; static std::string outputFilename; @@ -44,9 +46,13 @@ class Configuration { private: static void DecideInputFrom(); static void DecideOutputTo(); - static void DecideIOBaseFormat(); + static void DecideCiphertextFormat(); + static void MapCiphertextFormatToIOBases(); static void DecideModule(); + // This is just an intermediary value, used between methods + static IOBASE_FORMAT ciphertextFormat; + // No instanciation! >:( Configuration() {}; }; diff --git a/GCryptCLI/include/DataIngestionLayer.h b/GCryptCLI/include/DataIngestionLayer.h index 9b77032..d0510a7 100644 --- a/GCryptCLI/include/DataIngestionLayer.h +++ b/GCryptCLI/include/DataIngestionLayer.h @@ -38,9 +38,6 @@ namespace IO { 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/include/DataOutputLayer.h b/GCryptCLI/include/DataOutputLayer.h index 42fbd5a..4e6e6c8 100644 --- a/GCryptCLI/include/DataOutputLayer.h +++ b/GCryptCLI/include/DataOutputLayer.h @@ -34,9 +34,6 @@ namespace IO { private: static std::ostream* out; - // The format to write data in - static Configuration::IOBASE_FORMAT outFormat; - // 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 92c263b..d0a6ee4 100644 --- a/GCryptCLI/src/Configuration.cpp +++ b/GCryptCLI/src/Configuration.cpp @@ -5,7 +5,8 @@ void Configuration::Parse() { DecideModule(); DecideInputFrom(); DecideOutputTo(); - DecideIOBaseFormat(); + DecideCiphertextFormat(); + MapCiphertextFormatToIOBases(); return; } @@ -62,46 +63,38 @@ void Configuration::DecideOutputTo() { return; } -void Configuration::DecideIOBaseFormat() { +void Configuration::DecideCiphertextFormat() { // Do we have any iobase explicitly specified? if (CommandlineInterface::Get().HasParam("--iobase-bytes")) { - iobaseFormat = IOBASE_FORMAT::BASE_BYTES; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_BYTES; } else if (CommandlineInterface::Get().HasParam("--iobase-2")) { - iobaseFormat = IOBASE_FORMAT::BASE_2; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_2; } else if (CommandlineInterface::Get().HasParam("--iobase-8")) { - iobaseFormat = IOBASE_FORMAT::BASE_8; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_8; } else if (CommandlineInterface::Get().HasParam("--iobase-10")) { - iobaseFormat = IOBASE_FORMAT::BASE_10; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_10; } else if (CommandlineInterface::Get().HasParam("--iobase-16")) { - iobaseFormat = IOBASE_FORMAT::BASE_16; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_16; } else if (CommandlineInterface::Get().HasParam("--iobase-64")) { - iobaseFormat = IOBASE_FORMAT::BASE_64; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_64; } else if (CommandlineInterface::Get().HasParam("--iobase-uwu")) { - iobaseFormat = IOBASE_FORMAT::BASE_UWU; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_UWU; } else if (CommandlineInterface::Get().HasParam("--iobase-ugh")) { - iobaseFormat = IOBASE_FORMAT::BASE_UGH; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_UGH; } // So we have no iobase explicitly specified.. Let's default.. // If we are encrypting or hashing, - if ( + else if ( (activeModule == MODULE::ENCRYPTION) || (activeModule == MODULE::HASH) ) { @@ -112,13 +105,12 @@ void Configuration::DecideIOBaseFormat() { (inputFrom == INPUT_FROM::PARAMETER) && (outputTo == OUTPUT_TO::STDOUT) ) { - iobaseFormat = IOBASE_FORMAT::BASE_16; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_16; } // Any other case whilst encrypting, we'll assume base-bytes. else { - iobaseFormat = IOBASE_FORMAT::BASE_BYTES; + ciphertextFormat = IOBASE_FORMAT::BASE_BYTES; return; } @@ -128,13 +120,11 @@ void Configuration::DecideIOBaseFormat() { 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; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_16; } // Any other case whilst decrypting, we'll assume base-bytes. else { - iobaseFormat = IOBASE_FORMAT::BASE_BYTES; - return; + ciphertextFormat = IOBASE_FORMAT::BASE_BYTES; } } @@ -142,16 +132,41 @@ void Configuration::DecideIOBaseFormat() { else if (activeModule == MODULE::GENERATE_KEY) { // and we're outputting to stdout, we'll use base-16. if (outputTo == OUTPUT_TO::STDOUT) { - iobaseFormat = IOBASE_FORMAT::BASE_16; + ciphertextFormat = IOBASE_FORMAT::BASE_16; } // else, we're outputting to a file, use base-bytes. - iobaseFormat = IOBASE_FORMAT::BASE_BYTES; - return; + else { + ciphertextFormat = IOBASE_FORMAT::BASE_BYTES; + } } // Fallback: Bytes else { - iobaseFormat = IOBASE_FORMAT::BASE_BYTES; + ciphertextFormat = IOBASE_FORMAT::BASE_BYTES; + } + + return; +} + +void Configuration::MapCiphertextFormatToIOBases() { + + // Now, map the ciphertextFormat to either formatIn or formatOut. + switch (activeModule) { + // For encryption, keygen, and hashing: + // input is bytes and output is ciphertext + case MODULE::ENCRYPTION: + case MODULE::HASH: + case MODULE::GENERATE_KEY: + formatIn = IOBASE_FORMAT::BASE_BYTES; + formatOut = ciphertextFormat; + break; + + // For decryption: + // input is ciphertext and output is bytes + case MODULE::DECRYPTION: + formatIn = ciphertextFormat; + formatOut = IOBASE_FORMAT::BASE_BYTES; + break; } return; @@ -160,7 +175,9 @@ void Configuration::DecideIOBaseFormat() { std::string Configuration::inputFilename; std::string Configuration::outputFilename; Configuration::MODULE Configuration::activeModule; -Configuration::IOBASE_FORMAT Configuration::iobaseFormat; +Configuration::IOBASE_FORMAT Configuration::formatIn; +Configuration::IOBASE_FORMAT Configuration::formatOut; +Configuration::IOBASE_FORMAT Configuration::ciphertextFormat; Configuration::INPUT_FROM Configuration::inputFrom; Configuration::OUTPUT_TO Configuration::outputTo; diff --git a/GCryptCLI/src/DataIngestionLayer.cpp b/GCryptCLI/src/DataIngestionLayer.cpp index b735ea1..af53406 100644 --- a/GCryptCLI/src/DataIngestionLayer.cpp +++ b/GCryptCLI/src/DataIngestionLayer.cpp @@ -53,16 +53,6 @@ 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; @@ -152,6 +142,5 @@ 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; diff --git a/GCryptCLI/src/DataOutputLayer.cpp b/GCryptCLI/src/DataOutputLayer.cpp index e3e40b4..4760ea1 100644 --- a/GCryptCLI/src/DataOutputLayer.cpp +++ b/GCryptCLI/src/DataOutputLayer.cpp @@ -38,16 +38,6 @@ void DataOutputLayer::Init() { break; } - // Determine which iobase format to write in - // If we are decrypting, input is not formatted - if (Configuration::activeModule == Configuration::MODULE::DECRYPTION) { - outFormat = Configuration::IOBASE_FORMAT::BASE_BYTES; - } - // If we are doing anything else, output is the requested format - else { - outFormat = Configuration::iobaseFormat; - } - initialized = true; reachedEof = false; @@ -94,7 +84,7 @@ void DataOutputLayer::WriteBlock() { const std::string formattedBlock = DataFormatter::FormatBlock( block, - outFormat + Configuration::formatOut ); // Dump it @@ -105,8 +95,8 @@ void DataOutputLayer::WriteBlock() { if ( (!IsFinished()) && ( - (outFormat == Configuration::IOBASE_FORMAT::BASE_UWU) || - (outFormat == Configuration::IOBASE_FORMAT::BASE_UGH) + (Configuration::formatOut == Configuration::IOBASE_FORMAT::BASE_UWU) || + (Configuration::formatOut == Configuration::IOBASE_FORMAT::BASE_UGH) ) ) { *out << " "; @@ -130,6 +120,5 @@ std::ostream* DataOutputLayer::out; std::ofstream DataOutputLayer::ofs; bool DataOutputLayer::reachedEof = false; bool DataOutputLayer::initialized = false; -Configuration::IOBASE_FORMAT DataOutputLayer::outFormat; std::queue DataOutputLayer::blocks;