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/CMakeLists.txt | 3 +++ aesxx/include/aesxx/aes.hpp | 25 +++++++++++++------------ aesxx/include/aesxx/api.hpp | 15 +++------------ aesxx/include/aesxx/box.hpp | 29 +++++++---------------------- aesxx/include/aesxx/debug.hpp | 25 +++++++++---------------- aesxx/include/aesxx/error.hpp | 5 +++-- 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 ++++++++------ 18 files changed, 78 insertions(+), 92 deletions(-) (limited to 'aesxx') diff --git a/aesxx/CMakeLists.txt b/aesxx/CMakeLists.txt index 378bad7..338ecef 100644 --- a/aesxx/CMakeLists.txt +++ b/aesxx/CMakeLists.txt @@ -1,3 +1,6 @@ +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + find_package(Boost CONFIG REQUIRED) add_library(aesxx INTERFACE) diff --git a/aesxx/include/aesxx/aes.hpp b/aesxx/include/aesxx/aes.hpp index c3457c8..dedd366 100644 --- a/aesxx/include/aesxx/aes.hpp +++ b/aesxx/include/aesxx/aes.hpp @@ -14,6 +14,7 @@ #include #include +#include namespace aes { namespace aes128 { @@ -37,8 +38,8 @@ inline std::size_t get_number_of_rounds() { } template <> -inline void from_string(aes128::Block& dest, const char* src) { - aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +inline void from_string(aes128::Block& dest, std::string_view src) { + aes_AES128_parse_block(&dest, src.data(), ErrorDetailsThrowsInDestructor{}); } template <> @@ -56,8 +57,8 @@ inline std::string to_matrix_string(const aes128::Block& src) { } template <> -inline void from_string(aes128::Key& dest, const char* src) { - aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +inline void from_string(aes128::Key& dest, std::string_view src) { + aes_AES128_parse_key(&dest, src.data(), ErrorDetailsThrowsInDestructor{}); } template <> @@ -110,8 +111,8 @@ inline std::size_t get_number_of_rounds() { } template <> -inline void from_string(aes192::Block& dest, const char* src) { - aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +inline void from_string(aes192::Block& dest, std::string_view src) { + aes_AES192_parse_block(&dest, src.data(), ErrorDetailsThrowsInDestructor{}); } template <> @@ -129,8 +130,8 @@ inline std::string to_matrix_string(const aes192::Block& src) { } template <> -inline void from_string(aes192::Key& dest, const char* src) { - aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +inline void from_string(aes192::Key& dest, std::string_view src) { + aes_AES192_parse_key(&dest, src.data(), ErrorDetailsThrowsInDestructor{}); } template <> @@ -183,8 +184,8 @@ inline std::size_t get_number_of_rounds() { } template <> -inline void from_string(aes256::Block& dest, const char* src) { - aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +inline void from_string(aes256::Block& dest, std::string_view src) { + aes_AES256_parse_block(&dest, src.data(), ErrorDetailsThrowsInDestructor{}); } template <> @@ -202,8 +203,8 @@ inline std::string to_matrix_string(const aes256::Block& src) { } template <> -inline void from_string(aes256::Key& dest, const char* src) { - aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +inline void from_string(aes256::Key& dest, std::string_view src) { + aes_AES256_parse_key(&dest, src.data(), ErrorDetailsThrowsInDestructor{}); } template <> diff --git a/aesxx/include/aesxx/api.hpp b/aesxx/include/aesxx/api.hpp index 759ff0c..4c1c868 100644 --- a/aesxx/include/aesxx/api.hpp +++ b/aesxx/include/aesxx/api.hpp @@ -10,6 +10,7 @@ #include #include +#include #include namespace aes { @@ -21,12 +22,7 @@ template std::size_t get_number_of_rounds(); template -void from_string(typename Types::Block&, const char*); - -template -void from_string(typename Types::Block& dest, const std::string& src) { - from_string(dest, src.c_str()); -} +void from_string(typename Types::Block&, std::string_view); template std::string to_string(const typename Types::Block&); @@ -35,12 +31,7 @@ template std::string to_matrix_string(const typename Types::Block&); template -void from_string(typename Types::Key&, const char*); - -template -void from_string(typename Types::Key& dest, const std::string& src) { - from_string(dest, src.c_str()); -} +void from_string(typename Types::Key&, std::string_view); template std::string to_string(const typename Types::Key&); diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index 540688b..fb42525 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -13,6 +13,7 @@ #include #include +#include #include namespace aes { @@ -34,20 +35,12 @@ public: return reinterpret_cast(&str); } - static void parse_block(Block& dest, Algorithm algorithm, const char* src) { - aes_box_parse_block(&dest, algorithm, src, ErrorDetailsThrowsInDestructor{}); + static void parse_block(Block& dest, Algorithm algorithm, std::string_view src) { + aes_box_parse_block(&dest, algorithm, src.data(), ErrorDetailsThrowsInDestructor{}); } - static void parse_block(Block& dest, Algorithm algorithm, const std::string& src) { - parse_block(dest, algorithm, src.c_str()); - } - - static void parse_key(Key& dest, Algorithm algorithm, const char* src) { - aes_box_parse_key(&dest, algorithm, src, ErrorDetailsThrowsInDestructor{}); - } - - static void parse_key(Key& dest, Algorithm algorithm, const std::string& src) { - parse_key(dest, algorithm, src.c_str()); + static void parse_key(Key& dest, Algorithm algorithm, std::string_view src) { + aes_box_parse_key(&dest, algorithm, src.data(), ErrorDetailsThrowsInDestructor{}); } Box(Algorithm algorithm, const Key& key) : algorithm{algorithm}, mode{AES_ECB} { @@ -115,22 +108,14 @@ public: return format_key(src, get_algorithm()); } - void parse_block(Block& dest, const char* src) { + void parse_block(Block& dest, std::string_view src) { parse_block(dest, get_algorithm(), src); } - void parse_block(Block& dest, const std::string& src) { - parse_block(dest, src.c_str()); - } - - void parse_key(Key& dest, const char* src) { + void parse_key(Key& dest, std::string_view src) { parse_key(dest, get_algorithm(), src); } - void parse_key(Key& dest, const std::string& src) { - parse_key(dest, src.c_str()); - } - Algorithm get_algorithm() const { return algorithm; } diff --git a/aesxx/include/aesxx/debug.hpp b/aesxx/include/aesxx/debug.hpp index d54605d..13ea898 100644 --- a/aesxx/include/aesxx/debug.hpp +++ b/aesxx/include/aesxx/debug.hpp @@ -13,8 +13,10 @@ #endif #include +#include #include #include +#include namespace aes { namespace aux { @@ -34,37 +36,28 @@ public: private: template static std::string put_between_brackets(const T& x) { - std::ostringstream oss; - oss << "[" << x << "]"; - return oss.str(); - } - - template - static std::string stringify(const T& x) { - std::ostringstream oss; - oss << x; - return oss.str(); + return std::format("[{}]", x); } static std::string format_address_fallback(const void* addr) { return put_between_brackets(addr); } - static std::string format_module(const std::string& module_name, const void* offset = nullptr) { + static std::string format_module(std::string_view module_name, const void* offset = nullptr) { if (offset == nullptr) return put_between_brackets(module_name); else - return put_between_brackets(module_name + "+" + stringify(offset)); + return put_between_brackets(std::format("{}+{}", module_name, offset)); } - static std::string format_symbol(const std::string& symbol_name, const void* offset = nullptr) { + static std::string format_symbol(std::string_view symbol_name, const void* offset = nullptr) { return format_module(symbol_name, offset); } - static std::string format_symbol(const std::string& module_name, - const std::string& symbol_name, + static std::string format_symbol(std::string_view module_name, + std::string_view symbol_name, const void* offset = nullptr) { - return format_symbol(module_name + "!" + symbol_name, offset); + return format_symbol(std::format("{}!{}", module_name, symbol_name), offset); } #ifdef WIN32 diff --git a/aesxx/include/aesxx/error.hpp b/aesxx/include/aesxx/error.hpp index e5c5803..72a50b3 100644 --- a/aesxx/include/aesxx/error.hpp +++ b/aesxx/include/aesxx/error.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -60,10 +61,10 @@ private: }; inline std::ostream& operator<<(std::ostream& os, const Error& error) { - os << "AES error: " << error.what() << '\n'; + os << std::format("AES error: {}\n", error.what()); os << "Call stack:\n"; error.for_each_addr([&os](const void* addr, const std::string& name) { - os << '\t' << addr << ' ' << name << '\n'; + os << std::format("\t{} {}\n", addr, name); }); return os; } 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