Cleanup code duplication in zip_file_decompressed

This commit is contained in:
twinaphex 2016-01-26 07:06:38 +01:00
parent fbc3667f86
commit 173c9fd5ca
1 changed files with 34 additions and 44 deletions

View File

@ -371,6 +371,36 @@ struct decomp_state
* buf will be 0 then. * buf will be 0 then.
*/ */
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->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");
return false;
}
if (handle->stream)
free(handle->stream);
return true;
}
static int zip_file_decompressed(const char *name, const char *valid_exts, static int zip_file_decompressed(const char *name, const char *valid_exts,
const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size, const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata) uint32_t crc32, void *userdata)
@ -389,33 +419,11 @@ static int zip_file_decompressed(const char *name, const char *valid_exts,
if (st->opt_file != 0) if (st->opt_file != 0)
{ {
/* Called in case core has need_fullpath enabled. */ /* Called in case core has need_fullpath enabled. */
int ret = 0;
file_archive_file_handle_t handle = {0}; file_archive_file_handle_t handle = {0};
char *buf = NULL; char *buf = NULL;
zip_file_decompressed_handle(&handle, cdata, csize, size,
RARCH_LOG("opt file is: %s\n", st->opt_file); crc32);
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)
{
RARCH_ERR("Inflated checksum did not match CRC32!\n");
goto error;
}
if (handle.stream)
free(handle.stream);
buf = malloc(size); buf = malloc(size);
if (!buf) if (!buf)
@ -440,29 +448,11 @@ static int zip_file_decompressed(const char *name, const char *valid_exts,
/* Called in case core has need_fullpath disabled. /* Called in case core has need_fullpath disabled.
* Will copy decompressed content directly into * Will copy decompressed content directly into
* RetroArch's ROM buffer. */ * RetroArch's ROM buffer. */
int ret = 0;
file_archive_file_handle_t handle = {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{ zip_file_decompressed_handle(&handle, cdata, csize, size,
ret = handle.backend->stream_decompress_data_to_file_iterate( crc32);
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);
*st->buf = malloc(size); *st->buf = malloc(size);
memcpy(*st->buf, handle.data, size); memcpy(*st->buf, handle.data, size);