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

Fix processing of multi-stream files #87

Merged
merged 3 commits into from May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 18 additions & 16 deletions include/boost/iostreams/filter/bzip2.hpp
Expand Up @@ -365,22 +365,24 @@ bool bzip2_decompressor_impl<Alloc>::filter
( const char*& src_begin, const char* src_end,
char*& dest_begin, char* dest_end, bool flush )
{
if (eof_) {
// reset the stream if there are more characters
if(src_begin == src_end)
return false;
else
close();
}
if (!ready())
init();
before(src_begin, src_end, dest_begin, dest_end);
int result = decompress();
if(result == bzip2::ok && flush)
result = check_end(src_begin, dest_begin);
after(src_begin, dest_begin);
bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
eof_ = result == bzip2::stream_end;
do {
if (eof_) {
// reset the stream if there are more characters
if(src_begin == src_end)
return false;
else
close();
}
if (!ready())
init();
before(src_begin, src_end, dest_begin, dest_end);
int result = decompress();
if(result == bzip2::ok && flush)
result = check_end(src_begin, dest_begin);
after(src_begin, dest_begin);
bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
eof_ = result == bzip2::stream_end;
} while (eof_ && src_begin != src_end && dest_begin != dest_end);
return true;
}

Expand Down
28 changes: 15 additions & 13 deletions test/bzip2_test.cpp
Expand Up @@ -54,37 +54,39 @@ void bzip2_test()

void multiple_member_test()
{
const int num_sequences = 10;
text_sequence data;
std::vector<char> temp, dest;

// Write compressed data to temp, twice in succession
// Write compressed data to temp, several times in succession
filtering_ostream out;
out.push(bzip2_compressor());
out.push(io::back_inserter(temp));
io::copy(make_iterator_range(data), out);
out.push(io::back_inserter(temp));
io::copy(make_iterator_range(data), out);
for(int i = 0; i < num_sequences; ++i)
{
out.push(io::back_inserter(temp));
io::copy(make_iterator_range(data), out);
}

// Read compressed data from temp into dest
filtering_istream in;
in.push(bzip2_decompressor());
in.push(array_source(&temp[0], temp.size()));
io::copy(in, io::back_inserter(dest));

// Check that dest consists of two copies of data
BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
// Check that dest consists of as many copies of data as were provided
BOOST_REQUIRE_EQUAL(data.size() * num_sequences, dest.size());
for(int i = 0; i < num_sequences; ++i)
BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + i * dest.size() / num_sequences));

dest.clear();
io::copy(
array_source(&temp[0], temp.size()),
io::compose(bzip2_decompressor(), io::back_inserter(dest)));

// Check that dest consists of two copies of data
BOOST_REQUIRE_EQUAL(data.size() * 2, dest.size());
BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin()));
BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2));
// Check that dest consists of as many copies of data as were provided
BOOST_REQUIRE_EQUAL(data.size() * num_sequences, dest.size());
for(int i = 0; i < num_sequences; ++i)
BOOST_CHECK(std::equal(data.begin(), data.end(), dest.begin() + i * dest.size() / num_sequences));
}

test_suite* init_unit_test_suite(int, char* [])
Expand Down