spank-olm

WIP Do not look
git clone git://archive.git.mtrnord.blog/MTRNord/spank-olm.git
Log | Files | Refs | README | LICENSE

commit fd02a9dc5152f894522937b54345806102c7fde5
parent cec50f7a274d09d95ca9e00020b6b59c44f336a8
Author: MTRNord <mtrnord1@gmail.com>
Date:   Mon, 14 Apr 2025 16:39:50 +0200

Fix static functions and the templates

Diffstat:
Minclude/pickle.hpp | 35+++++++++++++++++++++++++++++++----
Msrc/pickle.cpp | 55++-----------------------------------------------------
2 files changed, 33 insertions(+), 57 deletions(-)

diff --git a/include/pickle.hpp b/include/pickle.hpp @@ -98,8 +98,9 @@ namespace spank_olm * @return A pair containing the pointer to the position in the byte array after the deserialized data and the * deserialized OneTimeKey object. */ - static std::pair<std::uint8_t const *, std::optional<OneTimeKey>> unpickle_otk(std::uint8_t const *pos, - std::uint8_t const *end); + std::pair<std::uint8_t const *, std::optional<OneTimeKey>> unpickle_otk(std::uint8_t const *pos, + std::uint8_t const *end); + /** * Serializes a FixedSizeArray object into a byte array. @@ -111,7 +112,15 @@ namespace spank_olm * @return Pointer to the position in the byte array after the serialized data. */ template <typename T, std::size_t max_size> - std::uint8_t *pickle(std::uint8_t *pos, FixedSizeArray<T, max_size> const &list); + std::uint8_t *pickle(std::uint8_t *pos, FixedSizeArray<T, max_size> const &list) + { + pos = pickle(pos, static_cast<std::uint32_t>(list.size())); + for (auto const &value : list) + { + pos = pickle(pos, *value); + } + return pos; + } /** * Deserializes a FixedSizeArray object from a byte array. @@ -124,7 +133,25 @@ namespace spank_olm */ template <std::size_t max_size> std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, - FixedSizeArray<OneTimeKey, max_size> &list); + FixedSizeArray<OneTimeKey, max_size> &list) + { + std::uint32_t size; + pos = unpickle(pos, end, size); + if (!pos) + { + return nullptr; + } + + while (size-- && pos != end) + { + auto [temp_pos, value] = unpickle_otk(pos, end); + if (!((pos = temp_pos))) + return nullptr; + list.insert(value.value()); + } + + return pos; + } /** * Serializes a uint8_t value into a byte array. diff --git a/src/pickle.cpp b/src/pickle.cpp @@ -172,8 +172,8 @@ namespace spank_olm * @return A pair containing the pointer to the position in the byte array after the deserialized data and the * deserialized OneTimeKey object. */ - static std::pair<std::uint8_t const *, std::optional<OneTimeKey>> unpickle_otk(std::uint8_t const *pos, - std::uint8_t const *end) + std::pair<std::uint8_t const *, std::optional<OneTimeKey>> unpickle_otk(std::uint8_t const *pos, + std::uint8_t const *end) { std::uint32_t id; ///< The unique identifier for the one-time key. bool published; ///< Indicates whether the key has been published. @@ -198,57 +198,6 @@ namespace spank_olm } /** - * Serializes a FixedSizeArray object into a byte array. - * - * @tparam T The type of elements in the FixedSizeArray. - * @tparam max_size The maximum size of the FixedSizeArray. - * @param pos Pointer to the current position in the byte array. - * @param list The FixedSizeArray object to serialize. - * @return Pointer to the position in the byte array after the serialized data. - */ - template <typename T, std::size_t max_size> - std::uint8_t *pickle(std::uint8_t *pos, FixedSizeArray<T, max_size> const &list) - { - pos = pickle(pos, static_cast<std::uint32_t>(list.size())); - for (auto const &value : list) - { - pos = pickle(pos, *value); - } - return pos; - } - - /** - * Deserializes a FixedSizeArray object from a byte array. - * - * @tparam max_size The maximum size of the FixedSizeArray. - * @param pos Pointer to the current position in the byte array. - * @param end Pointer to the end of the byte array. - * @param list Reference to the FixedSizeArray object to store the deserialized values. - * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure. - */ - template <std::size_t max_size> - std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, - FixedSizeArray<OneTimeKey, max_size> &list) - { - std::uint32_t size; - pos = unpickle(pos, end, size); - if (!pos) - { - return nullptr; - } - - while (size-- && pos != end) - { - auto [temp_pos, value] = unpickle_otk(pos, end); - if (!((pos = temp_pos))) - return nullptr; - list.insert(value.value()); - } - - return pos; - } - - /** * Serializes a uint8_t value into a byte array. * * @param pos Pointer to the current position in the byte array.