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
#![allow(dead_code)]
/// Error type for all mistakes made in faithe.
#[derive(Debug)]
pub enum FaitheError {
    #[cfg(not(feature = "no-std"))]
    /// Error code returned from `GetLastError()` WinAPI.
    ErrorCode(windows::Win32::Foundation::WIN32_ERROR),
    #[cfg(not(feature = "no-std"))]
    /// Error from `windows` crate
    WindowsError(windows::core::Error),
    /// No process with selected name were found.
    ProcessNotFound,
    /// No module with selected name were found.
    ModuleNotFound,
    /// Memory query failed
    QueryFailed,
    /// Failed to find selected pattern.
    PatternNotFound,
    /// Protection that cannot be represented with internal type.
    #[cfg(all(windows, not(feature = "no-std")))]
    UnknownProtection(u32),
    /// String is not a valid UTF-8/UTF-16 sequence.
    InvalidString,
    /// Pattern is not an ASCII sequence.
    NonAsciiPattern,
    /// Supplied string cannot be parsed as a pattern of given type.
    InvalidPattern,
    /// Length of mask is not equal to the length of the pattern.
    PatternMaskMismatch,
    /// Tried to resolve function pointer twice.
    AlreadyResolved,
}

pub(crate) type Result<T> = core::result::Result<T, FaitheError>;

cfg_if::cfg_if! {
    if #[cfg(not(feature = "no-std"))] {
        impl std::error::Error for FaitheError {}

        impl std::fmt::Display for FaitheError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{:?}", self)
            }
        }

        impl From<windows::core::Error> for FaitheError {
            fn from(e: windows::core::Error) -> Self {
                Self::WindowsError(e)
            }
        }

        impl FaitheError {
            pub(crate) fn last_error() -> Self {
                unsafe { Self::ErrorCode(windows::Win32::Foundation::GetLastError()) }
            }
        }
    }
}