Debian Codegraph @

libbinlogevents/src/codecs/binary.cpp

raw
1 /* Copyright (c) 2019, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "libbinlogevents/include/codecs/binary.h"
25 #include <string>
26 #include "my_byteorder.h"
27 #include "mysql_com.h" // net_store_length, net_length_size
28
29 namespace binary_log::codecs::binary {
30
31 /**
32 This is the decoder member function for the TLV encoded set of
33 values for the Transaction payload event. Note that the log event
34 must have the appropriate type code: TRANSACTION_PAYLOAD_EVENT to
35 be compatible with this specific decoder. If it is not, an error
36 shall be returned
37
38 @param from the buffer to decode from.
39 @param size the buffer size.
40 @param to The log event to decode to.
41
42 @return a pair containing the number of bytes decoded and whether
43 there was an error or not. False if no error, true
44 otherwise.
45 */
46 std::pair<std::size_t, bool> Transaction_payload::decode(
47 const unsigned char *from, size_t size, Binary_log_event &to) const {
48 BAPI_TRACE;
49 Event_reader reader(reinterpret_cast<const char *>(from), size);
50 bool have_payload_size = false;
51 bool have_compression_type = false;
52
53 // Compute a return status for the function
54 auto get_return_status = [&](bool error) {
55 return std::make_pair(reader.position(), error);
56 };
57
58 // Wrong use of this function.
59 if (to.header()->type_code != binary_log::TRANSACTION_PAYLOAD_EVENT) {
60 BAPI_ASSERT(false);
61 return get_return_status(true);
62 }
63
64 Transaction_payload_event &ev = down_cast<Transaction_payload_event &>(to);
65
66 while (reader.available_to_read()) {
67 uint64_t type{0};
68 uint64_t length{0};
69 uint64_t value{0};
70
71 // Decode the type of the field.
72 type = reader.net_field_length_ll();
73 if (reader.get_error()) return get_return_status(true);
74
75 // We have reached the end of the payload data header.
76 if (type == OTW_PAYLOAD_HEADER_END_MARK)
77 // For success, the function should have read both payload size
78 // and compression type, and payload size should not be bigger
79 // than the remainder of the event. It's OK to not have
80 // uncompressed size, since that is informational only.
81 return get_return_status(!have_payload_size || !have_compression_type ||
82 ev.get_payload_size() >
83 reader.available_to_read());
84
85 // Decode the size of the field.
86 length = reader.net_field_length_ll();
87 if (reader.get_error()) return get_return_status(true);
88
89 switch (type) {
90 case OTW_PAYLOAD_SIZE_FIELD:
91 // Decode the payload size.
92 value = reader.net_field_length_ll();
93 if (reader.get_error()) return get_return_status(true);
94 ev.set_payload_size(value);
95 have_payload_size = true;
96 break;
97
98 case OTW_PAYLOAD_COMPRESSION_TYPE_FIELD:
99 // Decode the compression type.
100 value = reader.net_field_length_ll();
101 if (reader.get_error()) return get_return_status(true);
102 if (!transaction::compression::type_is_valid(value))
103 return get_return_status(true);
104 ev.set_compression_type(
105 static_cast<transaction::compression::type>(value));
106 have_compression_type = true;
107 break;
108
109 case OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD:
110 // Decode the uncompressed size.
111 value = reader.net_field_length_ll();
112 if (reader.get_error()) return get_return_status(true);
113 ev.set_uncompressed_size(value);
114 break;
115
116 default:
117 // Ignore unrecognized fields.
118 reader.forward(length);
119 break;
120 }
121 }
122 // Reached end of event without seeing an END_MARK
123 return get_return_status(true);
124 }
125
126 /**
127 This is the decoder member function for decoding heartbeats. Note
128 that the log event must have the appropriate type code: HEARTBEAT_LOG_EVENT
129 to be compatible with this specific decoder. If it is not, an error shall be
130 returned
131
132 @param from the buffer to decode from.
133 @param size the buffer size.
134 @param to The log event to decode to.
135
136 @return a pair containing the number of bytes decoded and whether
137 there was an error or not. False if no error, true
138 otherwise.
139 */
140
141 std::pair<std::size_t, bool> Heartbeat::decode(const unsigned char *from,
142 size_t size,
143 Binary_log_event &to) const {
144 bool result = (to.header()->type_code != binary_log::HEARTBEAT_LOG_EVENT_V2);
145 Event_reader reader(reinterpret_cast<const char *>(from), size);
146 BAPI_ASSERT(!result);
147 if (result)
148 return std::make_pair<std::size_t, bool &>(reader.position(), result);
149
150 Heartbeat_event_v2 &ev = down_cast<Heartbeat_event_v2 &>(to);
151
152 while (reader.available_to_read()) {
153 uint64_t type{0};
154 uint64_t length{0};
155 uint64_t value{0};
156
157 /* read the type of the field. */
158 value = reader.net_field_length_ll();
159 if ((result = reader.get_error())) return {reader.position(), true};
160 type = value;
161
162 // we have reached the end of the header
163 if (type == OTW_HB_HEADER_END_MARK) break;
164
165 /* read the size of the field. */
166 value = reader.net_field_length_ll();
167 if ((result = reader.get_error())) return {reader.position(), true};
168 length = value;
169
170 switch (type) {
171 case OTW_HB_LOG_FILENAME_FIELD: {
172 // read the string
173 std::string name{reader.ptr(), static_cast<size_t>(length)};
174 // advance the cursor
175 reader.ptr(length);
176 if ((result = reader.get_error())) return {reader.position(), true};
177 ev.set_log_filename(name);
178 } break;
179
180 case OTW_HB_LOG_POSITION_FIELD:
181 value = reader.net_field_length_ll();
182 if ((result = reader.get_error())) return {reader.position(), true};
183 ev.set_log_position(value);
184 break;
185
186 /* purecov: begin inspected */
187 default:
188 /* ignore unrecognized field. */
189 reader.go_to(length);
190 break;
191 /* purecov: end */
192 }
193 }
194
195 return std::make_pair<std::size_t, bool &>(reader.position(), result);
196 }
197
198 /**
199 This is the encoder member function for the heartbeat event. Note
200 that the log event must have the appropriate type code: HEARTBEAT_LOG_EVENT
201 to be compatible with this specific enccoder. If it is not, an error
202 shall be returned
203
204 @param from The log event to decode to.
205 @param to the buffer to decode from.
206 @param size the buffer size.
207
208
209 @return a pair containing the number of bytes decoded and whether
210 there was an error or not. False if no error, true
211 otherwise.
212 */
213 std::pair<std::size_t, bool> Heartbeat::encode(const Binary_log_event &from,
214 unsigned char *to,
215 size_t size) const {
216 bool result = from.header()->type_code != binary_log::HEARTBEAT_LOG_EVENT_V2;
217 uchar *ptr = to;
218 BAPI_ASSERT(!result);
219 if (result) return std::make_pair<std::size_t, bool &>(ptr - to, result);
220
221 uint64_t type{0};
222 uint64_t length{0};
223 const Heartbeat_event_v2 &ev = down_cast<const Heartbeat_event_v2 &>(from);
224 const auto filename = ev.get_log_filename();
225 const auto position = ev.get_log_position();
226
227 // encode the string in the TLV section
228 type = static_cast<uint64_t>(OTW_HB_LOG_FILENAME_FIELD);
229 length = filename.size();
230 if (ptr + net_length_size(type) + net_length_size(length) + length >=
231 to + size)
232 return {ptr - to, true};
233 ptr = net_store_length(ptr, type);
234 ptr = net_store_length(ptr, length);
235 memcpy(ptr, filename.c_str(), length);
236 ptr = ptr + length;
237
238 // encode the log position in network encoding
239 type = static_cast<uint64_t>(OTW_HB_LOG_POSITION_FIELD);
240 length = net_length_size(position);
241 if (ptr + net_length_size(type) + net_length_size(length) + length >=
242 to + size)
243 return {ptr - to, true};
244
245 ptr = net_store_length(ptr, type);
246 ptr = net_store_length(ptr, length);
247 ptr = net_store_length(ptr, position);
248
249 // new fields go here -----------------------
250
251 // write the end of event header marker
252 type = static_cast<uint64_t>(OTW_HB_HEADER_END_MARK);
253 if (ptr + net_length_size(type) >= to + size)
254 return std::make_pair<std::size_t, bool &>(ptr - to, (result = true));
255 ptr = net_store_length(ptr, type);
256
257 return std::make_pair<std::size_t, bool &>(ptr - to, result);
258 }
259
260 /**
261 This is the encoder member function for the TLV encoded set of
262 values for the Transaction payload event. Note that the log event
263 must have the appropriate type code: TRANSACTION_PAYLOAD_EVENT to
264 be compatible with this specific enccoder. If it is not, an error
265 shall be returned
266
267 @param from The log event to decode to.
268 @param to the buffer to decode from.
269 @param size the buffer size.
270
271 @return a pair containing the number of bytes decoded and whether
272 there was an error or not. False if no error, true
273 otherwise.
274 */
275 std::pair<std::size_t, bool> Transaction_payload::encode(
276 const Binary_log_event &from, unsigned char *to,
277 size_t size [[maybe_unused]]) const {
278 bool result =
279 from.header()->type_code != binary_log::TRANSACTION_PAYLOAD_EVENT;
280 uchar *ptr = to;
281 BAPI_ASSERT(!result);
282 if (result == false) {
283 uint64_t type{0};
284 uint64_t length{0};
285 uint64_t value{0};
286 const Transaction_payload_event &ev =
287 down_cast<const Transaction_payload_event &>(from);
288
289 // encode the compression type
290 type = static_cast<uint64_t>(OTW_PAYLOAD_COMPRESSION_TYPE_FIELD);
291 value = static_cast<uint64_t>(ev.get_compression_type());
292 length = net_length_size(value);
293 ptr = net_store_length(ptr, type);
294 ptr = net_store_length(ptr, length);
295 ptr = net_store_length(ptr, value);
296
297 // encode uncompressed size
298 if (ev.get_compression_type() != transaction::compression::type::NONE) {
299 type = static_cast<uint64_t>(OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD);
300 value = ev.get_uncompressed_size();
301 length = net_length_size(value);
302 ptr = net_store_length(ptr, type);
303 ptr = net_store_length(ptr, length);
304 ptr = net_store_length(ptr, value);
305 }
306
307 // encode payload size
308 type = static_cast<uint64_t>(OTW_PAYLOAD_SIZE_FIELD);
309 value = ev.get_payload_size();
310 length = net_length_size(value);
311 ptr = net_store_length(ptr, type);
312 ptr = net_store_length(ptr, length);
313 ptr = net_store_length(ptr, value);
314
315 // write the end of event header marker
316 type = static_cast<uint64_t>(OTW_PAYLOAD_HEADER_END_MARK);
317 ptr = net_store_length(ptr, type);
318 }
319
320 return std::make_pair<std::size_t, bool &>(ptr - to, result);
321 }
322
323 } // namespace binary_log::codecs::binary