android_mem_kit/
memory.rs

1use std::ffi::{CString, c_void};
2
3unsafe extern "C" {
4    fn bridge_create_patch_hex(addr: usize, hex: *const i8) -> *mut c_void;
5    fn bridge_patch_apply(patch: *mut c_void) -> i32;
6    fn bridge_patch_restore(patch: *mut c_void) -> i32;
7    fn bridge_get_module_base(name: *const i8) -> usize;
8}
9
10pub struct MemoryPatch {
11    inner: *mut c_void, 
12}
13
14unsafe impl Send for MemoryPatch {}
15unsafe impl Sync for MemoryPatch {}
16
17impl MemoryPatch {
18    pub fn from_hex(address: usize, hex: &str) -> Result<Self, String> {
19        let c_hex = CString::new(hex).map_err(|_| "Invalid hex string")?;
20        
21        unsafe {
22            let ptr = bridge_create_patch_hex(address, c_hex.as_ptr() as *const i8);
23            if ptr.is_null() {
24                return Err("Failed to create patch (Invalid Hex or Address)".into());
25            }
26            Ok(Self { inner: ptr })
27        }
28    }
29
30    pub fn apply(&self) -> bool {
31        unsafe { bridge_patch_apply(self.inner) != 0 }
32    }
33
34    pub fn restore(&self) -> bool {
35        unsafe { bridge_patch_restore(self.inner) != 0 }
36    }
37}
38
39pub fn get_lib_base(lib_name: &str) -> usize {
40    let c_name = CString::new(lib_name).unwrap_or_default();
41    unsafe { bridge_get_module_base(c_name.as_ptr() as *const i8) }
42}