diff --git a/src/lib.rs b/src/lib.rs index 01fee44..4e3bdd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ +#![doc = include_str!("../README.md")] + mod error; -mod process; +pub mod process; pub use error::Error; -pub use process::{Identifier, Module, Process}; diff --git a/src/process/mod.rs b/src/process/mod.rs index 5149f82..dfdc1aa 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -10,5 +10,5 @@ mod snapshot; pub use module::Module; pub use process::{Identifier, Process}; -pub type ProcessSnapshot = snapshot::Process; -pub type ModuleSnapshot = snapshot::Module; +type ProcessSnapshot = snapshot::Process; +type ModuleSnapshot = snapshot::Module; diff --git a/src/process/module.rs b/src/process/module.rs index bcf764d..4ad09a2 100644 --- a/src/process/module.rs +++ b/src/process/module.rs @@ -1,4 +1,4 @@ -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Module { pub process_id: u32, pub name: String, diff --git a/src/process/process.rs b/src/process/process.rs index 586681c..5d2419e 100644 --- a/src/process/process.rs +++ b/src/process/process.rs @@ -2,6 +2,24 @@ use super::{handle::Handle, memory, module::Module, ModuleSnapshot, ProcessSnaps use derive_more::derive::Display; +#[derive(Display)] +pub enum Identifier { + Pid(u32), + Name(String), +} + +impl From for Identifier { + fn from(value: u32) -> Self { + Self::Pid(value) + } +} + +impl From<&str> for Identifier { + fn from(value: &str) -> Self { + Self::Name(value.to_string()) + } +} + #[derive(Debug, Default, Clone)] pub struct Process { pub name: String, @@ -13,18 +31,13 @@ pub struct Process { unsafe impl Send for Process {} unsafe impl Sync for Process {} -#[derive(Display)] -pub enum Identifier { - Id(u32), - Name(String), -} - impl Process { - pub fn from(identifier: &Identifier) -> Result { + pub fn from>(identifier: T) -> Result { + let identifier = identifier.into(); let Some(snapshot) = (match identifier { - Identifier::Id(pid) => ProcessSnapshot::get_processes()? + Identifier::Pid(pid) => ProcessSnapshot::get_processes()? .into_iter() - .find(|snapshot| snapshot.id == *pid), + .find(|snapshot| snapshot.id == pid), Identifier::Name(ref name) => ProcessSnapshot::get_processes()? .into_iter() .find(|snapshot| snapshot.name == *name), @@ -43,14 +56,6 @@ impl Process { Ok(process) } - pub fn with_pid(pid: u32) -> Result { - Self::from(&Identifier::Id(pid)) - } - - pub fn with_name(name: &str) -> Result { - Self::from(&Identifier::Name(name.to_string())) - } - pub fn module(&self, name: &str) -> Result { let Some(snapshot) = ModuleSnapshot::get_modules(self.id)? .into_iter() @@ -70,24 +75,7 @@ impl Process { Ok(module) } - pub fn read_mem(&self, address: usize) -> Result { - let mut value = Default::default(); - memory::read(&self.handle, address, &mut value)?; - Ok(value) - } - - pub fn read_mem_from_ptr_chain(&self, chain: &[usize]) -> Result { - let mut chain = chain.to_vec(); - let mut address = chain.remove(0); - while chain.len() > 1 { - address += chain.remove(0); - address = self.read_mem(address)?; - } - let value = self.read_mem(address + chain.remove(0))?; - Ok(value) - } - - pub fn addr_from_ptr_chain(&self, chain: &[usize]) -> Result { + pub fn resolve_pointer_chain(&self, chain: &[usize]) -> Result { let mut chain = chain.to_vec(); let mut address = chain.remove(0); while chain.len() > 1 { @@ -97,6 +85,12 @@ impl Process { Ok(address + chain.remove(0)) } + pub fn read_mem(&self, address: usize) -> Result { + let mut value = Default::default(); + memory::read(&self.handle, address, &mut value)?; + Ok(value) + } + pub fn write_mem(&self, address: usize, mut value: T) -> Result<(), crate::Error> { memory::write(&self.handle, address, &mut value)?; Ok(())