diff --git a/Src/Common/FSecure/CppTools/ByteConverter/ByteConverter.h b/Src/Common/FSecure/CppTools/ByteConverter/ByteConverter.h index 134cfc6..f735523 100644 --- a/Src/Common/FSecure/CppTools/ByteConverter/ByteConverter.h +++ b/Src/Common/FSecure/CppTools/ByteConverter/ByteConverter.h @@ -285,9 +285,9 @@ namespace FSecure /// @brief Class providing simple way of generating ByteConverter of custom types by treating them as tuple. /// ByteConverter can use this functionality by inheriting from TupleConverter and providing - /// public static std::tuple<...> TupleTransform(T const&) method. + /// public static std::tuple<...> Convert(T const&) method. /// Use Utils::MakeConversionTuple to create efficient tuple of value or references to members. - /// ByteVector can declare its own versions of To/Size/From methods if it needs dedicated logic to serialize type. + /// ByteVector can declare its own versions of To/From/Size methods if it needs dedicated logic to serialize type. /// @tparam T Type for serialization. template struct TupleConverter @@ -314,65 +314,32 @@ namespace FSecure }; public: - /// @brief Type of helper tuple returned by ByteConverter::TupleTransform. + /// @brief Type returned by ByteConverter::Convert. /// @note This type will be deduced late in instantiation procedure. /// @tparam C Type To be serialized. template - using TupleTransformType = decltype(ByteConverter::TupleTransform(std::declval())); + using ConvertType = decltype(ByteConverter::Convert(std::declval())); - // MSVC is using late deduction of default template parameter type, and only if they are used. - // This allows definition of constexpr version of Size function using SFINAE. - // There are proposals implement same delay in clang, but it is not available yet, or possibly will never be. -#if defined (__clang__) - /// @brief Default implementation of Size method. - /// @param obj Object for serialization. - /// @return size_t. Number of bytes used after serialization. - static size_t Size([[maybe_unused]] T const& obj) + /// @brief Use it to convert raw data into tuple. + /// Allows splitting deserialization into two phases. + /// 1. Retrieve data as tuple. ByteView internal pointer will be correctly moved in the process. + /// 2. Create dedicated logic of transforming tuple into desired type. + /// @param bv. Buffer with serialized data. + /// @return tuple of retrieved data for type construction. + static auto Convert(ByteView& bv) { - if constexpr (Utils::Apply>::value) - { - return Utils::Apply>::value; - } - else - { - return ByteVector::Size(ByteConverter::TupleTransform(obj)); - } - } -#else - /// @brief Default implementation of Size method with compile time evaluation. - /// @return size_t. Number of bytes used after serialization. - template , std::enable_if_t::value, int> = 0> - static constexpr size_t Size() - { - return Utils::Apply::value; + return bv.Read>(); } - /// @brief Default implementation of Size method. - /// @param obj Object for serialization. - /// @return size_t. Number of bytes used after serialization. - template , std::enable_if_t::value, int> = 0> - static size_t Size(T const& obj) - { - return ByteVector::Size(ByteConverter::TupleTransform(obj)); - } -#endif + // From this point forward will be implemented ByteConverter standard interface methods. /// @brief Default implementation of To method. - /// Serializes data treating it as tuple generated by TupleTransform. + /// Serializes data treating it as tuple generated by Convert. /// @param obj Object for serialization. /// @param bv output ByteVector with already allocated memory for data. static void To(T const& obj, ByteVector& bv) { - bv.Store(ByteConverter::TupleTransform(obj)); - } - - /// @brief Use it to read from ByteView to tuple of types deduced from TupleTransform. - /// This is a helper method used to create dedicated logic of type reading after all data was retrieved form view as tuple. - /// @param bv. Buffer with serialized data. - /// @return tuple of retrieved data for type construction. - static auto ReadTuple(ByteView& bv) - { - return bv.Read>(); + bv.Store(ByteConverter::Convert(obj)); } /// @brief Default implementation of From method. @@ -383,8 +350,44 @@ namespace FSecure /// @return constructed type. static T From(ByteView& bv) { - auto tpl = ReadTuple(bv); - return std::apply(Utils::Construction::Braces{}, std::move(tpl)); + return std::apply(Utils::Construction::Braces{}, Convert(bv)); } + + // MSVC is using late deduction of default template parameter type, and only if they are used. + // This allows definition of constexpr version of Size function using SFINAE. + // There are proposals to implement same delay in clang, but it is not available yet, or possibly will never be. +#if defined (__clang__) + /// @brief Default implementation of Size method. + /// @param obj Object for serialization. + /// @return size_t. Number of bytes used after serialization. + static size_t Size([[maybe_unused]] T const& obj) + { + if constexpr (Utils::Apply>::value) + { + return Utils::Apply>::value; + } + else + { + return ByteVector::Size(ByteConverter::Convert(obj)); + } + } +#else + /// @brief Default implementation of Size method with compile time evaluation. + /// @return size_t. Number of bytes used after serialization. + template , std::enable_if_t::value, int> = 0> + static constexpr size_t Size() + { + return Utils::Apply::value; + } + + /// @brief Default implementation of Size method. + /// @param obj Object for serialization. + /// @return size_t. Number of bytes used after serialization. + template , std::enable_if_t::value, int> = 0> + static size_t Size(T const& obj) + { + return ByteVector::Size(ByteConverter::Convert(obj)); + } +#endif }; } diff --git a/Src/Common/FSecure/WinTools/HostInfo.h b/Src/Common/FSecure/WinTools/HostInfo.h index 54c2db3..6c9ce35 100644 --- a/Src/Common/FSecure/WinTools/HostInfo.h +++ b/Src/Common/FSecure/WinTools/HostInfo.h @@ -40,7 +40,7 @@ namespace FSecure { /// Serialization of RTL_OSVERSIONINFOEXW type to/from ByteVector. /// @param obj. Object to be serialized. - static auto TupleTransform(RTL_OSVERSIONINFOEXW const& obj) + static auto Convert(RTL_OSVERSIONINFOEXW const& obj) { return Utils::MakeConversionTuple( obj.dwOSVersionInfoSize, @@ -81,7 +81,7 @@ namespace FSecure { /// Serialization of HostInfo type to/from ByteVector. /// @param obj. Object to be serialized. - static auto TupleTransform(HostInfo const& obj) + static auto Convert(HostInfo const& obj) { return Utils::MakeConversionTuple(obj.m_ComputerName, obj.m_UserName, obj.m_Domain, obj.m_OsVersionInfo, obj.m_ProcessId, obj.m_IsElevated); } diff --git a/Src/Core/RouteId.h b/Src/Core/RouteId.h index d9aec10..1e28095 100644 --- a/Src/Core/RouteId.h +++ b/Src/Core/RouteId.h @@ -80,7 +80,7 @@ namespace FSecure template <> struct ByteConverter : TupleConverter { - static auto TupleTransform(C3::RouteId const& obj) + static auto Convert(C3::RouteId const& obj) { return Utils::MakeConversionTuple(obj.GetAgentId(), obj.GetInterfaceId()); }