From 78feccd0a38bd1f430a9ca09bb89d547c573f986 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 24 Jan 2016 06:18:39 +0100 Subject: [PATCH] Split up file extract zlib driver code to separate file --- Makefile.common | 1 + griffin/griffin.c | 1 + libretro-common/file/file_extract.c | 159 +----------------- libretro-common/file/file_extract_zlib.c | 171 ++++++++++++++++++++ libretro-common/include/file/file_extract.h | 15 ++ 5 files changed, 190 insertions(+), 157 deletions(-) create mode 100644 libretro-common/file/file_extract_zlib.c diff --git a/Makefile.common b/Makefile.common index cd86c611fb..439188021b 100644 --- a/Makefile.common +++ b/Makefile.common @@ -796,6 +796,7 @@ endif ifeq ($(HAVE_ZLIB), 1) OBJ += libretro-common/file/file_extract.o \ + libretro-common/file/file_extract_zlib.o \ tasks/task_decompress.o OBJ += $(ZLIB_OBJS) DEFINES += -DHAVE_ZLIB diff --git a/griffin/griffin.c b/griffin/griffin.c index 853571830e..37a915e9d5 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -42,6 +42,7 @@ CONSOLE EXTENSIONS #ifdef HAVE_ZLIB #include "../libretro-common/file/file_extract.c" +#include "../libretro-common/file/file_extract_zlib.c" #endif /*============================================================ diff --git a/libretro-common/file/file_extract.c b/libretro-common/file/file_extract.c index 6249069858..ea087b1735 100644 --- a/libretro-common/file/file_extract.c +++ b/libretro-common/file/file_extract.c @@ -20,11 +20,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include + #include #include #include -#include #include #include @@ -34,19 +35,6 @@ #include #include -/* File backends. Can be fleshed out later, but keep it simple for now. - * The file is mapped to memory directly (via mmap() or just - * plain retro_read_file()). - */ - -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. */ -}; - #ifndef CENTRAL_FILE_HEADER_SIGNATURE #define CENTRAL_FILE_HEADER_SIGNATURE 0x02014b50 #endif @@ -55,154 +43,11 @@ struct zlib_file_backend #define END_OF_CENTRAL_DIR_SIGNATURE 0x06054b50 #endif -#ifdef HAVE_MMAP -#include -#include -#include - -#include -#include - -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 - -static const struct zlib_file_backend zlib_backend = { - zlib_file_open, - zlib_file_data, - zlib_file_size, - zlib_file_free, -}; - static const struct zlib_file_backend *zlib_get_default_file_backend(void) { return &zlib_backend; } - #undef GOTO_END_ERROR #define GOTO_END_ERROR() do { \ ret = false; \ diff --git a/libretro-common/file/file_extract_zlib.c b/libretro-common/file/file_extract_zlib.c new file mode 100644 index 0000000000..3162b670e8 --- /dev/null +++ b/libretro-common/file/file_extract_zlib.c @@ -0,0 +1,171 @@ +/* Copyright (C) 2010-2015 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (file_extract_zlib.c). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#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, +}; diff --git a/libretro-common/include/file/file_extract.h b/libretro-common/include/file/file_extract.h index 9fdeb8c84d..18c2b28003 100644 --- a/libretro-common/include/file/file_extract.h +++ b/libretro-common/include/file/file_extract.h @@ -55,6 +55,19 @@ typedef struct zlib_transfer const struct zlib_file_backend *backend; } zlib_transfer_t; +/* File backends. Can be fleshed out later, but keep it simple for now. + * The file is mapped to memory directly (via mmap() or just + * plain retro_read_file()). + */ + +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. */ +}; + /* Returns true when parsing should continue. False to stop. */ typedef int (*zlib_file_cb)(const char *name, const char *valid_exts, const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size, @@ -173,5 +186,7 @@ uint64_t zlib_stream_get_total_out(void *data); void zlib_stream_decrement_total_out(void *data, unsigned subtraction); +const struct zlib_file_backend zlib_backend; + #endif