forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_extract.c
440 lines (366 loc) · 10.5 KB
/
file_extract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* 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"
#include "file_ops.h"
#include <file/file_path.h>
#include <compat/strl.h>
#include <retro_miscellaneous.h>
#include "retroarch_logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "hash.h"
/* 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()).
*/
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. */
};
#ifdef HAVE_MMAP
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
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->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;
return (const uint8_t*)data->data;
}
static size_t zlib_file_size(void *handle)
{
zlib_file_data_t *data = (zlib_file_data_t*)handle;
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);
if (data->fd < 0)
{
RARCH_ERR("Failed to open archive: %s (%s).\n",
path, strerror(errno));
goto error;
}
struct stat fds;
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;
return (const uint8_t*)data->data;
}
static size_t zlib_file_size(void *handle)
{
zlib_file_data_t *data = (zlib_file_data_t*)handle;
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;
ssize_t ret = read_file(path, &data->data);
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)
{
return &zlib_backend;
}
/* Modified from nall::unzip (higan). */
#undef GOTO_END_ERROR
#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;
uint32_t val = 0;
size *= 8;
for (i = 0; i < size; i += 8)
val |= *data++ << i;
return val;
}
bool zlib_inflate_data_to_file(const char *path, const uint8_t *cdata,
uint32_t csize, uint32_t size, uint32_t checksum)
{
bool ret = true;
uint8_t *out_data = (uint8_t*)malloc(size);
if (!out_data)
return false;
uint32_t real_checksum = 0;
z_stream stream = {0};
if (inflateInit2(&stream, -MAX_WBITS) != Z_OK)
GOTO_END_ERROR();
stream.next_in = (uint8_t*)cdata;
stream.avail_in = csize;
stream.next_out = out_data;
stream.avail_out = size;
if (inflate(&stream, Z_FINISH) != Z_STREAM_END)
{
inflateEnd(&stream);
GOTO_END_ERROR();
}
inflateEnd(&stream);
real_checksum = crc32_calculate(out_data, size);
if (real_checksum != checksum)
RARCH_WARN("File CRC differs from ZIP CRC. File: 0x%x, ZIP: 0x%x.\n",
(unsigned)real_checksum, (unsigned)checksum);
if (!write_file(path, out_data, size))
GOTO_END_ERROR();
end:
free(out_data);
return ret;
}
bool zlib_parse_file(const char *file, zlib_file_cb file_cb, void *userdata)
{
const uint8_t *footer = NULL;
const uint8_t *directory = NULL;
bool ret = true;
const uint8_t *data = NULL;
const struct zlib_file_backend *backend = zlib_get_default_file_backend();
if (!backend)
return false;
ssize_t zip_size = 0;
void *handle = backend->open(file);
if (!handle)
GOTO_END_ERROR();
zip_size = backend->size(handle);
if (zip_size < 22)
GOTO_END_ERROR();
data = backend->data(handle);
footer = data + zip_size - 22;
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);
for (;;)
{
uint32_t signature = read_le(directory + 0, 4);
if (signature != 0x02014b50)
break;
unsigned cmode = read_le(directory + 10, 2);
uint32_t checksum = read_le(directory + 16, 4);
uint32_t csize = read_le(directory + 20, 4);
uint32_t size = read_le(directory + 24, 4);
unsigned namelength = read_le(directory + 28, 2);
unsigned extralength = read_le(directory + 30, 2);
unsigned commentlength = read_le(directory + 32, 2);
char filename[PATH_MAX] = {0};
if (namelength >= PATH_MAX)
GOTO_END_ERROR();
memcpy(filename, directory + 46, namelength);
uint32_t offset = read_le(directory + 42, 4);
unsigned offsetNL = read_le(data + offset + 26, 2);
unsigned offsetEL = read_le(data + offset + 28, 2);
const uint8_t *cdata = data + offset + 30 + offsetNL + offsetEL;
#if 0
RARCH_LOG("OFFSET: %u, CSIZE: %u, SIZE: %u.\n", offset + 30 +
offsetNL + offsetEL, csize, size);
#endif
if (!file_cb(filename, cdata, cmode, csize, size, checksum, userdata))
break;
directory += 46 + namelength + extralength + commentlength;
}
end:
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;
bool found_content;
};
static bool zip_extract_cb(const char *name, const uint8_t *cdata,
unsigned cmode, uint32_t csize, uint32_t size,
uint32_t checksum, void *userdata)
{
struct zip_extract_userdata *data = (struct zip_extract_userdata*)userdata;
/* Extract first content that matches our list. */
const char *ext = path_get_extension(name);
if (ext && string_list_find_elem(data->ext, ext))
{
char new_path[PATH_MAX];
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)
{
/* Uncompressed. */
case 0:
data->found_content = write_file(new_path, cdata, size);
return false;
/* Deflate. */
case 8:
if (zlib_inflate_data_to_file(new_path, cdata, csize, size, checksum))
{
strlcpy(data->zip_path, new_path, data->zip_path_size);
data->found_content = true;
return false;
}
return false;
default:
return false;
}
}
return true;
}
bool zlib_extract_first_content_file(char *zip_path, size_t zip_path_size,
const char *valid_exts, const char *extraction_directory)
{
bool ret;
struct zip_extract_userdata userdata = {0};
struct string_list *list;
if (!valid_exts)
{
RARCH_ERR("Libretro implementation does not have any valid extensions. Cannot unzip without knowing this.\n");
return false;
}
ret = true;
list = string_split(valid_exts, "|");
if (!list)
GOTO_END_ERROR();
userdata.zip_path = zip_path;
userdata.zip_path_size = zip_path_size;
userdata.extraction_directory = extraction_directory;
userdata.ext = list;
if (!zlib_parse_file(zip_path, zip_extract_cb, &userdata))
{
RARCH_ERR("Parsing ZIP failed.\n");
GOTO_END_ERROR();
}
if (!userdata.found_content)
{
RARCH_ERR("Didn't find any content that matched valid extensions for libretro implementation.\n");
GOTO_END_ERROR();
}
end:
if (list)
string_list_free(list);
return ret;
}
static bool zlib_get_file_list_cb(const char *path, const uint8_t *cdata,
unsigned cmode, uint32_t csize, uint32_t size, uint32_t checksum,
void *userdata)
{
(void)cdata;
(void)cmode;
(void)csize;
(void)size;
(void)checksum;
struct string_list *list = (struct string_list*)userdata;
union string_list_elem_attr attr;
memset(&attr, 0, sizeof(attr));
return string_list_append(list, path, attr);
}
struct string_list *zlib_get_file_list(const char *path)
{
struct string_list *list = string_list_new();
if (!list)
return NULL;
if (!zlib_parse_file(path, zlib_get_file_list_cb, list))
{
RARCH_ERR("Parsing ZIP failed.\n");
string_list_free(list);
return NULL;
}
return list;
}