RMG-Core: use std::vector<char> instead of malloc() and free()

This commit is contained in:
Rosalie Wanders committed 2023-09-05 23:13:39 +02:00
1 parent b0acaaa681
commit bfb7953c91
2 files changed
+40 -122

No files matched your search

+31 -98
View File
@@ -33,6 +33,7 @@
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iterator>
//
// Local Defines
@@ -134,7 +135,7 @@ static int zlib_filefunc_testerror(voidpf opaque, voidpf stream)
return errno;
}
static bool read_zip_file(std::filesystem::path file, std::filesystem::path* extractedFileName, bool* isDisk, char** buf, int* size)
static bool read_zip_file(std::filesystem::path file, std::filesystem::path& extractedFileName, bool& isDisk, std::vector<char>& outBuffer)
{
std::string error;
std::fstream fileStream;
@@ -202,33 +203,13 @@ static bool read_zip_file(std::filesystem::path file, std::filesystem::path* ext
fileExtension == ".ndd" ||
fileExtension == ".d64")
{
char* buffer;
char* outBuffer;
int dataSize = UNZIP_READ_SIZE;
int total_bytes_read = 0;
int bytes_read = 0;
std::vector<char> buffer(UNZIP_READ_SIZE);
int bytes_read = 0;
buffer = (char*)malloc(UNZIP_READ_SIZE);
if (buffer == nullptr)
{
error = "read_zip_file Failed: malloc Failed!";
CoreSetError(error);
return false;
}
outBuffer = (char*)malloc(UNZIP_READ_SIZE);
if (outBuffer == nullptr)
{
free(buffer);
error = "read_zip_file Failed: malloc Failed!";
CoreSetError(error);
return false;
}
outBuffer.reserve(UNZIP_READ_SIZE);
if (unzOpenCurrentFile(zipFile) != UNZ_OK)
{
free(buffer);
free(outBuffer);
error = "read_zip_file Failed: unzOpenCurrentFile Failed!";
CoreSetError(error);
return false;
@@ -236,49 +217,26 @@ static bool read_zip_file(std::filesystem::path file, std::filesystem::path* ext
do
{
bytes_read = unzReadCurrentFile(zipFile, buffer, UNZIP_READ_SIZE);
bytes_read = unzReadCurrentFile(zipFile, buffer.data(), UNZIP_READ_SIZE);
if (bytes_read < 0)
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
free(buffer);
free(outBuffer);
error = "read_zip_file Failed: unzReadCurrentFile Failed: ";
error += std::to_string(bytes_read);
CoreSetError(error);
return false;
}
if (bytes_read > 0)
else if (bytes_read > 0)
{
if (total_bytes_read + bytes_read > dataSize)
{
outBuffer = (char*)realloc(outBuffer, total_bytes_read + bytes_read);
dataSize += bytes_read;
if (outBuffer == nullptr)
{
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
free(buffer);
free(outBuffer);
error = "read_zip_file Failed: realloc Failed!";
CoreSetError(error);
return false;
}
}
memcpy((outBuffer + total_bytes_read), buffer, bytes_read);
total_bytes_read += bytes_read;
outBuffer.insert(outBuffer.end(), buffer.begin(), std::next(buffer.begin(), bytes_read));
}
} while (bytes_read > 0);
*size = total_bytes_read;
*buf = outBuffer;
*extractedFileName = fileNamePath;
*isDisk = (fileExtension == ".ndd" || fileExtension == ".d64");
extractedFileName = fileNamePath;
isDisk = (fileExtension == ".ndd" || fileExtension == ".d64");
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
free(buffer);
return true;
}
@@ -304,7 +262,7 @@ static bool read_zip_file(std::filesystem::path file, std::filesystem::path* ext
return false;
}
static bool read_7zip_file(std::filesystem::path file, std::filesystem::path* extractedFileName, bool* isDisk, char** buf, int* size)
static bool read_7zip_file(std::filesystem::path file, std::filesystem::path& extractedFileName, bool& isDisk, std::vector<char>& outBuffer)
{
std::string error;
@@ -405,13 +363,13 @@ static bool read_7zip_file(std::filesystem::path file, std::filesystem::path* ex
fileExtension == ".d64")
{
uint32_t blockIndex = 0xFFFFFFFF;
uint8_t* outBuffer = nullptr;
size_t outBufferSize = 0;
uint8_t* readBuffer = nullptr;
size_t readBufferSize = 0;
size_t offset = 0;
size_t outSizeProcessed = 0;
res = SzArEx_Extract(&db, &lookStream.vt, i,
&blockIndex, &outBuffer, &outBufferSize,
&blockIndex, &readBuffer, &readBufferSize,
&offset, &outSizeProcessed,
&allocImp, &allocTempImp);
if (res != SZ_OK)
@@ -423,22 +381,18 @@ static bool read_7zip_file(std::filesystem::path file, std::filesystem::path* ex
SzArEx_Free(&db, &allocImp);
ISzAlloc_Free(&allocImp, lookStream.buf);
File_Close(&archiveStream.file);
ISzAlloc_Free(&allocImp, outBuffer);
ISzAlloc_Free(&allocImp, readBuffer);
return false;
}
*size = outSizeProcessed;
// we have to memcpy it into buf, otherwise
// the free() in CoreOpenRom() crashes
*buf = (char*)malloc(outSizeProcessed);
memcpy(*buf, (outBuffer + offset), outSizeProcessed);
*extractedFileName = fileNamePath;
*isDisk = (fileExtension == ".ndd" || fileExtension == ".d64");
outBuffer.insert(outBuffer.begin(), readBuffer, readBuffer + outSizeProcessed);
extractedFileName = fileNamePath;
isDisk = (fileExtension == ".ndd" || fileExtension == ".d64");
SzArEx_Free(&db, &allocImp);
ISzAlloc_Free(&allocImp, lookStream.buf);
File_Close(&archiveStream.file);
ISzAlloc_Free(&allocImp, outBuffer);
ISzAlloc_Free(&allocImp, readBuffer);
return true;
}
}
@@ -453,12 +407,11 @@ static bool read_7zip_file(std::filesystem::path file, std::filesystem::path* ex
return false;
}
static bool read_raw_file(std::filesystem::path file, char** buf, int* size)
static bool read_raw_file(std::filesystem::path file, std::vector<char>& outBuffer)
{
std::string error;
std::ifstream fileStream;
int fileStreamLen;
char* fileStreamBuf;
// attempt to open file
fileStream.open(file, std::ios::binary);
@@ -479,29 +432,17 @@ static bool read_raw_file(std::filesystem::path file, char** buf, int* size)
fileStreamLen = fileStream.tellg();
fileStream.seekg(0, fileStream.beg);
// try to create buffer
fileStreamBuf = (char*)malloc(fileStreamLen);
if (fileStreamBuf == nullptr)
{
error = "read_raw_file Failed: ";
error += "malloc failed!";
CoreSetError(error);
free(fileStreamBuf);
fileStream.close();
return false;
}
// resize buffer
outBuffer.resize(fileStreamLen);
// read file
fileStream.read(fileStreamBuf, fileStreamLen);
*buf = fileStreamBuf;
*size = fileStreamLen;
fileStream.read(outBuffer.data(), fileStreamLen);
fileStream.close();
return true;
}
static bool write_file(std::filesystem::path file, char* buf, int size)
static bool write_file(std::filesystem::path file, std::vector<char>& buffer)
{
std::string error;
std::ofstream fileStream;
@@ -521,7 +462,7 @@ static bool write_file(std::filesystem::path file, char* buf, int size)
}
// write buffer to file
fileStream.write(buf, size);
fileStream.write(buffer.data(), buffer.size());
fileStream.close();
return true;
}
@@ -534,8 +475,7 @@ bool CoreOpenRom(std::filesystem::path file)
{
std::string error;
m64p_error ret;
char* buf = nullptr;
int buf_size = 0;
std::vector<char> buf;
std::string file_extension;
if (!m64p::Core.IsHooked())
@@ -562,14 +502,14 @@ bool CoreOpenRom(std::filesystem::path file)
if (file_extension == ".zip")
{
if (!read_zip_file(file, &extracted_file, &is_disk, &buf, &buf_size))
if (!read_zip_file(file, extracted_file, is_disk, buf))
{
return false;
}
}
else
{
if (!read_7zip_file(file, &extracted_file, &is_disk, &buf, &buf_size))
if (!read_7zip_file(file, extracted_file, is_disk, buf))
{
return false;
}
@@ -598,14 +538,12 @@ bool CoreOpenRom(std::filesystem::path file)
error += disk_file.parent_path().string();
error += "\"!";
CoreSetError(error);
free(buf);
return false;
}
// attempt to write temporary file
if (!write_file(disk_file, buf, buf_size))
if (!write_file(disk_file, buf))
{
free(buf);
return false;
}
@@ -626,7 +564,7 @@ bool CoreOpenRom(std::filesystem::path file)
}
else
{
if (!read_raw_file(file, &buf, &buf_size))
if (!read_raw_file(file, buf))
{
return false;
}
@@ -642,15 +580,10 @@ bool CoreOpenRom(std::filesystem::path file)
}
else
{
ret = m64p::Core.DoCommand(M64CMD_ROM_OPEN, buf_size, buf);
ret = m64p::Core.DoCommand(M64CMD_ROM_OPEN, buf.size(), buf.data());
error = "CoreOpenRom: m64p::Core.DoCommand(M64CMD_ROM_OPEN) Failed: ";
}
if (!l_HasDisk || l_HasExtractedDisk)
{
free(buf);
}
if (ret != M64ERR_SUCCESS)
{
error += m64p::Core.ErrorMessage(ret);
+9 -24
View File
@@ -13,6 +13,7 @@
#include <exception>
#include <filesystem>
#include <fstream>
#include <vector>
#include <unzip.h>
//
@@ -29,23 +30,15 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
{
std::string error;
unzFile zipFile;
unz_global_info zipInfo;
std::ofstream outputStream;
char* read_buffer = (char*)malloc(UNZIP_READ_SIZE);
unzFile zipFile;
unz_global_info zipInfo;
std::ofstream outputStream;
std::vector<char> readBuffer(UNZIP_READ_SIZE);
int bytes_read = 0;
if (read_buffer == nullptr)
{
error = "CoreUnzip: malloc Failed!";
CoreSetError(error);
return false;
}
zipFile = unzOpen(file.string().c_str());
if (zipFile == nullptr)
{
free(read_buffer);
error = "CoreUnzip: unzOpen Failed!";
CoreSetError(error);
return false;
@@ -53,7 +46,6 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
if (unzGetGlobalInfo(zipFile, &zipInfo) != UNZ_OK)
{
free(read_buffer);
error = "CoreUnzip: unzGetGlobalInfo Failed!";
CoreSetError(error);
return false;
@@ -69,7 +61,6 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
// ensure we can retrieve the current file info
if (unzGetCurrentFileInfo(zipFile, &fileInfo, fileName, PATH_MAX, nullptr, 0, nullptr, 0) != UNZ_OK)
{
free(read_buffer);
unzClose(zipFile);
error = "CoreUnzip: unzGetCurrentFileInfo Failed!";
CoreSetError(error);
@@ -92,7 +83,6 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
}
catch (...)
{
free(read_buffer);
unzClose(zipFile);
error = "CoreUnzip: std::filesystem::create_directory(";
error += targetPath.string();
@@ -105,7 +95,6 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
{ // file
if (unzOpenCurrentFile(zipFile) != UNZ_OK)
{
free(read_buffer);
unzClose(zipFile);
error = "CoreUnzip: unzOpenCurrentFile Failed!";
CoreSetError(error);
@@ -115,7 +104,6 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
outputStream.open(targetPath, std::ios::trunc | std::ios::binary);
if (!outputStream.is_open())
{
free(read_buffer);
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
error = "CoreUnzip: failed to open file!";
@@ -124,21 +112,20 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
return false;
}
do
do
{
bytes_read = unzReadCurrentFile(zipFile, read_buffer, UNZIP_READ_SIZE);
bytes_read = unzReadCurrentFile(zipFile, readBuffer.data(), UNZIP_READ_SIZE);
if (bytes_read < 0)
{
free(read_buffer);
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
error = "CoreUnzip: unzReadCurrentFile Failed!";
CoreSetError(error);
return false;
}
else
else if (bytes_read > 0)
{ // write data to file
outputStream.write(read_buffer, bytes_read);
outputStream.write(readBuffer.data(), bytes_read);
}
} while (bytes_read > 0);
@@ -155,7 +142,6 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
// move to next file
if (unzGoToNextFile(zipFile) != UNZ_OK)
{
free(read_buffer);
unzClose(zipFile);
error = "CoreUnzip: unzGoToNextFile Failed!";
CoreSetError(error);
@@ -164,6 +150,5 @@ bool CoreUnzip(std::filesystem::path file, std::filesystem::path path)
}
unzClose(zipFile);
free(read_buffer);
return true;
}