forked from TheOfficialFloW/VitaShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.c
520 lines (394 loc) · 13.2 KB
/
archive.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
VitaShell
Copyright (C) 2015-2018, TheFloW
This program 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 Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "archive.h"
#include "file.h"
#include "utils.h"
#include "elf.h"
static char archive_file[MAX_PATH_LENGTH];
static int archive_path_start = 0;
struct archive *archive_fd = NULL;
static FileList archive_list;
int checkForUnsafeImports(void *buffer);
char *uncompressBuffer(const Elf32_Ehdr *ehdr, const Elf32_Phdr *phdr, const segment_info *segment,
const char *buffer);
int archiveCheckFilesForUnsafeFself() {
FileListEntry *archive_entry = archive_list.head;
int i;
for (i = 0; i < archive_list.length; i++) {
char path[MAX_PATH_LENGTH];
snprintf(path, MAX_PATH_LENGTH - 1, "%s/%s", archive_file, archive_entry->name);
// Open
SceUID fd = archiveFileOpen(path, SCE_O_RDONLY, 0);
if (fd >= 0) {
uint32_t magic = 0;
archiveFileRead(fd, &magic, sizeof(uint32_t));
// SCE magic
if (magic == 0x00454353) {
char sce_header[0x84];
archiveFileRead(fd, sce_header, sizeof(sce_header));
uint64_t elf1_offset = *(uint64_t *)(sce_header + 0x3C);
uint64_t phdr_offset = *(uint64_t *)(sce_header + 0x44);
uint64_t section_info_offset = *(uint64_t *)(sce_header + 0x54);
// jump to elf1
// Until here we have read 0x88 bytes
int i;
for (i = 0; i < elf1_offset - 0x88; i += sizeof(uint32_t)) {
uint32_t dummy = 0;
archiveFileRead(fd, &dummy, sizeof(uint32_t));
}
// Check imports
char *buffer = malloc(archive_entry->size);
if (buffer) {
archiveFileRead(fd, buffer, archive_entry->size);
Elf32_Ehdr *elf1 = (Elf32_Ehdr*)buffer;
Elf32_Phdr *phdr = (Elf32_Phdr*)(buffer + phdr_offset - elf1_offset);
segment_info *info = (segment_info*)(buffer + section_info_offset - elf1_offset);
// segment is elf2 section
char *segment = buffer + info->offset - elf1_offset;
// zlib compress magic
char *uncompressed_buffer = NULL;
if (segment[0] == 0x78) {
// uncompressedBuffer will return elf2 section
uncompressed_buffer = uncompressBuffer(elf1, phdr, info, segment);
if (uncompressed_buffer) {
segment = uncompressed_buffer;
}
}
int unsafe = checkForUnsafeImports(segment);
if (uncompressed_buffer)
free(uncompressed_buffer);
free(buffer);
if (unsafe) {
archiveFileClose(fd);
return unsafe;
}
}
// Check authid flag
uint64_t authid = *(uint64_t *)(sce_header + 0x7C);
if (authid != 0x2F00000000000002) {
archiveFileClose(fd);
return 1; // Unsafe
}
}
archiveFileClose(fd);
}
// Next
archive_entry = archive_entry->next;
}
return 0; // Safe
}
int fileListGetArchiveEntries(FileList *list, const char *path, int sort) {
int res;
if (!list)
return -1;
FileListEntry *entry = malloc(sizeof(FileListEntry));
strcpy(entry->name, DIR_UP);
entry->name_length = strlen(entry->name);
entry->is_folder = 1;
entry->type = FILE_TYPE_UNKNOWN;
fileListAddEntry(list, entry, sort);
const char *archive_path = path + archive_path_start;
int name_length = strlen(archive_path);
FileListEntry *archive_entry = archive_list.head;
int i;
for (i = 0; i < archive_list.length; i++) {
if (archive_entry->name_length >= name_length &&
strncasecmp(archive_entry->name, archive_path, name_length) == 0) { // Needs a / at end
char *p = strchr(archive_entry->name + name_length, '/'); // it's a sub-directory if it has got a slash
if (p)
*p = '\0';
char name[MAX_PATH_LENGTH];
strcpy(name, archive_entry->name + name_length);
if (p)
addEndSlash(name);
if (strlen(name) > 0 && !fileListFindEntry(list, name)) {
FileListEntry *entry = malloc(sizeof(FileListEntry));
strcpy(entry->name, archive_entry->name + name_length);
if (p || archive_entry->is_folder) {
addEndSlash(entry->name);
entry->is_folder = 1;
entry->type = FILE_TYPE_UNKNOWN;
list->folders++;
} else {
entry->is_folder = 0;
entry->type = getFileType(entry->name);
list->files++;
}
entry->name_length = strlen(entry->name);
entry->size = archive_entry->size;
entry->size2 = archive_entry->size2;
memcpy(&entry->ctime, &archive_entry->ctime, sizeof(SceDateTime));
memcpy(&entry->mtime, &archive_entry->mtime, sizeof(SceDateTime));
memcpy(&entry->atime, &archive_entry->atime, sizeof(SceDateTime));
fileListAddEntry(list, entry, sort);
}
if (p)
*p = '/';
}
// Next
archive_entry = archive_entry->next;
}
return 0;
}
int getArchivePathInfo(const char *path, uint64_t *size, uint32_t *folders, uint32_t *files) {
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
if (archiveFileGetstat(path, &stat) < 0) {
FileList list;
memset(&list, 0, sizeof(FileList));
fileListGetArchiveEntries(&list, path, SORT_NONE);
FileListEntry *entry = list.head->next; // Ignore ..
int i;
for (i = 0; i < list.length - 1; i++) {
char *new_path = malloc(strlen(path) + strlen(entry->name) + 2);
snprintf(new_path, MAX_PATH_LENGTH - 1, "%s%s", path, entry->name);
getArchivePathInfo(new_path, size, folders, files);
free(new_path);
entry = entry->next;
}
if (folders)
(*folders)++;
fileListEmpty(&list);
} else {
if (size)
(*size) += stat.st_size;
if (files)
(*files)++;
}
return 0;
}
int extractArchivePath(const char *src, const char *dst, FileProcessParam *param) {
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
if (archiveFileGetstat(src, &stat) < 0) {
FileList list;
memset(&list, 0, sizeof(FileList));
fileListGetArchiveEntries(&list, src, SORT_NONE);
int ret = sceIoMkdir(dst, 0777);
if (ret < 0 && ret != SCE_ERROR_ERRNO_EEXIST) {
fileListEmpty(&list);
return ret;
}
if (param) {
if (param->value)
(*param->value) += DIRECTORY_SIZE;
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (param->cancelHandler && param->cancelHandler()) {
fileListEmpty(&list);
return 0;
}
}
FileListEntry *entry = list.head->next; // Ignore ..
int i;
for (i = 0; i < list.length - 1; i++) {
char *src_path = malloc(strlen(src) + strlen(entry->name) + 2);
snprintf(src_path, MAX_PATH_LENGTH - 1, "%s%s", src, entry->name);
char *dst_path = malloc(strlen(dst) + strlen(entry->name) + 2);
snprintf(dst_path, MAX_PATH_LENGTH - 1, "%s%s", dst, entry->name);
int ret = extractArchivePath(src_path, dst_path, param);
free(dst_path);
free(src_path);
if (ret <= 0) {
fileListEmpty(&list);
return ret;
}
entry = entry->next;
}
fileListEmpty(&list);
} else {
SceUID fdsrc = archiveFileOpen(src, SCE_O_RDONLY, 0);
if (fdsrc < 0)
return fdsrc;
SceUID fddst = sceIoOpen(dst, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if (fddst < 0) {
archiveFileClose(fdsrc);
return fddst;
}
void *buf = malloc(TRANSFER_SIZE);
uint64_t seek = 0;
while (1) {
int read = archiveFileRead(fdsrc, buf, TRANSFER_SIZE);
if (read < 0) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
return read;
}
if (read == 0)
break;
int written = sceIoWrite(fddst, buf, read);
if (written < 0) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
return written;
}
seek += written;
if (param) {
if (param->value)
(*param->value) += read;
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (param->cancelHandler && param->cancelHandler()) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
return 0;
}
}
}
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
}
return 1;
}
int archiveFileGetstat(const char *file, SceIoStat *stat) {
const char *archive_path = file+archive_path_start;
int name_length = strlen(archive_path);
// Is directory
if (archive_path[name_length - 1] == '/')
return -1;
FileListEntry *archive_entry = archive_list.head;
int i;
for (i = 0; i < archive_list.length; i++) {
if (archive_entry->name_length == name_length && strcasecmp(archive_entry->name, archive_path) == 0) {
if (stat) {
// stat->st_mode =
// stat->st_attr =
stat->st_size = archive_entry->size;
memcpy(&stat->st_ctime, &archive_entry->ctime, sizeof(SceDateTime));
memcpy(&stat->st_mtime, &archive_entry->mtime, sizeof(SceDateTime));
memcpy(&stat->st_atime, &archive_entry->atime, sizeof(SceDateTime));
}
return 0;
}
// Next
archive_entry = archive_entry->next;
}
return -1;
}
void waitpid() {}
void __archive_create_child() {}
void __archive_check_child() {}
int archiveFileOpen(const char *file, int flags, SceMode mode) {
int res;
const char *archive_path = file + archive_path_start;
// A file is already open
if (archive_fd)
return -1;
// Initialize
setlocale(LC_ALL, "");
archive_fd = archive_read_new();
archive_read_support_filter_all(archive_fd);
archive_read_support_format_all(archive_fd);
// Open archive file
res = archive_read_open_filename(archive_fd, archive_file, TRANSFER_SIZE);
if (res)
return -1;
// Go through all files
while (1) {
struct archive_entry *archive_entry;
res = archive_read_next_header(archive_fd, &archive_entry);
if (res == ARCHIVE_EOF)
break;
if (res != ARCHIVE_OK) {
archive_read_free(archive_fd);
return -1;
}
const char *name = archive_entry_pathname(archive_entry);
if (strcmp(name, archive_path) == 0) {
return ARCHIVE_FD;
}
}
archive_read_free(archive_fd);
archive_fd = NULL;
return -1;
}
int archiveFileRead(SceUID fd, void *data, SceSize size) {
if (!archive_fd || fd != ARCHIVE_FD)
return -1;
return archive_read_data(archive_fd, data, size);
}
int archiveFileClose(SceUID fd) {
if (!archive_fd || fd != ARCHIVE_FD)
return -1;
archive_read_free(archive_fd);
archive_fd = NULL;
return 0;
}
int ReadArchiveFile(const char *file, void *buf, int size) {
SceUID fd = archiveFileOpen(file, SCE_O_RDONLY, 0);
if (fd < 0)
return fd;
int read = archiveFileRead(fd, buf, size);
archiveFileClose(fd);
return read;
}
int archiveClose() {
fileListEmpty(&archive_list);
return 0;
}
int archiveOpen(const char *file) {
int res;
// Start position of the archive path
archive_path_start = strlen(file) + 1;
strcpy(archive_file, file);
// Clear archive list
memset(&archive_list, 0, sizeof(FileList));
// Initialize
setlocale(LC_ALL, "");
struct archive *archive = archive_read_new();
archive_read_support_filter_all(archive);
archive_read_support_format_all(archive);
// Open archive file
res = archive_read_open_filename(archive, file, TRANSFER_SIZE);
if (res)
return -1;
// Go through all files
while (1) {
struct archive_entry *archive_entry;
res = archive_read_next_header(archive, &archive_entry);
if (res == ARCHIVE_EOF)
break;
if (res != ARCHIVE_OK) {
archive_read_free(archive);
return -1;
}
const char *name = archive_entry_pathname(archive_entry);
const struct stat *stat = archive_entry_stat(archive_entry);
FileListEntry *entry = malloc(sizeof(FileListEntry));
// File info
strcpy(entry->name, name);
entry->name_length = strlen(name);
entry->is_folder = stat->st_mode & S_IFDIR;
entry->size = stat->st_size; // uncompressed size
entry->size2 = 0; // TODO: uncompressed size
// Time
SceDateTime time;
sceRtcSetTime_t(&time, stat->st_ctime);
convertLocalTimeToUtc(&entry->ctime, &time);
sceRtcSetTime_t(&time, stat->st_mtime);
convertLocalTimeToUtc(&entry->mtime, &time);
sceRtcSetTime_t(&time, stat->st_atime);
convertLocalTimeToUtc(&entry->atime, &time);
// Add entry
fileListAddEntry(&archive_list, entry, SORT_BY_NAME);
}
archive_read_free(archive);
return 0;
}