std::string read() final { std::string output; if (m_buffer) { const size_t buffer_size = 10240; output.resize(buffer_size); m_bzstream.next_out = const_cast<char*>(output.data()); m_bzstream.avail_out = buffer_size; int result = BZ2_bzDecompress(&m_bzstream); if (result != BZ_OK) { m_buffer = nullptr; m_buffer_size = 0; } if (result != BZ_OK && result != BZ_STREAM_END) { std::string message("bzip2 error: decompress failed: "); throw bzip2_error(message, result); } output.resize(static_cast<unsigned long>(m_bzstream.next_out - output.data())); } return output; }
Bzip2BufferDecompressor(const char* buffer, size_t size) : m_buffer(buffer), m_buffer_size(size), m_bzstream() { m_bzstream.next_in = const_cast<char*>(buffer); m_bzstream.avail_in = static_cast_with_assert<unsigned int>(size); int result = BZ2_bzDecompressInit(&m_bzstream, 0, 0); if (result != BZ_OK) { std::string message("bzip2 error: decompression init failed: "); throw bzip2_error(message, result); } }
void bzip2_error::check(int error) { switch (error) { case BZ_OK: case BZ_RUN_OK: case BZ_FLUSH_OK: case BZ_FINISH_OK: case BZ_STREAM_END: return; case BZ_MEM_ERROR: throw std::bad_alloc(); default: throw bzip2_error(error); } }