mirror of
https://github.com/libretro/RetroArch
synced 2025-03-29 22:20:21 +00:00
Cut down on some code duplication
This commit is contained in:
parent
b73832ce2e
commit
158a520edb
128
file_ops.c
128
file_ops.c
@ -364,6 +364,49 @@ struct decomp_state
|
||||
bool found;
|
||||
};
|
||||
|
||||
static bool zip_file_decompressed_handle(file_archive_file_handle_t *handle,
|
||||
const uint8_t *cdata, uint32_t csize,
|
||||
uint32_t size, uint32_t crc32)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
handle->backend = file_archive_get_default_file_backend();
|
||||
|
||||
if (!handle->backend)
|
||||
goto error;
|
||||
|
||||
if (!handle->backend->stream_decompress_data_to_file_init(
|
||||
handle, cdata, csize, size))
|
||||
return false;
|
||||
|
||||
do{
|
||||
ret = handle->backend->stream_decompress_data_to_file_iterate(
|
||||
handle->stream);
|
||||
}while(ret == 0);
|
||||
|
||||
handle->real_checksum = handle->backend->stream_crc_calculate(0,
|
||||
handle->data, size);
|
||||
|
||||
if (handle->real_checksum != crc32)
|
||||
{
|
||||
RARCH_ERR("Inflated checksum did not match CRC32!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (handle->stream)
|
||||
free(handle->stream);
|
||||
|
||||
return true;
|
||||
|
||||
error:
|
||||
if (handle->stream)
|
||||
free(handle->stream);
|
||||
if (handle->data)
|
||||
free(handle->data);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Extract the relative path (needle) from a
|
||||
* ZIP archive (path) and allocate a buffer for it to write it in.
|
||||
*
|
||||
@ -389,87 +432,50 @@ static int zip_file_decompressed(const char *name, const char *valid_exts,
|
||||
if (st->opt_file != 0)
|
||||
{
|
||||
/* Called in case core has need_fullpath enabled. */
|
||||
int ret = 0;
|
||||
file_archive_file_handle_t handle = {0};
|
||||
char *buf = NULL;
|
||||
|
||||
|
||||
RARCH_LOG("opt file is: %s\n", st->opt_file);
|
||||
handle.backend = file_archive_get_default_file_backend();
|
||||
if (!handle.backend->stream_decompress_data_to_file_init(
|
||||
&handle, cdata, csize, size))
|
||||
return false;
|
||||
|
||||
do{
|
||||
ret = handle.backend->stream_decompress_data_to_file_iterate(
|
||||
handle.stream);
|
||||
}while(ret == 0);
|
||||
|
||||
handle.real_checksum = handle.backend->stream_crc_calculate(0,
|
||||
handle.data, size);
|
||||
|
||||
if (handle.real_checksum != crc32)
|
||||
if (zip_file_decompressed_handle(&handle,
|
||||
cdata, csize, size, crc32))
|
||||
{
|
||||
RARCH_ERR("Inflated checksum did not match CRC32!\n");
|
||||
goto error;
|
||||
}
|
||||
bool goto_error = false;
|
||||
buf = malloc(size);
|
||||
|
||||
if (handle.stream)
|
||||
free(handle.stream);
|
||||
if (buf)
|
||||
{
|
||||
memcpy(buf, handle.data, size);
|
||||
if (!retro_write_file(st->opt_file, buf, size))
|
||||
goto_error = true;
|
||||
}
|
||||
|
||||
buf = malloc(size);
|
||||
if (!buf)
|
||||
goto error;
|
||||
memcpy(buf, handle.data, size);
|
||||
if (!retro_write_file(st->opt_file, buf, size))
|
||||
{
|
||||
if (handle.data)
|
||||
free(handle.data);
|
||||
free(buf);
|
||||
goto error;
|
||||
|
||||
if (goto_error)
|
||||
goto error;
|
||||
|
||||
st->size = 0;
|
||||
}
|
||||
|
||||
if (handle.data)
|
||||
free(handle.data);
|
||||
free(buf);
|
||||
|
||||
st->size = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Called in case core has need_fullpath disabled.
|
||||
* Will copy decompressed content directly into
|
||||
* RetroArch's ROM buffer. */
|
||||
int ret = 0;
|
||||
file_archive_file_handle_t handle = {0};
|
||||
handle.backend = file_archive_get_default_file_backend();
|
||||
if (!handle.backend->stream_decompress_data_to_file_init(
|
||||
&handle, cdata, csize, size))
|
||||
return false;
|
||||
|
||||
do{
|
||||
ret = handle.backend->stream_decompress_data_to_file_iterate(
|
||||
handle.stream);
|
||||
}while(ret == 0);
|
||||
|
||||
handle.real_checksum = handle.backend->stream_crc_calculate(0,
|
||||
handle.data, size);
|
||||
|
||||
if (handle.real_checksum != crc32)
|
||||
if (zip_file_decompressed_handle(&handle,
|
||||
cdata, csize, size, crc32))
|
||||
{
|
||||
RARCH_ERR("Inflated checksum did not match CRC32!\n");
|
||||
goto error;
|
||||
*st->buf = malloc(size);
|
||||
memcpy(*st->buf, handle.data, size);
|
||||
|
||||
if (handle.data)
|
||||
free(handle.data);
|
||||
|
||||
st->size = size;
|
||||
}
|
||||
|
||||
if (handle.stream)
|
||||
free(handle.stream);
|
||||
*st->buf = malloc(size);
|
||||
memcpy(*st->buf, handle.data, size);
|
||||
|
||||
if (handle.data)
|
||||
free(handle.data);
|
||||
|
||||
st->size = size;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user