From 80452ac0abc6494000a9bbad8d7dccfd72ea85f2 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 13 Jun 2026 15:42:15 +0200 Subject: aesxx: upgrade to std::string_view, std::format & --- aesxx/utils/CMakeLists.txt | 3 +-- aesxx/utils/block_cmd_parser.hpp | 3 ++- aesxx/utils/block_dumper.hpp | 14 ++++++++------ aesxx/utils/block_input.hpp | 5 +++-- aesxx/utils/decrypt_block.cpp | 7 ++++--- aesxx/utils/decrypt_bmp.cpp | 3 ++- aesxx/utils/decrypt_file.cpp | 3 ++- aesxx/utils/encrypt_block.cpp | 7 ++++--- aesxx/utils/encrypt_bmp.cpp | 3 ++- aesxx/utils/encrypt_file.cpp | 3 ++- aesxx/utils/file_cmd_parser.hpp | 3 ++- aesxx/utils/helpers/command_line.hpp | 14 ++++++++------ 12 files changed, 40 insertions(+), 28 deletions(-) (limited to 'aesxx/utils') diff --git a/aesxx/utils/CMakeLists.txt b/aesxx/utils/CMakeLists.txt index 9ee06d0..de6c206 100644 --- a/aesxx/utils/CMakeLists.txt +++ b/aesxx/utils/CMakeLists.txt @@ -1,4 +1,4 @@ -find_package(Boost CONFIG REQUIRED COMPONENTS filesystem program_options) +find_package(Boost CONFIG REQUIRED COMPONENTS program_options) function(add_util name src) set(target "util_${name}") @@ -6,7 +6,6 @@ function(add_util name src) target_link_libraries("${target}" PRIVATE aesxx Boost::disable_autolinking - Boost::filesystem Boost::program_options) set_target_properties("${target}" PROPERTIES OUTPUT_NAME "${name}") install(TARGETS "${target}" RUNTIME DESTINATION bin) diff --git a/aesxx/utils/block_cmd_parser.hpp b/aesxx/utils/block_cmd_parser.hpp index 3ae0eac..c179556 100644 --- a/aesxx/utils/block_cmd_parser.hpp +++ b/aesxx/utils/block_cmd_parser.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -29,7 +30,7 @@ public: std::vector inputs; - explicit BlockSettings(const std::string& argv0) : SettingsParser{argv0} { + explicit BlockSettings(std::string_view argv0) : SettingsParser{argv0} { visible.add_options()( "verbose,v", boost::program_options::bool_switch(&verbose), "enable verbose output"); visible.add_options()("algorithm,a", diff --git a/aesxx/utils/block_dumper.hpp b/aesxx/utils/block_dumper.hpp index 226ba20..c725670 100644 --- a/aesxx/utils/block_dumper.hpp +++ b/aesxx/utils/block_dumper.hpp @@ -8,13 +8,15 @@ #include #include +#include #include +#include #include template -void dump_block(const char* header, const typename aes::Types::Block& block) { - std::cout << header << ": " << aes::to_string(block) << "\n"; - std::cout << aes::to_matrix_string(block) << "\n"; +void dump_block(std::string_view header, const typename aes::Types::Block& block) { + std::cout << std::format("{}: {}\n", header, aes::to_string(block)); + std::cout << std::format("{}\n", aes::to_matrix_string(block)); } template @@ -24,7 +26,7 @@ void dump_plaintext(const typename aes::Types::Block& block) { template void dump_key(const typename aes::Types::Key& key) { - std::cout << "Key: " << aes::to_string(key) << "\n\n"; + std::cout << std::format("Key: {}\n\n", aes::to_string(key)); } template @@ -40,9 +42,9 @@ void dump_iv(const typename aes::Types::Block& iv) { template void dump_round_keys(const char* header, const typename aes::Types::RoundKeys& round_keys) { - std::cout << header << ":\n"; + std::cout << std::format("{}:\n", header); for (std::size_t i = 0; i < aes::get_number_of_rounds(); ++i) - std::cout << "\t[" << i << "]: " << aes::to_string(round_keys.keys[i]) << "\n"; + std::cout << std::format("\t[{}]: {}\n", i, aes::to_string(round_keys.keys[i])); std::cout << "\n"; } diff --git a/aesxx/utils/block_input.hpp b/aesxx/utils/block_input.hpp index 1dd3f70..042a680 100644 --- a/aesxx/utils/block_input.hpp +++ b/aesxx/utils/block_input.hpp @@ -6,15 +6,16 @@ #pragma once #include +#include #include #include class Input { public: - Input(const std::string& key, const std::string& iv, std::vector&& blocks) + Input(std::string_view key, std::string_view iv, std::vector&& blocks) : key{key}, iv{iv}, blocks{std::move(blocks)} {} - Input(const std::string& key, std::vector&& blocks) + Input(std::string key, std::vector&& blocks) : key{key}, blocks{std::move(blocks)} {} const std::string key; diff --git a/aesxx/utils/decrypt_block.cpp b/aesxx/utils/decrypt_block.cpp index 42e496e..d432e31 100644 --- a/aesxx/utils/decrypt_block.cpp +++ b/aesxx/utils/decrypt_block.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -48,7 +49,7 @@ void decrypt_with_mode(const Input& input, bool verbose = false) { dump_plaintext(plaintext); dump_next_iv(decrypt); } else { - std::cout << aes::to_string(plaintext) << '\n'; + std::cout << std::format("{}\n", aes::to_string(plaintext)); } } } @@ -112,7 +113,7 @@ void decrypt_using_particular_box(aes::Box& box, const std::vector& aes::Box::Block plaintext; box.decrypt_block(ciphertext, plaintext); - std::cout << box.format_block(plaintext) << '\n'; + std::cout << std::format("{}\n", box.format_block(plaintext)); } } @@ -161,7 +162,7 @@ int main(int argc, char** argv) { std::cerr << e; return 1; } catch (const std::exception& e) { - std::cerr << e.what() << "\n"; + std::cerr << std::format("{}\n", e.what()); return 1; } return 0; diff --git a/aesxx/utils/decrypt_bmp.cpp b/aesxx/utils/decrypt_bmp.cpp index 86f6b7d..397ec02 100644 --- a/aesxx/utils/decrypt_bmp.cpp +++ b/aesxx/utils/decrypt_bmp.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -69,7 +70,7 @@ int main(int argc, char** argv) { std::cerr << e; return 1; } catch (const std::exception& e) { - std::cerr << e.what() << "\n"; + std::cerr << std::format("{}\n", e.what()); return 1; } return 0; diff --git a/aesxx/utils/decrypt_file.cpp b/aesxx/utils/decrypt_file.cpp index 727362c..192f1f6 100644 --- a/aesxx/utils/decrypt_file.cpp +++ b/aesxx/utils/decrypt_file.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -67,7 +68,7 @@ int main(int argc, char** argv) { std::cerr << e; return 1; } catch (const std::exception& e) { - std::cerr << e.what() << "\n"; + std::cerr << std::format("{}\n", e.what()); return 1; } return 0; diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp index b6bdca9..e132256 100644 --- a/aesxx/utils/encrypt_block.cpp +++ b/aesxx/utils/encrypt_block.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -48,7 +49,7 @@ void encrypt_with_mode(const Input& input, bool verbose = false) { dump_ciphertext(ciphertext); dump_next_iv(encrypt); } else { - std::cout << aes::to_string(ciphertext) << '\n'; + std::cout << std::format("{}\n", aes::to_string(ciphertext)); } } } @@ -113,7 +114,7 @@ void encrypt_using_particular_box(aes::Box& box, aes::Box::Block ciphertext; box.encrypt_block(plaintext, ciphertext); - std::cout << box.format_block(ciphertext) << '\n'; + std::cout << std::format("{}\n", box.format_block(ciphertext)); } } @@ -162,7 +163,7 @@ int main(int argc, char** argv) { std::cerr << e; return 1; } catch (const std::exception& e) { - std::cerr << e.what() << "\n"; + std::cerr << std::format("{}\n", e.what()); return 1; } return 0; diff --git a/aesxx/utils/encrypt_bmp.cpp b/aesxx/utils/encrypt_bmp.cpp index 01f3a78..5b74fb9 100644 --- a/aesxx/utils/encrypt_bmp.cpp +++ b/aesxx/utils/encrypt_bmp.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -67,7 +68,7 @@ int main(int argc, char** argv) { std::cerr << e; return 1; } catch (const std::exception& e) { - std::cerr << e.what() << "\n"; + std::cerr << std::format("{}\n", e.what()); return 1; } return 0; diff --git a/aesxx/utils/encrypt_file.cpp b/aesxx/utils/encrypt_file.cpp index 4a774fd..2862bfb 100644 --- a/aesxx/utils/encrypt_file.cpp +++ b/aesxx/utils/encrypt_file.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -67,7 +68,7 @@ int main(int argc, char** argv) { std::cerr << e; return 1; } catch (const std::exception& e) { - std::cerr << e.what() << "\n"; + std::cerr << std::format("{}\n", e.what()); return 1; } return 0; diff --git a/aesxx/utils/file_cmd_parser.hpp b/aesxx/utils/file_cmd_parser.hpp index 4a504c8..2389d41 100644 --- a/aesxx/utils/file_cmd_parser.hpp +++ b/aesxx/utils/file_cmd_parser.hpp @@ -15,6 +15,7 @@ #include #include +#include #include class FileSettings : public command_line::SettingsParser { @@ -27,7 +28,7 @@ public: std::string key; std::string iv; - explicit FileSettings(const std::string& argv0) : SettingsParser{argv0} { + explicit FileSettings(std::string_view argv0) : SettingsParser{argv0} { visible.add_options()("algorithm,a", boost::program_options::value(&algorithm) ->required() diff --git a/aesxx/utils/helpers/command_line.hpp b/aesxx/utils/helpers/command_line.hpp index 9a2da05..cc05a6a 100644 --- a/aesxx/utils/helpers/command_line.hpp +++ b/aesxx/utils/helpers/command_line.hpp @@ -5,19 +5,21 @@ #pragma once -#include #include #include +#include +#include #include #include #include +#include namespace command_line { class SettingsParser { public: - explicit SettingsParser(const std::string& argv0) : prog_name{extract_filename(argv0)} { + explicit SettingsParser(std::string_view argv0) : prog_name{extract_filename(argv0)} { visible.add_options()("help,h", "show this message and exit"); } @@ -49,7 +51,7 @@ public: } void usage_error(const std::exception& e) const { - std::cerr << "usage error: " << e.what() << '\n'; + std::cerr << std::format("usage error: {}\n", e.what()); std::cerr << *this; } @@ -59,15 +61,15 @@ protected: boost::program_options::positional_options_description positional; private: - static std::string extract_filename(const std::string& path) { - return boost::filesystem::path{path}.filename().string(); + static std::string extract_filename(std::string_view path) { + return std::filesystem::path{path}.filename().string(); } 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 << std::format("usage: {} {}\n", parser.prog_name, short_descr); os << parser.visible; return os; } -- cgit v1.3