Less string copies

This commit is contained in:
LibretroAdmin 2025-01-17 13:10:17 +01:00
parent 1e83bfb971
commit b47e09534c
2 changed files with 32 additions and 49 deletions

View File

@ -59,7 +59,7 @@ struct sevenzip_context_t
uint32_t parse_index; uint32_t parse_index;
uint32_t decompress_index; uint32_t decompress_index;
uint32_t packIndex; uint32_t packIndex;
uint32_t block_index; uint32_t block_index;
}; };
static void *sevenzip_stream_alloc_impl(ISzAllocPtr p, size_t len) static void *sevenzip_stream_alloc_impl(ISzAllocPtr p, size_t len)
@ -71,15 +71,12 @@ static void *sevenzip_stream_alloc_impl(ISzAllocPtr p, size_t len)
static void sevenzip_stream_free_impl(ISzAllocPtr p, void *address) static void sevenzip_stream_free_impl(ISzAllocPtr p, void *address)
{ {
(void)p;
if (address) if (address)
free(address); free(address);
} }
static void *sevenzip_stream_alloc_tmp_impl(ISzAllocPtr p, size_t len) static void *sevenzip_stream_alloc_tmp_impl(ISzAllocPtr p, size_t len)
{ {
(void)p;
if (len == 0) if (len == 0)
return 0; return 0;
return malloc(len); return malloc(len);
@ -140,11 +137,11 @@ static int64_t sevenzip_file_read(
const char *needle, void **buf, const char *needle, void **buf,
const char *optional_outfile) const char *optional_outfile)
{ {
CSzArEx db;
CFileInStream archiveStream; CFileInStream archiveStream;
CLookToRead2 lookStream; CLookToRead2 lookStream;
ISzAlloc allocImp; ISzAlloc allocImp;
ISzAlloc allocTempImp; ISzAlloc allocTempImp;
CSzArEx db;
uint8_t *output = 0; uint8_t *output = 0;
int64_t outsize = -1; int64_t outsize = -1;
@ -164,18 +161,16 @@ static int64_t sevenzip_file_read(
#if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32) #if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32)
if (!string_is_empty(path)) if (!string_is_empty(path))
{ {
wchar_t *pathW = utf8_to_utf16_string_alloc(path); wchar_t *path_w = utf8_to_utf16_string_alloc(path);
if (path_w)
if (pathW)
{ {
/* Could not open 7zip archive? */ /* Could not open 7zip archive? */
if (InFile_OpenW(&archiveStream.file, pathW)) if (InFile_OpenW(&archiveStream.file, path_w))
{ {
free(pathW); free(path_w);
return -1; return -1;
} }
free(path_w);
free(pathW);
} }
} }
#else #else
@ -276,7 +271,7 @@ static int64_t sevenzip_file_read(
* copy and free the old one. */ * copy and free the old one. */
*buf = malloc((size_t)(outsize + 1)); *buf = malloc((size_t)(outsize + 1));
((char*)(*buf))[outsize] = '\0'; ((char*)(*buf))[outsize] = '\0';
memcpy(*buf,output + offset,outsize); memcpy(*buf, output + offset, outsize);
} }
break; break;
} }
@ -369,18 +364,18 @@ static int sevenzip_parse_file_init(file_archive_transfer_t *state,
#if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32) #if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32)
if (!string_is_empty(file)) if (!string_is_empty(file))
{ {
wchar_t *fileW = utf8_to_utf16_string_alloc(file); wchar_t *file_w = utf8_to_utf16_string_alloc(file);
if (fileW) if (file_w)
{ {
/* could not open 7zip archive? */ /* could not open 7zip archive? */
if (InFile_OpenW(&sevenzip_context->archiveStream.file, fileW)) if (InFile_OpenW(&sevenzip_context->archiveStream.file, file_w))
{ {
free(fileW); free(file_w);
goto error; goto error;
} }
free(fileW); free(file_w);
} }
} }
#else #else
@ -439,21 +434,18 @@ static int sevenzip_parse_file_iterate_step_internal(
if ( (_len < PATH_MAX_LENGTH) if ( (_len < PATH_MAX_LENGTH)
&& !SzArEx_IsDir(&sevenzip_context->db, sevenzip_context->parse_index)) && !SzArEx_IsDir(&sevenzip_context->db, sevenzip_context->parse_index))
{ {
char infile[PATH_MAX_LENGTH];
SRes res = SZ_ERROR_FAIL; SRes res = SZ_ERROR_FAIL;
uint16_t *temp = (uint16_t*)malloc(_len * sizeof(uint16_t)); uint16_t *temp = (uint16_t*)malloc(_len * sizeof(uint16_t));
if (!temp) if (!temp)
return -1; return -1;
infile[0] = '\0';
SzArEx_GetFileNameUtf16(&sevenzip_context->db, sevenzip_context->parse_index, SzArEx_GetFileNameUtf16(&sevenzip_context->db, sevenzip_context->parse_index,
temp); temp);
if (temp) if (temp)
{ {
res = utf16_to_char_string(temp, infile, sizeof(infile)) res = utf16_to_char_string(temp, s, PATH_MAX_LENGTH)
? SZ_OK : SZ_ERROR_FAIL; ? SZ_OK : SZ_ERROR_FAIL;
free(temp); free(temp);
} }
@ -461,8 +453,6 @@ static int sevenzip_parse_file_iterate_step_internal(
if (res != SZ_OK) if (res != SZ_OK)
return -1; return -1;
strlcpy(s, infile, PATH_MAX_LENGTH);
*cmode = 0; /* unused for 7zip */ *cmode = 0; /* unused for 7zip */
*checksum = sevenzip_context->db.CRCs.Vals[sevenzip_context->parse_index]; *checksum = sevenzip_context->db.CRCs.Vals[sevenzip_context->parse_index];
*size = (uint32_t)SzArEx_GetFileSize(&sevenzip_context->db, sevenzip_context->parse_index); *size = (uint32_t)SzArEx_GetFileSize(&sevenzip_context->db, sevenzip_context->parse_index);

View File

@ -61,15 +61,13 @@ typedef struct
uint8_t *decompressed_data; uint8_t *decompressed_data;
} zip_context_t; } zip_context_t;
static INLINE uint32_t read_le(const uint8_t *data, unsigned size) static INLINE uint32_t read_le(const uint8_t *data, size_t len)
{ {
unsigned i; unsigned i;
uint32_t val = 0; uint32_t val = 0;
len *= 8;
size *= 8; for (i = 0; i < len; i += 8)
for (i = 0; i < size; i += 8)
val |= (uint32_t)*data++ << i; val |= (uint32_t)*data++ << i;
return val; return val;
} }
@ -114,9 +112,7 @@ static bool zlib_stream_decompress_data_to_file_init(
/* seek past most of the local directory header */ /* seek past most of the local directory header */
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
if (state->archive_mmap_data) if (state->archive_mmap_data)
{
local_header = state->archive_mmap_data + (size_t)cdata + 26; local_header = state->archive_mmap_data + (size_t)cdata + 26;
}
else else
#endif #endif
{ {
@ -181,13 +177,11 @@ static int zlib_stream_decompress_data_to_file_iterate(
if (zip_context->cmode == ZIP_MODE_STORED) if (zip_context->cmode == ZIP_MODE_STORED)
{ {
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
/* Simply copy the data to the output buffer */
if (zip_context->state->archive_mmap_data) if (zip_context->state->archive_mmap_data)
{
/* Simply copy the data to the output buffer */
memcpy(zip_context->decompressed_data, memcpy(zip_context->decompressed_data,
zip_context->state->archive_mmap_data + (size_t)zip_context->fdoffset, zip_context->state->archive_mmap_data + (size_t)zip_context->fdoffset,
zip_context->usize); zip_context->usize);
}
else else
#endif #endif
{ {
@ -204,27 +198,26 @@ static int zlib_stream_decompress_data_to_file_iterate(
} }
else if (zip_context->cmode == ZIP_MODE_DEFLATED) else if (zip_context->cmode == ZIP_MODE_DEFLATED)
{ {
int to_read = MIN(zip_context->csize - zip_context->boffset, _READ_CHUNK_SIZE);
uint8_t *dptr; uint8_t *dptr;
int to_read = MIN(zip_context->csize - zip_context->boffset, _READ_CHUNK_SIZE);
/* File was uncompressed or decompression finished before */
if (!zip_context->zstream) if (!zip_context->zstream)
{
/* file was uncompressed or decompression finished before */
return 1; return 1;
}
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
if (state->archive_mmap_data) if (state->archive_mmap_data)
{ {
/* Decompress from the mapped file */ /* Decompress from the mapped file */
dptr = state->archive_mmap_data + (size_t)zip_context->fdoffset + zip_context->boffset; dptr = state->archive_mmap_data + (size_t)zip_context->fdoffset + zip_context->boffset;
rd = to_read; rd = to_read;
} }
else else
#endif #endif
{ {
/* Read some compressed data from file to the temp buffer */ /* Read some compressed data from file to the temp buffer */
filestream_seek(state->archive_file, zip_context->fdoffset + zip_context->boffset, filestream_seek(state->archive_file,
RETRO_VFS_SEEK_POSITION_START); zip_context->fdoffset + zip_context->boffset,
RETRO_VFS_SEEK_POSITION_START);
rd = filestream_read(state->archive_file, zip_context->tmpbuf, to_read); rd = filestream_read(state->archive_file, zip_context->tmpbuf, to_read);
if (rd < 0) if (rd < 0)
return -1; return -1;
@ -365,7 +358,6 @@ static int64_t zip_file_read(
file_archive_transfer_t state = {0}; file_archive_transfer_t state = {0};
decomp_state_t decomp = {0}; decomp_state_t decomp = {0};
struct archive_extract_userdata userdata = {0}; struct archive_extract_userdata userdata = {0};
bool returnerr = true;
int ret = 0; int ret = 0;
if (needle) if (needle)
@ -380,6 +372,7 @@ static int64_t zip_file_read(
do do
{ {
bool returnerr = true;
ret = file_archive_parse_file_iterate(&state, &returnerr, path, ret = file_archive_parse_file_iterate(&state, &returnerr, path,
"", zip_file_decompressed, &userdata); "", zip_file_decompressed, &userdata);
if (!returnerr) if (!returnerr)
@ -522,13 +515,13 @@ static int zip_parse_file_iterate_step(void *context,
file_archive_file_cb file_cb) file_archive_file_cb file_cb)
{ {
zip_context_t *zip_context = (zip_context_t *)context; zip_context_t *zip_context = (zip_context_t *)context;
const uint8_t *cdata = NULL; const uint8_t *cdata = NULL;
uint32_t checksum = 0; uint32_t checksum = 0;
uint32_t size = 0; uint32_t size = 0;
uint32_t csize = 0; uint32_t csize = 0;
unsigned cmode = 0; unsigned cmode = 0;
unsigned payload = 0; unsigned payload = 0;
int ret = zip_parse_file_iterate_step_internal(zip_context, int ret = zip_parse_file_iterate_step_internal(zip_context,
userdata->current_file_path, &cdata, &cmode, &size, &csize, &checksum, &payload); userdata->current_file_path, &cdata, &cmode, &size, &csize, &checksum, &payload);
if (ret != 1) if (ret != 1)