RetroArch/file_extract.c

574 lines
14 KiB
C
Raw Normal View History

2013-01-21 22:51:56 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2013-01-21 22:51:56 +00:00
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "file_extract.h"
2014-10-21 21:53:09 +00:00
#include "file_ops.h"
2014-10-21 22:23:06 +00:00
#include <file/file_path.h>
2014-10-21 05:58:58 +00:00
#include <compat/strl.h>
#include <retro_miscellaneous.h>
#include "general.h"
#include "retroarch_logger.h"
2013-01-21 22:51:56 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-01-21 22:51:56 +00:00
#include <zlib.h>
#include "hash.h"
2014-09-07 03:47:18 +00:00
/* 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 read_file()).
*/
2013-10-14 16:10:17 +00:00
struct zlib_file_backend
{
void *(*open)(const char *path);
const uint8_t *(*data)(void *handle);
size_t (*size)(void *handle);
2014-09-07 03:47:18 +00:00
void (*free)(void *handle); /* Closes, unmaps and frees. */
2013-10-14 16:10:17 +00:00
};
#ifdef HAVE_MMAP
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
2013-10-14 16:10:17 +00:00
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;
2015-02-02 18:20:13 +00:00
if (!data)
return;
2013-10-14 16:10:17 +00:00
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;
2015-01-09 14:46:22 +00:00
if (!data)
return NULL;
2013-10-14 16:10:17 +00:00
return (const uint8_t*)data->data;
}
static size_t zlib_file_size(void *handle)
{
zlib_file_data_t *data = (zlib_file_data_t*)handle;
2015-01-09 14:46:22 +00:00
if (!data)
return 0;
2013-10-14 16:10:17 +00:00
return data->size;
}
static void *zlib_file_open(const char *path)
{
2015-01-09 14:46:22 +00:00
struct stat fds;
2013-10-14 16:10:17 +00:00
zlib_file_data_t *data = (zlib_file_data_t*)calloc(1, sizeof(*data));
2015-01-09 14:46:22 +00:00
2013-10-14 16:10:17 +00:00
if (!data)
return NULL;
2015-01-09 14:46:22 +00:00
2013-10-14 16:10:17 +00:00
data->fd = open(path, O_RDONLY);
2015-01-09 14:46:22 +00:00
2013-10-14 16:10:17 +00:00
if (data->fd < 0)
{
RARCH_ERR("Failed to open archive: %s (%s).\n",
path, strerror(errno));
goto error;
}
if (fstat(data->fd, &fds) < 0)
goto error;
data->size = fds.st_size;
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;
RARCH_ERR("Failed to mmap() file: %s (%s).\n", path, strerror(errno));
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;
2013-10-14 16:10:17 +00:00
return (const uint8_t*)data->data;
}
static size_t zlib_file_size(void *handle)
{
zlib_file_data_t *data = (zlib_file_data_t*)handle;
2015-01-09 14:46:22 +00:00
if (!data)
2015-01-09 15:57:04 +00:00
return 0;
2013-10-14 16:10:17 +00:00
return data->size;
}
static void *zlib_file_open(const char *path)
{
ssize_t ret;
2013-10-14 16:10:17 +00:00
zlib_file_data_t *data = (zlib_file_data_t*)calloc(1, sizeof(*data));
2015-02-02 18:20:13 +00:00
2013-10-14 16:10:17 +00:00
if (!data)
return NULL;
ret = read_file(path, &data->data);
2015-01-23 08:08:58 +00:00
2013-10-14 16:10:17 +00:00
if (ret < 0)
{
RARCH_ERR("Failed to open archive: %s.\n",
path);
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)
2013-10-14 16:10:17 +00:00
{
return &zlib_backend;
}
2014-09-07 03:47:18 +00:00
/* Modified from nall::unzip (higan). */
2013-01-21 22:51:56 +00:00
2013-01-24 06:32:15 +00:00
#undef GOTO_END_ERROR
2013-01-21 22:51:56 +00:00
#define GOTO_END_ERROR() do { \
RARCH_ERR("ZIP extraction failed at line: %d.\n", __LINE__); \
ret = false; \
goto end; \
} while(0)
static uint32_t read_le(const uint8_t *data, unsigned size)
{
unsigned i;
2013-01-21 22:51:56 +00:00
uint32_t val = 0;
2015-01-09 14:46:22 +00:00
2013-01-21 22:51:56 +00:00
size *= 8;
for (i = 0; i < size; i += 8)
2013-01-21 22:51:56 +00:00
val |= *data++ << i;
return val;
}
2015-01-09 14:46:22 +00:00
/**
* zlib_inflate_data_to_file:
* @path : filename path of archive.
* @valid_exts : Valid extensions of archive to be parsed.
* If NULL, allow all.
2015-01-09 14:46:22 +00:00
* @cdata : input data.
* @csize : size of input data.
* @size : output file size
* @checksum : CRC32 checksum from input data.
*
* Decompress data to file.
*
* Returns: true (1) on success, otherwise false (0).
**/
bool zlib_inflate_data_to_file(const char *path, const char *valid_exts,
const uint8_t *cdata, uint32_t csize, uint32_t size, uint32_t checksum)
2013-01-21 22:51:56 +00:00
{
bool ret = true;
uint32_t real_checksum = 0;
z_stream stream = {0};
2013-01-21 22:51:56 +00:00
uint8_t *out_data = (uint8_t*)malloc(size);
2013-01-21 22:51:56 +00:00
if (!out_data)
return false;
if (inflateInit2(&stream, -MAX_WBITS) != Z_OK)
GOTO_END_ERROR();
2015-01-23 08:08:58 +00:00
stream.next_in = (uint8_t*)cdata;
stream.avail_in = csize;
stream.next_out = out_data;
2013-01-21 22:51:56 +00:00
stream.avail_out = size;
if (inflate(&stream, Z_FINISH) != Z_STREAM_END)
{
inflateEnd(&stream);
GOTO_END_ERROR();
}
inflateEnd(&stream);
2014-10-20 22:19:00 +00:00
real_checksum = crc32_calculate(out_data, size);
if (real_checksum != checksum)
2013-01-21 22:51:56 +00:00
RARCH_WARN("File CRC differs from ZIP CRC. File: 0x%x, ZIP: 0x%x.\n",
2014-10-20 22:19:00 +00:00
(unsigned)real_checksum, (unsigned)checksum);
2013-01-21 22:51:56 +00:00
if (!write_file(path, out_data, size))
GOTO_END_ERROR();
end:
free(out_data);
return ret;
}
2015-01-09 14:46:22 +00:00
/**
* zlib_parse_file:
* @file : filename path of archive
* @valid_exts : Valid extensions of archive to be parsed.
* If NULL, allow all.
2015-01-09 14:46:22 +00:00
* @file_cb : file_cb function pointer
* @userdata : userdata to pass to file_cb function pointer.
*
* Low-level file parsing. Enumerates over all files and calls
* file_cb with userdata.
*
* Returns: true (1) on success, otherwise false (0).
**/
bool zlib_parse_file(const char *file, const char *valid_exts,
zlib_file_cb file_cb, void *userdata)
2013-01-21 22:51:56 +00:00
{
2015-01-09 14:46:22 +00:00
void *handle;
const uint8_t *footer = NULL;
const uint8_t *directory = NULL;
const uint8_t *data = NULL;
ssize_t zip_size = 0;
2013-01-21 22:51:56 +00:00
bool ret = true;
2013-10-14 16:10:17 +00:00
const struct zlib_file_backend *backend = zlib_get_default_file_backend();
2013-10-14 16:10:17 +00:00
if (!backend)
2014-06-17 15:08:53 +00:00
return false;
2013-10-14 16:10:17 +00:00
(void)valid_exts;
handle = backend->open(file);
2013-10-14 16:10:17 +00:00
if (!handle)
GOTO_END_ERROR();
zip_size = backend->size(handle);
2013-01-21 22:51:56 +00:00
if (zip_size < 22)
GOTO_END_ERROR();
2013-10-14 16:10:17 +00:00
data = backend->data(handle);
footer = data + zip_size - 22;
2013-01-21 22:51:56 +00:00
for (;; footer--)
{
if (footer <= data + 22)
GOTO_END_ERROR();
if (read_le(footer, 4) == 0x06054b50)
{
unsigned comment_len = read_le(footer + 20, 2);
if (footer + 22 + comment_len == data + zip_size)
break;
}
}
directory = data + read_le(footer + 16, 4);
2013-01-21 22:51:56 +00:00
for (;;)
{
uint32_t checksum, csize, size, offset;
unsigned cmode, namelength, extralength, commentlength,
offsetNL, offsetEL;
char filename[PATH_MAX_LENGTH] = {0};
const uint8_t *cdata = NULL;
2013-01-21 22:51:56 +00:00
uint32_t signature = read_le(directory + 0, 4);
2013-01-21 22:51:56 +00:00
if (signature != 0x02014b50)
break;
2015-01-23 08:08:58 +00:00
cmode = read_le(directory + 10, 2);
checksum = read_le(directory + 16, 4);
csize = read_le(directory + 20, 4);
size = read_le(directory + 24, 4);
2013-01-21 22:51:56 +00:00
namelength = read_le(directory + 28, 2);
extralength = read_le(directory + 30, 2);
commentlength = read_le(directory + 32, 2);
2013-01-21 22:51:56 +00:00
if (namelength >= PATH_MAX_LENGTH)
2013-01-21 22:51:56 +00:00
GOTO_END_ERROR();
memcpy(filename, directory + 46, namelength);
2015-01-23 08:08:58 +00:00
offset = read_le(directory + 42, 4);
offsetNL = read_le(data + offset + 26, 2);
offsetEL = read_le(data + offset + 28, 2);
2013-01-21 22:51:56 +00:00
cdata = data + offset + 30 + offsetNL + offsetEL;
2013-01-21 22:51:56 +00:00
2014-09-07 03:47:18 +00:00
#if 0
RARCH_LOG("OFFSET: %u, CSIZE: %u, SIZE: %u.\n", offset + 30 +
offsetNL + offsetEL, csize, size);
#endif
2013-01-21 22:51:56 +00:00
if (!file_cb(filename, valid_exts, cdata, cmode,
csize, size, checksum, userdata))
break;
directory += 46 + namelength + extralength + commentlength;
}
end:
2013-10-14 16:10:17 +00:00
if (handle)
backend->free(handle);
return ret;
}
struct zip_extract_userdata
{
char *zip_path;
const char *extraction_directory;
size_t zip_path_size;
struct string_list *ext;
2014-08-13 06:52:13 +00:00
bool found_content;
};
static bool zip_extract_cb(const char *name, const char *valid_exts,
const uint8_t *cdata,
2014-09-07 03:47:18 +00:00
unsigned cmode, uint32_t csize, uint32_t size,
2014-10-20 22:19:00 +00:00
uint32_t checksum, void *userdata)
{
struct zip_extract_userdata *data = (struct zip_extract_userdata*)userdata;
2014-09-07 03:47:18 +00:00
/* Extract first content that matches our list. */
const char *ext = path_get_extension(name);
2015-01-09 14:46:22 +00:00
if (ext && string_list_find_elem(data->ext, ext))
{
char new_path[PATH_MAX_LENGTH];
if (data->extraction_directory)
fill_pathname_join(new_path, data->extraction_directory,
path_basename(name), sizeof(new_path));
else
fill_pathname_resolve_relative(new_path, data->zip_path,
path_basename(name), sizeof(new_path));
switch (cmode)
2013-01-21 22:51:56 +00:00
{
2014-09-07 03:47:18 +00:00
/* Uncompressed. */
case 0:
2014-08-13 06:52:13 +00:00
data->found_content = write_file(new_path, cdata, size);
return false;
2014-09-07 03:47:18 +00:00
/* Deflate. */
case 8:
if (zlib_inflate_data_to_file(new_path, valid_exts,
cdata, csize, size, checksum))
{
strlcpy(data->zip_path, new_path, data->zip_path_size);
2014-08-13 06:52:13 +00:00
data->found_content = true;
return false;
}
2014-08-27 00:06:39 +00:00
return false;
default:
return false;
2013-01-21 22:51:56 +00:00
}
}
2013-01-21 22:51:56 +00:00
return true;
}
2015-01-09 14:46:22 +00:00
/**
* zlib_extract_first_content_file:
* @zip_path : filename path to ZIP archive.
* @zip_path_size : size of ZIP archive.
* @valid_exts : valid extensions for a content file.
* @extraction_directory : the directory to extract temporary
* unzipped content to.
*
* Extract first content file from archive.
*
* Returns : true (1) on success, otherwise false (0).
**/
2014-09-07 03:47:18 +00:00
bool zlib_extract_first_content_file(char *zip_path, size_t zip_path_size,
const char *valid_exts, const char *extraction_directory)
{
2015-01-09 14:46:22 +00:00
struct string_list *list;
bool ret = true;
struct zip_extract_userdata userdata = {0};
if (!valid_exts)
{
RARCH_ERR("Libretro implementation does not have any valid extensions. Cannot unzip without knowing this.\n");
return false;
}
list = string_split(valid_exts, "|");
if (!list)
GOTO_END_ERROR();
2015-01-23 08:08:58 +00:00
userdata.zip_path = zip_path;
userdata.zip_path_size = zip_path_size;
userdata.extraction_directory = extraction_directory;
2015-01-23 08:08:58 +00:00
userdata.ext = list;
if (!zlib_parse_file(zip_path, valid_exts, zip_extract_cb, &userdata))
{
RARCH_ERR("Parsing ZIP failed.\n");
GOTO_END_ERROR();
2013-01-21 22:51:56 +00:00
}
2014-08-13 06:52:13 +00:00
if (!userdata.found_content)
{
2014-07-28 18:08:37 +00:00
RARCH_ERR("Didn't find any content that matched valid extensions for libretro implementation.\n");
GOTO_END_ERROR();
}
2013-01-21 22:51:56 +00:00
end:
if (list)
string_list_free(list);
2013-01-21 22:51:56 +00:00
return ret;
}
static bool zlib_get_file_list_cb(const char *path, const char *valid_exts,
const uint8_t *cdata,
2014-10-20 22:19:00 +00:00
unsigned cmode, uint32_t csize, uint32_t size, uint32_t checksum,
2014-09-07 03:47:18 +00:00
void *userdata)
{
union string_list_elem_attr attr;
struct string_list *ext_list = NULL;
const char *file_ext = NULL;
struct string_list *list = (struct string_list*)userdata;
(void)cdata;
(void)cmode;
(void)csize;
(void)size;
2014-10-20 22:19:00 +00:00
(void)checksum;
(void)valid_exts;
(void)file_ext;
(void)ext_list;
memset(&attr, 0, sizeof(attr));
if (valid_exts)
ext_list = string_split(valid_exts, "|");
if (ext_list)
{
char last_char = ' ';
/* Checks if this entry is a directory or a file. */
last_char = path[strlen(path)-1];
if (last_char == '/' || last_char == '\\' ) /* Skip if directory. */
goto error;
file_ext = path_get_extension(path);
if (!file_ext ||
!string_list_find_elem_prefix(ext_list, ".", file_ext))
goto error;
attr.i = RARCH_COMPRESSED_FILE_IN_ARCHIVE;
string_list_free(ext_list);
}
return string_list_append(list, path, attr);
error:
string_list_free(ext_list);
return false;
}
2015-01-09 14:46:22 +00:00
/**
* zlib_get_file_list:
* @path : filename path of archive
*
* Returns: string listing of files from archive on success, otherwise NULL.
**/
struct string_list *zlib_get_file_list(const char *path, const char *valid_exts)
{
struct string_list *list = string_list_new();
2015-01-09 14:46:22 +00:00
if (!list)
return NULL;
if (!zlib_parse_file(path, valid_exts,
zlib_get_file_list_cb, list))
{
RARCH_ERR("Parsing ZIP failed.\n");
string_list_free(list);
return NULL;
}
2014-08-27 00:06:39 +00:00
return list;
}
struct string_list *compressed_file_list_new(const char *path,
const char* ext)
{
#ifdef HAVE_COMPRESSION
const char* file_ext = path_get_extension(path);
#ifdef HAVE_7ZIP
if (strcasecmp(file_ext,"7z") == 0)
return compressed_7zip_file_list_new(path,ext);
#endif
#ifdef HAVE_ZLIB
if (strcasecmp(file_ext,"zip") == 0)
return zlib_get_file_list(path, ext);
#endif
#endif
return NULL;
}