mirror of https://github.com/AxioDL/metaforce.git
34 lines
1.1 KiB
C++
34 lines
1.1 KiB
C++
#include "CZipInputStream.hpp"
|
|
|
|
namespace metaforce {
|
|
CZipInputStream::CZipInputStream(std::unique_ptr<CInputStream>&& strm)
|
|
: CInputStream(4096), x24_compBuf(new u8[4096]), x28_strm(std::move(strm)) {
|
|
x30_zstrm = std::make_unique<z_stream>();
|
|
x30_zstrm->next_in = x24_compBuf.get();
|
|
x30_zstrm->avail_in = 0;
|
|
x30_zstrm->zalloc = [](void*, u32 c, u32 n) -> void* { return new u8[size_t{c} * size_t{n}]; };
|
|
x30_zstrm->zfree = [](void*, void* buf) { delete[] static_cast<u8*>(buf); };
|
|
inflateInit(x30_zstrm.get());
|
|
}
|
|
|
|
CZipInputStream::~CZipInputStream() { inflateEnd(x30_zstrm.get()); }
|
|
|
|
u32 CZipInputStream::Read(void* buf, u32 len) {
|
|
x30_zstrm->next_out = static_cast<Bytef*>(buf);
|
|
x30_zstrm->avail_out = len;
|
|
x30_zstrm->total_out = 0;
|
|
while (x30_zstrm->avail_out != 0) {
|
|
if (x30_zstrm->avail_in == 0) {
|
|
u32 readSz = x28_strm->ReadBytes(x24_compBuf.get(), 4096);
|
|
x30_zstrm->avail_in = readSz;
|
|
x30_zstrm->next_in = x24_compBuf.get();
|
|
}
|
|
int inflateRet = inflate(x30_zstrm.get(), Z_NO_FLUSH);
|
|
if (inflateRet != Z_OK) {
|
|
break;
|
|
}
|
|
}
|
|
return x30_zstrm->total_out;
|
|
}
|
|
|
|
} // namespace metaforce
|