Line data Source code
1 : /*
2 : * Protobuf serialization library - Public API for protobuf.
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 "pb/deserialize.hpp"
12 : #include "pb/serialize.hpp"
13 : #include "spb/io/io.hpp"
14 : #include "spb/pb/wire-types.h"
15 : #include <cstdint>
16 : #include <cstdlib>
17 :
18 : namespace spb::pb
19 : {
20 :
21 : struct serialize_options
22 : {
23 : /**
24 : * @brief Writes the size of the message (as a varint) before the message itself.
25 : * Compatible with Google's `writeDelimitedTo` and NanoPb's PB_ENCODE_DELIMITED.
26 : */
27 : bool delimited = false;
28 : };
29 :
30 : struct deserialize_options
31 : {
32 : /**
33 : * @brief Expect the size of the message (encoded as a varint) to come before the message
34 : * itself. Compatible with Google's `parseDelimitedFrom` and NanoPb's PB_DECODE_DELIMITED. Will
35 : * return after having read the specified length; the spb::io::reader object can then be read
36 : * from again to get the next message (if any).
37 : */
38 : bool delimited = false;
39 : };
40 :
41 : /**
42 : * @brief serialize message via writer
43 : *
44 : * @param[in] message to be serialized
45 : * @param[in] on_write function for handling the writes
46 : * @param[in] options
47 : * @return serialized size in bytes
48 : * @throws exceptions only from `on_write`
49 : */
50 776 : size_t serialize(const auto &message, spb::io::writer on_write, const serialize_options &options = {})
51 : {
52 776 : auto stream = detail::ostream_writer{on_write};
53 776 : if (options.delimited)
54 388 : detail::serialize_varint(stream, detail::serialize_size(message));
55 :
56 776 : serialize_value(stream, message);
57 776 : return stream.size;
58 : }
59 :
60 : /**
61 : * @brief return protobuf serialized size in bytes
62 : *
63 : * @param[in] message to be serialized
64 : * @param[in] options
65 : * @return serialized size in bytes
66 : */
67 1552 : [[nodiscard]] size_t serialize_size(const auto &message, const serialize_options &options = {})
68 : {
69 1552 : const auto size = detail::serialize_size(message);
70 1552 : return (options.delimited) ? size + detail::serialize_varint_size(size) : size;
71 : }
72 :
73 : size_t serialize(const auto &message, void *buffer, const serialize_options &options = {})
74 : {
75 : const auto start = (uint8_t *)buffer;
76 : auto stream = detail::ostream_buffer((uint8_t *)buffer);
77 : if (options.delimited)
78 : detail::serialize_varint(stream, detail::serialize_size(message));
79 :
80 : serialize_value(stream, message);
81 : return stream.p_buffer - start;
82 : }
83 :
84 : /**
85 : * @brief serialize message into protobuf
86 : *
87 : * @param[in] message to be serialized
88 : * @param[in] options
89 : * @param[out] result serialized protobuf
90 : * @return serialized size in bytes
91 : * @throws std::runtime_error on error
92 : * @example `auto serialized = std::vector< std::byte >();`
93 : * `spb::pb::serialize( message, serialized );`
94 : */
95 : template <spb::resizable_container Container>
96 1635 : size_t serialize(const auto &message, Container &result, const serialize_options &options = {})
97 : {
98 : static_assert(sizeof(*result.data()) == sizeof(uint8_t));
99 :
100 1635 : const auto size = detail::serialize_size(message);
101 1634 : const auto serialized_size = options.delimited ? size + detail::serialize_varint_size(size) : size;
102 1634 : result.resize(serialized_size);
103 1634 : auto stream = detail::ostream_buffer((uint8_t *)result.data());
104 1634 : if (options.delimited)
105 778 : detail::serialize_varint(stream, size);
106 :
107 1634 : serialize_value(stream, message);
108 3136 : return result.size();
109 : }
110 :
111 : /**
112 : * @brief serialize message into protobuf
113 : *
114 : * @param[in] message to be serialized
115 : * @param[in] options
116 : * @return serialized protobuf
117 : * @throws std::runtime_error on error
118 : * @example `auto serialized_message = spb::pb::serialize< std::vector< std::byte > >( message );`
119 : */
120 : template <spb::resizable_container Container = std::string>
121 1635 : [[nodiscard]] Container serialize(const auto &message, const serialize_options &options = {})
122 : {
123 1635 : auto result = Container();
124 1635 : serialize(message, result, options);
125 1568 : return result;
126 67 : }
127 :
128 1993 : size_t deserialize(auto &message, const void *buffer, size_t size, const deserialize_options &options = {})
129 : {
130 1993 : detail::istream_buffer stream((const uint8_t *)buffer, size);
131 1993 : if (options.delimited)
132 : {
133 778 : const auto substream_length = read_varint<uint32_t>(stream);
134 778 : auto substream = stream.sub_stream(substream_length);
135 777 : deserialize<detail::serialize_mode{}>(substream, message);
136 : }
137 : else
138 : {
139 1215 : deserialize<detail::serialize_mode{}>(stream, message);
140 : }
141 1629 : return size - stream.size();
142 : }
143 :
144 : /**
145 : * @brief deserialize message from protobuf
146 : *
147 : * @param[in] reader function for handling reads
148 : * @param[in] options
149 : * @param[out] message deserialized message
150 : * @throws std::runtime_error on error
151 : */
152 776 : size_t deserialize(auto &message, spb::io::reader reader, const deserialize_options &options = {})
153 : {
154 776 : detail::istream_reader stream{reader};
155 776 : if (options.delimited)
156 : {
157 388 : const auto substream_length = read_varint<uint32_t>(stream);
158 388 : auto substream = stream.sub_stream(substream_length);
159 388 : deserialize<detail::serialize_mode{}>(substream, message);
160 : }
161 : else
162 : {
163 388 : deserialize<detail::serialize_mode{}>(stream, message);
164 : }
165 1552 : return stream.consumed_size();
166 : }
167 :
168 : /**
169 : * @brief deserialize message from protobuf
170 : *
171 : * @param[in] protobuf string with protobuf
172 : * @param[in] options
173 : * @param[out] message deserialized message
174 : * @throws std::runtime_error on error
175 : * @example `auto serialized = std::vector< std::byte >( ... );`
176 : * `auto message = Message();`
177 : * `spb::pb::deserialize( message, serialized );`
178 : */
179 : template <typename Message, spb::size_container Container>
180 776 : size_t deserialize(Message &message, const Container &protobuf, const deserialize_options &options = {})
181 : {
182 776 : return deserialize(message, protobuf.data(), protobuf.size(), options);
183 : }
184 :
185 : /**
186 : * @brief deserialize message from protobuf
187 : *
188 : * @param[in] protobuf serialized protobuf
189 : * @param[in] options
190 : * @return deserialized message
191 : * @throws std::runtime_error on error
192 : * @example `auto serialized = std::vector< std::byte >( ... );`
193 : * `auto message = spb::pb::deserialize< Message >( serialized );`
194 : */
195 : template <typename Message, spb::size_container Container>
196 1217 : [[nodiscard]] Message deserialize(const Container &protobuf, const deserialize_options &options = {})
197 : {
198 1179 : auto message = Message{};
199 1217 : deserialize(message, protobuf.data(), protobuf.size(), options);
200 860 : return message;
201 128 : }
202 :
203 : /**
204 : * @brief deserialize message from reader
205 : *
206 : * @param[in] reader function for handling reads
207 : * @param[in] options
208 : * @return deserialized message
209 : * @throws std::runtime_error on error
210 : */
211 : template <typename Message>
212 : [[nodiscard]] Message deserialize(spb::io::reader reader, const deserialize_options &options = {})
213 : {
214 : auto message = Message{};
215 : deserialize(message, reader, options);
216 : return message;
217 : }
218 : } // namespace spb::pb
|