No dependencies in file_extract.c other than what is in libretro-common

This commit is contained in:
twinaphex 2015-03-28 20:06:59 +01:00
parent 150887f0eb
commit f8b1a8a382
6 changed files with 41 additions and 34 deletions

View File

@ -73,7 +73,7 @@ static bool read_content_file(unsigned i, const char *path, void **buf,
if (!global->block_patch)
patch_content(&ret_buf, length);
global->content_crc = crc32_calculate(ret_buf, *length);
global->content_crc = zlib_crc32_calculate(ret_buf, *length);
RARCH_LOG("CRC32: 0x%x .\n", (unsigned)global->content_crc);
*buf = ret_buf;

View File

@ -142,7 +142,7 @@ int database_info_write_rdl_iterate(database_info_rdl_handle_t *dbl)
rarch_main_msg_queue_push(msg, 1, 180, true);
crc = crc32_calculate(ret_buf, ret);
crc = zlib_crc32_calculate(ret_buf, ret);
RARCH_LOG("CRC32: 0x%x .\n", (unsigned)crc);

View File

@ -15,18 +15,15 @@
*/
#include "file_extract.h"
#include "file_ops.h"
#include <file/file_path.h>
#include <compat/strl.h>
#include <retro_miscellaneous.h>
#include <string/string_list.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "hash.h"
/* File backends. Can be fleshed out later, but keep it simple for now.
* The file is mapped to memory directly (via mmap() or just
* plain read_file()).
@ -62,6 +59,18 @@ typedef struct
size_t size;
} zlib_file_data_t;
bool zlib_write_file(const char *path, const void *data, ssize_t size)
{
bool ret = false;
FILE *file = fopen(path, "wb");
if (!file)
return false;
ret = fwrite(data, 1, size, file) == size;
fclose(file);
return ret;
}
static void zlib_file_free(void *handle)
{
zlib_file_data_t *data = (zlib_file_data_t*)handle;
@ -300,6 +309,18 @@ int zlib_inflate_data_to_file_iterate(void *data)
return 0;
}
uint32_t zlib_crc32_calculate(const uint8_t *data, size_t length)
{
return crc32(0, data, length);
}
uint32_t zlib_crc32_adjust(uint32_t crc, uint8_t data)
{
/* zlib and nall have different assumptions on "sign" for this
* function. */
return ~crc32(~crc, &data, 1);
}
/**
* zlib_inflate_data_to_file:
* @path : filename path of archive.
@ -327,12 +348,12 @@ int zlib_inflate_data_to_file(zlib_file_handle_t *handle,
goto end;
}
handle->real_checksum = crc32_calculate(handle->data, size);
handle->real_checksum = zlib_crc32_calculate(handle->data, size);
if (handle->real_checksum != checksum)
RARCH_WARN("File CRC differs from ZIP CRC. File: 0x%x, ZIP: 0x%x.\n",
(unsigned)handle->real_checksum, (unsigned)checksum);
if (!write_file(path, handle->data, size))
if (!zlib_write_file(path, handle->data, size))
GOTO_END_ERROR();
end:
@ -484,7 +505,7 @@ static int zip_extract_cb(const char *name, const char *valid_exts,
switch (cmode)
{
case ZLIB_MODE_UNCOMPRESSED:
data->found_content = write_file(new_path, cdata, size);
data->found_content = zlib_write_file(new_path, cdata, size);
return false;
case ZLIB_MODE_DEFLATE:
{

View File

@ -33,6 +33,10 @@ typedef int (*zlib_file_cb)(const char *name, const char *valid_exts,
const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata);
uint32_t zlib_crc32_calculate(const uint8_t *data, size_t length);
uint32_t zlib_crc32_adjust(uint32_t crc, uint8_t data);
/**
* zlib_parse_file:
* @file : filename path of archive

19
hash.h
View File

@ -60,25 +60,6 @@
**/
void sha256_hash(char *out, const uint8_t *in, size_t size);
#ifdef HAVE_ZLIB
#include <zlib.h>
static INLINE uint32_t crc32_calculate(const uint8_t *data, size_t length)
{
return crc32(0, data, length);
}
static INLINE uint32_t crc32_adjust(uint32_t crc, uint8_t data)
{
/* zlib and nall have different
* assumptions on "sign" for this function. */
return ~crc32(~crc, &data, 1);
}
#else
uint32_t crc32_calculate(const uint8_t *data, size_t length);
uint32_t crc32_adjust(uint32_t crc, uint8_t data);
#endif
typedef struct SHA1Context
{
unsigned Message_Digest[5]; /* Message Digest (output) */

13
patch.c
View File

@ -25,6 +25,7 @@
#include "patch.h"
#include "hash.h"
#include "file_ops.h"
#include "file_extract.h"
#include "general.h"
#include "retroarch_logger.h"
@ -50,7 +51,7 @@ struct bps_data
static uint8_t bps_read(struct bps_data *bps)
{
uint8_t data = bps->modify_data[bps->modify_offset++];
bps->modify_checksum = crc32_adjust(bps->modify_checksum, data);
bps->modify_checksum = zlib_crc32_adjust(bps->modify_checksum, data);
return data;
}
@ -77,7 +78,7 @@ static void bps_write(struct bps_data *bps, uint8_t data)
return;
bps->target_data[bps->output_offset++] = data;
bps->target_checksum = crc32_adjust(bps->target_checksum, data);
bps->target_checksum = zlib_crc32_adjust(bps->target_checksum, data);
}
patch_error_t bps_apply_patch(
@ -175,7 +176,7 @@ patch_error_t bps_apply_patch(
for (i = 0; i < 32; i += 8)
modify_modify_checksum |= bps_read(&bps) << i;
bps.source_checksum = crc32_calculate(bps.source_data, bps.source_length);
bps.source_checksum = zlib_crc32_calculate(bps.source_data, bps.source_length);
bps.target_checksum = ~bps.target_checksum;
if (bps.source_checksum != modify_source_checksum)
@ -204,7 +205,7 @@ static uint8_t ups_patch_read(struct ups_data *data)
if (data && data->patch_offset < data->patch_length)
{
uint8_t n = data->patch_data[data->patch_offset++];
data->patch_checksum = crc32_adjust(data->patch_checksum, n);
data->patch_checksum = zlib_crc32_adjust(data->patch_checksum, n);
return n;
}
return 0x00;
@ -215,7 +216,7 @@ static uint8_t ups_source_read(struct ups_data *data)
if (data && data->source_offset < data->source_length)
{
uint8_t n = data->source_data[data->source_offset++];
data->source_checksum = crc32_adjust(data->source_checksum, n);
data->source_checksum = zlib_crc32_adjust(data->source_checksum, n);
return n;
}
return 0x00;
@ -226,7 +227,7 @@ static void ups_target_write(struct ups_data *data, uint8_t n)
if (data && data->target_offset < data->target_length)
{
data->target_data[data->target_offset] = n;
data->target_checksum = crc32_adjust(data->target_checksum, n);
data->target_checksum = zlib_crc32_adjust(data->target_checksum, n);
}
if (data)