Skip to content

Commit

Permalink
- use the detected stream backend's crc32 function
Browse files Browse the repository at this point in the history
- remove zlib/7zip ifdef from archive_file.c, task_content.c and task_database.c
- don't re-compute CRC from one stored in archive
  • Loading branch information
Brad Parker committed Sep 18, 2016
1 parent 5d2c0ba commit f13bce4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 37 deletions.
1 change: 1 addition & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@ endif
ifeq ($(HAVE_COMPRESSION), 1)
DEFINES += -DHAVE_COMPRESSION
OBJ += libretro-common/file/archive_file.o \
libretro-common/encodings/encoding_crc32.o \
tasks/task_decompress.o
endif

Expand Down
14 changes: 4 additions & 10 deletions libretro-common/file/archive_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ static int file_archive_decompress_data_to_file(
goto end;
}

handle->real_checksum = handle->backend->stream_crc_calculate(
0, handle->data, size);
handle->backend->stream_free(handle->stream);

#if 0
handle->real_checksum = handle->backend->stream_crc_calculate(
0, handle->data, size);
if (handle->real_checksum != checksum)
{
/* File CRC difers from archive CRC. */
Expand Down Expand Up @@ -668,16 +668,10 @@ struct string_list *file_archive_file_list_new(const char *path,
const char* ext)
{
#ifdef HAVE_COMPRESSION
const char* file_ext = path_get_extension(path);
bool compressed = path_is_compressed_file(path);

#ifdef HAVE_7ZIP
if (string_is_equal_noncase(file_ext, "7z"))
if (compressed)
return file_archive_get_file_list(path, ext);
#endif
#ifdef HAVE_ZLIB
if (string_is_equal_noncase(file_ext, "zip"))
return file_archive_get_file_list(path, ext);
#endif
#endif
return NULL;
}
Expand Down
5 changes: 3 additions & 2 deletions libretro-common/file/archive_file_7z.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <streams/file_stream.h>
#include <retro_miscellaneous.h>
#include <encodings/utf.h>
#include <encodings/crc32.h>
#include <string/stdstring.h>
#include <lists/string_list.h>
#include <file/file_path.h>
Expand Down Expand Up @@ -334,7 +335,7 @@ static int sevenzip_parse_file_iterate_step_internal(
if (res != SZ_OK)
return -1;

strlcpy(filename, infile, sizeof(infile));
strlcpy(filename, infile, PATH_MAX_LENGTH);

*cmode = ARCHIVE_MODE_COMPRESSED;
*checksum = file->Crc;
Expand Down Expand Up @@ -380,7 +381,7 @@ static int sevenzip_parse_file_iterate_step(file_archive_transfer_t *state,
static uint32_t sevenzip_stream_crc32_calculate(uint32_t crc,
const uint8_t *data, size_t length)
{
return CrcUpdate(crc, data, length);
return encoding_crc32(crc, data, length);
}

const struct file_archive_file_backend sevenzip_backend = {
Expand Down
11 changes: 6 additions & 5 deletions libretro-common/file/archive_file_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <streams/file_stream.h>
#include <string.h>
#include <retro_miscellaneous.h>
#include <encodings/crc32.h>

#ifndef CENTRAL_FILE_HEADER_SIGNATURE
#define CENTRAL_FILE_HEADER_SIGNATURE 0x02014b50
Expand Down Expand Up @@ -204,7 +205,7 @@ static void zlib_stream_compress_init(void *data, int level)
static uint32_t zlib_stream_crc32_calculate(uint32_t crc,
const uint8_t *data, size_t length)
{
return crc32(crc, data, length);
return encoding_crc32(crc, data, length);
}

struct decomp_state
Expand Down Expand Up @@ -233,7 +234,7 @@ static bool zip_file_decompressed_handle(
ret = handle->backend->stream_decompress_data_to_file_iterate(
handle->stream);
}while(ret == 0);

#if 0
handle->real_checksum = handle->backend->stream_crc_calculate(0,
handle->data, size);

Expand All @@ -242,12 +243,12 @@ static bool zip_file_decompressed_handle(
//RARCH_ERR("%s\n", msg_hash_to_str(MSG_INFLATED_CHECKSUM_DID_NOT_MATCH_CRC32));
goto error;
}

#endif
if (handle->stream)
free(handle->stream);

return true;

#if 0
error:
if (handle->stream)
free(handle->stream);
Expand All @@ -256,7 +257,7 @@ static bool zip_file_decompressed_handle(

handle->stream = NULL;
handle->data = NULL;

#endif
return false;
}

Expand Down
18 changes: 10 additions & 8 deletions tasks/task_content.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ typedef struct content_stream
uint32_t crc;
} content_stream_t;

static const struct file_archive_file_backend *stream_backend = NULL;
static struct string_list *temporary_content = NULL;
static bool _content_is_inited = false;
static bool core_does_not_need_content = false;
Expand All @@ -125,7 +124,7 @@ static uint32_t content_crc = 0;
static int content_file_read(const char *path, void **buf, ssize_t *length)
{
#ifdef HAVE_COMPRESSION
if (path_contains_compressed_file(path))
if (path_is_compressed_file(path))
{
if (file_archive_compressed_read(path, buf, NULL, length))
return 1;
Expand Down Expand Up @@ -389,7 +388,7 @@ static bool content_load(content_ctx_info_t *info)
static bool read_content_file(unsigned i, const char *path, void **buf,
ssize_t *length)
{
#ifdef HAVE_ZLIB
#ifdef HAVE_COMPRESSION
content_stream_t stream_info;
uint32_t *content_crc_ptr = NULL;
#endif
Expand All @@ -411,17 +410,20 @@ static bool read_content_file(unsigned i, const char *path, void **buf,
if (!global->patch.block_patch)
patch_content(&ret_buf, length);

#ifdef HAVE_ZLIB
#ifdef HAVE_COMPRESSION
content_get_crc(&content_crc_ptr);

stream_info.a = 0;
stream_info.b = ret_buf;
stream_info.c = *length;

if (!stream_backend)
stream_backend = file_archive_get_zlib_file_backend();
stream_info.crc = stream_backend->stream_crc_calculate(
stream_info.a, stream_info.b, stream_info.c);
const struct file_archive_file_backend *stream_backend =
file_archive_get_file_backend(path);

if (stream_backend)
stream_info.crc = stream_backend->stream_crc_calculate(
stream_info.a, stream_info.b, stream_info.c);

*content_crc_ptr = stream_info.crc;

RARCH_LOG("CRC32: 0x%x .\n", (unsigned)*content_crc_ptr);
Expand Down
26 changes: 14 additions & 12 deletions tasks/task_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ typedef struct db_handle

#ifdef HAVE_LIBRETRODB

#ifdef HAVE_ZLIB
static int zlib_compare_crc32(const char *name, const char *valid_exts,
#ifdef HAVE_COMPRESSION
static int archive_compare_crc32(const char *name, const char *valid_exts,
const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
uint32_t crc32, void *userdata)
{
Expand Down Expand Up @@ -166,17 +166,19 @@ static bool file_get_crc(database_state_handle_t *db_state,
int read_from = filestream_read_file(
name, (void**)&db_state->buf, &ret);

#ifdef HAVE_ZLIB
const struct file_archive_file_backend *stream_backend =
file_archive_get_zlib_file_backend();
#endif

if (read_from != 1 || ret <= 0)
return 0;

#ifdef HAVE_ZLIB
*crc = stream_backend->stream_crc_calculate(
0, db_state->buf, ret);
#ifdef HAVE_COMPRESSION
if(!path_is_compressed_file(name))
{
const struct file_archive_file_backend *stream_backend =
file_archive_get_file_backend(name);

if (stream_backend)
*crc = stream_backend->stream_crc_calculate(
0, db_state->buf, ret);
}
#endif

return 1;
Expand Down Expand Up @@ -409,13 +411,13 @@ static int task_database_iterate_playlist_archive(
database_info_handle_t *db, const char *name)
{
bool returnerr = true;
#ifdef HAVE_ZLIB
#ifdef HAVE_COMPRESSION
if (db_state->crc != 0)
return task_database_iterate_crc_lookup(
db_state, db, db_state->archive_name);

if (file_archive_parse_file_iterate(&db->state,
&returnerr, name, NULL, zlib_compare_crc32,
&returnerr, name, NULL, archive_compare_crc32,
(void*)db_state) != 0)
return 0;

Expand Down

0 comments on commit f13bce4

Please sign in to comment.