(task_patch.c) Cleanups

This commit is contained in:
twinaphex 2017-05-14 00:52:56 +02:00
parent 0185f71bab
commit 71499abf1f

View File

@ -506,47 +506,36 @@ static enum patch_error ips_apply_patch(
static bool apply_patch_content(uint8_t **buf,
ssize_t *size, const char *patch_desc, const char *patch_path,
patch_func_t func)
patch_func_t func, void *patch_data, ssize_t patch_size)
{
size_t target_size;
ssize_t patch_size;
void *patch_data = NULL;
enum patch_error err = PATCH_UNKNOWN;
bool success = false;
uint8_t *patched_content = NULL;
ssize_t ret_size = *size;
uint8_t *ret_buf = *buf;
if (!path_is_valid(patch_path))
return false;
if (!filestream_read_file(patch_path, &patch_data, &patch_size))
return false;
if (patch_size < 0)
{
free(patch_data);
return false;
}
if (!path_file_exists(patch_path))
{
free(patch_data);
return false;
}
RARCH_LOG("Found %s file in \"%s\", attempting to patch ...\n",
patch_desc, patch_path);
target_size = ret_size * 4; /* Just to be sure. */
target_size = ret_size * 4; /* Just to be sure. */
patched_content = (uint8_t*)malloc(target_size);
if (!patched_content)
{
RARCH_ERR("%s\n",
msg_hash_to_str(MSG_FAILED_TO_ALLOCATE_MEMORY_FOR_PATCHED_CONTENT));
goto error;
*buf = ret_buf;
*size = ret_size;
return false;
}
err = func((const uint8_t*)patch_data, patch_size, ret_buf,
@ -569,27 +558,32 @@ static bool apply_patch_content(uint8_t **buf,
if (success)
{
free(ret_buf);
*buf = patched_content;
*buf = patched_content;
*size = target_size;
}
free(patch_data);
return true;
error:
*buf = ret_buf;
*size = ret_size;
free(patch_data);
return false;
}
static bool try_bps_patch(bool allow_bps, const char *name_bps,
uint8_t **buf, ssize_t *size)
{
if (allow_bps && !string_is_empty(name_bps))
return apply_patch_content(buf, size, "BPS", name_bps,
bps_apply_patch);
if (path_is_valid(name_bps))
{
ssize_t patch_size;
bool ret = false;
void *patch_data = NULL;
if (!filestream_read_file(name_bps, &patch_data, &patch_size))
return false;
ret = apply_patch_content(
buf, size, "BPS", name_bps,
bps_apply_patch, patch_data, patch_size);
free(patch_data);
return ret;
}
return false;
}
@ -597,8 +591,21 @@ static bool try_ups_patch(bool allow_ups, const char *name_ups,
uint8_t **buf, ssize_t *size)
{
if (allow_ups && !string_is_empty(name_ups))
return apply_patch_content(buf, size, "UPS", name_ups,
ups_apply_patch);
if (path_is_valid(name_ups))
{
ssize_t patch_size;
bool ret = false;
void *patch_data = NULL;
if (!filestream_read_file(name_ups, &patch_data, &patch_size))
return false;
ret = apply_patch_content(
buf, size, "UPS", name_ups,
ups_apply_patch, patch_data, patch_size);
free(patch_data);
return ret;
}
return false;
}
@ -606,7 +613,21 @@ static bool try_ips_patch(bool allow_ips,
const char *name_ips, uint8_t **buf, ssize_t *size)
{
if (allow_ips && !string_is_empty(name_ips))
return apply_patch_content(buf, size, "IPS", name_ips, ips_apply_patch);
if (path_is_valid(name_ips))
{
ssize_t patch_size;
bool ret = false;
void *patch_data = NULL;
if (!filestream_read_file(name_ips, &patch_data, &patch_size))
return false;
ret = apply_patch_content(
buf, size, "IPS", name_ips,
ips_apply_patch, patch_data, patch_size);
free(patch_data);
return ret;
}
return false;
}