mirror of
https://github.com/libretro/RetroArch
synced 2025-04-16 08:43:10 +00:00
Less string copies
This commit is contained in:
parent
1e83bfb971
commit
b47e09534c
@ -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);
|
||||||
|
@ -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
|
||||||
if (zip_context->state->archive_mmap_data)
|
|
||||||
{
|
|
||||||
/* Simply copy the data to the output buffer */
|
/* Simply copy the data to the output buffer */
|
||||||
|
if (zip_context->state->archive_mmap_data)
|
||||||
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,13 +198,11 @@ 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)
|
||||||
@ -223,7 +215,8 @@ static int zlib_stream_decompress_data_to_file_iterate(
|
|||||||
#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,
|
||||||
|
zip_context->fdoffset + zip_context->boffset,
|
||||||
RETRO_VFS_SEEK_POSITION_START);
|
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)
|
||||||
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user