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
//! Error types and utilities.

use std::error::Error as StdError;
use std::fmt;

/// The result of a detour operation.
pub type Result<T> = ::std::result::Result<T, Error>;

/// A representation of all possible errors.
#[derive(Debug)]
pub enum Error {
  /// The address for the target and detour are identical
  SameAddress,
  /// The address does not contain valid instructions.
  InvalidCode,
  /// The address has no available area for patching.
  NoPatchArea,
  /// The address is not executable memory.
  NotExecutable,
  /// The detour is not initialized.
  NotInitialized,
  /// The detour is already initialized.
  AlreadyInitialized,
  /// The system is out of executable memory.
  OutOfMemory,
  /// The address contains an instruction that prevents detouring.
  UnsupportedInstruction,
  /// A memory operation failed.
  RegionFailure(region::Error),
}

impl StdError for Error {
  fn source(&self) -> Option<&(dyn StdError + 'static)> {
    if let Error::RegionFailure(error) = self {
      Some(error)
    } else {
      None
    }
  }
}

impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Error::SameAddress => write!(f, "Target and detour address is the same"),
      Error::InvalidCode => write!(f, "Address contains invalid assembly"),
      Error::NoPatchArea => write!(f, "Cannot find an inline patch area"),
      Error::NotExecutable => write!(f, "Address is not executable"),
      Error::NotInitialized => write!(f, "Detour is not initialized"),
      Error::AlreadyInitialized => write!(f, "Detour is already initialized"),
      Error::OutOfMemory => write!(f, "Cannot allocate memory"),
      Error::UnsupportedInstruction => write!(f, "Address contains an unsupported instruction"),
      Error::RegionFailure(ref error) => write!(f, "{}", error),
    }
  }
}

impl From<region::Error> for Error {
  fn from(error: region::Error) -> Self {
    Error::RegionFailure(error)
  }
}