From cf94c0b6c01f1e8b9f695d7d44eb8257cdce2f10 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 24 Jan 2016 16:18:18 +0100 Subject: [PATCH] Move file backend back to file_archive.c --- libretro-common/file/file_archive.c | 151 +++++++++++++++++++- libretro-common/file/file_archive_zlib.c | 142 +----------------- libretro-common/include/file/file_archive.h | 5 +- 3 files changed, 147 insertions(+), 151 deletions(-) diff --git a/libretro-common/file/file_archive.c b/libretro-common/file/file_archive.c index 97bc7e8ab1..e79218399f 100644 --- a/libretro-common/file/file_archive.c +++ b/libretro-common/file/file_archive.c @@ -20,14 +20,26 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include #include -#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#ifdef HAVE_MMAP +#include +#include +#include + +#include +#include +#endif + +#include +#include #include #include #include @@ -59,6 +71,133 @@ enum zlib_compression_mode ZLIB_MODE_DEFLATE = 8 }; +typedef struct +{ +#ifdef HAVE_MMAP + int fd; +#endif + void *data; + size_t size; +} zlib_file_data_t; + +#ifdef HAVE_MMAP +/* Closes, unmaps and frees. */ +static void file_archive_free(void *handle) +{ + zlib_file_data_t *data = (zlib_file_data_t*)handle; + + if (!data) + return; + + if (data->data) + munmap(data->data, data->size); + if (data->fd >= 0) + close(data->fd); + free(data); +} + +static const uint8_t *file_archive_data(void *handle) +{ + zlib_file_data_t *data = (zlib_file_data_t*)handle; + if (!data) + return NULL; + return (const uint8_t*)data->data; +} + +static size_t file_archive_size(void *handle) +{ + zlib_file_data_t *data = (zlib_file_data_t*)handle; + if (!data) + return 0; + return data->size; +} + +static void *file_archive_open(const char *path) +{ + zlib_file_data_t *data = (zlib_file_data_t*)calloc(1, sizeof(*data)); + + if (!data) + return NULL; + + data->fd = open(path, O_RDONLY); + + /* Failed to open archive. */ + if (data->fd < 0) + goto error; + + data->size = path_get_size(path); + if (!data->size) + return data; + + data->data = mmap(NULL, data->size, PROT_READ, MAP_SHARED, data->fd, 0); + if (data->data == MAP_FAILED) + { + data->data = NULL; + + /* Failed to mmap() file */ + goto error; + } + + return data; + +error: + file_archive_free(data); + return NULL; +} +#else + +/* Closes, unmaps and frees. */ +static void file_archive_free(void *handle) +{ + zlib_file_data_t *data = (zlib_file_data_t*)handle; + if (!data) + return; + free(data->data); + free(data); +} + +static const uint8_t *file_archive_data(void *handle) +{ + zlib_file_data_t *data = (zlib_file_data_t*)handle; + if (!data) + return NULL; + return (const uint8_t*)data->data; +} + +static size_t file_archive_size(void *handle) +{ + zlib_file_data_t *data = (zlib_file_data_t*)handle; + if (!data) + return 0; + return data->size; +} + +static void *file_archive_open(const char *path) +{ + ssize_t ret = -1; + bool read_from_file = false; + zlib_file_data_t *data = (zlib_file_data_t*)calloc(1, sizeof(*data)); + + if (!data) + return NULL; + + read_from_file = retro_read_file(path, &data->data, &ret); + + if (!read_from_file || ret < 0) + { + /* Failed to open archive. */ + goto error; + } + + data->size = ret; + return data; + +error: + file_archive_free(data); + return NULL; +} +#endif + static const struct zlib_file_backend *file_archive_get_default_file_backend(void) { return &zlib_backend; @@ -339,15 +478,15 @@ static int file_archive_parse_file_init(zlib_transfer_t *state, if (!state->backend) return -1; - state->handle = state->backend->open(file); + state->handle = file_archive_open(file); if (!state->handle) return -1; - state->zip_size = state->backend->size(state->handle); + state->zip_size = file_archive_size(state->handle); if (state->zip_size < 22) return -1; - state->data = state->backend->data(state->handle); + state->data = file_archive_data(state->handle); state->footer = state->data + state->zip_size - 22; for (;; state->footer--) @@ -400,7 +539,7 @@ int file_archive_parse_file_iterate(void *data, bool *returnerr, const char *fil case ZLIB_TRANSFER_DEINIT: if (state->handle) { - state->backend->free(state->handle); + file_archive_free(state->handle); state->handle = NULL; } break; diff --git a/libretro-common/file/file_archive_zlib.c b/libretro-common/file/file_archive_zlib.c index 3543a59dbf..22314e1936 100644 --- a/libretro-common/file/file_archive_zlib.c +++ b/libretro-common/file/file_archive_zlib.c @@ -22,150 +22,10 @@ #include -#ifdef HAVE_MMAP -#include -#include -#include - -#include -#include -#endif - #include #include #include -#ifdef HAVE_MMAP -typedef struct -{ - int fd; - void *data; - size_t size; -} zlib_file_data_t; - -static void zlib_file_free(void *handle) -{ - zlib_file_data_t *data = (zlib_file_data_t*)handle; - - if (!data) - return; - - if (data->data) - munmap(data->data, data->size); - if (data->fd >= 0) - close(data->fd); - free(data); -} - -static const uint8_t *zlib_file_data(void *handle) -{ - zlib_file_data_t *data = (zlib_file_data_t*)handle; - if (!data) - return NULL; - return (const uint8_t*)data->data; -} - -static size_t zlib_file_size(void *handle) -{ - zlib_file_data_t *data = (zlib_file_data_t*)handle; - if (!data) - return 0; - return data->size; -} - -static void *zlib_file_open(const char *path) -{ - zlib_file_data_t *data = (zlib_file_data_t*)calloc(1, sizeof(*data)); - - if (!data) - return NULL; - - data->fd = open(path, O_RDONLY); - - /* Failed to open archive. */ - if (data->fd < 0) - goto error; - - data->size = path_get_size(path); - if (!data->size) - return data; - - data->data = mmap(NULL, data->size, PROT_READ, MAP_SHARED, data->fd, 0); - if (data->data == MAP_FAILED) - { - data->data = NULL; - - /* Failed to mmap() file */ - goto error; - } - - return data; - -error: - zlib_file_free(data); - return NULL; -} -#else -typedef struct -{ - void *data; - size_t size; -} zlib_file_data_t; - -static void zlib_file_free(void *handle) -{ - zlib_file_data_t *data = (zlib_file_data_t*)handle; - if (!data) - return; - free(data->data); - free(data); -} - -static const uint8_t *zlib_file_data(void *handle) -{ - zlib_file_data_t *data = (zlib_file_data_t*)handle; - if (!data) - return NULL; - return (const uint8_t*)data->data; -} - -static size_t zlib_file_size(void *handle) -{ - zlib_file_data_t *data = (zlib_file_data_t*)handle; - if (!data) - return 0; - return data->size; -} - -static void *zlib_file_open(const char *path) -{ - ssize_t ret = -1; - bool read_from_file = false; - zlib_file_data_t *data = (zlib_file_data_t*)calloc(1, sizeof(*data)); - - if (!data) - return NULL; - - read_from_file = retro_read_file(path, &data->data, &ret); - - if (!read_from_file || ret < 0) - { - /* Failed to open archive. */ - goto error; - } - - data->size = ret; - return data; - -error: - zlib_file_free(data); - return NULL; -} -#endif - const struct zlib_file_backend zlib_backend = { - zlib_file_open, - zlib_file_data, - zlib_file_size, - zlib_file_free, + "zlib" }; diff --git a/libretro-common/include/file/file_archive.h b/libretro-common/include/file/file_archive.h index 813ef3a528..79556b8d6b 100644 --- a/libretro-common/include/file/file_archive.h +++ b/libretro-common/include/file/file_archive.h @@ -62,10 +62,7 @@ typedef struct zlib_transfer struct zlib_file_backend { - void *(*open)(const char *path); - const uint8_t *(*data)(void *handle); - size_t (*size)(void *handle); - void (*free)(void *handle); /* Closes, unmaps and frees. */ + const char *ident; }; /* Returns true when parsing should continue. False to stop. */