2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/db/binlog/BinlogEvent.h"
|
|
|
|
|
2018-07-22 00:03:21 +02:00
|
|
|
#include "td/utils/crypto.h"
|
2021-06-23 02:22:23 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-07-22 00:03:21 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2018-07-22 00:03:21 +02:00
|
|
|
#include "td/utils/tl_storers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2023-02-02 02:03:17 +01:00
|
|
|
void BinlogEvent::init(BufferSlice &&raw_event) {
|
2023-02-02 19:13:52 +01:00
|
|
|
TlParser parser(as_slice(raw_event));
|
2023-02-02 02:03:17 +01:00
|
|
|
size_ = static_cast<uint32>(parser.fetch_int());
|
|
|
|
LOG_CHECK(size_ == raw_event.size()) << size_ << ' ' << raw_event.size() << debug_info_;
|
|
|
|
id_ = static_cast<uint64>(parser.fetch_long());
|
2018-12-31 20:04:05 +01:00
|
|
|
type_ = parser.fetch_int();
|
|
|
|
flags_ = parser.fetch_int();
|
2023-02-02 02:03:17 +01:00
|
|
|
extra_ = static_cast<uint64>(parser.fetch_long());
|
2020-06-11 17:21:18 +02:00
|
|
|
CHECK(size_ >= MIN_SIZE);
|
2023-02-02 01:08:26 +01:00
|
|
|
parser.fetch_string_raw<Slice>(size_ - MIN_SIZE); // skip data
|
2018-12-31 20:04:05 +01:00
|
|
|
crc32_ = static_cast<uint32>(parser.fetch_int());
|
|
|
|
raw_event_ = std::move(raw_event);
|
|
|
|
}
|
|
|
|
|
2023-02-02 01:08:26 +01:00
|
|
|
Slice BinlogEvent::get_data() const {
|
2023-02-02 13:45:20 +01:00
|
|
|
CHECK(raw_event_.size() >= MIN_SIZE);
|
2023-02-02 19:13:52 +01:00
|
|
|
return Slice(as_slice(raw_event_).data() + HEADER_SIZE, raw_event_.size() - MIN_SIZE);
|
2023-02-02 01:08:26 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 12:48:22 +02:00
|
|
|
Status BinlogEvent::validate() const {
|
2023-02-02 02:03:17 +01:00
|
|
|
if (raw_event_.size() < MIN_SIZE) {
|
2018-07-09 12:48:22 +02:00
|
|
|
return Status::Error("Too small event");
|
|
|
|
}
|
2023-02-02 19:13:52 +01:00
|
|
|
TlParser parser(as_slice(raw_event_));
|
2023-02-02 02:03:17 +01:00
|
|
|
uint32 size = static_cast<uint32>(parser.fetch_int());
|
|
|
|
if (size_ != size || size_ != raw_event_.size()) {
|
2018-07-09 12:48:22 +02:00
|
|
|
return Status::Error(PSLICE() << "Size of event changed: " << tag("was", size_) << tag("now", size));
|
|
|
|
}
|
2023-02-02 02:03:17 +01:00
|
|
|
parser.fetch_string_raw<Slice>(size_ - TAIL_SIZE - sizeof(int)); // skip
|
|
|
|
auto stored_crc32 = static_cast<uint32>(parser.fetch_int());
|
2023-02-02 19:13:52 +01:00
|
|
|
auto calculated_crc = crc32(Slice(as_slice(raw_event_).data(), size_ - TAIL_SIZE));
|
2023-02-02 02:03:17 +01:00
|
|
|
if (calculated_crc != crc32_ || calculated_crc != stored_crc32) {
|
|
|
|
return Status::Error(PSLICE() << "CRC mismatch " << tag("actual", format::as_hex(calculated_crc))
|
|
|
|
<< tag("expected", format::as_hex(crc32_)) << public_to_string());
|
|
|
|
}
|
|
|
|
return Status::OK();
|
2018-07-09 12:48:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 00:03:21 +02:00
|
|
|
BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) {
|
2020-06-11 17:21:18 +02:00
|
|
|
auto raw_event = BufferSlice{storer.size() + MIN_SIZE};
|
2018-07-22 00:03:21 +02:00
|
|
|
|
2023-02-02 15:40:47 +01:00
|
|
|
TlStorerUnsafe tl_storer(raw_event.as_mutable_slice().ubegin());
|
2018-07-22 00:03:21 +02:00
|
|
|
tl_storer.store_int(narrow_cast<int32>(raw_event.size()));
|
|
|
|
tl_storer.store_long(id);
|
|
|
|
tl_storer.store_int(type);
|
|
|
|
tl_storer.store_int(flags);
|
|
|
|
tl_storer.store_long(0);
|
|
|
|
|
2020-06-11 17:21:18 +02:00
|
|
|
CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + HEADER_SIZE);
|
2018-07-22 00:03:21 +02:00
|
|
|
tl_storer.store_storer(storer);
|
|
|
|
|
2020-06-11 17:21:18 +02:00
|
|
|
CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - TAIL_SIZE);
|
2021-06-02 14:43:56 +02:00
|
|
|
tl_storer.store_int(crc32(raw_event.as_slice().truncate(raw_event.size() - TAIL_SIZE)));
|
2018-07-22 00:03:21 +02:00
|
|
|
|
|
|
|
return raw_event;
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:30:05 +02:00
|
|
|
void BinlogEvent::realloc() {
|
|
|
|
raw_event_ = raw_event_.copy();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|