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
101
102
103
104
105
106
107
108
109
110
111
112
extern crate byteorder;
use self::byteorder::{ByteOrder, BigEndian, LittleEndian};
use errors::*;
pub type Addr = usize;
pub type Byte = u8;
pub trait MemoryBlock {
fn get_size(&self) -> Addr;
fn set(&mut self, Addr, Byte) -> Result<(), Error>;
fn get(&self, Addr) -> Result<Byte, Error>;
fn delete(&mut self, from: Addr, to: Addr) -> Result<(), Error> {
if from == to {
self.set(from, 0).chain_err(|| format!("failure to delete {:#X}", from))?;
}
for i in from..to {
self.set(i, 0).chain_err(|| format!("failure to delete byte {} in {:#X}-{:#X}", i-from, from, to))?;
}
self.set(to, 0).chain_err(|| format!("failure to delete {:#X}", to))
}
fn flush(&mut self) -> Result<(), Error> {
Ok(())
}
}
pub trait MemoryBlock32be: MemoryBlock {
fn set32be(&mut self, addr: Addr, data: u32) -> Result<(), Error> {
let mut bytes = [0; 4];
BigEndian::write_u32(&mut bytes, data);
for i in 0..4 {
self.set(addr + i, bytes[i])?;
}
Ok(())
}
fn get32be(&self, addr: Addr) -> Result<u32, Error> {
let mut bytes = [0; 4];
for i in 0..4 {
bytes[i] = self.get(addr + i)?;
}
Ok(BigEndian::read_u32(&bytes))
}
}
pub trait MemoryBlock32le: MemoryBlock {
fn set32le(&mut self, addr: Addr, data: u32) -> Result<(), Error> {
let mut bytes = [0; 4];
LittleEndian::write_u32(&mut bytes, data);
for i in 0..4 {
self.set(addr + i, bytes[i])?;
}
Ok(())
}
fn get32le(&self, addr: Addr) -> Result<u32, Error> {
let mut bytes = [0; 4];
for i in 0..4 {
bytes[i] = self.get(addr + i)?;
}
Ok(LittleEndian::read_u32(&bytes))
}
}
pub trait MemoryCreator<T> {
fn new(size: Addr) -> T;
}
pub trait MemoryMiddlewareCreator<M> {
fn new(Box<M>) -> Self;
}