Skip to content

Commit ffdb0e8

Browse files
committed
fixing unicode output on Windows
1 parent dcec2ea commit ffdb0e8

8 files changed

+175
-106
lines changed

pkg2zip.c

+45-45
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ static const uint8_t pkg_vita_4[] = { 0xaf, 0x07, 0xfd, 0x59, 0x65, 0x25, 0x27,
2525
// https://github.com/TheOfficialFloW/VitaShell/blob/1.74/sfo.h#L29
2626
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)
2727
{
28-
2928
if (get32le(sfo) != 0x46535000)
3029
{
31-
fatal("ERROR: incorrect sfo signature\n");
30+
sys_error("ERROR: incorrect sfo signature\n");
3231
}
3332

3433
uint32_t keys = get32le(sfo + 8);
@@ -44,7 +43,7 @@ static void parse_sfo_content(const uint8_t* sfo, uint32_t sfo_size, char* categ
4443
{
4544
if (i * 16 + 20 + 2 > sfo_size)
4645
{
47-
fatal("ERROR: sfo information is too small\n");
46+
sys_error("ERROR: sfo information is too small\n");
4847
}
4948

5049
char* key = (char*)sfo + keys + get16le(sfo + i * 16 + 20);
@@ -79,7 +78,7 @@ static void parse_sfo_content(const uint8_t* sfo, uint32_t sfo_size, char* categ
7978

8079
if (title_index < 0)
8180
{
82-
fatal("ERROR: cannot find title from sfo file, pkg is probably corrupted\n");
81+
sys_error("ERROR: cannot find title from sfo file, pkg is probably corrupted\n");
8382
}
8483

8584
char* value = (char*)sfo + values + get32le(sfo + title_index * 16 + 20 + 12);
@@ -168,11 +167,11 @@ static void parse_sfo(sys_file f, uint64_t sfo_offset, uint32_t sfo_size, char*
168167
uint8_t sfo[16 * 1024];
169168
if (sfo_size < 16)
170169
{
171-
fatal("ERROR: sfo information is too small\n");
170+
sys_error("ERROR: sfo information is too small\n");
172171
}
173172
if (sfo_size > sizeof(sfo))
174173
{
175-
fatal("ERROR: sfo information is too big, pkg file is probably corrupted\n");
174+
sys_error("ERROR: sfo information is too big, pkg file is probably corrupted\n");
176175
}
177176
sys_read(f, sfo_offset, sfo, sfo_size);
178177

@@ -200,7 +199,7 @@ static void find_psp_sfo(const aes128_key* key, const aes128_key* ps3_key, const
200199
if (pkg_size < enc_offset + name_offset + name_size ||
201200
pkg_size < enc_offset + data_offset + data_size)
202201
{
203-
fatal("ERROR: pkg file is too short, possibly corrupted\n");
202+
sys_error("ERROR: pkg file is too short, possibly corrupted\n");
204203
}
205204

206205
const aes128_key* item_key = psp_type == 0x90 ? key : ps3_key;
@@ -215,11 +214,11 @@ static void find_psp_sfo(const aes128_key* key, const aes128_key* ps3_key, const
215214
uint8_t sfo[16 * 1024];
216215
if (data_size < 16)
217216
{
218-
fatal("ERROR: sfo information is too small\n");
217+
sys_error("ERROR: sfo information is too small\n");
219218
}
220219
if (data_size > sizeof(sfo))
221220
{
222-
fatal("ERROR: sfo information is too big, pkg file is probably corrupted\n");
221+
sys_error("ERROR: sfo information is too big, pkg file is probably corrupted\n");
223222
}
224223

225224
sys_read(pkg, enc_offset + data_offset, sfo, (uint32_t)data_size);
@@ -272,7 +271,8 @@ typedef enum {
272271

273272
int main(int argc, char* argv[])
274273
{
275-
printf("pkg2zip v1.8\n");
274+
sys_output_init();
275+
sys_output("pkg2zip v1.8\n");
276276

277277
int zipped = 1;
278278
int cso = 5;
@@ -309,10 +309,10 @@ int main(int argc, char* argv[])
309309
if (pkg_arg == NULL)
310310
{
311311
fprintf(stderr, "ERROR: no pkg file specified\n");
312-
fatal("Usage: %s [-x] [-c[N]] file.pkg [zRIF]\n", argv[0]);
312+
sys_error("Usage: %s [-x] [-c[N]] file.pkg [zRIF]\n", argv[0]);
313313
}
314314

315-
printf("[*] loading...\n");
315+
sys_output("[*] loading...\n");
316316

317317
uint64_t pkg_size;
318318
sys_file pkg = sys_open(pkg_arg, &pkg_size);
@@ -322,7 +322,7 @@ int main(int argc, char* argv[])
322322

323323
if (get32be(pkg_header) != 0x7f504b47 || get32be(pkg_header + PKG_HEADER_SIZE) != 0x7F657874)
324324
{
325-
fatal("ERROR: not a pkg file\n");
325+
sys_error("ERROR: not a pkg file\n");
326326
}
327327

328328
// http://www.psdevwiki.com/ps3/PKG_files
@@ -337,11 +337,11 @@ int main(int argc, char* argv[])
337337

338338
if (pkg_size < total_size)
339339
{
340-
fatal("ERROR: pkg file is too small\n");
340+
sys_error("ERROR: pkg file is too small\n");
341341
}
342342
if (pkg_size < enc_offset + item_count * 32)
343343
{
344-
fatal("ERROR: pkg file is too small\n");
344+
sys_error("ERROR: pkg file is too small\n");
345345
}
346346

347347
uint32_t content_type = 0;
@@ -402,7 +402,7 @@ int main(int argc, char* argv[])
402402
}
403403
else
404404
{
405-
fatal("ERROR: unsupported content type 0x%x", content_type);
405+
sys_error("ERROR: unsupported content type 0x%x", content_type);
406406
}
407407

408408
aes128_key ps3_key;
@@ -476,7 +476,7 @@ int main(int argc, char* argv[])
476476
const char* rif_contentid = (char*)rif + (type == PKG_TYPE_VITA_PSM ? 0x50 : 0x10);
477477
if (strncmp(rif_contentid, content, 0x30) != 0)
478478
{
479-
fatal("ERROR: zRIF content id '%s' doesn't match pkg '%s'\n", rif_contentid, content);
479+
sys_error("ERROR: zRIF content id '%s' doesn't match pkg '%s'\n", rif_contentid, content);
480480
}
481481
}
482482
}
@@ -496,42 +496,42 @@ int main(int argc, char* argv[])
496496
type_str = content_type == 0xe ? "PSP-Go" : content_type == 0xf ? "PSP-Mini" : "PSP-NeoGeo";
497497
}
498498
snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, type_str, ext);
499-
printf("[*] unpacking %s\n", type_str);
499+
sys_output("[*] unpacking %s\n", type_str);
500500
}
501501
else if (type == PKG_TYPE_PSX)
502502
{
503503
snprintf(root, sizeof(root), "%s [%.9s] [PSX]%s", title, id, ext);
504-
printf("[*] unpacking PSX\n");
504+
sys_output("[*] unpacking PSX\n");
505505
}
506506
else if (type == PKG_TYPE_VITA_DLC)
507507
{
508508
snprintf(root, sizeof(root), "%s [%.9s] [%s] [DLC-%s]%s", title, id, get_region(id), id2, ext);
509-
printf("[*] unpacking DLC\n");
509+
sys_output("[*] unpacking DLC\n");
510510
}
511511
else if (type == PKG_TYPE_VITA_PATCH)
512512
{
513513
snprintf(root, sizeof(root), "%s [%.9s] [%s] [PATCH] [v%s]%s", title, id, get_region(id), pkg_version, ext);
514-
printf("[*] unpacking PATCH\n");
514+
sys_output("[*] unpacking PATCH\n");
515515
}
516516
else if (type == PKG_TYPE_VITA_PSM)
517517
{
518518
snprintf(root, sizeof(root), "%.9s [%s]%s", id, get_region(id), ext);
519-
printf("[*] unpacking PSM\n");
519+
sys_output("[*] unpacking PSM\n");
520520
}
521521
else if (type == PKG_TYPE_VITA_APP)
522522
{
523523
snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, get_region(id), ext);
524-
printf("[*] unpacking APP\n");
524+
sys_output("[*] unpacking APP\n");
525525
}
526526
else
527527
{
528528
assert(0);
529-
fatal("ERROR: unsupported type\n");
529+
sys_error("ERROR: unsupported type\n");
530530
}
531531

532532
if (zipped)
533533
{
534-
printf("[*] creating '%s' archive\n", root);
534+
sys_output("[*] creating '%s' archive\n", root);
535535
}
536536

537537
out_begin(root, zipped);
@@ -603,10 +603,10 @@ int main(int argc, char* argv[])
603603
else
604604
{
605605
assert(0);
606-
fatal("ERROR: unsupported type\n");
606+
sys_error("ERROR: unsupported type\n");
607607
}
608608

609-
printf("[*] decrypting...\n");
609+
sys_output("[*] decrypting...\n");
610610
char path[1024];
611611

612612
int sce_sys_package_created = 0;
@@ -631,12 +631,12 @@ int main(int argc, char* argv[])
631631
if (pkg_size < enc_offset + name_offset + name_size ||
632632
pkg_size < enc_offset + data_offset + data_size)
633633
{
634-
fatal("ERROR: pkg file is too short, possibly corrupted\n");
634+
sys_error("ERROR: pkg file is too short, possibly corrupted\n");
635635
}
636636

637637
if (name_size >= ZIP_MAX_FILENAME)
638638
{
639-
fatal("ERROR: pkg file contains file with very long name\n");
639+
sys_error("ERROR: pkg file contains file with very long name\n");
640640
}
641641

642642
const aes128_key* item_key;
@@ -654,7 +654,7 @@ int main(int argc, char* argv[])
654654
aes128_ctr_xor(item_key, iv, name_offset / 16, (uint8_t*)name, name_size);
655655
name[name_size] = 0;
656656

657-
printf("[%u/%u] %s\n", item_index + 1, item_count, name);
657+
sys_output("[%u/%u] %s\n", item_index + 1, item_count, name);
658658

659659
if (flags == 4 || flags == 18)
660660
{
@@ -770,12 +770,12 @@ int main(int argc, char* argv[])
770770
{
771771
if (!sce_sys_package_created)
772772
{
773-
printf("[*] creating sce_sys/package\n");
773+
sys_output("[*] creating sce_sys/package\n");
774774
snprintf(path, sizeof(path), "%s/sce_sys/package", root);
775775
out_add_folder(path);
776776
}
777777

778-
printf("[*] creating sce_sys/package/head.bin\n");
778+
sys_output("[*] creating sce_sys/package/head.bin\n");
779779
snprintf(path, sizeof(path), "%s/sce_sys/package/head.bin", root);
780780

781781
out_begin_file(path, 0);
@@ -792,7 +792,7 @@ int main(int argc, char* argv[])
792792
}
793793
out_end_file();
794794

795-
printf("[*] creating sce_sys/package/tail.bin\n");
795+
sys_output("[*] creating sce_sys/package/tail.bin\n");
796796
snprintf(path, sizeof(path), "%s/sce_sys/package/tail.bin", root);
797797

798798
out_begin_file(path, 0);
@@ -807,7 +807,7 @@ int main(int argc, char* argv[])
807807
}
808808
out_end_file();
809809

810-
printf("[*] creating sce_sys/package/stat.bin\n");
810+
sys_output("[*] creating sce_sys/package/stat.bin\n");
811811
snprintf(path, sizeof(path), "%s/sce_sys/package/stat.bin", root);
812812

813813
uint8_t stat[768] = { 0 };
@@ -820,16 +820,16 @@ int main(int argc, char* argv[])
820820
{
821821
if (type == PKG_TYPE_VITA_PSM)
822822
{
823-
printf("[*] creating RO/License\n");
823+
sys_output("[*] creating RO/License\n");
824824
snprintf(path, sizeof(path), "%s/RO/License", root);
825825
out_add_folder(path);
826826

827-
printf("[*] creating RO/License/FAKE.rif\n");
827+
sys_output("[*] creating RO/License/FAKE.rif\n");
828828
snprintf(path, sizeof(path), "%s/RO/License/FAKE.rif", root);
829829
}
830830
else
831831
{
832-
printf("[*] creating sce_sys/package/work.bin\n");
832+
sys_output("[*] creating sce_sys/package/work.bin\n");
833833
snprintf(path, sizeof(path), "%s/sce_sys/package/work.bin", root);
834834
}
835835

@@ -840,29 +840,29 @@ int main(int argc, char* argv[])
840840

841841
if (type == PKG_TYPE_VITA_PSM)
842842
{
843-
printf("[*] creating RW\n");
843+
sys_output("[*] creating RW\n");
844844
snprintf(path, sizeof(path), "%s/RW", root);
845845
out_add_folder(path);
846846

847-
printf("[*] creating RW/Documents\n");
847+
sys_output("[*] creating RW/Documents\n");
848848
snprintf(path, sizeof(path), "%s/RW/Documents", root);
849849
out_add_folder(path);
850850

851-
printf("[*] creating RW/Temp\n");
851+
sys_output("[*] creating RW/Temp\n");
852852
snprintf(path, sizeof(path), "%s/RW/Temp", root);
853853
out_add_folder(path);
854854

855-
printf("[*] creating RW/System\n");
855+
sys_output("[*] creating RW/System\n");
856856
snprintf(path, sizeof(path), "%s/RW/System", root);
857857
out_add_folder(path);
858858

859-
printf("[*] creating RW/System/content_id\n");
859+
sys_output("[*] creating RW/System/content_id\n");
860860
snprintf(path, sizeof(path), "%s/RW/System/content_id", root);
861861
out_begin_file(path, 0);
862862
out_write(pkg_header + 0x30, 0x30);
863863
out_end_file();
864864

865-
printf("[*] creating RW/System/pm.dat\n");
865+
sys_output("[*] creating RW/System/pm.dat\n");
866866
snprintf(path, sizeof(path), "%s/RW/System/pm.dat", root);
867867

868868
uint8_t pm[1 << 16] = { 0 };
@@ -875,8 +875,8 @@ int main(int argc, char* argv[])
875875

876876
if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_PATCH)
877877
{
878-
printf("[*] minimum fw version required: %s\n", min_version);
878+
sys_output("[*] minimum fw version required: %s\n", min_version);
879879
}
880880

881-
printf("[*] done!\n");
881+
sys_output("[*] done!\n");
882882
}

0 commit comments

Comments
 (0)