aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aesxx/utils
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2026-06-13 14:37:12 +0200
committerEgor Tensin <egor@tensin.name>2026-06-13 14:37:12 +0200
commit6cac3cb57cb4f430df80f8a21e358ad40ae59c89 (patch)
treeb9551474bedb7fdd6a9a4e37bc641bd8d0cfcfb4 /aesxx/utils
parentcmake: upgrade cmake-common, introduce new features, etc. (diff)
downloadaes-tools-6cac3cb57cb4f430df80f8a21e358ad40ae59c89.tar.gz
aes-tools-6cac3cb57cb4f430df80f8a21e358ad40ae59c89.zip
add .clang-format
Diffstat (limited to 'aesxx/utils')
-rw-r--r--aesxx/utils/block_cmd_parser.hpp74
-rw-r--r--aesxx/utils/block_dumper.hpp60
-rw-r--r--aesxx/utils/block_input.hpp12
-rw-r--r--aesxx/utils/data_parsers.hpp12
-rw-r--r--aesxx/utils/decrypt_block.cpp247
-rw-r--r--aesxx/utils/decrypt_bmp.cpp83
-rw-r--r--aesxx/utils/decrypt_file.cpp79
-rw-r--r--aesxx/utils/encrypt_block.cpp248
-rw-r--r--aesxx/utils/encrypt_bmp.cpp79
-rw-r--r--aesxx/utils/encrypt_file.cpp79
-rw-r--r--aesxx/utils/file_cmd_parser.hpp71
-rw-r--r--aesxx/utils/helpers/bmp.hpp71
-rw-r--r--aesxx/utils/helpers/command_line.hpp113
-rw-r--r--aesxx/utils/helpers/file.hpp84
14 files changed, 538 insertions, 774 deletions
diff --git a/aesxx/utils/block_cmd_parser.hpp b/aesxx/utils/block_cmd_parser.hpp
index 58f86d8..3ae0eac 100644
--- a/aesxx/utils/block_cmd_parser.hpp
+++ b/aesxx/utils/block_cmd_parser.hpp
@@ -19,8 +19,7 @@
#include <utility>
#include <vector>
-class BlockSettings : public command_line::SettingsParser
-{
+class BlockSettings : public command_line::SettingsParser {
public:
aes::Algorithm algorithm = AES_AES128;
aes::Mode mode = AES_ECB;
@@ -30,65 +29,52 @@ public:
std::vector<Input> 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<aes::Algorithm>(&algorithm)
- ->required()
- ->value_name("NAME"),
- "set algorithm")
- ("mode,m",
- boost::program_options::value<aes::Mode>(&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<std::vector<std::string>>(&args),
- "shouldn't be visible");
+ explicit BlockSettings(const std::string& argv0) : SettingsParser{argv0} {
+ visible.add_options()(
+ "verbose,v", boost::program_options::bool_switch(&verbose), "enable verbose output");
+ visible.add_options()("algorithm,a",
+ boost::program_options::value<aes::Algorithm>(&algorithm)
+ ->required()
+ ->value_name("NAME"),
+ "set algorithm");
+ visible.add_options()(
+ "mode,m",
+ boost::program_options::value<aes::Mode>(&mode)->required()->value_name("MODE"),
+ "set mode of operation");
+ visible.add_options()("use-boxes,b",
+ boost::program_options::bool_switch(&use_boxes),
+ "use the \"boxes\" interface");
+ hidden.add_options()("args",
+ boost::program_options::value<std::vector<std::string>>(&args),
+ "shouldn't be visible");
positional.add("args", -1);
}
- const char* get_short_description() const override
- {
+ 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
- {
+ void parse(int argc, char* argv[]) override {
SettingsParser::parse(argc, argv);
- parse_inputs(std::deque<std::string>{
- std::make_move_iterator(args.begin()),
- std::make_move_iterator(args.end())});
+ parse_inputs(std::deque<std::string>{std::make_move_iterator(args.begin()),
+ std::make_move_iterator(args.end())});
}
private:
- void parse_inputs(std::deque<std::string>&& src)
- {
+ void parse_inputs(std::deque<std::string>&& src) {
while (!src.empty())
inputs.emplace_back(parse_input(src));
}
- Input parse_input(std::deque<std::string>& src) const
- {
+ Input parse_input(std::deque<std::string>& src) const {
std::string key{std::move(src.front())};
src.pop_front();
std::string iv;
- if (aes::mode_requires_init_vector(mode))
- {
- if (src.empty())
- {
+ if (aes::mode_requires_init_vector(mode)) {
+ if (src.empty()) {
throw boost::program_options::error{
"an initialization vector is required for the selected mode of operation"};
}
@@ -104,12 +90,10 @@ private:
return {key, std::move(blocks)};
}
- static std::vector<std::string> parse_blocks(std::deque<std::string>& src)
- {
+ static std::vector<std::string> parse_blocks(std::deque<std::string>& src) {
std::vector<std::string> blocks;
- while (!src.empty())
- {
+ while (!src.empty()) {
std::string block{std::move(src.front())};
src.pop_front();
if (block == "--")
diff --git a/aesxx/utils/block_dumper.hpp b/aesxx/utils/block_dumper.hpp
index a07d855..226ba20 100644
--- a/aesxx/utils/block_dumper.hpp
+++ b/aesxx/utils/block_dumper.hpp
@@ -8,48 +8,38 @@
#include <aesxx/all.hpp>
#include <cstdlib>
-
#include <iostream>
#include <type_traits>
template <aes::Algorithm algorithm>
-void dump_block(
- const char* header,
- const typename aes::Types<algorithm>::Block& block)
-{
+void dump_block(const char* header, const typename aes::Types<algorithm>::Block& block) {
std::cout << header << ": " << aes::to_string<algorithm>(block) << "\n";
std::cout << aes::to_matrix_string<algorithm>(block) << "\n";
}
template <aes::Algorithm algorithm>
-void dump_plaintext(const typename aes::Types<algorithm>::Block& block)
-{
+void dump_plaintext(const typename aes::Types<algorithm>::Block& block) {
dump_block<algorithm>("Plaintext", block);
}
template <aes::Algorithm algorithm>
-void dump_key(const typename aes::Types<algorithm>::Key& key)
-{
+void dump_key(const typename aes::Types<algorithm>::Key& key) {
std::cout << "Key: " << aes::to_string<algorithm>(key) << "\n\n";
}
template <aes::Algorithm algorithm>
-void dump_ciphertext(const typename aes::Types<algorithm>::Block& ciphertext)
-{
+void dump_ciphertext(const typename aes::Types<algorithm>::Block& ciphertext) {
dump_block<algorithm>("Ciphertext", ciphertext);
}
template <aes::Algorithm algorithm>
-void dump_iv(const typename aes::Types<algorithm>::Block& iv)
-{
+void dump_iv(const typename aes::Types<algorithm>::Block& iv) {
dump_block<algorithm>("Initialization vector", iv);
}
template <aes::Algorithm algorithm>
-void dump_round_keys(
- const char* header,
- 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 << 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";
@@ -57,49 +47,45 @@ void dump_round_keys(
}
template <aes::Algorithm algorithm>
-void dump_encryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys)
-{
+void dump_encryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) {
dump_round_keys<algorithm>("Encryption round keys", round_keys);
}
template <aes::Algorithm algorithm>
-void dump_decryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys)
-{
+void dump_decryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) {
dump_round_keys<algorithm>("Decryption round keys", round_keys);
}
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,
+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,
+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,
+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,
+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/aesxx/utils/block_input.hpp b/aesxx/utils/block_input.hpp
index f629062..1dd3f70 100644
--- a/aesxx/utils/block_input.hpp
+++ b/aesxx/utils/block_input.hpp
@@ -9,19 +9,13 @@
#include <utility>
#include <vector>
-class Input
-{
+class Input {
public:
Input(const std::string& key, const std::string& iv, std::vector<std::string>&& blocks)
- : key{key}
- , iv{iv}
- , blocks{std::move(blocks)}
- { }
+ : key{key}, iv{iv}, blocks{std::move(blocks)} {}
Input(const std::string& key, std::vector<std::string>&& blocks)
- : key{key}
- , blocks{std::move(blocks)}
- { }
+ : key{key}, blocks{std::move(blocks)} {}
const std::string key;
const std::string iv;
diff --git a/aesxx/utils/data_parsers.hpp b/aesxx/utils/data_parsers.hpp
index 1207c7c..a08f1bf 100644
--- a/aesxx/utils/data_parsers.hpp
+++ b/aesxx/utils/data_parsers.hpp
@@ -14,13 +14,11 @@
#include <string>
#include <unordered_map>
-inline std::istream& operator>>(std::istream& is, aes::Mode& dest)
-{
+inline std::istream& operator>>(std::istream& is, aes::Mode& dest) {
std::string src;
is >> src;
- static const std::unordered_map<std::string, aes::Mode> lookup_table =
- {
+ static const std::unordered_map<std::string, aes::Mode> lookup_table = {
{"ecb", AES_ECB},
{"cbc", AES_CBC},
{"cfb", AES_CFB},
@@ -37,13 +35,11 @@ inline std::istream& operator>>(std::istream& is, aes::Mode& dest)
return is;
}
-inline std::istream& operator>>(std::istream& is, aes::Algorithm& dest)
-{
+inline std::istream& operator>>(std::istream& is, aes::Algorithm& dest) {
std::string src;
is >> src;
- static const std::unordered_map<std::string, aes::Algorithm> lookup_table =
- {
+ static const std::unordered_map<std::string, aes::Algorithm> lookup_table = {
{"aes128", AES_AES128},
{"aes192", AES_AES192},
{"aes256", AES_AES256},
diff --git a/aesxx/utils/decrypt_block.cpp b/aesxx/utils/decrypt_block.cpp
index 02a3033..42e496e 100644
--- a/aesxx/utils/decrypt_block.cpp
+++ b/aesxx/utils/decrypt_block.cpp
@@ -16,198 +16,151 @@
#include <stdexcept>
#include <string>
-namespace
-{
- template <aes::Algorithm algorithm, aes::Mode mode>
- void decrypt_with_mode(
- const Input& input,
- bool verbose = false)
- {
- typename aes::Types<algorithm>::Block iv;
+namespace {
- if (aes::ModeRequiresInitVector<mode>())
- {
- aes::from_string<algorithm>(iv, input.iv);
- if (verbose)
- dump_iv<algorithm>(iv);
- }
+template <aes::Algorithm algorithm, aes::Mode mode>
+void decrypt_with_mode(const Input& input, bool verbose = false) {
+ typename aes::Types<algorithm>::Block iv;
- typename aes::Types<algorithm>::Key key;
- aes::from_string<algorithm>(key, input.key);
+ if (aes::ModeRequiresInitVector<mode>()) {
+ aes::from_string<algorithm>(iv, input.iv);
if (verbose)
- dump_key<algorithm>(key);
+ dump_iv<algorithm>(iv);
+ }
- aes::DecryptWrapper<algorithm, mode> decrypt{key, iv};
- if (verbose)
- dump_wrapper<algorithm, mode>(decrypt);
+ typename aes::Types<algorithm>::Key key;
+ aes::from_string<algorithm>(key, input.key);
+ if (verbose)
+ dump_key<algorithm>(key);
- for (const auto& block : input.blocks)
- {
- typename aes::Types<algorithm>::Block ciphertext, plaintext;
- aes::from_string<algorithm>(ciphertext, block);
+ aes::DecryptWrapper<algorithm, mode> decrypt{key, iv};
+ if (verbose)
+ dump_wrapper<algorithm, mode>(decrypt);
- decrypt.decrypt_block(ciphertext, plaintext);
+ for (const auto& block : input.blocks) {
+ typename aes::Types<algorithm>::Block ciphertext, plaintext;
+ aes::from_string<algorithm>(ciphertext, block);
- if (verbose)
- {
- dump_ciphertext<algorithm>(ciphertext);
- dump_plaintext<algorithm>(plaintext);
- dump_next_iv<algorithm, mode>(decrypt);
- }
- else
- {
- std::cout << aes::to_string<algorithm>(plaintext) << '\n';
- }
+ decrypt.decrypt_block(ciphertext, plaintext);
+
+ if (verbose) {
+ dump_ciphertext<algorithm>(ciphertext);
+ dump_plaintext<algorithm>(plaintext);
+ dump_next_iv<algorithm, mode>(decrypt);
+ } else {
+ std::cout << aes::to_string<algorithm>(plaintext) << '\n';
}
}
+}
- template <aes::Algorithm algorithm>
- void decrypt_with_algorithm(
- aes::Mode mode,
- const Input& input,
- bool verbose = false)
- {
- switch (mode)
- {
- case AES_ECB:
- decrypt_with_mode<algorithm, AES_ECB>(input, verbose);
- break;
+template <aes::Algorithm algorithm>
+void decrypt_with_algorithm(aes::Mode mode, const Input& input, bool verbose = false) {
+ switch (mode) {
+ case AES_ECB:
+ decrypt_with_mode<algorithm, AES_ECB>(input, verbose);
+ break;
- case AES_CBC:
- decrypt_with_mode<algorithm, AES_CBC>(input, verbose);
- break;
+ case AES_CBC:
+ decrypt_with_mode<algorithm, AES_CBC>(input, verbose);
+ break;
- case AES_CFB:
- decrypt_with_mode<algorithm, AES_CFB>(input, verbose);
- break;
+ case AES_CFB:
+ decrypt_with_mode<algorithm, AES_CFB>(input, verbose);
+ break;
- case AES_OFB:
- decrypt_with_mode<algorithm, AES_OFB>(input, verbose);
- break;
+ case AES_OFB:
+ decrypt_with_mode<algorithm, AES_OFB>(input, verbose);
+ break;
- case AES_CTR:
- decrypt_with_mode<algorithm, AES_CTR>(input, verbose);
- break;
+ case AES_CTR:
+ decrypt_with_mode<algorithm, AES_CTR>(input, verbose);
+ break;
- default:
- throw std::runtime_error("the selected mode of operation is not implemented");
- break;
- }
+ default:
+ throw std::runtime_error("the selected mode of operation is not implemented");
+ break;
}
+}
- void decrypt_using_cxx_api(
- aes::Algorithm algorithm,
- aes::Mode mode,
- const Input& input,
- bool verbose = false)
- {
- switch (algorithm)
- {
- case AES_AES128:
- decrypt_with_algorithm<AES_AES128>(mode, input, verbose);
- break;
+void decrypt_using_cxx_api(aes::Algorithm algorithm,
+ aes::Mode mode,
+ const Input& input,
+ bool verbose = false) {
+ switch (algorithm) {
+ case AES_AES128:
+ decrypt_with_algorithm<AES_AES128>(mode, input, verbose);
+ break;
- case AES_AES192:
- decrypt_with_algorithm<AES_AES192>(mode, input, verbose);
- break;
+ case AES_AES192:
+ decrypt_with_algorithm<AES_AES192>(mode, input, verbose);
+ break;
- case AES_AES256:
- decrypt_with_algorithm<AES_AES256>(mode, input, verbose);
- break;
+ case AES_AES256:
+ decrypt_with_algorithm<AES_AES256>(mode, input, verbose);
+ break;
- default:
- throw std::runtime_error("the selected algorithm is not implemented");
- break;
- }
+ default:
+ throw std::runtime_error("the selected algorithm is not implemented");
+ break;
}
+}
- void decrypt_using_particular_box(
- aes::Box& box,
- const std::vector<std::string>& blocks)
- {
- for (const auto& block : blocks)
- {
- aes::Box::Block ciphertext;
- box.parse_block(ciphertext, block);
+void decrypt_using_particular_box(aes::Box& box, const std::vector<std::string>& blocks) {
+ for (const auto& block : blocks) {
+ aes::Box::Block ciphertext;
+ box.parse_block(ciphertext, block);
- aes::Box::Block plaintext;
- box.decrypt_block(ciphertext, plaintext);
- std::cout << box.format_block(plaintext) << '\n';
- }
+ aes::Box::Block plaintext;
+ box.decrypt_block(ciphertext, plaintext);
+ std::cout << box.format_block(plaintext) << '\n';
}
+}
- void decrypt_using_boxes(
- aes::Algorithm algorithm,
- aes::Mode mode,
- const Input& input)
- {
- aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, input.key);
+void decrypt_using_boxes(aes::Algorithm algorithm, aes::Mode mode, const Input& input) {
+ aes::Box::Key key;
+ aes::Box::parse_key(key, algorithm, input.key);
- if (aes::mode_requires_init_vector(mode))
- {
- aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, input.iv);
- aes::Box box{algorithm, key, mode, iv};
+ if (aes::mode_requires_init_vector(mode)) {
+ aes::Box::Block iv;
+ aes::Box::parse_block(iv, algorithm, input.iv);
+ aes::Box box{algorithm, key, mode, iv};
- decrypt_using_particular_box(box, input.blocks);
- }
- else
- {
- aes::Box box{algorithm, key};
- decrypt_using_particular_box(box, input.blocks);
- }
+ decrypt_using_particular_box(box, input.blocks);
+ } else {
+ aes::Box box{algorithm, key};
+ decrypt_using_particular_box(box, input.blocks);
}
}
-int main(int argc, char** argv)
-{
- try
- {
+} // namespace
+
+int main(int argc, char** argv) {
+ try {
BlockSettings settings{argv[0]};
- try
- {
+ try {
settings.parse(argc, argv);
- }
- catch (const boost::program_options::error& e)
- {
+ } catch (const boost::program_options::error& e) {
settings.usage_error(e);
return 1;
}
- if (settings.exit_with_usage)
- {
+ if (settings.exit_with_usage) {
settings.usage();
return 0;
}
- for (const auto& input : settings.inputs)
- {
- if (settings.use_boxes)
- {
- decrypt_using_boxes(
- settings.algorithm,
- settings.mode,
- input);
- }
- else
- {
- decrypt_using_cxx_api(
- settings.algorithm,
- settings.mode,
- input,
- settings.verbose);
+ for (const auto& input : settings.inputs) {
+ if (settings.use_boxes) {
+ decrypt_using_boxes(settings.algorithm, settings.mode, input);
+ } else {
+ decrypt_using_cxx_api(settings.algorithm, settings.mode, input, settings.verbose);
}
}
- }
- catch (const aes::Error& e)
- {
+ } catch (const aes::Error& e) {
std::cerr << e;
return 1;
- }
- catch (const std::exception& e)
- {
+ } catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
diff --git a/aesxx/utils/decrypt_bmp.cpp b/aesxx/utils/decrypt_bmp.cpp
index 9f9cd52..86f6b7d 100644
--- a/aesxx/utils/decrypt_bmp.cpp
+++ b/aesxx/utils/decrypt_bmp.cpp
@@ -9,83 +9,66 @@
#include <aesxx/all.hpp>
-#include <windows.h>
-
#include <boost/program_options.hpp>
+#include <windows.h>
+
#include <exception>
#include <iostream>
#include <string>
-namespace
-{
- void decrypt_bmp(
- aes::Box& box,
- const std::string& ciphertext_path,
- const std::string& plaintext_path)
- {
- bmp::BmpFile bmp{file::read_file(ciphertext_path)};
- bmp.replace_pixels(box.decrypt_buffer(
- bmp.get_pixels(),
- bmp.get_pixels_size()));
- file::write_file(plaintext_path, bmp.get_buffer(), bmp.get_size());
- }
+namespace {
- void decrypt_bmp(const FileSettings& settings)
- {
- const auto& algorithm = settings.algorithm;
- const auto& mode = settings.mode;
+void decrypt_bmp(aes::Box& box,
+ const std::string& ciphertext_path,
+ const std::string& plaintext_path) {
+ bmp::BmpFile bmp{file::read_file(ciphertext_path)};
+ bmp.replace_pixels(box.decrypt_buffer(bmp.get_pixels(), bmp.get_pixels_size()));
+ file::write_file(plaintext_path, bmp.get_buffer(), bmp.get_size());
+}
- aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, settings.key);
+void decrypt_bmp(const FileSettings& settings) {
+ const auto& algorithm = settings.algorithm;
+ const auto& mode = settings.mode;
- if (aes::mode_requires_init_vector(mode))
- {
- aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, settings.iv);
+ aes::Box::Key key;
+ aes::Box::parse_key(key, algorithm, settings.key);
- aes::Box box{algorithm, key, mode, iv};
- decrypt_bmp(box, settings.input_path, settings.output_path);
- }
- else
- {
- aes::Box box{algorithm, key};
- decrypt_bmp(box, settings.input_path, settings.output_path);
- }
+ if (aes::mode_requires_init_vector(mode)) {
+ aes::Box::Block iv;
+ aes::Box::parse_block(iv, algorithm, settings.iv);
+
+ aes::Box box{algorithm, key, mode, iv};
+ decrypt_bmp(box, settings.input_path, settings.output_path);
+ } else {
+ aes::Box box{algorithm, key};
+ decrypt_bmp(box, settings.input_path, settings.output_path);
}
}
-int main(int argc, char** argv)
-{
- try
- {
+} // namespace
+
+int main(int argc, char** argv) {
+ try {
FileSettings settings{argv[0]};
- try
- {
+ try {
settings.parse(argc, argv);
- }
- catch (const boost::program_options::error& e)
- {
+ } catch (const boost::program_options::error& e) {
settings.usage_error(e);
return 1;
}
- if (settings.exit_with_usage)
- {
+ if (settings.exit_with_usage) {
settings.usage();
return 0;
}
decrypt_bmp(settings);
- }
- catch (const aes::Error& e)
- {
+ } catch (const aes::Error& e) {
std::cerr << e;
return 1;
- }
- catch (const std::exception& e)
- {
+ } catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
diff --git a/aesxx/utils/decrypt_file.cpp b/aesxx/utils/decrypt_file.cpp
index 6da3d17..727362c 100644
--- a/aesxx/utils/decrypt_file.cpp
+++ b/aesxx/utils/decrypt_file.cpp
@@ -15,75 +15,58 @@
#include <string>
#include <vector>
-namespace
-{
- void decrypt_file(
- aes::Box& box,
- const std::string& ciphertext_path,
- const std::string& plaintext_path)
- {
- const auto ciphertext_buf = file::read_file(ciphertext_path);
- const auto plaintext_buf = box.decrypt_buffer(
- ciphertext_buf.data(),
- ciphertext_buf.size());
- file::write_file(plaintext_path, plaintext_buf);
- }
+namespace {
- void decrypt_file(const FileSettings& settings)
- {
- const auto& algorithm = settings.algorithm;
- const auto& mode = settings.mode;
+void decrypt_file(aes::Box& box,
+ const std::string& ciphertext_path,
+ const std::string& plaintext_path) {
+ const auto ciphertext_buf = file::read_file(ciphertext_path);
+ const auto plaintext_buf = box.decrypt_buffer(ciphertext_buf.data(), ciphertext_buf.size());
+ file::write_file(plaintext_path, plaintext_buf);
+}
- aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, settings.key);
+void decrypt_file(const FileSettings& settings) {
+ const auto& algorithm = settings.algorithm;
+ const auto& mode = settings.mode;
- if (aes::mode_requires_init_vector(mode))
- {
- aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, settings.iv);
+ aes::Box::Key key;
+ aes::Box::parse_key(key, algorithm, settings.key);
- aes::Box box{algorithm, key, mode, iv};
- decrypt_file(box, settings.input_path, settings.output_path);
- }
- else
- {
- aes::Box box{algorithm, key};
- decrypt_file(box, settings.input_path, settings.output_path);
- }
+ if (aes::mode_requires_init_vector(mode)) {
+ aes::Box::Block iv;
+ aes::Box::parse_block(iv, algorithm, settings.iv);
+
+ aes::Box box{algorithm, key, mode, iv};
+ decrypt_file(box, settings.input_path, settings.output_path);
+ } else {
+ aes::Box box{algorithm, key};
+ decrypt_file(box, settings.input_path, settings.output_path);
}
}
-int main(int argc, char** argv)
-{
- try
- {
+} // namespace
+
+int main(int argc, char** argv) {
+ try {
FileSettings settings{argv[0]};
- try
- {
+ try {
settings.parse(argc, argv);
- }
- catch (const boost::program_options::error& e)
- {
+ } catch (const boost::program_options::error& e) {
settings.usage_error(e);
return 1;
}
- if (settings.exit_with_usage)
- {
+ if (settings.exit_with_usage) {
settings.usage();
return 0;
}
decrypt_file(settings);
- }
- catch (const aes::Error& e)
- {
+ } catch (const aes::Error& e) {
std::cerr << e;
return 1;
- }
- catch (const std::exception& e)
- {
+ } catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp
index 0b87364..b6bdca9 100644
--- a/aesxx/utils/encrypt_block.cpp
+++ b/aesxx/utils/encrypt_block.cpp
@@ -16,198 +16,152 @@
#include <stdexcept>
#include <string>
-namespace
-{
- template <aes::Algorithm algorithm, aes::Mode mode>
- void encrypt_with_mode(
- const Input& input,
- bool verbose = false)
- {
- typename aes::Types<algorithm>::Block iv;
+namespace {
- if (aes::ModeRequiresInitVector<mode>::value)
- {
- aes::from_string<algorithm>(iv, input.iv);
- if (verbose)
- dump_iv<algorithm>(iv);
- }
+template <aes::Algorithm algorithm, aes::Mode mode>
+void encrypt_with_mode(const Input& input, bool verbose = false) {
+ typename aes::Types<algorithm>::Block iv;
- typename aes::Types<algorithm>::Key key;
- aes::from_string<algorithm>(key, input.key);
+ if (aes::ModeRequiresInitVector<mode>::value) {
+ aes::from_string<algorithm>(iv, input.iv);
if (verbose)
- dump_key<algorithm>(key);
+ dump_iv<algorithm>(iv);
+ }
- aes::EncryptWrapper<algorithm, mode> encrypt{key, iv};
- if (verbose)
- dump_wrapper<algorithm, mode>(encrypt);
+ typename aes::Types<algorithm>::Key key;
+ aes::from_string<algorithm>(key, input.key);
+ if (verbose)
+ dump_key<algorithm>(key);
- for (const auto& input_block_string : input.blocks)
- {
- typename aes::Types<algorithm>::Block plaintext, ciphertext;
- aes::from_string<algorithm>(plaintext, input_block_string);
+ aes::EncryptWrapper<algorithm, mode> encrypt{key, iv};
+ if (verbose)
+ dump_wrapper<algorithm, mode>(encrypt);
- encrypt.encrypt_block(plaintext, ciphertext);
+ for (const auto& input_block_string : input.blocks) {
+ typename aes::Types<algorithm>::Block plaintext, ciphertext;
+ aes::from_string<algorithm>(plaintext, input_block_string);
- if (verbose)
- {
- dump_plaintext<algorithm>(plaintext);
- dump_ciphertext<algorithm>(ciphertext);
- dump_next_iv<algorithm, mode>(encrypt);
- }
- else
- {
- std::cout << aes::to_string<algorithm>(ciphertext) << '\n';
- }
+ encrypt.encrypt_block(plaintext, ciphertext);
+
+ if (verbose) {
+ dump_plaintext<algorithm>(plaintext);
+ dump_ciphertext<algorithm>(ciphertext);
+ dump_next_iv<algorithm, mode>(encrypt);
+ } else {
+ std::cout << aes::to_string<algorithm>(ciphertext) << '\n';
}
}
+}
- template <aes::Algorithm algorithm>
- void encrypt_with_algorithm(
- aes::Mode mode,
- const Input& input,
- bool verbose = false)
- {
- switch (mode)
- {
- case AES_ECB:
- encrypt_with_mode<algorithm, AES_ECB>(input, verbose);
- break;
+template <aes::Algorithm algorithm>
+void encrypt_with_algorithm(aes::Mode mode, const Input& input, bool verbose = false) {
+ switch (mode) {
+ case AES_ECB:
+ encrypt_with_mode<algorithm, AES_ECB>(input, verbose);
+ break;
- case AES_CBC:
- encrypt_with_mode<algorithm, AES_CBC>(input, verbose);
- break;
+ case AES_CBC:
+ encrypt_with_mode<algorithm, AES_CBC>(input, verbose);
+ break;
- case AES_CFB:
- encrypt_with_mode<algorithm, AES_CFB>(input, verbose);
- break;
+ case AES_CFB:
+ encrypt_with_mode<algorithm, AES_CFB>(input, verbose);
+ break;
- case AES_OFB:
- encrypt_with_mode<algorithm, AES_OFB>(input, verbose);
- break;
+ case AES_OFB:
+ encrypt_with_mode<algorithm, AES_OFB>(input, verbose);
+ break;
- case AES_CTR:
- encrypt_with_mode<algorithm, AES_CTR>(input, verbose);
- break;
+ case AES_CTR:
+ encrypt_with_mode<algorithm, AES_CTR>(input, verbose);
+ break;
- default:
- throw std::runtime_error("the selected mode of operation is not implemented");
- break;
- }
+ default:
+ throw std::runtime_error("the selected mode of operation is not implemented");
+ break;
}
+}
- void encrypt_using_cxx_api(
- aes::Algorithm algorithm,
- aes::Mode mode,
- const Input& input,
- bool verbose = false)
- {
- switch (algorithm)
- {
- case AES_AES128:
- encrypt_with_algorithm<AES_AES128>(mode, input, verbose);
- break;
+void encrypt_using_cxx_api(aes::Algorithm algorithm,
+ aes::Mode mode,
+ const Input& input,
+ bool verbose = false) {
+ switch (algorithm) {
+ case AES_AES128:
+ encrypt_with_algorithm<AES_AES128>(mode, input, verbose);
+ break;
- case AES_AES192:
- encrypt_with_algorithm<AES_AES192>(mode, input, verbose);
- break;
+ case AES_AES192:
+ encrypt_with_algorithm<AES_AES192>(mode, input, verbose);
+ break;
- case AES_AES256:
- encrypt_with_algorithm<AES_AES256>(mode, input, verbose);
- break;
+ case AES_AES256:
+ encrypt_with_algorithm<AES_AES256>(mode, input, verbose);
+ break;
- default:
- throw std::runtime_error("the selected algorithm is not implemented");
- break;
- }
+ default:
+ throw std::runtime_error("the selected algorithm is not implemented");
+ break;
}
+}
- void encrypt_using_particular_box(
- aes::Box& box,
- const std::vector<std::string>& input_block_strings)
- {
- for (const auto& input_block_string : input_block_strings)
- {
- aes::Box::Block plaintext;
- box.parse_block(plaintext, input_block_string);
+void encrypt_using_particular_box(aes::Box& box,
+ const std::vector<std::string>& input_block_strings) {
+ for (const auto& input_block_string : input_block_strings) {
+ aes::Box::Block plaintext;
+ box.parse_block(plaintext, input_block_string);
- aes::Box::Block ciphertext;
- box.encrypt_block(plaintext, ciphertext);
- std::cout << box.format_block(ciphertext) << '\n';
- }
+ aes::Box::Block ciphertext;
+ box.encrypt_block(plaintext, ciphertext);
+ std::cout << box.format_block(ciphertext) << '\n';
}
+}
- void encrypt_using_boxes(
- aes::Algorithm algorithm,
- aes::Mode mode,
- const Input& input)
- {
- aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, input.key);
+void encrypt_using_boxes(aes::Algorithm algorithm, aes::Mode mode, const Input& input) {
+ aes::Box::Key key;
+ aes::Box::parse_key(key, algorithm, input.key);
- if (aes::mode_requires_init_vector(mode))
- {
- aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, input.iv);
- aes::Box box{algorithm, key, mode, iv};
+ if (aes::mode_requires_init_vector(mode)) {
+ aes::Box::Block iv;
+ aes::Box::parse_block(iv, algorithm, input.iv);
+ aes::Box box{algorithm, key, mode, iv};
- encrypt_using_particular_box(box, input.blocks);
- }
- else
- {
- aes::Box box{algorithm, key};
- encrypt_using_particular_box(box, input.blocks);
- }
+ encrypt_using_particular_box(box, input.blocks);
+ } else {
+ aes::Box box{algorithm, key};
+ encrypt_using_particular_box(box, input.blocks);
}
}
-int main(int argc, char** argv)
-{
- try
- {
+} // namespace
+
+int main(int argc, char** argv) {
+ try {
BlockSettings settings{argv[0]};
- try
- {
+ try {
settings.parse(argc, argv);
- }
- catch (const boost::program_options::error& e)
- {
+ } catch (const boost::program_options::error& e) {
settings.usage_error(e);
return 1;
}
- if (settings.exit_with_usage)
- {
+ if (settings.exit_with_usage) {
settings.usage();
return 0;
}
- for (const auto& input : settings.inputs)
- {
- if (settings.use_boxes)
- {
- encrypt_using_boxes(
- settings.algorithm,
- settings.mode,
- input);
- }
- else
- {
- encrypt_using_cxx_api(
- settings.algorithm,
- settings.mode,
- input,
- settings.verbose);
+ for (const auto& input : settings.inputs) {
+ if (settings.use_boxes) {
+ encrypt_using_boxes(settings.algorithm, settings.mode, input);
+ } else {
+ encrypt_using_cxx_api(settings.algorithm, settings.mode, input, settings.verbose);
}
}
- }
- catch (const aes::Error& e)
- {
+ } catch (const aes::Error& e) {
std::cerr << e;
return 1;
- }
- catch (const std::exception& e)
- {
+ } catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
diff --git a/aesxx/utils/encrypt_bmp.cpp b/aesxx/utils/encrypt_bmp.cpp
index 7f1a890..01f3a78 100644
--- a/aesxx/utils/encrypt_bmp.cpp
+++ b/aesxx/utils/encrypt_bmp.cpp
@@ -15,75 +15,58 @@
#include <iostream>
#include <string>
-namespace
-{
- void encrypt_bmp(
- aes::Box& box,
- const std::string& plaintext_path,
- const std::string& ciphertext_path)
- {
- bmp::BmpFile bmp{file::read_file(plaintext_path)};
- bmp.replace_pixels(box.encrypt_buffer(
- bmp.get_pixels(),
- bmp.get_pixels_size()));
- file::write_file(ciphertext_path, bmp.get_buffer(), bmp.get_size());
- }
+namespace {
- void encrypt_bmp(const FileSettings& settings)
- {
- const auto& algorithm = settings.algorithm;
- const auto& mode = settings.mode;
+void encrypt_bmp(aes::Box& box,
+ const std::string& plaintext_path,
+ const std::string& ciphertext_path) {
+ bmp::BmpFile bmp{file::read_file(plaintext_path)};
+ bmp.replace_pixels(box.encrypt_buffer(bmp.get_pixels(), bmp.get_pixels_size()));
+ file::write_file(ciphertext_path, bmp.get_buffer(), bmp.get_size());
+}
- aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, settings.key);
+void encrypt_bmp(const FileSettings& settings) {
+ const auto& algorithm = settings.algorithm;
+ const auto& mode = settings.mode;
- if (aes::mode_requires_init_vector(mode))
- {
- aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, settings.iv);
+ aes::Box::Key key;
+ aes::Box::parse_key(key, algorithm, settings.key);
- aes::Box box{algorithm, key, mode, iv};
- encrypt_bmp(box, settings.input_path, settings.output_path);
- }
- else
- {
- aes::Box box{algorithm, key};
- encrypt_bmp(box, settings.input_path, settings.output_path);
- }
+ if (aes::mode_requires_init_vector(mode)) {
+ aes::Box::Block iv;
+ aes::Box::parse_block(iv, algorithm, settings.iv);
+
+ aes::Box box{algorithm, key, mode, iv};
+ encrypt_bmp(box, settings.input_path, settings.output_path);
+ } else {
+ aes::Box box{algorithm, key};
+ encrypt_bmp(box, settings.input_path, settings.output_path);
}
}
-int main(int argc, char** argv)
-{
- try
- {
+} // namespace
+
+int main(int argc, char** argv) {
+ try {
FileSettings settings{argv[0]};
- try
- {
+ try {
settings.parse(argc, argv);
- }
- catch (const boost::program_options::error& e)
- {
+ } catch (const boost::program_options::error& e) {
settings.usage_error(e);
return 1;
}
- if (settings.exit_with_usage)
- {
+ if (settings.exit_with_usage) {
settings.usage();
return 0;
}
encrypt_bmp(settings);
- }
- catch (const aes::Error& e)
- {
+ } catch (const aes::Error& e) {
std::cerr << e;
return 1;
- }
- catch (const std::exception& e)
- {
+ } catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
diff --git a/aesxx/utils/encrypt_file.cpp b/aesxx/utils/encrypt_file.cpp
index 570bc60..4a774fd 100644
--- a/aesxx/utils/encrypt_file.cpp
+++ b/aesxx/utils/encrypt_file.cpp
@@ -15,75 +15,58 @@
#include <string>
#include <vector>
-namespace
-{
- void encrypt_file(
- aes::Box& box,
- const std::string& plaintext_path,
- const std::string& ciphertext_path)
- {
- const auto plaintext_buf = file::read_file(plaintext_path);
- const auto ciphertext_buf = box.encrypt_buffer(
- plaintext_buf.data(),
- plaintext_buf.size());
- file::write_file(ciphertext_path, ciphertext_buf);
- }
+namespace {
- void encrypt_file(const FileSettings& settings)
- {
- const auto& algorithm = settings.algorithm;
- const auto& mode = settings.mode;
+void encrypt_file(aes::Box& box,
+ const std::string& plaintext_path,
+ const std::string& ciphertext_path) {
+ const auto plaintext_buf = file::read_file(plaintext_path);
+ const auto ciphertext_buf = box.encrypt_buffer(plaintext_buf.data(), plaintext_buf.size());
+ file::write_file(ciphertext_path, ciphertext_buf);
+}
- aes::Box::Key key;
- aes::Box::parse_key(key, algorithm, settings.key);
+void encrypt_file(const FileSettings& settings) {
+ const auto& algorithm = settings.algorithm;
+ const auto& mode = settings.mode;
- if (aes::mode_requires_init_vector(mode))
- {
- aes::Box::Block iv;
- aes::Box::parse_block(iv, algorithm, settings.iv);
+ aes::Box::Key key;
+ aes::Box::parse_key(key, algorithm, settings.key);
- aes::Box box{algorithm, key, mode, iv};
- encrypt_file(box, settings.input_path, settings.output_path);
- }
- else
- {
- aes::Box box{algorithm, key};
- encrypt_file(box, settings.input_path, settings.output_path);
- }
+ if (aes::mode_requires_init_vector(mode)) {
+ aes::Box::Block iv;
+ aes::Box::parse_block(iv, algorithm, settings.iv);
+
+ aes::Box box{algorithm, key, mode, iv};
+ encrypt_file(box, settings.input_path, settings.output_path);
+ } else {
+ aes::Box box{algorithm, key};
+ encrypt_file(box, settings.input_path, settings.output_path);
}
}
-int main(int argc, char** argv)
-{
- try
- {
+} // namespace
+
+int main(int argc, char** argv) {
+ try {
FileSettings settings{argv[0]};
- try
- {
+ try {
settings.parse(argc, argv);
- }
- catch (const boost::program_options::error& e)
- {
+ } catch (const boost::program_options::error& e) {
settings.usage_error(e);
return 1;
}
- if (settings.exit_with_usage)
- {
+ if (settings.exit_with_usage) {
settings.usage();
return 0;
}
encrypt_file(settings);
- }
- catch (const aes::Error& e)
- {
+ } catch (const aes::Error& e) {
std::cerr << e;
return 1;
- }
- catch (const std::exception& e)
- {
+ } catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
diff --git a/aesxx/utils/file_cmd_parser.hpp b/aesxx/utils/file_cmd_parser.hpp
index e199409..4a504c8 100644
--- a/aesxx/utils/file_cmd_parser.hpp
+++ b/aesxx/utils/file_cmd_parser.hpp
@@ -17,8 +17,7 @@
#include <string>
#include <utility>
-class FileSettings : public command_line::SettingsParser
-{
+class FileSettings : public command_line::SettingsParser {
public:
aes::Algorithm algorithm = AES_AES128;
aes::Mode mode = AES_ECB;
@@ -28,54 +27,44 @@ public:
std::string key;
std::string iv;
- explicit FileSettings(const std::string& argv0)
- : SettingsParser{argv0}
- {
- visible.add_options()
- ("algorithm,a",
- boost::program_options::value<aes::Algorithm>(&algorithm)
- ->required()
- ->value_name("NAME"),
- "set algorithm")
- ("mode,m",
- boost::program_options::value<aes::Mode>(&mode)
- ->required()
- ->value_name("MODE"),
- "set mode of operation")
- ("key,k",
- boost::program_options::value<std::string>(&key)
- ->required()
- ->value_name("KEY"),
- "set encryption key")
- ("iv,v",
- boost::program_options::value<std::string>(&iv)
- ->value_name("BLOCK"),
- "set initialization vector")
- ("input,i",
- boost::program_options::value<std::string>(&input_path)
- ->required()
- ->value_name("PATH"),
- "set input file path")
- ("output,o",
- boost::program_options::value<std::string>(&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<aes::Algorithm>(&algorithm)
+ ->required()
+ ->value_name("NAME"),
+ "set algorithm");
+ visible.add_options()(
+ "mode,m",
+ boost::program_options::value<aes::Mode>(&mode)->required()->value_name("MODE"),
+ "set mode of operation");
+ visible.add_options()(
+ "key,k",
+ boost::program_options::value<std::string>(&key)->required()->value_name("KEY"),
+ "set encryption key");
+ visible.add_options()("iv,v",
+ boost::program_options::value<std::string>(&iv)->value_name("BLOCK"),
+ "set initialization vector");
+ visible.add_options()(
+ "input,i",
+ boost::program_options::value<std::string>(&input_path)->required()->value_name("PATH"),
+ "set input file path");
+ visible.add_options()("output,o",
+ boost::program_options::value<std::string>(&output_path)
+ ->required()
+ ->value_name("PATH"),
+ "set output file path");
}
- const char* get_short_description() const override
- {
+ 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
- {
+ void parse(int argc, char** argv) override {
SettingsParser::parse(argc, argv);
- if (aes::mode_requires_init_vector(mode) && iv.empty())
- {
+ 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"};
}
diff --git a/aesxx/utils/helpers/bmp.hpp b/aesxx/utils/helpers/bmp.hpp
index b4b283b..c256a7a 100644
--- a/aesxx/utils/helpers/bmp.hpp
+++ b/aesxx/utils/helpers/bmp.hpp
@@ -7,51 +7,50 @@
#include <cstddef>
#include <cstring>
-
#include <string>
#include <utility>
#include <vector>
-namespace bmp
-{
- class BmpFile
- {
- public:
- BmpFile(std::vector<char>&& buffer)
- : buffer{std::move(buffer)}
- , header_size{extract_pixels_offset()}
- { }
+namespace bmp {
+
+class BmpFile {
+public:
+ BmpFile(std::vector<char>&& buffer)
+ : buffer{std::move(buffer)}, header_size{extract_pixels_offset()} {}
+
+ const void* get_buffer() const {
+ return buffer.data();
+ }
- const void* get_buffer() const { return buffer.data(); }
+ std::size_t get_size() const {
+ return buffer.size();
+ }
- std::size_t get_size() const { return buffer.size(); }
+ std::size_t get_header_size() const {
+ return header_size;
+ }
- std::size_t get_header_size() const { return header_size; }
+ const void* get_pixels() const {
+ return buffer.data() + get_header_size();
+ }
- const void* get_pixels() const
- {
- return buffer.data() + get_header_size();
- }
+ std::size_t get_pixels_size() const {
+ return get_size() - get_header_size();
+ }
- std::size_t get_pixels_size() const
- {
- return get_size() - get_header_size();
- }
+ void replace_pixels(std::vector<unsigned char>&& pixels) {
+ buffer.resize(get_header_size() + pixels.size());
+ std::memcpy(buffer.data() + get_header_size(), pixels.data(), pixels.size());
+ }
- void replace_pixels(std::vector<unsigned char>&& pixels)
- {
- buffer.resize(get_header_size() + pixels.size());
- std::memcpy(buffer.data() + get_header_size(), pixels.data(), pixels.size());
- }
+private:
+ std::size_t extract_pixels_offset() const {
+ const auto header = reinterpret_cast<const BITMAPFILEHEADER*>(get_buffer());
+ return header->bfOffBits;
+ }
- private:
- std::size_t extract_pixels_offset() const
- {
- const auto header = reinterpret_cast<const BITMAPFILEHEADER*>(get_buffer());
- return header->bfOffBits;
- }
+ std::vector<char> buffer;
+ std::size_t header_size;
+};
- std::vector<char> buffer;
- std::size_t header_size;
- };
-}
+} // namespace bmp
diff --git a/aesxx/utils/helpers/command_line.hpp b/aesxx/utils/helpers/command_line.hpp
index 2e4d803..9a2da05 100644
--- a/aesxx/utils/helpers/command_line.hpp
+++ b/aesxx/utils/helpers/command_line.hpp
@@ -13,75 +13,64 @@
#include <ostream>
#include <string>
-namespace command_line
-{
- class SettingsParser
- {
- public:
- explicit SettingsParser(const std::string& argv0)
- : prog_name{extract_filename(argv0)}
- {
- visible.add_options()
- ("help,h",
- "show this message and exit");
- }
+namespace command_line {
- virtual ~SettingsParser() = default;
+class SettingsParser {
+public:
+ explicit SettingsParser(const std::string& argv0) : prog_name{extract_filename(argv0)} {
+ visible.add_options()("help,h", "show this message and exit");
+ }
- virtual const char* get_short_description() const
- {
- return "[--option VALUE]...";
- }
+ virtual ~SettingsParser() = default;
- virtual void parse(int argc, char* argv[])
- {
- boost::program_options::options_description all;
- all.add(hidden).add(visible);
- boost::program_options::variables_map vm;
- boost::program_options::store(
- boost::program_options::command_line_parser{argc, argv}
- .options(all)
- .positional(positional)
- .run(),
- vm);
- if (vm.count("help"))
- exit_with_usage = true;
- else
- boost::program_options::notify(vm);
- }
+ virtual const char* get_short_description() const {
+ return "[--option VALUE]...";
+ }
- bool exit_with_usage = false;
+ virtual void parse(int argc, char* argv[]) {
+ boost::program_options::options_description all;
+ all.add(hidden).add(visible);
+ boost::program_options::variables_map vm;
+ boost::program_options::store(boost::program_options::command_line_parser{argc, argv}
+ .options(all)
+ .positional(positional)
+ .run(),
+ vm);
+ if (vm.count("help"))
+ exit_with_usage = true;
+ else
+ boost::program_options::notify(vm);
+ }
- void usage() const
- {
- std::cout << *this;
- }
+ bool exit_with_usage = false;
- void usage_error(const std::exception& e) const
- {
- std::cerr << "usage error: " << e.what() << '\n';
- std::cerr << *this;
- }
+ void usage() const {
+ std::cout << *this;
+ }
- protected:
- boost::program_options::options_description hidden;
- boost::program_options::options_description visible;
- boost::program_options::positional_options_description positional;
+ void usage_error(const std::exception& e) const {
+ std::cerr << "usage error: " << e.what() << '\n';
+ std::cerr << *this;
+ }
- private:
- static std::string extract_filename(const std::string& path)
- {
- return boost::filesystem::path{path}.filename().string();
- }
+protected:
+ boost::program_options::options_description hidden;
+ boost::program_options::options_description visible;
+ boost::program_options::positional_options_description positional;
- const std::string prog_name;
+private:
+ static std::string extract_filename(const std::string& path) {
+ return boost::filesystem::path{path}.filename().string();
+ }
- friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser)
- {
- const auto short_descr = parser.get_short_description();
- os << "usage: " << parser.prog_name << ' ' << short_descr << '\n';
- os << parser.visible;
- return os;
- }
- };
-}
+ const std::string prog_name;
+
+ friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser) {
+ const auto short_descr = parser.get_short_description();
+ os << "usage: " << parser.prog_name << ' ' << short_descr << '\n';
+ os << parser.visible;
+ return os;
+ }
+};
+
+} // namespace command_line
diff --git a/aesxx/utils/helpers/file.hpp b/aesxx/utils/helpers/file.hpp
index 48e4c51..76e3e96 100644
--- a/aesxx/utils/helpers/file.hpp
+++ b/aesxx/utils/helpers/file.hpp
@@ -4,7 +4,6 @@
// Distributed under the MIT License.
#include <cstddef>
-
#include <fstream>
#include <iterator>
#include <limits>
@@ -13,57 +12,46 @@
#include <type_traits>
#include <vector>
-namespace file
-{
- inline std::size_t cast_to_size_t(std::streamoff size)
- {
- if (size < 0)
- throw std::range_error{"file::cast_to_size_t: something went really wrong"};
- typedef std::make_unsigned<std::streamoff>::type unsigned_streamoff;
- if (static_cast<unsigned_streamoff>(size) > std::numeric_limits<std::size_t>::max())
- throw std::range_error{"file::cast_to_size_t: this file is too large"};
- return static_cast<std::size_t>(size);
- }
+namespace file {
- inline std::size_t get_file_size(const std::string& path)
- {
- std::ifstream ifs;
- ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
- ifs.open(path, std::ifstream::binary | std::ifstream::ate);
- return cast_to_size_t(ifs.tellg());
- }
+inline std::size_t cast_to_size_t(std::streamoff size) {
+ if (size < 0)
+ throw std::range_error{"file::cast_to_size_t: something went really wrong"};
+ typedef std::make_unsigned<std::streamoff>::type unsigned_streamoff;
+ if (static_cast<unsigned_streamoff>(size) > std::numeric_limits<std::size_t>::max())
+ throw std::range_error{"file::cast_to_size_t: this file is too large"};
+ return static_cast<std::size_t>(size);
+}
- inline std::vector<char> read_file(const std::string& path)
- {
- const auto size = get_file_size(path);
+inline std::size_t get_file_size(const std::string& path) {
+ std::ifstream ifs;
+ ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
+ ifs.open(path, std::ifstream::binary | std::ifstream::ate);
+ return cast_to_size_t(ifs.tellg());
+}
- std::ifstream ifs;
- ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
- ifs.open(path, std::ifstream::binary);
+inline std::vector<char> read_file(const std::string& path) {
+ const auto size = get_file_size(path);
- std::vector<char> src_buf;
- src_buf.reserve(size);
- src_buf.assign(
- std::istreambuf_iterator<char>{ifs},
- std::istreambuf_iterator<char>{});
- return src_buf;
- }
+ std::ifstream ifs;
+ ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
+ ifs.open(path, std::ifstream::binary);
- inline void write_file(
- const std::string& path,
- const void* buffer,
- const std::size_t size)
- {
- std::ofstream ofs;
- ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
- ofs.open(path, std::ofstream::binary);
- ofs.write(reinterpret_cast<const char*>(buffer), size);
- }
+ std::vector<char> src_buf;
+ src_buf.reserve(size);
+ src_buf.assign(std::istreambuf_iterator<char>{ifs}, std::istreambuf_iterator<char>{});
+ return src_buf;
+}
- inline void write_file(
- const std::string& path,
- const std::vector<unsigned char>& src)
- {
- write_file(path, src.data(), src.size());
- }
+inline void write_file(const std::string& path, const void* buffer, const std::size_t size) {
+ std::ofstream ofs;
+ ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
+ ofs.open(path, std::ofstream::binary);
+ ofs.write(reinterpret_cast<const char*>(buffer), size);
}
+
+inline void write_file(const std::string& path, const std::vector<unsigned char>& src) {
+ write_file(path, src.data(), src.size());
+}
+
+} // namespace file