Struct memflow::mem::memory_view::cursor::MemoryCursor
source · pub struct MemoryCursor<T> { /* private fields */ }
Expand description
MemoryCursor implments a Cursor around the MemoryView
trait.
The cursor provides the Read
,
Write
and Seek
traits
for the underlying MemoryView
object.
Examples:
use std::io::{self, Read, Write, Seek};
use memflow::dummy::{DummyOs, DummyMemory};
use memflow::types::size;
use memflow::mem::{DirectTranslate, VirtualDma, MemoryCursor};
use memflow::architecture::x86::x64;
fn main() -> io::Result<()> {
// setup a pseudo virtual memory reader
let phys_mem = DummyMemory::new(size::mb(16));
let mut os = DummyOs::new(phys_mem);
let (dtb, virt_base) = os.alloc_dtb(size::mb(8), &[]);
let phys_mem = os.into_inner();
let translator = x64::new_translator(dtb);
let virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
// create the actual cursor and seek it to the dummy virt_base
let mut cursor = MemoryCursor::new(virt_mem);
cursor.seek(io::SeekFrom::Start(virt_base.to_umem() as u64))?;
// read up to 10 bytes
let mut buffer = [0; 10];
cursor.read(&mut buffer)?;
// write the previously read 10 bytes again
cursor.seek(io::SeekFrom::Start(virt_base.to_umem() as u64))?;
cursor.write(&buffer)?;
Ok(())
}
Implementations§
source§impl<T: MemoryView> MemoryCursor<T>
impl<T: MemoryView> MemoryCursor<T>
sourcepub fn new(mem: T) -> Self
pub fn new(mem: T) -> Self
Creates a new MemoryCursor by wrapping around a MemoryView
object.
Cursor initial position is 0
.
Examples:
Borrowing a MemoryView
object:
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cursor = MemoryCursor::new(virt_mem);
Taking (temporary) ownership of a MemoryView
object:
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cursor = MemoryCursor::new(virt_mem);
sourcepub fn at(mem: T, address: Address) -> Self
pub fn at(mem: T, address: Address) -> Self
Creates a new MemoryCursor by wrapping around a MemoryView
object
at the desired starting position.
Cursor initial position is * address
.
Examples:
Borrowing a MemoryView
object:
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let mut virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cursor = MemoryCursor::at(virt_mem, 0x1000.into());
Taking (temporary) ownership of a MemoryView
object:
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let virt_mem = VirtualDma::new(phys_mem, x64::ARCH, translator);
let mut cursor = MemoryCursor::at(virt_mem, 0x1000.into());
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this cursor, returning the underlying MemoryView
object.
Examples
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let mut cursor = MemoryCursor::new(VirtualDma::new(phys_mem, x64::ARCH, translator));
let phys_mem = cursor.into_inner();
sourcepub fn get_ref(&self) -> &T
pub fn get_ref(&self) -> &T
Gets a reference to the underlying MemoryView
object in this cursor.
Examples
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let mut cursor = MemoryCursor::new(VirtualDma::new(phys_mem, x64::ARCH, translator));
let reference = cursor.get_ref();
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Gets a mutable reference to the underlying MemoryView
object in this cursor.
Examples
use memflow::dummy::DummyMemory;
use memflow::types::size;
use memflow::mem::MemoryCursor;
let mut cursor = MemoryCursor::new(VirtualDma::new(phys_mem, x64::ARCH, translator));
let reference = cursor.get_mut();
sourcepub fn address(&self) -> Address
pub fn address(&self) -> Address
Returns the current address of this cursor.
Examples
use std::io::{Seek, SeekFrom};
use memflow::dummy::DummyMemory;
use memflow::types::{Address, size};
use memflow::mem::MemoryCursor;
let mut cursor = MemoryCursor::new(VirtualDma::new(phys_mem, x64::ARCH, translator));
assert_eq!(cursor.address(), Address::NULL);
cursor.seek(SeekFrom::Current(2)).unwrap();
assert_eq!(cursor.address(), Address::from(2));
cursor.seek(SeekFrom::Current(-1)).unwrap();
assert_eq!(cursor.address(), Address::from(1));
sourcepub fn set_address(&mut self, address: Address)
pub fn set_address(&mut self, address: Address)
Sets the address of this cursor.
Examples
use memflow::dummy::DummyMemory;
use memflow::types::{Address, size};
use memflow::mem::MemoryCursor;
let mut cursor = MemoryCursor::new(VirtualDma::new(phys_mem, x64::ARCH, translator));
assert_eq!(cursor.address(), Address::NULL);
cursor.set_address(Address::from(2));
assert_eq!(cursor.address(), Address::from(2));
cursor.set_address(Address::from(4));
assert_eq!(cursor.address(), Address::from(4));
Trait Implementations§
source§impl<T: MemoryView> Read for MemoryCursor<T>
impl<T: MemoryView> Read for MemoryCursor<T>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moresource§impl<T: MemoryView> Seek for MemoryCursor<T>
impl<T: MemoryView> Seek for MemoryCursor<T>
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)source§impl<T: MemoryView> Write for MemoryCursor<T>
impl<T: MemoryView> Write for MemoryCursor<T>
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Auto Trait Implementations§
impl<T> RefUnwindSafe for MemoryCursor<T>where
T: RefUnwindSafe,
impl<T> Send for MemoryCursor<T>where
T: Send,
impl<T> Sync for MemoryCursor<T>where
T: Sync,
impl<T> Unpin for MemoryCursor<T>where
T: Unpin,
impl<T> UnwindSafe for MemoryCursor<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> GetWithMetadata for T
impl<T> GetWithMetadata for T
§type ForSelf = WithMetadata_<T, T>
type ForSelf = WithMetadata_<T, T>
WithMetadata_<Self, Self>
§impl<Ctx, R> IOread<Ctx> for R
impl<Ctx, R> IOread<Ctx> for R
§fn ioread<N>(&mut self) -> Result<N, Error>where
N: FromCtx<Ctx> + SizeWith<Ctx>,
Ctx: Default,
fn ioread<N>(&mut self) -> Result<N, Error>where
N: FromCtx<Ctx> + SizeWith<Ctx>,
Ctx: Default,
N
from Self
, with a default parsing context.
For the primitive numeric types, this will be at the host machine’s endianness. Read more§fn ioread_with<N>(&mut self, ctx: Ctx) -> Result<N, Error>where
N: FromCtx<Ctx> + SizeWith<Ctx>,
fn ioread_with<N>(&mut self, ctx: Ctx) -> Result<N, Error>where
N: FromCtx<Ctx> + SizeWith<Ctx>,
N
from Self
, with the parsing context ctx
.
NB: this will panic if the type you’re reading has a size greater than 256. Plans are to have this allocate in larger cases. Read more§impl<Ctx, W> IOwrite<Ctx> for W
impl<Ctx, W> IOwrite<Ctx> for W
§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset
. Read more§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset
. Read more§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset
. Read more§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset
. Read more§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read more§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read more§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
§fn piped_ref<'a, F, U>(&'a self, f: F) -> U
fn piped_ref<'a, F, U>(&'a self, f: F) -> U
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read more§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more