forked from hippie68/sfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfo.cpp
1128 lines (1042 loc) · 37.5 KB
/
sfo.cpp
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Reads a file to print or modify its SFO parameters.
* Supported file types:
* - PS4 param.sfo (print and modify)
* - PS4 disc param.sfo (print only)
* - PS4 PKG (print only)
* Made with info from https://www.psdevwiki.com/ps4/Param.sfo.
* Get updates and Windows binaries at https://github.com/hippie68/sfo. */
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef uint32_t u8chr_t;
//https://dev.to/rdentato/utf-8-strings-in-c-1-3-42a4
//https://dev.to/rdentato/utf-8-strings-in-c-2-3-3kp1
//https://dev.to/rdentato/utf-8-strings-in-c-3-3-2pc7
u8chr_t u8encode(uint32_t codepoint)
{
u8chr_t c = codepoint;
if (codepoint > 0x7F) {
c = (codepoint & 0x000003F)
| (codepoint & 0x0000FC0) << 2
| (codepoint & 0x003F000) << 4
| (codepoint & 0x01C0000) << 6;
if (codepoint < 0x0000800) c |= 0x0000C080;
else if (codepoint < 0x0010000) c |= 0x00E08080;
else c |= 0xF0808080;
}
return c;
}
uint32_t u8decode(u8chr_t c)
{
uint32_t mask;
if (c > 0x7F) {
mask = (c <= 0x00EFBFBF)? 0x000F0000 : 0x003F0000 ;
c = ((c & 0x07000000) >> 6) |
((c & mask ) >> 4) |
((c & 0x00003F00) >> 2) |
(c & 0x0000003F);
}
return c;
}
// Replacement function for byteswap.h's bswap_32
uint32_t bswap_32(uint32_t val) {
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0x00FF00FF );
return (val << 16) | (val >> 16);
}
uint16_t bswap_16(uint16_t val) {
uint16_t hi = (val & 0xff00);
uint16_t lo = (val & 0xff);
uint16_t y = (lo << 8);
y |= (hi >> 8);
return y;
}
// Global variables
const char program_version[] = "1.03 (March 4, 2022)";
char *program_name;
char *query_string;
FILE *file;
int option_debug;
int option_decimal;
int option_force;
int option_new_file;
int option_verbose;
// Complete param.sfo file structure, 4 parts:
// 1. header
// 2. all entries
// 3. key_table.content (with trailing 4-byte alignment)
// 4. data_table.content
struct header {
uint32_t magic;
uint32_t version;
uint32_t key_table_offset;
uint32_t data_table_offset;
uint32_t entries_count;
} header;
struct index_table_entry {
uint16_t key_offset;
uint16_t param_fmt;
uint32_t param_len;
uint32_t param_max_len;
uint32_t data_offset;
} *entries;
struct table {
unsigned int size;
char *content;
} key_table, data_table;
enum cmd {cmd_add, cmd_delete, cmd_edit, cmd_set};
struct command {
enum cmd cmd;
struct {
char *type;
char *key;
char *value;
} param;
} *commands;
int commands_count;
void load_header(FILE *file) {
if (fread(&header, sizeof(struct header), 1, file) != 1) {
fprintf(stderr, "Could not read header.\n");
exit(1);
}
}
void load_entries(FILE *file) {
unsigned int size = sizeof(struct index_table_entry) * header.entries_count;
entries = (struct index_table_entry *) malloc(size);
if (entries == NULL) {
fprintf(stderr, "Could not allocate %u bytes of memory for index table.\n",
size);
exit(1);
}
if (size && fread(entries, size, 1, file) != 1) {
fprintf(stderr, "Could not read index table entries.\n");
exit(1);
}
}
void load_key_table(FILE *file) {
key_table.size = header.data_table_offset - header.key_table_offset;
key_table.content = (char*) malloc(key_table.size);
if (key_table.content == NULL) {
fprintf(stderr, "Could not allocate %u bytes of memory for key table.\n",
key_table.size);
exit(1);
}
if (key_table.size && fread(key_table.content, key_table.size, 1, file) != 1) {
fprintf(stderr, "Could not read key table.\n");
exit(1);
}
}
void load_data_table(FILE *file) {
if (header.entries_count) {
data_table.size =
(entries[header.entries_count - 1].data_offset +
entries[header.entries_count - 1].param_max_len);
} else {
data_table.size = 0; // For newly created, empty param.sfo files
}
data_table.content = (char*) malloc(data_table.size);
if (data_table.content == NULL) {
fprintf(stderr, "Could not allocate %u bytes of memory for data table.\n",
data_table.size);
exit(1);
}
if (data_table.size && fread(data_table.content, data_table.size, 1, file) != 1) {
fprintf(stderr, "Could not read data table.\n");
exit(1);
}
}
// Debug function that prints a byte array's content in hex editor style
void hexprint(char *array, int array_len) {
int offset = 0;
fprintf(stderr, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
while (offset < array_len) {
// Print starting offset
fprintf(stderr, "%04x ", offset);
// Print bytes
for (int i = 0; i < 16 && offset + i < array_len; i++) {
fprintf(stderr, "%02x ", array[offset + i]);
}
int remaining_bytes = array_len - offset;
if (remaining_bytes < 16) {
for (int i = 0; i < 16 - remaining_bytes; i++) {
fprintf(stderr, " ");
}
}
// Print characters
for (int i = 0; i < 16 && offset + i < array_len; i++) {
if (isprint(array[offset + i])) {
fprintf(stderr, "%c", array[offset + i]);
} else {
fprintf(stderr, ".");
}
}
fprintf(stderr, "\n");
offset += 16;
}
if (array_len > 64) fprintf(stderr, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
}
// Debug function
void print_header(void) {
fprintf(stderr, "Header:\n");
fprintf(stderr, "Size: %d (0x%04X)\n", sizeof(header), sizeof(header));
uint32_t dude1 = bswap_32(header.magic);
fprintf(stderr, ".magic: %u (0x%08X) - (0x%08X)\n", header.magic, header.magic, dude1);
uint32_t dude2 = bswap_32(header.version);
fprintf(stderr, ".version: %u (0x%08X) - (0x%08X)\n", header.version, header.version, dude2);
uint32_t dude3 = bswap_32(header.key_table_offset);
fprintf(stderr, ".key_table_offset: %u (0x%08X) - (0x%08X)\n", header.key_table_offset, header.key_table_offset, dude3);
uint32_t dude4 = bswap_32(header.data_table_offset);
fprintf(stderr, ".data_table_offset: %u (0x%08X) - (0x%08X)\n", header.data_table_offset, header.data_table_offset, dude4);
uint32_t dude5 = bswap_32(header.entries_count);
fprintf(stderr, ".entries_count: %u (0x%08X) - (0x%08X)\n", header.entries_count, header.entries_count, dude5);
fprintf(stderr, "\n");
}
// Debug function
void print_entries(void) {
fprintf(stderr, "Index table:\n");
fprintf(stderr, "Size: %d (0x%04X)\n", sizeof(struct index_table_entry) * header.entries_count, sizeof(struct index_table_entry) * header.entries_count);
for (int i = 0; i < header.entries_count; i++) {
fprintf(stderr, "Entry %d: (0x%04X)\n", i, i);
uint16_t dude6 = bswap_16(entries[i].key_offset);
fprintf(stderr, " .key_offset: %u (0x%04X) - (0x%04X)-> \"%s\"\n", entries[i].key_offset, entries[i].key_offset, dude6,
&key_table.content[entries[i].key_offset]);
uint16_t dude7 = bswap_16(entries[i].param_fmt);
fprintf(stderr, " .param_fmt: %u (0x%04X) - (0x%04X)\n", entries[i].param_fmt, entries[i].param_fmt, dude7);
uint32_t dude8 = bswap_32(entries[i].param_len);
fprintf(stderr, " .param_len: %u (0x%08x) - (0x%08x)\n", entries[i].param_len, entries[i].param_len, dude8);
uint32_t dude9 = bswap_32(entries[i].param_max_len);
fprintf(stderr, " .param_max_len: %u (0x%08x) - (0x%08x)\n", entries[i].param_max_len, entries[i].param_max_len, dude9);
uint32_t dude10 = bswap_32(entries[i].data_offset);
fprintf(stderr, " .data_offset: %u (0x%08x) - (0x%08x)-> ", entries[i].data_offset, entries[i].data_offset, dude10);
switch (entries[i].param_fmt) {
case 516:
case 1024:
fprintf(stderr, "\"%s\"\n", &data_table.content[entries[i].data_offset]);
break;
case 1028:
;
uint32_t *integer = (uint32_t *) &data_table.content[entries[i].data_offset];
fprintf(stderr, "0x%08x\n", *integer);
break;
}
}
fprintf(stderr, "\n");
}
// Debug function
void print_key_table(void) {
fprintf(stderr, "Key table:\n");
fprintf(stderr, "Size: %d (0x%04x)\n", key_table.size, key_table.size);
if (key_table.size) {
fprintf(stderr, "Content:\n");
for (int i = 0; i < key_table.size; i++) {
if (isprint(key_table.content[i])) {
fprintf(stderr, "%c", key_table.content[i]);
} else {
fprintf(stderr, "'\\%d'", key_table.content[i]);
}
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
// Debug function
void print_data_table(void) {
fprintf(stderr, "Data table:\n");
fprintf(stderr, "Size: %d (0x%04x)\n", data_table.size, data_table.size);
if (data_table.size) {
fprintf(stderr, "Content:\n");
hexprint(data_table.content, data_table.size);
}
fprintf(stderr, "\n");
}
// Saves all 4 param.sfo parts to a param.sfo file
void save_to_file(char *file_name) {
FILE *file = fopen(file_name, "wb");
if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\" in write mode.\n", file_name);
exit(1);
}
// Adjust header's table offsets before saving
header.key_table_offset = sizeof(struct header) +
sizeof(struct index_table_entry) * header.entries_count;
header.data_table_offset = header.key_table_offset + key_table.size;
if (fwrite(&header, sizeof(struct header), 1, file) != 1) {
fprintf(stderr, "Could not write header to file \"%s\".\n", file_name);
exit(1);
}
if (header.entries_count && fwrite(entries,
sizeof(struct index_table_entry) * header.entries_count, 1, file) != 1) {
fprintf(stderr, "Could not write index table to file \"%s\".\n", file_name);
exit(1);
}
if (key_table.size && fwrite(key_table.content, key_table.size, 1, file) != 1) {
fprintf(stderr, "Could not write key table to file \"%s\".\n", file_name);
exit(1);
}
if (data_table.size && fwrite(data_table.content, data_table.size, 1, file) != 1) {
fprintf(stderr, "Could not write data table to file \"%s\".\n", file_name);
exit(1);
}
fclose(file);
}
// Prints a single parameter
int print_param(char *key) {
for (int i = 0; i < header.entries_count; i ++) {
if (!strcmp(key, &key_table.content[entries[i].key_offset])) {
switch(entries[i].param_fmt) {
case 516:
case 1024:
printf("%s\n", &data_table.content[entries[i].data_offset]);
return 0;
case 1028:
;
uint32_t *integer = (uint32_t *) &data_table.content[entries[i].data_offset];
if (option_decimal) {
printf("%u\n", *integer);
} else {
printf("0x%08x\n", *integer);
}
return 0;
}
}
}
return 1; // Parameter not found
}
// Prints all parameters
void print_params(void) {
uint32_t *integer;
if (option_verbose) {
char version[6] = {0};
snprintf(version, 6, "%04x", header.version);
version[4] = version[3];
version[3] = version[2];
version[2] = '.';
if (version[0] == '0') {
printf("Param.sfo version: %s\n", &version[1]);
} else {
printf("Param.sfo version: %s\n", version);
}
printf("Number of parameters: %d\n", header.entries_count);
}
for (int i = 0; i < header.entries_count; i++) {
switch (entries[i].param_fmt) {
case 516:
if (option_verbose) {
printf("[%d] %s=\"%s\" (%d/%d bytes UTF-8 string)\n", i,
&key_table.content[entries[i].key_offset],
&data_table.content[entries[i].data_offset],
entries[i].param_len, entries[i].param_max_len);
}
else {
printf("%s=%s\n", &key_table.content[entries[i].key_offset], &data_table.content[entries[i].data_offset]);
}
break;
case 1024:
if (option_verbose) {
printf("[%d] %s=\"%s\" (%d/%d bytes UTF-8 special mode string)\n", i,
&key_table.content[entries[i].key_offset],
&data_table.content[entries[i].data_offset],
entries[i].param_len, entries[i].param_max_len);
} else {
printf("%s=%s\n", &key_table.content[entries[i].key_offset],
&data_table.content[entries[i].data_offset]);
}
break;
case 1028:
integer = (uint32_t *) &data_table.content[entries[i].data_offset];
if (option_verbose) {
if (option_decimal) {
printf("[%d] %s=%u (%d/%d bytes unsigned integer)\n", i,
&key_table.content[entries[i].key_offset], *integer,
entries[i].param_len, entries[i].param_max_len);
} else {
printf("[%d] %s=0x%08x (%d/%d bytes unsigned integer)\n", i,
&key_table.content[entries[i].key_offset], *integer,
entries[i].param_len, entries[i].param_max_len);
}
} else {
if (option_decimal) {
printf("%s=%u\n", &key_table.content[entries[i].key_offset], *integer);
} else {
printf("%s=0x%08x\n", &key_table.content[entries[i].key_offset], *integer);
}
}
break;
}
}
}
// Replacement for realloc() that exits on error
static inline void *_realloc(void *ptr, unsigned int size) {
if (size == 0) { // Avoid double free (which is implementation-dependant)
if (ptr) free(ptr);
ptr = NULL;
} else if ((ptr = realloc(ptr, size)) == NULL) {
fprintf(stderr, "Failed to reallocate memory.\n");
exit(1);
}
return ptr;
}
// Resizes the data table, starting at specified offset
void expand_data_table(int offset, int additional_size) {
data_table.size += additional_size;
data_table.content = (char*) _realloc(data_table.content, data_table.size);
// Move higher indexed data to make room for new data
for (int i = data_table.size - 1; i >= offset + additional_size; i--) {
data_table.content[i] = data_table.content[i - additional_size];
}
// Set new memory to zero
memset(&data_table.content[offset], 0, additional_size);
}
// Returns a parameter's index table position
int get_index(char *key) {
for (int i = 0; i < header.entries_count; i++) {
if (strcmp(key, &key_table.content[entries[i].key_offset]) == 0) {
return i;
}
}
return -1;
}
// Edits a parameter in memory
void edit_param(char *key, char *value, int no_fail) {
int index = get_index(key);
if (index < 0) { // Parameter not found
if (no_fail) {
return;
} else {
fprintf(stderr, "Could not edit \"%s\": parameter not found.\n", key);
exit(1);
}
}
switch (entries[index].param_fmt) {
case 516: // String
case 1024: {// Special mode string
entries[index].param_len = strlen(value) + 1;
// Enlarge data table if new string is longer than allowed
int diff = entries[index].param_len - entries[index].param_max_len;
if (diff > 0) {
int offset = entries[index].data_offset + entries[index].param_max_len;
entries[index].param_max_len = entries[index].param_len;
// 4-byte alignment
while (entries[index].param_max_len % 4) {
entries[index].param_max_len++;
diff++;
}
expand_data_table(offset, diff);
// Adjust follow-up index table entries' data offsets
for (int i = index + 1; i < header.entries_count; i++) {
entries[i].data_offset += diff;
}
}
// Overwrite old data with zeros
memset(&data_table.content[entries[index].data_offset], 0,
entries[index].param_max_len);
// Save new string to data table
snprintf(&data_table.content[entries[index].data_offset],
entries[index].param_max_len, "%s", value);
break;
}
case 1028: // Integer
;
uint32_t integer = strtoul(value, NULL, 0);
memcpy(&data_table.content[entries[index].data_offset], &integer, 4);
break;
}
}
// Pad a table to obey the 4-byte alignment rule
// Currently only used for the key table
void pad_table(struct table *table) {
// Remove all trailing zeros
while (table->size > 0 && table->content[table->size - 1] == '\0') {
table->size--;
}
if (table->size) table->size++; // Re-add 1 zero if there are strings left
table->content = (char*) _realloc(table->content, table->size);
// Pad table with zeros
while (table->size % 4) {
table->size++;
table->content = (char*) _realloc(table->content, table->size);
table->content[table->size - 1] = '\0';
}
}
// Deletes a parameter from memory
void delete_param(char *key, int no_fail) {
int index = get_index(key);
if (index < 0) { // Parameter not found
if (no_fail) {
return;
} else {
fprintf(stderr, "Could not delete \"%s\": parameter not found.\n", key);
exit(1);
}
}
// Delete parameter from key table
for (int i = entries[index].key_offset; i < key_table.size - strlen(key) - 1; i++) {
key_table.content[i] = key_table.content[i + strlen(key) + 1];
}
// Resize key table
key_table.size -= strlen(key) + 1;
key_table.content = (char*) _realloc(key_table.content, key_table.size);
pad_table(&key_table);
// Delete parameter from data table
for (int i = entries[index].data_offset; i < data_table.size - entries[index].param_max_len; i++) {
data_table.content[i] = data_table.content[i + entries[index].param_max_len];
}
// Resize data table
data_table.size -= entries[index].param_max_len;
if (data_table.size) {
data_table.content = (char*) _realloc(data_table.content, data_table.size);
} else {
free(data_table.content);
data_table.content = NULL;
}
// Delete parameter from index table
int param_max_len = entries[index].param_max_len;
for (int i = index; i < header.entries_count - 1; i++) {
entries[i] = entries[i + 1];
entries[i].key_offset -= strlen(key) + 1;
entries[i].data_offset -= param_max_len;
}
// Resize index table
header.entries_count--;
if (header.entries_count) {
entries = (struct index_table_entry *) _realloc(entries, sizeof(struct index_table_entry) * header.entries_count);
}
else {
free(entries);
entries = NULL;
}
}
// Checks if key is reserved and returns its default length
int get_reserved_string_len(char *key) {
int len = 0;
if (!strcmp(key, "CATEGORY") || !strcmp(key, "FORMAT")) {
len = 4;
} else if (!strcmp(key, "APP_VER") || !strcmp(key, "CONTENT_VER") || !strcmp(key, "VERSION")) {
len = 8;
} else if (!strcmp(key, "INSTALL_DIR_SAVEDATA") || !strcmp(key, "TITLE_ID")) {
len = 12;
} else if (!strcmp(key, "SERVICE_ID_ADDCONT_ADD_1") ||
!strcmp(key, "SERVICE_ID_ADDCONT_ADD_2") ||
!strcmp(key, "SERVICE_ID_ADDCONT_ADD_3") ||
!strcmp(key, "SERVICE_ID_ADDCONT_ADD_4") ||
!strcmp(key, "SERVICE_ID_ADDCONT_ADD_5") ||
!strcmp(key, "SERVICE_ID_ADDCONT_ADD_6") ||
!strcmp(key, "SERVICE_ID_ADDCONT_ADD_7")) {
len = 20;
} else if (!strcmp(key, "CONTENT_ID")) {
len = 48;
} else if (!strcmp(key, "PROVIDER") || !strcmp(key, "TITLE") ||
!strcmp(key, "PROVIDER_00") || !strcmp(key, "TITLE_00") ||
!strcmp(key, "PROVIDER_01") || !strcmp(key, "TITLE_01") ||
!strcmp(key, "PROVIDER_02") || !strcmp(key, "TITLE_02") ||
!strcmp(key, "PROVIDER_03") || !strcmp(key, "TITLE_03") ||
!strcmp(key, "PROVIDER_04") || !strcmp(key, "TITLE_04") ||
!strcmp(key, "PROVIDER_05") || !strcmp(key, "TITLE_05") ||
!strcmp(key, "PROVIDER_06") || !strcmp(key, "TITLE_06") ||
!strcmp(key, "PROVIDER_07") || !strcmp(key, "TITLE_07") ||
!strcmp(key, "PROVIDER_08") || !strcmp(key, "TITLE_08") ||
!strcmp(key, "PROVIDER_09") || !strcmp(key, "TITLE_09") ||
!strcmp(key, "PROVIDER_10") || !strcmp(key, "TITLE_10") ||
!strcmp(key, "PROVIDER_11") || !strcmp(key, "TITLE_11") ||
!strcmp(key, "PROVIDER_12") || !strcmp(key, "TITLE_12") ||
!strcmp(key, "PROVIDER_13") || !strcmp(key, "TITLE_13") ||
!strcmp(key, "PROVIDER_14") || !strcmp(key, "TITLE_14") ||
!strcmp(key, "PROVIDER_15") || !strcmp(key, "TITLE_15") ||
!strcmp(key, "PROVIDER_16") || !strcmp(key, "TITLE_16") ||
!strcmp(key, "PROVIDER_17") || !strcmp(key, "TITLE_17") ||
!strcmp(key, "PROVIDER_18") || !strcmp(key, "TITLE_18") ||
!strcmp(key, "PROVIDER_19") || !strcmp(key, "TITLE_19") ||
!strcmp(key, "PROVIDER_20") || !strcmp(key, "TITLE_20") ||
!strcmp(key, "TITLE_21") || !strcmp(key, "TITLE_22") ||
!strcmp(key, "TITLE_23") || !strcmp(key, "TITLE_24") ||
!strcmp(key, "TITLE_25") || !strcmp(key, "TITLE_26") ||
!strcmp(key, "TITLE_27") || !strcmp(key, "TITLE_28") ||
!strcmp(key, "TITLE_29")) {
len = 128;
} else if (!strcmp(key, "PUBTOOLINFO") || !strcmp(key, "PS3_TITLE_ID_LIST_FOR_BOOT") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_1") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_2") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_3") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_4") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_5") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_6") ||
!strcmp(key, "SAVE_DATA_TRANSFER_TITLE_ID_LIST_7")) {
len = 512;
}
return len;
}
// Adds a new parameter to memory
void add_param(char *type, char *key, char *value, int no_fail) {
struct index_table_entry new_entry = {0};
int new_index = 0;
// Get new entry's .param_len and .param_max_len
if (!strcmp(type, "str")) {
new_entry.param_fmt = 516;
new_entry.param_max_len = get_reserved_string_len(key);
new_entry.param_len = strlen(value) + 1;
if (new_entry.param_max_len < new_entry.param_len) {
new_entry.param_max_len = new_entry.param_len;
// 4-byte alignment
while (new_entry.param_max_len % 4) {
new_entry.param_max_len++;
}
}
} else { // "int"
new_entry.param_fmt = 1028;
new_entry.param_len = 4;
new_entry.param_max_len = 4;
}
// Get new entry's index and offsets
for (int i = 0; i < header.entries_count; i++) {
int result = strcmp(key, &key_table.content[entries[i].key_offset]);
if (result == 0) { // Parameter already exists
if (no_fail) {
return;
} else {
fprintf(stderr, "Could not add \"%s\": parameter already exists.\n", key);
exit(1);
}
} else if (result < 0) {
new_index = i;
new_entry.key_offset = entries[i].key_offset;
new_entry.data_offset = entries[i].data_offset;
break;
} else if (i == header.entries_count - 1) {
new_index = i + 1;
new_entry.key_offset = entries[i].key_offset +
strlen(&key_table.content[entries[i].key_offset]) + 1;
new_entry.data_offset = entries[i].data_offset +
entries[i].param_max_len;
break;
}
}
// Make room for the new index table entry by moving the old ones
header.entries_count++;
entries = (struct index_table_entry *) _realloc(entries, sizeof(struct index_table_entry) * header.entries_count);
for (int i = header.entries_count - 1; i > new_index; i--) {
entries[i] = entries[i - 1];
entries[i].key_offset += strlen(key) + 1;
entries[i].data_offset += new_entry.param_max_len;
}
// Insert new index table entry
memcpy(&entries[new_index], &new_entry, sizeof(struct index_table_entry));
// Resize key table
key_table.size += strlen(key) + 1;
key_table.content = (char *) _realloc(key_table.content, key_table.size);
// Move higher indexed keys to make room for new key
for (int i = key_table.size - 1; i > new_entry.key_offset + strlen(key); i--) {
key_table.content[i] = key_table.content[i - strlen(key) - 1];
}
// Insert new key
memcpy(&key_table.content[new_entry.key_offset], key, strlen(key) + 1);
pad_table(&key_table);
// Resize data table
expand_data_table(new_entry.data_offset, new_entry.param_max_len);
// Insert new data
if (!strcmp(type, "str")) {
memset(&data_table.content[entries[new_index].data_offset], 0,
new_entry.param_len); // Overwrite whole space with zeros first
memcpy(&data_table.content[entries[new_index].data_offset],
value, strlen(value) + 1); // Then copy new value
} else if (!strcmp(type, "int")) {
uint32_t new_value = strtoul(value, NULL, 0);
memcpy(&data_table.content[entries[new_index].data_offset],
&new_value, 4);
}
}
// Overwrites an existing parameter or creates a new one
void set_param(char *type, char *key, char *value) {
delete_param(key, 1);
add_param(type, key, value, 1);
}
// Returns a filename without its path
char *basename(char *filename) {
#if defined(_WIN32) || defined(_WIN64)
char *base = strrchr(filename, '\\');
#else
char *base = strrchr(filename, '/');
#endif
if (base != NULL && strlen(base) > 1) {
base++;
return base;
} else {
return filename;
}
}
// Prints usage information and exits with exit_code
void print_usage(int exit_code) {
FILE *output;
if (exit_code) {
output = stderr;
} else {
output = stdout;
}
fprintf(output,
"Usage: %s [OPTIONS] FILE\n\n"
"Reads a file to print or modify its SFO parameters.\n"
"Supported file types:\n"
" - PS4 param.sfo (print and modify)\n"
" - PS4 disc param.sfo (print only)\n"
" - PS4 PKG (print only)\n\n"
"The modification options (-a/--add, -d/--delete, -e/--edit, -s/--set) can be\n"
"used multiple times. Modifications are done in memory first, in the order in\n"
"which they appear in the program's command line arguments.\n"
"If any modification fails, all changes are discarded and no data is written:\n\n"
" Modification Fail condition\n"
" --------------------------------------\n"
" Add Parameter already exists\n"
" Delete Parameter not found\n"
" Edit Parameter not found\n"
" Set None\n\n"
"Options:\n"
" -a, --add TYPE PARAMETER VALUE Add a new parameter, not overwriting existing\n"
" data. TYPE must be either \"int\" or \"str\".\n\n"
" -d, --delete PARAMETER Delete specified parameter.\n"
" --debug Print debug information.\n"
" --decimal Display integer values as decimal numerals.\n\n"
" -e, --edit PARAMETER VALUE Change specified parameter's value.\n\n"
" -f, --force Do not abort when modifications fail. Make\n"
" option --new-file overwrite existing files.\n\n"
" -h, --help Print usage information and quit.\n"
" --new-file If FILE (see above) does not exist, create a\n"
" new param.sfo file of the same name.\n\n"
" -o, --output-file OUTPUT_FILE Save the final data to a new file of type\n"
" \"param.sfo\", overwriting existing files.\n\n"
" -q, --query PARAMETER Print a parameter's value and quit.\n"
" If the parameter exists, the exit code is 0.\n\n"
" -s, --set TYPE PARAMETER VALUE Set a parameter, whether it exists or not,\n"
" overwriting existing data.\n\n"
" -v, --verbose Increase verbosity.\n"
" --version Print version information and quit.\n\n"
,basename(program_name));
exit(exit_code);
}
void print_version(void) {
printf("SFO v%s\n", program_version);
printf("https://github.com/hippie68/sfo\n");
}
// Finds the param.sfo's offset inside a PS4 PKG file
long int get_ps4_pkg_offset() {
uint32_t pkg_table_offset;
uint32_t pkg_file_count;
fseek(file, 0x00C, SEEK_SET);
fread(&pkg_file_count, 4, 1, file);
fseek(file, 0x018, SEEK_SET);
fread(&pkg_table_offset, 4, 1, file);
pkg_file_count = bswap_32(pkg_file_count);
pkg_table_offset = bswap_32(pkg_table_offset);
struct pkg_table_entry {
uint32_t id;
uint32_t filename_offset;
uint32_t flags1;
uint32_t flags2;
uint32_t offset;
uint32_t size;
uint64_t padding;
} pkg_table_entry[pkg_file_count];
fseek(file, pkg_table_offset, SEEK_SET);
fread(pkg_table_entry, sizeof (struct pkg_table_entry), pkg_file_count, file);
for (int i = 0; i < pkg_file_count; i++) {
if (pkg_table_entry[i].id == 1048576) { // param.sfo ID
return bswap_32(pkg_table_entry[i].offset);
}
}
fprintf(stderr, "Could not find a param.sfo file inside the PS4 PKG.\n");
fclose(file);
exit(1);
}
// Removes the leftmost argument from argv; decrements argc
int shift(int *pargc, char **pargv[]) {
// Exit and print usage information if there is nothing left to shift
if (*pargc <= 0) {
fprintf(stderr, "A required argument is missing.\n");
print_usage(1);
exit(1);
}
(*pargv)++;
(*pargc)--;
return 0;
}
// Converts a string to uppercase
void toupper_string(char *string) {
while (*string) {
string[0] = toupper(string[0]);
string++;
}
}
// Frees all previously malloc'ed pointers; used for memory leak tests
void clean_exit(void) {
if (commands) free(commands);
if (entries) free(entries);
if (key_table.content) free(key_table.content);
if (data_table.content) free(data_table.content);
if (file) fclose(file);
}
// Creates an empty param.sfo file
void create_param_sfo(char *file_name) {
header.magic = 1179865088;
header.version = 257;
header.key_table_offset = 20;
header.data_table_offset = 20;
header.entries_count = 0;
save_to_file(file_name);
}
int main(int argc, char *argv[]) {
atexit(clean_exit);
char *input_file_name = NULL;
char *output_file_name = NULL;
// Parse command line arguments
program_name = argv[0];
shift(&argc, &argv);
while (argc) {
// Parse file names
if (argv[0][0] != '-') {
if (input_file_name == NULL) {
input_file_name = argv[0];
} else {
fprintf(stderr, "Only 1 input file is allowed. Conflicting file names:\n"
" \"%s\"\n \"%s\"\n", input_file_name, argv[0]);
print_usage(1);
}
// Parse options
} else if (!strcmp(argv[0], "-a") || !strcmp(argv[0], "--add")) {
commands = (struct command *) _realloc(commands, sizeof(struct command) * (commands_count + 1));
commands[commands_count].cmd = cmd_add;
shift(&argc, &argv);
// TYPE
if (strcmp(argv[0], "str") && strcmp(argv[0], "int")) {
fprintf(stderr, "Option --add: TYPE must be \"int\" or \"str\".\n");
print_usage(1);
}
commands[commands_count].param.type = argv[0];
shift(&argc, &argv);
// NAME
toupper_string(argv[0]);
commands[commands_count].param.key = argv[0];
shift(&argc, &argv);
// VALUE
commands[commands_count].param.value = argv[0];
commands_count++;
} else if (!strcmp(argv[0], "--new-file")) {
option_new_file = 1;
} else if (!strcmp(argv[0], "-d") || !strcmp(argv[0], "--delete")) {
commands = (struct command *) _realloc(commands, sizeof(struct command) * (commands_count + 1));
commands[commands_count].cmd = cmd_delete;
shift(&argc, &argv);
// NAME
toupper_string(argv[0]);
commands[commands_count].param.key = argv[0];
commands_count++;
} else if (!strcmp(argv[0], "--debug")) {
option_debug = 1;
} else if (!strcmp(argv[0], "--decimal")) {
option_decimal = 1;
} else if (!strcmp(argv[0], "-e") || !strcmp(argv[0], "--edit")) {
commands = (struct command *) _realloc(commands, sizeof(struct command) * (commands_count + 1));
commands[commands_count].cmd = cmd_edit;
shift(&argc, &argv);
// NAME
toupper_string(argv[0]);
commands[commands_count].param.key = argv[0];
shift(&argc, &argv);
// VALUE
commands[commands_count].param.value = argv[0];
commands_count++;
} else if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--force")) {
option_force = 1;
} else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
print_usage(0);
} else if (!strcmp(argv[0], "-o") || !strcmp(argv[0], "--output-file")) {
shift(&argc, &argv);
output_file_name = argv[0];
} else if (!strcmp(argv[0], "-q") || !strcmp(argv[0], "--query")) {
shift(&argc, &argv);
toupper_string(argv[0]);
if (!query_string) {
query_string = argv[0];
} else {
fprintf(stderr, "Only 1 search is allowed. Conflicting search strings:\n"
" \"%s\"\n, \"%s\"\n.\n", query_string, argv[0]);
exit(1);
}
} else if (!strcmp(argv[0], "-s") || !strcmp(argv[0], "--set")) {
commands = (struct command *) _realloc(commands, sizeof(struct command) * (commands_count + 1));
commands[commands_count].cmd = cmd_set;
shift(&argc, &argv);
// TYPE
if (strcmp(argv[0], "str") && strcmp(argv[0], "int")) {
fprintf(stderr, "Option --set: TYPE must be \"int\" or \"str\".\n");
print_usage(1);
}
commands[commands_count].param.type = argv[0];
shift(&argc, &argv);
// NAME
toupper_string(argv[0]);
commands[commands_count].param.key = argv[0];
shift(&argc, &argv);
// VALUE
commands[commands_count].param.value = argv[0];
commands_count++;
} else if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "--verbose")) {
option_verbose = 1;
} else if (!strcmp(argv[0], "--version")) {
print_version();
exit(0);
} else {
fprintf(stderr, "Unknown option: %s\n", argv[0]);
print_usage(1);
}
shift(&argc, &argv);
}
// DEBUG: Print parsing results
if (option_debug) {
fprintf(stderr, "Command line parsing results:\n\n");
if (input_file_name == NULL) { // printf + null pointer = undefined behavior
fprintf(stderr, "input_file_name: NULL\n");
} else {
fprintf(stderr, "input_file_name: \"%s\"\n", input_file_name);
}
if (output_file_name == NULL) {
fprintf(stderr, "output_file_name: NULL\n");
} else {
fprintf(stderr, "output_file_name: \"%s\"\n", output_file_name);
}
fprintf(stderr, "option_debug: %d\n", option_debug);
fprintf(stderr, "option_decimal: %d\n", option_decimal);
fprintf(stderr, "option_force: %d\n", option_force);
fprintf(stderr, "option_new_file: %d\n", option_new_file);
fprintf(stderr, "option_verbose: %d\n", option_verbose);
if (query_string == NULL) {
fprintf(stderr, "query_string: NULL\n");
} else {
fprintf(stderr, "query_string: \"%s\"\n", query_string);
}
fprintf(stderr, "commands_count: %d\n", commands_count);
for (int i = 0; i < commands_count; i++) {
fprintf(stderr, "Command %d:\n", i);
fprintf(stderr, " .cmd: %d (", commands[i].cmd);
char *cmd;
switch (commands[i].cmd) {
case 0: cmd = (char*)"add"; break;
case 1: cmd = (char*)"delete"; break;
case 2: cmd = (char*)"edit"; break;
case 3: cmd = (char*)"set"; break;