#![no_std]
pub mod header;
pub mod sections;
use header::Header;
use sections::{SectionIter, Section};
pub const HEADER_SIZE: usize = core::mem::size_of::<Header>();
pub const SECTION_SIZE: usize = core::mem::size_of::<Section>();
#[derive(Debug, Clone, Copy)]
pub struct Parser {
buf: &'static [u8],
header: Header,
total_sections: u16,
}
impl Parser {
pub unsafe fn init(buf: &'static [u8]) -> Result<Self, Error> {
let header_raw = &buf[0..HEADER_SIZE]; let header = unsafe { *(header_raw.as_ptr() as *const Header) };
if !header.validate() {
return Err(Error::NotValidExecutable);
}
Ok(Self {
buf, header,
total_sections: header.sections,
})
}
#[inline]
pub fn header(&self) -> Header {
self.header
}
#[allow(private_interfaces)]
pub fn sections(&self) -> SectionIter {
SectionIter {
buf: self.buf,
total: self.total_sections,
current: 0,
}
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
NotValidExecutable,
}