From 968689157cb3315b9a1e9a514aee343f6a2fb771 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 15 Mar 2020 01:56:48 +0300 Subject: [PATCH] Make max_compression_ratio required parameter in gzencode. GitOrigin-RevId: c787fdeae202d3b80944412e7db4209f35adcd07 --- td/telegram/net/NetQueryCreator.cpp | 2 +- tdutils/td/utils/Gzip.cpp | 4 ++-- tdutils/td/utils/Gzip.h | 2 +- tdutils/test/gzip.cpp | 3 ++- test/http.cpp | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/td/telegram/net/NetQueryCreator.cpp b/td/telegram/net/NetQueryCreator.cpp index ba48f9db4..9e2f0564c 100644 --- a/td/telegram/net/NetQueryCreator.cpp +++ b/td/telegram/net/NetQueryCreator.cpp @@ -27,7 +27,7 @@ NetQueryCreator::Ptr NetQueryCreator::create(uint64 id, const Storer &storer, Dc int32 tl_constructor = NetQuery::tl_magic(slice); if (gzip_flag == NetQuery::GzipFlag::On) { // TODO: try to compress files? - BufferSlice compressed = gzencode(slice.as_slice()); + BufferSlice compressed = gzencode(slice.as_slice(), 0.9); if (compressed.empty()) { gzip_flag = NetQuery::GzipFlag::Off; } else { diff --git a/tdutils/td/utils/Gzip.cpp b/tdutils/td/utils/Gzip.cpp index a5e6d492f..6235a1ed2 100644 --- a/tdutils/td/utils/Gzip.cpp +++ b/tdutils/td/utils/Gzip.cpp @@ -184,12 +184,12 @@ BufferSlice gzdecode(Slice s) { return message.extract_reader().move_as_buffer_slice(); } -BufferSlice gzencode(Slice s, double k) { +BufferSlice gzencode(Slice s, double max_compression_ratio) { Gzip gzip; gzip.init_encode().ensure(); gzip.set_input(s); gzip.close_input(); - size_t max_size = static_cast(static_cast(s.size()) * k); + size_t max_size = static_cast(static_cast(s.size()) * max_compression_ratio); BufferWriter message{max_size}; gzip.set_output(message.prepare_append()); auto r_state = gzip.run(); diff --git a/tdutils/td/utils/Gzip.h b/tdutils/td/utils/Gzip.h index 17cf6434a..e5e21f147 100644 --- a/tdutils/td/utils/Gzip.h +++ b/tdutils/td/utils/Gzip.h @@ -99,7 +99,7 @@ class Gzip { BufferSlice gzdecode(Slice s); -BufferSlice gzencode(Slice s, double k = 0.9); +BufferSlice gzencode(Slice s, double max_compression_ratio); } // namespace td diff --git a/tdutils/test/gzip.cpp b/tdutils/test/gzip.cpp index 10724bc7c..f1c4be182 100644 --- a/tdutils/test/gzip.cpp +++ b/tdutils/test/gzip.cpp @@ -63,7 +63,8 @@ TEST(Gzip, flow) { } TEST(Gzip, flow_error) { auto str = td::rand_string('a', 'z', 1000000); - auto zip = td::gzencode(str).as_slice().str(); + auto zip = td::gzencode(str, 0.9).as_slice().str(); + ASSERT_TRUE(!zip.empty()); zip.resize(zip.size() - 1); auto parts = td::rand_split(zip); diff --git a/test/http.cpp b/test/http.cpp index e9acf4228..834a75cbf 100644 --- a/test/http.cpp +++ b/test/http.cpp @@ -354,7 +354,7 @@ TEST(Http, chunked_flow_error) { TEST(Http, gzip_chunked_flow) { auto str = rand_string('a', 'z', 1000000); - auto parts = rand_split(make_chunked(gzencode(str).as_slice().str())); + auto parts = rand_split(make_chunked(gzencode(str, 2.0).as_slice().str())); ChainBufferWriter input_writer; auto input = input_writer.extract_reader();