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
use crate::common::{EccEdc, MSF, Optimizations, SectorType, StatusError};
use log::{debug, error, trace};
const SYNC_DATA: &[u8; 12] = &[
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
];
///
/// Decoder implementation able to recover the original sector data by regenerating the removed data.
///
pub struct Decoder {
decoded_buffer: [u8; 2352],
edc_ecc_calculator: EccEdc,
}
impl Decoder {
///
/// Initialize the decoder library generating the required data.
///
/// # Arguments
///
/// **None**
///
/// # Return
///
/// **Decoder:** A new object instance to be used to decode the previously encoded CD-ROM sectors.
///
pub fn new() -> Self {
Decoder {
decoded_buffer: [0; 2352],
edc_ecc_calculator: EccEdc::new(),
}
}
///
/// Decodes the encoded sector to recover the original data
///
/// # Arguments
///
/// * **sector:** The sector slice containing the encoded data.
/// * **sector_type:** The sector type of the encoded data. Important for the decoding process.
/// * **sector_number:** The sector number in the CD-ROM used to generate the MSF data, not required if the RemoveMSF optimization was not used.
/// * **optimizations:** The optimizations used to encode the sector. Important for the decoding process.
///
/// # Return
///
/// **Vec(u8):** Decoded stream.
///
pub fn decode_sector(
&mut self,
sector: &[u8],
sector_type: SectorType,
sector_number: u32,
optimizations: Optimizations,
) -> Result<Vec<u8>, StatusError> {
// First we should check that the input size matches the expected size
trace!(
"Checking the encoded size of the {:?} sector for optimizations {:?}",
sector_type, optimizations
);
let encoded_size = self.get_encoded_size(sector_type, optimizations);
if encoded_size != sector.len() {
error!(
"The expected encoded size ({}) doesn't matches the input sector size ({})",
encoded_size,
sector.len()
);
return Err(StatusError::NotEnoughSectorData);
}
// Reset the output buffer
trace!("Clearing the buffer...");
self.decoded_buffer.fill(0);
// Process the CDDA sector using the optimizations
if sector_type == SectorType::Cdda || sector_type == SectorType::CddaGap {
trace!("The sector is a CDDA sector, executing the decode_sector_cdda function.");
return Ok(self.decode_sector_cdda(sector, sector_type, optimizations));
}
// Process the Mode1 sector using the optimizations
if sector_type == SectorType::Mode1
|| sector_type == SectorType::Mode1Gap
|| sector_type == SectorType::Mode1Raw
{
trace!("The sector is a Mode1 sector, executing the decode_sector_mode1 function.");
return Ok(self.decode_sector_mode1(sector, sector_type, sector_number, optimizations));
}
if sector_type == SectorType::Mode2 || sector_type == SectorType::Mode2Gap {
trace!("The sector is a Mode1 sector, executing the decode_sector_mode2 function.");
return Ok(self.decode_sector_mode2(sector, sector_type, sector_number, optimizations));
}
if sector_type == SectorType::Mode2Xa1
|| sector_type == SectorType::Mode2Xa1Gap
|| sector_type == SectorType::Mode2Xa2
|| sector_type == SectorType::Mode2Xa2Gap
|| sector_type == SectorType::Mode2XaGap
{
trace!(
"The sector is a Mode1 XA sector, executing the decode_sector_mode2_xa function."
);
return Ok(self.decode_sector_mode2_xa(
sector,
sector_type,
sector_number,
optimizations,
));
}
// This should not happen
error!("Unknown format detected...");
Err(StatusError::UnknownFormat(format!("{:?}", sector_type)))
}
///
/// Decodes a CDDA sector previously encoded sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
/// * **sector_type:** The CDDA sector type
/// * **optimizations:** Optimizations used during the encoding process.
///
/// # Return
///
/// **Vec(u8):** Decoded stream.
///
fn decode_sector_cdda(
&mut self,
sector: &[u8],
sector_type: SectorType,
optimizations: Optimizations,
) -> Vec<u8> {
debug!("Decoding a CDDA sector");
// If the sector type is CDDA or the GAP was not optimized, justo copy it.
let copy_needed =
!optimizations.contains(Optimizations::RemoveGap) || sector_type == SectorType::Cdda;
if copy_needed {
trace!(
"The sector is RAW or the RemoveGap optimization was not enabled. Copying the sector..."
);
self.decoded_buffer.copy_from_slice(sector);
}
// The sector was zeroed into the decode_sector function, so no GAP generation is required.
// Return the buffer containing the copied data or the zeroed sector.
debug!("Returning the generated decoded data.");
self.decoded_buffer.to_vec()
}
///
/// Decodes a Mode 1 sector previously encoded sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
/// * **sector_type:** The Mode 1 sector type
/// * **sector_number:** Used to generate the MSF data. Not required if the RemoveMSF optimization was not used.
/// * **optimizations:** Optimizations used during the encoding process.
///
/// # Return
///
/// **Vec(u8):** Decoded stream.
///
fn decode_sector_mode1(
&mut self,
sector: &[u8],
sector_type: SectorType,
sector_number: u32,
optimizations: Optimizations,
) -> Vec<u8> {
debug!("Decoding a sector Mode 1");
// Store the current reader offset
let mut current_offset: usize = 0;
// The sync data is fixed, so we will just generate the data
trace!("Adding the SYNC data.");
self.decoded_buffer[0..=0xB].copy_from_slice(SYNC_DATA);
// If the optimization wasn't enabled, sync the reader offset
if !optimizations.contains(Optimizations::RemoveSync) {
trace!("The source data contains the SYNC data. FF the position.");
current_offset += 12;
}
// MSF Data
if !optimizations.contains(Optimizations::RemoveMSF) {
// If the optimization is disabled, just copy the data
trace!("Copying the MSF data from source.");
self.decoded_buffer[0xC..=0xE]
.copy_from_slice(§or[current_offset..current_offset + 3]);
current_offset += 3;
} else {
// Else generate the the MSF using the provided sector_number
trace!("Generating the MSF data.");
self.decoded_buffer[0xC..=0xE].copy_from_slice(&MSF::sectors_to_msf(sector_number));
}
// The mode data is also fixed in this case to 0x01
trace!("Adding the mode byte.");
self.decoded_buffer[0xF] = 0x01;
// If the optimization wasn't enable, sync the reader offset
if !optimizations.contains(Optimizations::RemoveMode) {
trace!("The source file containes the mode byte. FF the position.");
current_offset += 1;
}
// The sector data depends of the sector type and if the gap optimization is enabled
if !optimizations.contains(Optimizations::RemoveGap)
|| sector_type == SectorType::Mode1
|| sector_type == SectorType::Mode1Raw
{
if sector_type == SectorType::Mode1Raw {
trace!("Copying the RAW data of a Mode1 RAW sector.");
self.decoded_buffer[0x10..=0x92F]
.copy_from_slice(§or[current_offset..current_offset + 2336]);
// The sector is now complete, so there is no reason to continue evaluating
debug!("Returning the generated decoded data (Mode1 Raw).");
return self.decoded_buffer.to_vec();
} else {
trace!("Copying the data of a Mode1 sector.");
// The sector is a Mode1 Standard or a GAP without optimization. Copy the data block...
self.decoded_buffer[0x10..=0x80F]
.copy_from_slice(§or[current_offset..current_offset + 2048]);
current_offset += 2048;
}
}
// The sector must be a GAP sector optimized
else if sector_type == SectorType::Mode1Gap {
trace!("The sector is a Mode1 GAP optimized, so data will be generated.");
self.decoded_buffer[0x10..=0x80F].fill(0);
}
// The EDC Data
if !optimizations.contains(Optimizations::RemoveEDC) {
trace!("Copying the EDC data.");
self.decoded_buffer[0x810..=0x813]
.copy_from_slice(§or[current_offset..current_offset + 4]);
current_offset += 4;
} else {
// We have to generate the EDC
trace!("Generating the EDC data.");
let (left, right) = self.decoded_buffer.split_at_mut(0x810);
let edc = self.edc_ecc_calculator.generate_edc(&left[..=0x80F]);
right[0..4].copy_from_slice(&edc.to_le_bytes());
}
// Blank block is always full of zeroes. Generate it...
trace!("Generating the Blank data.");
self.decoded_buffer[0x814..=0x81B].fill(0);
// And sync the reader offset if the optimization was off
if !optimizations.contains(Optimizations::RemoveBlanks) {
trace!("Blank data was not optimized. FF the source sector.");
current_offset += 8;
}
// The ECC Data
if !optimizations.contains(Optimizations::RemoveECC) {
trace!("Copying the ECC data.");
self.decoded_buffer[0x81C..=0x92F]
.copy_from_slice(§or[current_offset..current_offset + 276]);
} else {
trace!("Generating the ECC data.");
let mut generated_ecc_p = [0u8; 172];
let mut generated_ecc_q = [0u8; 104];
// We must generate the ECC P data
self.edc_ecc_calculator.generate_ecc_pq(
&self.decoded_buffer[0x0C..=0xF],
&self.decoded_buffer[0x10..=0x81B],
&mut generated_ecc_p,
86,
24,
2,
86,
);
// Write the ECC P data which is used to calculate the ECC Q
self.decoded_buffer[0x81C..=0x8C7].copy_from_slice(&generated_ecc_p);
// Now we must generate the ECC Q data using the sector data including the ECC P data.
self.edc_ecc_calculator.generate_ecc_pq(
&self.decoded_buffer[0x0C..=0xF],
&self.decoded_buffer[0x10..=0x8C7],
&mut generated_ecc_q,
52,
43,
86,
88,
);
// Write the ECC Q data
self.decoded_buffer[0x8C8..=0x92F].copy_from_slice(&generated_ecc_q);
}
// Return the decoded sector buffer
debug!("Returning the generated decoded data.");
self.decoded_buffer.to_vec()
}
///
/// Decodes a Mode 2 sector previously encoded sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
/// * **sector_type:** The Mode 2 sector type
/// * **sector_number:** Used to generate the MSF data. Not required if the RemoveMSF optimization was not used.
/// * **optimizations:** Optimizations used during the encoding process.
///
/// # Return
///
/// **Vec(u8):** Decoded stream.
///
fn decode_sector_mode2(
&mut self,
sector: &[u8],
sector_type: SectorType,
sector_number: u32,
optimizations: Optimizations,
) -> Vec<u8> {
debug!("Decoding a sector Mode 2");
// Store the current reader offset
let mut current_offset: usize = 0;
trace!("Adding the SYNC data.");
// The sync data is fixed, so we will just generate the data
self.decoded_buffer[0..=0xB].copy_from_slice(SYNC_DATA);
// If the optimization wasn't enabled, sync the reader offset
if !optimizations.contains(Optimizations::RemoveSync) {
trace!("The source data contains the SYNC data. FF the position.");
current_offset += 12;
}
// MSF Data
if !optimizations.contains(Optimizations::RemoveMSF) {
// If the optimization is disabled, just copy the data
trace!("Copying the MSF data from source.");
self.decoded_buffer[0xC..=0xE]
.copy_from_slice(§or[current_offset..current_offset + 3]);
current_offset += 3;
} else {
// Else generate the the MSF using the provided sector_number
trace!("Generating the MSF data.");
self.decoded_buffer[0xC..=0xE].copy_from_slice(&MSF::sectors_to_msf(sector_number));
}
// The mode data is also fixed in this case to 0x02
trace!("Adding the mode byte.");
self.decoded_buffer[0xF] = 0x02;
// If the optimization wasn't enable, sync the reader offset
if !optimizations.contains(Optimizations::RemoveMode) {
trace!("The source file containes the mode byte. FF the position.");
current_offset += 1;
}
// If the Gap optimization is not enabled or the sector is a Mode2, copy all the data
if !optimizations.contains(Optimizations::RemoveGap) || sector_type == SectorType::Mode2 {
trace!("Copying the data of a Mode2 sector.");
self.decoded_buffer[0x10..=0x92F]
.copy_from_slice(§or[current_offset..current_offset + 2336]);
} else if sector_type == SectorType::Mode2Gap {
trace!("Generating the data of a Mode2 GAP optimized sector.");
self.decoded_buffer[0x10..=0x92F].fill(0);
}
debug!("Returning the generated decoded data.");
self.decoded_buffer.to_vec()
}
///
/// Decodes a Mode 2 XA sector previously encoded sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
/// * **sector_type:** The Mode 2 XA sector type
/// * **sector_number:** Used to generate the MSF data. Not required if the RemoveMSF optimization was not used.
/// * **optimizations:** Optimizations used during the encoding process.
///
/// # Return
///
/// **Vec(u8):** Decoded stream.
///
fn decode_sector_mode2_xa(
&mut self,
sector: &[u8],
sector_type: SectorType,
sector_number: u32,
optimizations: Optimizations,
) -> Vec<u8> {
debug!("Decoding a sector Mode 2 XA");
// Store the current reader offset
let mut current_offset: usize = 0;
trace!("Adding the SYNC data.");
// The sync data is fixed, so we will just generate the data
self.decoded_buffer[0..=0xB].copy_from_slice(SYNC_DATA);
// If the optimization wasn't enabled, sync the reader offset
if !optimizations.contains(Optimizations::RemoveSync) {
trace!("The source data contains the SYNC data. FF the position.");
current_offset += 12;
}
// MSF Data
if !optimizations.contains(Optimizations::RemoveMSF) {
// If the optimization is disabled, just copy the data
trace!("Copying the MSF data from source.");
self.decoded_buffer[0xC..=0xE]
.copy_from_slice(§or[current_offset..current_offset + 3]);
current_offset += 3;
} else {
// Else generate the the MSF using the provided sector_number
trace!("Generating the MSF data.");
self.decoded_buffer[0xC..=0xE].copy_from_slice(&MSF::sectors_to_msf(sector_number));
}
// The mode data is also fixed in this case to 0x02
trace!("Adding the mode byte.");
self.decoded_buffer[0xF] = 0x02;
// If the optimization wasn't enable, sync the reader offset
if !optimizations.contains(Optimizations::RemoveMode) {
trace!("The source file containes the mode byte. FF the position.");
current_offset += 1;
}
// If the optimization is not enabled, copy all the data
if !optimizations.contains(Optimizations::RemoveRedundantFlag) {
trace!("Copying both Redundant Flags.");
self.decoded_buffer[0x10..=0x17]
.copy_from_slice(§or[current_offset..current_offset + 8]);
current_offset += 8;
}
// If the optimization is enabled, copy the single source flag douplicated
else {
trace!("Copying one Redundant Flag and duplicating the data.");
self.decoded_buffer[0x10..=0x13]
.copy_from_slice(§or[current_offset..current_offset + 4]);
self.decoded_buffer[0x14..=0x17]
.copy_from_slice(§or[current_offset..current_offset + 4]);
current_offset += 4;
}
// Copy the data depending of the sector type
if sector_type == SectorType::Mode2XaGap {
trace!("Generating the zeroed data present in a Mode2 XA GAP sector.");
// XA Gap is full zeroes, so will be just generated
self.decoded_buffer[0x18..=0x817].fill(0);
// No more data will be processed in this kind of sector
trace!("Returning the generated decoded data (Mode 2 XA Gap).");
return self.decoded_buffer.to_vec();
} else if sector_type == SectorType::Mode2Xa1 || sector_type == SectorType::Mode2Xa1Gap {
// If sector type is Mode2 XA1 then process 2048 bytes
if !optimizations.contains(Optimizations::RemoveGap)
|| sector_type == SectorType::Mode2Xa1
{
trace!("Copying the Mode2 XA1 data.");
self.decoded_buffer[0x18..=0x817]
.copy_from_slice(§or[current_offset..current_offset + 2048]);
current_offset += 2048;
} else {
trace!("Generating the the Mode2 XA1 GAP zeroed data.");
self.decoded_buffer[0x18..=0x817].fill(0);
}
} else {
// Else the sector is Mode2 XA2 and the data size is 2324 bytes
if !optimizations.contains(Optimizations::RemoveGap)
|| sector_type == SectorType::Mode2Xa2
{
trace!("Copying the Mode2 XA2 data.");
self.decoded_buffer[0x18..=0x92B]
.copy_from_slice(§or[current_offset..current_offset + 2324]);
current_offset += 2324;
} else {
trace!("Generating the the Mode2 XA2 GAP zeroed data.");
self.decoded_buffer[0x18..=0x92B].fill(0);
}
}
// EDC Data
if sector_type == SectorType::Mode2Xa1 || sector_type == SectorType::Mode2Xa1Gap {
// Mode2 XA1 sectors
if !optimizations.contains(Optimizations::RemoveEDC) {
trace!("Copying the EDC data Mode2 XA1.");
self.decoded_buffer[0x818..=0x81B]
.copy_from_slice(§or[current_offset..current_offset + 4]);
current_offset += 4;
} else {
// We have to generate the EDC
trace!("Generating the EDC data Mode2 XA1.");
let (left, right) = self.decoded_buffer.split_at_mut(0x818);
let edc = self.edc_ecc_calculator.generate_edc(&left[0x10..=0x817]);
right[0..4].copy_from_slice(&edc.to_le_bytes());
}
} else {
// Else the sector is Mode2 XA2
if !optimizations.contains(Optimizations::RemoveEDC) {
trace!("Copying the EDC data Mode2 XA2.");
self.decoded_buffer[0x92C..=0x92F]
.copy_from_slice(§or[current_offset..current_offset + 4]);
} else {
// We have to generate the EDC
trace!("Generating the EDC data Mode2 XA2.");
let edc = self
.edc_ecc_calculator
.generate_edc(&self.decoded_buffer[0x10..=0x92B]);
self.decoded_buffer[0x92C..=0x92F].copy_from_slice(&edc.to_le_bytes());
}
// Mode2 XA2 doesn't have any other data. Just return the data.
debug!("Returning the generated decoded data Mode2 XA2.");
return self.decoded_buffer.to_vec();
}
// Mode2 XA1 sector. We must copy/generate the ECC data
if !optimizations.contains(Optimizations::RemoveECC) {
trace!("Copying the ECC data Mode2 XA1.");
self.decoded_buffer[0x81C..=0x92F]
.copy_from_slice(§or[current_offset..current_offset + 276]);
} else {
trace!("Generating the ECC data Mode2 XA1.");
let mut generated_ecc_p = [0u8; 172];
let mut generated_ecc_q = [0u8; 104];
let address = [0; 4];
// We must generate the ECC P data
self.edc_ecc_calculator.generate_ecc_pq(
&address,
&self.decoded_buffer[0x10..=0x81B],
&mut generated_ecc_p,
86,
24,
2,
86,
);
// Write the ECC P data which is used to calculate the ECC Q
self.decoded_buffer[0x81C..=0x8C7].copy_from_slice(&generated_ecc_p);
// Now we must generate the ECC Q data using the sector data including the ECC P data.
self.edc_ecc_calculator.generate_ecc_pq(
&address,
&self.decoded_buffer[0x10..=0x8C7],
&mut generated_ecc_q,
52,
43,
86,
88,
);
// Write the ECC Q data
self.decoded_buffer[0x8C8..=0x92F].copy_from_slice(&generated_ecc_q);
}
debug!("Returning the generated decoded data.");
self.decoded_buffer.to_vec()
}
///
/// Determines the expected encoded size of a sector based on their optimizations.
///
/// # Arguments
///
/// * **sector_type:** The sector type
/// * **optimizations:** The optimizations used in the process
///
/// Return
///
/// **Usize:** Expected size of the optimized sector
///
pub fn get_encoded_size(&self, sector_type: SectorType, optimizations: Optimizations) -> usize {
use SectorType::*;
debug!(
"Provided sector type {:?} with the optimizations {:?}",
sector_type, optimizations
);
// Easily calculated sector modes
match sector_type {
CddaGap => {
return if optimizations.contains(Optimizations::RemoveGap) {
trace!("Sector is a GAP CDDA with the optimization RemoveGap. Size = 0.");
0
} else {
trace!("Sector is a GAP CDDA without the optimization RemoveGap. Size = 2352.");
2352
};
}
Cdda => {
trace!("Sector is a CDDA. Size = 2352.");
return 2352;
}
Mode1Raw => {
trace!("Sector is a Mode1RAW. Size = 2336 + variables.");
return 2336 + {
let mut size = 0;
if !optimizations.contains(Optimizations::RemoveSync) {
trace!("Adding sync size: 12");
size += 12;
}
if !optimizations.contains(Optimizations::RemoveMSF) {
trace!("Adding MSF size: 3");
size += 3;
}
if !optimizations.contains(Optimizations::RemoveMode) {
trace!("Adding Mode size: 1");
size += 1;
}
trace!("Final size: {}", size);
size
};
}
Mode2 | Mode2Gap => {
return {
trace!("Sector is a Mode2 or Mode2Gap.");
let mut size = 0;
if !optimizations.contains(Optimizations::RemoveSync) {
trace!("Adding sync size: 12");
size += 12;
}
if !optimizations.contains(Optimizations::RemoveMSF) {
trace!("Adding MSF size: 3");
size += 3;
}
if !optimizations.contains(Optimizations::RemoveMode) {
trace!("Adding Mode size: 1");
size += 1;
}
if !optimizations.contains(Optimizations::RemoveGap) || sector_type == Mode2 {
trace!("Adding sector data size: 2336");
size += 2336;
}
trace!("Final size: {}", size);
size
};
}
_ => {}
}
// The other needs to determine the size based in optimizations
let mut size = 0;
// Common optimizations (SYNC, MSF and Mode)
if !optimizations.contains(Optimizations::RemoveSync) {
trace!("Adding sync size: 12");
size += 12;
}
if !optimizations.contains(Optimizations::RemoveMSF) {
trace!("Adding MSF size: 3");
size += 3;
}
if !optimizations.contains(Optimizations::RemoveMode) {
trace!("Adding Mode size: 1");
size += 1;
}
// Redundant flags (XA only)
if matches!(
sector_type,
Mode2Xa1 | Mode2Xa1Gap | Mode2Xa2 | Mode2Xa2Gap | Mode2XaGap
) {
size += if optimizations.contains(Optimizations::RemoveRedundantFlag) {
trace!("Adding a single flag size: 4");
4
} else {
trace!("Adding both flags size: 8");
8
};
}
// Data area
if optimizations.contains(Optimizations::RemoveGap) {
size += match sector_type {
Mode1 => 2048,
Mode2Xa1 => 2048,
Mode2Xa2 => 2324,
_ => 0,
};
} else {
size += match sector_type {
Mode1 | Mode1Gap => 2048,
Mode2Xa1 | Mode2Xa1Gap => 2048,
Mode2Xa2 | Mode2Xa2Gap => 2324,
Mode2XaGap => 2328,
_ => 0,
};
}
// EDC
if !optimizations.contains(Optimizations::RemoveEDC) {
if matches!(
sector_type,
Mode1 | Mode1Gap | Mode2Xa1 | Mode2Xa1Gap | Mode2Xa2 | Mode2Xa2Gap
) {
size += 4;
}
}
// Blanks
if !optimizations.contains(Optimizations::RemoveBlanks)
&& matches!(sector_type, Mode1 | Mode1Gap)
{
size += 8;
}
// ECC
if !optimizations.contains(Optimizations::RemoveECC)
&& matches!(sector_type, Mode1 | Mode1Gap | Mode2Xa1 | Mode2Xa1Gap)
{
size += 276;
}
trace!("Final size: {}", size);
size
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(serde::Deserialize)]
struct RawCheck {
sector_type: String,
optimizations: Vec<String>,
expected_len: usize,
description: String,
}
struct Check {
sector_type: SectorType,
optimizations: Optimizations,
expected_len: usize,
description: String,
}
impl From<RawCheck> for Check {
fn from(raw: RawCheck) -> Self {
let sector_type = match raw.sector_type.as_str() {
"Cdda" => SectorType::Cdda,
"CddaGap" => SectorType::CddaGap,
"Mode1" => SectorType::Mode1,
"Mode1Gap" => SectorType::Mode1Gap,
"Mode1Raw" => SectorType::Mode1Raw,
"Mode2" => SectorType::Mode2,
"Mode2Gap" => SectorType::Mode2Gap,
"Mode2Xa1" => SectorType::Mode2Xa1,
"Mode2Xa1Gap" => SectorType::Mode2Xa1Gap,
"Mode2Xa2" => SectorType::Mode2Xa2,
"Mode2Xa2Gap" => SectorType::Mode2Xa2Gap,
"Mode2XaGap" => SectorType::Mode2XaGap,
_ => panic!("Unknown SectorType {}", raw.sector_type),
};
let mut opt = Optimizations::empty();
for o in raw.optimizations {
opt |= match o.as_str() {
"None" => Optimizations::None,
"RemoveGap" => Optimizations::RemoveGap,
"RemoveSync" => Optimizations::RemoveSync,
"RemoveMSF" => Optimizations::RemoveMSF,
"RemoveMode" => Optimizations::RemoveMode,
"RemoveEDC" => Optimizations::RemoveEDC,
"RemoveBlanks" => Optimizations::RemoveBlanks,
"RemoveECC" => Optimizations::RemoveECC,
"RemoveRedundantFlag" => Optimizations::RemoveRedundantFlag,
_ => panic!("Unknown optimization {}", o),
};
}
Check {
sector_type,
optimizations: opt,
expected_len: raw.expected_len,
description: raw.description,
}
}
}
#[test]
fn check_encoded_size() {
// Initialize the encoder
let decoder = Decoder::new();
let json = std::fs::read_to_string("tests/data/check_encoded_size.json").unwrap();
let raw_rows: Vec<(String, Vec<String>, usize, String)> =
serde_json::from_str(&json).unwrap();
let checks: Vec<Check> = raw_rows
.into_iter()
.map(|(sector_type, optimizations, expected_len, description)| {
RawCheck {
sector_type,
optimizations,
expected_len,
description,
}
.into()
})
.collect();
for check in &checks {
let encoded_size = decoder.get_encoded_size(check.sector_type, check.optimizations);
assert_eq!(
encoded_size, check.expected_len,
"The sector size estimation is incorrect for {}.",
check.description
);
println!(
"The sector size for {} was correctly estimated: {}",
check.description, encoded_size
);
}
}
}