2
This file is part of TON Blockchain Library.
4
TON Blockchain Library is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as published by
6
the Free Software Foundation, either version 2 of the License, or
7
(at your option) any later version.
9
TON Blockchain Library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public License
15
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
17
Copyright 2017-2020 Telegram Systems LLP
19
#include "BinlogReaderHelper.h"
20
#include "BinlogReaderInterface.h"
22
#include "td/utils/misc.h"
25
td::Status BinlogReaderHelper::parse(BinlogReaderInterface& reader, td::Slice data) {
30
if (expected_prefix_size_ > 0 && expected_prefix_size_ == prefix_size_) {
31
TRY_RESULT(size, reader.parse(MutableSlice(buf_.data(), prefix_size_)));
33
if (expected_prefix_size_ > td::narrow_cast<size_t>(-size)) {
34
return td::Status::Error("BinlogReader decreased logevent size estimation (1)");
36
expected_prefix_size_ = static_cast<size_t>(-size);
38
if (expected_prefix_size_ != td::narrow_cast<size_t>(size)) {
39
return td::Status::Error("BinlogReader changed logevent");
42
expected_prefix_size_ = 0;
50
if (expected_prefix_size_ > 0) {
51
CHECK(expected_prefix_size_ < buf_.size());
52
CHECK(prefix_size_ < expected_prefix_size_);
53
auto got = data.copy().truncate(expected_prefix_size_ - prefix_size_);
55
auto dest = td::MutableSlice(buf_.data(), buf_.size()).substr(prefix_size_);
56
if (dest.size() < got.size()) {
57
return td::Status::Error("Too big logevent");
60
prefix_size_ += got.size();
61
data = data.substr(got.size());
67
TRY_RESULT(size, reader.parse(data));
69
expected_prefix_size_ = td::narrow_cast<size_t>(-size);
70
prefix_size_ = data.size();
71
if (expected_prefix_size_ < prefix_size_) {
72
return td::Status::Error("BinlogReader waits for less data than it already has");
74
if (expected_prefix_size_ > buf_.size()) {
75
return td::Status::Error("BinlogReader waits for too big logevent");
78
td::MutableSlice(buf_.data(), prefix_size_).copy_from(data);
82
return td::Status::Error("BinlogReader parseed nothing and asked for nothing");
84
if (td::narrow_cast<size_t>(size) > data.size()) {
85
return td::Status::Error("BinlogReader parseed more than was given");
87
data = data.substr(static_cast<size_t>(size));
89
return td::Status::OK();
92
size_t BinlogReaderHelper::unparsed_size() const {