1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::{error::Error};

use crate::{KOFileReader, KOFileWriter};

pub struct HeaderTable {
    headers: Vec<SectionHeader>
}

impl HeaderTable {

    pub fn new(headers: Vec<SectionHeader>) -> HeaderTable {
        HeaderTable { headers }
    }

    pub fn get_headers(&self) -> &Vec<SectionHeader> {
        &self.headers
    }

    pub fn get_header(&self, index: usize) -> Result<&SectionHeader, Box<dyn Error>> {
        match self.headers.get(index) {
            Some(h) => Ok(h),
            None => Err(format!("Tried to index section {} in the section header, which does not exist", index).into()),
        }
    }

    pub fn add(&mut self, header: SectionHeader) {
        self.headers.push(header);
    }

    pub fn read(reader: &mut KOFileReader, number_sections: u16) -> Result<HeaderTable, Box<dyn Error>> {

        let mut headers: Vec<SectionHeader> = Vec::with_capacity(number_sections as usize);

        for _ in 0..number_sections {
            headers.push(SectionHeader::read(reader)?);
        }
        
        Ok(HeaderTable::new(headers))
    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        for header in self.headers.iter() {
            header.write(writer)?;
        }

        Ok(())
    }

    pub fn validate_conventions(&self) -> Result<(), Box<dyn Error>> {

        if self.headers.len() < 4 {
            return Err("At least the null section, symbol string table, symbol table, and data section are required. One or more are missing.".into());
        }

        if self.get_header(0)?.get_type() != SectionType::NULL {
            return Err("The first entry into the section header table should be the null section.".into());
        }

        if self.get_header(1)?.get_type() != SectionType::STRTAB
            || self.get_header(1)?.name() != ".symstrtab" {
            return Err("Expected the symbol string table at index 1".into());
        }

        if self.get_header(2)?.get_type() != SectionType::DATA
            || self.get_header(2)?.name() != ".data" {
            return Err("Expected the symbol data section at index 2".into());
        }

        if self.get_header(3)?.get_type() != SectionType::SYMTAB
            || self.get_header(3)?.name() != ".symtab" {
            return Err("Expected the symbol table at index 3".into());
        }

        Ok(())
    }

}

pub struct SectionHeader {
    section_type: SectionType,
    section_offset: u32,
    section_size: u32,
    section_name: String,
}

impl SectionHeader {

    pub fn new(section_type: SectionType, section_offset: u32, section_size: u32, section_name: &str) -> SectionHeader {
        SectionHeader {
            section_type,
            section_offset,
            section_size,
            section_name: section_name.to_owned(),
        }
    }

    pub fn read(reader: &mut KOFileReader) -> Result<SectionHeader, Box<dyn Error>> {

        let section_type = SectionType::from(reader.next()?)?;

        let section_offset = reader.read_uint32()?;

        let section_size = reader.read_uint32()?;

        let section_name = reader.read_string()?;

        Ok(SectionHeader::new(section_type, section_offset, section_size, &section_name))

    }

    pub fn write(&self, writer: &mut KOFileWriter) -> Result<(), Box<dyn Error>> {

        writer.write(self.section_type.to_byte())?;

        writer.write_uint32(self.section_offset)?;

        writer.write_uint32(self.section_size)?;

        writer.write_string(&self.section_name)?;

        Ok(())
    }

    pub fn get_type(&self) -> SectionType {
        self.section_type
    }

    pub fn offset(&self) -> u32 {
        self.section_offset
    }

    pub fn set_offset(&mut self, offset: u32) {
        self.section_offset = offset;
    }

    pub fn size(&self) -> u32 {
        9 + self.section_name.len() as u32 + 1
    }

    pub fn section_size(&self) -> u32 {
        self.section_size
    }

    pub fn name(&self) -> &String {
        &self.section_name
    }

}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SectionType {
    NULL,
    SYMTAB,
    STRTAB,
    REL,
    DATA,
    DEBUG
}

impl SectionType {

    pub fn to_byte(&self) -> u8 {
        match self {
            SectionType::NULL => 0,
            SectionType::SYMTAB => 1,
            SectionType::STRTAB => 2,
            SectionType::REL => 3,
            SectionType::DATA => 4,
            SectionType::DEBUG => 5,
        }
    }

    pub fn from(byte: u8) -> Result<SectionType, Box<dyn Error>>{
        match byte {
            0 => Ok(SectionType::NULL),
            1 => Ok(SectionType::SYMTAB),
            2 => Ok(SectionType::STRTAB),
            3 => Ok(SectionType::REL),
            4 => Ok(SectionType::DATA),
            5 => Ok(SectionType::DEBUG),
            b => Err(format!("Section type of {} is not a valid section type.", b).into())
        }
    }

}