forked from TheOfficialFloW/VitaShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psarc.c
534 lines (413 loc) · 15 KB
/
psarc.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
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 "browser.h"
#include "psarc.h"
#include "file.h"
#include "utils.h"
#define SCE_FIOS_FH_SIZE 80
#define SCE_FIOS_DH_SIZE 80
#define SCE_FIOS_OP_SIZE 168
#define SCE_FIOS_CHUNK_SIZE 64
#define SCE_FIOS_ALIGN_UP(val, align) (((val) + ((align) - 1)) & ~((align) - 1))
#define SCE_FIOS_STORAGE_SIZE(num, size) (((num) * (size)) + SCE_FIOS_ALIGN_UP(SCE_FIOS_ALIGN_UP((num), 8) / 8, 8))
#define SCE_FIOS_DH_STORAGE_SIZE(numDHs, pathMax) SCE_FIOS_STORAGE_SIZE(numDHs, SCE_FIOS_DH_SIZE + pathMax)
#define SCE_FIOS_FH_STORAGE_SIZE(numFHs, pathMax) SCE_FIOS_STORAGE_SIZE(numFHs, SCE_FIOS_FH_SIZE + pathMax)
#define SCE_FIOS_OP_STORAGE_SIZE(numOps, pathMax) SCE_FIOS_STORAGE_SIZE(numOps, SCE_FIOS_OP_SIZE + pathMax)
#define SCE_FIOS_CHUNK_STORAGE_SIZE(numChunks) SCE_FIOS_STORAGE_SIZE(numChunks, SCE_FIOS_CHUNK_SIZE)
#define SCE_FIOS_BUFFER_INITIALIZER { 0, 0 }
#define SCE_FIOS_PSARC_DEARCHIVER_CONTEXT_INITIALIZER { sizeof(SceFiosPsarcDearchiverContext), 0, 0, 0, {0, 0, 0} }
#define SCE_FIOS_PARAMS_INITIALIZER { 0, sizeof(SceFiosParams), 0, 0, 2, 1, 0, 0, 256 * 1024, 2, 0, 0, 0, 0, 0, SCE_FIOS_BUFFER_INITIALIZER, SCE_FIOS_BUFFER_INITIALIZER, SCE_FIOS_BUFFER_INITIALIZER, SCE_FIOS_BUFFER_INITIALIZER, NULL, NULL, NULL, { 66, 189, 66 }, { 0x40000, 0, 0x40000}, { 8 * 1024, 16 * 1024, 8 * 1024}}
typedef int32_t SceFiosFH;
typedef int32_t SceFiosDH;
typedef uint64_t SceFiosDate;
typedef int64_t SceFiosOffset;
typedef int64_t SceFiosSize;
typedef struct SceFiosPsarcDearchiverContext {
size_t sizeOfContext;
size_t workBufferSize;
void *pWorkBuffer;
intptr_t flags;
intptr_t reserved[3];
} SceFiosPsarcDearchiverContext;
typedef struct SceFiosBuffer {
void *pPtr;
size_t length;
} SceFiosBuffer;
typedef struct SceFiosParams {
uint32_t initialized : 1;
uint32_t paramsSize : 15;
uint32_t pathMax : 16;
uint32_t profiling;
uint32_t ioThreadCount;
uint32_t threadsPerScheduler;
uint32_t extraFlag1 : 1;
uint32_t extraFlags : 31;
uint32_t maxChunk;
uint8_t maxDecompressorThreadCount;
uint8_t reserved1;
uint8_t reserved2;
uint8_t reserved3;
intptr_t reserved4;
intptr_t reserved5;
SceFiosBuffer opStorage;
SceFiosBuffer fhStorage;
SceFiosBuffer dhStorage;
SceFiosBuffer chunkStorage;
void *pVprintf;
void *pMemcpy;
void *pProfileCallback;
int threadPriority[3];
int threadAffinity[3];
int threadStackSize[3];
} SceFiosParams;
typedef struct SceFiosDirEntry {
SceFiosOffset fileSize;
uint32_t statFlags;
uint16_t nameLength;
uint16_t fullPathLength;
uint16_t offsetToName;
uint16_t reserved[3];
char fullPath[1024];
} SceFiosDirEntry;
typedef struct SceFiosStat {
SceFiosOffset fileSize;
SceFiosDate accessDate;
SceFiosDate modificationDate;
SceFiosDate creationDate;
uint32_t statFlags;
uint32_t reserved;
int64_t uid;
int64_t gid;
int64_t dev;
int64_t ino;
int64_t mode;
} SceFiosStat;
int sceFiosInitialize(const SceFiosParams *params);
void sceFiosTerminate();
SceFiosSize sceFiosArchiveGetMountBufferSizeSync(const void *attr, const char *path, void *params);
int sceFiosArchiveMountSync(const void *attr, SceFiosFH *fh, const char *path, const char *mount_point, SceFiosBuffer mount_buffer, void *params);
int sceFiosArchiveUnmountSync(const void *attr, SceFiosFH fh);
int sceFiosStatSync(const void *attr, const char *path, SceFiosStat *stat);
int sceFiosFHOpenSync(const void *attr, SceFiosFH *fh, const char *path, const void *params);
SceFiosSize sceFiosFHReadSync(const void *attr, SceFiosFH fh, void *data, SceFiosSize size);
int sceFiosFHCloseSync(const void *attr, SceFiosFH fh);
int sceFiosDHOpenSync(const void *attr, SceFiosDH *dh, const char *path, SceFiosBuffer buf);
int sceFiosDHReadSync(const void *attr, SceFiosDH dh, SceFiosDirEntry *dir);
int sceFiosDHCloseSync(const void *attr, SceFiosDH dh);
SceDateTime *sceFiosDateToSceDateTime(SceFiosDate date, SceDateTime *sce_date);
int sceFiosIOFilterAdd(int index, void (* callback)(), void *context);
int sceFiosIOFilterRemove(int index);
void sceFiosIOFilterPsarcDearchiver();
int64_t g_OpStorage[SCE_FIOS_OP_STORAGE_SIZE(64, MAX_PATH_LENGTH) / sizeof(int64_t) + 1];
int64_t g_ChunkStorage[SCE_FIOS_CHUNK_STORAGE_SIZE(1024) / sizeof(int64_t) + 1];
int64_t g_FHStorage[SCE_FIOS_FH_STORAGE_SIZE(32, MAX_PATH_LENGTH) / sizeof(int64_t) + 1];
int64_t g_DHStorage[SCE_FIOS_DH_STORAGE_SIZE(32, MAX_PATH_LENGTH) / sizeof(int64_t) + 1];
SceFiosPsarcDearchiverContext g_DearchiverContext = SCE_FIOS_PSARC_DEARCHIVER_CONTEXT_INITIALIZER;
char g_DearchiverWorkBuffer[0x30000] __attribute__((aligned(64)));
int g_ArchiveIndex = 0;
SceFiosBuffer g_MountBuffer = SCE_FIOS_BUFFER_INITIALIZER;
SceFiosFH g_ArchiveFH = -1;
int psarcOpen(const char *file) {
int res;
SceFiosParams params = SCE_FIOS_PARAMS_INITIALIZER;
params.opStorage.pPtr = g_OpStorage;
params.opStorage.length = sizeof(g_OpStorage);
params.chunkStorage.pPtr = g_ChunkStorage;
params.chunkStorage.length = sizeof(g_ChunkStorage);
params.fhStorage.pPtr = g_FHStorage;
params.fhStorage.length = sizeof(g_FHStorage);
params.dhStorage.pPtr = g_DHStorage;
params.dhStorage.length = sizeof(g_DHStorage);
params.pathMax = MAX_PATH_LENGTH;
res = sceFiosInitialize(¶ms);
if (res < 0)
goto ERROR_INITIALIZE;
g_DearchiverContext.workBufferSize = sizeof(g_DearchiverWorkBuffer);
g_DearchiverContext.pWorkBuffer = g_DearchiverWorkBuffer;
res = sceFiosIOFilterAdd(g_ArchiveIndex, sceFiosIOFilterPsarcDearchiver, &g_DearchiverContext);
if (res < 0)
goto ERROR_FILTER_ADD;
SceFiosSize result = sceFiosArchiveGetMountBufferSizeSync(NULL, file, NULL);
if (result < 0) {
res = (int)result;
goto ERROR_ARCHIVE_GET_MOUNT_BUFFER_SIZE;
}
g_MountBuffer.length = (size_t)result;
g_MountBuffer.pPtr = malloc(g_MountBuffer.length);
res = sceFiosArchiveMountSync(NULL, &g_ArchiveFH, file, file, g_MountBuffer, NULL);
if (res < 0)
goto ERROR_ARCHIVE_MOUNT;
return res;
ERROR_ARCHIVE_MOUNT:
free(g_MountBuffer.pPtr);
ERROR_ARCHIVE_GET_MOUNT_BUFFER_SIZE:
sceFiosIOFilterRemove(g_ArchiveIndex);
ERROR_FILTER_ADD:
sceFiosTerminate();
ERROR_INITIALIZE:
return res;
}
int psarcClose() {
sceFiosArchiveUnmountSync(NULL, g_ArchiveFH);
free(g_MountBuffer.pPtr);
sceFiosIOFilterRemove(g_ArchiveIndex);
sceFiosTerminate();
return 0;
}
int fileListGetPsarcEntries(FileList *list, const char *path, int sort) {
int res;
if (!list)
return VITASHELL_ERROR_ILLEGAL_ADDR;
SceFiosDH dh = -1;
SceFiosBuffer buf = SCE_FIOS_BUFFER_INITIALIZER;
res = sceFiosDHOpenSync(NULL, &dh, path, buf);
if (res < 0)
return res;
FileListEntry *entry = malloc(sizeof(FileListEntry));
if (entry) {
entry->name_length = strlen(DIR_UP);
entry->name = malloc(entry->name_length + 1);
strcpy(entry->name, DIR_UP);
entry->is_folder = 1;
entry->is_symlink = 0;
entry->type = FILE_TYPE_UNKNOWN;
fileListAddEntry(list, entry, sort);
}
do {
SceFiosDirEntry dir;
memset(&dir, 0, sizeof(SceFiosDirEntry));
res = sceFiosDHReadSync(NULL, dh, &dir);
if (res >= 0) {
char *name = dir.fullPath + dir.offsetToName;
SceFiosStat stat;
memset(&stat, 0, sizeof(SceFiosStat));
if (sceFiosStatSync(NULL, dir.fullPath, &stat) >= 0) {
FileListEntry *entry = malloc(sizeof(FileListEntry));
if (entry) {
entry->is_symlink = 0;
entry->is_folder = stat.statFlags & 0x1;
if (entry->is_folder) {
entry->name_length = strlen(name) + 1;
entry->name = malloc(entry->name_length + 1);
strcpy(entry->name, name);
addEndSlash(entry->name);
entry->type = FILE_TYPE_UNKNOWN;
list->folders++;
} else {
entry->name_length = strlen(name);
entry->name = malloc(entry->name_length + 1);
strcpy(entry->name, name);
entry->type = getFileType(entry->name);
list->files++;
}
entry->size = stat.fileSize;
SceDateTime time;
sceFiosDateToSceDateTime(stat.accessDate, &time);
memcpy(&entry->ctime, (SceDateTime *)&time, sizeof(SceDateTime));
sceFiosDateToSceDateTime(stat.modificationDate, &time);
memcpy(&entry->mtime, (SceDateTime *)&time, sizeof(SceDateTime));
sceFiosDateToSceDateTime(stat.creationDate, &time);
memcpy(&entry->atime, (SceDateTime *)&time, sizeof(SceDateTime));
fileListAddEntry(list, entry, sort);
}
}
}
} while (res >= 0);
sceFiosDHCloseSync(NULL, dh);
return 0;
}
int getPsarcPathInfo(const char *path, uint64_t *size, uint32_t *folders, uint32_t *files, int (* handler)(const char *path)) {
SceFiosDH dh = -1;
SceFiosBuffer buf = SCE_FIOS_BUFFER_INITIALIZER;
if (sceFiosDHOpenSync(NULL, &dh, path, buf) >= 0) {
int res = 0;
do {
SceFiosDirEntry dir;
memset(&dir, 0, sizeof(SceFiosDirEntry));
res = sceFiosDHReadSync(NULL, dh, &dir);
if (res >= 0) {
char *name = dir.fullPath + dir.offsetToName;
char *new_path = malloc(strlen(path) + strlen(name) + 2);
snprintf(new_path, MAX_PATH_LENGTH, "%s%s%s", path, hasEndSlash(path) ? "" : "/", name);
if (handler && handler(new_path)) {
free(new_path);
continue;
}
if (dir.statFlags & 0x1) {
int ret = getPsarcPathInfo(new_path, size, folders, files, handler);
if (ret <= 0) {
free(new_path);
sceFiosDHCloseSync(NULL, dh);
return ret;
}
} else {
if (size)
(*size) += dir.fileSize;
if (files)
(*files)++;
}
free(new_path);
}
} while (res >= 0);
sceFiosDHCloseSync(NULL, dh);
if (folders)
(*folders)++;
} else {
if (handler && handler(path))
return 1;
if (size) {
SceFiosStat stat;
memset(&stat, 0, sizeof(SceFiosStat));
int res = sceFiosStatSync(NULL, path, &stat);
if (res < 0)
return res;
(*size) += stat.fileSize;
}
if (files)
(*files)++;
}
return 1;
}
int extractPsarcFile(const char *src_path, const char *dst_path, FileProcessParam *param) {
SceUID fdsrc = psarcFileOpen(src_path, SCE_O_RDONLY, 0);
if (fdsrc < 0)
return fdsrc;
SceUID fddst = sceIoOpen(dst_path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if (fddst < 0) {
psarcFileClose(fdsrc);
return fddst;
}
void *buf = memalign(4096, TRANSFER_SIZE);
while (1) {
int read = psarcFileRead(fdsrc, buf, TRANSFER_SIZE);
if (read < 0) {
free(buf);
sceIoClose(fddst);
psarcFileClose(fdsrc);
sceIoRemove(dst_path);
return read;
}
if (read == 0)
break;
int written = sceIoWrite(fddst, buf, read);
if (written < 0) {
free(buf);
sceIoClose(fddst);
psarcFileClose(fdsrc);
sceIoRemove(dst_path);
return 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);
psarcFileClose(fdsrc);
sceIoRemove(dst_path);
return 0;
}
}
}
free(buf);
sceIoClose(fddst);
psarcFileClose(fdsrc);
return 1;
}
int extractPsarcPath(const char *src_path, const char *dst_path, FileProcessParam *param) {
SceFiosDH dh = -1;
SceFiosBuffer buf = SCE_FIOS_BUFFER_INITIALIZER;
if (sceFiosDHOpenSync(NULL, &dh, src_path, buf) >= 0) {
int ret = sceIoMkdir(dst_path, 0777);
if (ret < 0 && ret != SCE_ERROR_ERRNO_EEXIST) {
sceFiosDHCloseSync(NULL, dh);
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()) {
sceFiosDHCloseSync(NULL, dh);
return 0;
}
}
int res = 0;
do {
SceFiosDirEntry dir;
memset(&dir, 0, sizeof(SceFiosDirEntry));
res = sceFiosDHReadSync(NULL, dh, &dir);
if (res >= 0) {
char *name = dir.fullPath + dir.offsetToName;
char *new_src_path = malloc(strlen(src_path) + strlen(name) + 2);
snprintf(new_src_path, MAX_PATH_LENGTH, "%s%s%s", src_path, hasEndSlash(src_path) ? "" : "/", name);
char *new_dst_path = malloc(strlen(dst_path) + strlen(name) + 2);
snprintf(new_dst_path, MAX_PATH_LENGTH, "%s%s%s", dst_path, hasEndSlash(dst_path) ? "" : "/", name);
int ret = 0;
if (dir.statFlags & 0x1) {
ret = extractPsarcPath(new_src_path, new_dst_path, param);
} else {
ret = extractPsarcFile(new_src_path, new_dst_path, param);
}
free(new_dst_path);
free(new_src_path);
if (ret <= 0) {
sceFiosDHCloseSync(NULL, dh);
return ret;
}
}
} while (res >= 0);
sceFiosDHCloseSync(NULL, dh);
} else {
return extractPsarcFile(src_path, dst_path, param);
}
return 1;
}
int psarcFileGetstat(const char *file, SceIoStat *stat) {
SceFiosStat fios_stat;
memset(&fios_stat, 0, sizeof(SceFiosStat));
int res = sceFiosStatSync(NULL, file, &fios_stat);
if (res < 0)
return res;
if (stat) {
stat->st_mode = (fios_stat.statFlags & 0x1) ? SCE_S_IFDIR : SCE_S_IFREG;
stat->st_size = fios_stat.fileSize;
SceDateTime time;
sceFiosDateToSceDateTime(fios_stat.accessDate, &time);
memcpy(&stat->st_ctime, (SceDateTime *)&time, sizeof(SceDateTime));
sceFiosDateToSceDateTime(fios_stat.modificationDate, &time);
memcpy(&stat->st_mtime, (SceDateTime *)&time, sizeof(SceDateTime));
sceFiosDateToSceDateTime(fios_stat.creationDate, &time);
memcpy(&stat->st_atime, (SceDateTime *)&time, sizeof(SceDateTime));
}
return res;
}
int psarcFileOpen(const char *file, int flags, SceMode mode) {
SceFiosFH fh = -1;
int res = sceFiosFHOpenSync(NULL, &fh, file, NULL);
if (res < 0)
return res;
return fh;
}
int psarcFileRead(SceUID fd, void *data, SceSize size) {
return (int)sceFiosFHReadSync(NULL, fd, data, (SceFiosSize)size);
}
int psarcFileClose(SceUID fd) {
return sceFiosFHCloseSync(NULL, fd);
}