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/Gzip.h"
|
|
|
|
|
|
|
|
char disable_linker_warning_about_empty_file_gzip_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#if TD_HAVE_ZLIB
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
2019-07-23 00:50:12 +02:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Gzip::Impl {
|
|
|
|
public:
|
|
|
|
z_stream stream_;
|
|
|
|
|
|
|
|
// z_stream is not copyable nor movable
|
|
|
|
Impl() = default;
|
2023-05-05 12:51:19 +02:00
|
|
|
Impl(const Impl &) = delete;
|
|
|
|
Impl &operator=(const Impl &) = delete;
|
|
|
|
Impl(Impl &&) = delete;
|
|
|
|
Impl &operator=(Impl &&) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
~Impl() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
Status Gzip::init_encode() {
|
2020-01-19 01:02:56 +01:00
|
|
|
CHECK(mode_ == Mode::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
init_common();
|
2020-01-19 01:02:56 +01:00
|
|
|
mode_ = Mode::Encode;
|
2018-12-31 20:04:05 +01:00
|
|
|
int ret = deflateInit2(&impl_->stream_, 6, Z_DEFLATED, 15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
|
|
|
if (ret != Z_OK) {
|
2018-10-24 21:56:48 +02:00
|
|
|
return Status::Error(PSLICE() << "zlib deflate init failed: " << ret);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Gzip::init_decode() {
|
2020-01-19 01:02:56 +01:00
|
|
|
CHECK(mode_ == Mode::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
init_common();
|
2020-01-19 01:02:56 +01:00
|
|
|
mode_ = Mode::Decode;
|
2018-12-31 20:04:05 +01:00
|
|
|
int ret = inflateInit2(&impl_->stream_, MAX_WBITS + 32);
|
|
|
|
if (ret != Z_OK) {
|
2018-10-24 21:56:48 +02:00
|
|
|
return Status::Error(PSLICE() << "zlib inflate init failed: " << ret);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gzip::set_input(Slice input) {
|
|
|
|
CHECK(input_size_ == 0);
|
|
|
|
CHECK(!close_input_flag_);
|
|
|
|
CHECK(input.size() <= std::numeric_limits<uInt>::max());
|
|
|
|
CHECK(impl_->stream_.avail_in == 0);
|
|
|
|
input_size_ = input.size();
|
|
|
|
impl_->stream_.avail_in = static_cast<uInt>(input.size());
|
|
|
|
impl_->stream_.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(input.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gzip::set_output(MutableSlice output) {
|
|
|
|
CHECK(output_size_ == 0);
|
|
|
|
CHECK(output.size() <= std::numeric_limits<uInt>::max());
|
|
|
|
CHECK(impl_->stream_.avail_out == 0);
|
|
|
|
output_size_ = output.size();
|
|
|
|
impl_->stream_.avail_out = static_cast<uInt>(output.size());
|
|
|
|
impl_->stream_.next_out = reinterpret_cast<Bytef *>(output.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<Gzip::State> Gzip::run() {
|
|
|
|
while (true) {
|
|
|
|
int ret;
|
2020-01-19 01:02:56 +01:00
|
|
|
if (mode_ == Mode::Decode) {
|
2018-12-31 20:04:05 +01:00
|
|
|
ret = inflate(&impl_->stream_, Z_NO_FLUSH);
|
|
|
|
} else {
|
|
|
|
ret = deflate(&impl_->stream_, close_input_flag_ ? Z_FINISH : Z_NO_FLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == Z_OK) {
|
2020-01-19 01:02:56 +01:00
|
|
|
return State::Running;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (ret == Z_STREAM_END) {
|
|
|
|
// TODO(now): fail if input is not empty;
|
|
|
|
clear();
|
2020-01-19 01:02:56 +01:00
|
|
|
return State::Done;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
clear();
|
|
|
|
return Status::Error(PSLICE() << "zlib error " << ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Gzip::left_input() const {
|
|
|
|
return impl_->stream_.avail_in;
|
|
|
|
}
|
|
|
|
size_t Gzip::left_output() const {
|
|
|
|
return impl_->stream_.avail_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gzip::init_common() {
|
|
|
|
std::memset(&impl_->stream_, 0, sizeof(impl_->stream_));
|
|
|
|
impl_->stream_.zalloc = Z_NULL;
|
|
|
|
impl_->stream_.zfree = Z_NULL;
|
|
|
|
impl_->stream_.opaque = Z_NULL;
|
|
|
|
impl_->stream_.avail_in = 0;
|
|
|
|
impl_->stream_.next_in = nullptr;
|
|
|
|
impl_->stream_.avail_out = 0;
|
|
|
|
impl_->stream_.next_out = nullptr;
|
|
|
|
|
|
|
|
input_size_ = 0;
|
|
|
|
output_size_ = 0;
|
|
|
|
|
|
|
|
close_input_flag_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gzip::clear() {
|
2020-01-19 01:02:56 +01:00
|
|
|
if (mode_ == Mode::Decode) {
|
2018-12-31 20:04:05 +01:00
|
|
|
inflateEnd(&impl_->stream_);
|
2020-01-19 01:02:56 +01:00
|
|
|
} else if (mode_ == Mode::Encode) {
|
2018-12-31 20:04:05 +01:00
|
|
|
deflateEnd(&impl_->stream_);
|
|
|
|
}
|
2020-01-19 01:02:56 +01:00
|
|
|
mode_ = Mode::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Gzip::Gzip() : impl_(make_unique<Impl>()) {
|
|
|
|
}
|
|
|
|
|
2021-10-18 13:36:15 +02:00
|
|
|
Gzip::Gzip(Gzip &&other) noexcept : Gzip() {
|
2019-07-06 13:29:15 +02:00
|
|
|
swap(other);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-18 13:36:15 +02:00
|
|
|
Gzip &Gzip::operator=(Gzip &&other) noexcept {
|
2019-07-21 20:07:07 +02:00
|
|
|
CHECK(this != &other);
|
2019-07-06 13:29:15 +02:00
|
|
|
clear();
|
|
|
|
swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gzip::swap(Gzip &other) {
|
2019-07-21 20:07:07 +02:00
|
|
|
using std::swap;
|
|
|
|
swap(impl_, other.impl_);
|
|
|
|
swap(input_size_, other.input_size_);
|
|
|
|
swap(output_size_, other.output_size_);
|
|
|
|
swap(close_input_flag_, other.close_input_flag_);
|
|
|
|
swap(mode_, other.mode_);
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Gzip::~Gzip() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice gzdecode(Slice s) {
|
|
|
|
Gzip gzip;
|
|
|
|
gzip.init_decode().ensure();
|
2018-09-10 17:00:28 +02:00
|
|
|
ChainBufferWriter message;
|
2018-12-31 20:04:05 +01:00
|
|
|
gzip.set_input(s);
|
|
|
|
gzip.close_input();
|
|
|
|
double k = 2;
|
|
|
|
gzip.set_output(message.prepare_append(static_cast<size_t>(static_cast<double>(s.size()) * k)));
|
|
|
|
while (true) {
|
|
|
|
auto r_state = gzip.run();
|
|
|
|
if (r_state.is_error()) {
|
|
|
|
return BufferSlice();
|
|
|
|
}
|
|
|
|
auto state = r_state.ok();
|
2020-01-19 01:02:56 +01:00
|
|
|
if (state == Gzip::State::Done) {
|
2018-12-31 20:04:05 +01:00
|
|
|
message.confirm_append(gzip.flush_output());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gzip.need_input()) {
|
|
|
|
return BufferSlice();
|
|
|
|
}
|
|
|
|
if (gzip.need_output()) {
|
|
|
|
message.confirm_append(gzip.flush_output());
|
|
|
|
k *= 1.5;
|
|
|
|
gzip.set_output(message.prepare_append(static_cast<size_t>(static_cast<double>(gzip.left_input()) * k)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message.extract_reader().move_as_buffer_slice();
|
|
|
|
}
|
|
|
|
|
2020-03-14 23:56:48 +01:00
|
|
|
BufferSlice gzencode(Slice s, double max_compression_ratio) {
|
2018-12-31 20:04:05 +01:00
|
|
|
Gzip gzip;
|
|
|
|
gzip.init_encode().ensure();
|
|
|
|
gzip.set_input(s);
|
|
|
|
gzip.close_input();
|
2021-10-18 18:26:14 +02:00
|
|
|
auto max_size = static_cast<size_t>(static_cast<double>(s.size()) * max_compression_ratio);
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferWriter message{max_size};
|
|
|
|
gzip.set_output(message.prepare_append());
|
|
|
|
auto r_state = gzip.run();
|
|
|
|
if (r_state.is_error()) {
|
|
|
|
return BufferSlice();
|
|
|
|
}
|
|
|
|
auto state = r_state.ok();
|
2020-01-19 01:02:56 +01:00
|
|
|
if (state != Gzip::State::Done) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return BufferSlice();
|
|
|
|
}
|
|
|
|
message.confirm_append(gzip.flush_output());
|
|
|
|
return message.as_buffer_slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
#endif
|