From aae1faf76f4578772d7d843c34f23631893bd61e Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 24 Jan 2016 21:50:28 +0100 Subject: [PATCH] Move crc32_calculate to file_archive_zlib.c --- content.c | 5 +-- file_ops.c | 2 +- libretro-common/file/file_archive.c | 11 +----- libretro-common/file/file_archive_zlib.c | 7 ++++ libretro-common/formats/png/rpng_encode.c | 4 ++- libretro-common/include/file/file_archive.h | 3 +- patch.c | 38 +++++++++++++++++---- tasks/task_database.c | 6 +++- 8 files changed, 53 insertions(+), 23 deletions(-) diff --git a/content.c b/content.c index 15bf21976a..eeecc7c15f 100644 --- a/content.c +++ b/content.c @@ -76,6 +76,8 @@ static bool read_content_file(unsigned i, const char *path, void **buf, { #ifdef HAVE_ZLIB uint32_t *content_crc_ptr = NULL; + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); #endif uint8_t *ret_buf = NULL; global_t *global = global_get_ptr(); @@ -97,8 +99,7 @@ static bool read_content_file(unsigned i, const char *path, void **buf, #ifdef HAVE_ZLIB content_ctl(CONTENT_CTL_GET_CRC, &content_crc_ptr); - - *content_crc_ptr = file_archive_crc32_calculate(0, ret_buf, *length); + *content_crc_ptr = stream_backend->stream_crc_calculate(0, ret_buf, *length); RARCH_LOG("CRC32: 0x%x .\n", (unsigned)*content_crc_ptr); #endif diff --git a/file_ops.c b/file_ops.c index 170b6a0ece..cc78aedb48 100644 --- a/file_ops.c +++ b/file_ops.c @@ -403,7 +403,7 @@ static int zip_file_decompressed(const char *name, const char *valid_exts, ret = handle.backend->stream_decompress_data_to_file_iterate(handle.stream); }while(ret == 0); - handle.real_checksum = file_archive_crc32_calculate(0, handle.data, size); + handle.real_checksum = handle.backend->stream_crc_calculate(0, handle.data, size); if (handle.real_checksum != crc32) { diff --git a/libretro-common/file/file_archive.c b/libretro-common/file/file_archive.c index 203d875fe3..534290565d 100644 --- a/libretro-common/file/file_archive.c +++ b/libretro-common/file/file_archive.c @@ -39,7 +39,6 @@ #endif #include -#include #include #include #include @@ -399,14 +398,6 @@ static int file_archive_parse_file_init(zlib_transfer_t *state, return 0; } -uint32_t file_archive_crc32_calculate( - uint32_t crc, - const uint8_t *data, - size_t length) -{ - return crc32(crc, data, length); -} - /** * file_archive_inflate_data_to_file: * @path : filename path of archive. @@ -443,7 +434,7 @@ int file_archive_inflate_data_to_file( goto end; } - handle->real_checksum = file_archive_crc32_calculate( + handle->real_checksum = handle->backend->stream_crc_calculate( 0, handle->data, size); #if 0 diff --git a/libretro-common/file/file_archive_zlib.c b/libretro-common/file/file_archive_zlib.c index 28b017cc4a..7168eabe7f 100644 --- a/libretro-common/file/file_archive_zlib.c +++ b/libretro-common/file/file_archive_zlib.c @@ -188,6 +188,12 @@ static void zlib_stream_compress_init(void *data, int level) deflateInit(stream, level); } +static uint32_t zlib_stream_crc32_calculate(uint32_t crc, + const uint8_t *data, size_t length) +{ + return crc32(crc, data, length); +} + const struct zlib_file_backend zlib_backend = { zlib_stream_new, zlib_stream_free, @@ -202,5 +208,6 @@ const struct zlib_file_backend zlib_backend = { zlib_stream_compress_init, zlib_stream_compress_free, zlib_stream_compress_data_to_file, + zlib_stream_crc32_calculate, "zlib" }; diff --git a/libretro-common/formats/png/rpng_encode.c b/libretro-common/formats/png/rpng_encode.c index e842a99719..77772d7d09 100644 --- a/libretro-common/formats/png/rpng_encode.c +++ b/libretro-common/formats/png/rpng_encode.c @@ -48,7 +48,9 @@ static void dword_write_be(uint8_t *buf, uint32_t val) static bool png_write_crc(RFILE *file, const uint8_t *data, size_t size) { uint8_t crc_raw[4] = {0}; - uint32_t crc = file_archive_crc32_calculate(0, data, size); + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); + uint32_t crc = stream_backend->stream_crc_calculate(0, data, size); dword_write_be(crc_raw, crc); return retro_fwrite(file, crc_raw, sizeof(crc_raw)) == sizeof(crc_raw); diff --git a/libretro-common/include/file/file_archive.h b/libretro-common/include/file/file_archive.h index 9463d74cac..d31739243c 100644 --- a/libretro-common/include/file/file_archive.h +++ b/libretro-common/include/file/file_archive.h @@ -62,6 +62,7 @@ struct zlib_file_backend void (*stream_compress_init)(void *, int); void (*stream_compress_free)(void *); int (*stream_compress_data_to_file)(void *); + uint32_t (*stream_crc_calculate)(uint32_t, const uint8_t *, size_t); const char *ident; }; @@ -82,8 +83,6 @@ typedef int (*file_archive_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 file_archive_crc32_calculate(uint32_t crc, const uint8_t *data, size_t length); - /** * zlib_parse_file: * @file : filename path of archive diff --git a/patch.c b/patch.c index 3d3f4fa415..257dfa1df2 100644 --- a/patch.c +++ b/patch.c @@ -55,7 +55,10 @@ static uint8_t bps_read(struct bps_data *bps) { uint8_t data = bps->modify_data[bps->modify_offset++]; #ifdef HAVE_ZLIB - bps->modify_checksum = ~file_archive_crc32_calculate(~bps->modify_checksum, &data, 1); + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); + bps->modify_checksum = ~stream_backend->stream_crc_calculate( + ~bps->modify_checksum, &data, 1); #endif return data; } @@ -79,12 +82,16 @@ static uint64_t bps_decode(struct bps_data *bps) static void bps_write(struct bps_data *bps, uint8_t data) { +#ifdef HAVE_ZLIB + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); +#endif if (!bps) return; bps->target_data[bps->output_offset++] = data; #ifdef HAVE_ZLIB - bps->target_checksum = ~file_archive_crc32_calculate(~bps->target_checksum, &data, 1); + bps->target_checksum = ~stream_backend->stream_crc_calculate(~bps->target_checksum, &data, 1); #endif } @@ -99,6 +106,10 @@ patch_error_t bps_apply_patch( struct bps_data bps = {0}; uint32_t modify_source_checksum = 0, modify_target_checksum = 0, modify_modify_checksum = 0, checksum; +#ifdef HAVE_ZLIB + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); +#endif if (modify_length < 19) return PATCH_PATCH_TOO_SMALL; @@ -186,7 +197,7 @@ patch_error_t bps_apply_patch( modify_modify_checksum |= bps_read(&bps) << i; #ifdef HAVE_ZLIB - bps.source_checksum = file_archive_crc32_calculate(0, bps.source_data, bps.source_length); + bps.source_checksum = stream_backend->stream_crc_calculate(0, bps.source_data, bps.source_length); #else return PATCH_PATCH_CHECKSUM_INVALID; #endif @@ -216,11 +227,16 @@ struct ups_data static uint8_t ups_patch_read(struct ups_data *data) { +#ifdef HAVE_ZLIB + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); +#endif + if (data && data->patch_offset < data->patch_length) { uint8_t n = data->patch_data[data->patch_offset++]; #ifdef HAVE_ZLIB - data->patch_checksum = ~file_archive_crc32_calculate(~data->patch_checksum, &n, 1); + data->patch_checksum = ~stream_backend->stream_crc_calculate(~data->patch_checksum, &n, 1); #endif return n; } @@ -229,11 +245,16 @@ static uint8_t ups_patch_read(struct ups_data *data) static uint8_t ups_source_read(struct ups_data *data) { +#ifdef HAVE_ZLIB + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); +#endif + if (data && data->source_offset < data->source_length) { uint8_t n = data->source_data[data->source_offset++]; #ifdef HAVE_ZLIB - data->source_checksum = ~file_archive_crc32_calculate(~data->source_checksum, &n, 1); + data->source_checksum = ~stream_backend->stream_crc_calculate(~data->source_checksum, &n, 1); #endif return n; } @@ -242,11 +263,16 @@ static uint8_t ups_source_read(struct ups_data *data) static void ups_target_write(struct ups_data *data, uint8_t n) { +#ifdef HAVE_ZLIB + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); +#endif + if (data && data->target_offset < data->target_length) { data->target_data[data->target_offset] = n; #ifdef HAVE_ZLIB - data->target_checksum = ~file_archive_crc32_calculate(~data->target_checksum, &n, 1); + data->target_checksum = ~stream_backend->stream_crc_calculate(~data->target_checksum, &n, 1); #endif } diff --git a/tasks/task_database.c b/tasks/task_database.c index 990a067c82..c67f5e401e 100644 --- a/tasks/task_database.c +++ b/tasks/task_database.c @@ -202,13 +202,17 @@ static int database_info_iterate_playlist( { ssize_t ret; int read_from = read_file(name, (void**)&db_state->buf, &ret); +#ifdef HAVE_ZLIB + const struct zlib_file_backend *stream_backend = + file_archive_get_default_file_backend(); +#endif if (read_from != 1 || ret <= 0) return 0; #ifdef HAVE_ZLIB - db_state->crc = file_archive_crc32_calculate(0, db_state->buf, ret); + db_state->crc = stream_backend->stream_crc_calculate(0, db_state->buf, ret); #endif db->type = DATABASE_TYPE_CRC_LOOKUP; }