Refactor serialization code 73/293873/1
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 5 Jun 2023 12:36:55 +0000 (14:36 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 7 Jun 2023 08:40:59 +0000 (10:40 +0200)
* Remove unused overloads (pointers, stl).
* Unify primitive types (de)serialization.
* Modify Serializable interface to allow passing an existing object.
* Update serialization wrappers' implementations and move them to cpp.

Change-Id: I498f9dd9cca570a1a7eb424fb481d07a1297a279

src/manager/common/data-type.cpp
src/manager/common/data-type.h
src/manager/common/protocols.cpp
src/manager/common/protocols.h
src/manager/dpl/core/include/dpl/serialization.h

index e59bd3f..38d9c3d 100644 (file)
@@ -83,9 +83,7 @@ DataType::DataType(Type data) :
 
 DataType::DataType(IStream &stream)
 {
-       stream.Read(sizeof(m_dataType), &m_dataType);
-
-       checkRange();
+       Deserialize(stream); // beware: calling virtual method from ctor
 }
 
 void DataType::Serialize(IStream &stream) const
@@ -93,6 +91,13 @@ void DataType::Serialize(IStream &stream) const
        stream.Write(sizeof(m_dataType), &m_dataType);
 }
 
+void DataType::Deserialize(IStream &stream)
+{
+       stream.Read(sizeof(m_dataType), &m_dataType);
+
+       checkRange();
+}
+
 DataType::operator int() const
 {
        return static_cast<int>(m_dataType);
index 1866204..fd08a19 100644 (file)
@@ -71,6 +71,7 @@ public:
 
        explicit DataType(IStream &stream);
        void Serialize(IStream &stream) const override;
+       void Deserialize(IStream &stream) override;
 
        explicit DataType(KeyType key);
        DataType(const DataType &) = default;
index 6a762a7..cd31509 100644 (file)
@@ -42,6 +42,27 @@ char const *const ALIAS_SEPARATOR = " ";
 char const *const CLIENT_ID_SYSTEM = "/System";
 char const *const CLIENT_ID_ADMIN_USER = "/User";
 
+PolicySerializable::PolicySerializable(IStream &stream)
+{
+       Deserialize(stream); // beware: calling virtual method from ctor
+}
+
+void PolicySerializable::Serialize(IStream &stream) const
+{
+       Serialization::Serialize(stream, password);
+       Serialization::Serialize(stream, extractable);
+       Serialization::Serialize(stream, static_cast<int>(backend));
+}
+
+void PolicySerializable::Deserialize(IStream &stream)
+{
+       int policyBackend;
+       Deserialization::Deserialize(stream, password);
+       Deserialization::Deserialize(stream, extractable);
+       Deserialization::Deserialize(stream, policyBackend);
+       backend = static_cast<PolicyBackend>(policyBackend);
+}
+
 PKCS12Serializable::PKCS12Serializable()
 {
 }
@@ -56,6 +77,11 @@ PKCS12Serializable::PKCS12Serializable(PKCS12Serializable &&other)
 {
 }
 
+PKCS12Serializable::PKCS12Serializable(IStream &stream)
+{
+       Deserialize(stream); // beware: calling virtual method from ctor
+}
+
 PKCS12Serializable &PKCS12Serializable::operator=(PKCS12Serializable &&other)
 {
        if (this == &other)
@@ -68,7 +94,7 @@ PKCS12Serializable &PKCS12Serializable::operator=(PKCS12Serializable &&other)
        return *this;
 }
 
-PKCS12Serializable::PKCS12Serializable(IStream &stream)
+void PKCS12Serializable::Deserialize(IStream &stream)
 {
        bool keyPresent = false;
        Deserialization::Deserialize(stream, keyPresent);
@@ -180,6 +206,11 @@ CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(
 
 CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(IStream &stream)
 {
+       Deserialize(stream); // beware: calling virtual method from ctor
+}
+
+void CryptoAlgorithmSerializable::Deserialize(IStream &stream)
+{
        size_t plen = 0;
        Deserializer<size_t>::Deserialize(stream, plen);
 
index 4a2c2bb..5d6a759 100644 (file)
@@ -91,28 +91,16 @@ typedef std::vector<std::tuple<ClientId, Name, bool>> OwnerNameEncryptionStatusV
 
 class IStream;
 
-struct COMMON_API PolicySerializable : public Policy, ISerializable {
+struct COMMON_API PolicySerializable final : public Policy, ISerializable {
        PolicySerializable() {}
        explicit PolicySerializable(const Policy &policy) : Policy(policy) {}
+       explicit PolicySerializable(IStream &stream);
 
-       explicit PolicySerializable(IStream &stream)
-       {
-               int policyBackend;
-               Deserialization::Deserialize(stream, password);
-               Deserialization::Deserialize(stream, extractable);
-               Deserialization::Deserialize(stream, policyBackend);
-               backend = static_cast<PolicyBackend>(policyBackend);
-       }
-
-       void Serialize(IStream &stream) const
-       {
-               Serialization::Serialize(stream, password);
-               Serialization::Serialize(stream, extractable);
-               Serialization::Serialize(stream, static_cast<int>(backend));
-       }
+       void Serialize(IStream &stream) const override;
+       void Deserialize(IStream &stream) override;
 };
 
-struct COMMON_API PKCS12Serializable : public PKCS12Impl, ISerializable {
+struct COMMON_API PKCS12Serializable final : public PKCS12Impl, ISerializable {
        PKCS12Serializable();
 
        PKCS12Serializable(const PKCS12Serializable &) = delete;
@@ -126,10 +114,12 @@ struct COMMON_API PKCS12Serializable : public PKCS12Impl, ISerializable {
        PKCS12Serializable(KeyShPtr privKey,
                                           CertificateShPtr cert,
                                           CertificateShPtrVector chainCerts);
-       void Serialize(IStream &) const;
+
+       void Serialize(IStream &) const override;
+       void Deserialize(IStream &) override;
 };
 
-struct COMMON_API CryptoAlgorithmSerializable : public CryptoAlgorithm,
+struct COMMON_API CryptoAlgorithmSerializable final : public CryptoAlgorithm,
        ISerializable {
        DECLARE_EXCEPTION_TYPE(Exception, Base);
        DECLARE_EXCEPTION_TYPE(Exception, UnsupportedParam);
@@ -138,7 +128,8 @@ struct COMMON_API CryptoAlgorithmSerializable : public CryptoAlgorithm,
        explicit CryptoAlgorithmSerializable(const CryptoAlgorithm &);
        explicit CryptoAlgorithmSerializable(IStream &);
 
-       void Serialize(IStream &) const;
+       void Serialize(IStream &) const override;
+       void Deserialize(IStream &) override;
 };
 
 } // namespace CKM
index 0559c42..4656427 100644 (file)
 
 #include <string>
 #include <vector>
-#include <list>
-#include <map>
-#include <memory>
-#include <tuple>
+#include <utility>
+#include <type_traits>
 
 namespace CKM {
 // Abstract data stream buffer
@@ -43,95 +41,25 @@ public:
 // Serializable interface
 class ISerializable {
 public:
-       /*    ISerializable(){};
-        *    ISerializable(IStream&){}; */
        virtual void Serialize(IStream &) const = 0;
+       virtual void Deserialize(IStream &) = 0;
        virtual ~ISerializable() {}
 };
 
 struct Serialization {
        // serialization
-       // normal functions
 
-       // ISerializable objects
-       static void Serialize(IStream &stream, const ISerializable &object)
-       {
-               object.Serialize(stream);
-       }
-
-       static void Serialize(IStream &stream, const ISerializable *const object)
-       {
-               object->Serialize(stream);
-       }
-
-       // char
-       static void Serialize(IStream &stream, const char value)
-       {
-               stream.Write(sizeof(value), &value);
-       }
-       static void Serialize(IStream &stream, const char *const value)
-       {
-               stream.Write(sizeof(*value), value);
-       }
-
-       // unsigned char
-       static void Serialize(IStream &stream, const unsigned char value)
-       {
-               stream.Write(sizeof(value), &value);
-       }
-       static void Serialize(IStream &stream, const unsigned char *const value)
-       {
-               stream.Write(sizeof(*value), value);
-       }
-
-       // unsigned int32
-       static void Serialize(IStream &stream, const uint32_t value)
-       {
-               stream.Write(sizeof(value), &value);
-       }
-       static void Serialize(IStream &stream, const uint32_t *const value)
-       {
-               stream.Write(sizeof(*value), value);
-       }
-
-       // int32
-       static void Serialize(IStream &stream, const int32_t value)
-       {
-               stream.Write(sizeof(value), &value);
-       }
-       static void Serialize(IStream &stream, const int32_t *const value)
-       {
-               stream.Write(sizeof(*value), value);
-       }
-
-       // unsigned int64
-       static void Serialize(IStream &stream, const uint64_t value)
+       template <typename T, std::enable_if_t<!std::is_base_of_v<ISerializable, T>, bool> = true>
+       static void Serialize(IStream &stream, const T& value)
        {
                stream.Write(sizeof(value), &value);
        }
-       static void Serialize(IStream &stream, const uint64_t *const value)
-       {
-               stream.Write(sizeof(*value), value);
-       }
 
-       // int64
-       static void Serialize(IStream &stream, const int64_t value)
-       {
-               stream.Write(sizeof(value), &value);
-       }
-       static void Serialize(IStream &stream, const int64_t *const value)
-       {
-               stream.Write(sizeof(*value), value);
-       }
-
-       // bool
-       static void Serialize(IStream &stream, const bool value)
-       {
-               stream.Write(sizeof(value), &value);
-       }
-       static void Serialize(IStream &stream, const bool *const value)
+       // ISerializable objects
+       template <typename T, std::enable_if_t<std::is_base_of_v<ISerializable, T>, bool> = true>
+       static void Serialize(IStream &stream, const T &object)
        {
-               stream.Write(sizeof(*value), value);
+               object.Serialize(stream);
        }
 
        // std::string
@@ -143,34 +71,8 @@ struct Serialization {
                stream.Write(length * sizeof(T), str.data());
        }
 
-       template<typename T, typename R, typename A>
-       static void Serialize(IStream &stream,
-                                                 const std::basic_string<T, R, A> *const str)
-       {
-               int length = str->size();
-               stream.Write(sizeof(length), &length);
-               stream.Write(length * sizeof(T), str->data());
-       }
-
        // STL templates
 
-       // std::list
-       template <typename T>
-       static void Serialize(IStream &stream, const std::list<T> &list)
-       {
-               int length = list.size();
-               stream.Write(sizeof(length), &length);
-
-               for (typename std::list<T>::const_iterator list_iter = list.begin();
-                               list_iter != list.end(); list_iter++)
-                       Serialize(stream, *list_iter);
-       }
-       template <typename T>
-       static void Serialize(IStream &stream, const std::list<T> *const list)
-       {
-               Serialize(stream, *list);
-       }
-
        // RawBuffer
        template <typename A>
        static void Serialize(IStream &stream,
@@ -181,13 +83,6 @@ struct Serialization {
                stream.Write(length, vec.data());
        }
 
-       template <typename A>
-       static void Serialize(IStream &stream,
-                                                 const std::vector<unsigned char, A> *const vec)
-       {
-               Serialize(stream, *vec);
-       }
-
        // std::vector
        template <typename T, typename A>
        static void Serialize(IStream &stream, const std::vector<T, A> &vec)
@@ -198,11 +93,6 @@ struct Serialization {
                for (const auto &i : vec)
                        Serialize(stream, i);
        }
-       template <typename T, typename A>
-       static void Serialize(IStream &stream, const std::vector<T, A> *const vec)
-       {
-               Serialize(stream, *vec);
-       }
 
        // std::pair
        template <typename A, typename B>
@@ -211,149 +101,22 @@ struct Serialization {
                Serialize(stream, p.first);
                Serialize(stream, p.second);
        }
-       template <typename A, typename B>
-       static void Serialize(IStream &stream, const std::pair<A, B> *const p)
-       {
-               Serialize(stream, *p);
-       }
-
-       // std::tuple non generic!
-       template <typename A, typename B, typename C>
-       static void Serialize(IStream &stream, const std::tuple<A, B, C> &p)
-       {
-               Serialize(stream, std::get<0>(p));
-               Serialize(stream, std::get<1>(p));
-               Serialize(stream, std::get<2>(p));
-       }
-       template <typename A, typename B, typename C>
-       static void Serialize(IStream &stream, const std::tuple<A, B, C> *const p)
-       {
-               Serialize(stream, *p);
-       }
-
-       // std::map
-       template <typename K, typename T>
-       static void Serialize(IStream &stream, const std::map<K, T> &map)
-       {
-               int length = map.size();
-               stream.Write(sizeof(length), &length);
-               typename std::map<K, T>::const_iterator it;
-
-               for (it = map.begin(); it != map.end(); ++it) {
-                       Serialize(stream, (*it).first);
-                       Serialize(stream, (*it).second);
-               }
-       }
-       template <typename K, typename T>
-       static void Serialize(IStream &stream, const std::map<K, T> *const map)
-       {
-               Serialize(stream, *map);
-       }
-
-       // std::unique_ptr
-       template <typename T>
-       static void Serialize(IStream &stream, const std::unique_ptr<T> &p)
-       {
-               Serialize(stream, *p);
-       }
 }; // struct Serialization
 
 struct Deserialization {
        // deserialization
-       // normal functions
 
-       // ISerializable objects
-       // T instead of ISerializable is needed to call proper constructor
-       template <typename T>
-       static void Deserialize(IStream &stream, T &object)
-       {
-               object = T(stream);
-       }
-       template <typename T>
-       static void Deserialize(IStream &stream, T *&object)
-       {
-               object = new T(stream);
-       }
-
-       // *& deserialization template to simplify rest of the code
-       template <typename T>
-       static inline void DeserializePtr(IStream &stream, T *&value)
-       {
-               T *tmp = new T;
-               std::unique_ptr<T> ptr(tmp);
-               Deserialize(stream, *tmp);
-               ptr.release();
-               value = tmp;
-       }
-
-       // char
-       static void Deserialize(IStream &stream, char &value)
-       {
-               stream.Read(sizeof(value), &value);
-       }
-       static void Deserialize(IStream &stream, char *&value)
-       {
-               DeserializePtr(stream, value);
-       }
-
-       // unsigned char
-       static void Deserialize(IStream &stream, unsigned char &value)
-       {
-               stream.Read(sizeof(value), &value);
-       }
-       static void Deserialize(IStream &stream, unsigned char *&value)
-       {
-               DeserializePtr(stream, value);
-       }
-
-       // unsigned int32
-       static void Deserialize(IStream &stream, uint32_t &value)
-       {
-               stream.Read(sizeof(value), &value);
-       }
-       static void Deserialize(IStream &stream, uint32_t *&value)
-       {
-               DeserializePtr(stream, value);
-       }
-
-       // int32
-       static void Deserialize(IStream &stream, int32_t &value)
-       {
-               stream.Read(sizeof(value), &value);
-       }
-       static void Deserialize(IStream &stream, int32_t *&value)
-       {
-               DeserializePtr(stream, value);
-       }
-
-       // unsigned int64
-       static void Deserialize(IStream &stream, uint64_t &value)
-       {
-               stream.Read(sizeof(value), &value);
-       }
-       static void Deserialize(IStream &stream, uint64_t *&value)
-       {
-               DeserializePtr(stream, value);
-       }
-
-       // int64
-       static void Deserialize(IStream &stream, int64_t &value)
+       template <typename T, std::enable_if_t<!std::is_base_of_v<ISerializable, T>, bool> = true>
+       static void Deserialize(IStream &stream, T &value)
        {
                stream.Read(sizeof(value), &value);
        }
-       static void Deserialize(IStream &stream, int64_t *&value)
-       {
-               DeserializePtr(stream, value);
-       }
 
-       // bool
-       static void Deserialize(IStream &stream, bool &value)
-       {
-               stream.Read(sizeof(value), &value);
-       }
-       static void Deserialize(IStream &stream, bool *&value)
+       // ISerializable objects
+       template <typename T, std::enable_if_t<std::is_base_of_v<ISerializable, T>, bool> = true>
+       static void Deserialize(IStream &stream, T &object)
        {
-               DeserializePtr(stream, value);
+               object.Deserialize(stream);
        }
 
        template <typename T, typename R, typename A>
@@ -366,37 +129,8 @@ struct Deserialization {
                str = std::basic_string<T, R, A>(buf.data(), buf.data() + length);
        }
 
-       template <typename T, typename R, typename A>
-       static void Deserialize(IStream &stream, std::basic_string<T, R, A> *&str)
-       {
-               int length;
-               stream.Read(sizeof(length), &length);
-               std::vector<T> buf(length);
-               stream.Read(length * sizeof(T), buf.data());
-               str = new std::basic_string<T, R, A>(buf.data(), buf.data() + length);
-       }
-
        // STL templates
 
-       // std::list
-       template <typename T>
-       static void Deserialize(IStream &stream, std::list<T> &list)
-       {
-               int length;
-               stream.Read(sizeof(length), &length);
-
-               for (int i = 0; i < length; ++i) {
-                       T obj;
-                       Deserialize(stream, obj);
-                       list.push_back(std::move(obj));
-               }
-       }
-       template <typename T>
-       static void Deserialize(IStream &stream, std::list<T> *&list)
-       {
-               DeserializePtr(stream, list);
-       }
-
        // RawBuffer
        template <typename A>
        static void Deserialize(IStream &stream, std::vector<unsigned char, A> &vec)
@@ -407,12 +141,6 @@ struct Deserialization {
                stream.Read(length, vec.data());
        }
 
-       template <typename A>
-       static void Deserialize(IStream &stream, std::vector<unsigned char, A> *&vec)
-       {
-               DeserializePtr<std::vector<unsigned char, A>>(stream, vec);
-       }
-
        // std::vector
        template <typename T, typename A>
        static void Deserialize(IStream &stream, std::vector<T, A> &vec)
@@ -426,11 +154,6 @@ struct Deserialization {
                        vec.push_back(std::move(obj));
                }
        }
-       template <typename T, typename A>
-       static void Deserialize(IStream &stream, std::vector<T, A> *&vec)
-       {
-               DeserializePtr(stream, vec);
-       }
 
        // std::pair
        template <typename A, typename B>
@@ -439,47 +162,6 @@ struct Deserialization {
                Deserialize(stream, p.first);
                Deserialize(stream, p.second);
        }
-       template <typename A, typename B>
-       static void Deserialize(IStream &stream, std::pair<A, B> *&p)
-       {
-               DeserializePtr(stream, p);
-       }
-
-       // std::tuple non generic!
-       template <typename A, typename B, typename C>
-       static void Deserialize(IStream &stream, std::tuple<A, B, C> &p)
-       {
-               Deserialize(stream, std::get<0>(p));
-               Deserialize(stream, std::get<1>(p));
-               Deserialize(stream, std::get<2>(p));
-       }
-       template <typename A, typename B, typename C>
-       static void Deserialize(IStream &stream, std::tuple<A, B, C> *&p)
-       {
-               p = new std::tuple<A, B, C>;
-               Deserialize(stream, *p);
-       }
-
-       // std::map
-       template <typename K, typename T>
-       static void Deserialize(IStream &stream, std::map<K, T> &map)
-       {
-               int length;
-               stream.Read(sizeof(length), &length);
-
-               for (int i = 0; i < length; ++i) {
-                       K key;
-                       T obj;
-                       Deserialize(stream, key);
-                       Deserialize(stream, obj);
-                       map[key] = std::move(obj);
-               }
-       }
-       template <typename K, typename T>
-       static void Deserialize(IStream &stream, std::map<K, T> *&map)
-       {
-               DeserializePtr(stream, map);
-       }
 }; // struct Deserialization
 
 // generic serialization
@@ -515,17 +197,6 @@ struct Deserializer<First, Args...> : public Deserializer<Args...> {
                Deserialization::Deserialize(stream, f);
                Deserializer<Args...>::Deserialize(stream, args...);
        }
-
-       static void Deserialize(IStream &stream, First *&f, Args &... args)
-       {
-               First *tmp = NULL;
-               Deserialization::Deserialize(stream, tmp);
-               std::unique_ptr<First> ptr(tmp);
-               Deserializer<Args...>::Deserialize(stream, args...);
-               ptr.release();
-               f = tmp;
-       }
-
 };
 
 // end of recursion