LCOV - code coverage report
Current view: top level - spb/pb - serialize.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 98.6 % 140 138
Test Date: 2026-10-31 08:41:17 Functions: 71.4 % 1388 991

            Line data    Source code
       1              : /*
       2              :  * Protobuf serialization library - protobuf deserialize library.
       3              :  *
       4              :  * Copyright (c) 2024 Antonin Kriz <antonin.kriz@gmail.com>
       5              :  * Released under the MIT License. See LICENSE in the project root.
       6              :  */
       7              : 
       8              : #pragma once
       9              : 
      10              : #include "../concepts.h"
      11              : #include "../utf8.h"
      12              : #include "wire-types.h"
      13              : #include <cstddef>
      14              : #include <cstdint>
      15              : #include <cstring>
      16              : #include <memory>
      17              : #include <spb/io/io.hpp>
      18              : #include <sys/types.h>
      19              : #include <type_traits>
      20              : 
      21              : namespace spb::pb::detail
      22              : {
      23              : struct ostream_size
      24              : {
      25              :     static constexpr bool size_only = true;
      26              :     size_t size;
      27              : 
      28         3758 :     void write(const void *, size_t data_size)
      29              :     {
      30         3758 :         size += data_size;
      31         3758 :     }
      32              : 
      33        10736 :     void write(uint8_t)
      34              :     {
      35        10736 :         ++size;
      36        10736 :     }
      37              : };
      38              : struct ostream_buffer
      39              : {
      40              :     static constexpr bool size_only = false;
      41              :     uint8_t *p_buffer;
      42              : 
      43         2022 :     explicit ostream_buffer(void *buffer) : p_buffer((uint8_t *)buffer)
      44              :     {
      45         2022 :     }
      46              : 
      47         4805 :     void write(uint8_t byte)
      48              :     {
      49         4805 :         *p_buffer++ = byte;
      50         4805 :     }
      51              : 
      52         1176 :     void write(const void *data, size_t size)
      53              :     {
      54         1176 :         memcpy(p_buffer, data, size);
      55         1176 :         p_buffer += size;
      56         1176 :     }
      57              : };
      58              : 
      59              : struct ostream_writer
      60              : {
      61              :     static constexpr bool size_only = false;
      62              :     spb::io::writer on_write;
      63              :     size_t size = 0;
      64              : 
      65          776 :     explicit ostream_writer(spb::io::writer writer) : on_write(writer)
      66              :     {
      67          776 :     }
      68              : 
      69         2144 :     void write(uint8_t byte)
      70              :     {
      71         2144 :         write(&byte, 1);
      72         2144 :     }
      73              : 
      74         2714 :     void write(const void *data, size_t data_size)
      75              :     {
      76         2714 :         on_write(data, data_size);
      77         2714 :         size += data_size;
      78         2714 :     }
      79              : };
      80              : 
      81              : template <typename T> void check_enum_value_or_throw(enum_type);
      82              : template <serialize_mode = serialize_mode{}> size_t serialize_size(const auto &value);
      83              : template <serialize_mode = serialize_mode{}> size_t serialize_size(uint32_t field, const auto &value);
      84              : template <serialize_mode> void serialize(auto &stream, const spb::detail::proto_message auto &value);
      85              : 
      86         1554 : inline size_t serialize_varint_size(uint64_t value)
      87              : {
      88         1554 :     size_t size = 1;
      89         1554 :     while (value >= 0x80)
      90              :     {
      91            0 :         ++size;
      92            0 :         value >>= 7;
      93              :     }
      94         1554 :     return size;
      95              : }
      96              : 
      97        15898 : void serialize_varint(auto &stream, uint64_t value)
      98              : {
      99        17445 :     while (value >= 0x80)
     100              :     {
     101         1547 :         stream.write((uint8_t)(value & 0x7F) | 0x80);
     102         1547 :         value >>= 7;
     103              :     }
     104        15898 :     stream.write((uint8_t)value);
     105        15898 : }
     106              : 
     107          570 : void serialize_svarint(auto &stream, int64_t value)
     108              : {
     109          570 :     const auto tmp = uint64_t((value << 1) ^ (value >> 63));
     110          570 :     serialize_varint(stream, tmp);
     111          570 : }
     112              : 
     113         7640 : void serialize_tag(auto &stream, uint32_t field_number, wire_type type)
     114              : {
     115         7640 :     const auto tag = (field_number << 3) | uint32_t(type);
     116         7640 :     serialize_varint(stream, tag);
     117         7640 : }
     118              : 
     119              : template <serialize_mode>
     120              : void serialize(auto &stream, uint32_t field, const spb::detail::proto_message auto &value);
     121              : template <serialize_mode>
     122              : void serialize(auto &stream, uint32_t field, const spb::detail::proto_field_string auto &value);
     123              : template <serialize_mode>
     124              : void serialize(auto &stream, uint32_t field, const spb::detail::proto_field_bytes auto &value);
     125              : template <serialize_mode>
     126              : void serialize(auto &stream, uint32_t field, const spb::detail::proto_label_repeated auto &value);
     127              : template <serialize_mode>
     128              : void serialize(auto &stream, uint32_t field, const spb::detail::proto_label_repeated_fixed_size auto &value);
     129              : 
     130              : template <serialize_mode>
     131              : void serialize(auto &stream, uint32_t field, const spb::detail::proto_map auto &value);
     132              : 
     133              : template <serialize_mode mode>
     134         3926 : void serialize(auto &stream, uint32_t field, spb::detail::proto_field_number auto value)
     135              : {
     136         3926 :     serialize_tag(stream, field, to_wire_type(mode.encoder));
     137         3926 :     serialize<mode>(stream, value);
     138         3925 : }
     139              : 
     140          436 : template <serialize_mode mode> void serialize(auto &stream, const spb::detail::proto_enum auto &value)
     141              : {
     142              :     using T = std::remove_cvref_t<decltype(value)>;
     143              : 
     144          436 :     const auto int_enum = (enum_type)value;
     145          436 :     check_enum_value_or_throw<T>(int_enum);
     146          435 :     serialize_varint(stream, int_enum);
     147          435 : }
     148              : 
     149         5780 : template <serialize_mode mode> void serialize(auto &stream, spb::detail::proto_field_int_or_float auto value)
     150              : {
     151              :     using T = std::remove_cvref_t<decltype(value)>;
     152              : 
     153         5780 :     constexpr auto type = encoder_type(mode.encoder);
     154              :     if constexpr (type == scalar_encoder::varint)
     155              :     {
     156              :         static_assert(std::is_integral_v<T>);
     157              : 
     158              :         if constexpr (std::is_same_v<bool, T>)
     159              :         {
     160          240 :             const uint8_t tmp = value ? 1 : 0;
     161          240 :             return stream.write(tmp);
     162              :         }
     163              :         else if constexpr (std::is_signed_v<T>)
     164              :         {
     165              :             //- GPB is serializing all negative ints always as int64_t
     166         1655 :             const auto u_value = uint64_t(int64_t(value));
     167         1655 :             return serialize_varint(stream, u_value);
     168              :         }
     169              :         else
     170              :         {
     171          330 :             return serialize_varint(stream, value);
     172              :         }
     173              :     }
     174              :     else if constexpr (type == scalar_encoder::svarint)
     175              :     {
     176              :         static_assert(std::is_signed_v<T> && std::is_integral_v<T>);
     177              : 
     178          570 :         return serialize_svarint(stream, value);
     179              :     }
     180              :     else if constexpr (type == scalar_encoder::i32)
     181              :     {
     182              :         if constexpr (sizeof(value) == sizeof(uint32_t))
     183              :         {
     184          825 :             return stream.write(&value, sizeof(value));
     185              :         }
     186              :         else
     187              :         {
     188          855 :             const auto tmp = uint32_t(value);
     189          951 :             return stream.write(&tmp, sizeof(tmp));
     190              :         }
     191              :     }
     192              :     else if constexpr (type == scalar_encoder::i64)
     193              :     {
     194              :         if constexpr (sizeof(value) == sizeof(uint64_t))
     195              :         {
     196          735 :             return stream.write(&value, sizeof(value));
     197              :         }
     198              :         else
     199              :         {
     200          570 :             const auto tmp = uint64_t(value);
     201          634 :             return stream.write(&tmp, sizeof(tmp));
     202              :         }
     203              :     }
     204              : }
     205              : 
     206              : template <serialize_mode mode>
     207           60 : void serialize_packed(auto &stream, const spb::detail::proto_label_repeated_fixed_size auto &container)
     208              : {
     209              :     static_assert(is_packed(mode.encoder), "repeated field with fixed size has to have attribute 'packed'");
     210              : 
     211              :     using ValueType = typename std::remove_cvref_t<decltype(container)>::value_type;
     212              : 
     213          600 :     for (size_t i = 0; i < container.size(); i++)
     214              :     {
     215              :         if constexpr (std::is_same_v<ValueType, bool>)
     216              :             serialize<mode>(stream, bool(container[i]));
     217              :         else
     218          240 :             serialize<mode>(stream, container[i]);
     219              :     }
     220           60 : }
     221              : 
     222              : template <serialize_mode mode>
     223           30 : void serialize(auto &stream, const spb::detail::proto_label_repeated_fixed_size auto &container)
     224              : {
     225           30 :     serialize_packed<mode>(stream, container);
     226           30 : }
     227              : 
     228              : template <serialize_mode mode>
     229         1100 : void serialize_packed(auto &stream, const spb::detail::proto_label_repeated auto &container)
     230              : {
     231              :     static_assert(is_packed(mode.encoder), "repeated field has to have attribute 'packed'");
     232              : 
     233              :     using ValueType = typename std::remove_cvref_t<decltype(container)>::value_type;
     234              : 
     235         4190 :     for (const auto &v : container)
     236              :     {
     237              :         if constexpr (std::is_same_v<ValueType, bool>)
     238           90 :             serialize<mode>(stream, bool(v));
     239              :         else
     240         1960 :             serialize<mode>(stream, v);
     241              :     }
     242         1100 : }
     243              : 
     244              : template <serialize_mode mode>
     245          550 : void serialize(auto &stream, const spb::detail::proto_label_repeated auto &container)
     246              : {
     247          550 :     serialize_packed<mode>(stream, container);
     248          550 : }
     249              : 
     250              : template <serialize_mode mode>
     251         1825 : void serialize(auto &stream, uint32_t field, const spb::detail::proto_field_string auto &value)
     252              : {
     253              :     using stream_type = std::remove_cvref_t<decltype(stream)>;
     254              : 
     255              :     if constexpr (!stream_type::size_only && mode.max_size)
     256           64 :         check_size(value.size(), mode.max_size);
     257              : 
     258              :     if constexpr (!stream_type::size_only)
     259          474 :         spb::detail::utf8::validate(std::string_view(value.data(), value.size()));
     260              : 
     261         1809 :     serialize_tag(stream, field, wire_type::length_delimited);
     262         1809 :     serialize_varint(stream, value.size());
     263         1809 :     stream.write(value.data(), value.size());
     264         1809 : }
     265              : 
     266              : template <serialize_mode mode>
     267          726 : void serialize(auto &stream, uint32_t field, const spb::detail::proto_field_bytes auto &value)
     268              : {
     269              :     using stream_type = std::remove_cvref_t<decltype(stream)>;
     270              : 
     271              :     if constexpr (!stream_type::size_only && mode.max_size)
     272           64 :         check_size(value.size(), mode.max_size);
     273              : 
     274          710 :     serialize_tag(stream, field, wire_type::length_delimited);
     275          710 :     serialize_varint(stream, value.size());
     276          710 :     stream.write(value.data(), value.size());
     277          710 : }
     278              : 
     279              : template <serialize_mode mode>
     280          135 : void serialize(auto &stream, uint32_t field, const spb::detail::proto_map auto &value)
     281              : {
     282          135 :     constexpr auto key_encoder   = serialize_mode{.encoder = mode.encoder};
     283          135 :     constexpr auto value_encoder = serialize_mode{.encoder = mode.encoder2};
     284              : 
     285          300 :     for (const auto &[k, v] : value)
     286              :     {
     287          165 :         serialize_tag(stream, field, wire_type::length_delimited);
     288          165 :         serialize_varint(stream, serialize_size<key_encoder>(1, k) + serialize_size<value_encoder>(2, v));
     289          165 :         serialize<key_encoder>(stream, 1, k);
     290          165 :         serialize<value_encoder>(stream, 2, v);
     291              :     }
     292          135 : }
     293              : 
     294              : template <serialize_mode mode>
     295         2398 : void serialize(auto &stream, uint32_t field, const spb::detail::proto_label_repeated auto &container)
     296              : {
     297              :     using value_type  = typename std::remove_cvref_t<decltype(container)>::value_type;
     298              :     using stream_type = std::remove_cvref_t<decltype(stream)>;
     299              : 
     300              :     if constexpr (!stream_type::size_only && mode.max_count)
     301          136 :         check_size(container.size(), mode.max_count);
     302              : 
     303              :     if constexpr (is_packed(mode.encoder))
     304              :     {
     305          865 :         if (container.empty())
     306          315 :             return;
     307              : 
     308          550 :         serialize_tag(stream, field, wire_type::length_delimited);
     309          550 :         serialize_varint(stream, serialize_size<mode>(container));
     310          550 :         serialize_packed<mode>(stream, container);
     311              :     }
     312              :     else
     313              :     {
     314         5123 :         for (const auto &value : container)
     315              :         {
     316              :             if constexpr (std::is_same_v<value_type, bool>)
     317           45 :                 serialize<mode>(stream, field, bool(value));
     318              :             else
     319         2140 :                 serialize<mode>(stream, field, value);
     320              :         }
     321              :     }
     322         1499 : }
     323              : 
     324              : template <serialize_mode mode>
     325           30 : void serialize(auto &stream, uint32_t field,
     326              :                const spb::detail::proto_label_repeated_fixed_size auto &container)
     327              : {
     328              :     static_assert(is_packed(mode.encoder), "repeated field with fixed size has to have attribute 'packed'");
     329              : 
     330           30 :     serialize_tag(stream, field, wire_type::length_delimited);
     331           30 :     serialize_varint(stream, serialize_size<mode>(container));
     332           30 :     serialize_packed<mode>(stream, container);
     333           30 : }
     334              : 
     335              : template <serialize_mode mode>
     336         2022 : void serialize(auto &stream, uint32_t field, const spb::detail::proto_label_optional auto &p_value)
     337              : {
     338         2022 :     if (p_value.has_value())
     339         1752 :         serialize<mode>(stream, field, *p_value);
     340         2006 : }
     341              : 
     342              : template <serialize_mode mode, typename T>
     343              : void serialize(auto &stream, uint32_t field, const std::unique_ptr<T> &p_value)
     344              : {
     345              :     if (p_value)
     346              :         serialize<mode>(stream, field, *p_value);
     347              : }
     348              : 
     349              : template <serialize_mode mode>
     350          450 : void serialize(auto &stream, uint32_t field, const spb::detail::proto_message auto &value)
     351              : {
     352          450 :     serialize_tag(stream, field, wire_type::length_delimited);
     353          450 :     serialize_varint(stream, serialize_size<mode>(value));
     354          450 :     serialize_value(stream, value);
     355          450 : }
     356              : 
     357         4605 : template <serialize_mode mode> auto serialize_size(const auto &value) -> size_t
     358              : {
     359         4605 :     auto stream = ostream_size();
     360         4605 :     serialize<mode>(stream, value);
     361         4604 :     return stream.size;
     362              : }
     363              : 
     364          330 : template <serialize_mode mode> auto serialize_size(uint32_t field, const auto &value) -> size_t
     365              : {
     366          330 :     auto stream = ostream_size();
     367          330 :     serialize<mode>(stream, field, value);
     368          330 :     return stream.size;
     369              : }
     370              : 
     371         4025 : template <serialize_mode mode> void serialize(auto &stream, const spb::detail::proto_message auto &value)
     372              : {
     373         4025 :     serialize_value(stream, value);
     374         4024 : }
     375              : 
     376              : } // namespace spb::pb::detail
        

Generated by: LCOV version 2.0-1