More streamlined handling of io bases

This commit is contained in:
Leonetienne 2022-05-31 17:44:30 +02:00
parent fd75678ea3
commit 0ab87de23c
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
6 changed files with 59 additions and 64 deletions

View File

@ -25,7 +25,9 @@ class Configuration {
BASE_64, BASE_64,
BASE_UWU, BASE_UWU,
BASE_UGH BASE_UGH
} iobaseFormat; }
formatIn,
formatOut;
static std::string inputFilename; static std::string inputFilename;
static std::string outputFilename; static std::string outputFilename;
@ -44,9 +46,13 @@ class Configuration {
private: private:
static void DecideInputFrom(); static void DecideInputFrom();
static void DecideOutputTo(); static void DecideOutputTo();
static void DecideIOBaseFormat(); static void DecideCiphertextFormat();
static void MapCiphertextFormatToIOBases();
static void DecideModule(); static void DecideModule();
// This is just an intermediary value, used between methods
static IOBASE_FORMAT ciphertextFormat;
// No instanciation! >:( // No instanciation! >:(
Configuration() {}; Configuration() {};
}; };

View File

@ -38,9 +38,6 @@ namespace IO {
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

@ -34,9 +34,6 @@ namespace IO {
private: private:
static std::ostream* out; 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, // 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

@ -5,7 +5,8 @@ void Configuration::Parse() {
DecideModule(); DecideModule();
DecideInputFrom(); DecideInputFrom();
DecideOutputTo(); DecideOutputTo();
DecideIOBaseFormat(); DecideCiphertextFormat();
MapCiphertextFormatToIOBases();
return; return;
} }
@ -62,46 +63,38 @@ void Configuration::DecideOutputTo() {
return; return;
} }
void Configuration::DecideIOBaseFormat() { void Configuration::DecideCiphertextFormat() {
// Do we have any iobase explicitly specified? // Do we have any iobase explicitly specified?
if (CommandlineInterface::Get().HasParam("--iobase-bytes")) { if (CommandlineInterface::Get().HasParam("--iobase-bytes")) {
iobaseFormat = IOBASE_FORMAT::BASE_BYTES; ciphertextFormat = IOBASE_FORMAT::BASE_BYTES;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-2")) { else if (CommandlineInterface::Get().HasParam("--iobase-2")) {
iobaseFormat = IOBASE_FORMAT::BASE_2; ciphertextFormat = IOBASE_FORMAT::BASE_2;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-8")) { else if (CommandlineInterface::Get().HasParam("--iobase-8")) {
iobaseFormat = IOBASE_FORMAT::BASE_8; ciphertextFormat = IOBASE_FORMAT::BASE_8;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-10")) { else if (CommandlineInterface::Get().HasParam("--iobase-10")) {
iobaseFormat = IOBASE_FORMAT::BASE_10; ciphertextFormat = IOBASE_FORMAT::BASE_10;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-16")) { else if (CommandlineInterface::Get().HasParam("--iobase-16")) {
iobaseFormat = IOBASE_FORMAT::BASE_16; ciphertextFormat = IOBASE_FORMAT::BASE_16;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-64")) { else if (CommandlineInterface::Get().HasParam("--iobase-64")) {
iobaseFormat = IOBASE_FORMAT::BASE_64; ciphertextFormat = IOBASE_FORMAT::BASE_64;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-uwu")) { else if (CommandlineInterface::Get().HasParam("--iobase-uwu")) {
iobaseFormat = IOBASE_FORMAT::BASE_UWU; ciphertextFormat = IOBASE_FORMAT::BASE_UWU;
return;
} }
else if (CommandlineInterface::Get().HasParam("--iobase-ugh")) { else if (CommandlineInterface::Get().HasParam("--iobase-ugh")) {
iobaseFormat = IOBASE_FORMAT::BASE_UGH; ciphertextFormat = IOBASE_FORMAT::BASE_UGH;
return;
} }
// So we have no iobase explicitly specified.. Let's default.. // So we have no iobase explicitly specified.. Let's default..
// If we are encrypting or hashing, // If we are encrypting or hashing,
if ( else if (
(activeModule == MODULE::ENCRYPTION) || (activeModule == MODULE::ENCRYPTION) ||
(activeModule == MODULE::HASH) (activeModule == MODULE::HASH)
) { ) {
@ -112,13 +105,12 @@ void Configuration::DecideIOBaseFormat() {
(inputFrom == INPUT_FROM::PARAMETER) && (inputFrom == INPUT_FROM::PARAMETER) &&
(outputTo == OUTPUT_TO::STDOUT) (outputTo == OUTPUT_TO::STDOUT)
) { ) {
iobaseFormat = IOBASE_FORMAT::BASE_16; ciphertextFormat = IOBASE_FORMAT::BASE_16;
return;
} }
// Any other case whilst encrypting, we'll assume base-bytes. // Any other case whilst encrypting, we'll assume base-bytes.
else { else {
iobaseFormat = IOBASE_FORMAT::BASE_BYTES; ciphertextFormat = IOBASE_FORMAT::BASE_BYTES;
return; return;
} }
@ -128,13 +120,11 @@ void Configuration::DecideIOBaseFormat() {
else if (activeModule == MODULE::DECRYPTION) { 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; ciphertextFormat = IOBASE_FORMAT::BASE_16;
return;
} }
// Any other case whilst decrypting, we'll assume base-bytes. // Any other case whilst decrypting, we'll assume base-bytes.
else { else {
iobaseFormat = IOBASE_FORMAT::BASE_BYTES; ciphertextFormat = IOBASE_FORMAT::BASE_BYTES;
return;
} }
} }
@ -142,16 +132,41 @@ void Configuration::DecideIOBaseFormat() {
else if (activeModule == MODULE::GENERATE_KEY) { else if (activeModule == MODULE::GENERATE_KEY) {
// and we're outputting to stdout, we'll use base-16. // and we're outputting to stdout, we'll use base-16.
if (outputTo == OUTPUT_TO::STDOUT) { 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. // else, we're outputting to a file, use base-bytes.
iobaseFormat = IOBASE_FORMAT::BASE_BYTES; else {
return; ciphertextFormat = IOBASE_FORMAT::BASE_BYTES;
}
} }
// Fallback: Bytes // Fallback: Bytes
else { 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; return;
@ -160,7 +175,9 @@ void Configuration::DecideIOBaseFormat() {
std::string Configuration::inputFilename; std::string Configuration::inputFilename;
std::string Configuration::outputFilename; std::string Configuration::outputFilename;
Configuration::MODULE Configuration::activeModule; 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::INPUT_FROM Configuration::inputFrom;
Configuration::OUTPUT_TO Configuration::outputTo; Configuration::OUTPUT_TO Configuration::outputTo;

View File

@ -53,16 +53,6 @@ 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;
@ -152,6 +142,5 @@ 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;

View File

@ -38,16 +38,6 @@ void DataOutputLayer::Init() {
break; 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; initialized = true;
reachedEof = false; reachedEof = false;
@ -94,7 +84,7 @@ void DataOutputLayer::WriteBlock() {
const std::string formattedBlock = const std::string formattedBlock =
DataFormatter::FormatBlock( DataFormatter::FormatBlock(
block, block,
outFormat Configuration::formatOut
); );
// Dump it // Dump it
@ -105,8 +95,8 @@ void DataOutputLayer::WriteBlock() {
if ( if (
(!IsFinished()) && (!IsFinished()) &&
( (
(outFormat == Configuration::IOBASE_FORMAT::BASE_UWU) || (Configuration::formatOut == Configuration::IOBASE_FORMAT::BASE_UWU) ||
(outFormat == Configuration::IOBASE_FORMAT::BASE_UGH) (Configuration::formatOut == Configuration::IOBASE_FORMAT::BASE_UGH)
) )
) { ) {
*out << " "; *out << " ";
@ -130,6 +120,5 @@ std::ostream* DataOutputLayer::out;
std::ofstream DataOutputLayer::ofs; std::ofstream DataOutputLayer::ofs;
bool DataOutputLayer::reachedEof = false; bool DataOutputLayer::reachedEof = false;
bool DataOutputLayer::initialized = false; bool DataOutputLayer::initialized = false;
Configuration::IOBASE_FORMAT DataOutputLayer::outFormat;
std::queue<Block> DataOutputLayer::blocks; std::queue<Block> DataOutputLayer::blocks;