Better handling of unknown chunks

This commit is contained in:
Matteo Cypriani 2016-12-06 13:45:55 -05:00
parent e2a41e6282
commit 423aec0a63
2 changed files with 32 additions and 14 deletions

View File

@ -17,6 +17,7 @@ pub struct NrgMetadata {
pub daox_chunk: Option<NrgDaox>,
pub sinf_chunk: Option<NrgSinf>,
pub mtyp_chunk: Option<NrgMtyp>,
pub skipped_chunks: Vec<String>,
}
impl NrgMetadata {
@ -29,6 +30,7 @@ impl NrgMetadata {
daox_chunk: None,
sinf_chunk: None,
mtyp_chunk: None,
skipped_chunks: Vec::new(),
}
}
}
@ -62,6 +64,12 @@ impl fmt::Display for NrgMetadata {
Some(ref chunk) => try!(write!(f, "\n\n\
{}", chunk)),
}
if !self.skipped_chunks.is_empty() {
try!(write!(f, "\n\nUnhandled chunks present in this image:"));
for chunk_id in &self.skipped_chunks {
try!(write!(f, " {}", chunk_id));
}
}
Ok(())
}
}

View File

@ -75,28 +75,39 @@ fn read_nrg_version(fd: &mut File, file_size: u64) -> Result<u8, NrgError> {
/// Reads all the available NRG chunks.
///
/// Returns the number of chunks read.
fn read_nrg_chunks(fd: &mut File, nm: &mut NrgMetadata) -> Result<u16, NrgError> {
let mut nread = 0u16;
fn read_nrg_chunks(fd: &mut File, nm: &mut NrgMetadata) -> Result<(), NrgError> {
loop {
let chunk_id = try!(read_nrg_chunk_id(fd));
nread += 1;
match chunk_id.as_ref() {
"END!" => break,
"CUEX" => { nm.cuex_chunk = Some(try!(cuex::read_nrg_cuex(fd))); },
"DAOX" => { nm.daox_chunk = Some(try!(daox::read_nrg_daox(fd))); },
"CDTX" => { try!(skip_unhandled_chunk(fd, &chunk_id)); },
"ETN2" => { try!(skip_unhandled_chunk(fd, &chunk_id)); },
"CDTX" => {
try!(skip_chunk(fd));
nm.skipped_chunks.push(chunk_id);
},
"ETN2" => {
try!(skip_chunk(fd));
nm.skipped_chunks.push(chunk_id);
},
"SINF" => { nm.sinf_chunk = Some(try!(sinf::read_nrg_sinf(fd))); },
"MTYP" => { nm.mtyp_chunk = Some(try!(mtyp::read_nrg_mtyp(fd))); },
"DINF" => { try!(skip_unhandled_chunk(fd, &chunk_id)); },
"TOCT" => { try!(skip_unhandled_chunk(fd, &chunk_id)); },
"RELO" => { try!(skip_unhandled_chunk(fd, &chunk_id)); },
"DINF" => {
try!(skip_chunk(fd));
nm.skipped_chunks.push(chunk_id);
},
"TOCT" => {
try!(skip_chunk(fd));
nm.skipped_chunks.push(chunk_id);
},
"RELO" => {
try!(skip_chunk(fd));
nm.skipped_chunks.push(chunk_id);
},
_ => { println!("{}", chunk_id); return Err(NrgError::NrgChunkId); }, //fixme
}
}
Ok(nread)
Ok(())
}
@ -106,10 +117,9 @@ fn read_nrg_chunk_id(fd: &File) -> Result<String, NrgError> {
}
fn skip_unhandled_chunk(fd: &mut File, chunk_id: &str) -> Result<(), NrgError> {
/// Skips a chunk.
fn skip_chunk(fd: &mut File) -> Result<(), NrgError> {
let chunk_size = try!(read_u32(fd));
try!(fd.seek(SeekFrom::Current(chunk_size as i64)));
// fixme: lib should'nt print!
println!("Skipping unhandled chunk: {} ({} bytes)", chunk_id, chunk_size);
Ok(())
}