aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-08-04 04:37:12 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-08-04 04:37:12 +0300
commit0c593583dccd88e90450972e6a1b9e6bc67e0911 (patch)
treed38de5fb6255fec9424bf9694c1cf3fcfd8b9c39
parenttest/toolkit.py: fix utility names (diff)
downloadaes-tools-0c593583dccd88e90450972e6a1b9e6bc67e0911.tar.gz
aes-tools-0c593583dccd88e90450972e6a1b9e6bc67e0911.zip
utils: bugfix & refactoring
Diffstat (limited to '')
-rw-r--r--utils/block_cmd_parser.hpp88
-rw-r--r--utils/block_common.hpp242
-rw-r--r--utils/block_dumper.hpp108
-rw-r--r--utils/data_parsers.hpp55
-rw-r--r--utils/decrypt_block.cpp26
-rw-r--r--utils/decrypt_bmp.cpp17
-rw-r--r--utils/decrypt_file.cpp17
-rw-r--r--utils/encrypt_block.cpp26
-rw-r--r--utils/encrypt_bmp.cpp17
-rw-r--r--utils/encrypt_file.cpp19
-rw-r--r--utils/file_cmd_parser.hpp84
-rw-r--r--utils/file_common.hpp132
12 files changed, 416 insertions, 415 deletions
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 <Egor.Tensin@gmail.com>
+ * \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 <aesnixx/all.hpp>
+
+#include <boost/program_options.hpp>
+
+#include <ostream>
+#include <string>
+#include <vector>
+
+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<aesni::Mode>(&mode)->required(), "set mode of operation")
+ ("algorithm,a", po::value<aesni::Algorithm>(&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<std::vector<std::string>>(&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<std::string> 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 <Egor.Tensin@gmail.com>
- * \date 2015
- * \copyright This file is licensed under the terms of the MIT License.
- * See LICENSE.txt for details.
- */
-
-#pragma once
-
-#include <aesnixx/all.hpp>
-
-#include <boost/algorithm/string.hpp>
-#include <boost/program_options.hpp>
-
-#include <cstdlib>
-
-#include <deque>
-#include <iostream>
-#include <iterator>
-#include <istream>
-#include <string>
-#include <vector>
-
-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<aesni::Mode>(&encryption_mode)->required(), "set mode of operation")
- ("algorithm,a", po::value<aesni::Algorithm>(&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<std::vector<std::string>>(&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<std::string> 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<std::string> args;
- bool verbose_flag;
- };
-}
-
-namespace
-{
- template <aesni::Algorithm algorithm>
- void dump_block(const char* name, const typename aesni::Types<algorithm>::Block& block)
- {
- std::cout << name << ": " << aesni::to_string<algorithm>(block) << "\n" << aesni::to_matrix_string<algorithm>(block) << "\n";
- }
-
- template <aesni::Algorithm algorithm>
- void dump_plaintext(const typename aesni::Types<algorithm>::Block& block)
- {
- dump_block<algorithm>("Plaintext", block);
- }
-
- template <aesni::Algorithm algorithm>
- void dump_key(const typename aesni::Types<algorithm>::Key& key)
- {
- std::cout << "Key: " << aesni::to_string<algorithm>(key) << "\n\n";
- }
-
- template <aesni::Algorithm algorithm>
- void dump_ciphertext(const typename aesni::Types<algorithm>::Block& ciphertext)
- {
- dump_block<algorithm>("Ciphertext", ciphertext);
- }
-
- template <aesni::Algorithm algorithm>
- void dump_iv(const typename aesni::Types<algorithm>::Block& iv)
- {
- dump_block<algorithm>("Initialization vector", iv);
- }
-
- template <aesni::Algorithm algorithm>
- void dump_round_keys(const char* name, const typename aesni::Types<algorithm>::RoundKeys& round_keys)
- {
- std::cout << name << ":\n";
- for (std::size_t i = 0; i < aesni::get_number_of_rounds<algorithm>(); ++i)
- std::cout << "\t[" << i << "]: " << aesni::to_string<algorithm>(round_keys.keys[i]) << "\n";
- std::cout << "\n";
- }
-
- template <aesni::Algorithm algorithm>
- void dump_encryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
- {
- dump_round_keys<algorithm>("Encryption round keys", round_keys);
- }
-
- template <aesni::Algorithm algorithm>
- void dump_decryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
- {
- dump_round_keys<algorithm>("Decryption round keys", round_keys);
- }
-
- template <aesni::Algorithm algorithm, aesni::Mode mode>
- void dump_wrapper(
- const aesni::EncryptWrapper<algorithm, mode>& wrapper)
- {
- dump_encryption_keys<algorithm>(wrapper.encryption_keys);
- }
-
- template <aesni::Algorithm algorithm, aesni::Mode mode>
- void dump_wrapper(
- const aesni::DecryptWrapper<algorithm, mode>& wrapper)
- {
- dump_decryption_keys<algorithm>(wrapper.decryption_keys);
- }
-
- template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
- void dump_next_iv(
- const aesni::EncryptWrapper<algorithm, mode>& wrapper)
- {
- dump_block<algorithm>("Next initialization vector", wrapper.iv);
- }
-
- template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
- void dump_next_iv(
- const aesni::EncryptWrapper<algorithm, mode>&)
- { }
-
- template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
- void dump_next_iv(
- const aesni::DecryptWrapper<algorithm, mode>& wrapper)
- {
- dump_block<algorithm>("Next initialization vector", wrapper.iv);
- }
-
- template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
- void dump_next_iv(
- const aesni::DecryptWrapper<algorithm, mode>&)
- { }
-}
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 <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#pragma once
+
+#include <aesnixx/all.hpp>
+
+#include <cstdlib>
+
+#include <iostream>
+#include <type_traits>
+
+namespace
+{
+ template <aesni::Algorithm algorithm>
+ void dump_block(const char* name, const typename aesni::Types<algorithm>::Block& block)
+ {
+ std::cout << name << ": " << aesni::to_string<algorithm>(block) << "\n" << aesni::to_matrix_string<algorithm>(block) << "\n";
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_plaintext(const typename aesni::Types<algorithm>::Block& block)
+ {
+ dump_block<algorithm>("Plaintext", block);
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_key(const typename aesni::Types<algorithm>::Key& key)
+ {
+ std::cout << "Key: " << aesni::to_string<algorithm>(key) << "\n\n";
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_ciphertext(const typename aesni::Types<algorithm>::Block& ciphertext)
+ {
+ dump_block<algorithm>("Ciphertext", ciphertext);
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_iv(const typename aesni::Types<algorithm>::Block& iv)
+ {
+ dump_block<algorithm>("Initialization vector", iv);
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_round_keys(const char* name, const typename aesni::Types<algorithm>::RoundKeys& round_keys)
+ {
+ std::cout << name << ":\n";
+ for (std::size_t i = 0; i < aesni::get_number_of_rounds<algorithm>(); ++i)
+ std::cout << "\t[" << i << "]: " << aesni::to_string<algorithm>(round_keys.keys[i]) << "\n";
+ std::cout << "\n";
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_encryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
+ {
+ dump_round_keys<algorithm>("Encryption round keys", round_keys);
+ }
+
+ template <aesni::Algorithm algorithm>
+ void dump_decryption_keys(const typename aesni::Types<algorithm>::RoundKeys& round_keys)
+ {
+ dump_round_keys<algorithm>("Decryption round keys", round_keys);
+ }
+
+ template <aesni::Algorithm algorithm, aesni::Mode mode>
+ void dump_wrapper(
+ const aesni::EncryptWrapper<algorithm, mode>& wrapper)
+ {
+ dump_encryption_keys<algorithm>(wrapper.encryption_keys);
+ }
+
+ template <aesni::Algorithm algorithm, aesni::Mode mode>
+ void dump_wrapper(
+ const aesni::DecryptWrapper<algorithm, mode>& wrapper)
+ {
+ dump_decryption_keys<algorithm>(wrapper.decryption_keys);
+ }
+
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::EncryptWrapper<algorithm, mode>& wrapper)
+ {
+ dump_block<algorithm>("Next initialization vector", wrapper.iv);
+ }
+
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::EncryptWrapper<algorithm, mode>&)
+ { }
+
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::DecryptWrapper<algorithm, mode>& wrapper)
+ {
+ dump_block<algorithm>("Next initialization vector", wrapper.iv);
+ }
+
+ template <aesni::Algorithm algorithm, aesni::Mode mode, typename std::enable_if<!aesni::ModeRequiresInitializationVector<mode>::value>::type* = 0>
+ void dump_next_iv(
+ const aesni::DecryptWrapper<algorithm, mode>&)
+ { }
+}
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 <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#pragma once
+
+#include <aesnixx/all.hpp>
+
+#include <boost/algorithm/string.hpp>
+#include <boost/program_options.hpp>
+
+#include <istream>
+#include <string>
+
+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 <aesni/all.h>
#include <aesnixx/all.hpp>
+#include <boost/program_options.hpp>
+
#include <deque>
#include <exception>
#include <iostream>
+#include <iterator>
#include <string>
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<std::string> 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 <aesni/all.h>
@@ -21,8 +21,8 @@
#include <exception>
#include <fstream>
#include <iostream>
+#include <iterator>
#include <string>
-#include <utility>
#include <vector>
#include <Windows.h>
@@ -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<std::string> 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 <aesni/all.h>
@@ -20,8 +20,8 @@
#include <exception>
#include <fstream>
#include <iostream>
+#include <iterator>
#include <string>
-#include <utility>
#include <vector>
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<std::string> 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 <aesni/all.h>
#include <aesnixx/all.hpp>
+#include <boost/program_options.hpp>
+
#include <deque>
#include <exception>
#include <iostream>
+#include <iterator>
#include <string>
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<std::string> 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 <aesni/all.h>
@@ -21,8 +21,8 @@
#include <exception>
#include <fstream>
#include <iostream>
+#include <iterator>
#include <string>
-#include <utility>
#include <vector>
#include <Windows.h>
@@ -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<std::string> 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 <aesni/all.h>
@@ -20,8 +20,8 @@
#include <exception>
#include <fstream>
#include <iostream>
+#include <iterator>
#include <string>
-#include <utility>
#include <vector>
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<std::string> 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 <Egor.Tensin@gmail.com>
+ * \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 <aesnixx/all.hpp>
+
+#include <boost/program_options.hpp>
+
+#include <ostream>
+#include <string>
+#include <vector>
+
+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<aesni::Mode>(&mode)->required(), "set mode of operation")
+ ("algorithm,a", po::value<aesni::Algorithm>(&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<std::vector<std::string>>(&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<std::string> 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 <Egor.Tensin@gmail.com>
- * \date 2015
- * \copyright This file is licensed under the terms of the MIT License.
- * See LICENSE.txt for details.
- */
-
-#pragma once
-
-#include <aesnixx/all.hpp>
-
-#include <boost/algorithm/string.hpp>
-#include <boost/program_options.hpp>
-
-#include <deque>
-#include <iostream>
-#include <iterator>
-#include <istream>
-#include <string>
-#include <vector>
-
-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<aesni::Mode>(&encryption_mode)->required(), "set mode of operation")
- ("algorithm,a", po::value<aesni::Algorithm>(&encryption_algo)->required(), "set algorithm");
-
- po::options_description hidden_options;
- hidden_options.add_options()
- ("positional", po::value<std::vector<std::string>>(&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<std::string> 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<std::string> args;
- };
-}