Skip to content

Commit

Permalink
Less string copies
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jan 17, 2025
1 parent 1e83bfb commit b47e095
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 49 deletions.
38 changes: 14 additions & 24 deletions libretro-common/file/archive_file_7z.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct sevenzip_context_t
uint32_t parse_index;
uint32_t decompress_index;
uint32_t packIndex;
uint32_t block_index;
uint32_t block_index;
};

static void *sevenzip_stream_alloc_impl(ISzAllocPtr p, size_t len)
Expand All @@ -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)
{
(void)p;

if (address)
free(address);
}

static void *sevenzip_stream_alloc_tmp_impl(ISzAllocPtr p, size_t len)
{
(void)p;
if (len == 0)
return 0;
return malloc(len);
Expand Down Expand Up @@ -140,11 +137,11 @@ static int64_t sevenzip_file_read(
const char *needle, void **buf,
const char *optional_outfile)
{
CSzArEx db;
CFileInStream archiveStream;
CLookToRead2 lookStream;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
CSzArEx db;
uint8_t *output = 0;
int64_t outsize = -1;

Expand All @@ -164,18 +161,16 @@ static int64_t sevenzip_file_read(
#if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32)
if (!string_is_empty(path))
{
wchar_t *pathW = utf8_to_utf16_string_alloc(path);

if (pathW)
wchar_t *path_w = utf8_to_utf16_string_alloc(path);
if (path_w)
{
/* 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;
}

free(pathW);
free(path_w);
}
}
#else
Expand Down Expand Up @@ -276,7 +271,7 @@ static int64_t sevenzip_file_read(
* copy and free the old one. */
*buf = malloc((size_t)(outsize + 1));
((char*)(*buf))[outsize] = '\0';
memcpy(*buf,output + offset,outsize);
memcpy(*buf, output + offset, outsize);
}
break;
}
Expand Down Expand Up @@ -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 (!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? */
if (InFile_OpenW(&sevenzip_context->archiveStream.file, fileW))
if (InFile_OpenW(&sevenzip_context->archiveStream.file, file_w))
{
free(fileW);
free(file_w);
goto error;
}

free(fileW);
free(file_w);
}
}
#else
Expand Down Expand Up @@ -439,30 +434,25 @@ static int sevenzip_parse_file_iterate_step_internal(
if ( (_len < PATH_MAX_LENGTH)
&& !SzArEx_IsDir(&sevenzip_context->db, sevenzip_context->parse_index))
{
char infile[PATH_MAX_LENGTH];
SRes res = SZ_ERROR_FAIL;
uint16_t *temp = (uint16_t*)malloc(_len * sizeof(uint16_t));

if (!temp)
return -1;

infile[0] = '\0';

SzArEx_GetFileNameUtf16(&sevenzip_context->db, sevenzip_context->parse_index,
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;
free(temp);
}

if (res != SZ_OK)
return -1;

strlcpy(s, infile, PATH_MAX_LENGTH);

*cmode = 0; /* unused for 7zip */
*checksum = sevenzip_context->db.CRCs.Vals[sevenzip_context->parse_index];
*size = (uint32_t)SzArEx_GetFileSize(&sevenzip_context->db, sevenzip_context->parse_index);
Expand Down
43 changes: 18 additions & 25 deletions libretro-common/file/archive_file_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ typedef struct
uint8_t *decompressed_data;
} 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;
uint32_t val = 0;

size *= 8;
for (i = 0; i < size; i += 8)
len *= 8;
for (i = 0; i < len; i += 8)
val |= (uint32_t)*data++ << i;

return val;
}

Expand Down Expand Up @@ -114,9 +112,7 @@ static bool zlib_stream_decompress_data_to_file_init(
/* seek past most of the local directory header */
#ifdef HAVE_MMAP
if (state->archive_mmap_data)
{
local_header = state->archive_mmap_data + (size_t)cdata + 26;
}
else
#endif
{
Expand Down Expand Up @@ -181,13 +177,11 @@ static int zlib_stream_decompress_data_to_file_iterate(
if (zip_context->cmode == ZIP_MODE_STORED)
{
#ifdef HAVE_MMAP
/* Simply copy the data to the output buffer */
if (zip_context->state->archive_mmap_data)
{
/* Simply copy the data to the output buffer */
memcpy(zip_context->decompressed_data,
zip_context->state->archive_mmap_data + (size_t)zip_context->fdoffset,
zip_context->usize);
}
else
#endif
{
Expand All @@ -204,27 +198,26 @@ static int zlib_stream_decompress_data_to_file_iterate(
}
else if (zip_context->cmode == ZIP_MODE_DEFLATED)
{
int to_read = MIN(zip_context->csize - zip_context->boffset, _READ_CHUNK_SIZE);
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)
{
/* file was uncompressed or decompression finished before */
return 1;
}

#ifdef HAVE_MMAP
if (state->archive_mmap_data)
{
/* Decompress from the mapped file */
dptr = state->archive_mmap_data + (size_t)zip_context->fdoffset + zip_context->boffset;
rd = to_read;
rd = to_read;
}
else
#endif
{
/* Read some compressed data from file to the temp buffer */
filestream_seek(state->archive_file, zip_context->fdoffset + zip_context->boffset,
RETRO_VFS_SEEK_POSITION_START);
filestream_seek(state->archive_file,
zip_context->fdoffset + zip_context->boffset,
RETRO_VFS_SEEK_POSITION_START);
rd = filestream_read(state->archive_file, zip_context->tmpbuf, to_read);
if (rd < 0)
return -1;
Expand Down Expand Up @@ -365,7 +358,6 @@ static int64_t zip_file_read(
file_archive_transfer_t state = {0};
decomp_state_t decomp = {0};
struct archive_extract_userdata userdata = {0};
bool returnerr = true;
int ret = 0;

if (needle)
Expand All @@ -380,6 +372,7 @@ static int64_t zip_file_read(

do
{
bool returnerr = true;
ret = file_archive_parse_file_iterate(&state, &returnerr, path,
"", zip_file_decompressed, &userdata);
if (!returnerr)
Expand Down Expand Up @@ -522,13 +515,13 @@ static int zip_parse_file_iterate_step(void *context,
file_archive_file_cb file_cb)
{
zip_context_t *zip_context = (zip_context_t *)context;
const uint8_t *cdata = NULL;
uint32_t checksum = 0;
uint32_t size = 0;
uint32_t csize = 0;
unsigned cmode = 0;
unsigned payload = 0;
int ret = zip_parse_file_iterate_step_internal(zip_context,
const uint8_t *cdata = NULL;
uint32_t checksum = 0;
uint32_t size = 0;
uint32_t csize = 0;
unsigned cmode = 0;
unsigned payload = 0;
int ret = zip_parse_file_iterate_step_internal(zip_context,
userdata->current_file_path, &cdata, &cmode, &size, &csize, &checksum, &payload);

if (ret != 1)
Expand Down

0 comments on commit b47e095

Please sign in to comment.