MakeELF: Replaced wx streams with zlib functions

This commit is contained in:
mpm11011 2016-03-18 23:36:08 -04:00
parent 23f03a19e8
commit e7fc5228d5
1 changed files with 30 additions and 20 deletions

View File

@ -5,10 +5,12 @@
#include "Emu/FS/vfsLocalFile.h"
#include "unself.h"
#pragma warning(push)
#pragma message("TODO: remove wx dependencies: <wx/mstream.h> <wx/zstream.h>")
#pragma message("TODO: remove wx dependencies: See comment below.")
#pragma warning(disable : 4996)
#include <wx/mstream.h>
#include <wx/zstream.h>
// TODO: Still reliant on wxWidgets for zlib functions. Alternative solutions?
#include <zlib.h>
#pragma warning(pop)
force_inline u8 Read8(vfsStream& f)
@ -1150,29 +1152,37 @@ bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32)
// Decompress if necessary.
if (meta_shdr[i].compressed == 2)
{
// Allocate a buffer for decompression.
u8 *decomp_buf = (u8 *)malloc(phdr64_arr[meta_shdr[i].program_idx].p_filesz);
/// Removed all wxWidget dependent code. Replaced with zlib functions.
/// Also changed local mallocs to unique_ptrs.
// Set up memory streams for input/output.
wxMemoryInputStream decomp_stream_in(data_buf + data_buf_offset, meta_shdr[i].data_size);
wxMemoryOutputStream decomp_stream_out;
// Store the length in writeable memory space.
std::unique_ptr<uLongf> decomp_buf_length(new uLongf);
memcpy(decomp_buf_length.get(), &phdr64_arr[meta_shdr[i].program_idx].p_filesz, sizeof(uLongf));
// Create a Zlib stream, read the data and flush the stream.
wxZlibInputStream* z_stream = new wxZlibInputStream(decomp_stream_in);
z_stream->Read(decomp_stream_out);
delete z_stream;
/// Create a pointer to a buffer for decompression.
std::unique_ptr<u8[]> decomp_buf(new u8[phdr64_arr[meta_shdr[i].program_idx].p_filesz]);
// Copy the decompressed result from the stream.
decomp_stream_out.CopyTo(decomp_buf, phdr64_arr[meta_shdr[i].program_idx].p_filesz);
// Create a buffer separate from data_buf to uncompress.
std::unique_ptr<u8[]> zlib_buf(new u8[data_buf_length]);
memcpy(zlib_buf.get(), data_buf, data_buf_length);
// Use zlib uncompress on the new buffer.
// decomp_buf_length changes inside the call to uncompress, so it must be a pointer to correct type (in writeable mem space).
int rv = uncompress(decomp_buf.get(), decomp_buf_length.get(), zlib_buf.get() + data_buf_offset, data_buf_length);
// Check for errors (TODO: Probably safe to remove this once these changes have passed testing.)
switch (rv)
{
case Z_MEM_ERROR: LOG_ERROR(LOADER, "MakeELF encountered a Z_MEM_ERROR!"); break;
case Z_BUF_ERROR: LOG_ERROR(LOADER, "MakeELF encountered a Z_BUF_ERROR!"); break;
case Z_DATA_ERROR: LOG_ERROR(LOADER, "MakeELF encountered a Z_DATA_ERROR!"); break;
default: break;
}
// Seek to the program header data offset and write the data.
CHECK_ASSERTION(e.seek(phdr64_arr[meta_shdr[i].program_idx].p_offset) != -1);
e.write(decomp_buf.get(), phdr64_arr[meta_shdr[i].program_idx].p_filesz);
e.write(decomp_buf, phdr64_arr[meta_shdr[i].program_idx].p_filesz);
// Release the decompression buffer.
free(decomp_buf);
}
else
{
@ -1316,7 +1326,7 @@ bool CheckDebugSelf(const std::string& self, const std::string& elf)
// Copy the data.
char buf[2048];
while (ssize_t size = s.read(buf, 2048))
while (size_t size = s.read(buf, 2048)) // Did size need to be of type ssize_t?
{
e.write(buf, size);
}