From a151a774f4ce6c4b13c819502efcfe919bbf050b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 25 Jun 2017 19:56:14 +0300 Subject: code style --- utils/block_cmd_parser.hpp | 175 ++++++++++++++++++++++----------------------- utils/block_dumper.hpp | 169 +++++++++++++++++++++---------------------- utils/block_input.hpp | 33 ++++----- utils/data_parsers.hpp | 4 +- utils/file_cmd_parser.hpp | 117 +++++++++++++++--------------- 5 files changed, 243 insertions(+), 255 deletions(-) (limited to 'utils') diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index c748b3d..58f86d8 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -19,109 +19,106 @@ #include #include -namespace +class BlockSettings : public command_line::SettingsParser { - class BlockSettings : public command_line::SettingsParser - { - public: - aes::Algorithm algorithm = AES_AES128; - aes::Mode mode = AES_ECB; +public: + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - bool use_boxes = false; - bool verbose = false; + bool use_boxes = false; + bool verbose = false; - std::vector inputs; + std::vector inputs; - explicit BlockSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("verbose,v", - boost::program_options::bool_switch(&verbose), - "enable verbose output") - ("algorithm,a", - boost::program_options::value(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("use-boxes,b", - boost::program_options::bool_switch(&use_boxes), - "use the \"boxes\" interface"); - hidden.add_options() - ("args", - boost::program_options::value>(&args), - "shouldn't be visible"); - positional.add("args", -1); - } + explicit BlockSettings(const std::string& argv0) + : SettingsParser{argv0} + { + visible.add_options() + ("verbose,v", + boost::program_options::bool_switch(&verbose), + "enable verbose output") + ("algorithm,a", + boost::program_options::value(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("use-boxes,b", + boost::program_options::bool_switch(&use_boxes), + "use the \"boxes\" interface"); + hidden.add_options() + ("args", + boost::program_options::value>(&args), + "shouldn't be visible"); + positional.add("args", -1); + } + + const char* get_short_description() const override + { + return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" + " [-- KEY [IV] [BLOCK]...]..."; + } - const char* get_short_description() const override - { - return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" - " [-- KEY [IV] [BLOCK]...]..."; - } + void parse(int argc, char* argv[]) override + { + SettingsParser::parse(argc, argv); + parse_inputs(std::deque{ + std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())}); + } + +private: + void parse_inputs(std::deque&& src) + { + while (!src.empty()) + inputs.emplace_back(parse_input(src)); + } - void parse(int argc, char* argv[]) override - { - SettingsParser::parse(argc, argv); - parse_inputs(std::deque{ - std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end())}); - } + Input parse_input(std::deque& src) const + { + std::string key{std::move(src.front())}; + src.pop_front(); - private: - void parse_inputs(std::deque&& src) - { - while (!src.empty()) - inputs.emplace_back(parse_input(src)); - } + std::string iv; - Input parse_input(std::deque& src) const + if (aes::mode_requires_init_vector(mode)) { - std::string key{std::move(src.front())}; - src.pop_front(); - - std::string iv; - - if (aes::mode_requires_init_vector(mode)) + if (src.empty()) { - if (src.empty()) - { - throw boost::program_options::error{ - "an initialization vector is required for the selected mode of operation"}; - } - iv = std::move(src.front()); - src.pop_front(); + throw boost::program_options::error{ + "an initialization vector is required for the selected mode of operation"}; } - - auto blocks = parse_blocks(src); - - if (aes::mode_requires_init_vector(mode)) - return {key, iv, std::move(blocks)}; - else - return {key, std::move(blocks)}; + iv = std::move(src.front()); + src.pop_front(); } - static std::vector parse_blocks(std::deque& src) - { - std::vector blocks; + auto blocks = parse_blocks(src); - while (!src.empty()) - { - std::string block{std::move(src.front())}; - src.pop_front(); - if (block == "--") - break; - blocks.emplace_back(std::move(block)); - } + if (aes::mode_requires_init_vector(mode)) + return {key, iv, std::move(blocks)}; + else + return {key, std::move(blocks)}; + } + + static std::vector parse_blocks(std::deque& src) + { + std::vector blocks; - return blocks; + while (!src.empty()) + { + std::string block{std::move(src.front())}; + src.pop_front(); + if (block == "--") + break; + blocks.emplace_back(std::move(block)); } - std::vector args; - }; -} + return blocks; + } + + std::vector args; +}; diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp index ff28c75..00f75f1 100644 --- a/utils/block_dumper.hpp +++ b/utils/block_dumper.hpp @@ -12,91 +12,88 @@ #include #include -namespace +template +void dump_block( + const char* header, + const typename aes::Types::Block& block) { - template - void dump_block( - const char* header, - const typename aes::Types::Block& block) - { - std::cout << header << ": " << aes::to_string(block) << "\n"; - std::cout << aes::to_matrix_string(block) << "\n"; - } - - template - void dump_plaintext(const typename aes::Types::Block& block) - { - dump_block("Plaintext", block); - } - - template - void dump_key(const typename aes::Types::Key& key) - { - std::cout << "Key: " << aes::to_string(key) << "\n\n"; - } - - template - void dump_ciphertext(const typename aes::Types::Block& ciphertext) - { - dump_block("Ciphertext", ciphertext); - } - - template - void dump_iv(const typename aes::Types::Block& iv) - { - dump_block("Initialization vector", iv); - } - - template - void dump_round_keys(const char* header, const typename aes::Types::RoundKeys& round_keys) - { - std::cout << header << ":\n"; - for (std::size_t i = 0; i < aes::get_number_of_rounds(); ++i) - std::cout << "\t[" << i << "]: " << aes::to_string(round_keys.keys[i]) << "\n"; - std::cout << "\n"; - } - - template - void dump_encryption_keys(const typename aes::Types::RoundKeys& round_keys) - { - dump_round_keys("Encryption round keys", round_keys); - } - - template - void dump_decryption_keys(const typename aes::Types::RoundKeys& round_keys) - { - dump_round_keys("Decryption round keys", round_keys); - } - - template - void dump_wrapper(const aes::EncryptWrapper& wrapper) - { - dump_encryption_keys(wrapper.encryption_keys); - } - - template - void dump_wrapper(const aes::DecryptWrapper& wrapper) - { - dump_decryption_keys(wrapper.decryption_keys); - } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::EncryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::EncryptWrapper&) - { } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::DecryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = nullptr> - void dump_next_iv(const aes::DecryptWrapper&) - { } + std::cout << header << ": " << aes::to_string(block) << "\n"; + std::cout << aes::to_matrix_string(block) << "\n"; } + +template +void dump_plaintext(const typename aes::Types::Block& block) +{ + dump_block("Plaintext", block); +} + +template +void dump_key(const typename aes::Types::Key& key) +{ + std::cout << "Key: " << aes::to_string(key) << "\n\n"; +} + +template +void dump_ciphertext(const typename aes::Types::Block& ciphertext) +{ + dump_block("Ciphertext", ciphertext); +} + +template +void dump_iv(const typename aes::Types::Block& iv) +{ + dump_block("Initialization vector", iv); +} + +template +void dump_round_keys(const char* header, const typename aes::Types::RoundKeys& round_keys) +{ + std::cout << header << ":\n"; + for (std::size_t i = 0; i < aes::get_number_of_rounds(); ++i) + std::cout << "\t[" << i << "]: " << aes::to_string(round_keys.keys[i]) << "\n"; + std::cout << "\n"; +} + +template +void dump_encryption_keys(const typename aes::Types::RoundKeys& round_keys) +{ + dump_round_keys("Encryption round keys", round_keys); +} + +template +void dump_decryption_keys(const typename aes::Types::RoundKeys& round_keys) +{ + dump_round_keys("Decryption round keys", round_keys); +} + +template +void dump_wrapper(const aes::EncryptWrapper& wrapper) +{ + dump_encryption_keys(wrapper.encryption_keys); +} + +template +void dump_wrapper(const aes::DecryptWrapper& wrapper) +{ + dump_decryption_keys(wrapper.decryption_keys); +} + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::EncryptWrapper& wrapper) +{ + dump_block("Next initialization vector", wrapper.iv); +} + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::EncryptWrapper&) +{ } + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::DecryptWrapper& wrapper) +{ + dump_block("Next initialization vector", wrapper.iv); +} + +template ::value>::type* = nullptr> +void dump_next_iv(const aes::DecryptWrapper&) +{ } diff --git a/utils/block_input.hpp b/utils/block_input.hpp index 549e3f2..f629062 100644 --- a/utils/block_input.hpp +++ b/utils/block_input.hpp @@ -9,24 +9,21 @@ #include #include -namespace +class Input { - class Input - { - public: - Input(const std::string& key, const std::string& iv, std::vector&& blocks) - : key{key} - , iv{iv} - , blocks{std::move(blocks)} - { } +public: + Input(const std::string& key, const std::string& iv, std::vector&& blocks) + : key{key} + , iv{iv} + , blocks{std::move(blocks)} + { } - Input(const std::string& key, std::vector&& blocks) - : key{key} - , blocks{std::move(blocks)} - { } + Input(const std::string& key, std::vector&& blocks) + : key{key} + , blocks{std::move(blocks)} + { } - const std::string key; - const std::string iv; - const std::vector blocks; - }; -} + const std::string key; + const std::string iv; + const std::vector blocks; +}; diff --git a/utils/data_parsers.hpp b/utils/data_parsers.hpp index e4f0ae2..1207c7c 100644 --- a/utils/data_parsers.hpp +++ b/utils/data_parsers.hpp @@ -14,7 +14,7 @@ #include #include -static std::istream& operator>>(std::istream& is, aes::Mode& dest) +inline std::istream& operator>>(std::istream& is, aes::Mode& dest) { std::string src; is >> src; @@ -37,7 +37,7 @@ static std::istream& operator>>(std::istream& is, aes::Mode& dest) return is; } -static std::istream& operator>>(std::istream& is, aes::Algorithm& dest) +inline std::istream& operator>>(std::istream& is, aes::Algorithm& dest) { std::string src; is >> src; diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp index 6d7a962..e199409 100644 --- a/utils/file_cmd_parser.hpp +++ b/utils/file_cmd_parser.hpp @@ -17,70 +17,67 @@ #include #include -namespace +class FileSettings : public command_line::SettingsParser { - class FileSettings : public command_line::SettingsParser - { - public: - aes::Algorithm algorithm = AES_AES128; - aes::Mode mode = AES_ECB; +public: + aes::Algorithm algorithm = AES_AES128; + aes::Mode mode = AES_ECB; - std::string input_path; - std::string output_path; - std::string key; - std::string iv; + std::string input_path; + std::string output_path; + std::string key; + std::string iv; - explicit FileSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("algorithm,a", - boost::program_options::value(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("key,k", - boost::program_options::value(&key) - ->required() - ->value_name("KEY"), - "set encryption key") - ("iv,v", - boost::program_options::value(&iv) - ->value_name("BLOCK"), - "set initialization vector") - ("input,i", - boost::program_options::value(&input_path) - ->required() - ->value_name("PATH"), - "set input file path") - ("output,o", - boost::program_options::value(&output_path) - ->required() - ->value_name("PATH"), - "set output file path"); - } + explicit FileSettings(const std::string& argv0) + : SettingsParser{argv0} + { + visible.add_options() + ("algorithm,a", + boost::program_options::value(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm") + ("mode,m", + boost::program_options::value(&mode) + ->required() + ->value_name("MODE"), + "set mode of operation") + ("key,k", + boost::program_options::value(&key) + ->required() + ->value_name("KEY"), + "set encryption key") + ("iv,v", + boost::program_options::value(&iv) + ->value_name("BLOCK"), + "set initialization vector") + ("input,i", + boost::program_options::value(&input_path) + ->required() + ->value_name("PATH"), + "set input file path") + ("output,o", + boost::program_options::value(&output_path) + ->required() + ->value_name("PATH"), + "set output file path"); + } - const char* get_short_description() const override - { - return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" - " [-k|--key KEY] [-v|--iv BLOCK]" - " [-i|--input PATH] [-o|--output PATH]"; - } + const char* get_short_description() const override + { + return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" + " [-k|--key KEY] [-v|--iv BLOCK]" + " [-i|--input PATH] [-o|--output PATH]"; + } - void parse(int argc, char** argv) override - { - SettingsParser::parse(argc, argv); + void parse(int argc, char** argv) override + { + SettingsParser::parse(argc, argv); - if (aes::mode_requires_init_vector(mode) && iv.empty()) - { - throw boost::program_options::error{ - "an initialization vector is required for the selected mode of operation"}; - } + if (aes::mode_requires_init_vector(mode) && iv.empty()) + { + throw boost::program_options::error{ + "an initialization vector is required for the selected mode of operation"}; } - }; -} + } +}; -- cgit v1.2.3