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/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 +++-- 5 files changed, 35 insertions(+), 64 deletions(-) (limited to 'aesxx/include') 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; } -- cgit v1.3