Debian Codegraph @

libbinlogevents/src/binlog_event.cpp

raw
1 /* Copyright (c) 2011, 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 "byteorder.h"
25 #include "statement_events.h"
26
27 #include <stdint.h>
28 #include <algorithm>
29
30 const unsigned char checksum_version_split[3] = {5, 6, 1};
31 const unsigned long checksum_version_product =
32 (checksum_version_split[0] * 256 + checksum_version_split[1]) * 256 +
33 checksum_version_split[2];
34
35 namespace binary_log_debug {
36 bool debug_query_mts_corrupt_db_names = false;
37 bool debug_checksum_test = false;
38 bool debug_simulate_invalid_address = false;
39
40 } // namespace binary_log_debug
41
42 namespace binary_log {
43
44 Log_event_footer::Log_event_footer(Event_reader &reader,
45 Log_event_type event_type,
46 const Format_description_event *fde)
47 : checksum_alg(BINLOG_CHECKSUM_ALG_UNDEF) {
48 BAPI_ENTER("Log_event_footer::Log_event_footer(Event_reader& , ...)");
49 if (event_type == binary_log::FORMAT_DESCRIPTION_EVENT) {
50 /* This is a FDE. We will get the checksum_alg from its buffer directly. */
51 checksum_alg = Log_event_footer::get_checksum_alg(reader);
52 } else {
53 /* This is an ordinary event other than FDE. Using FDE's checksum_alg. */
54 checksum_alg = fde->footer()->checksum_alg;
55
56 if (checksum_alg != binary_log::BINLOG_CHECKSUM_ALG_UNDEF &&
57 checksum_alg != binary_log::BINLOG_CHECKSUM_ALG_OFF)
58 reader.shrink_limit(BINLOG_CHECKSUM_LEN);
59 }
60 BAPI_VOID_RETURN;
61 }
62
63 enum_binlog_checksum_alg Log_event_footer::get_checksum_alg(
64 Event_reader &reader) {
65 enum_binlog_checksum_alg alg =
66 get_checksum_alg(reader.buffer(), reader.length());
67 if (alg != binary_log::BINLOG_CHECKSUM_ALG_UNDEF)
68 reader.shrink_limit(BINLOG_CHECKSUM_LEN);
69 return alg;
70 }
71
72 /**
73 The method returns the checksum algorithm used to checksum the binary log.
74 For MySQL server versions < 5.6, the algorithm is undefined. For the higher
75 versions, the type is decoded from the FORMAT_DESCRIPTION_EVENT.
76
77 @param buf buffer holding serialized FD event
78 @param len netto (possible checksum is stripped off) length of the event buf
79
80 @return the version-safe checksum alg descriptor where zero
81 designates no checksum, 255 - the orginator is
82 checksum-unaware (effectively no checksum) and the actual
83 [1-254] range alg descriptor.
84 */
85 enum_binlog_checksum_alg Log_event_footer::get_checksum_alg(const char *buf,
86 unsigned long len) {
87 BAPI_ENTER("Log_event_footer::get_checksum_alg(const char*, unsigned long)");
88 enum_binlog_checksum_alg ret = BINLOG_CHECKSUM_ALG_UNDEF;
89 char version[ST_SERVER_VER_LEN];
90 unsigned char version_split[3];
91 BAPI_ASSERT(buf[EVENT_TYPE_OFFSET] == FORMAT_DESCRIPTION_EVENT);
92 if (len > LOG_EVENT_MINIMAL_HEADER_LEN + ST_COMMON_HEADER_LEN_OFFSET + 1) {
93 uint8_t common_header_len =
94 buf[LOG_EVENT_MINIMAL_HEADER_LEN + ST_COMMON_HEADER_LEN_OFFSET];
95 if (len >=
96 static_cast<unsigned long>(common_header_len + ST_SERVER_VER_OFFSET +
97 ST_SERVER_VER_LEN)) {
98 memcpy(version, buf + common_header_len + ST_SERVER_VER_OFFSET,
99 ST_SERVER_VER_LEN);
100 version[ST_SERVER_VER_LEN - 1] = 0;
101
102 do_server_version_split(version, version_split);
103 if (version_product(version_split) < checksum_version_product)
104 ret = BINLOG_CHECKSUM_ALG_UNDEF;
105 else {
106 size_t checksum_alg_offset =
107 len - (BINLOG_CHECKSUM_ALG_DESC_LEN + BINLOG_CHECKSUM_LEN);
108 ret =
109 static_cast<enum_binlog_checksum_alg>(*(buf + checksum_alg_offset));
110 }
111 }
112 }
113 BAPI_RETURN(ret);
114 }
115
116 /**
117 Tests the checksum algorithm used for the binary log, and asserts in case
118 if the checksum algorithm is invalid.
119
120 @param event_buf point to the buffer containing serialized event
121 @param event_len length of the event accounting possible
122 checksum alg
123 @param alg checksum algorithm used for the binary log
124
125 @retval true if test fails
126 @retval false as success
127 */
128 bool Log_event_footer::event_checksum_test(unsigned char *event_buf,
129 unsigned long event_len,
130 enum_binlog_checksum_alg alg) {
131 bool res = false;
132 unsigned short flags = 0; // to store in FD's buffer flags orig value
133
134 if (alg != BINLOG_CHECKSUM_ALG_OFF && alg != BINLOG_CHECKSUM_ALG_UNDEF) {
135 uint32_t incoming;
136 uint32_t computed;
137
138 if (event_buf[EVENT_TYPE_OFFSET] == FORMAT_DESCRIPTION_EVENT) {
139 #ifndef NDEBUG
140 unsigned char fd_alg = event_buf[event_len - BINLOG_CHECKSUM_LEN -
141 BINLOG_CHECKSUM_ALG_DESC_LEN];
142 #endif
143 /*
144 FD event is checksummed and therefore verified w/o
145 the binlog-in-use flag.
146 */
147 memcpy(&flags, event_buf + FLAGS_OFFSET, sizeof(flags));
148 flags = le16toh(flags);
149 if (flags & LOG_EVENT_BINLOG_IN_USE_F)
150 event_buf[FLAGS_OFFSET] &= ~LOG_EVENT_BINLOG_IN_USE_F;
151 /*
152 The only algorithm currently is CRC32. Zero indicates
153 the binlog file is checksum-free *except* the FD-event.
154 */
155 #ifndef NDEBUG
156 BAPI_ASSERT(fd_alg == BINLOG_CHECKSUM_ALG_CRC32 || fd_alg == 0);
157 #endif
158 BAPI_ASSERT(alg == BINLOG_CHECKSUM_ALG_CRC32);
159 /*
160 Compile time guard to watch over the max number of alg
161 */
162 static_assert(BINLOG_CHECKSUM_ALG_ENUM_END <= 0x80, "");
163 }
164 memcpy(&incoming, event_buf + event_len - BINLOG_CHECKSUM_LEN,
165 sizeof(incoming));
166 incoming = le32toh(incoming);
167
168 computed = checksum_crc32(0L, nullptr, 0);
169 /* checksum the event content but not the checksum part itself */
170 computed =
171 binary_log::checksum_crc32(computed, (const unsigned char *)event_buf,
172 event_len - BINLOG_CHECKSUM_LEN);
173
174 if (flags != 0) {
175 /* restoring the orig value of flags of FD */
176 BAPI_ASSERT(event_buf[EVENT_TYPE_OFFSET] == FORMAT_DESCRIPTION_EVENT);
177 event_buf[FLAGS_OFFSET] = static_cast<unsigned char>(flags);
178 }
179
180 res = !(computed == incoming);
181 }
182 #ifndef NDEBUG
183 if (binary_log_debug::debug_checksum_test) return true;
184 #endif
185 return res;
186 }
187
188 Log_event_header::Log_event_header(Event_reader &reader)
189 : data_written(0), log_pos(0), m_is_valid(false) {
190 BAPI_ENTER("Log_event_header::Log_event_header(Event_reader &)");
191
192 /*
193 The caller should always pass an Event_reader with a buffer allowing to
194 read at least LOG_EVENT_MINIMAL_HEADER_LEN bytes.
195 */
196 BAPI_ASSERT(reader.can_read(LOG_EVENT_MINIMAL_HEADER_LEN));
197
198 /**
199 @verbatim
200 The first 19 bytes in the header is as follows:
201 +============================================+
202 | member_variable offset : len |
203 +============================================+
204 | when.tv_sec 0 : 4 |
205 +--------------------------------------------+
206 | type_code EVENT_TYPE_OFFSET(4) : 1 |
207 +--------------------------------------------+
208 | server_id SERVER_ID_OFFSET(5) : 4 |
209 +--------------------------------------------+
210 | data_written EVENT_LEN_OFFSET(9) : 4 |
211 +--------------------------------------------+
212 | log_pos LOG_POS_OFFSET(13) : 4 |
213 +--------------------------------------------+
214 | flags FLAGS_OFFSET(17) : 2 |
215 +--------------------------------------------+
216 | extra_headers 19 : x-19|
217 +============================================+
218 @endverbatim
219 */
220 BAPI_ASSERT(reader.position() == 0);
221 when.tv_sec = reader.read<uint32_t>();
222 when.tv_usec = 0;
223
224 BAPI_ASSERT(reader.position() == EVENT_TYPE_OFFSET);
225 type_code = static_cast<Log_event_type>(reader.read<unsigned char>());
226
227 BAPI_ASSERT(reader.position() == SERVER_ID_OFFSET);
228 unmasked_server_id = reader.read<uint32_t>();
229
230 BAPI_ASSERT(reader.position() == EVENT_LEN_OFFSET);
231 data_written = reader.read<uint64_t>(4);
232 reader.set_length(data_written);
233
234 BAPI_ASSERT(reader.position() == LOG_POS_OFFSET);
235 log_pos = reader.read<uint64_t>(4);
236
237 BAPI_ASSERT(reader.position() == FLAGS_OFFSET);
238 flags = reader.read<uint16_t>();
239
240 set_is_valid(!reader.has_error() &&
241 (type_code < ENUM_END_EVENT || flags & LOG_EVENT_IGNORABLE_F));
242
243 BAPI_VOID_RETURN;
244 }
245
246 Binary_log_event::Binary_log_event(const char **buf,
247 const Format_description_event *fde)
248 : m_reader(*buf, LOG_EVENT_MINIMAL_HEADER_LEN), m_header(m_reader) {
249 BAPI_ENTER("Binary_log_event::Binary_log_event(const char **, ...)");
250 /*
251 m_reader cursor is advanced in Log_event_reader constructor to after the
252 event common header.
253 */
254 m_footer = Log_event_footer(m_reader, m_header.type_code, fde);
255 BAPI_VOID_RETURN;
256 }
257
258 /*
259 The destructor is pure virtual to prevent instantiation of the class.
260 */
261 Binary_log_event::~Binary_log_event() = default;
262
263 /**
264 This event type should never occur. It is never written to a binary log.
265 If an event is read from a binary log that cannot be recognized as something
266 else, it is treated as Unknown_event.
267
268 @param buf Contains the serialized event.
269 @param fde An FDE required by Binary_log_event constructor.
270 */
271 Unknown_event::Unknown_event(const char *buf,
272 const Format_description_event *fde)
273 : Binary_log_event(&buf, fde) {
274 BAPI_ENTER("Unknown_event::Unknown_event(const char *, ...)");
275 BAPI_VOID_RETURN;
276 }
277 #ifndef HAVE_MYSYS
278 void Binary_log_event::print_event_info(std::ostream &) {}
279 void Binary_log_event::print_long_info(std::ostream &) {}
280 /**
281 This method is used by the binlog_browser to print short and long
282 information about the event. Since the body of Stop_event is empty
283 the relevant information contains only the timestamp.
284 Please note this is different from the print_event_info methods
285 used by mysqlbinlog.cc.
286
287 @param info std output stream to which the event data is appended.
288 */
289 void Stop_event::print_long_info(std::ostream &info) {
290 info << "Timestamp: " << header()->when.tv_sec;
291 this->print_event_info(info);
292 }
293
294 void Unknown_event::print_event_info(std::ostream &info) {
295 info << "Unhandled event";
296 }
297
298 void Unknown_event::print_long_info(std::ostream &info) {
299 info << "Timestamp: " << header()->when.tv_sec;
300 this->print_event_info(info);
301 }
302
303 #endif
304 } // end namespace binary_log