From f6d646da55718374eb08d9bf4bc342cb5f396376 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Thu, 26 May 2022 23:22:04 +0200 Subject: [PATCH] Implemented to-block methods of dataformatter --- GCryptCLI/include/ModuleDataFormatter.h | 1 + GCryptCLI/src/ModuleDataFormatter.cpp | 81 ++++++++++++++++++++++++- GCryptCLI/src/main.cpp | 1 - 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/GCryptCLI/include/ModuleDataFormatter.h b/GCryptCLI/include/ModuleDataFormatter.h index bbdccdf..f08276a 100644 --- a/GCryptCLI/include/ModuleDataFormatter.h +++ b/GCryptCLI/include/ModuleDataFormatter.h @@ -12,6 +12,7 @@ using namespace Leonetienne::GCrypt; class ModuleDataFormatter { public: static std::string FormatBlock(const Block& block, const Configuration::IOBASE_FORMAT base); + static Block StringToBlock(const std::string& str, const Configuration::IOBASE_FORMAT base); private: diff --git a/GCryptCLI/src/ModuleDataFormatter.cpp b/GCryptCLI/src/ModuleDataFormatter.cpp index 858d9bf..eca1014 100644 --- a/GCryptCLI/src/ModuleDataFormatter.cpp +++ b/GCryptCLI/src/ModuleDataFormatter.cpp @@ -2,6 +2,7 @@ #include "Bases.h" #include #include +#include using namespace Leonetienne::GCrypt; using namespace Leonetienne::StringTools; @@ -55,10 +56,85 @@ std::string ModuleDataFormatter::FormatBlock( ); default: - throw std::invalid_argument("Iobase now found! Oh no. Anyway."); + throw std::invalid_argument("FormatBlock(): Iobase now found! Oh no. Anyway."); } } +Block ModuleDataFormatter::StringToBlock( + const std::string& str, + const Configuration::IOBASE_FORMAT base +) { + + Block b; + + switch (base) { + case Configuration::IOBASE_FORMAT::BASE_BYTES: + b.FromByteString(str); + break; + + case Configuration::IOBASE_FORMAT::BASE_2: + b.FromBinaryString(str); + break; + + case Configuration::IOBASE_FORMAT::BASE_8: + b.FromBinaryString( + CustomBase2Bin( + str, + BASE_8 + ) + ); + break; + + case Configuration::IOBASE_FORMAT::BASE_10: + b.FromBinaryString( + CustomBase2Bin( + str, + BASE_10 + ) + ); + break; + + case Configuration::IOBASE_FORMAT::BASE_16: + b.FromHexString(str); + break; + + case Configuration::IOBASE_FORMAT::BASE_64: + b.FromBinaryString( + CustomBase2Bin( + str, + BASE_64 + ) + ); + break; + + case Configuration::IOBASE_FORMAT::BASE_UWU: + b.FromBinaryString( + CustomBase2Bin( + str, + BASE_UWU, + " " + ) + ); + break; + + case Configuration::IOBASE_FORMAT::BASE_UGH: + b.FromBinaryString( + CustomBase2Bin( + str, + BASE_UGH, + " " + ) + ); + break; + + default: + throw std::invalid_argument("StringToBlock(): Iobase now found! Oh no. Anyway."); + } + + return b; +} + + std::string ModuleDataFormatter::Bin2CustomBase( const std::string& bin, const std::vector& customSet, @@ -94,6 +170,9 @@ std::string ModuleDataFormatter::CustomBase2Bin( std::string binary = Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y, std::string>(in_symbols, customSet, std::string("01")); + // Pad to BLOCK_SIZE + binary = PadStringToLength(binary, Block::BLOCK_SIZE_BITS, '0', false); + // Check that our string is of size BLOCK_SIZE if (binary.length() != Block::BLOCK_SIZE_BITS) { throw std::invalid_argument("ModuleDataFormatter::CustomBase2Bin received input, which does note translate to a bitstring of size BLOCK_SIZE. Huh?"); diff --git a/GCryptCLI/src/main.cpp b/GCryptCLI/src/main.cpp index 4ceac2b..9683abd 100644 --- a/GCryptCLI/src/main.cpp +++ b/GCryptCLI/src/main.cpp @@ -22,7 +22,6 @@ int main(int argc, char* const* argv) { ) << std::endl; - return 0; }