goblin/pe/
data_directories.rs

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
use crate::error;
use scroll::{
    ctx::{self},
    Pread, Pwrite, SizeWith,
};

#[repr(C)]
#[derive(Debug, PartialEq, Copy, Clone, Default, Pread, Pwrite, SizeWith)]
pub struct DataDirectory {
    pub virtual_address: u32,
    pub size: u32,
}

pub const SIZEOF_DATA_DIRECTORY: usize = 8;
const NUM_DATA_DIRECTORIES: usize = 16;

impl DataDirectory {
    pub fn parse(bytes: &[u8], offset: &mut usize) -> error::Result<Self> {
        Ok(bytes.gread_with(offset, scroll::LE)?)
    }
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum DataDirectoryType {
    ExportTable,
    ImportTable,
    ResourceTable,
    ExceptionTable,
    CertificateTable,
    BaseRelocationTable,
    DebugTable,
    Architecture,
    GlobalPtr,
    TlsTable,
    LoadConfigTable,
    BoundImportTable,
    ImportAddressTable,
    DelayImportDescriptor,
    ClrRuntimeHeader,
}

impl TryFrom<usize> for DataDirectoryType {
    type Error = error::Error;
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        Ok(match value {
            0 => Self::ExportTable,
            1 => Self::ImportTable,
            2 => Self::ResourceTable,
            3 => Self::ExceptionTable,
            4 => Self::CertificateTable,
            5 => Self::BaseRelocationTable,
            6 => Self::DebugTable,
            7 => Self::Architecture,
            8 => Self::GlobalPtr,
            9 => Self::TlsTable,
            10 => Self::LoadConfigTable,
            11 => Self::BoundImportTable,
            12 => Self::ImportAddressTable,
            13 => Self::DelayImportDescriptor,
            14 => Self::ClrRuntimeHeader,
            _ => {
                return Err(error::Error::Malformed(
                    "Wrong data directory index number".into(),
                ))
            }
        })
    }
}

#[derive(Debug, PartialEq, Copy, Clone, Default)]
pub struct DataDirectories {
    pub data_directories: [Option<(usize, DataDirectory)>; NUM_DATA_DIRECTORIES],
}

impl ctx::TryIntoCtx<scroll::Endian> for DataDirectories {
    type Error = error::Error;

    fn try_into_ctx(self, bytes: &mut [u8], ctx: scroll::Endian) -> Result<usize, Self::Error> {
        let offset = &mut 0;
        for opt_dd in self.data_directories {
            if let Some((dd_offset, dd)) = opt_dd {
                bytes.pwrite_with(dd, dd_offset, ctx)?;
                *offset += dd_offset;
            } else {
                bytes.gwrite(&[0; SIZEOF_DATA_DIRECTORY][..], offset)?;
            }
        }
        Ok(NUM_DATA_DIRECTORIES * SIZEOF_DATA_DIRECTORY)
    }
}

macro_rules! build_dd_getter {
    ($dd_name:tt, $index:tt) => {
        pub fn $dd_name(&self) -> Option<&DataDirectory> {
            let idx = $index;
            self.data_directories[idx].as_ref().map(|(_, dd)| dd)
        }
    };
}

impl DataDirectories {
    pub fn parse(bytes: &[u8], count: usize, offset: &mut usize) -> error::Result<Self> {
        let mut data_directories = [None; NUM_DATA_DIRECTORIES];
        if count > NUM_DATA_DIRECTORIES {
            return Err(error::Error::Malformed(format!(
                "data directory count ({}) is greater than maximum number of data directories ({})",
                count, NUM_DATA_DIRECTORIES
            )));
        }
        for dir in data_directories.iter_mut().take(count) {
            let dd = DataDirectory::parse(bytes, offset)?;
            let dd = if dd.virtual_address == 0 && dd.size == 0 {
                None
            } else {
                Some((*offset, dd))
            };
            *dir = dd;
        }
        Ok(DataDirectories { data_directories })
    }

    build_dd_getter!(get_export_table, 0);
    build_dd_getter!(get_import_table, 1);
    build_dd_getter!(get_resource_table, 2);
    build_dd_getter!(get_exception_table, 3);
    build_dd_getter!(get_certificate_table, 4);
    build_dd_getter!(get_base_relocation_table, 5);
    build_dd_getter!(get_debug_table, 6);
    build_dd_getter!(get_architecture, 7);
    build_dd_getter!(get_global_ptr, 8);
    build_dd_getter!(get_tls_table, 9);
    build_dd_getter!(get_load_config_table, 10);
    build_dd_getter!(get_bound_import_table, 11);
    build_dd_getter!(get_import_address_table, 12);
    build_dd_getter!(get_delay_import_descriptor, 13);
    build_dd_getter!(get_clr_runtime_header, 14);

    pub fn dirs(&self) -> impl Iterator<Item = (DataDirectoryType, DataDirectory)> {
        self.data_directories
            .into_iter()
            .enumerate()
            // (Index, Option<DD>) -> Option<(Index, DD)> -> (DDT, DD)
            .filter_map(|(i, o)|
                // We should not have invalid indexes.
                // Indeed: `data_directories: &[_; N]` where N is the number
                // of data directories.
                // The `TryFrom` trait for integers to DataDirectoryType
                // takes into account the N possible data directories.
                // Therefore, the unwrap can never fail as long as Rust guarantees
                // on types are honored.
                o.map(|(_, v)| (i.try_into().unwrap(), v)))
    }
}