diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-05-04 04:08:15 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-05-04 04:08:15 +0300 |
commit | 188460a91bd37ca6f18fb89e448819b642953be7 (patch) | |
tree | 12fa7b29fa207240fe8ddf19d70f5a5333bf1233 /utils/block | |
parent | refactoring (diff) | |
download | aes-tools-188460a91bd37ca6f18fb89e448819b642953be7.tar.gz aes-tools-188460a91bd37ca6f18fb89e448819b642953be7.zip |
code style & refactoring
Diffstat (limited to '')
-rw-r--r-- | utils/block_cmd_parser.hpp | 90 | ||||
-rw-r--r-- | utils/block_dumper.hpp | 29 | ||||
-rw-r--r-- | utils/block_input.hpp | 36 |
3 files changed, 68 insertions, 87 deletions
diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp index 70452e3..9c9d61c 100644 --- a/utils/block_cmd_parser.hpp +++ b/utils/block_cmd_parser.hpp @@ -13,8 +13,8 @@ #include <boost/filesystem.hpp> #include <boost/program_options.hpp> -#include <iterator> #include <deque> +#include <iterator> #include <ostream> #include <string> #include <utility> @@ -86,9 +86,9 @@ namespace po::notify(vm); - parse_inputs(settings, inputs, std::deque<std::string>( + inputs = parse_inputs(settings, std::deque<std::string>{ std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end()))); + std::make_move_iterator(args.end())}); return settings; } @@ -96,57 +96,55 @@ namespace bool exit_with_usage() const { return help_flag; } private: - static void parse_inputs( + static std::vector<Input> parse_inputs( const Settings& settings, - std::vector<Input>& inputs, std::deque<std::string>&& args) { + std::vector<Input> inputs; while (!args.empty()) + inputs.emplace_back(parse_input(settings, args)); + return inputs; + } + + static Input parse_input( + const Settings& settings, + std::deque<std::string>& args) + { + std::string key{std::move(args.front())}; + args.pop_front(); + + std::string iv; + + if (aes::mode_requires_init_vector(settings.mode)) { - auto key_string = std::move(args.front()); + if (args.empty()) + throw boost::program_options::error{"an initialization vector is required for the selected mode of operation"}; + iv = std::move(args.front()); args.pop_front(); + } - std::string iv_string; - - if (aes::mode_requires_init_vector(settings.mode)) - { - if (args.empty()) - { - throw boost::program_options::error( - "an initialization vector is required for the selected mode of operation"); - } - iv_string = std::move(args.front()); - args.pop_front(); - } - - std::vector<std::string> input_block_strings; - - while (!args.empty()) - { - if (args.front() == "--") - { - args.pop_front(); - break; - } - - input_block_strings.emplace_back(std::move(args.front())); - args.pop_front(); - } - - if (aes::mode_requires_init_vector(settings.mode)) - { - inputs.emplace_back( - std::move(key_string), - std::move(iv_string), - std::move(input_block_strings)); - } - else - { - inputs.emplace_back( - std::move(key_string), - std::move(input_block_strings)); - } + auto blocks = parse_blocks(args); + + if (aes::mode_requires_init_vector(settings.mode)) + return {key, iv, std::move(blocks)}; + else + return {key, std::move(blocks)}; + } + + static std::vector<std::string> parse_blocks(std::deque<std::string>& args) + { + std::vector<std::string> blocks; + + while (!args.empty()) + { + std::string block{std::move(args.front())}; + args.pop_front(); + if (block == "--") + break; + blocks.emplace_back(std::move(block)); } + + return blocks; } const std::string prog_name; diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp index df5b818..ff28c75 100644 --- a/utils/block_dumper.hpp +++ b/utils/block_dumper.hpp @@ -15,9 +15,12 @@ namespace { template <aes::Algorithm algorithm> - void dump_block(const char* name, const typename aes::Types<algorithm>::Block& block) + void dump_block( + const char* header, + const typename aes::Types<algorithm>::Block& block) { - std::cout << name << ": " << aes::to_string<algorithm>(block) << "\n" << aes::to_matrix_string<algorithm>(block) << "\n"; + std::cout << header << ": " << aes::to_string<algorithm>(block) << "\n"; + std::cout << aes::to_matrix_string<algorithm>(block) << "\n"; } template <aes::Algorithm algorithm> @@ -45,9 +48,9 @@ namespace } template <aes::Algorithm algorithm> - void dump_round_keys(const char* name, const typename aes::Types<algorithm>::RoundKeys& round_keys) + void dump_round_keys(const char* header, const typename aes::Types<algorithm>::RoundKeys& round_keys) { - std::cout << name << ":\n"; + std::cout << header << ":\n"; for (std::size_t i = 0; i < aes::get_number_of_rounds<algorithm>(); ++i) std::cout << "\t[" << i << "]: " << aes::to_string<algorithm>(round_keys.keys[i]) << "\n"; std::cout << "\n"; @@ -66,40 +69,34 @@ namespace } template <aes::Algorithm algorithm, aes::Mode mode> - void dump_wrapper( - const aes::EncryptWrapper<algorithm, mode>& wrapper) + void dump_wrapper(const aes::EncryptWrapper<algorithm, mode>& wrapper) { dump_encryption_keys<algorithm>(wrapper.encryption_keys); } template <aes::Algorithm algorithm, aes::Mode mode> - void dump_wrapper( - const aes::DecryptWrapper<algorithm, mode>& wrapper) + void dump_wrapper(const aes::DecryptWrapper<algorithm, mode>& wrapper) { dump_decryption_keys<algorithm>(wrapper.decryption_keys); } template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> - void dump_next_iv( - const aes::EncryptWrapper<algorithm, mode>& wrapper) + void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>& wrapper) { dump_block<algorithm>("Next initialization vector", wrapper.iv); } template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> - void dump_next_iv( - const aes::EncryptWrapper<algorithm, mode>&) + void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>&) { } template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> - void dump_next_iv( - const aes::DecryptWrapper<algorithm, mode>& wrapper) + void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>& wrapper) { dump_block<algorithm>("Next initialization vector", wrapper.iv); } template <aes::Algorithm algorithm, aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> - void dump_next_iv( - const aes::DecryptWrapper<algorithm, mode>&) + void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>&) { } } diff --git a/utils/block_input.hpp b/utils/block_input.hpp index 0bcabbb..549e3f2 100644 --- a/utils/block_input.hpp +++ b/utils/block_input.hpp @@ -6,6 +6,7 @@ #pragma once #include <string> +#include <utility> #include <vector> namespace @@ -13,34 +14,19 @@ namespace class Input { public: - Input( - const std::string& key_string, - const std::string& iv_string, - const std::vector<std::string>& input_block_strings) - : key_string(key_string) - , iv_string(iv_string) - , input_block_strings(input_block_strings) + Input(const std::string& key, const std::string& iv, std::vector<std::string>&& blocks) + : key{key} + , iv{iv} + , blocks{std::move(blocks)} { } - Input( - const std::string& key_string, - const std::vector<std::string>& input_block_strings) - : key_string(key_string) - , input_block_strings(input_block_strings) + Input(const std::string& key, std::vector<std::string>&& blocks) + : key{key} + , blocks{std::move(blocks)} { } - const std::string& get_key_string() const { return key_string; } - - const std::string& get_iv_string() const { return iv_string; } - - const std::vector<std::string>& get_input_block_strings() const - { - return input_block_strings; - } - - private: - const std::string key_string; - const std::string iv_string; - const std::vector<std::string> input_block_strings; + const std::string key; + const std::string iv; + const std::vector<std::string> blocks; }; } |