LCOV - code coverage report
Current view: top level - spb/pb - deserialize.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.6 % 299 283
Test Date: 2026-10-31 08:41:17 Functions: 82.3 % 1238 1019

            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 "../bits.h"
      11              : #include "../concepts.h"
      12              : #include "../utf8.h"
      13              : #include "wire-types.h"
      14              : #include <climits>
      15              : #include <cstddef>
      16              : #include <cstdint>
      17              : #include <cstring>
      18              : #include <limits>
      19              : #include <memory>
      20              : #include <spb/io/io.hpp>
      21              : #include <stdexcept>
      22              : #include <string_view>
      23              : #include <type_traits>
      24              : 
      25              : namespace spb::pb::detail
      26              : {
      27              : 
      28              : struct istream_reader
      29              : {
      30              :     size_t bytes_left;
      31              :     size_t consumed_bytes = 0;
      32              :     spb::io::reader on_read;
      33              : 
      34         1546 :     istream_reader(spb::io::reader reader, size_t size = std::numeric_limits<size_t>::max()) noexcept
      35         1546 :         : bytes_left(size), on_read(reader)
      36              :     {
      37         1546 :     }
      38          776 :     size_t consumed_size() const noexcept
      39              :     {
      40          776 :         return consumed_bytes;
      41              :     }
      42         4366 :     size_t size() const noexcept
      43              :     {
      44         4366 :         return bytes_left;
      45              :     }
      46         2310 :     bool empty() const noexcept
      47              :     {
      48         2310 :         return bytes_left == 0;
      49              :     }
      50              : 
      51         3096 :     [[nodiscard]] size_t read(void *data, size_t data_size)
      52              :     {
      53         3096 :         data_size = std::min(data_size, size());
      54         3096 :         if (data_size == 0) [[unlikely]]
      55            0 :             return 0;
      56              : 
      57         3096 :         const auto size = on_read(data, data_size);
      58         3096 :         bytes_left -= size;
      59         3096 :         consumed_bytes += size;
      60         3096 :         return size;
      61              :     }
      62              : 
      63         1314 :     [[nodiscard]] uint8_t read_byte_or_throw()
      64              :     {
      65              :         uint8_t result;
      66              : 
      67         1314 :         if (read(&result, sizeof(result)) != sizeof(result)) [[unlikely]]
      68            0 :             throw std::runtime_error("unexpected end of stream");
      69              : 
      70         1314 :         return result;
      71              :     }
      72              : 
      73         1218 :     [[nodiscard]] int read_byte_or_eof()
      74              :     {
      75              :         uint8_t result;
      76         2436 :         return read(&result, sizeof(result)) == sizeof(result) ? result : -1;
      77              :     }
      78              : 
      79          570 :     void read_exact_or_throw(void *data, size_t data_size)
      80              :     {
      81         1134 :         while (data_size > 0)
      82              :         {
      83          564 :             auto chunk_size = read(data, data_size);
      84          564 :             if (chunk_size == 0) [[unlikely]]
      85            0 :                 throw std::runtime_error("unexpected end of stream");
      86              : 
      87          564 :             data_size -= chunk_size;
      88              :         }
      89          570 :     }
      90              : 
      91          770 :     [[nodiscard]] istream_reader sub_stream(size_t sub_size)
      92              :     {
      93          770 :         if (size() < sub_size) [[unlikely]]
      94            0 :             throw std::runtime_error("unexpected end of stream");
      95              : 
      96          770 :         bytes_left -= sub_size;
      97          770 :         consumed_bytes += sub_size;
      98          770 :         return istream_reader(on_read, sub_size);
      99              :     }
     100              : 
     101            0 :     void skip_or_throw(size_t size)
     102              :     {
     103              :         uint8_t buffer[128];
     104            0 :         while (size > 0)
     105              :         {
     106            0 :             const auto chunk_size = std::min(size, sizeof(buffer));
     107            0 :             read_exact_or_throw(buffer, chunk_size);
     108            0 :             size -= chunk_size;
     109              :         }
     110            0 :     }
     111              : };
     112              : 
     113              : struct istream_buffer
     114              : {
     115              :     const uint8_t *p_start;
     116              :     const uint8_t *p_end;
     117              : 
     118              :     istream_buffer(const uint8_t *start, const uint8_t *end) noexcept : p_start(start), p_end(end)
     119              :     {
     120              :         assert(start <= end);
     121              :     }
     122         3763 :     istream_buffer(const uint8_t *start, size_t size) noexcept : p_start(start), p_end(start + size)
     123              :     {
     124         3763 :     }
     125         5905 :     size_t size() const noexcept
     126              :     {
     127         5905 :         return p_end - p_start;
     128              :     }
     129         5625 :     bool empty() const noexcept
     130              :     {
     131         5625 :         return p_start >= p_end;
     132              :     }
     133              : 
     134         1276 :     [[nodiscard]] size_t read(void *data, size_t data_size) noexcept
     135              :     {
     136         1276 :         data_size = std::min(data_size, size());
     137         1276 :         memcpy(data, p_start, data_size);
     138         1276 :         p_start += data_size;
     139         1276 :         return data_size;
     140              :     }
     141              : 
     142         3464 :     [[nodiscard]] uint8_t read_byte_or_throw()
     143              :     {
     144         3464 :         if (p_start >= p_end) [[unlikely]]
     145           43 :             throw std::runtime_error("unexpected end of stream");
     146              : 
     147         3421 :         return *p_start++;
     148              :     }
     149              : 
     150         2226 :     [[nodiscard]] int read_byte_or_eof() noexcept
     151              :     {
     152         2226 :         if (p_start < p_end) [[likely]]
     153         2226 :             return *p_start++;
     154              : 
     155            0 :         return -1;
     156              :     }
     157              : 
     158         1276 :     void read_exact_or_throw(void *data, size_t data_size)
     159              :     {
     160         1276 :         if (read(data, data_size) != data_size) [[unlikely]]
     161           20 :             throw std::runtime_error("unexpected end of stream");
     162         1256 :     }
     163              : 
     164         1813 :     [[nodiscard]] istream_buffer sub_stream(size_t sub_size)
     165              :     {
     166         1813 :         if (size() < sub_size) [[unlikely]]
     167           43 :             throw std::runtime_error("unexpected end of stream");
     168              : 
     169         1770 :         const auto sub_start = p_start;
     170         1770 :         p_start += sub_size;
     171         1770 :         return istream_buffer(sub_start, sub_size);
     172              :     }
     173              : 
     174           74 :     void skip_or_throw(size_t size)
     175              :     {
     176           74 :         if (this->size() < size) [[unlikely]]
     177            6 :             throw std::runtime_error("unexpected end of stream");
     178              : 
     179           68 :         p_start += size;
     180           68 :     }
     181              : };
     182              : 
     183              : void skip(auto &stream, wire_type);
     184              : 
     185              : template <typename T> void check_enum_value_or_throw(enum_type);
     186              : 
     187              : template <serialize_mode, size_t ordinal, typename T>
     188              : void deserialize_variant(auto &stream, T &variant, wire_type type);
     189              : 
     190              : template <serialize_mode, typename T>
     191              : auto deserialize_bitfield(auto &stream, uint32_t bits, wire_type type) -> T;
     192              : 
     193         6198 : [[nodiscard]] inline auto wire_type_from_tag(tag_type tag) -> wire_type
     194              : {
     195         6198 :     return wire_type(uint32_t(tag) & 0x07);
     196              : }
     197              : 
     198         6302 : [[nodiscard]] inline auto field_from_tag(tag_type tag) -> uint32_t
     199              : {
     200         6302 :     return uint32_t(tag) >> 3;
     201              : }
     202              : 
     203         3187 : inline void check_tag_or_throw(tag_type tag)
     204              : {
     205         3187 :     if (field_from_tag(tag) == 0) [[unlikely]]
     206            1 :         throw std::runtime_error("invalid field id");
     207         3186 : }
     208              : 
     209         6025 : inline void check_wire_type_or_throw(wire_type type1, wire_type type2)
     210              : {
     211         6025 :     if (type1 != type2) [[unlikely]]
     212            9 :         throw std::runtime_error("invalid wire type");
     213         6016 : }
     214              : 
     215         1278 : void check_if_empty_or_throw(auto &stream)
     216              : {
     217         1278 :     if (!stream.empty()) [[unlikely]]
     218            4 :         throw std::runtime_error("unexpected data in stream");
     219         1274 : }
     220              : 
     221         3444 : [[nodiscard]] auto read_tag_or_eof(auto &stream) -> tag_type
     222              : {
     223         3444 :     auto byte_or_eof = stream.read_byte_or_eof();
     224         3444 :     if (byte_or_eof < 0) [[unlikely]]
     225          388 :         return tag_type::invalid;
     226              : 
     227         3056 :     auto byte = (uint8_t)(byte_or_eof);
     228         3056 :     auto tag  = (uint32_t)(byte & 0x7F);
     229              : 
     230         3091 :     for (size_t shift = CHAR_BIT - 1; (byte & 0x80) != 0; shift += CHAR_BIT - 1)
     231              :     {
     232           36 :         if (shift >= sizeof(tag) * CHAR_BIT) [[unlikely]]
     233            1 :             throw std::runtime_error("invalid tag");
     234              : 
     235           35 :         byte = stream.read_byte_or_throw();
     236           35 :         tag |= uint64_t(byte & 0x7F) << shift;
     237              :     }
     238              : 
     239         3055 :     const auto result = tag_type(tag);
     240         3055 :     check_tag_or_throw(result);
     241         3054 :     return result;
     242              : }
     243              : 
     244         3908 : template <typename T> [[nodiscard]] auto read_varint(auto &stream) -> T
     245              : {
     246              :     if constexpr (std::is_same_v<T, bool>)
     247              :     {
     248           86 :         switch (stream.read_byte_or_throw())
     249              :         {
     250           30 :         case 0:
     251           30 :             return false;
     252           46 :         case 1:
     253           46 :             return true;
     254            6 :         default:
     255            6 :             throw std::runtime_error("invalid varint for bool");
     256              :         }
     257              :     }
     258              :     else
     259              :     {
     260         3822 :         auto value = uint64_t(0);
     261              : 
     262         4671 :         for (auto shift = 0U; shift < sizeof(value) * CHAR_BIT; shift += CHAR_BIT - 1)
     263              :         {
     264         4657 :             uint8_t byte = stream.read_byte_or_throw();
     265         4618 :             value |= uint64_t(byte & 0x7F) << shift;
     266         4618 :             if ((byte & 0x80) == 0)
     267              :             {
     268              :                 if constexpr (std::is_signed_v<T> && sizeof(T) < sizeof(value))
     269              :                 {
     270              :                     //- GPB encodes signed varints always as 64-bits
     271              :                     //- so int32_t(-2) is encoded as "\xfe\xff\xff\xff\xff\xff\xff\xff\xff\x01",
     272              :                     // same as int64_t(-2)
     273              :                     //- but it should be encoded as  "\xfe\xff\xff\xff\x0f"
     274          627 :                     value = T(value);
     275              :                 }
     276         3769 :                 auto result = T(value);
     277              :                 if constexpr (std::is_signed_v<T>)
     278              :                 {
     279          701 :                     if (result == std::make_signed_t<T>(value)) [[likely]]
     280          701 :                         return result;
     281              :                 }
     282              :                 else
     283              :                 {
     284         3068 :                     if (result == value) [[likely]]
     285         3062 :                         return result;
     286              :                 }
     287              : 
     288            6 :                 break;
     289              :             }
     290              :         }
     291           20 :         throw std::runtime_error("invalid varint");
     292              :     }
     293              : }
     294              : 
     295              : template <serialize_mode>
     296              : void deserialize(auto &stream, spb::detail::proto_message auto &value, wire_type type);
     297              : template <serialize_mode>
     298              : void deserialize(auto &stream, spb::detail::proto_field_int_or_float auto &value, wire_type type);
     299              : template <serialize_mode>
     300              : void deserialize(auto &stream, spb::detail::proto_field_bytes auto &value, wire_type type);
     301              : template <serialize_mode>
     302              : void deserialize(auto &stream, spb::detail::proto_field_string auto &value, wire_type type);
     303              : template <serialize_mode, spb::detail::proto_label_repeated Container>
     304              : void deserialize(auto &stream, Container &value, wire_type type);
     305              : template <serialize_mode, spb::detail::proto_label_repeated_fixed_size Container>
     306              : void deserialize(auto &stream, Container &value, wire_type type);
     307              : template <serialize_mode, spb::detail::proto_label_optional Container>
     308              : void deserialize(auto &stream, Container &p_value, wire_type type);
     309              : 
     310              : template <serialize_mode> void deserialize(auto &stream, spb::detail::proto_map auto &value, wire_type type);
     311              : 
     312              : template <serialize_mode, typename T>
     313              : void deserialize(auto &stream, std::unique_ptr<T> &value, wire_type type);
     314              : 
     315          220 : template <typename T, typename signedT, typename unsignedT> auto create_tmp_var()
     316              : {
     317              :     if constexpr (std::is_signed<T>::value)
     318              :     {
     319           88 :         return signedT();
     320              :     }
     321              :     else
     322              :     {
     323          132 :         return unsignedT();
     324              :     }
     325              : }
     326              : 
     327              : template <serialize_mode mode, typename T>
     328          564 : auto deserialize_bitfield(auto &stream, uint32_t bits, wire_type type) -> T
     329              : {
     330          564 :     auto value = T();
     331              :     if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::svarint)
     332              :     {
     333           52 :         check_wire_type_or_throw(type, wire_type::varint);
     334              : 
     335           52 :         auto tmp = read_varint<std::make_unsigned_t<T>>(stream);
     336           52 :         value    = T((tmp >> 1) ^ (~(tmp & 1) + 1));
     337              :     }
     338              :     else if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::varint)
     339              :     {
     340          116 :         check_wire_type_or_throw(type, wire_type::varint);
     341          116 :         value = read_varint<T>(stream);
     342              :     }
     343              :     else if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::i32)
     344              :     {
     345              :         static_assert(sizeof(T) <= sizeof(uint32_t));
     346              : 
     347          220 :         check_wire_type_or_throw(type, wire_type::fixed32);
     348              : 
     349              :         if constexpr (sizeof(value) == sizeof(uint32_t))
     350              :         {
     351           88 :             stream.read_exact_or_throw(&value, sizeof(value));
     352              :         }
     353              :         else
     354              :         {
     355          132 :             auto tmp = create_tmp_var<T, int32_t, uint32_t>();
     356          132 :             stream.read_exact_or_throw(&tmp, sizeof(tmp));
     357          132 :             spb::detail::check_if_value_fit_in_bits(tmp, bits);
     358          108 :             value = T(tmp);
     359              :         }
     360              :     }
     361              :     else if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::i64)
     362              :     {
     363              :         static_assert(sizeof(T) <= sizeof(uint64_t));
     364          176 :         check_wire_type_or_throw(type, wire_type::fixed64);
     365              : 
     366              :         if constexpr (sizeof(value) == sizeof(uint64_t))
     367              :         {
     368           88 :             stream.read_exact_or_throw(&value, sizeof(value));
     369              :         }
     370              :         else
     371              :         {
     372           88 :             auto tmp = create_tmp_var<T, int64_t, uint64_t>();
     373           88 :             stream.read_exact_or_throw(&tmp, sizeof(tmp));
     374           88 :             spb::detail::check_if_value_fit_in_bits(tmp, bits);
     375           72 :             value = T(tmp);
     376              :         }
     377              :     }
     378          520 :     spb::detail::check_if_value_fit_in_bits(value, bits);
     379          468 :     return value;
     380              : }
     381              : 
     382              : template <serialize_mode mode>
     383          125 : void deserialize(auto &stream, spb::detail::proto_enum auto &value, wire_type type)
     384              : {
     385              :     using T = std::remove_cvref_t<decltype(value)>;
     386              : 
     387              :     if constexpr (!is_packed(mode.encoder))
     388              :     {
     389          125 :         check_wire_type_or_throw(type, wire_type::varint);
     390              :     }
     391          125 :     const auto int_enum = read_varint<enum_type>(stream);
     392          125 :     check_enum_value_or_throw<T>(int_enum);
     393          120 :     value = (T)int_enum;
     394          120 : }
     395              : 
     396              : template <serialize_mode mode>
     397         1556 : void deserialize(auto &stream, spb::detail::proto_field_int_or_float auto &value, wire_type type)
     398              : {
     399              :     using T = std::remove_cvref_t<decltype(value)>;
     400              : 
     401              :     if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::svarint)
     402              :     {
     403              :         if constexpr (!is_packed(mode.encoder))
     404              :         {
     405          160 :             check_wire_type_or_throw(type, wire_type::varint);
     406              :         }
     407          160 :         auto tmp = read_varint<std::make_unsigned_t<T>>(stream);
     408          146 :         value    = T((tmp >> 1) ^ (~(tmp & 1) + 1));
     409              :     }
     410              :     else if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::varint)
     411              :     {
     412              :         if constexpr (!is_packed(mode.encoder))
     413              :         {
     414          654 :             check_wire_type_or_throw(type, wire_type::varint);
     415              :         }
     416          652 :         value = read_varint<T>(stream);
     417              :     }
     418              :     else if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::i32)
     419              :     {
     420              :         static_assert(sizeof(T) <= sizeof(uint32_t));
     421              : 
     422              :         if constexpr (!is_packed(mode.encoder))
     423              :         {
     424          420 :             check_wire_type_or_throw(type, wire_type::fixed32);
     425              :         }
     426              :         if constexpr (sizeof(value) == sizeof(uint32_t))
     427              :         {
     428          220 :             stream.read_exact_or_throw(&value, sizeof(value));
     429              :         }
     430              :         else
     431              :         {
     432              :             if constexpr (std::is_signed_v<T>)
     433              :             {
     434           66 :                 auto tmp = int32_t(0);
     435           66 :                 stream.read_exact_or_throw(&tmp, sizeof(tmp));
     436           64 :                 if (tmp > std::numeric_limits<T>::max() || tmp < std::numeric_limits<T>::min()) [[unlikely]]
     437            4 :                     throw std::runtime_error("int overflow");
     438              : 
     439           60 :                 value = T(tmp);
     440              :             }
     441              :             else
     442              :             {
     443          132 :                 auto tmp = uint32_t(0);
     444          132 :                 stream.read_exact_or_throw(&tmp, sizeof(tmp));
     445          128 :                 if (tmp > std::numeric_limits<T>::max()) [[unlikely]]
     446            8 :                     throw std::runtime_error("int overflow");
     447              : 
     448          120 :                 value = T(tmp);
     449              :             }
     450              :         }
     451              :     }
     452              :     else if constexpr (scalar_encoder(mode.encoder) == scalar_encoder::i64)
     453              :     {
     454              :         static_assert(sizeof(T) <= sizeof(uint64_t));
     455              :         if constexpr (!is_packed(mode.encoder))
     456              :         {
     457          322 :             check_wire_type_or_throw(type, wire_type::fixed64);
     458              :         }
     459              :         if constexpr (sizeof(value) == sizeof(uint64_t))
     460              :         {
     461          188 :             stream.read_exact_or_throw(&value, sizeof(value));
     462              :         }
     463              :         else
     464              :         {
     465              :             if constexpr (std::is_signed_v<T>)
     466              :             {
     467           66 :                 auto tmp = int64_t(0);
     468           66 :                 stream.read_exact_or_throw(&tmp, sizeof(tmp));
     469           64 :                 if (tmp > std::numeric_limits<T>::max() || tmp < std::numeric_limits<T>::min()) [[unlikely]]
     470            4 :                     throw std::runtime_error("int overflow");
     471              : 
     472           60 :                 value = T(tmp);
     473              :             }
     474              :             else
     475              :             {
     476           66 :                 auto tmp = uint64_t(0);
     477           66 :                 stream.read_exact_or_throw(&tmp, sizeof(tmp));
     478           64 :                 if (tmp > std::numeric_limits<T>::max()) [[unlikely]]
     479            4 :                     throw std::runtime_error("int overflow");
     480              : 
     481           60 :                 value = T(tmp);
     482              :             }
     483              :         }
     484              :     }
     485         1464 : }
     486              : 
     487              : template <serialize_mode mode, spb::detail::proto_label_optional Container>
     488          549 : void deserialize(auto &stream, Container &p_value, wire_type type)
     489              : {
     490          669 :     auto &value = p_value.emplace(typename Container::value_type());
     491          549 :     deserialize<mode>(stream, value, type);
     492          511 : }
     493              : 
     494              : template <serialize_mode mode>
     495          493 : void deserialize(auto &stream, spb::detail::proto_field_string auto &value, wire_type type)
     496              : {
     497          493 :     check_wire_type_or_throw(type, wire_type::length_delimited);
     498              :     if constexpr (mode.max_size)
     499           64 :         check_size(stream.size(), mode.max_size);
     500              : 
     501              :     if constexpr (spb::detail::proto_field_string_resizable<decltype(value)>)
     502              :     {
     503          416 :         value.resize(stream.size());
     504              :     }
     505              :     else
     506              :     {
     507           58 :         if (value.size() != stream.size()) [[unlikely]]
     508           14 :             throw std::runtime_error("invalid string size");
     509              :     }
     510          504 :     stream.read_exact_or_throw(value.data(), stream.size());
     511          504 :     spb::detail::utf8::validate(std::string_view(value.data(), value.size()));
     512          459 : }
     513              : 
     514              : template <serialize_mode mode, typename T>
     515              : void deserialize(auto &stream, std::unique_ptr<T> &value, wire_type type)
     516              : {
     517              :     value = std::make_unique<T>();
     518              :     deserialize<mode>(stream, *value, type);
     519              : }
     520              : 
     521              : template <serialize_mode mode>
     522          280 : void deserialize(auto &stream, spb::detail::proto_field_bytes auto &value, wire_type type)
     523              : {
     524          280 :     check_wire_type_or_throw(type, wire_type::length_delimited);
     525              : 
     526              :     if constexpr (mode.max_size)
     527           64 :         check_size(stream.size(), mode.max_size);
     528              : 
     529              :     if constexpr (spb::detail::proto_field_bytes_resizable<decltype(value)>)
     530              :     {
     531          216 :         value.resize(stream.size());
     532              :     }
     533              :     else
     534              :     {
     535           96 :         if (stream.size() != value.size()) [[unlikely]]
     536           12 :             throw std::runtime_error("invalid bytes size");
     537              :     }
     538          288 :     stream.read_exact_or_throw(value.data(), stream.size());
     539          252 : }
     540              : 
     541              : template <serialize_mode mode, spb::detail::proto_label_repeated Container>
     542          330 : void deserialize_packed(auto &stream, Container &value)
     543              : {
     544              :     static_assert(is_packed(mode.encoder));
     545              : 
     546          848 :     while (!stream.empty())
     547              :     {
     548              :         if constexpr (mode.max_count)
     549          222 :             check_size(value.size() + 1, mode.max_count);
     550              : 
     551              :         if constexpr (std::is_same_v<typename Container::value_type, bool>)
     552              :         {
     553           20 :             value.emplace_back(read_varint<bool>(stream));
     554              :         }
     555              :         else
     556              :         {
     557          510 :             deserialize<reset_packed(mode)>(stream, value.emplace_back(), to_wire_type(mode.encoder));
     558              :         }
     559              :     }
     560          288 : }
     561              : 
     562              : template <serialize_mode mode, spb::detail::proto_label_repeated_fixed_size Container>
     563           20 : void deserialize_packed(auto &stream, Container &value)
     564              : {
     565              :     static_assert(is_packed(mode.encoder));
     566              : 
     567              :     using value_type = typename Container::value_type;
     568              : 
     569          116 :     for (size_t i = 0; i < value.size(); i++)
     570              :     {
     571              :         if constexpr (std::is_same_v<value_type, bool>)
     572              :         {
     573              :             value[i] = read_varint<bool>(stream);
     574              :         }
     575              :         else
     576              :         {
     577              :             value_type tmp;
     578           80 :             deserialize<reset_packed(mode)>(stream, tmp, to_wire_type(mode.encoder));
     579           76 :             value[i] = tmp;
     580              :         }
     581              :     }
     582           16 :     check_if_empty_or_throw(stream);
     583           12 : }
     584              : 
     585              : template <serialize_mode mode, spb::detail::proto_label_repeated_fixed_size Container>
     586           20 : void deserialize(auto &stream, Container &value, wire_type type)
     587              : {
     588              :     static_assert(is_packed(mode.encoder), "repeated field with fixed size has to have attribute 'packed'");
     589              : 
     590           20 :     check_wire_type_or_throw(type, wire_type::length_delimited);
     591           20 :     deserialize_packed<mode>(stream, value);
     592           12 : }
     593              : 
     594              : template <serialize_mode mode, spb::detail::proto_label_repeated Container>
     595         1154 : void deserialize(auto &stream, Container &value, wire_type type)
     596              : {
     597              :     if constexpr (is_packed(mode.encoder))
     598              :     {
     599          330 :         deserialize_packed<mode>(stream, value);
     600              :     }
     601              :     else
     602              :     {
     603              :         if constexpr (mode.max_count)
     604          356 :             check_size(value.size() + 1, mode.max_count);
     605              : 
     606              :         if constexpr (std::is_same_v<typename Container::value_type, bool>)
     607              :         {
     608           26 :             value.emplace_back(read_varint<bool>(stream));
     609              :         }
     610              :         else
     611              :         {
     612          794 :             deserialize<mode>(stream, value.emplace_back(), type);
     613              :         }
     614              :     }
     615         1096 : }
     616              : 
     617              : template <serialize_mode mode>
     618           66 : void deserialize(auto &stream, spb::detail::proto_map auto &value, wire_type type)
     619              : {
     620              :     using map_type    = std::remove_cvref_t<decltype(value)>;
     621              :     using key_type    = typename map_type::key_type;
     622              :     using mapped_type = typename map_type::mapped_type;
     623              : 
     624           66 :     constexpr auto key_encoder   = serialize_mode{.encoder = mode.encoder};
     625           66 :     constexpr auto value_encoder = serialize_mode{.encoder = mode.encoder2};
     626              : 
     627           66 :     check_wire_type_or_throw(type, wire_type::length_delimited);
     628              : 
     629           66 :     auto pair                 = std::pair<key_type, mapped_type>();
     630           66 :     auto key_or_value_defined = false;
     631          198 :     while (!stream.empty())
     632              :     {
     633          132 :         const auto tag          = tag_type(read_varint<uint32_t>(stream));
     634          132 :         const auto field_number = field_from_tag(tag);
     635          132 :         const auto field_type   = wire_type_from_tag(tag);
     636              : 
     637          132 :         check_tag_or_throw(tag);
     638              : 
     639          132 :         switch (field_number)
     640              :         {
     641           66 :         case 1:
     642              :             if constexpr (std::is_integral_v<key_type>)
     643              :             {
     644           30 :                 deserialize<key_encoder>(stream, pair.first, field_type);
     645              :             }
     646              :             else
     647              :             {
     648           36 :                 if (field_type == wire_type::length_delimited)
     649              :                 {
     650           36 :                     const auto size = read_varint<uint32_t>(stream);
     651           36 :                     auto substream  = stream.sub_stream(size);
     652           36 :                     deserialize<key_encoder>(substream, pair.first, field_type);
     653           36 :                     check_if_empty_or_throw(substream);
     654              :                 }
     655              :                 else
     656              :                 {
     657            0 :                     deserialize<key_encoder>(stream, pair.first, field_type);
     658              :                 }
     659              :             }
     660           66 :             key_or_value_defined = true;
     661           66 :             break;
     662           66 :         case 2:
     663              :             if constexpr (spb::detail::proto_field_number<mapped_type>)
     664              :             {
     665           30 :                 deserialize<value_encoder>(stream, pair.second, field_type);
     666              :             }
     667              :             else
     668              :             {
     669           36 :                 if (field_type == wire_type::length_delimited)
     670              :                 {
     671           36 :                     const auto size = read_varint<uint32_t>(stream);
     672           36 :                     auto substream  = stream.sub_stream(size);
     673           36 :                     deserialize<value_encoder>(substream, pair.second, field_type);
     674           36 :                     check_if_empty_or_throw(substream);
     675              :                 }
     676            0 :                 else [[unlikely]]
     677              :                 {
     678            0 :                     throw std::runtime_error("invalid field");
     679              :                 }
     680              :             }
     681           66 :             key_or_value_defined = true;
     682           66 :             break;
     683            0 :         default:
     684            0 :             throw std::runtime_error("invalid field");
     685              :         }
     686              :     }
     687           66 :     if (key_or_value_defined) [[likely]]
     688              :     {
     689           66 :         value.insert(std::move(pair));
     690              :     }
     691           66 : }
     692              : 
     693              : template <serialize_mode mode, size_t ordinal, typename T>
     694           24 : void deserialize_variant(auto &stream, T &variant, wire_type type)
     695              : {
     696           24 :     deserialize<mode>(stream, variant.template emplace<ordinal>(), type);
     697           24 : }
     698              : 
     699              : template <serialize_mode>
     700         2921 : void deserialize(auto &stream, spb::detail::proto_message auto &value, wire_type type)
     701              : {
     702         2921 :     check_wire_type_or_throw(type, wire_type::length_delimited);
     703              : 
     704         5611 :     while (!stream.empty())
     705              :     {
     706         3444 :         const auto tag = read_tag_or_eof(stream);
     707         3442 :         if (tag == tag_type::invalid)
     708          388 :             return;
     709              : 
     710         3054 :         const auto field_type = wire_type_from_tag(tag);
     711         3054 :         if (field_type == wire_type::length_delimited)
     712              :         {
     713         1345 :             const auto size = read_varint<uint32_t>(stream);
     714         1345 :             auto substream  = stream.sub_stream(size);
     715         1303 :             deserialize_value(substream, value, tag);
     716         1190 :             check_if_empty_or_throw(substream);
     717              :         }
     718              :         else
     719              :         {
     720         1709 :             deserialize_value(stream, value, tag);
     721              :         }
     722              :     }
     723              : }
     724         2768 : template <serialize_mode mode> void deserialize(auto &stream, spb::detail::proto_message auto &value)
     725              : {
     726         2766 :     return deserialize<mode>(stream, value, wire_type::length_delimited);
     727              : }
     728              : 
     729          117 : void skip(auto &stream, wire_type type)
     730              : {
     731          117 :     switch (type)
     732              :     {
     733           42 :     case wire_type::varint:
     734           42 :         return (void)read_varint<uint64_t>(stream);
     735           35 :     case wire_type::length_delimited:
     736           35 :         return stream.skip_or_throw(stream.size());
     737           20 :     case wire_type::fixed32:
     738           20 :         return stream.skip_or_throw(sizeof(uint32_t));
     739           19 :     case wire_type::fixed64:
     740           19 :         return stream.skip_or_throw(sizeof(uint64_t));
     741            1 :     default:
     742            1 :         throw std::runtime_error("invalid wire type");
     743              :     }
     744              : }
     745              : } // namespace spb::pb::detail
        

Generated by: LCOV version 2.0-1