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_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() {};
};

View File

@ -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!

View File

@ -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!

View File

@ -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;

View File

@ -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<Block> DataIngestionLayer::blocks;

View File

@ -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<Block> DataOutputLayer::blocks;