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
//! Dispatch over the binary PE/COFF input kinds.
use super::header::{is_bigobj, is_import_object};
use super::import::ShortImport;
use super::object::CoffObject;
use super::pe::PeImage;
use super::source::Source;
use crate::error::Result;
/// Any binary PE/COFF input: an object, a short import object or an image.
#[derive(Clone, Copy, Debug)]
pub enum CoffFile<'a> {
/// A relocatable object (regular or `/bigobj`).
Object(CoffObject<'a>),
/// A short import object from an import library.
ShortImport(ShortImport<'a>),
/// A PE image (DLL or executable).
Image(PeImage<'a>),
}
impl<'a> CoffFile<'a> {
/// Parses `data` as whichever kind its leading bytes announce:
/// `ANON_OBJECT_HEADER_BIGOBJ` or a regular header for objects, an
/// `IMPORT_OBJECT_HEADER` for short imports, `MZ` for images.
///
/// # Errors
///
/// Returns `Error::Malformed` if the selected parser fails.
pub fn parse(data: &'a [u8], source: Source<'a>) -> Result<Self> {
if is_import_object(data) && !is_bigobj(data) {
return ShortImport::parse(data, source).map(Self::ShortImport);
}
if data.starts_with(b"MZ") {
return PeImage::parse(data, source).map(Self::Image);
}
CoffObject::parse(data, source).map(Self::Object)
}
/// `Machine` (`IMAGE_FILE_MACHINE_*`).
#[must_use]
pub fn machine(&self) -> u16 {
match self {
Self::Object(object) => object.machine(),
Self::ShortImport(import) => import.machine,
Self::Image(image) => image.machine(),
}
}
}