| 297 | } |
| 298 | |
| 299 | Bytes Bytes::decompress() const |
| 300 | { |
| 301 | Bytes output; |
| 302 | ByteWriter bw(output); |
| 303 | Bytes outputBuffer(1024 * 1024); |
| 304 | |
| 305 | z_stream stream = {}; |
| 306 | inflateInit(&stream); |
| 307 | stream.avail_in = size(); |
| 308 | stream.next_in = (uint8_t*)cbegin(); |
| 309 | |
| 310 | int ret; |
| 311 | do |
| 312 | { |
| 313 | stream.avail_out = outputBuffer.size(); |
| 314 | stream.next_out = &outputBuffer[0]; |
| 315 | ret = inflate(&stream, Z_NO_FLUSH); |
| 316 | if (ret == Z_BUF_ERROR) |
| 317 | ret = Z_OK; |
| 318 | if ((ret != Z_OK) && (ret != Z_STREAM_END)) |
| 319 | error("failed to decompress data: {}", |
| 320 | stream.msg ? stream.msg : "(unknown error)"); |
| 321 | |
| 322 | bw += outputBuffer.slice(0, outputBuffer.size() - stream.avail_out); |
| 323 | } while (ret != Z_STREAM_END); |
| 324 | inflateEnd(&stream); |
| 325 | |
| 326 | return output; |
| 327 | } |
| 328 | |
| 329 | void Bytes::writeToFile(const std::string& filename) const |
| 330 | { |