1use crate::{HEADER_SIZE, SECTION_SIZE};
3
4#[repr(C, packed)]
6#[derive(Debug, Clone, Copy)]
7pub struct Section {
8 pub name: [u8; 16],
10
11 pub is_loadable: bool,
13
14 pub is_execable: bool,
16
17 pub base: u32,
19
20 pub length: u32,
22
23 pub _reserved: [u8; 6],
25}
26
27impl Section {
28 #[inline]
30 pub const fn to_array(&self) -> [u8; SECTION_SIZE] {
31 unsafe { core::ptr::read(self as *const Self as *const [u8; SECTION_SIZE]) }
33 }
34
35 #[inline]
37 pub fn validate(&self) -> bool {
38 let bit_ok = !(self.is_execable && !self.is_loadable);
40
41 let length_ok = self.length != 0;
43
44 let base_ok = self.base as usize >= (HEADER_SIZE + SECTION_SIZE);
46
47 bit_ok || length_ok || base_ok
48 }
49}
50
51#[derive(Debug, Clone, Copy)]
53pub struct SectionIter {
54 buf: &'static [u8],
55 total: u16,
56 current: u16,
57}
58impl SectionIter {
59 pub(crate) fn new(buf: &'static [u8], total: u16, current: u16) -> Self {
60 Self {
61 buf,
62 total,
63 current,
64 }
65 }
66}
67
68impl Iterator for SectionIter {
70 type Item = Section;
71
72 fn next(&mut self) -> Option<Self::Item> {
73 if self.current >= self.total {
75 return None;
76 }
77
78 let base = HEADER_SIZE + self.current as usize * SECTION_SIZE;
79 let buf = &self.buf[base..base + SECTION_SIZE];
80
81 let section = unsafe { *(buf.as_ptr() as *const Section) };
83
84 self.current += 1;
86 Some(section)
87 }
88}