Cut down on some code duplication

This commit is contained in:
twinaphex 2016-01-26 07:31:48 +01:00
parent b73832ce2e
commit 158a520edb

View File

@ -364,6 +364,49 @@ struct decomp_state
bool found; 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 /* Extract the relative path (needle) from a
* ZIP archive (path) and allocate a buffer for it to write it in. * 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) 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;
if (zip_file_decompressed_handle(&handle,
RARCH_LOG("opt file is: %s\n", st->opt_file); cdata, csize, size, 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"); bool goto_error = false;
goto error; buf = malloc(size);
}
if (handle.stream) if (buf)
free(handle.stream); {
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) if (handle.data)
free(handle.data); free(handle.data);
free(buf); free(buf);
goto error;
if (goto_error)
goto error;
st->size = 0;
} }
if (handle.data)
free(handle.data);
free(buf);
st->size = 0;
} }
else else
{ {
/* 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{ if (zip_file_decompressed_handle(&handle,
ret = handle.backend->stream_decompress_data_to_file_iterate( cdata, csize, size, 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"); *st->buf = malloc(size);
goto error; 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;
} }
} }