1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (archive_file.h).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef LIBRETRO_SDK_ARCHIVE_FILE_H__
24 #define LIBRETRO_SDK_ARCHIVE_FILE_H__
36 #include <retro_miscellaneous.h>
38 #include <retro_common_api.h>
40 #if defined(RARCH_INTERNAL) && defined(HAVE_CONFIG_H)
41 #include "../../../config.h" /* for HAVE_MMAP */
46 enum file_archive_transfer_type
48 ARCHIVE_TRANSFER_NONE = 0,
49 ARCHIVE_TRANSFER_INIT,
50 ARCHIVE_TRANSFER_ITERATE,
51 ARCHIVE_TRANSFER_DEINIT,
52 ARCHIVE_TRANSFER_DEINIT_ERROR
55 typedef struct file_archive_handle
58 uint32_t real_checksum;
59 } file_archive_file_handle_t;
61 typedef struct file_archive_transfer
65 struct RFILE *archive_file;
66 const struct file_archive_file_backend *backend;
68 uint8_t *archive_mmap_data;
72 unsigned step_current;
73 enum file_archive_transfer_type type;
74 } file_archive_transfer_t;
78 file_archive_transfer_t archive; /* int64_t alignment */
85 struct archive_extract_userdata *userdata;
88 struct archive_extract_userdata
90 /* These are set or read by the archive processing */
91 char *first_extracted_file_path;
92 const char *extraction_directory;
93 struct string_list *ext;
94 struct string_list *list;
95 file_archive_transfer_t *transfer;
96 /* Not used by the processing, free to use outside or in iterate callback */
97 decompress_state_t *dec;
100 char archive_path[PATH_MAX_LENGTH];
101 char current_file_path[PATH_MAX_LENGTH];
106 /* Returns true when parsing should continue. False to stop. */
107 typedef int (*file_archive_file_cb)(const char *name, const char *valid_exts,
108 const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
109 uint32_t crc32, struct archive_extract_userdata *userdata);
111 struct file_archive_file_backend
113 int (*archive_parse_file_init)(
114 file_archive_transfer_t *state,
116 int (*archive_parse_file_iterate_step)(
118 const char *valid_exts,
119 struct archive_extract_userdata *userdata,
120 file_archive_file_cb file_cb);
121 void (*archive_parse_file_free)(
124 bool (*stream_decompress_data_to_file_init)(
125 void *context, file_archive_file_handle_t *handle,
126 const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size);
127 int (*stream_decompress_data_to_file_iterate)(
129 file_archive_file_handle_t *handle);
131 uint32_t (*stream_crc_calculate)(uint32_t, const uint8_t *, size_t);
132 int64_t (*compressed_file_read)(const char *path, const char *needle, void **buf,
133 const char *optional_outfile);
137 int file_archive_parse_file_iterate(
138 file_archive_transfer_t *state,
141 const char *valid_exts,
142 file_archive_file_cb file_cb,
143 struct archive_extract_userdata *userdata);
145 void file_archive_parse_file_iterate_stop(file_archive_transfer_t *state);
147 int file_archive_parse_file_progress(file_archive_transfer_t *state);
150 * file_archive_extract_file:
151 * @archive_path : filename path to ZIP archive.
152 * @valid_exts : valid extensions for a file.
153 * @extraction_directory : the directory to extract the temporary
156 * Extract file from archive. If no file inside the archive is
157 * specified, the first file found will be used.
159 * Returns : true (1) on success, otherwise false (0).
161 bool file_archive_extract_file(const char *archive_path,
162 const char *valid_exts, const char *extraction_dir,
163 char *out_path, size_t len);
165 /* Warning: 'list' must zero initialised before
166 * calling this function, otherwise memory leaks/
167 * undefined behaviour will occur */
168 bool file_archive_get_file_list_noalloc(struct string_list *list,
170 const char *valid_exts);
173 * file_archive_get_file_list:
174 * @path : filename path of archive
175 * @valid_exts : Valid extensions of archive to be parsed.
176 * If NULL, allow all.
178 * Returns: string listing of files from archive on success, otherwise NULL.
180 struct string_list* file_archive_get_file_list(const char *path, const char *valid_exts);
182 bool file_archive_perform_mode(const char *name, const char *valid_exts,
183 const uint8_t *cdata, unsigned cmode, uint32_t csize, uint32_t size,
184 uint32_t crc32, struct archive_extract_userdata *userdata);
186 int file_archive_compressed_read(
187 const char* path, void **buf,
188 const char* optional_filename, int64_t *length);
190 const struct file_archive_file_backend* file_archive_get_zlib_file_backend(void);
191 const struct file_archive_file_backend* file_archive_get_7z_file_backend(void);
193 const struct file_archive_file_backend* file_archive_get_file_backend(const char *path);
196 * file_archive_get_file_crc32:
197 * @path : filename path of archive
199 * Returns: CRC32 of the specified file in the archive, otherwise 0.
200 * If no path within the archive is specified, the first
201 * file found inside is used.
203 uint32_t file_archive_get_file_crc32(const char *path);
205 extern const struct file_archive_file_backend zlib_backend;
206 extern const struct file_archive_file_backend sevenzip_backend;