spank-olm

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

pickle.hpp (9056B)


      1 #pragma once
      2 #include <account.hpp>
      3 
      4 namespace spank_olm
      5 {
      6 
      7     /**
      8      * Serializes a 32-bit unsigned integer into a byte array.
      9      *
     10      * @param pos Pointer to the current position in the byte array.
     11      * @param value The 32-bit unsigned integer to serialize.
     12      * @return Pointer to the position in the byte array after the serialized data.
     13      */
     14     std::uint8_t *pickle(std::uint8_t *pos, std::uint32_t value);
     15 
     16     /**
     17      * Deserializes a 32-bit unsigned integer from a byte array.
     18      *
     19      * @param pos Pointer to the current position in the byte array.
     20      * @param end Pointer to the end of the byte array.
     21      * @param value Reference to the 32-bit unsigned integer to store the deserialized value.
     22      * @return Pointer to the position in the byte array after the deserialized data, or
     23      * nullptr on failure.
     24      */
     25     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, std::uint32_t &value);
     26 
     27     /**
     28      * Serializes a boolean value into a byte array.
     29      *
     30      * @param pos Pointer to the current position in the byte array.
     31      * @param value The boolean value to serialize.
     32      * @return Pointer to the position in the byte array after the serialized data.
     33      */
     34     std::uint8_t *pickle(std::uint8_t *pos, bool value);
     35 
     36     /**
     37      * Deserializes a boolean value from a byte array.
     38      *
     39      * @param pos Pointer to the current position in the byte array.
     40      * @param end Pointer to the end of the byte array.
     41      * @param value Reference to the boolean value to store the deserialized value.
     42      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
     43      */
     44     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, bool &value);
     45 
     46     /**
     47      * Serializes a Botan::secure_vector<uint8_t> into a byte array.
     48      *
     49      * @param pos Pointer to the current position in the byte array.
     50      * @param value The Botan::secure_vector<uint8_t> to serialize.
     51      * @return Pointer to the position in the byte array after the serialized data.
     52      */
     53     std::uint8_t *pickle(std::uint8_t *pos, const Botan::secure_vector<uint8_t> &value);
     54 
     55     /**
     56      * Deserializes a Botan::secure_vector<uint8_t> from a byte array.
     57      *
     58      * @param pos Pointer to the current position in the byte array.
     59      * @param end Pointer to the end of the byte array.
     60      * @param value Reference to the Botan::secure_vector<uint8_t> to store the deserialized value.
     61      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
     62      */
     63     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end,
     64                                  Botan::secure_vector<uint8_t> &value);
     65 
     66     /**
     67      * Serializes a std::vector<uint8_t> into a byte array.
     68      *
     69      * @param pos Pointer to the current position in the byte array.
     70      * @param value The Botan::secure_vector<uint8_t> to serialize.
     71      * @return Pointer to the position in the byte array after the serialized data.
     72      */
     73     std::uint8_t *pickle(std::uint8_t *pos, const std::vector<uint8_t> &value);
     74 
     75     /**
     76      * Deserializes a std::vector<uint8_t> from a byte array.
     77      *
     78      * @param pos Pointer to the current position in the byte array.
     79      * @param end Pointer to the end of the byte array.
     80      * @param value Reference to the Botan::secure_vector<uint8_t> to store the deserialized value.
     81      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
     82      */
     83     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, std::vector<uint8_t> &value);
     84 
     85     /**
     86      *
     87      * @param pos Pointer to the current position in the byte array.
     88      * @param value A OneTimeKey object to serialize.
     89      * @return Pointer to the position in the byte array after the serialized data.
     90      */
     91     std::uint8_t *pickle(std::uint8_t *pos, const std::optional<OneTimeKey> &value);
     92 
     93     /**
     94      * Deserializes a OneTimeKey object from a byte array.
     95      *
     96      * @param pos Pointer to the current position in the byte array.
     97      * @param end Pointer to the end of the byte array.
     98      * @return A pair containing the pointer to the position in the byte array after the deserialized data and the
     99      * deserialized OneTimeKey object.
    100      */
    101     std::pair<std::uint8_t const *, std::optional<OneTimeKey>> unpickle_otk(std::uint8_t const *pos,
    102                                                                             std::uint8_t const *end);
    103 
    104 
    105     /**
    106      * Serializes a FixedSizeArray object into a byte array.
    107      *
    108      * @tparam T The type of elements in the FixedSizeArray.
    109      * @tparam max_size The maximum size of the FixedSizeArray.
    110      * @param pos Pointer to the current position in the byte array.
    111      * @param list The FixedSizeArray object to serialize.
    112      * @return Pointer to the position in the byte array after the serialized data.
    113      */
    114     template <typename T, std::size_t max_size>
    115     std::uint8_t *pickle(std::uint8_t *pos, FixedSizeArray<T, max_size> const &list)
    116     {
    117         pos = pickle(pos, static_cast<std::uint32_t>(list.size()));
    118         for (auto const &value : list)
    119         {
    120             pos = pickle(pos, *value);
    121         }
    122         return pos;
    123     }
    124 
    125     /**
    126      * Deserializes a FixedSizeArray object from a byte array.
    127      *
    128      * @tparam max_size The maximum size of the FixedSizeArray.
    129      * @param pos Pointer to the current position in the byte array.
    130      * @param end Pointer to the end of the byte array.
    131      * @param list Reference to the FixedSizeArray object to store the deserialized values.
    132      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
    133      */
    134     template <std::size_t max_size>
    135     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end,
    136                                  FixedSizeArray<OneTimeKey, max_size> &list)
    137     {
    138         std::uint32_t size;
    139         pos = unpickle(pos, end, size);
    140         if (!pos)
    141         {
    142             return nullptr;
    143         }
    144 
    145         while (size-- && pos != end)
    146         {
    147             auto [temp_pos, value] = unpickle_otk(pos, end);
    148             if (!((pos = temp_pos)))
    149                 return nullptr;
    150             list.insert(value.value());
    151         }
    152 
    153         return pos;
    154     }
    155 
    156     /**
    157      * Serializes a uint8_t value into a byte array.
    158      *
    159      * @param pos Pointer to the current position in the byte array.
    160      * @param value The uint8_t value to serialize.
    161      * @return Pointer to the position in the byte array after the serialized data.
    162      */
    163     std::uint8_t *pickle(std::uint8_t *pos, std::uint8_t value);
    164 
    165     /**
    166      * Deserializes a uint8_t value from a byte array.
    167      *
    168      * @param pos Pointer to the current position in the byte array.
    169      * @param end Pointer to the end of the byte array.
    170      * @param value Reference to the uint8_t value to store the deserialized value.
    171      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
    172      */
    173     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, std::uint8_t &value);
    174 
    175     /**
    176      * Serializes an optional IdentityKeys object into a byte array.
    177      *
    178      * @param pos Pointer to the current position in the byte array.
    179      * @param value The optional IdentityKeys object to serialize.
    180      * @return Pointer to the position in the byte array after the serialized data.
    181      */
    182     std::uint8_t *pickle(std::uint8_t *pos, const std::optional<IdentityKeys> &value);
    183 
    184     /**
    185      * Deserializes an optional IdentityKeys object from a byte array.
    186      *
    187      * @param pos Pointer to the current position in the byte array.
    188      * @param end Pointer to the end of the byte array.
    189      * @param value Reference to the optional IdentityKeys object to store the deserialized value.
    190      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
    191      */
    192     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, std::optional<IdentityKeys> &value);
    193 
    194     /**
    195      * Deserializes an optional OneTimeKey object from a byte array.
    196      *
    197      * @param pos Pointer to the current position in the byte array.
    198      * @param end Pointer to the end of the byte array.
    199      * @param value Reference to the optional OneTimeKey object to store the deserialized value.
    200      * @return Pointer to the position in the byte array after the deserialized data, or nullptr on failure.
    201      */
    202     std::uint8_t const *unpickle(std::uint8_t const *pos, std::uint8_t const *end, std::optional<OneTimeKey> &value);
    203 
    204     std::uint8_t *pickle_bytes(std::uint8_t *pos, const std::uint8_t *bytes, const std::size_t bytes_length);
    205 
    206     std::uint8_t const *unpickle_bytes(const std::uint8_t *pos, const std::uint8_t *end, std::uint8_t *bytes,
    207                                        const std::size_t bytes_length);
    208 } // namespace spank_olm