/
iso.c
4536 lines (3437 loc) · 131 KB
/
iso.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
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
/*
(c) 2011 Hermes/Estwald <www.elotrolado.net>
IrisManager (HMANAGER port) (c) 2011 D_Skywalk <http://david.dantoine.org>
HMANAGER4 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.
HMANAGER4 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 HMANAGER4. If not, see <http://www.gnu.org/licenses/>.
*/
/*
(c) 2013 Estwald/Hermes <www.elotrolado.net>
MAKEPS3ISO, EXTRACTPS3ISO and PATCHPS3ISO 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.
MAKEPS3ISO, EXTRACTPS3ISO and PATCHPS3ISO 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 MAKEPS3ISO, EXTRACTPS3ISO and PATCHPS3ISO. If not, see <http://www.gnu.org/licenses/>.
*/
#include "iso.h"
#include "main.h"
#include "utils.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <sys/file.h>
//#define ALIGNED32SECTORS 1
#define NOPS3_UPDATE 1
#define SUCCESS 0
#define FAILED -1
#define ERROR_OUT_OF_MEMORY -1
#define ERROR_TOO_MUCH_DIR_ENTRIES -444
#define ERROR_FILE_NAME_TOO_LONG -555
#define ERROR_OPENING_INPUT_FILE -666
#define ERROR_READING_INPUT_FILE -668
#define ERROR_WRITING_OUTPUT_FILE -667
#define ERROR_INPUT_FILE_NOT_EXISTS -669
#define ERROR_CREATING_SPLIT_FILE -777
#define ERROR_ABORTED_BY_USER -999
#define TICKS_PER_SEC 0x4c1a6bdULL
#define IS_DIRECTORY 1
static u16 wstring[1024];
static char temp_string[1024];
static inline u64 get_ticks(void)
{
u64 ticks;
asm volatile("mftb %0" : "=r" (ticks));
return ticks;
}
#define SWAP16(x) (x)
#define SWAP32(x) (x)
#define SECTOR_FILL 2047
#define SECTOR_SIZE 2048
#define SECTOR_SIZELL 2048LL
int nfiles = 0;
static int get_input_char()
{
pad_last_time = 0;
while(true)
{
tiny3d_Flip();
ps3pad_read();
tiny3d_Project2D();
DbgDraw();
if(new_pad & BUTTON_CROSS_) return 1; // YES
if(new_pad & BUTTON_TRIANGLE) return -1; // NO
if(new_pad & BUTTON_CIRCLE_) return 0; // CANCEL
}
return 0;
}
static int get_input_abort()
{
pad_last_time = 0;
ps3pad_read();
if(new_pad & (BUTTON_TRIANGLE | BUTTON_CIRCLE_)) return 1;
return 0;
}
static void press_button_to_continue()
{
DPrintf("\n\n > > > Press a button to continue < < <\n\n");
get_input_char();
DCls();
cls(); tiny3d_Flip();
}
int UTF8_to_UTF16(u8 *stb, u16 *stw);
int UTF16_to_UTF8(u16 *stw, u8 *stb);
static void utf8_to_ansiname(char *utf8, char *ansi, int len)
{
u8 *ch= (u8 *) utf8;
u8 c;
int is_space = 1;
char *a = ansi;
*ansi = 0;
while(*ch != 0 && len > 0)
{
// 3, 4 bytes utf-8 code
if(((*ch & 0xF1) == 0xF0 || (*ch & 0xF0) == 0xe0) && (*(ch+1) & 0xc0) == 0x80)
{
if(!is_space)
{
*ansi++=' '; // ignore
len--;
is_space = 1;
}
ch += 2+1*((*ch & 0xF1) == 0xF0);
}
else
// 2 bytes utf-8 code
if((*ch & 0xE0) == 0xc0 && (*(ch+1) & 0xc0) == 0x80)
{
c = (((*ch & 3)<<6) | (*(ch+1) & 63));
if(c >= 0xC0 && c <= 0xC5) c='A';
else if(c == 0xc7) c = 'C';
else if(c >= 0xc8 && c <= 0xcb) c = 'E';
else if(c >= 0xcc && c <= 0xcf) c = 'I';
else if(c == 0xd1) c = 'N';
else if(c >= 0xd2 && c <= 0xd6) c = 'O';
else if(c >= 0xd9 && c <= 0xdc) c = 'U';
else if(c == 0xdd) c = 'Y';
else if(c >= 0xe0 && c <= 0xe5) c = 'a';
else if(c == 0xe7) c = 'c';
else if(c >= 0xe8 && c <= 0xeb) c = 'e';
else if(c >= 0xec && c <= 0xef) c = 'i';
else if(c == 0xf1) c = 'n';
else if(c >= 0xf2 && c <= 0xf6) c = 'o';
else if(c >= 0xf9 && c <= 0xfc) c = 'u';
else if(c == 0xfd || c == 0xff) c = 'y';
else if(c>127) c=*(++ch+1); //' ';
if(!is_space || c!= 32)
{
*ansi++=c;
len--;
if(c == 32) is_space = 1; else is_space = 0;
}
ch++;
}
else
{
if(*ch<32) *ch=32;
if(!is_space || *ch!= 32) {
*ansi++=*ch;
len--;
if(*ch == 32) is_space = 1; else is_space = 0;
}
}
ch++;
}
while(len > 0)
{
*ansi++=0;
len--;
}
if(a[0] == 0 || a[0] == ' ') strcpy(a, "PS3");
}
extern int firmware;
#define ISODCL(from, to) (to - from + 1)
static void setdaterec(unsigned char *p,int dd,int mm,int aa,int ho,int mi,int se)
{
*p++=(unsigned char) ((aa-1900) & 255);*p++=(char) (mm & 15) ;*p++=(char) (dd & 31);*p++=(char) ho;*p++=(char) mi;*p++=(char) se;*p++=(char) 0;
}
static void set731(unsigned char *p,int n)
{
*p++=(n & 0xff);*p++=((n>>8) & 0xff);*p++=((n>>16) & 0xff);*p++=((n>>24) & 0xff);
}
static void set733(unsigned char *p,int n)
{
*p++=(n & 0xff);*p++=((n>>8) & 0xff);*p++=((n>>16) & 0xff);*p++=((n>>24) & 0xff);
*p++=((n>>24) & 0xff);*p++=((n>>16) & 0xff);*p++=((n>>8) & 0xff);*p++=(n & 0xff);
}
static void set732(unsigned char *p,int n)
{
*p++=((n>>24) & 0xff);*p++=((n>>16) & 0xff);*p++=((n>>8) & 0xff);*p++=(n & 0xff);
}
static void set721(unsigned char *p,int n)
{
*p++=(n & 0xff);*p++=((n>>8) & 0xff);
}
static void set722(unsigned char *p,int n)
{
*p++=((n>>8) & 0xff);*p++=(n & 0xff);
}
static void set723(unsigned char *p,int n)
{
*p++=(n & 0xff);*p++=((n>>8) & 0xff);*p++=((n>>8) & 0xff);*p++=(n & 0xff);
}
static int isonum_731 (unsigned char * p)
{
return ((p[0] & 0xff)
| ((p[1] & 0xff) << 8)
| ((p[2] & 0xff) << 16)
| ((p[3] & 0xff) << 24));
}
/*
static int isonum_732 (unsigned char * p)
{
return ((p[3] & 0xff)
| ((p[2] & 0xff) << 8)
| ((p[1] & 0xff) << 16)
| ((p[0] & 0xff) << 24));
}
*/
static int isonum_733 (unsigned char * p)
{
return (isonum_731 (p));
}
static int isonum_721 (unsigned char * p)
{
return ((p[0] & 0xff) | ((p[1] & 0xff) << 8));
}
/*
static int isonum_723 (unsigned char * p)
{
return (isonum_721 (p));
}
*/
struct iso_primary_descriptor {
unsigned char type [ISODCL ( 1, 1)]; /* 711 */
unsigned char id [ISODCL ( 2, 6)];
unsigned char version [ISODCL ( 7, 7)]; /* 711 */
unsigned char unused1 [ISODCL ( 8, 8)];
unsigned char system_id [ISODCL ( 9, 40)]; /* aunsigned chars */
unsigned char volume_id [ISODCL ( 41, 72)]; /* dunsigned chars */
unsigned char unused2 [ISODCL ( 73, 80)];
unsigned char volume_space_size [ISODCL ( 81, 88)]; /* 733 */
unsigned char unused3 [ISODCL ( 89, 120)];
unsigned char volume_set_size [ISODCL (121, 124)]; /* 723 */
unsigned char volume_sequence_number [ISODCL (125, 128)]; /* 723 */
unsigned char logical_block_size [ISODCL (129, 132)]; /* 723 */
unsigned char path_table_size [ISODCL (133, 140)]; /* 733 */
unsigned char type_l_path_table [ISODCL (141, 144)]; /* 731 */
unsigned char opt_type_l_path_table [ISODCL (145, 148)]; /* 731 */
unsigned char type_m_path_table [ISODCL (149, 152)]; /* 732 */
unsigned char opt_type_m_path_table [ISODCL (153, 156)]; /* 732 */
unsigned char root_directory_record [ISODCL (157, 190)]; /* 9.1 */
unsigned char volume_set_id [ISODCL (191, 318)]; /* dunsigned chars */
unsigned char publisher_id [ISODCL (319, 446)]; /* achars */
unsigned char preparer_id [ISODCL (447, 574)]; /* achars */
unsigned char application_id [ISODCL (575, 702)]; /* achars */
unsigned char copyright_file_id [ISODCL (703, 739)]; /* 7.5 dchars */
unsigned char abstract_file_id [ISODCL (740, 776)]; /* 7.5 dchars */
unsigned char bibliographic_file_id [ISODCL (777, 813)]; /* 7.5 dchars */
unsigned char creation_date [ISODCL (814, 830)]; /* 8.4.26.1 */
unsigned char modification_date [ISODCL (831, 847)]; /* 8.4.26.1 */
unsigned char expiration_date [ISODCL (848, 864)]; /* 8.4.26.1 */
unsigned char effective_date [ISODCL (865, 881)]; /* 8.4.26.1 */
unsigned char file_structure_version [ISODCL (882, 882)]; /* 711 */
unsigned char unused4 [ISODCL (883, 883)];
unsigned char application_data [ISODCL (884, 1395)];
unsigned char unused5 [ISODCL (1396, 2048)];
};
struct iso_directory_record {
unsigned char length [ISODCL ( 1, 1)]; /* 711 */
unsigned char ext_attr_length [ISODCL ( 2, 2)]; /* 711 */
unsigned char extent [ISODCL ( 3, 10)]; /* 733 */
unsigned char size [ISODCL (11, 18)]; /* 733 */
unsigned char date [ISODCL (19, 25)]; /* 7 by 711 */
unsigned char flags [ISODCL (26, 26)];
unsigned char file_unit_size [ISODCL (27, 27)]; /* 711 */
unsigned char interleave [ISODCL (28, 28)]; /* 711 */
unsigned char volume_sequence_number [ISODCL (29, 32)]; /* 723 */
unsigned char name_len [1]; /* 711 */
unsigned char name [1];
};
struct iso_path_table{
unsigned char name_len[2]; /* 721 */
char extent[4]; /* 731 */
char parent[2]; /* 721 */
char name[1];
};
struct psp_file_entry_descriptor {
unsigned char filler1[2];
unsigned char lba_msd[4];
unsigned char lba_lsd[4];
unsigned char size_msd[4];
unsigned char size_lsd[4];
unsigned char filler2[14];
unsigned char name_len[1];
unsigned char file_name[23];
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void get_rand(void *bfr, u32 size)
{
int n;
if (size == 0)
return;
srand(get_ticks());
for(n = 0; n < size; n++)
*(((char *) bfr) + n) = rand() & 0xFF;
}
u64 get_disk_free_space(char *path)
{
struct statvfs svfs;
u32 blockSize;
u64 freeSize = 0;
int is_ntfs = 0;
if(!strncmp(path, "/ntfs", 5) || !strncmp(path, "/ext", 4)) is_ntfs = 1;
if(!is_ntfs)
{
if(sysFsGetFreeSize(path, &blockSize, &freeSize) !=0) return (u64) (-1LL);
return (((u64)blockSize * freeSize));
}
else
{
if(ps3ntfs_statvfs((const char *) path, &svfs) != 0) return (u64) (-1LL);
}
return ( ((u64)svfs.f_bsize * svfs.f_bfree));
}
#ifdef USE_64BITS_LSEEK
int get_iso_file_pos(int fd, char *path, u32 *flba, u64 *size)
#else
int get_iso_file_pos(FILE *fp, unsigned char *path, u32 *flba, u64 *size)
#endif
{
static struct iso_primary_descriptor sect_descriptor;
struct iso_directory_record * idr;
static int folder_split[64][3];
int nfolder_split = 0;
u32 file_lba = 0xffffffff;
u8 *sectors = NULL;
#ifdef USE_64BITS_LSEEK
if(fd <= 0 || !size || !flba) return FAILED;
#else
if(!fp || !size || !flba) return FAILED;
#endif
*size = 0;
folder_split[nfolder_split][0] = 0;
folder_split[nfolder_split][1] = 0;
folder_split[nfolder_split][2] = 0;
int i = 0;
int len = strlen(path);
while(path[i] != 0 && i < len)
{
if(path[i] == '/')
{
folder_split[nfolder_split][2] = i - folder_split[nfolder_split][1];
while(path[i] =='/' && i < len) i++;
if(folder_split[nfolder_split][2] == 0) {folder_split[nfolder_split][1] = i; continue;}
folder_split[nfolder_split][0] = 1;
nfolder_split++;
folder_split[nfolder_split][0] = 0;
folder_split[nfolder_split][1] = i;
folder_split[nfolder_split][2] = 0;
} else i++;
}
folder_split[nfolder_split][0] = 0;
folder_split[nfolder_split][2] = i - folder_split[nfolder_split][1];
nfolder_split++;
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, 0x8800LL, SEEK_SET) != 0x8800LL) goto err;
if(ps3ntfs_read(fd, (void *) §_descriptor, sizeof(struct iso_primary_descriptor)) != sizeof(struct iso_primary_descriptor)) goto err;
#else
if(fseek(fp, 0x8800, SEEK_SET) != 0) goto err;
if(fread((void *) §_descriptor, 1, sizeof(struct iso_primary_descriptor), fp) != sizeof(struct iso_primary_descriptor)) goto err;
#endif
if((sect_descriptor.type[0] != 2 && sect_descriptor.type[0] != 0xFF) || strncmp((void *) sect_descriptor.id, "CD001", 5)) goto err;
// PSP ISO
if(sect_descriptor.type[0] == 0xFF)
{
unsigned char dir_entry[0x38];
char entry_name[0x20];
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, 0xB860LL, SEEK_SET) != 0xB860LL) goto err;
#else
if(fseek(fp, 0xB860, SEEK_SET) != 0) goto err;
#endif
int c;
for(int i = 0; i < 8; i++)
{
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_read(fd, (void *) &dir_entry, 0x38) != 0x38) goto err;
#else
if(fread((void *) &dir_entry, 1, 0x38, fp) != 0x38) goto err;
#endif
for(c = 0; c < dir_entry[0x20]; c++) entry_name[c] = dir_entry[0x21 + c];
for( ; c < 0x20; c++) entry_name[c] = 0;
if(strstr(path, entry_name))
{
file_lba = isonum_731(&dir_entry[2]);
*flba = file_lba;
*size = isonum_731(&dir_entry[10]);
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, ((s64) file_lba) * SECTOR_SIZELL, SEEK_SET) != ((s64) file_lba) * SECTOR_SIZELL) goto err;
#else
if(fseek(fp, file_lba * SECTOR_SIZE, SEEK_SET) != 0) goto err;
#endif
return SUCCESS;
}
}
return -4;
}
u32 lba0 = isonum_731(§_descriptor.type_l_path_table[0]); // lba
if(!lba0) return -3;
u32 size0 = isonum_733(§_descriptor.path_table_size[0]); // size
if(!size0) return -3;
int size1 = ((size0 + SECTOR_FILL) / SECTOR_SIZE) * SECTOR_SIZE;
if(size1 < 0 || size1 > 0x400000) return -3; // size larger than 4MB
sectors = malloc(size1 + SECTOR_SIZE);
if(!sectors) return -3;
memset(sectors, 0, size1 + SECTOR_SIZE);
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, ((s64) lba0) * SECTOR_SIZELL, SEEK_SET) != ((s64) lba0) * SECTOR_SIZELL) goto err;
if(ps3ntfs_read(fd, (void *) sectors, size1) != size1) goto err;
#else
if(fseek(fp, lba0 * SECTOR_SIZE, SEEK_SET) != 0) goto err;
if(fread((void *) sectors, 1, size1, fp) != size1) goto err;
#endif
u32 p = 0;
u32 lba_folder = 0xffffffff;
u32 lba = 0xffffffff;
int nsplit = 0;
int last_parent = 1;
int cur_parent = 1;
while(p < size0)
{
if(nsplit >= nfolder_split) break;
if(folder_split[nsplit][0] == 0 && nsplit != 0) {lba_folder = lba; break;}
if(folder_split[nsplit][2] == 0) continue;
u32 snamelen = isonum_721(§ors[p]);
if(snamelen == 0) p = ((p / SECTOR_SIZE) * SECTOR_SIZE) + SECTOR_SIZE; //break;
p += 2;
lba = isonum_731(§ors[p]);
p += 4;
u32 parent_name = isonum_721(§ors[p]);
p += 2;
memset(wstring, 0, 512 * 2);
memcpy(wstring, §ors[p], snamelen);
len = UTF16_to_UTF8(wstring, (u8 *) temp_string);
if(cur_parent == 1 && folder_split[nsplit][0] == 0 && nsplit == 0) {lba_folder = lba; break;}
if(last_parent == parent_name && len == folder_split[nsplit][2] &&
!strncmp((void *) temp_string, &path[folder_split[nsplit][1]], folder_split[nsplit][2]))
{
//DPrintf("p: %s %u %u\n", &path[folder_split[nsplit][1]], folder_split[nsplit][2], snamelen);
last_parent = cur_parent;
nsplit++;
if(folder_split[nsplit][0] == 0) {lba_folder = lba; break;}
}
p += snamelen;
cur_parent++;
if(snamelen & 1) {p++;}
}
if(lba_folder == 0xffffffff) goto err;
memset(sectors, 0, 4096);
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, ((s64) lba_folder) * SECTOR_SIZELL, SEEK_SET) != ((s64) lba_folder) * SECTOR_SIZELL) goto err;
if(ps3ntfs_read(fd, (void *) sectors, SECTOR_SIZE) != SECTOR_SIZE) goto err;
#else
if(fseek(fp, lba_folder * SECTOR_SIZE, SEEK_SET) != 0) goto err;
if(fread((void *) sectors, 1, SECTOR_SIZE, fp) != SECTOR_SIZE) goto err;
#endif
int size_directory = -1;
int p2 = 0;
p = 0;
while(true)
{
if(nsplit >= nfolder_split) break;
idr = (struct iso_directory_record *) §ors[p];
if(size_directory == -1)
{
if((int) idr->name_len[0] == 1 && idr->name[0] == 0 && lba == isonum_731((void *) idr->extent) && idr->flags[0] == 0x2)
{
size_directory = isonum_733((void *) idr->size);
}
}
if(idr->length[0] == 0 && sizeof(struct iso_directory_record) + p > SECTOR_SIZE)
{
lba_folder++;
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, ((s64) lba_folder) * SECTOR_SIZELL, SEEK_SET) != ((s64) lba_folder) * SECTOR_SIZELL) goto err;
if(ps3ntfs_read(fd, (void *) sectors, SECTOR_SIZE) != SECTOR_SIZE) goto err;
#else
if(fseek(fp, lba_folder * SECTOR_SIZE, SEEK_SET) != 0) goto err;
if(fread((void *) sectors, 1, SECTOR_SIZE, fp) != SECTOR_SIZE) goto err;
#endif
p = 0; p2 = (p2 & ~SECTOR_FILL) + SECTOR_SIZE;
idr = (struct iso_directory_record *) §ors[p];
if((int) idr->length[0] == 0) break;
if((size_directory == -1 && idr->length[0] == 0) || (size_directory != -1 && p2 >= size_directory)) break;
continue;
}
if((size_directory == -1 && idr->length[0] == 0) || (size_directory != -1 && p2 >= size_directory)) break;
if((int) idr->length[0] == 0) break;
memset(wstring, 0, 512 * 2);
memcpy(wstring, (char *) idr->name, idr->name_len[0]);
len = UTF16_to_UTF8(wstring, (u8 *) temp_string);
if(len == folder_split[nsplit][2] &&
!strncmp((char *) temp_string, &path[folder_split[nsplit][1]], (int) folder_split[nsplit][2]))
{
if(file_lba == 0xffffffff) file_lba = isonum_733(&idr->extent[0]);
*size+= (u64) (u32) isonum_733(&idr->size[0]);
} else if(file_lba != 0xffffffff) break;
p += idr->length[0]; p2 += idr->length[0];
}
*flba = file_lba;
if(file_lba == 0xffffffff) goto err;
#ifdef USE_64BITS_LSEEK
if(ps3ntfs_seek64(fd, ((s64) file_lba) * SECTOR_SIZELL, SEEK_SET) != ((s64) file_lba) * SECTOR_SIZELL) goto err;
#else
if(fseek(fp, file_lba * SECTOR_SIZE, SEEK_SET) != 0) goto err;
#endif
if(sectors) free(sectors);
return SUCCESS;
err:
if(sectors) free(sectors);
return -4;
}
int create_fake_file_iso(char *path, char *filename, u64 size)
{
u8 *mem = create_fake_file_iso_mem(filename, size);
if(!mem) return ERROR_OUT_OF_MEMORY;
int ret = SUCCESS;
FILE *fp2 = fopen(path, "wb");
if(fp2)
{
fwrite((void *) mem, 1, sizeof(build_iso_data), fp2);
fclose(fp2);
}
else ret = FAILED;
free(mem);
return ret;
}
u8 *create_fake_file_iso_mem(char *filename, u64 size)
{
int len_string;
u8 *mem = malloc(sizeof(build_iso_data));
if(!mem) return NULL;
u16 *string = (u16 *) malloc(256);
if(!string) {free(mem); return NULL;}
#define MAX_NAME_LEN 110
char name[MAX_NAME_LEN + 1];
strncpy(name, filename, MAX_NAME_LEN);
name[MAX_NAME_LEN] = 0;
if(strlen(filename) > MAX_NAME_LEN)
{
// break the string
int pos = MAX_NAME_LEN - 1 - strlen(get_extension(filename));
while(pos > 0 && (name[pos] & 192) == 128) pos--; // skip UTF extra codes
strcpy(&name[pos], get_extension(filename));
}
len_string = UTF8_to_UTF16((u8 *) name, string);
memcpy(mem, build_iso_data, sizeof(build_iso_data));
struct iso_primary_descriptor *ipd = (struct iso_primary_descriptor *) &mem[0x8000];
struct iso_primary_descriptor *ipd2 = (struct iso_primary_descriptor *) &mem[0x8800];
struct iso_directory_record *idr = (struct iso_directory_record *) &mem[0xB840];
struct iso_directory_record *idr2 = (struct iso_directory_record *) &mem[0xC044];
u32 last_lba = isonum_733 (ipd->volume_space_size);
u64 size0 = size;
while(size)
{
u8 flags = 0;
if(size > 0xFFFFF800ULL) {flags = 0x80; size0 = 0xFFFFF800ULL;} else size0 = size;
idr->name_len[0] = strlen(name);
memcpy(idr->name, name, idr->name_len[0]);
idr->length[0] = (idr->name_len[0] + sizeof(struct iso_directory_record) + 1) & ~1;
idr->ext_attr_length[0] = 0;
set733(idr->extent, last_lba);
set733(idr->size, size0);
idr->date[0] = 0x71; idr->date[1] = 0x0B;
idr->date[2] = 0x0A; idr->date[3] = 0x0D;
idr->date[4] = 0x38; idr->date[5] = 0x00;
idr->date[6] = 0x04;
idr->flags[0] = flags;
idr->file_unit_size[0] = 0;
idr->interleave[0] = 0;
set723(idr->volume_sequence_number, 1);
idr = (struct iso_directory_record *) (((char *) idr) + idr->length[0]);
/////////////////////
idr2->name_len[0] = len_string * 2;
memcpy(idr2->name, string, idr2->name_len[0]);
idr2->length[0] = (idr2->name_len[0] + sizeof(struct iso_directory_record) + 1) & ~1;
idr2->ext_attr_length[0] = 0;
set733(idr2->extent, last_lba);
set733(idr2->size, size0);
idr2->date[0] = 0x71; idr2->date[1] = 0x0B;
idr2->date[2] = 0x0A; idr2->date[3] = 0x0D;
idr2->date[4] = 0x38; idr2->date[5] = 0x00;
idr2->date[6] = 0x04;
idr2->flags[0] = flags;
idr2->file_unit_size[0] = 0;
idr2->interleave[0] = 0;
set723(idr2->volume_sequence_number, 1);
idr2 = (struct iso_directory_record *) (((char *) idr2) + idr2->length[0]);
/////////////////////
last_lba += (size0 + 0x7ffULL)/ 0x800ULL;
size-= size0;
}
last_lba += (size + SECTOR_FILL) / SECTOR_SIZE;
set733(ipd->volume_space_size, last_lba);
set733(ipd2->volume_space_size, last_lba);
free(string);
return mem;
}
/***********************************************************************************************************/
/* MAKEPS3ISO - EXTRACTPS3ISO - PATCHPS3ISO */
/***********************************************************************************************************/
static int param_patched = 0;
static int self_sprx_patched = 0;
static int cur_isop = -1;
static int lpath;
static int wpath;
static u32 llba0 = 0; // directory path0
static u32 llba1 = 0; // directory path1
static u32 wlba0 = 0; // directory pathw0
static u32 wlba1 = 0; // directory pathw1
static u32 dllba = 0; // dir entries
static u32 dwlba = 0; // dir entriesw
static u32 dlsz = 0; // dir entries size (sectors)
static u32 dwsz = 0; // dir entriesw size (sectors)
static u32 flba = 0; // first lba for files
static u32 toc = 0; // TOC of the iso
static char iso_split = 0;
static char output_name[0x420];
static char output_name2[0x420];
static int pos_lpath0 = 0;
static int pos_lpath1 = 0;
static int pos_wpath0 = 0;
static int pos_wpath1 = 0;
static int dd = 1, mm = 1, aa = 2013, ho = 0, mi = 0, se = 2;
static u8 *sectors = NULL;
static u8 *sectors3 = NULL;
#define MAX_ISO_PATHS 4096
typedef struct {
u32 ldir;
u32 wdir;
u32 llba;
u32 wlba;
int parent;
char *name;
} _directory_iso;
typedef struct {
int parent;
char *name;
} _directory_iso2;
typedef struct {
u32 size;
char path[0x420];
} _split_file;
static _split_file *split_file = NULL;
static int fd_split = -1;
static int fd_split0 = -1;
static int split_index = 0;
static int split_files = 0;
static _directory_iso *directory_iso = NULL;
static _directory_iso2 *directory_iso2 = NULL;
static char dbhead[64];
static void memcapscpy(void *dest, void *src, int size)
{
char *d = dest;
char *s = src;
char c;
int n;
for(n = 0; n < size; n++) {c = *s++; *d++ = (char) toupper((int) c);}
}
static int iso_parse_param_sfo(char * file, char *title_id, char *title_name)
{
int fd;
int bytes;
int ct = 0;
fd = ps3ntfs_open(file, O_RDONLY, 0766);
if(fd >= 0)
{
int len, pos, str;
unsigned char *mem=NULL;
len = (int) ps3ntfs_seek64(fd, 0, SEEK_END);
mem = (unsigned char *) malloc(len + 16);
if(!mem) {ps3ntfs_close(fd); return -2;}
memset(mem, 0, len + 16);
ps3ntfs_seek64(fd, 0, SEEK_SET);
bytes = ps3ntfs_read(fd, (void *) mem, len);
ps3ntfs_close(fd);
if(bytes != len)
{
free(mem);
return -2;
}
str = (mem[8] + (mem[9]<<8));
pos = (mem[0xc] + (mem[0xd]<<8));
int indx = 0;
while(str < len)
{
if(mem[str] == 0) break;
if(!strncmp((char *) &mem[str], "TITLE", 6))
{
strncpy(title_name, (char *) &mem[pos], 63);
title_name[63] = 0;
ct++;
}
else if(!strncmp((char *) &mem[str], "TITLE_ID", 9))
{
memcpy(title_id, (char *) &mem[pos], 4);
title_id[4] = '-';
strncpy(&title_id[5], (char *) &mem[pos + 4], 58);
title_id[63] = 0;
ct++;
}
if(ct >= 2)
{
free(mem);
return SUCCESS;
}
while(mem[str]) str++; str++;
pos += (mem[0x1c+indx]+(mem[0x1d+indx]<<8));
indx += 16;
}
if(mem) free(mem);
}
return FAILED;
}
static int calc_entries(char *path, int parent)
{
DIR *dir;
int len_string;
struct stat s;
int cldir = 0;
int ldir = sizeof(struct iso_directory_record) + 6; // ..
ldir = (ldir + 7) & ~7;
ldir += sizeof(struct iso_directory_record) + 6; // .
ldir += ldir & 1;
int cwdir = 0;
int wdir = sizeof(struct iso_directory_record) + 6; // ..
wdir = (wdir + 7) & ~7;
wdir += sizeof(struct iso_directory_record) + 6; // .
wdir += wdir & 1;
cldir = ldir;
cwdir = wdir;
lpath+= (lpath & 1);
wpath+= (wpath & 1);
int cur = cur_isop;
if(cur >= MAX_ISO_PATHS) return ERROR_TOO_MUCH_DIR_ENTRIES;
directory_iso[cur].ldir = ldir;
directory_iso[cur].wdir = wdir;
directory_iso[cur].parent = parent;
if(!cur)
{
directory_iso[cur].name = malloc(16);
if(!directory_iso[cur].name) return ERROR_OUT_OF_MEMORY;
directory_iso[cur].name[0] = 0;
}
int cur2 = cur;
int nfolders = 0, mfolders, nentries, progress, pcount;
nentries = 0; progress = 100; pcount = 0;
cur_isop++;
bool is_dir_entry;
int len = strlen(path);
// calc folders, subfolders, multi-part files & files
#ifdef NOPS3_UPDATE
if(len >= 11 && strstr(path + len - 11, "/PS3_UPDATE")) {dir = NULL; nfiles++;}else
#endif
dir = opendir(path);
if(dir)
{
int l = len + 1;
strcat(path + len, "/");