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.
This commit is contained in:
Matteo Cypriani 2016-12-08 20:52:16 -05:00
parent 3a970c3e33
commit 3faf42e2a7
2 changed files with 18 additions and 2 deletions

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)

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)
}