mirror of
https://github.com/cemu-project/Cemu.git
synced 2025-07-15 11:18:29 +12:00
Add support for WUHB file format (#1190)
This commit is contained in:
parent
f28043e0e9
commit
dc480ac00b
15 changed files with 617 additions and 3 deletions
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include <boost/random/uniform_int.hpp>
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
|
||||
#if BOOST_OS_WINDOWS
|
||||
#include <TlHelp32.h>
|
||||
|
@ -437,3 +439,42 @@ std::string GenerateRandomString(const size_t length, const std::string_view cha
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<std::vector<uint8>> zlibDecompress(const std::vector<uint8>& compressed, size_t sizeHint)
|
||||
{
|
||||
int err;
|
||||
std::vector<uint8> decompressed;
|
||||
size_t outWritten = 0;
|
||||
size_t bytesPerIteration = sizeHint;
|
||||
z_stream stream;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = Z_NULL;
|
||||
stream.avail_in = compressed.size();
|
||||
stream.next_in = (Bytef*)compressed.data();
|
||||
err = inflateInit2(&stream, 32); // 32 is a zlib magic value to enable header detection
|
||||
if (err != Z_OK)
|
||||
return {};
|
||||
|
||||
do
|
||||
{
|
||||
decompressed.resize(decompressed.size() + bytesPerIteration);
|
||||
const auto availBefore = decompressed.size() - outWritten;
|
||||
stream.avail_out = availBefore;
|
||||
stream.next_out = decompressed.data() + outWritten;
|
||||
err = inflate(&stream, Z_NO_FLUSH);
|
||||
if (!(err == Z_OK || err == Z_STREAM_END))
|
||||
{
|
||||
inflateEnd(&stream);
|
||||
return {};
|
||||
}
|
||||
outWritten += availBefore - stream.avail_out;
|
||||
bytesPerIteration *= 2;
|
||||
}
|
||||
while (err != Z_STREAM_END);
|
||||
|
||||
inflateEnd(&stream);
|
||||
decompressed.resize(stream.total_out);
|
||||
|
||||
return decompressed;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue