Add a delegating seek() on Reader so we can use the same Reader for more things

This commit is contained in:
Christopher Cole 2022-10-07 15:22:42 -07:00
parent e2ead26514
commit 81112fcd51
No known key found for this signature in database
GPG Key ID: 0AC856975983E9DB
2 changed files with 7 additions and 4 deletions

View File

@ -74,13 +74,13 @@ impl File {
pub fn open_stream<T: Read + Seek>(io_file: &mut T) -> Result<File, ParseError> {
let ehdr = file::FileHeader::parse(io_file)?;
let mut reader = Reader::new(io_file, ehdr.endianness);
// Parse the program headers
io_file.seek(io::SeekFrom::Start(ehdr.e_phoff))?;
reader.seek(io::SeekFrom::Start(ehdr.e_phoff))?;
let mut phdrs = Vec::<segment::ProgramHeader>::default();
for _ in 0..ehdr.e_phnum {
let mut reader = Reader::new(io_file, ehdr.endianness);
let phdr = segment::ProgramHeader::parse(ehdr.class, &mut reader)?;
phdrs.push(phdr);
}
@ -88,9 +88,8 @@ impl File {
let mut sections = Vec::<Section>::default();
// Parse the section headers
io_file.seek(io::SeekFrom::Start(ehdr.e_shoff))?;
reader.seek(io::SeekFrom::Start(ehdr.e_shoff))?;
for _ in 0..ehdr.e_shnum {
let mut reader = Reader::new(io_file, ehdr.endianness);
let shdr = section::SectionHeader::parse(ehdr.class, &mut reader)?;
sections.push(
Section {

View File

@ -34,6 +34,10 @@ impl<'data, D: Read + Seek> Reader<'data, D> {
pub fn new(delegate: &'data mut D, endian: Endian) -> Reader<'data, D> {
Reader{delegate: delegate, endian: endian}
}
pub fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64, std::io::Error> {
self.delegate.seek(pos)
}
}
impl<'data, D: Read + Seek> ReadExt for Reader<'data, D> {