Reputation: 2949
I'm trying to decompress a gzip'd string inside boost using the following code
std::string DecompressString(const std::string &compressedString)
{
std::stringstream src(compressedString);
if (src.good())
{
boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
in.push(boost::iostreams::zlib_decompressor());
boost::iostreams::copy(in, out);
return dst.str();
}
return "";
}
however, whenever I make a call to this functions (such as below)
string result = DecompressString("H4sIA");
string result = DecompressString("H4sIAAAAAAAAAO2YMQ6DMAxFfZnCXOgK9AA9ACsURuj9N2wpkSIDootxhv+lN2V5sqLIP0T55cEUgdLR48lUgToTjw/5zaRhBuVSKO5yE5c2kDp5zunIaWG6mz3SxLvjeX/hAQ94wAMe8IAHPCwyMS9mdvYYmTfzdfSQ/rQGjx/t92A578l+T057y1Ff6NW51Uy0h+zkLZ33ByuPtB8IuhdcnSMIglgm/r15/rtJctlf4puMt/i/bN16EotQFgAA");
the program will always fail on this line
in.push(boost::iostreams::zlib_decompressor());
and generate the following exception
Unhandled exception at 0x7627b727 in KHMP.exe: Microsoft C++ exception:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::logic_error> > at memory location 0x004dd868..
I really have no idea on this one... anyone have any suggestions?
Thanks
EDIT:
Following a suggestion, I switch the code to
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;//(src);
in.push(boost::iostreams::zlib_decompressor());
in.push(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;//(dst);
out.push(dst);
boost::iostreams::copy(in, out);
however, the exception still happens, except it now happens on copy
Upvotes: 2
Views: 3746
Reputation: 516
After following brado86's advice, change zlib_decompressor()
to gzip_decompressor()
Upvotes: 2
Reputation: 174
Looks like you are pushing your filters in the wrong order for in.
From what I can understand from the Boost.Iostreams documentations, for input, data flows through the filters in the reverse order in which you've pushed the filters in. So if you change the following lines as follows, I think it should work.
Change
boost::iostreams::filtering_streambuf<boost::iostreams::input> in(src);
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
in.push(boost::iostreams::zlib_decompressor());
to
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(src); // Note the order of pushing filters into the instream.
std::stringstream dst;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out(dst);
For more info, read Boost.Iostreams Documentation.
Upvotes: 1