process_memory/
architecture.rs

1use std::convert::TryInto;
2
3/// Enum representing the architecture of a process
4#[derive(Clone, Debug, Copy)]
5#[repr(u8)]
6pub enum Architecture {
7    /// 8-bit architecture
8    #[cfg(any(
9        target_pointer_width = "8",
10        target_pointer_width = "16",
11        target_pointer_width = "32",
12        target_pointer_width = "64",
13        target_pointer_width = "128"
14    ))]
15    Arch8Bit = 1,
16    /// 16-bit architecture
17    #[cfg(any(
18        target_pointer_width = "16",
19        target_pointer_width = "32",
20        target_pointer_width = "64",
21        target_pointer_width = "128"
22    ))]
23    Arch16Bit = 2,
24    /// 32-bit architecture
25    #[cfg(any(
26        target_pointer_width = "32",
27        target_pointer_width = "64",
28        target_pointer_width = "128"
29    ))]
30    Arch32Bit = 4,
31    /// 64-bit architecture
32    #[cfg(any(target_pointer_width = "64", target_pointer_width = "128"))]
33    Arch64Bit = 8,
34    /// 128-bit architecture
35    #[cfg(target_pointer_width = "128")]
36    Arch128Bit = 16,
37}
38
39impl Architecture {
40    /// Create an Architecture matching that of the host process.
41    #[must_use]
42    pub fn from_native() -> Architecture {
43        #[cfg(target_pointer_width = "8")]
44        return Architecture::Arch8Bit;
45        #[cfg(target_pointer_width = "16")]
46        return Architecture::Arch16Bit;
47        #[cfg(target_pointer_width = "32")]
48        return Architecture::Arch32Bit;
49        #[cfg(target_pointer_width = "64")]
50        return Architecture::Arch64Bit;
51        #[cfg(target_pointer_width = "128")]
52        return Architecture::Arch128Bit;
53    }
54
55    /// Convert bytes read from memory into a pointer in the
56    /// current architecture.
57    ///
58    /// # Panics
59    /// If there are not enough bytes in the slice to make an integer of the sized indicated by
60    /// `self`.
61    #[must_use]
62    pub fn pointer_from_ne_bytes(self, bytes: &[u8]) -> usize {
63        match self {
64            #[allow(clippy::cast_possible_truncation)]
65            #[cfg(any(
66                target_pointer_width = "8",
67                target_pointer_width = "16",
68                target_pointer_width = "32",
69                target_pointer_width = "64",
70                target_pointer_width = "128"
71            ))]
72            Architecture::Arch8Bit => u8::from_ne_bytes(bytes.try_into().unwrap()) as usize,
73            #[allow(clippy::cast_possible_truncation)]
74            #[cfg(any(
75                target_pointer_width = "16",
76                target_pointer_width = "32",
77                target_pointer_width = "64",
78                target_pointer_width = "128"
79            ))]
80            Architecture::Arch16Bit => u16::from_ne_bytes(bytes.try_into().unwrap()) as usize,
81            #[allow(clippy::cast_possible_truncation)]
82            #[cfg(any(
83                target_pointer_width = "32",
84                target_pointer_width = "64",
85                target_pointer_width = "128"
86            ))]
87            Architecture::Arch32Bit => u32::from_ne_bytes(bytes.try_into().unwrap()) as usize,
88            #[allow(clippy::cast_possible_truncation)]
89            #[cfg(any(target_pointer_width = "64", target_pointer_width = "128"))]
90            Architecture::Arch64Bit => u64::from_ne_bytes(bytes.try_into().unwrap()) as usize,
91            #[allow(clippy::cast_possible_truncation)]
92            #[cfg(target_pointer_width = "128")]
93            Architecture::Arch128Bit => u128::from_ne_bytes(bytes.try_into().unwrap()) as usize,
94        }
95    }
96}