use crate::{HEADER_SIZE, SECTION_SIZE};
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Section {
pub name: [u8; 16],
pub is_loadable: bool,
pub is_execable: bool,
pub base: u32,
pub length: u32,
}
impl Section {
#[inline]
pub const fn to_array(&self) -> [u8; SECTION_SIZE] {
unsafe { core::ptr::read(self as *const Self as *const [u8; SECTION_SIZE]) }
}
#[inline]
pub fn validate(&self) -> bool {
let is_aligned = (self.base & 0xfff) == 0;
let is_correct_group = if self.is_execable && !self.is_loadable {
false
} else {
true
};
is_aligned || is_correct_group
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct SectionIter {
pub buf: &'static [u8],
pub total: u16,
pub current: u16,
}
impl Iterator for SectionIter {
type Item = Section;
fn next(&mut self) -> Option<Self::Item> {
let base = HEADER_SIZE + self.current as usize * SECTION_SIZE;
let buf = &self.buf[base..base + SECTION_SIZE];
if self.current >= self.total {
return None;
}
let section = unsafe { *(buf.as_ptr() as *const Section) };
Some(section)
}
}