2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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/utils/GzipByteFlow.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_gzipbyteflow_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#if TD_HAVE_ZLIB
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-07-22 20:52:00 +02:00
|
|
|
bool GzipByteFlow::loop() {
|
|
|
|
if (gzip_.need_input()) {
|
|
|
|
auto slice = input_->prepare_read();
|
|
|
|
if (slice.empty()) {
|
|
|
|
if (!is_input_active_) {
|
|
|
|
gzip_.close_input();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-07-23 15:39:13 +02:00
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
} else {
|
|
|
|
gzip_.set_input(input_->prepare_read());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
}
|
|
|
|
if (gzip_.need_output()) {
|
|
|
|
auto slice = output_.prepare_append();
|
|
|
|
CHECK(!slice.empty());
|
|
|
|
gzip_.set_output(slice);
|
|
|
|
}
|
|
|
|
auto r_state = gzip_.run();
|
|
|
|
auto output_size = gzip_.flush_output();
|
|
|
|
if (output_size) {
|
2022-10-12 14:59:58 +02:00
|
|
|
uncommitted_size_ += output_size;
|
2020-07-22 20:52:00 +02:00
|
|
|
total_output_size_ += output_size;
|
|
|
|
if (total_output_size_ > max_output_size_) {
|
|
|
|
finish(Status::Error("Max output size limit exceeded"));
|
2020-07-23 15:39:13 +02:00
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
output_.confirm_append(output_size);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-07-22 20:52:00 +02:00
|
|
|
auto input_size = gzip_.flush_input();
|
|
|
|
if (input_size) {
|
|
|
|
input_->confirm_read(input_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
if (r_state.is_error()) {
|
|
|
|
finish(r_state.move_as_error());
|
|
|
|
return false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-07-22 20:52:00 +02:00
|
|
|
auto state = r_state.ok();
|
|
|
|
if (state == Gzip::State::Done) {
|
|
|
|
consume_input();
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-23 15:39:13 +02:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|