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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#if TD_HAVE_ZLIB
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Gzip {
|
|
|
|
public:
|
|
|
|
Gzip();
|
|
|
|
Gzip(const Gzip &) = delete;
|
|
|
|
Gzip &operator=(const Gzip &) = delete;
|
2021-10-18 13:36:15 +02:00
|
|
|
Gzip(Gzip &&other) noexcept;
|
|
|
|
Gzip &operator=(Gzip &&other) noexcept;
|
2018-12-31 20:04:05 +01:00
|
|
|
~Gzip();
|
|
|
|
|
2020-01-19 01:02:56 +01:00
|
|
|
enum class Mode { Empty, Encode, Decode };
|
2018-12-31 20:04:05 +01:00
|
|
|
Status init(Mode mode) TD_WARN_UNUSED_RESULT {
|
2020-01-19 01:02:56 +01:00
|
|
|
if (mode == Mode::Encode) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return init_encode();
|
2020-01-19 01:02:56 +01:00
|
|
|
} else if (mode == Mode::Decode) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return init_decode();
|
|
|
|
}
|
|
|
|
clear();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status init_encode() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
Status init_decode() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
void set_input(Slice input);
|
|
|
|
|
|
|
|
void set_output(MutableSlice output);
|
|
|
|
|
|
|
|
void close_input() {
|
|
|
|
close_input_flag_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_input() const {
|
|
|
|
return left_input() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_output() const {
|
|
|
|
return left_output() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t left_input() const;
|
|
|
|
|
|
|
|
size_t left_output() const;
|
|
|
|
|
|
|
|
size_t used_input() const {
|
|
|
|
return input_size_ - left_input();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t used_output() const {
|
|
|
|
return output_size_ - left_output();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t flush_input() {
|
|
|
|
auto res = used_input();
|
|
|
|
input_size_ = left_input();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t flush_output() {
|
|
|
|
auto res = used_output();
|
|
|
|
output_size_ = left_output();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-01-19 01:02:56 +01:00
|
|
|
enum class State { Running, Done };
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<State> run() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
|
|
|
unique_ptr<Impl> impl_;
|
|
|
|
|
|
|
|
size_t input_size_ = 0;
|
|
|
|
size_t output_size_ = 0;
|
|
|
|
bool close_input_flag_ = false;
|
2020-01-19 01:02:56 +01:00
|
|
|
Mode mode_ = Mode::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void init_common();
|
|
|
|
void clear();
|
2019-07-06 13:29:15 +02:00
|
|
|
|
|
|
|
void swap(Gzip &other);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
BufferSlice gzdecode(Slice s);
|
|
|
|
|
2020-03-14 23:56:48 +01:00
|
|
|
BufferSlice gzencode(Slice s, double max_compression_ratio);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|