Move crc32_calculate to file_archive_zlib.c

This commit is contained in:
twinaphex 2016-01-24 21:50:28 +01:00
parent d53a0c5994
commit aae1faf76f
8 changed files with 53 additions and 23 deletions

View File

@ -76,6 +76,8 @@ static bool read_content_file(unsigned i, const char *path, void **buf,
{ {
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
uint32_t *content_crc_ptr = NULL; uint32_t *content_crc_ptr = NULL;
const struct zlib_file_backend *stream_backend =
file_archive_get_default_file_backend();
#endif #endif
uint8_t *ret_buf = NULL; uint8_t *ret_buf = NULL;
global_t *global = global_get_ptr(); 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 #ifdef HAVE_ZLIB
content_ctl(CONTENT_CTL_GET_CRC, &content_crc_ptr); content_ctl(CONTENT_CTL_GET_CRC, &content_crc_ptr);
*content_crc_ptr = stream_backend->stream_crc_calculate(0, ret_buf, *length);
*content_crc_ptr = file_archive_crc32_calculate(0, ret_buf, *length);
RARCH_LOG("CRC32: 0x%x .\n", (unsigned)*content_crc_ptr); RARCH_LOG("CRC32: 0x%x .\n", (unsigned)*content_crc_ptr);
#endif #endif

View File

@ -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); ret = handle.backend->stream_decompress_data_to_file_iterate(handle.stream);
}while(ret == 0); }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) if (handle.real_checksum != crc32)
{ {

View File

@ -39,7 +39,6 @@
#endif #endif
#include <compat/strl.h> #include <compat/strl.h>
#include <compat/zlib.h>
#include <file/file_archive.h> #include <file/file_archive.h>
#include <file/file_path.h> #include <file/file_path.h>
#include <retro_file.h> #include <retro_file.h>
@ -399,14 +398,6 @@ static int file_archive_parse_file_init(zlib_transfer_t *state,
return 0; 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: * file_archive_inflate_data_to_file:
* @path : filename path of archive. * @path : filename path of archive.
@ -443,7 +434,7 @@ int file_archive_inflate_data_to_file(
goto end; goto end;
} }
handle->real_checksum = file_archive_crc32_calculate( handle->real_checksum = handle->backend->stream_crc_calculate(
0, handle->data, size); 0, handle->data, size);
#if 0 #if 0

View File

@ -188,6 +188,12 @@ static void zlib_stream_compress_init(void *data, int level)
deflateInit(stream, 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 = { const struct zlib_file_backend zlib_backend = {
zlib_stream_new, zlib_stream_new,
zlib_stream_free, zlib_stream_free,
@ -202,5 +208,6 @@ const struct zlib_file_backend zlib_backend = {
zlib_stream_compress_init, zlib_stream_compress_init,
zlib_stream_compress_free, zlib_stream_compress_free,
zlib_stream_compress_data_to_file, zlib_stream_compress_data_to_file,
zlib_stream_crc32_calculate,
"zlib" "zlib"
}; };

View File

@ -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) static bool png_write_crc(RFILE *file, const uint8_t *data, size_t size)
{ {
uint8_t crc_raw[4] = {0}; 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); dword_write_be(crc_raw, crc);
return retro_fwrite(file, crc_raw, sizeof(crc_raw)) == sizeof(crc_raw); return retro_fwrite(file, crc_raw, sizeof(crc_raw)) == sizeof(crc_raw);

View File

@ -62,6 +62,7 @@ struct zlib_file_backend
void (*stream_compress_init)(void *, int); void (*stream_compress_init)(void *, int);
void (*stream_compress_free)(void *); void (*stream_compress_free)(void *);
int (*stream_compress_data_to_file)(void *); int (*stream_compress_data_to_file)(void *);
uint32_t (*stream_crc_calculate)(uint32_t, const uint8_t *, size_t);
const char *ident; 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, const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata); uint32_t crc32, void *userdata);
uint32_t file_archive_crc32_calculate(uint32_t crc, const uint8_t *data, size_t length);
/** /**
* zlib_parse_file: * zlib_parse_file:
* @file : filename path of archive * @file : filename path of archive

38
patch.c
View File

@ -55,7 +55,10 @@ static uint8_t bps_read(struct bps_data *bps)
{ {
uint8_t data = bps->modify_data[bps->modify_offset++]; uint8_t data = bps->modify_data[bps->modify_offset++];
#ifdef HAVE_ZLIB #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 #endif
return data; 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) 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) if (!bps)
return; return;
bps->target_data[bps->output_offset++] = data; bps->target_data[bps->output_offset++] = data;
#ifdef HAVE_ZLIB #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 #endif
} }
@ -99,6 +106,10 @@ patch_error_t bps_apply_patch(
struct bps_data bps = {0}; struct bps_data bps = {0};
uint32_t modify_source_checksum = 0, modify_target_checksum = 0, uint32_t modify_source_checksum = 0, modify_target_checksum = 0,
modify_modify_checksum = 0, checksum; 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) if (modify_length < 19)
return PATCH_PATCH_TOO_SMALL; return PATCH_PATCH_TOO_SMALL;
@ -186,7 +197,7 @@ patch_error_t bps_apply_patch(
modify_modify_checksum |= bps_read(&bps) << i; modify_modify_checksum |= bps_read(&bps) << i;
#ifdef HAVE_ZLIB #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 #else
return PATCH_PATCH_CHECKSUM_INVALID; return PATCH_PATCH_CHECKSUM_INVALID;
#endif #endif
@ -216,11 +227,16 @@ struct ups_data
static uint8_t ups_patch_read(struct ups_data *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) if (data && data->patch_offset < data->patch_length)
{ {
uint8_t n = data->patch_data[data->patch_offset++]; uint8_t n = data->patch_data[data->patch_offset++];
#ifdef HAVE_ZLIB #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 #endif
return n; 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) 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) if (data && data->source_offset < data->source_length)
{ {
uint8_t n = data->source_data[data->source_offset++]; uint8_t n = data->source_data[data->source_offset++];
#ifdef HAVE_ZLIB #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 #endif
return n; 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) 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) if (data && data->target_offset < data->target_length)
{ {
data->target_data[data->target_offset] = n; data->target_data[data->target_offset] = n;
#ifdef HAVE_ZLIB #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 #endif
} }

View File

@ -202,13 +202,17 @@ static int database_info_iterate_playlist(
{ {
ssize_t ret; ssize_t ret;
int read_from = read_file(name, (void**)&db_state->buf, &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) if (read_from != 1 || ret <= 0)
return 0; return 0;
#ifdef HAVE_ZLIB #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 #endif
db->type = DATABASE_TYPE_CRC_LOOKUP; db->type = DATABASE_TYPE_CRC_LOOKUP;
} }