diff --git a/libretro-common/encodings/encoding_crc32.c b/libretro-common/encodings/encoding_crc32.c index 4775e76357..85881e521b 100644 --- a/libretro-common/encodings/encoding_crc32.c +++ b/libretro-common/encodings/encoding_crc32.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include static const uint32_t crc32_table[256] = { 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, @@ -88,3 +90,50 @@ uint32_t encoding_crc32(uint32_t crc, const uint8_t *buf, size_t len) return crc ^ 0xffffffff; } + +#define CRC32_BUFFER_SIZE 1048576 +#define CRC32_MAX_MB 64 + +/** + * Calculate a CRC32 from the first part of the given file. + * "first part" being the first (CRC32_BUFFER_SIZE * CRC32_MAX_MB) + * bytes. + * + * Returns: the crc32, or 0 if there was an error. + */ +uint32_t file_crc32(uint32_t crc, const char *path) { + if(path == NULL) + return 0; + + RFILE *file = NULL; + unsigned char *buf = NULL; + int i, nread; + + file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0); + if(file == NULL) + goto error; + + buf = (char *)malloc(CRC32_BUFFER_SIZE); + if(buf == NULL) + goto error; + + for(i = 0; i < CRC32_MAX_MB; i++) { + nread = filestream_read(file, buf, CRC32_BUFFER_SIZE); + if(nread < 0) + goto error; + + crc = encoding_crc32(crc, buf, nread); + if(filestream_eof(file)) + break; + } + free(buf); + filestream_close(file); + return crc; + + error: + if(buf) + free(buf); + if(file) + filestream_close(file); + return 0; +} diff --git a/libretro-common/include/encodings/crc32.h b/libretro-common/include/encodings/crc32.h index 76fbb5475e..25e926276f 100644 --- a/libretro-common/include/encodings/crc32.h +++ b/libretro-common/include/encodings/crc32.h @@ -31,6 +31,7 @@ RETRO_BEGIN_DECLS uint32_t encoding_crc32(uint32_t crc, const uint8_t *buf, size_t len); +uint32_t file_crc32(uint32_t crc, const char *path); RETRO_END_DECLS diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index 680a809ec5..b24102ff3b 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -482,7 +482,8 @@ int filestream_close(RFILE *stream) * * Read the contents of a file into @buf. * - * Returns: number of items read, -1 on error. + * Returns: 1 on success, 0 on failure + * In the error case, the dereferenced buf is set to NULL and the len is set to -1. */ int64_t filestream_read_file(const char *path, void **buf, int64_t *len) { diff --git a/tasks/task_content.c b/tasks/task_content.c index c2df099419..c366ab1673 100644 --- a/tasks/task_content.c +++ b/tasks/task_content.c @@ -603,9 +603,6 @@ static bool content_file_load( } else { - RARCH_LOG("%s\n", - msg_hash_to_str( - MSG_CONTENT_LOADING_SKIPPED_IMPLEMENTATION_WILL_DO_IT)); #ifdef HAVE_COMPRESSION if ( !content_ctx->block_extract @@ -619,6 +616,9 @@ static bool content_file_load( goto error; #endif } + RARCH_LOG("%s\n", msg_hash_to_str(MSG_CONTENT_LOADING_SKIPPED_IMPLEMENTATION_WILL_DO_IT)); + content_rom_crc = file_crc32(0, path); + RARCH_LOG("CRC32: 0x%x .\n", (unsigned)content_rom_crc); } load_info.content = content;