From 0c593583dccd88e90450972e6a1b9e6bc67e0911 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 4 Aug 2015 04:37:12 +0300 Subject: utils: bugfix & refactoring --- utils/block_cmd_parser.hpp | 88 +++++++++++++++++ utils/block_common.hpp | 242 --------------------------------------------- utils/block_dumper.hpp | 108 ++++++++++++++++++++ utils/data_parsers.hpp | 55 +++++++++++ utils/decrypt_block.cpp | 26 +++-- utils/decrypt_bmp.cpp | 17 +++- utils/decrypt_file.cpp | 17 +++- utils/encrypt_block.cpp | 26 +++-- utils/encrypt_bmp.cpp | 17 +++- utils/encrypt_file.cpp | 19 ++-- utils/file_cmd_parser.hpp | 84 ++++++++++++++++ utils/file_common.hpp | 132 ------------------------- 12 files changed, 416 insertions(+), 415 deletions(-) create mode 100644 utils/block_cmd_parser.hpp delete mode 100644 utils/block_common.hpp create mode 100644 utils/block_dumper.hpp create mode 100644 utils/data_parsers.hpp create mode 100644 utils/file_cmd_parser.hpp delete mode 100644 utils/file_common.hpp diff --git a/utils/block_cmd_parser.hpp b/utils/block_cmd_parser.hpp new file mode 100644 index 0000000..c191154 --- /dev/null +++ b/utils/block_cmd_parser.hpp @@ -0,0 +1,88 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "data_parsers.hpp" + +#include + +#include + +#include +#include +#include + +namespace +{ + class CommandLineParser + { + public: + CommandLineParser(const char* prog_name) + : prog_name(prog_name) + , options("Options") + { + namespace po = boost::program_options; + + options.add_options() + ("help,h", "show this message and exit") + ("box,b", po::bool_switch(&use_boxes)->default_value(false), "use the \"boxes\" interface") + ("mode,m", po::value(&mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&algorithm)->required(), "set algorithm") + ("verbose,v", po::bool_switch(&verbose)->default_value(false), "enable verbose output"); + } + + void parse(int argc, char** argv) + { + namespace po = boost::program_options; + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&args)); + + po::options_description all_options; + all_options.add(options).add(hidden_options); + + po::positional_options_description positional_options; + positional_options.add("positional", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); + + if (vm.count("help")) + { + help_flag = true; + return; + } + + po::notify(vm); + } + + bool requested_help() const { return help_flag; } + + aesni::Mode mode; + aesni::Algorithm algorithm; + bool use_boxes; + std::vector args; + bool verbose; + + private: + const std::string prog_name; + boost::program_options::options_description options; + + bool help_flag = false; + + friend std::ostream& operator<<(std::ostream&, const CommandLineParser&); + }; + + std::ostream& operator<<(std::ostream& os, const CommandLineParser& cmd_parser) + { + return os << "Usage: " << cmd_parser.prog_name << " [OPTIONS...] [-- KEY [IV] [BLOCK...]...]\n" + << cmd_parser.options << "\n"; + } +} diff --git a/utils/block_common.hpp b/utils/block_common.hpp deleted file mode 100644 index 52b9ce3..0000000 --- a/utils/block_common.hpp +++ /dev/null @@ -1,242 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -#include - -#include -#include - -#include - -#include -#include -#include -#include -#include -#include - -static std::istream& operator>>(std::istream& is, aesni::Mode& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "ecb")) - dest = AESNI_ECB; - else if (boost::iequals(src, "cbc")) - dest = AESNI_CBC; - else if (boost::iequals(src, "cfb")) - dest = AESNI_CFB; - else if (boost::iequals(src, "ofb")) - dest = AESNI_OFB; - else if (boost::iequals(src, "ctr")) - dest = AESNI_CTR; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src); - - return is; -} - -static std::istream& operator>>(std::istream& is, aesni::Algorithm& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "aes128")) - dest = AESNI_AES128; - else if (boost::iequals(src, "aes192")) - dest = AESNI_AES192; - else if (boost::iequals(src, "aes256")) - dest = AESNI_AES256; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src); - - return is; -} - -namespace -{ - class CommandLineParser - { - public: - CommandLineParser(const std::string& prog_name) - : prog_name(prog_name) - , options("Options") - , boxes_flag(false) - , verbose_flag(false) - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - options.add_options() - ("help,h", "show this message and exit") - ("box,b", po::bool_switch(&boxes_flag)->default_value(false), "use the \"boxes\" interface") - ("mode,m", po::value(&encryption_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&encryption_algo)->required(), "set algorithm") - ("verbose,v", po::bool_switch(&verbose_flag)->default_value(false), "enable verbose output"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value>(&args)); - - po::options_description all_options; - all_options.add(options).add(hidden_options); - - po::positional_options_description positional_options; - positional_options.add("positional", -1); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); - - if (vm.count("help")) - { - print_usage(); - return false; - } - - po::notify(vm); - return true; - } - - void print_usage() - { - std::cout << "Usage: " << prog_name << " [OPTIONS...] [-- KEY [IV] [BLOCK...]...]\n"; - std::cout << options << "\n"; - } - - aesni::Mode get_mode() const - { - return encryption_mode; - } - - aesni::Algorithm get_algorithm() const - { - return encryption_algo; - } - - bool use_boxes() const - { - return boxes_flag; - } - - std::deque get_args() - { - return { std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()) }; - } - - bool verbose() const - { - return verbose_flag; - } - - private: - const std::string prog_name; - boost::program_options::options_description options; - - aesni::Mode encryption_mode; - aesni::Algorithm encryption_algo; - bool boxes_flag; - std::vector args; - bool verbose_flag; - }; -} - -namespace -{ - template - void dump_block(const char* name, const typename aesni::Types::Block& block) - { - std::cout << name << ": " << aesni::to_string(block) << "\n" << aesni::to_matrix_string(block) << "\n"; - } - - template - void dump_plaintext(const typename aesni::Types::Block& block) - { - dump_block("Plaintext", block); - } - - template - void dump_key(const typename aesni::Types::Key& key) - { - std::cout << "Key: " << aesni::to_string(key) << "\n\n"; - } - - template - void dump_ciphertext(const typename aesni::Types::Block& ciphertext) - { - dump_block("Ciphertext", ciphertext); - } - - template - void dump_iv(const typename aesni::Types::Block& iv) - { - dump_block("Initialization vector", iv); - } - - template - void dump_round_keys(const char* name, const typename aesni::Types::RoundKeys& round_keys) - { - std::cout << name << ":\n"; - for (std::size_t i = 0; i < aesni::get_number_of_rounds(); ++i) - std::cout << "\t[" << i << "]: " << aesni::to_string(round_keys.keys[i]) << "\n"; - std::cout << "\n"; - } - - template - void dump_encryption_keys(const typename aesni::Types::RoundKeys& round_keys) - { - dump_round_keys("Encryption round keys", round_keys); - } - - template - void dump_decryption_keys(const typename aesni::Types::RoundKeys& round_keys) - { - dump_round_keys("Decryption round keys", round_keys); - } - - template - void dump_wrapper( - const aesni::EncryptWrapper& wrapper) - { - dump_encryption_keys(wrapper.encryption_keys); - } - - template - void dump_wrapper( - const aesni::DecryptWrapper& wrapper) - { - dump_decryption_keys(wrapper.decryption_keys); - } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::EncryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::EncryptWrapper&) - { } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::DecryptWrapper& wrapper) - { - dump_block("Next initialization vector", wrapper.iv); - } - - template ::value>::type* = 0> - void dump_next_iv( - const aesni::DecryptWrapper&) - { } -} diff --git a/utils/block_dumper.hpp b/utils/block_dumper.hpp new file mode 100644 index 0000000..a473cd1 --- /dev/null +++ b/utils/block_dumper.hpp @@ -0,0 +1,108 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include + +#include + +#include +#include + +namespace +{ + template + void dump_block(const char* name, const typename aesni::Types::Block& block) + { + std::cout << name << ": " << aesni::to_string(block) << "\n" << aesni::to_matrix_string(block) << "\n"; + } + + template + void dump_plaintext(const typename aesni::Types::Block& block) + { + dump_block("Plaintext", block); + } + + template + void dump_key(const typename aesni::Types::Key& key) + { + std::cout << "Key: " << aesni::to_string(key) << "\n\n"; + } + + template + void dump_ciphertext(const typename aesni::Types::Block& ciphertext) + { + dump_block("Ciphertext", ciphertext); + } + + template + void dump_iv(const typename aesni::Types::Block& iv) + { + dump_block("Initialization vector", iv); + } + + template + void dump_round_keys(const char* name, const typename aesni::Types::RoundKeys& round_keys) + { + std::cout << name << ":\n"; + for (std::size_t i = 0; i < aesni::get_number_of_rounds(); ++i) + std::cout << "\t[" << i << "]: " << aesni::to_string(round_keys.keys[i]) << "\n"; + std::cout << "\n"; + } + + template + void dump_encryption_keys(const typename aesni::Types::RoundKeys& round_keys) + { + dump_round_keys("Encryption round keys", round_keys); + } + + template + void dump_decryption_keys(const typename aesni::Types::RoundKeys& round_keys) + { + dump_round_keys("Decryption round keys", round_keys); + } + + template + void dump_wrapper( + const aesni::EncryptWrapper& wrapper) + { + dump_encryption_keys(wrapper.encryption_keys); + } + + template + void dump_wrapper( + const aesni::DecryptWrapper& wrapper) + { + dump_decryption_keys(wrapper.decryption_keys); + } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper& wrapper) + { + dump_block("Next initialization vector", wrapper.iv); + } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::EncryptWrapper&) + { } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper& wrapper) + { + dump_block("Next initialization vector", wrapper.iv); + } + + template ::value>::type* = 0> + void dump_next_iv( + const aesni::DecryptWrapper&) + { } +} diff --git a/utils/data_parsers.hpp b/utils/data_parsers.hpp new file mode 100644 index 0000000..08486fa --- /dev/null +++ b/utils/data_parsers.hpp @@ -0,0 +1,55 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include + +#include +#include + +#include +#include + +static std::istream& operator>>(std::istream& is, aesni::Mode& dest) +{ + std::string src; + is >> src; + + if (boost::iequals(src, "ecb")) + dest = AESNI_ECB; + else if (boost::iequals(src, "cbc")) + dest = AESNI_CBC; + else if (boost::iequals(src, "cfb")) + dest = AESNI_CFB; + else if (boost::iequals(src, "ofb")) + dest = AESNI_OFB; + else if (boost::iequals(src, "ctr")) + dest = AESNI_CTR; + else + throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src); + + return is; +} + +static std::istream& operator>>(std::istream& is, aesni::Algorithm& dest) +{ + std::string src; + is >> src; + + if (boost::iequals(src, "aes128")) + dest = AESNI_AES128; + else if (boost::iequals(src, "aes192")) + dest = AESNI_AES192; + else if (boost::iequals(src, "aes256")) + dest = AESNI_AES256; + else + throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src); + + return is; +} diff --git a/utils/decrypt_block.cpp b/utils/decrypt_block.cpp index 9784657..6159eb5 100644 --- a/utils/decrypt_block.cpp +++ b/utils/decrypt_block.cpp @@ -6,15 +6,19 @@ * See LICENSE.txt for details. */ -#include "block_common.hpp" +#include "block_cmd_parser.hpp" +#include "block_dumper.hpp" #include #include +#include + #include #include #include +#include #include namespace @@ -212,14 +216,16 @@ int main(int argc, char** argv) try { CommandLineParser cmd_parser("decrypt_block.exe"); + cmd_parser.parse(argc, argv); - if (!cmd_parser.parse_options(argc, argv)) + if (cmd_parser.requested_help()) + { + std::cout << cmd_parser; return 0; + } - const auto algorithm = cmd_parser.get_algorithm(); - const auto mode = cmd_parser.get_mode(); - - auto args = cmd_parser.get_args(); + std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), + std::make_move_iterator(cmd_parser.args.end()) }; while (!args.empty()) { @@ -240,13 +246,13 @@ int main(int argc, char** argv) args.pop_front(); } - const auto success = cmd_parser.use_boxes() - ? decrypt_using_boxes(algorithm, mode, key, ciphertexts) - : decrypt_using_cxx_api(algorithm, mode, key, ciphertexts, cmd_parser.verbose()); + const auto success = cmd_parser.use_boxes + ? decrypt_using_boxes(cmd_parser.algorithm, cmd_parser.mode, key, ciphertexts) + : decrypt_using_cxx_api(cmd_parser.algorithm, cmd_parser.mode, key, ciphertexts, cmd_parser.verbose); if (!success) { - cmd_parser.print_usage(); + std::cout << cmd_parser; return 1; } } diff --git a/utils/decrypt_bmp.cpp b/utils/decrypt_bmp.cpp index 3b075a4..aafde38 100644 --- a/utils/decrypt_bmp.cpp +++ b/utils/decrypt_bmp.cpp @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "file_common.hpp" +#include "file_cmd_parser.hpp" #include @@ -21,8 +21,8 @@ #include #include #include +#include #include -#include #include #include @@ -175,13 +175,20 @@ int main(int argc, char** argv) try { CommandLineParser cmd_parser("decrypt_bmp.exe"); + cmd_parser.parse(argc, argv); - if (!cmd_parser.parse_options(argc, argv)) + if (cmd_parser.requested_help()) + { + std::cout << cmd_parser; return 0; + } + + std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), + std::make_move_iterator(cmd_parser.args.end()) }; - if (!decrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + if (!decrypt_bmp(cmd_parser.algorithm, cmd_parser.mode, args)) { - cmd_parser.print_usage(); + std::cout << cmd_parser; return 1; } diff --git a/utils/decrypt_file.cpp b/utils/decrypt_file.cpp index 3e08d6f..2e4da3b 100644 --- a/utils/decrypt_file.cpp +++ b/utils/decrypt_file.cpp @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "file_common.hpp" +#include "file_cmd_parser.hpp" #include @@ -20,8 +20,8 @@ #include #include #include +#include #include -#include #include namespace @@ -165,13 +165,20 @@ int main(int argc, char** argv) try { CommandLineParser cmd_parser("decrypt_file.exe"); + cmd_parser.parse(argc, argv); - if (!cmd_parser.parse_options(argc, argv)) + if (cmd_parser.requested_help()) + { + std::cout << cmd_parser; return 0; + } + + std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), + std::make_move_iterator(cmd_parser.args.end()) }; - if (!decrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + if (!decrypt_file(cmd_parser.algorithm, cmd_parser.mode, args)) { - cmd_parser.print_usage(); + std::cout << cmd_parser; return 1; } diff --git a/utils/encrypt_block.cpp b/utils/encrypt_block.cpp index a40cc67..c8313a6 100644 --- a/utils/encrypt_block.cpp +++ b/utils/encrypt_block.cpp @@ -6,15 +6,19 @@ * See LICENSE.txt for details. */ -#include "block_common.hpp" +#include "block_cmd_parser.hpp" +#include "block_dumper.hpp" #include #include +#include + #include #include #include +#include #include namespace @@ -211,14 +215,16 @@ int main(int argc, char** argv) try { CommandLineParser cmd_parser("encrypt_block.exe"); + cmd_parser.parse(argc, argv); - if (!cmd_parser.parse_options(argc, argv)) + if (cmd_parser.requested_help()) + { + std::cout << cmd_parser; return 0; + } - const auto algorithm = cmd_parser.get_algorithm(); - const auto mode = cmd_parser.get_mode(); - - auto args = cmd_parser.get_args(); + std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), + std::make_move_iterator(cmd_parser.args.end()) }; while (!args.empty()) { @@ -239,13 +245,13 @@ int main(int argc, char** argv) args.pop_front(); } - const auto success = cmd_parser.use_boxes() - ? encrypt_using_boxes(algorithm, mode, key, plaintexts) - : encrypt_using_cxx_api(algorithm, mode, key, plaintexts, cmd_parser.verbose()); + const auto success = cmd_parser.use_boxes + ? encrypt_using_boxes(cmd_parser.algorithm, cmd_parser.mode, key, plaintexts) + : encrypt_using_cxx_api(cmd_parser.algorithm, cmd_parser.mode, key, plaintexts, cmd_parser.verbose); if (!success) { - cmd_parser.print_usage(); + std::cout << cmd_parser; return 1; } } diff --git a/utils/encrypt_bmp.cpp b/utils/encrypt_bmp.cpp index acdd893..4eaa767 100644 --- a/utils/encrypt_bmp.cpp +++ b/utils/encrypt_bmp.cpp @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "file_common.hpp" +#include "file_cmd_parser.hpp" #include @@ -21,8 +21,8 @@ #include #include #include +#include #include -#include #include #include @@ -174,13 +174,20 @@ int main(int argc, char** argv) try { CommandLineParser cmd_parser("encrypt_bmp.exe"); + cmd_parser.parse(argc, argv); - if (!cmd_parser.parse_options(argc, argv)) + if (cmd_parser.requested_help()) + { + std::cout << cmd_parser; return 0; + } + + std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), + std::make_move_iterator(cmd_parser.args.end()) }; - if (!encrypt_bmp(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + if (!encrypt_bmp(cmd_parser.algorithm, cmd_parser.mode, args)) { - cmd_parser.print_usage(); + std::cout << cmd_parser; return 1; } diff --git a/utils/encrypt_file.cpp b/utils/encrypt_file.cpp index 5747536..0619568 100644 --- a/utils/encrypt_file.cpp +++ b/utils/encrypt_file.cpp @@ -6,7 +6,7 @@ * See LICENSE.txt for details. */ -#include "file_common.hpp" +#include "file_cmd_parser.hpp" #include @@ -20,8 +20,8 @@ #include #include #include +#include #include -#include #include namespace @@ -77,7 +77,7 @@ namespace } if (args.size() != 2) - return true; + return false; const auto src_path = args[0]; const auto dest_path = args[1]; @@ -165,13 +165,20 @@ int main(int argc, char** argv) try { CommandLineParser cmd_parser("encrypt_file.exe"); + cmd_parser.parse(argc, argv); - if (!cmd_parser.parse_options(argc, argv)) + if (cmd_parser.requested_help()) + { + std::cout << cmd_parser; return 0; + } + + std::deque args{ std::make_move_iterator(cmd_parser.args.begin()), + std::make_move_iterator(cmd_parser.args.end()) }; - if (!encrypt_file(cmd_parser.get_algorithm(), cmd_parser.get_mode(), cmd_parser.get_args())) + if (!encrypt_file(cmd_parser.algorithm, cmd_parser.mode, args)) { - cmd_parser.print_usage(); + std::cout << cmd_parser; return 1; } diff --git a/utils/file_cmd_parser.hpp b/utils/file_cmd_parser.hpp new file mode 100644 index 0000000..f20d80f --- /dev/null +++ b/utils/file_cmd_parser.hpp @@ -0,0 +1,84 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include "data_parsers.hpp" + +#include + +#include + +#include +#include +#include + +namespace +{ + class CommandLineParser + { + public: + CommandLineParser(const char* prog_name) + : prog_name(prog_name) + , options("Options") + { + namespace po = boost::program_options; + + options.add_options() + ("help,h", "show this message and exit") + ("mode,m", po::value(&mode)->required(), "set mode of operation") + ("algorithm,a", po::value(&algorithm)->required(), "set algorithm"); + } + + void parse(int argc, char** argv) + { + namespace po = boost::program_options; + + po::options_description hidden_options; + hidden_options.add_options() + ("positional", po::value>(&args)); + + po::options_description all_options; + all_options.add(options).add(hidden_options); + + po::positional_options_description positional_options; + positional_options.add("positional", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); + + if (vm.count("help")) + { + help_flag = true; + return; + } + + po::notify(vm); + } + + bool requested_help() const { return help_flag; } + + aesni::Mode mode; + aesni::Algorithm algorithm; + std::vector args; + + private: + const std::string prog_name; + boost::program_options::options_description options; + + bool help_flag = false; + + friend std::ostream& operator<<(std::ostream&, const CommandLineParser&); + }; + + std::ostream& operator<<(std::ostream& os, const CommandLineParser& cmd_parser) + { + return os << "Usage: " << cmd_parser.prog_name << " [OPTIONS...] KEY [IV] SRC_PATH DEST_PATH\n" + << cmd_parser.options << "\n"; + } +} diff --git a/utils/file_common.hpp b/utils/file_common.hpp deleted file mode 100644 index 8a13b48..0000000 --- a/utils/file_common.hpp +++ /dev/null @@ -1,132 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#pragma once - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -static std::istream& operator>>(std::istream& is, aesni::Mode& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "ecb")) - dest = AESNI_ECB; - else if (boost::iequals(src, "cbc")) - dest = AESNI_CBC; - else if (boost::iequals(src, "cfb")) - dest = AESNI_CFB; - else if (boost::iequals(src, "ofb")) - dest = AESNI_OFB; - else if (boost::iequals(src, "ctr")) - dest = AESNI_CTR; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "mode", src); - - return is; -} - -static std::istream& operator>>(std::istream& is, aesni::Algorithm& dest) -{ - std::string src; - is >> src; - - if (boost::iequals(src, "aes128")) - dest = AESNI_AES128; - else if (boost::iequals(src, "aes192")) - dest = AESNI_AES192; - else if (boost::iequals(src, "aes256")) - dest = AESNI_AES256; - else - throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value, "algorithm", src); - - return is; -} - -namespace -{ - class CommandLineParser - { - public: - CommandLineParser(const std::string& prog_name) - : prog_name(prog_name) - , options("Options") - { } - - bool parse_options(int argc, char** argv) - { - namespace po = boost::program_options; - - options.add_options() - ("help,h", "show this message and exit") - ("mode,m", po::value(&encryption_mode)->required(), "set mode of operation") - ("algorithm,a", po::value(&encryption_algo)->required(), "set algorithm"); - - po::options_description hidden_options; - hidden_options.add_options() - ("positional", po::value>(&args)); - - po::options_description all_options; - all_options.add(options).add(hidden_options); - - po::positional_options_description positional_options; - positional_options.add("positional", -1); - - po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(all_options).positional(positional_options).run(), vm); - - if (vm.count("help")) - { - print_usage(); - return false; - } - - po::notify(vm); - return true; - } - - void print_usage() - { - std::cout << "Usage: " << prog_name << " [OPTIONS...] KEY [IV] SRC_PATH DEST_PATH\n"; - std::cout << options << "\n"; - } - - aesni::Mode get_mode() const - { - return encryption_mode; - } - - aesni::Algorithm get_algorithm() const - { - return encryption_algo; - } - - std::deque get_args() - { - return { std::make_move_iterator(args.begin()), std::make_move_iterator(args.end()) }; - } - - private: - const std::string prog_name; - boost::program_options::options_description options; - - aesni::Mode encryption_mode; - aesni::Algorithm encryption_algo; - std::vector args; - }; -} -- cgit v1.2.3