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
use crate::common::{EccEdc, MSF, Optimizations, SectorType, StatusError};
use log::{debug, error, trace};
///
/// Encoder implementation to optimize the CD-ROM sectors data and reduce their size.
/// This "compression" produces a lossless encoding method.
///
pub struct Encoder {
optimizations: Optimizations,
applicable_optimizations: Optimizations,
encoded_buffer: [u8; 2352],
sector_type: SectorType,
edc_ecc_calculator: EccEdc,
}
impl Encoder {
///
/// Initialize the encoder library generating the required data.
///
/// # Arguments
///
/// * **optimizations**: The desired optimizations to apply to the encoded sectors.
///
/// **NOTE**: The optimizations can vary depending of the sector type and the state.
/// Use the method get_last_used_optimizations() to return the last used optimizations.
///
/// # Return
///
/// **Encoder:** A new object instance to be used to encode the CD-ROM sectors with the provided optimizations.
///
/// # Example
///
/// ```text
/// let encoder = Encoder::new();
/// ```
pub fn new(optimizations: Optimizations) -> Self {
Encoder {
optimizations: optimizations,
applicable_optimizations: Optimizations::None,
encoded_buffer: [0; 2352],
sector_type: SectorType::Unknown,
edc_ecc_calculator: EccEdc::new(),
}
}
///
/// Returns the detected sector type during the encoding. This method must be used efter the encoding process.
///
/// # Arguments
///
/// **None**
///
/// # Return
///
/// **SectorType:** The detected sector type during the last encoding.
///
pub fn get_sector_type(&self) -> SectorType {
self.sector_type
}
///
/// Change the configured optimizations on-the-fly to be used in future encodings.
///
/// # Arguments
///
/// * **optimizations:** New optimizations to be used
///
/// # Return
///
/// **()**
///
pub fn set_optimizations(&mut self, optimizations: Optimizations) {
self.optimizations = optimizations;
}
///
/// Get the last used optimizations in the encoding process. These optimizations can differ to the configured optimizations.
///
/// During the encoding process, the optimizations are checked to ensure that can be applied. If not, they are changed on-the-fly,
/// and the way to return the last used optimizations is to use this function. The returned optimizations will be the real one used
/// during the encoding process and must be provided to de decoder.
///
/// # Arguments
///
/// **None**
///
/// # Return
///
/// **Optimizations:** The last used optimizations
///
pub fn get_last_used_optimizations(&self) -> Optimizations {
self.applicable_optimizations
}
///
/// Encodes a sector removing the non essential data based on the configured optimizations.
///
/// # Arguments
///
/// * **sector:** The sector data to be optimized
/// * **sector_number:** The sector number used to detect if the optimization is possible.
/// * **sector_type:** The sector type to process. Useful to skip the detection step if you already know the sector type.
/// * **force:** force the encoding without checking the optimizations
///
/// # Return
///
/// **Vec(u8):** Encoded stream.
///
pub fn encode_sector(
&mut self,
sector: &[u8],
sector_number: u32,
sector_type: Option<SectorType>,
force: bool,
) -> Result<Vec<u8>, StatusError> {
debug!("Encoding the sector.");
// Check if there is enough data to work with it.
if sector.len() < 2352 {
return Err(StatusError::NotEnoughSectorData);
}
// Get the sector type
match sector_type {
Some(s_type) => {
trace!("The sector type was provided by the user: {:?}", s_type);
self.sector_type = s_type;
}
None => {
trace!("Detecting the sector type.");
self.sector_type = self.detect_sector_type(sector).unwrap();
trace!("The detected type is: {:?}", self.sector_type);
}
}
// Validate the optimizations if force is false
self.applicable_optimizations = if !force {
debug!("Force is false so optimizations will be tested.");
self.check_optimizations(sector, sector_number, self.sector_type, self.optimizations)
} else {
debug!("Force is true. The provided optimizations will be used.");
self.optimizations
};
// Reset the output buffer
trace!("Clearing the output buffer.");
self.encoded_buffer = [0; 2352];
// Process the CDDA sector using the optimizations
if self.sector_type == SectorType::Cdda || self.sector_type == SectorType::CddaGap {
trace!("Running the CDDA encoding function");
return Ok(self.encode_sector_cdda(sector));
}
// Process the Mode1 sector using the optimizations
if self.sector_type == SectorType::Mode1
|| self.sector_type == SectorType::Mode1Gap
|| self.sector_type == SectorType::Mode1Raw
{
trace!("Running the Mode1 encoding function");
return Ok(self.encode_sector_mode1(sector));
}
if self.sector_type == SectorType::Mode2 || self.sector_type == SectorType::Mode2Gap {
trace!("Running the Mode2 encoding function");
return Ok(self.encode_sector_mode2(sector));
}
if self.sector_type == SectorType::Mode2Xa1
|| self.sector_type == SectorType::Mode2Xa1Gap
|| self.sector_type == SectorType::Mode2Xa2
|| self.sector_type == SectorType::Mode2Xa2Gap
|| self.sector_type == SectorType::Mode2XaGap
{
trace!("Running the Mode2 XA encoding function");
return Ok(self.encode_sector_mode2_xa(sector));
}
// This should not happen
error!("Unknown format detected...");
Err(StatusError::UnknownFormat(format!(
"{:?}",
self.sector_type
)))
}
///
/// Encodes a CDDA sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
///
/// # Return
///
/// **Vec(u8):** Encoded stream.
///
fn encode_sector_cdda(&mut self, sector: &[u8]) -> Vec<u8> {
match (
self.sector_type,
self.applicable_optimizations
.contains(Optimizations::RemoveGap),
) {
// If the sector is a GAP and the GAP optimization is enabled return empty
(SectorType::CddaGap, true) => Vec::new(),
// Otherwise copy the sector
_ => {
self.encoded_buffer.copy_from_slice(sector);
self.encoded_buffer.to_vec()
}
}
}
///
/// Encodes a Mode 1 sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
///
/// # Return
///
/// **Vec(u8):** Encoded stream.
///
fn encode_sector_mode1(&mut self, sector: &[u8]) -> Vec<u8> {
let mut current_offset: usize = 0;
// Sync data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveSync)
{
self.encoded_buffer[current_offset..current_offset + 12]
.copy_from_slice(§or[0x0..=0xB]);
current_offset += 12;
}
// MSF data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveMSF)
{
self.encoded_buffer[current_offset..current_offset + 3]
.copy_from_slice(§or[0xC..=0xE]);
current_offset += 3;
}
// Mode data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveMode)
{
self.encoded_buffer[current_offset] = sector[0xF];
current_offset += 1;
}
// Sector Data
// The Mode1 RAW sector contains full random data from this point to the end
// Copy the entire sector and return the slice
if self.sector_type == SectorType::Mode1Raw {
self.encoded_buffer[current_offset..current_offset + 2336]
.copy_from_slice(§or[0x10..=0x92F]);
current_offset += 2336;
return self.encoded_buffer[..current_offset].to_vec();
}
// The other Mode1 sectors allows to remove more data so we will continue
if !self
.applicable_optimizations
.contains(Optimizations::RemoveGap)
|| self.sector_type == SectorType::Mode1
{
self.encoded_buffer[current_offset..current_offset + 2048]
.copy_from_slice(§or[0x10..=0x80F]);
current_offset += 2048;
}
// EDC data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveEDC)
{
self.encoded_buffer[current_offset..current_offset + 4]
.copy_from_slice(§or[0x810..=0x813]);
current_offset += 4;
}
// Blanks data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveBlanks)
{
self.encoded_buffer[current_offset..current_offset + 8]
.copy_from_slice(§or[0x814..=0x81B]);
current_offset += 8;
}
// ECC data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveECC)
{
self.encoded_buffer[current_offset..current_offset + 276]
.copy_from_slice(§or[0x81C..=0x92F]);
current_offset += 276;
}
// Return the stored data
self.encoded_buffer[..current_offset].to_vec()
}
///
/// Encodes a Mode 2 sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
///
/// # Return
///
/// **Vec(u8):** Encoded stream.
///
fn encode_sector_mode2(&mut self, sector: &[u8]) -> Vec<u8> {
let mut current_offset: usize = 0;
// Sync data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveSync)
{
self.encoded_buffer[current_offset..current_offset + 12]
.copy_from_slice(§or[0x0..=0xB]);
current_offset += 12;
}
// MSF data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveMSF)
{
self.encoded_buffer[current_offset..current_offset + 3]
.copy_from_slice(§or[0xC..=0xE]);
current_offset += 3;
}
// Mode data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveMode)
{
self.encoded_buffer[current_offset] = sector[0xF];
current_offset += 1;
}
// Sector Data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveGap)
|| self.sector_type == SectorType::Mode2
{
self.encoded_buffer[current_offset..current_offset + 2336]
.copy_from_slice(§or[0x10..=0x92F]);
current_offset += 2336;
}
// Return the stored data
self.encoded_buffer[..current_offset].to_vec()
}
///
/// Encodes a Mode 2 XA sector using the provided optimizations
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be procesed
///
/// # Return
///
/// **Vec(u8):** Encoded stream.
///
fn encode_sector_mode2_xa(&mut self, sector: &[u8]) -> Vec<u8> {
let mut current_offset: usize = 0;
// Sync data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveSync)
{
self.encoded_buffer[current_offset..current_offset + 12]
.copy_from_slice(§or[0x0..=0xB]);
current_offset += 12;
}
// MSF data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveMSF)
{
self.encoded_buffer[current_offset..current_offset + 3]
.copy_from_slice(§or[0xC..=0xE]);
current_offset += 3;
}
// Mode data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveMode)
{
self.encoded_buffer[current_offset] = sector[0xF];
current_offset += 1;
}
// Flags data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveRedundantFlag)
{
debug!("Copying both XA Flags");
self.encoded_buffer[current_offset..current_offset + 8]
.copy_from_slice(§or[0x10..=0x17]);
current_offset += 8;
} else {
debug!("Copying just one copy of the XA Flags");
self.encoded_buffer[current_offset..current_offset + 4]
.copy_from_slice(§or[0x10..=0x13]);
current_offset += 4;
}
// Sector Data
// If remove GAP is not enabled or the sector is a data sector, copy the data block
if !self
.applicable_optimizations
.contains(Optimizations::RemoveGap)
|| self.sector_type == SectorType::Mode2Xa1
|| self.sector_type == SectorType::Mode2Xa2
{
// The XA GAP is fully zeroed, so will be copied only if no GAP optimization is enabled
if self.sector_type == SectorType::Mode2XaGap {
self.encoded_buffer[current_offset..current_offset + 2328]
.copy_from_slice(§or[0x18..=0x92F]);
current_offset += 2328;
return self.encoded_buffer[..current_offset].to_vec();
}
// The optimization is not enabled or sector is XA1
if self.sector_type == SectorType::Mode2Xa1Gap
|| self.sector_type == SectorType::Mode2Xa1
{
self.encoded_buffer[current_offset..current_offset + 2048]
.copy_from_slice(§or[0x18..=0x817]);
current_offset += 2048;
}
if self.sector_type == SectorType::Mode2Xa2Gap
|| self.sector_type == SectorType::Mode2Xa2
{
self.encoded_buffer[current_offset..current_offset + 2324]
.copy_from_slice(§or[0x18..=0x92B]);
current_offset += 2324;
}
}
// The Mode2 XA Gap doesn't have more data to process. Just return...
if self.sector_type == SectorType::Mode2XaGap {
return self.encoded_buffer[..current_offset].to_vec();
}
// Mode2 XA1/XA2 must continue with the EDC/ECC
// EDC data
if !self
.applicable_optimizations
.contains(Optimizations::RemoveEDC)
{
if self.sector_type == SectorType::Mode2Xa1
|| self.sector_type == SectorType::Mode2Xa1Gap
{
// Sector is a Mode2 XA1
self.encoded_buffer[current_offset..current_offset + 4]
.copy_from_slice(§or[0x818..=0x81B]);
} else {
// Sector is a Mode2 XA2
self.encoded_buffer[current_offset..current_offset + 4]
.copy_from_slice(§or[0x92C..=0x92F]);
}
current_offset += 4;
}
// ECC data (Only XA1)
if !self
.applicable_optimizations
.contains(Optimizations::RemoveECC)
&& (self.sector_type == SectorType::Mode2Xa1
|| self.sector_type == SectorType::Mode2Xa1Gap)
{
self.encoded_buffer[current_offset..current_offset + 276]
.copy_from_slice(§or[0x81C..=0x92F]);
current_offset += 276;
}
// Return the stored data
self.encoded_buffer[..current_offset].to_vec()
}
///
/// Analyzes the sector data to determine the sector type.
///
/// # Arguments
///
/// * **sector:** The slice with the sector data to be used
///
/// # Return
///
/// **SectorType:** Sector type detected in the sector.
///
pub fn detect_sector_type(&self, sector: &[u8]) -> Result<SectorType, StatusError> {
// Check if there is enough data to work with it.
if sector.len() < 2352 {
return Err(StatusError::NotEnoughSectorData);
}
if sector[..0x00C]
== [
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
]
{
debug!("Sync data detected... Sector is a data sector.");
// Sector is a MODE1/MODE2 sector
if sector[0x00F] == 0x01
&& sector[0x814..0x81C] == [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
{
// The sector is for sure a MODE1 sector, but we will check the EDC
debug!(
"The sector seems to be a MODE1 sector. Checking the EDC & ECC to be sure of the type."
);
// Check if the generated EDC matches the sector EDC. If not, maybe the sector is damaged or copy protected and will be marked as RAW.
let sector_edc = §or[0x810..0x814];
let generated_edc = self.edc_ecc_calculator.generate_edc(§or[..0x810]);
if generated_edc.to_le_bytes() == sector_edc
&& self.edc_ecc_calculator.check_ecc(
§or[0x0C..0x10],
§or[0x10..],
§or[0x81C..0x930],
)
{
debug!("MODE1 sector detected... Checking if it is a GAP.");
if sector[0x10..0x810].iter().all(|&b| b == 0) {
debug!("The sector is a MODE1 GAP sector.");
return Ok(SectorType::Mode1Gap);
} else {
debug!("The sector is a normal MODE1 sector.");
return Ok(SectorType::Mode1);
}
} else {
debug!(
"The EDC cannot be verified. The sector will be threated as RAW (non compliant)."
);
return Ok(SectorType::Mode1Raw);
}
} else if sector[0x00F] == 0x02 {
// The sector is MODE2, and now we will detect what kind.
//
// Checking for a MODE2 GAP Sector
debug!("Mode 2 sector detected. Checking if is just a GAP");
if sector[0x10..0x930].iter().all(|&b| b == 0) {
debug!("The sector is a MODE2 GAP sector.");
return Ok(SectorType::Mode2Gap);
}
// Checking for a MODE2 XA GAP sector. It's a wrong type, but it is used in some games and can free some space.
debug!("Checking if it's an XA GAP sector type.");
if sector[0x10..0x14] == sector[0x14..0x18]
&& sector[0x18..0x930].iter().all(|&b| b == 0)
{
debug!("Mode 2 XA GAP detected.");
return Ok(SectorType::Mode2XaGap);
}
// Checking for a MODE2 XA1 sector.
let sector_edc = §or[0x818..0x81C];
let generated_edc = self.edc_ecc_calculator.generate_edc(§or[0x10..0x818]);
let address = [0; 4];
if generated_edc.to_le_bytes() == sector_edc
&& self.edc_ecc_calculator.check_ecc(
&address,
§or[0x10..],
§or[0x81C..],
)
{
// It is a MODE2 XA1 sector. Checking if it is a GAP.
if sector[0x18..0x818].iter().all(|&b| b == 0) {
debug!("Mode 2 XA1 GAP detected.");
return Ok(SectorType::Mode2Xa1Gap);
} else {
debug!("Mode 2 XA1 detected.");
return Ok(SectorType::Mode2Xa1);
}
}
// Checking for a MODE2 XA2 sector.
let sector_edc = §or[0x92C..0x930];
let generated_edc = self.edc_ecc_calculator.generate_edc(§or[0x10..0x92C]);
if generated_edc.to_le_bytes() == sector_edc {
// It is a MODE2 XA1 sector. Checking if it is a GAP.
if sector[0x18..0x92C].iter().all(|&b| b == 0) {
debug!("Mode 2 XA2 GAP detected.");
return Ok(SectorType::Mode2Xa2Gap);
} else {
debug!("Mode 2 XA2 detected.");
return Ok(SectorType::Mode2Xa2);
}
}
// No XA detected, so the sector might be a Mode 2 standard sector
debug!("The sector might be a non XA Mode 2 sector.");
return Ok(SectorType::Mode2);
}
} else {
debug!("Raw sector detected... It may be a CDDA sector. Checking for a GAP sector.");
if sector.iter().all(|&b| b == 0) {
debug!("The sector is a CDDA GAP sector.");
return Ok(SectorType::CddaGap);
} else {
debug!("The sector is a normal CDDA sector.");
return Ok(SectorType::Cdda);
}
}
Ok(SectorType::Unknown)
}
///
/// This function will check that is possible to apply all the optimizations to the sector
/// and will remove those that are not applicable.
///
/// # Arguments
///
/// * **sector:** The sector data
/// * **sector_numer:** The sector number to check the MSF
/// * **sector_type:** The sector type to determine the detection mode
/// * **optimizations:** Optimizations that will be applied
///
/// # Result
///
/// **Optimizations:** Applicable optimizations after the checks.
///
pub fn check_optimizations(
&self,
sector: &[u8],
sector_number: u32,
sector_type: SectorType,
optimizations: Optimizations,
) -> Optimizations {
debug!("Checking the applicable optimizations.");
//
// NOTE: The GAP optimization will not be checked because if a sector was detected as GAP
// is because the GAP is there and it is correct. There is no reason to check it again.
//
// The SYNC and Mode are intrinsic to the type of sector so will not be checked too.
//
let mut applicable_optimizations = optimizations;
// A CDDA is just raw data that cannot be processed, so the only applicable optimization is GAP remove and cannot fail.
if sector_type == SectorType::Cdda || sector_type == SectorType::CddaGap {
debug!("The sector is a CDDA type so there are no tests to do.");
return applicable_optimizations;
}
// Now we will check the MSF which is common of all non CDDA sectors
let sector_msf = MSF::sectors_to_msf(sector_number);
trace!(
"Comparing the original MSF ({:02x}{:02x}{:02x}) against the generated MSF ({:02x}{:02x}{:02x}).",
sector[0xC], sector[0xD], sector[0xE], sector_msf[0], sector_msf[1], sector_msf[02]
);
if sector[0xC..0xF] != sector_msf {
trace!("The MSF doesn't matches so will be removed as applicable optimization.");
// The sector number doesn't matches and cannot be optimized.
applicable_optimizations.remove(Optimizations::RemoveMSF);
}
// The MODE1 sector will not be checked:
//
// * MSF was already checked above
// * Blank data is used to determine the mode
// * ECC and EDC were checked to determine the compliant MODE1 sector
//
// The MODE2 sector is all data and the MSF was already checked above, so will be skipped too.
//
// Only the sectors MODE2 XA1/2 will be checked
//
trace!("Comparing the redundant flags to determine if matches.");
if sector_type == SectorType::Mode2Xa1
|| sector_type == SectorType::Mode2Xa1Gap
|| sector_type == SectorType::Mode2Xa2
|| sector_type == SectorType::Mode2Xa2Gap
|| sector_type == SectorType::Mode2Gap
{
// Like the MODE1 sector, the ECC and EDC was checked to determine the correct sector mode so will not be checked again.
// Only the flags must be checked.
if sector[0x10..0x14] != sector[0x14..0x18] {
applicable_optimizations.remove(Optimizations::RemoveRedundantFlag);
}
}
trace!("Applicable optimizations: {:?}", applicable_optimizations);
applicable_optimizations
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::Path;
///
/// Test to determine the correct sector detection
///
#[test]
fn check_sector_detection() {
// Sectors list to be checked
let sectors: [(SectorType, &'static str, &'static str); 10] = [
(SectorType::Cdda, "cdda.bin", "CDDA"),
(SectorType::CddaGap, "cdda_gap.bin", "CDDA GAP"),
(SectorType::Mode1, "mode1.bin", "Mode1"),
(SectorType::Mode1Gap, "mode1_gap.bin", "Mode1 GAP"),
(SectorType::Mode1Raw, "mode1_raw.bin", "Mode1 RAW"),
(SectorType::Mode2, "mode2.bin", "Mode2"),
(SectorType::Mode2Gap, "mode2_gap.bin", "Mode2 GAP"),
(SectorType::Mode2Xa1, "mode2_xa1.bin", "Mode2 XA1"),
(
SectorType::Mode2Xa1Gap,
"mode2_xa1_gap.bin",
"Mode2 XA1 GAP",
),
(SectorType::Mode2XaGap, "mode2_xa_gap.bin", "Mode2 XA GAP"),
];
// Initialize the encoder
let encoder = Encoder::new(Optimizations::None);
// Check every sector
for (sector_type, file, sector_type_str) in §ors {
// Open the file
let path = Path::new("tests/data").join(file);
let sector = fs::read(path).expect("Cannot open test sector.");
// Check that the filesize is correct
assert_eq!(
sector.len(),
2352,
"The sector {} must be 2352 bytes length.",
sector_type_str
);
// Time to verify the detection function.
assert_eq!(
encoder.detect_sector_type(§or).unwrap(),
*sector_type,
"The sector {} was not correctly identified ({}).",
sector_type_str,
file
);
println!("Sector {} detected correcly.", sector_type_str)
}
}
}