string_to_offset

Function string_to_offset 

Source
pub fn string_to_offset(s: &str) -> Result<usize>
Expand description

Parse a hexadecimal string to a numeric offset.

Accepts strings with or without “0x” prefix.

§Arguments

  • s - String containing hexadecimal number (e.g., “0x123456” or “123456”)

§Returns

Ok(usize) containing the parsed offset. Err(SubstrateError::ParseError) if parsing fails.

§Examples

use substrate::utils::string_to_offset;

let offset = string_to_offset("0x123456").expect("Parse failed");
assert_eq!(offset, 0x123456);
Examples found in repository?
examples/error_handling.rs (line 25)
4fn demonstrate_error_handling() {
5    println!("=== Error Handling Example ===\n");
6
7    unsafe {
8        println!("1. Testing null pointer hook:");
9        match hook_function(std::ptr::null_mut::<u8>(), std::ptr::null_mut()) {
10            Ok(_) => println!("  Unexpected success"),
11            Err(SubstrateError::NullPointer) => println!("  ✓ Correctly caught null pointer"),
12            Err(e) => println!("  Different error: {}", e),
13        }
14
15        println!("\n2. Testing library not found:");
16        match utils::find_library("nonexistent.so") {
17            Ok(_) => println!("  Unexpected success"),
18            Err(SubstrateError::LibraryNotFound(name)) => {
19                println!("  ✓ Correctly caught missing library: {}", name)
20            }
21            Err(e) => println!("  Different error: {}", e),
22        }
23
24        println!("\n3. Testing invalid offset string:");
25        match utils::string_to_offset("not_a_hex") {
26            Ok(_) => println!("  Unexpected success"),
27            Err(SubstrateError::ParseError(msg)) => {
28                println!("  ✓ Correctly caught parse error: {}", msg)
29            }
30            Err(e) => println!("  Different error: {}", e),
31        }
32
33        println!("\n4. Testing valid hex parsing:");
34        match utils::string_to_offset("0x123ABC") {
35            Ok(offset) => println!("  ✓ Successfully parsed: 0x{:X}", offset),
36            Err(e) => println!("  ✗ Unexpected error: {}", e),
37        }
38    }
39
40    println!("\n=== All Error Cases Handled ===");
41}
More examples
Hide additional examples
examples/android_game_hook.rs (line 59)
45fn main() {
46    println!("=== Android Game Hook Example (IL2CPP/Unity) ===\n");
47
48    let library = "libil2cpp.so";
49
50    let update_offset_str = "0x123456";
51    let fixed_update_offset_str = "0x789ABC";
52
53    println!("Waiting for {} to load...", library);
54
55    if wait_for_library(library, 30) {
56        println!("✓ {} loaded!", library);
57
58        unsafe {
59            match string_to_offset(update_offset_str) {
60                Ok(update_offset) => {
61                    println!("\nHooking Update() at offset: 0x{:X}", update_offset);
62
63                    match get_absolute_address(library, update_offset) {
64                        Ok(addr) => {
65                            println!("Absolute address: 0x{:x}", addr);
66
67                            MSHookFunction(
68                                addr as *mut c_void,
69                                hooked_update as *mut c_void,
70                                &mut OLD_UPDATE
71                            );
72
73                            if !OLD_UPDATE.is_null() {
74                                println!("✓ Update() hooked successfully!");
75                            }
76                        }
77                        Err(e) => eprintln!("✗ Failed to get address: {}", e),
78                    }
79                }
80                Err(e) => eprintln!("✗ Invalid offset: {}", e),
81            }
82
83            match string_to_offset(fixed_update_offset_str) {
84                Ok(fixed_offset) => {
85                    println!("\nHooking FixedUpdate() at offset: 0x{:X}", fixed_offset);
86
87                    match get_absolute_address(library, fixed_offset) {
88                        Ok(addr) => {
89                            println!("Absolute address: 0x{:x}", addr);
90
91                            MSHookFunction(
92                                addr as *mut c_void,
93                                hooked_fixed_update as *mut c_void,
94                                &mut OLD_FIXED_UPDATE
95                            );
96
97                            if !OLD_FIXED_UPDATE.is_null() {
98                                println!("✓ FixedUpdate() hooked successfully!");
99                            }
100                        }
101                        Err(e) => eprintln!("✗ Failed to get address: {}", e),
102                    }
103                }
104                Err(e) => eprintln!("✗ Invalid offset: {}", e),
105            }
106
107            println!("\n=== Hooks Installed ===");
108            println!("The game functions will now call your hooks.");
109            println!("\nNote: Replace the offset values with real ones from your game!");
110        }
111    } else {
112        eprintln!("✗ Timeout waiting for {} to load", library);
113        eprintln!("\nTo use this example:");
114        eprintln!("1. Find function offsets using IDA Pro, Ghidra, or similar");
115        eprintln!("2. Replace the offset strings with actual values");
116        eprintln!("3. Run as part of an injected library in the game process");
117    }
118}