Browse Source

Read track number & index as BCD-encoded bytes

The track number and track index from the CUEX chunk are BCD-encoded
(Binary-Coded Decimal). Reading them as binary led to wrong track
numbers starting with track 10.

There is doubt whereas the index number is BCD or not, but two-digit
indices are seldom seen.
master
Matteo Cypriani 4 years ago
parent
commit
3faf42e2a7
2 changed files with 18 additions and 2 deletions
  1. +2
    -2
      src/metadata/cuex.rs
  2. +16
    -0
      src/metadata/readers.rs

+ 2
- 2
src/metadata/cuex.rs View File

@ -153,8 +153,8 @@ pub fn read_nrg_cuex(fd: &mut File) -> Result<NrgCuex, NrgError> {
fn read_nrg_cuex_track(fd: &mut File) -> Result<NrgCuexTrack, NrgError> {
let mut track = NrgCuexTrack::new();
track.mode = try!(read_u8(fd));
track.track_number = try!(read_u8(fd));
track.index_number = try!(read_u8(fd));
track.track_number = try!(read_u8_bcd(fd));
track.index_number = try!(read_u8_bcd(fd));
track.padding = try!(read_u8(fd));
track.position_sectors = try!(read_u32(fd)) as i32;
Ok(track)


+ 16
- 0
src/metadata/readers.rs View File

@ -97,3 +97,19 @@ pub fn read_u8(fd: &mut File) -> Result<u8, NrgError> {
try!(fd.read_exact(&mut buf));
Ok(buf[0])
}
/// Reads a BCD-encoded byte from `fd`.
///
/// If the decoded value is more than 99, which is not a valid binary-coded
/// decimal value, the byte read is returned as is, without decoding.
pub fn read_u8_bcd(fd: &mut File) -> Result<u8, NrgError> {
let byte = try!(read_u8(fd));
let tens = (byte >> 4) * 10;
let units = (byte << 4) >> 4;
let value = tens + units;
if value < 100 {
return Ok(value);
}
Ok(byte)
}

Loading…
Cancel
Save