From f8b1a8a382b1e9b772c236eb1e74a1c63fd3db48 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 28 Mar 2015 20:06:59 +0100 Subject: [PATCH] No dependencies in file_extract.c other than what is in libretro-common --- content.c | 2 +- database_info.c | 2 +- file_extract.c | 35 ++++++++++++++++++++++++++++------- file_extract.h | 4 ++++ hash.h | 19 ------------------- patch.c | 13 +++++++------ 6 files changed, 41 insertions(+), 34 deletions(-) diff --git a/content.c b/content.c index d90e71ecc8..6ef643caaa 100644 --- a/content.c +++ b/content.c @@ -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; diff --git a/database_info.c b/database_info.c index 4f038b823e..e3d4266844 100644 --- a/database_info.c +++ b/database_info.c @@ -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); diff --git a/file_extract.c b/file_extract.c index e69dc6aed0..6a38cd7e96 100644 --- a/file_extract.c +++ b/file_extract.c @@ -15,18 +15,15 @@ */ #include "file_extract.h" -#include "file_ops.h" #include #include #include +#include #include #include #include - #include -#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: { diff --git a/file_extract.h b/file_extract.h index 9bae8b8dd6..317732d7d2 100644 --- a/file_extract.h +++ b/file_extract.h @@ -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 diff --git a/hash.h b/hash.h index 8d3d23c58b..a15219748c 100644 --- a/hash.h +++ b/hash.h @@ -60,25 +60,6 @@ **/ void sha256_hash(char *out, const uint8_t *in, size_t size); -#ifdef HAVE_ZLIB -#include - -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) */ diff --git a/patch.c b/patch.c index 6160293438..c9b89a16db 100644 --- a/patch.c +++ b/patch.c @@ -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)