forked from mmozeiko/pkg2zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg2zip.c
937 lines (830 loc) · 27.4 KB
/
pkg2zip.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#include "pkg2zip_aes.h"
#include "pkg2zip_zip.h"
#include "pkg2zip_out.h"
#include "pkg2zip_psp.h"
#include "pkg2zip_utils.h"
#include "pkg2zip_zrif.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
#define PKG_HEADER_SIZE 192
#define PKG_HEADER_EXT_SIZE 64
// https://wiki.henkaku.xyz/vita/Packages#AES_Keys
static const uint8_t pkg_ps3_key[] = { 0x2e, 0x7b, 0x71, 0xd7, 0xc9, 0xc9, 0xa1, 0x4e, 0xa3, 0x22, 0x1f, 0x18, 0x88, 0x28, 0xb8, 0xf8 };
static const uint8_t pkg_psp_key[] = { 0x07, 0xf2, 0xc6, 0x82, 0x90, 0xb5, 0x0d, 0x2c, 0x33, 0x81, 0x8d, 0x70, 0x9b, 0x60, 0xe6, 0x2b };
static const uint8_t pkg_vita_2[] = { 0xe3, 0x1a, 0x70, 0xc9, 0xce, 0x1d, 0xd7, 0x2b, 0xf3, 0xc0, 0x62, 0x29, 0x63, 0xf2, 0xec, 0xcb };
static const uint8_t pkg_vita_3[] = { 0x42, 0x3a, 0xca, 0x3a, 0x2b, 0xd5, 0x64, 0x9f, 0x96, 0x86, 0xab, 0xad, 0x6f, 0xd8, 0x80, 0x1f };
static const uint8_t pkg_vita_4[] = { 0xaf, 0x07, 0xfd, 0x59, 0x65, 0x25, 0x27, 0xba, 0xf1, 0x33, 0x89, 0x66, 0x8b, 0x17, 0xd9, 0xea };
// http://vitadevwiki.com/vita/System_File_Object_(SFO)_(PSF)#Internal_Structure
// https://github.com/TheOfficialFloW/VitaShell/blob/1.74/sfo.h#L29
static void parse_sfo_content(const uint8_t* sfo, uint32_t sfo_size, char* category, char* title, char* content, char* min_version, char* pkg_version)
{
if (get32le(sfo) != 0x46535000)
{
sys_error("ERROR: incorrect sfo signature\n");
}
uint32_t keys = get32le(sfo + 8);
uint32_t values = get32le(sfo + 12);
uint32_t count = get32le(sfo + 16);
int title_index = -1;
int content_index = -1;
int category_index = -1;
int minver_index = -1;
int pkgver_index = -1;
for (uint32_t i = 0; i < count; i++)
{
if (i * 16 + 20 + 2 > sfo_size)
{
sys_error("ERROR: sfo information is too small\n");
}
char* key = (char*)sfo + keys + get16le(sfo + i * 16 + 20);
if (strcmp(key, "TITLE") == 0)
{
if (title_index < 0)
{
title_index = (int)i;
}
}
else if (strcmp(key, "STITLE") == 0)
{
title_index = (int)i;
}
else if (strcmp(key, "CONTENT_ID") == 0)
{
content_index = (int)i;
}
else if (strcmp(key, "CATEGORY") == 0)
{
category_index = (int)i;
}
else if (strcmp(key, "PSP2_DISP_VER") == 0)
{
minver_index = (int)i;
}
else if (strcmp(key, "APP_VER") == 0)
{
pkgver_index = (int)i;
}
}
if (title_index < 0)
{
sys_error("ERROR: cannot find title from sfo file, pkg is probably corrupted\n");
}
char* value = (char*)sfo + values + get32le(sfo + title_index * 16 + 20 + 12);
size_t i;
size_t max = 255;
for (i = 0; i<max && *value; i++, value++)
{
if ((*value >= 32 && *value < 127 && strchr("<>\"/\\|?*", *value) == NULL) || (uint8_t)*value >= 128)
{
if (*value == ':')
{
*title++ = ' ';
*title++ = '-';
max--;
}
else
{
*title++ = *value;
}
}
else if (*value == 10)
{
*title++ = ' ';
}
}
*title = 0;
if (content_index >= 0 && content)
{
value = (char*)sfo + values + get32le(sfo + content_index * 16 + 20 + 12);
while (*value)
{
*content++ = *value++;
}
*content = 0;
}
if (category_index >= 0)
{
value = (char*)sfo + values + get32le(sfo + category_index * 16 + 20 + 12);
while (*value)
{
*category++ = *value++;
}
}
*category = 0;
if (minver_index >= 0 && min_version)
{
value = (char*)sfo + values + get32le(sfo + minver_index * 16 + 20 + 12);
if (*value == '0')
{
value++;
}
while (*value)
{
*min_version++ = *value++;
}
if (min_version[-1] == '0')
{
min_version[-1] = 0;
}
else
{
*min_version = 0;
}
}
if (pkgver_index >= 0 && pkg_version)
{
value = (char*)sfo + values + get32le(sfo + pkgver_index * 16 + 20 + 12);
if (*value == '0')
{
value++;
}
while (*value)
{
*pkg_version++ = *value++;
}
*pkg_version = 0;
}
}
static void parse_sfo(sys_file f, uint64_t sfo_offset, uint32_t sfo_size, char* category, char* title, char* content, char* min_version, char* pkg_version)
{
uint8_t sfo[16 * 1024];
if (sfo_size < 16)
{
sys_error("ERROR: sfo information is too small\n");
}
if (sfo_size > sizeof(sfo))
{
sys_error("ERROR: sfo information is too big, pkg file is probably corrupted\n");
}
sys_read(f, sfo_offset, sfo, sfo_size);
parse_sfo_content(sfo, sfo_size, category, title, content, min_version, pkg_version);
}
static void find_psp_sfo(const aes128_key* key, const aes128_key* ps3_key, const uint8_t* iv, sys_file pkg, uint64_t pkg_size, uint64_t enc_offset, uint64_t items_offset, uint32_t item_count, char* category, char* title)
{
for (uint32_t item_index = 0; item_index < item_count; item_index++)
{
uint8_t item[32];
uint64_t item_offset = items_offset + item_index * 32;
sys_read(pkg, enc_offset + item_offset, item, sizeof(item));
aes128_ctr_xor(key, iv, item_offset / 16, item, sizeof(item));
uint32_t name_offset = get32be(item + 0);
uint32_t name_size = get32be(item + 4);
uint64_t data_offset = get64be(item + 8);
uint64_t data_size = get64be(item + 16);
uint8_t psp_type = item[24];
assert(name_offset % 16 == 0);
assert(data_offset % 16 == 0);
if (pkg_size < enc_offset + name_offset + name_size ||
pkg_size < enc_offset + data_offset + data_size)
{
sys_error("ERROR: pkg file is too short, possibly corrupted\n");
}
const aes128_key* item_key = psp_type == 0x90 ? key : ps3_key;
char name[ZIP_MAX_FILENAME];
sys_read(pkg, enc_offset + name_offset, name, name_size);
aes128_ctr_xor(item_key, iv, name_offset / 16, (uint8_t*)name, name_size);
name[name_size] = 0;
if (strcmp(name, "PARAM.SFO") == 0)
{
uint8_t sfo[16 * 1024];
if (data_size < 16)
{
sys_error("ERROR: sfo information is too small\n");
}
if (data_size > sizeof(sfo))
{
sys_error("ERROR: sfo information is too big, pkg file is probably corrupted\n");
}
sys_read(pkg, enc_offset + data_offset, sfo, (uint32_t)data_size);
aes128_ctr_xor(item_key, iv, data_offset / 16, sfo, (uint32_t)data_size);
parse_sfo_content(sfo, (uint32_t)data_size, category, title, NULL, NULL, NULL);
return;
}
}
}
static const char* get_region(const char* id)
{
if (memcmp(id, "PCSE", 4) == 0 || memcmp(id, "PCSA", 4) == 0 ||
memcmp(id, "NPNA", 4) == 0)
{
return "USA";
}
else if (memcmp(id, "PCSF", 4) == 0 || memcmp(id, "PCSB", 4) == 0 ||
memcmp(id, "NPOA", 4) == 0)
{
return "EUR";
}
else if (memcmp(id, "PCSC", 4) == 0 || memcmp(id, "VCJS", 4) == 0 ||
memcmp(id, "PCSG", 4) == 0 || memcmp(id, "VLJS", 4) == 0 ||
memcmp(id, "VLJM", 4) == 0 || memcmp(id, "NPPA", 4) == 0)
{
return "JPN";
}
else if (memcmp(id, "VCAS", 4) == 0 || memcmp(id, "PCSH", 4) == 0 ||
memcmp(id, "VLAS", 4) == 0 || memcmp(id, "PCSD", 4) == 0 ||
memcmp(id, "NPQA", 4) == 0)
{
return "ASA";
}
else
{
return "unknown region";
}
}
typedef enum {
PKG_TYPE_VITA_APP,
PKG_TYPE_VITA_DLC,
PKG_TYPE_VITA_PATCH,
PKG_TYPE_VITA_PSM,
PKG_TYPE_PSP,
PKG_TYPE_PSX,
} pkg_type;
int main(int argc, char* argv[])
{
sys_output_init();
int zipped = 1;
int listing = 0;
int cso = 0;
const char* pkg_arg = NULL;
const char* zrif_arg = NULL;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-x") == 0)
{
zipped = 0;
}
else if (strcmp(argv[i], "-l") == 0)
{
listing = 1;
}
else if (strncmp(argv[i], "-c", 2) == 0)
{
if (argv[i][2] != 0)
{
cso = atoi(argv[i] + 2);
cso = cso > 9 ? 9 : cso < 0 ? 0 : cso;
}
}
else
{
if (pkg_arg != NULL)
{
zrif_arg = argv[i];
break;
}
else
{
pkg_arg = argv[i];
}
}
}
if (listing == 0)
{
sys_output("pkg2zip v1.8\n");
}
if (pkg_arg == NULL)
{
fprintf(stderr, "ERROR: no pkg file specified\n");
sys_error("Usage: %s [-x] [-l] [-c[N]] file.pkg [zRIF]\n", argv[0]);
}
if (listing == 0)
{
sys_output("[*] loading...\n");
}
uint64_t pkg_size;
sys_file pkg = sys_open(pkg_arg, &pkg_size);
uint8_t pkg_header[PKG_HEADER_SIZE + PKG_HEADER_EXT_SIZE];
sys_read(pkg, 0, pkg_header, sizeof(pkg_header));
if (get32be(pkg_header) != 0x7f504b47 || get32be(pkg_header + PKG_HEADER_SIZE) != 0x7F657874)
{
sys_error("ERROR: not a pkg file\n");
}
// http://www.psdevwiki.com/ps3/PKG_files
uint64_t meta_offset = get32be(pkg_header + 8);
uint32_t meta_count = get32be(pkg_header + 12);
uint32_t item_count = get32be(pkg_header + 20);
uint64_t total_size = get64be(pkg_header + 24);
uint64_t enc_offset = get64be(pkg_header + 32);
uint64_t enc_size = get64be(pkg_header + 40);
const uint8_t* iv = pkg_header + 0x70;
int key_type = pkg_header[0xe7] & 7;
if (pkg_size < total_size)
{
sys_error("ERROR: pkg file is too small\n");
}
if (pkg_size < enc_offset + item_count * 32)
{
sys_error("ERROR: pkg file is too small\n");
}
uint32_t content_type = 0;
uint32_t sfo_offset = 0;
uint32_t sfo_size = 0;
uint32_t items_offset = 0;
uint32_t items_size = 0;
for (uint32_t i = 0; i < meta_count; i++)
{
uint8_t block[16];
sys_read(pkg, meta_offset, block, sizeof(block));
uint32_t type = get32be(block + 0);
uint32_t size = get32be(block + 4);
if (type == 2)
{
content_type = get32be(block + 8);
}
else if (type == 13)
{
items_offset = get32be(block + 8);
items_size = get32be(block + 12);
}
else if (type == 14)
{
sfo_offset = get32be(block + 8);
sfo_size = get32be(block + 12);
}
meta_offset += 2 * sizeof(uint32_t) + size;
}
pkg_type type;
// http://www.psdevwiki.com/ps3/PKG_files
if (content_type == 6)
{
type = PKG_TYPE_PSX;
}
else if (content_type == 7 || content_type == 0xe || content_type == 0xf || content_type == 0x10)
{
// PSP & PSP-PCEngine / PSP-Go / PSP-Mini / PSP-NeoGeo
type = PKG_TYPE_PSP;
}
else if (content_type == 0x15)
{
type = PKG_TYPE_VITA_APP;
}
else if (content_type == 0x16)
{
type = PKG_TYPE_VITA_DLC;
}
else if (content_type == 0x18 || content_type == 0x1d)
{
type = PKG_TYPE_VITA_PSM;
}
else
{
sys_error("ERROR: unsupported content type 0x%x", content_type);
}
aes128_key ps3_key;
uint8_t main_key[16];
if (key_type == 1)
{
memcpy(main_key, pkg_psp_key, sizeof(main_key));
aes128_init(&ps3_key, pkg_ps3_key);
}
else if (key_type == 2)
{
aes128_key key;
aes128_init(&key, pkg_vita_2);
aes128_ecb_encrypt(&key, iv, main_key);
}
else if (key_type == 3)
{
aes128_key key;
aes128_init(&key, pkg_vita_3);
aes128_ecb_encrypt(&key, iv, main_key);
}
else if (key_type == 4)
{
aes128_key key;
aes128_init(&key, pkg_vita_4);
aes128_ecb_encrypt(&key, iv, main_key);
}
aes128_key key;
aes128_init(&key, main_key);
char content[256];
char title[256];
char category[256];
char min_version[256];
char pkg_version[256];
const char* id = content + 7;
const char* id2 = id + 13;
// first 512 - for vita games - https://github.com/TheOfficialFloW/NoNpDrm/blob/v1.1/src/main.c#L42
// 1024 is used for PSM
uint8_t rif[1024];
uint32_t rif_size = 0;
if (type == PKG_TYPE_PSP || type == PKG_TYPE_PSX)
{
find_psp_sfo(&key, &ps3_key, iv, pkg, pkg_size, enc_offset, items_offset, item_count, category, title);
id = (char*)pkg_header + 0x37;
}
else // Vita
{
if (type == PKG_TYPE_VITA_PSM)
{
memcpy(content, pkg_header + 0x30, 0x30);
rif_size = 1024;
}
else // Vita APP, DLC or PATCH
{
parse_sfo(pkg, sfo_offset, sfo_size, category, title, content, min_version, pkg_version);
rif_size = 512;
if (type == PKG_TYPE_VITA_APP && strcmp(category, "gp") == 0)
{
type = PKG_TYPE_VITA_PATCH;
}
}
if (type != PKG_TYPE_VITA_PATCH && zrif_arg != NULL)
{
zrif_decode(zrif_arg, rif, rif_size);
const char* rif_contentid = (char*)rif + (type == PKG_TYPE_VITA_PSM ? 0x50 : 0x10);
if (strncmp(rif_contentid, content, 0x30) != 0)
{
sys_error("ERROR: zRIF content id '%s' doesn't match pkg '%s'\n", rif_contentid, content);
}
}
}
const char* ext = zipped ? ".zip" : "";
char root[1024];
if (type == PKG_TYPE_PSP)
{
const char* type_str;
if (content_type == 7)
{
type_str = (strcmp(category, "HG") == 0) ? "PSP-PCEngine" : "PSP";
}
else
{
type_str = content_type == 0xe ? "PSP-Go" : content_type == 0xf ? "PSP-Mini" : "PSP-NeoGeo";
}
snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, type_str, ext);
if (listing == 0)
{
sys_output("[*] unpacking %s\n", type_str);
}
}
else if (type == PKG_TYPE_PSX)
{
snprintf(root, sizeof(root), "%s [%.9s] [PSX]%s", title, id, ext);
if (listing == 0)
{
sys_output("[*] unpacking PSX\n");
}
}
else if (type == PKG_TYPE_VITA_DLC)
{
snprintf(root, sizeof(root), "%s [%.9s] [%s] [DLC-%s]%s", title, id, get_region(id), id2, ext);
if (listing == 0)
{
sys_output("[*] unpacking Vita DLC\n");
}
}
else if (type == PKG_TYPE_VITA_PATCH)
{
snprintf(root, sizeof(root), "%s [%.9s] [%s] [PATCH] [v%s]%s", title, id, get_region(id), pkg_version, ext);
if (listing == 0)
{
sys_output("[*] unpacking Vita PATCH\n");
}
}
else if (type == PKG_TYPE_VITA_PSM)
{
snprintf(root, sizeof(root), "%.9s [%s] [PSM]%s", id, get_region(id), ext);
if (listing == 0)
{
sys_output("[*] unpacking Vita PSM\n");
}
}
else if (type == PKG_TYPE_VITA_APP)
{
snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, get_region(id), ext);
if (listing == 0)
{
sys_output("[*] unpacking Vita APP\n");
}
}
else
{
assert(0);
sys_error("ERROR: unsupported type\n");
}
if (listing && zipped)
{
sys_output("%s\n", root);
exit(0);
}
else if (listing && zipped == 0)
{
sys_error("ERROR: Listing option without creating zip is useless\n");
}
if (zipped)
{
sys_output("[*] creating '%s' archive\n", root);
}
out_begin(root, zipped);
root[0] = 0;
if (type == PKG_TYPE_PSP)
{
snprintf(root, sizeof(root), "pspemu/ISO");
out_add_folder(root);
if (content_type == 7 && strcmp(category, "HG") == 0)
{
snprintf(root, sizeof(root), "pspemu");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/PSP");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/GAME");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%.9s", id);
out_add_folder(root);
}
}
else if (type == PKG_TYPE_PSX)
{
sys_vstrncat(root, sizeof(root), "pspemu");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/PSP");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/GAME");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%.9s", id);
out_add_folder(root);
}
else if (type == PKG_TYPE_VITA_DLC)
{
sys_vstrncat(root, sizeof(root), "addcont");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%.9s", id);
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%s", id2);
out_add_folder(root);
}
else if (type == PKG_TYPE_VITA_PATCH)
{
sys_vstrncat(root, sizeof(root), "patch");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%.9s", id);
out_add_folder(root);
}
else if (type == PKG_TYPE_VITA_PSM)
{
sys_vstrncat(root, sizeof(root), "psm");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%.9s", id);
out_add_folder(root);
}
else if (type == PKG_TYPE_VITA_APP)
{
sys_vstrncat(root, sizeof(root), "app");
out_add_folder(root);
sys_vstrncat(root, sizeof(root), "/%.9s", id);
out_add_folder(root);
}
else
{
assert(0);
sys_error("ERROR: unsupported type\n");
}
char path[1024];
int sce_sys_package_created = 0;
sys_output_progress_init(pkg_size);
for (uint32_t item_index = 0; item_index < item_count; item_index++)
{
uint8_t item[32];
uint64_t item_offset = items_offset + item_index * 32;
sys_read(pkg, enc_offset + item_offset, item, sizeof(item));
aes128_ctr_xor(&key, iv, item_offset / 16, item, sizeof(item));
uint32_t name_offset = get32be(item + 0);
uint32_t name_size = get32be(item + 4);
uint64_t data_offset = get64be(item + 8);
uint64_t data_size = get64be(item + 16);
uint8_t psp_type = item[24];
uint8_t flags = item[27];
assert(name_offset % 16 == 0);
assert(data_offset % 16 == 0);
if (pkg_size < enc_offset + name_offset + name_size ||
pkg_size < enc_offset + data_offset + data_size)
{
sys_error("ERROR: pkg file is too short, possibly corrupted\n");
}
if (name_size >= ZIP_MAX_FILENAME)
{
sys_error("ERROR: pkg file contains file with very long name\n");
}
const aes128_key* item_key;
if (type == PKG_TYPE_PSP || type == PKG_TYPE_PSX)
{
item_key = psp_type == 0x90 ? &key : &ps3_key;
}
else
{
item_key = &key;
}
char name[ZIP_MAX_FILENAME];
sys_read(pkg, enc_offset + name_offset, name, name_size);
aes128_ctr_xor(item_key, iv, name_offset / 16, (uint8_t*)name, name_size);
name[name_size] = 0;
// sys_output("[%u/%u] %s\n", item_index + 1, item_count, name);
if (flags == 4 || flags == 18)
{
if (type == PKG_TYPE_VITA_PSM)
{
// skip "content/" prefix
char* slash = strchr(name, '/');
if (slash != NULL)
{
snprintf(path, sizeof(path), "%s/RO/%s", root, name + 8);
out_add_folder(path);
}
}
else if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PATCH)
{
snprintf(path, sizeof(path), "%s/%s", root, name);
out_add_folder(path);
if (strcmp("sce_sys/package", name) == 0)
{
sce_sys_package_created = 1;
}
}
}
else
{
int decrypt = 1;
if ((type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PATCH) && strcmp("sce_sys/package/digs.bin", name) == 0)
{
// TODO: is this really needed?
if (!sce_sys_package_created)
{
snprintf(path, sizeof(path), "%s/sce_sys/package", root);
out_add_folder(path);
sce_sys_package_created = 1;
}
snprintf(name, sizeof(name), "%s", "sce_sys/package/body.bin");
decrypt = 0;
}
if (type == PKG_TYPE_PSX)
{
if (strcmp("USRDIR/CONTENT/DOCUMENT.DAT", name) == 0)
{
snprintf(path, sizeof(path), "%s/DOCUMENT.DAT", root);
}
else if (strcmp("USRDIR/CONTENT/EBOOT.PBP", name) == 0)
{
snprintf(path, sizeof(path), "%s/EBOOT.PBP", root);
}
else
{
continue;
}
}
else if (type == PKG_TYPE_PSP)
{
if (strcmp("USRDIR/CONTENT/EBOOT.PBP", name) == 0)
{
snprintf(path, sizeof(path), "pspemu/ISO/%s [%.9s].%s", title, id, cso ? "cso" : "iso");
unpack_psp_eboot(path, item_key, iv, pkg, enc_offset, data_offset, data_size, cso);
continue;
}
else if (strcmp("USRDIR/CONTENT/PSP-KEY.EDAT", name) == 0)
{
snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/PSP-KEY.EDAT", id);
unpack_psp_key(path, item_key, iv, pkg, enc_offset, data_offset, data_size);
continue;
}
else if (strcmp("USRDIR/CONTENT/CONTENT.DAT", name) == 0)
{
snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/CONTENT.DAT", id);
}
else
{
continue;
}
}
else if (type == PKG_TYPE_VITA_PSM)
{
// skip "content/" prefix
snprintf(path, sizeof(path), "%s/RO/%s", root, name + 8);
}
else
{
snprintf(path, sizeof(path), "%s/%s", root, name);
}
uint64_t offset = data_offset;
out_begin_file(path, 0);
while (data_size != 0)
{
uint8_t PKG_ALIGN(16) buffer[1 << 16];
uint32_t size = (uint32_t)min64(data_size, sizeof(buffer));
sys_output_progress(enc_offset + offset);
sys_read(pkg, enc_offset + offset, buffer, size);
if (decrypt)
{
aes128_ctr_xor(item_key, iv, offset / 16, buffer, size);
}
out_write(buffer, size);
offset += size;
data_size -= size;
}
out_end_file();
}
}
sys_output("[*] unpacking completed\n");
if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PATCH)
{
if (!sce_sys_package_created)
{
sys_output("[*] creating sce_sys/package\n");
snprintf(path, sizeof(path), "%s/sce_sys/package", root);
out_add_folder(path);
}
sys_output("[*] creating sce_sys/package/head.bin\n");
snprintf(path, sizeof(path), "%s/sce_sys/package/head.bin", root);
out_begin_file(path, 0);
uint64_t head_size = enc_offset + items_size;
uint64_t head_offset = 0;
while (head_size != 0)
{
uint8_t PKG_ALIGN(16) buffer[1 << 16];
uint32_t size = (uint32_t)min64(head_size, sizeof(buffer));
sys_read(pkg, head_offset, buffer, size);
out_write(buffer, size);
head_size -= size;
head_offset += size;
}
out_end_file();
sys_output("[*] creating sce_sys/package/tail.bin\n");
snprintf(path, sizeof(path), "%s/sce_sys/package/tail.bin", root);
out_begin_file(path, 0);
uint64_t tail_offset = enc_offset + enc_size;
while (tail_offset != pkg_size)
{
uint8_t PKG_ALIGN(16) buffer[1 << 16];
uint32_t size = (uint32_t)min64(pkg_size - tail_offset, sizeof(buffer));
sys_read(pkg, tail_offset, buffer, size);
out_write(buffer, size);
tail_offset += size;
}
out_end_file();
sys_output("[*] creating sce_sys/package/stat.bin\n");
snprintf(path, sizeof(path), "%s/sce_sys/package/stat.bin", root);
uint8_t stat[768] = { 0 };
out_begin_file(path, 0);
out_write(stat, sizeof(stat));
out_end_file();
}
if ((type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PSM) && zrif_arg != NULL)
{
if (type == PKG_TYPE_VITA_PSM)
{
sys_output("[*] creating RO/License\n");
snprintf(path, sizeof(path), "%s/RO/License", root);
out_add_folder(path);
sys_output("[*] creating RO/License/FAKE.rif\n");
snprintf(path, sizeof(path), "%s/RO/License/FAKE.rif", root);
}
else
{
sys_output("[*] creating sce_sys/package/work.bin\n");
snprintf(path, sizeof(path), "%s/sce_sys/package/work.bin", root);
}
out_begin_file(path, 0);
out_write(rif, rif_size);
out_end_file();
}
if (type == PKG_TYPE_VITA_PSM)
{
sys_output("[*] creating RW\n");
snprintf(path, sizeof(path), "%s/RW", root);
out_add_folder(path);
sys_output("[*] creating RW/Documents\n");
snprintf(path, sizeof(path), "%s/RW/Documents", root);
out_add_folder(path);
sys_output("[*] creating RW/Temp\n");
snprintf(path, sizeof(path), "%s/RW/Temp", root);
out_add_folder(path);
sys_output("[*] creating RW/System\n");
snprintf(path, sizeof(path), "%s/RW/System", root);
out_add_folder(path);
sys_output("[*] creating RW/System/content_id\n");
snprintf(path, sizeof(path), "%s/RW/System/content_id", root);
out_begin_file(path, 0);
out_write(pkg_header + 0x30, 0x30);
out_end_file();
sys_output("[*] creating RW/System/pm.dat\n");
snprintf(path, sizeof(path), "%s/RW/System/pm.dat", root);
uint8_t pm[1 << 16] = { 0 };
out_begin_file(path, 0);
out_write(pm, sizeof(pm));
out_end_file();
}
out_end();
if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_PATCH)
{
sys_output("[*] minimum fw version required: %s\n", min_version);
}
sys_output("[*] done!\n");
sys_output_done();
}