15 unsigned long size_to_decompress =
static_cast<unsigned long>(length);
17 std::vector<unsigned char> decompressed_data(size_to_decompress);
18 decompressed_data.reserve(size_to_decompress);
21 memset(&strm, 0,
sizeof(strm));
22 strm.next_in =
const_cast<unsigned char*
>(&(*iter));
23 strm.avail_in = size_to_decompress;
24 strm.next_out = decompressed_data.data();
25 strm.avail_out =
static_cast<unsigned int>(decompressed_data.size());
27 int res = inflateInit2(&strm, 16 + MAX_WBITS);
30 throw std::runtime_error(
"inflateInit failed: " + std::string(strm.msg));
35 if (strm.total_out >= decompressed_data.size())
37 decompressed_data.resize(decompressed_data.size() + length / 2);
39 strm.next_out = decompressed_data.data() + strm.total_out;
40 strm.avail_out =
static_cast<unsigned long>(decompressed_data.size()) - strm.total_out;
42 res = inflate(&strm, Z_SYNC_FLUSH);
43 if (res == Z_STREAM_END)
50 throw std::runtime_error(
"Inflate decompression failed: " + std::string(strm.msg));
54 if (inflateEnd(&strm) != Z_OK)
56 throw std::runtime_error(
"inflateEnd failed: " + std::string(strm.msg));
59 iter += strm.total_in;
60 length -= strm.total_in;
61 decompressed_data.resize(strm.total_out);
63 return decompressed_data;
65 throw std::runtime_error(
"ProtocolCraft compiled without GZIP support");