Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flushing support for gzip compressor #116

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/boost/iostreams/filter/gzip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class basic_gzip_compressor : basic_zlib_compressor<Alloc> {
struct category
: dual_use,
filter_tag,
flushable_tag,
multichar_tag,
closable_tag
{ };
Expand Down Expand Up @@ -271,6 +272,12 @@ class basic_gzip_compressor : basic_zlib_compressor<Alloc> {
}
close_impl();
}

template<typename Sink>
bool flush(Sink& snk) {
base_type::force_flush(snk);
return true;
}
private:
static gzip_params normalize_params(gzip_params p);
void prepare_footer();
Expand Down
20 changes: 20 additions & 0 deletions include/boost/iostreams/filter/symmetric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ class symmetric_filter {
close_impl();
}
}

template<typename Sink>
void force_flush(Sink &snk) {
if (!(state() & f_write))
begin_write();

buffer_type& buf = pimpl_->buf_;
char_type dummy;
const char_type* end = &dummy;
// Repeatedly invokes force_flush(), till the time zlib::deflate returns zlib::Z_BUF_ERROR.
// zlib::Z_BUF_ERROR ensures that no more futher progress is possible.
bool again = true;
while(again) {
if(buf.ptr() != buf.eptr()) {
again = filter().force_flush(end, end, buf.ptr(), buf.eptr());
}
flush(snk);
}
}

SymmetricFilter& filter() { return *pimpl_; }
string_type unconsumed_input() const;

Expand Down
14 changes: 14 additions & 0 deletions include/boost/iostreams/filter/zlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ BOOST_IOSTREAMS_DECL extern const int buf_error;
BOOST_IOSTREAMS_DECL extern const int finish;
BOOST_IOSTREAMS_DECL extern const int no_flush;
BOOST_IOSTREAMS_DECL extern const int sync_flush;
BOOST_IOSTREAMS_DECL extern const int full_flush;

// Code for current OS

Expand Down Expand Up @@ -232,6 +233,8 @@ class zlib_compressor_impl : public zlib_base, public zlib_allocator<Alloc> {
bool filter( const char*& src_begin, const char* src_end,
char*& dest_begin, char* dest_end, bool flush );
void close();
bool force_flush( const char*& src_begin, const char* src_end,
char*& dest_begin, char* dest_end);
};

//
Expand Down Expand Up @@ -361,6 +364,17 @@ bool zlib_compressor_impl<Alloc>::filter
template<typename Alloc>
void zlib_compressor_impl<Alloc>::close() { reset(true, true); }

template<typename Alloc>
bool zlib_compressor_impl<Alloc>::force_flush
( const char*& src_begin, const char* src_end,
char*& dest_begin, char* dest_end)
{
before(src_begin, src_end, dest_begin, dest_end);
int result = xdeflate(zlib::full_flush);
after(src_begin, dest_begin, true);
return result == zlib::okay;
}

//------------------Implementation of zlib_decompressor_impl------------------//

template<typename Alloc>
Expand Down
1 change: 1 addition & 0 deletions src/zlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const int buf_error = Z_BUF_ERROR;
const int finish = Z_FINISH;
const int no_flush = Z_NO_FLUSH;
const int sync_flush = Z_SYNC_FLUSH;
const int full_flush = Z_FULL_FLUSH;

// Code for current OS

Expand Down
30 changes: 30 additions & 0 deletions test/gzip_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ void array_source_test()
BOOST_CHECK_EQUAL(data, res);
}


void flush_test()
{
std::string encoded;
std::vector<int> result;

static const int flush_marker_bytes[] = {'\x00', '\x00', '\xff', '\xff', '\x00', '\x00', '\xff', '\xff'};
std::vector<int> target(flush_marker_bytes, flush_marker_bytes + sizeof(flush_marker_bytes) / sizeof(flush_marker_bytes[0]) );

filtering_ostream out;
out.push(gzip_compressor());
out.push(io::back_inserter(encoded));
write_data_in_chunks(out);
out.flush();

for(int i = 0; i < 4; i++) {
result.push_back(encoded[encoded.length() - 4 + i]);
}

write_data_in_chunks(out);
out.flush();

for(int i = 0; i < 4; i++) {
result.push_back(encoded[encoded.length() - 4 + i]);
}

BOOST_CHECK(std::equal(result.begin(), result.end(), target.begin()));
}

#if defined(BOOST_MSVC)
# pragma warning(push)
# pragma warning(disable:4309) // Truncation of constant value
Expand Down Expand Up @@ -239,6 +268,7 @@ test_suite* init_unit_test_suite(int, char* [])
test->add(BOOST_TEST_CASE(&compression_test));
test->add(BOOST_TEST_CASE(&multiple_member_test));
test->add(BOOST_TEST_CASE(&array_source_test));
test->add(BOOST_TEST_CASE(&flush_test));
test->add(BOOST_TEST_CASE(&header_test));
test->add(BOOST_TEST_CASE(&empty_file_test));
test->add(BOOST_TEST_CASE(&multipart_test));
Expand Down