2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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"
|
|
|
|
#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
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 VERBOSITY_NAME(binlog) = VERBOSITY_NAME(DEBUG) + 8;
|
|
|
|
|
|
|
|
Status BinlogEvent::init(BufferSlice &&raw_event, bool check_crc) {
|
|
|
|
TlParser parser(raw_event.as_slice());
|
|
|
|
size_ = parser.fetch_int();
|
2018-06-28 19:00:11 +02:00
|
|
|
CHECK(size_ == raw_event.size()) << size_ << " " << raw_event.size() << debug_info_;
|
2018-12-31 20:04:05 +01:00
|
|
|
id_ = parser.fetch_long();
|
|
|
|
type_ = parser.fetch_int();
|
|
|
|
flags_ = parser.fetch_int();
|
|
|
|
extra_ = parser.fetch_long();
|
|
|
|
CHECK(size_ >= MIN_EVENT_SIZE);
|
|
|
|
auto slice_data = parser.fetch_string_raw<Slice>(size_ - MIN_EVENT_SIZE);
|
|
|
|
data_ = MutableSlice(const_cast<char *>(slice_data.begin()), slice_data.size());
|
|
|
|
crc32_ = static_cast<uint32>(parser.fetch_int());
|
|
|
|
if (check_crc) {
|
|
|
|
CHECK(size_ >= EVENT_TAIL_SIZE);
|
|
|
|
auto calculated_crc = crc32(raw_event.as_slice().truncate(size_ - EVENT_TAIL_SIZE));
|
|
|
|
if (calculated_crc != crc32_) {
|
|
|
|
return Status::Error(PSLICE() << "crc mismatch " << tag("actual", format::as_hex(calculated_crc))
|
2018-07-09 12:48:22 +02:00
|
|
|
<< tag("expected", format::as_hex(crc32_)) << public_to_string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
raw_event_ = std::move(raw_event);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-07-09 12:48:22 +02:00
|
|
|
Status BinlogEvent::validate() const {
|
|
|
|
BinlogEvent event;
|
|
|
|
if (raw_event_.size() < 4) {
|
|
|
|
return Status::Error("Too small event");
|
|
|
|
}
|
|
|
|
uint32 size = TlParser(raw_event_.as_slice().truncate(4)).fetch_int();
|
|
|
|
if (size_ != size) {
|
|
|
|
return Status::Error(PSLICE() << "Size of event changed: " << tag("was", size_) << tag("now", size));
|
|
|
|
}
|
|
|
|
return event.init(raw_event_.clone(), true);
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:03:21 +02:00
|
|
|
BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) {
|
|
|
|
auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE};
|
|
|
|
|
|
|
|
TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin());
|
|
|
|
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);
|
|
|
|
|
|
|
|
CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE);
|
|
|
|
tl_storer.store_storer(storer);
|
|
|
|
|
|
|
|
CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - EVENT_TAIL_SIZE);
|
|
|
|
tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE)));
|
|
|
|
|
|
|
|
return raw_event;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|