process_image/
access.rs

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use core::ops::Deref;
use core::ops::DerefMut;

/// Mutable accessor for a single bit.
///
/// This type dereferences to an `&mut bool` which can be used to write the value of a single bit
/// in the process image.
#[derive(Debug)]
pub struct BitMut<'a> {
    buf: &'a mut u8,
    index: u8,
    value: bool,
}

impl<'a> BitMut<'a> {
    #[inline(always)]
    pub fn new(buf: &'a mut u8, index: u8) -> Self {
        let value = *buf & (1 << index) != 0;
        Self { buf, index, value }
    }
}

impl Deref for BitMut<'_> {
    type Target = bool;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl DerefMut for BitMut<'_> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl Drop for BitMut<'_> {
    #[inline(always)]
    fn drop(&mut self) {
        *self.buf &= !(1 << self.index);
        *self.buf |= u8::from(self.value) << self.index;
    }
}

/// Mutable accessor for a word.
///
/// This type dereferences to an `&mut u16` which can be used to write the value of a word in the
/// process image.
#[derive(Debug)]
pub struct WordMut<'a> {
    buf: &'a mut [u8; 2],
    value: u16,
}

impl<'a> WordMut<'a> {
    #[inline(always)]
    pub fn new(buf: &'a mut [u8; 2]) -> Self {
        let value = u16::from_be_bytes(*buf);
        Self { buf, value }
    }
}

impl Deref for WordMut<'_> {
    type Target = u16;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl DerefMut for WordMut<'_> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl Drop for WordMut<'_> {
    #[inline(always)]
    fn drop(&mut self) {
        *self.buf = self.value.to_be_bytes();
    }
}

/// Mutable accessor for a double word.
///
/// This type dereferences to an `&mut u32` which can be used to write the value of a double word
/// in the process image.
#[derive(Debug)]
pub struct DWordMut<'a> {
    buf: &'a mut [u8; 4],
    value: u32,
}

impl<'a> DWordMut<'a> {
    #[inline(always)]
    pub fn new(buf: &'a mut [u8; 4]) -> Self {
        let value = u32::from_be_bytes(*buf);
        Self { buf, value }
    }
}

impl Deref for DWordMut<'_> {
    type Target = u32;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl DerefMut for DWordMut<'_> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl Drop for DWordMut<'_> {
    #[inline(always)]
    fn drop(&mut self) {
        *self.buf = self.value.to_be_bytes();
    }
}

/// Mutable accessor for a long word.
///
/// This type dereferences to an `&mut u64` which can be used to write the value of a long word in
/// the process image.
#[derive(Debug)]
pub struct LWordMut<'a> {
    buf: &'a mut [u8; 8],
    value: u64,
}

impl<'a> LWordMut<'a> {
    #[inline(always)]
    pub fn new(buf: &'a mut [u8; 8]) -> Self {
        let value = u64::from_be_bytes(*buf);
        Self { buf, value }
    }
}

impl Deref for LWordMut<'_> {
    type Target = u64;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl DerefMut for LWordMut<'_> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl Drop for LWordMut<'_> {
    #[inline(always)]
    fn drop(&mut self) {
        *self.buf = self.value.to_be_bytes();
    }
}