1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#[macro_export]
macro_rules! make_func {
($addy:expr, $res:ty, $($arg:ty),*) => {
std::mem::transmute::<*const usize, fn($($arg,)*) -> $res>($addy as *const usize)
}
}
pub enum Endian { Big, Little }
pub struct Memory {
end: Endian
}
impl Memory {
pub fn new(endian: Endian) -> Memory {
Memory {end: endian}
}
pub fn read_bytes(&self, address: usize, length: usize) -> Vec<u8> {
let mut result = Vec::<u8>::new();
let mut i;
match self.end {
Endian::Big => {
i = 0;
while i <= length {
unsafe { result.push(*((address + i) as *const u8)) };
i += 1;
}
},
Endian::Little => {
i = 1;
while i <= length {
unsafe { result.push(*((address + length - i) as *const u8)) };
i += 1;
}
}
}
result
}
pub fn write_bytes(&self, address: usize, bytes: &[u8]) {
let length = bytes.len();
let mut i;
match self.end {
Endian::Big => {
i = 1;
for byte in bytes.into_iter() {
println!("GenAddr: {:x}", unsafe { *((address + i) as *mut u8) });
println!("GenByte: {:x}", *byte);
unsafe { *((address + i) as *mut u8) = *byte };
i += 1;
}
},
Endian::Little => {
i = 1;
for byte in bytes.into_iter() {
unsafe { *((address + length - i) as *mut u8) = *byte };
i += 1;
}
}
}
}
pub fn search(&self, pattern: &[u8], from: usize, to: usize, wildcard: u8) -> usize {
if from >= to {
panic!("the from address is higher than the to address");
}
let length = pattern.len() - 1;
for position in from..to {
let bytes = self.read_bytes(position, length);
let mut p = 0;
for byte in &bytes {
if pattern[p] == wildcard {
p += 1;
continue
}
if byte != &pattern[p] {
break
} else if p == length {
return position - 1
}
p += 1;
}
}
0
}
}