Skip to main content

plthook/
types.rs

1//! Platform-agnostic value types shared by the format parsers and the patching
2//! engine. Keeping `Symbol`/`ImportKind` here (rather than in the Windows-only
3//! [`crate::slot`] module) lets the pure parser in [`crate::pe`] — and the ELF /
4//! platform code.
5
6/// An imported symbol, identified either by name or by ordinal.
7///
8/// Function names are matched **exactly** (case-sensitive) — correct for 64-bit
9/// providing library is matched separately and case-insensitively.
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub enum Symbol {
12    /// Import by name (e.g. `CreateFileW`).
13    Name(String),
14    /// Import by ordinal (e.g. `OLEAUT32.dll` `#8`).
15    Ordinal(u16),
16}
17
18impl Symbol {
19    /// Borrow a name symbol from a string slice.
20    #[must_use]
21    pub fn name(name: &str) -> Self {
22        Self::Name(name.to_owned())
23    }
24
25    /// Whether two symbols denote the same import (name equality or ordinal
26    /// equality). A name never matches an ordinal.
27    #[must_use]
28    pub fn matches(&self, other: &Self) -> bool {
29        match (self, other) {
30            (Self::Name(a), Self::Name(b)) => a == b,
31            (Self::Ordinal(a), Self::Ordinal(b)) => a == b,
32            _ => false,
33        }
34    }
35
36    /// Human-readable form for diagnostics (`CreateFileW`, `#8`).
37    #[must_use]
38    pub fn describe(&self) -> String {
39        match self {
40            Self::Name(name) => name.clone(),
41            Self::Ordinal(ordinal) => format!("#{ordinal}"),
42        }
43    }
44}
45
46impl core::fmt::Display for Symbol {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        match self {
49            Self::Name(name) => f.write_str(name),
50            Self::Ordinal(ordinal) => write!(f, "#{ordinal}"),
51        }
52    }
53}
54
55/// How an import reaches its slot: a normal load-time binding or a delay-load
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum ImportKind {
58    /// `IMAGE_DIRECTORY_ENTRY_IMPORT`.
59    Standard,
60    /// `IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT`.
61    DelayLoad,
62}