memflow/mem/memory_view/
arch_overlay.rs

1//! Overlays a custom architecture on the memory view
2
3use super::*;
4use crate::architecture::{ArchitectureObj, Endianess};
5use crate::error::*;
6
7/// Allows to overwrite the architecture of the memory view.
8///
9/// Is useful when a 32 bit process runs in a 64 bit architecture, and a 64-bit Pointer is wanted
10/// to be read with `read_ptr`.
11#[repr(C)]
12#[derive(Clone, Copy, Debug)]
13pub struct ArchOverlayView<T> {
14    mem: T,
15    arch_bits: u8,
16    little_endian: bool,
17}
18
19impl<T: MemoryView> ArchOverlayView<T> {
20    pub fn new_parts(mem: T, arch_bits: u8, little_endian: bool) -> Self {
21        Self {
22            mem,
23            arch_bits,
24            little_endian,
25        }
26    }
27
28    pub fn new(mem: T, arch: ArchitectureObj) -> Self {
29        Self::new_parts(
30            mem,
31            arch.bits(),
32            arch.endianess() == Endianess::LittleEndian,
33        )
34    }
35}
36
37impl<T: MemoryView> MemoryView for ArchOverlayView<T> {
38    fn read_raw_iter(&mut self, data: ReadRawMemOps) -> Result<()> {
39        self.mem.read_raw_iter(data)
40    }
41
42    fn write_raw_iter(&mut self, data: WriteRawMemOps) -> Result<()> {
43        self.mem.write_raw_iter(data)
44    }
45
46    fn metadata(&self) -> MemoryViewMetadata {
47        MemoryViewMetadata {
48            little_endian: self.little_endian,
49            arch_bits: self.arch_bits,
50            ..self.mem.metadata()
51        }
52    }
53}