-
Notifications
You must be signed in to change notification settings - Fork 2
/
cdpatch.c
2196 lines (2013 loc) · 59.4 KB
/
cdpatch.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
////////////////////////////////////////////////////////////////////////////////
//
#define TITLE "cdpatch - CD-XA image insert/extract utility"
#define COPYR "Copyright (C) 2001,2011 Neill Corlett"
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
#include "common.h"
#include "banner.h"
////////////////////////////////////////////////////////////////////////////////
static const uint32_t max_path_depth = 256;
////////////////////////////////////////////////////////////////////////////////
//
// Program options
//
struct cdpatch_options {
int8_t insert;
int8_t extract;
const char* binname;
const char* basedir;
int8_t big;
int8_t little;
int8_t boot;
int8_t enforce_fscheck;
int8_t overwrite;
int8_t verbose;
int8_t recurse;
const char** files;
int files_count;
};
static const char* fsoverride = " (use -f to override)";
////////////////////////////////////////////////////////////////////////////////
static int exists(const char* filename) {
FILE* f = fopen(filename, "rb");
if(f) { fclose(f); return 1; }
return 0;
}
////////////////////////////////////////////////////////////////////////////////
static const char* oom = "Error: Out of memory\n";
////////////////////////////////////////////////////////////////////////////////
static uint32_t get32lsb(const uint8_t* p) {
return
(((uint32_t)(p[0])) << 0) |
(((uint32_t)(p[1])) << 8) |
(((uint32_t)(p[2])) << 16) |
(((uint32_t)(p[3])) << 24);
}
static uint32_t get32msb(const uint8_t* p) {
return
(((uint32_t)(p[0])) << 24) |
(((uint32_t)(p[1])) << 16) |
(((uint32_t)(p[2])) << 8) |
(((uint32_t)(p[3])) << 0);
}
static void set32lsb(uint8_t* p, uint32_t value) {
p[0] = (uint8_t)(value >> 0);
p[1] = (uint8_t)(value >> 8);
p[2] = (uint8_t)(value >> 16);
p[3] = (uint8_t)(value >> 24);
}
static void set32msb(uint8_t* p, uint32_t value) {
p[0] = (uint8_t)(value >> 24);
p[1] = (uint8_t)(value >> 16);
p[2] = (uint8_t)(value >> 8);
p[3] = (uint8_t)(value >> 0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns nonzero if any bytes in the array are nonzero
//
static int anynonzero(const uint8_t* data, size_t len) {
for(; len; len--) {
if(*data++) { return 1; }
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//
// Convert ISO9660 file size to sector count, rounding up
//
static uint32_t sectorcount(uint32_t size) {
return (size >> 11) + ((size & 0x7FF) != 0);
}
////////////////////////////////////////////////////////////////////////////////
//
// LUTs for computing ECC/EDC
//
static uint8_t ecc_f_lut[256];
static uint8_t ecc_b_lut[256];
static uint32_t edc_lut [256];
static void eccedc_init(void) {
uint32_t i, j, edc;
for(i = 0; i < 256; i++) {
j = (i << 1) ^ (i & 0x80 ? 0x11D : 0);
ecc_f_lut[i ] = (uint8_t)j;
ecc_b_lut[i ^ j] = (uint8_t)i;
edc = i;
for(j = 0; j < 8; j++) {
edc = (edc >> 1) ^ (edc & 1 ? 0xD8018001 : 0);
}
edc_lut[i] = edc;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Compute EDC for a block
//
static void edc_computeblock(const uint8_t* src, size_t size, uint8_t* dest) {
uint32_t edc = 0;
while(size--) {
edc = (edc >> 8) ^ edc_lut[(edc ^ (*src++)) & 0xFF];
}
set32lsb(dest, edc);
}
////////////////////////////////////////////////////////////////////////////////
//
// Compute ECC for a block (can do either P or Q)
//
static void ecc_computeblock(
uint8_t* src,
uint32_t major_count,
uint32_t minor_count,
uint32_t major_mult,
uint32_t minor_inc,
uint8_t* dest
) {
uint32_t size = major_count * minor_count;
uint32_t major, minor;
for(major = 0; major < major_count; major++) {
uint32_t index = (major >> 1) * major_mult + (major & 1);
uint8_t ecc_a = 0;
uint8_t ecc_b = 0;
for(minor = 0; minor < minor_count; minor++) {
uint8_t temp = src[index];
index += minor_inc;
if(index >= size) index -= size;
ecc_a ^= temp;
ecc_b ^= temp;
ecc_a = ecc_f_lut[ecc_a];
}
ecc_a = ecc_b_lut[ecc_f_lut[ecc_a] ^ ecc_b];
dest[major ] = ecc_a;
dest[major + major_count] = ecc_a ^ ecc_b;
}
}
//
// Generate ECC P and Q codes for a block
//
static void ecc_generate(uint8_t* sector, int zeroaddress) {
uint8_t saved_address[4];
//
// Save the address and zero it out, if necessary
//
if(zeroaddress) {
memmove(saved_address, sector + 12, 4);
memset(sector + 12, 0, 4);
}
//
// Compute ECC P code
//
ecc_computeblock(sector + 0xC, 86, 24, 2, 86, sector + 0x81C);
//
// Compute ECC Q code
//
ecc_computeblock(sector + 0xC, 52, 43, 86, 88, sector + 0x8C8);
//
// Restore the address, if necessary
//
if(zeroaddress) {
memmove(sector + 12, saved_address, 4);
}
}
////////////////////////////////////////////////////////////////////////////////
//
// CD sync header
//
static const uint8_t sync_header[12] = {
0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00
};
////////////////////////////////////////////////////////////////////////////////
//
// Generate ECC/EDC information for a sector (must be 2352 = 0x930 bytes)
//
static void eccedc_generate(uint8_t* sector) {
//
// Generate sync
//
memmove(sector, sync_header, sizeof(sync_header));
switch(sector[0x0F]) {
case 0x00:
//
// Mode 0: no data; generate zeroes
//
memset(sector + 0x10, 0, 0x920);
break;
case 0x01:
//
// Mode 1:
//
// Compute EDC
//
edc_computeblock(sector + 0x00, 0x810, sector + 0x810);
//
// Zero out reserved area
//
memset(sector + 0x814, 0, 8);
//
// Generate ECC P/Q codes
//
ecc_generate(sector, 0);
break;
case 0x02:
//
// Mode 2:
//
// Make sure XA flags match
//
memmove(sector + 0x14, sector + 0x10, 4);
if(!(sector[0x12] & 0x20)) {
//
// Form 1: Compute EDC
//
edc_computeblock(sector + 0x10, 0x808, sector + 0x818);
//
// Generate ECC P/Q codes
//
ecc_generate(sector, 1);
} else {
//
// Form 2: Compute EDC
//
edc_computeblock(sector + 0x10, 0x91C, sector + 0x92C);
}
break;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Verify EDC for a sector (must be 2352 = 0x930 bytes)
// Returns 0 on success
//
static int edc_verify(const uint8_t* sector) {
uint8_t myedc[4];
//
// Verify sync
//
if(memcmp(sector, sync_header, sizeof(sync_header))) { return 1; }
switch(sector[0x0F]) {
case 0x00:
//
// Mode 0: no data; everything had better be zero
//
return anynonzero(sector + 0x10, 0x920);
case 0x01:
//
// Mode 1
//
edc_computeblock(sector + 0x00, 0x810, myedc);
return memcmp(myedc, sector + 0x810, 4);
case 0x02:
//
// Mode 2: Verify that the XA type is correctly copied twice
//
if(memcmp(sector + 0x10, sector + 0x14, 4)) { return 1; }
if(!(sector[0x12] & 0x20)) {
//
// Form 1
//
edc_computeblock(sector + 0x10, 0x808, myedc);
return memcmp(myedc, sector + 0x818, 4);
} else {
//
// Form 2
//
edc_computeblock(sector + 0x10, 0x91C, myedc);
return memcmp(myedc, sector + 0x92C, 4);
}
}
//
// Invalid mode
//
return 1;
}
////////////////////////////////////////////////////////////////////////////////
struct cacheentry {
uint8_t* data;
uint32_t sector;
uint8_t valid;
};
enum { CACHE_ENTRIES = 4 };
enum {
BINTYPE_UNKNOWN = 0,
BINTYPE_2048 = 1,
BINTYPE_2352 = 2
};
struct binfile {
FILE* f;
const char* name;
int type;
uint32_t sectors;
struct cacheentry cache[CACHE_ENTRIES];
};
static void bin_quit(struct binfile* bin) {
size_t i;
if(bin->f) { fclose(bin->f); }
for(i = 0; i < CACHE_ENTRIES; i++) {
if(bin->cache[i].data) { free(bin->cache[i].data); }
}
}
static int bin_init(struct binfile* bin) {
size_t i;
memset(bin, 0, sizeof(struct binfile));
for(i = 0; i < CACHE_ENTRIES; i++) {
bin->cache[i].data = malloc(2352);
if(!bin->cache[i].data) {
printf("%s", oom);
bin_quit(bin);
return 1;
}
}
return 0;
}
static void cache_mtf(struct binfile* bin, size_t entry) {
if(entry) {
struct cacheentry tmp = bin->cache[entry];
memmove(bin->cache + 1, bin->cache, sizeof(struct cacheentry) * entry);
bin->cache[0] = tmp;
}
}
static uint8_t* cache_find(struct binfile* bin, uint32_t sector) {
size_t i;
for(i = 0; i < CACHE_ENTRIES; i++) {
if((bin->cache[i].sector == sector) && bin->cache[i].valid) {
cache_mtf(bin, i);
return bin->cache[0].data;
}
}
return NULL;
}
static uint8_t* cache_allocbegin(struct binfile* bin, uint32_t sector) {
bin->cache[CACHE_ENTRIES - 1].valid = 0;
bin->cache[CACHE_ENTRIES - 1].sector = sector;
return bin->cache[CACHE_ENTRIES - 1].data;
}
static void cache_allocend(struct binfile* bin) {
cache_mtf(bin, CACHE_ENTRIES - 1);
bin->cache[0].valid = 1;
}
////////////////////////////////////////////////////////////////////////////////
//
// Detect whether image is ISO or BIN
// Returns nonzero on error
//
static int bintype_detect(struct binfile* bin) {
uint8_t* sector = NULL;
off_t size;
bin->type = BINTYPE_UNKNOWN;
if(fseeko(bin->f, 0, SEEK_END) != 0) { goto error_bin; }
size = ftello(bin->f);
if(size == -1) { goto error_bin; }
if(fseeko(bin->f, 0, SEEK_SET) != 0) { goto error_bin; }
if(size <= 0 || ((size % 2352) != 0 && (size % 2048) != 0)) {
//
// Size is zero or not cleanly divisible
//
printf("Error: %s: Unable to determine BIN or ISO format based on size\n",
bin->name
);
goto error;
} else if((size % 2352) != 0) {
//
// If indivisible by 2352, assume ISO
//
bin->type = BINTYPE_2048;
} else if((size % 2048) != 0) {
//
// If indivisible by 2048, assume BIN
//
bin->type = BINTYPE_2352;
} else {
//
// If divisible by both, read the first 2352 bytes and see if it's a
// valid raw sector. If so, assume BIN.
//
sector = malloc(2352);
if(!sector) { goto error_mem; }
if(fread(sector, 1, 2352, bin->f) != 2352) { goto error_bin; }
bin->type = edc_verify(sector) ? BINTYPE_2048 : BINTYPE_2352;
}
//
// Figure out the number of sectors
//
{ off_t sectorsize = (bin->type == BINTYPE_2048) ? 2048 : 2352;
bin->sectors =
(sizeof(off_t) > 4) ? (
((((off_t)(size / sectorsize)) >> 31) > 1) ?
((uint32_t)(0xFFFFFFFFLU)) :
((uint32_t)(size / sectorsize))
) : (((uint32_t)size) / ((uint32_t)sectorsize));
}
goto done;
error_mem:
printf("%s", oom);
goto error;
error_bin:
printfileerror(bin->f, bin->name);
goto error;
error:
bin->type = BINTYPE_UNKNOWN;
goto done;
done:
if(sector) { free(sector); }
return (bin->type == BINTYPE_UNKNOWN);
}
////////////////////////////////////////////////////////////////////////////////
//
// Ensure that the sector number is within the seekable range for the bin file;
// returns nonzero on error
//
int check_bin_sector_range(const struct binfile* bin, uint32_t sector) {
if(sector >= bin->sectors) {
printf("Error: Sector %lu is out of range\n", (unsigned long)sector);
return 1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns NULL on failure
// The returned buffer is valid until the next call
//
static uint8_t* read_raw_sector(struct binfile* bin, uint32_t sector) {
uint8_t* data = NULL;
if(check_bin_sector_range(bin, sector)) { goto error; }
if(bin->type != BINTYPE_2352) {
printf("Error: Tried to read raw sector from ISO\n");
goto error;
}
data = cache_find(bin, sector);
if(!data) {
data = cache_allocbegin(bin, sector);
if(fseeko(bin->f, 2352 * ((off_t)sector), SEEK_SET) != 0) {
goto error_f;
}
if(fread(data, 1, 2352, bin->f) != 2352) { goto error_f; }
cache_allocend(bin);
}
return data;
error_f:
printf("At sector %lu: ", (unsigned long)sector);
printfileerror(bin->f, bin->name);
goto error;
error:
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns NULL on failure
// The returned buffer is valid until the next call
//
static uint8_t* read_cooked_sector(
struct binfile* bin,
uint32_t sector,
const struct cdpatch_options* opt
) {
uint8_t* data = NULL;
if(check_bin_sector_range(bin, sector)) { goto error; }
data = cache_find(bin, sector);
if(!data) {
data = cache_allocbegin(bin, sector);
if(bin->type == BINTYPE_2048) {
if(fseeko(bin->f, 2048 * ((off_t)sector), SEEK_SET) != 0) {
goto error_f;
}
if(fread(data, 1, 2048, bin->f) != 2048) { goto error_f; }
} else {
if(fseeko(bin->f, 2352 * ((off_t)sector), SEEK_SET) != 0) {
goto error_f;
}
if(fread(data, 1, 2352, bin->f) != 2352) { goto error_f; }
//
// Verify the EDC
//
if(edc_verify(data)) {
if(opt->enforce_fscheck || opt->verbose) {
printf("%s: CD sector %lu is corrupt%s\n",
opt->enforce_fscheck ? "Error" : "Warning",
(unsigned long)sector,
opt->enforce_fscheck ? fsoverride : ""
);
}
if(opt->enforce_fscheck) { goto error; }
}
}
cache_allocend(bin);
}
if(bin->type == BINTYPE_2352) {
//
// Figure out where the data actually resides in the sector
//
switch(data[0xF]) {
case 1:
data += 0x10;
break;
case 2:
if(data[0x12] & 0x20) {
if(opt->enforce_fscheck || opt->verbose) {
printf("%s: Attempted to read Form 2 sector %lu%s\n",
opt->enforce_fscheck ? "Error" : "Warning",
(unsigned long)sector,
opt->enforce_fscheck ? fsoverride : ""
);
}
if(opt->enforce_fscheck) { goto error; }
}
data += 0x18;
break;
default:
if(opt->enforce_fscheck || opt->verbose) {
printf("%s: Invalid mode 0x%02X at sector %lu%s\n",
opt->enforce_fscheck ? "Error" : "Warning",
(int)data[0xF], (unsigned long)sector,
opt->enforce_fscheck ? fsoverride : ""
);
if(opt->enforce_fscheck) { goto error; }
}
data += 0x10; // assume mode 1
break;
}
}
return data;
error_f:
printf("At sector %lu: ", (unsigned long)sector);
printfileerror(bin->f, bin->name);
goto error;
error:
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
//
// Allocate space for a cooked sector, with the understanding that we will
// overwrite all of it
//
// Returns NULL on failure
// The returned buffer is valid until the next call
//
static uint8_t* alloc_cooked_sector(
struct binfile* bin,
uint32_t sector,
const struct cdpatch_options* opt
) {
if(bin->type == BINTYPE_2048) {
uint8_t* data;
if(check_bin_sector_range(bin, sector)) { return NULL; }
//
// Just allocate space and not initialize it
//
data = cache_find(bin, sector);
if(!data) {
data = cache_allocbegin(bin, sector);
cache_allocend(bin);
}
return data;
} else {
//
// We need to read the sector anyway, so just read it
//
return read_cooked_sector(bin, sector, opt);
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns 0 on success
//
static int writeback_raw_sector(struct binfile* bin, uint32_t sector) {
int returncode = 0;
const uint8_t* data;
if(check_bin_sector_range(bin, sector)) { goto error; }
if(bin->type != BINTYPE_2352) {
printf("Error: Tried to write raw sector to ISO\n");
goto error;
}
data = cache_find(bin, sector);
if(!data) {
printf("Error: Sector not in cache\n");
goto error;
}
//
// Seek and write
//
if(fseeko(bin->f, 2352 * ((off_t)sector), SEEK_SET) != 0) { goto error_f; }
if(fwrite(data, 1, 2352, bin->f) != 2352) { goto error_f; }
fflush(bin->f);
goto done;
error_f:
printf("At sector %lu: ", (unsigned long)sector);
printfileerror(bin->f, bin->name);
goto error;
error:
returncode = 1;
goto done;
done:
return returncode;
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns 0 on success
//
static int writeback_cooked_sector(
struct binfile* bin,
uint32_t sector,
int redoflags,
int last
) {
int returncode = 0;
uint8_t* data;
if(check_bin_sector_range(bin, sector)) { goto error; }
data = cache_find(bin, sector);
if(!data) {
printf("Error: Sector not in cache\n");
goto error;
}
if(bin->type == BINTYPE_2048) {
//
// If this is an ISO file, just seek and write directly
//
if(fseeko(bin->f, 2048 * ((off_t)sector), SEEK_SET) != 0) { goto error_f; }
if(fwrite(data, 1, 2048, bin->f) != 2048) { goto error_f; }
} else {
//
// If mode 2, and we care about the XA flags, set those up
//
if(redoflags && (data[0xF] == 2)) {
data[0x10] = data[0x14] = 0;
data[0x11] = data[0x15] = 0;
data[0x12] = data[0x16] = 0x08 | (last ? 0x81 : 0x00);
data[0x13] = data[0x17] = 0;
}
//
// Regenerate ECC/EDC
//
eccedc_generate(data);
//
// Seek and write
//
if(fseeko(bin->f, 2352 * ((off_t)sector), SEEK_SET) != 0) { goto error_f; }
if(fwrite(data, 1, 2352, bin->f) != 2352) { goto error_f; }
}
fflush(bin->f);
goto done;
error_f:
printf("At sector %lu: ", (unsigned long)sector);
printfileerror(bin->f, bin->name);
goto error;
error:
returncode = 1;
goto done;
done:
return returncode;
}
////////////////////////////////////////////////////////////////////////////////
//
// Read or write arbitrary cooked data; returns nonzero on error
//
static int rw_cooked_data(
struct binfile* bin,
uint32_t sector,
uint32_t offset,
uint8_t* data,
size_t size,
const struct cdpatch_options* opt,
int write
) {
int returncode = 0;
//
// Normalize sector/offset, and verify the range
//
if((offset >> 11) > (0xFFFFFFFFLU - sector)) { goto error_range; }
sector += offset >> 11;
offset &= 0x7FF;
if(check_bin_sector_range(bin, sector)) { goto error; }
if(bin->type == BINTYPE_2048) {
//
// Read or write the image file directly
//
if(fseeko(
bin->f, 2048 * ((off_t)sector) + ((off_t)offset), SEEK_SET
) != 0) { goto error_f; }
if(write) {
if(fwrite(data, 1, size, bin->f) != size) { goto error_f; }
} else {
if(fread (data, 1, size, bin->f) != size) { goto error_f; }
}
} else {
//
// Read or write sector-by-sector
//
for(; size; sector++) {
size_t insector = 0x800 - offset;
size_t z = size < insector ? size : insector;
uint8_t* d = read_cooked_sector(bin, sector, opt);
if(!d) { goto error; }
if(write) {
memmove(d + offset, data, z);
if(writeback_cooked_sector(bin, sector, 0, 0)) { goto error; }
} else {
memmove(data, d + offset, z);
}
data += z;
size -= z;
offset = 0;
if(size && (sector == 0xFFFFFFFFLU)) { goto error_range; }
}
}
goto done;
error_f:
printf("At sector %lu: ", (unsigned long)sector);
printfileerror(bin->f, bin->name);
goto error;
error_range:
printf("Error: %s out of range: sector=%lu offset=%lu\n",
write ? "Write" : "Read", (unsigned long)sector, (unsigned long)offset
);
goto error;
error:
returncode = 1;
done:
return returncode;
}
static int read_cooked_data(
struct binfile* bin,
uint32_t sector,
uint32_t offset,
uint8_t* data,
size_t size,
const struct cdpatch_options* opt
) {
return rw_cooked_data(bin, sector, offset, data, size, opt, 0);
}
static int write_cooked_data(
struct binfile* bin,
uint32_t sector,
uint32_t offset,
const uint8_t* data,
size_t size,
const struct cdpatch_options* opt
) {
return rw_cooked_data(bin, sector, offset, (uint8_t*)data, size, opt, 1);
}
////////////////////////////////////////////////////////////////////////////////
//
// Use RIFF if:
// - The sector is mode 0, or invalid
// - The sector is mode 2 and any of the flag bits besides 'data', 'last sector
// of data record', or 'last sector of file' are set.
//
static int should_use_riff_format(const uint8_t* sector) {
if(sector[0xF] == 1) { return 0; }
if(sector[0xF] == 2) {
return
( sector[0x10] != 0) ||
( sector[0x11] != 0) ||
((sector[0x12] & 0x76) != 0) ||
( sector[0x13] != 0);
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns nonzero on error
//
static int extract_file(
struct binfile* bin,
uint32_t sector,
uint32_t filesize,
time_t modtime,
const char* filename,
const struct cdpatch_options* opt
) {
int returncode = 0;
FILE* f = NULL;
uint8_t* data = NULL;
if(!opt->overwrite && exists(filename)) {
printf("Error: %s already exists (use -o to override)\n", filename);
goto error;
}
f = fopen(filename, "wb");
if(!f) { goto error_f; }
//
// Check for RIFF format
//
if(bin->type == BINTYPE_2352) {
data = read_raw_sector(bin, sector);
if(!data) { goto error; }
}
if(data && should_use_riff_format(data)) {
//
// Extract in RIFF format
//
uint32_t sectors = sectorcount(filesize);
if(sectors > 1826091LU) {
sectors = 1826091LU; // Exceeds uint32_t - just cap it
}
if(opt->verbose) {
printf(
"Extract %7lu %10lu %s (CDXA)\n",
(unsigned long)sector,
(unsigned long)(2352 * sectors + 0x2C),
filename
);
}
//
// Write header
//
{ uint8_t hdr[0x2C];
memset(hdr, 0, sizeof(hdr));
memmove (hdr + 0x00, "RIFF", 4);
set32lsb(hdr + 0x04, 2352 * sectors + 0x2C);
memmove (hdr + 0x08, "CDXA", 4);
set32lsb(hdr + 0x0C, 2352 * sectors);
if(fwrite(hdr, 1, 0x2C, f) != 0x2C) { goto error_f; }
}
//
// Write contents
//
for(; sectors; sector++, sectors--) {
data = read_raw_sector(bin, sector);
if(!data) { goto error; }
if(edc_verify(data)) {
if(opt->enforce_fscheck || opt->verbose) {
printf("%s: %s: CD sector %lu is corrupt%s\n",
opt->enforce_fscheck ? "Error" : "Warning",
filename, (unsigned long)sector,
opt->enforce_fscheck ? fsoverride : ""
);
}
if(opt->enforce_fscheck) { goto error; }
}
if(fwrite(data, 1, 2352, f) != 2352) { goto error_f; }
}
} else {
if(opt->verbose) {
printf(
"Extract %7lu %10lu %s\n",
(unsigned long)sector,
(unsigned long)filesize,
filename
);
}
//
// Extract normally
//
for(; filesize; sector++) {
size_t remain = filesize < 2048 ? filesize : 2048;
data = read_cooked_sector(bin, sector, opt);
if(!data) { goto error; }
if(fwrite(data, 1, remain, f) != remain) { goto error_f; }
filesize -= remain;
}
}
if(f) { fclose(f); f = NULL; }
//
// Set modification time, if it was valid
//
if(modtime != ((time_t)(-1))) {
struct utimbuf b;
b.actime = modtime;
b.modtime = modtime;
if(utime((char*)filename, &b) != 0) {
// Silently fail - preserving modtime shouldn't be considered critical
}
}
goto done;
error_f:
printfileerror(f, filename);
goto error;
error:
returncode = 1;
goto done;
done:
if(f) { fclose(f); }
return returncode;
}
////////////////////////////////////////////////////////////////////////////////
//
// Returns nonzero on error
// Outputs the new size in *filesize
//
static int insert_file(
struct binfile* bin,
uint32_t sector,
uint32_t* filesize,