-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalue.py
88 lines (73 loc) · 3.05 KB
/
value.py
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
# This file is part of ot-project.
#
# ot-project is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ot-project is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <https://www.gnu.org/licenses/>.
#
import os
import typing
import math
from core.binary_io import BinaryIO
from core.type import Type
class Value(BinaryIO):
"""
Represents a value in memory
TODO: somehow add functionality to update it from this class, i.e. add some `.read` function
problem for that though is that I've not figured out a good way to pass it down to each
of the addresses, maybe I'll just add another argument to the __init__ function which is the
file descriptor. Not sure problem with that implementation is that you can't really make Values
outside of Memory reasonably then.
We'll see though, I'll figure something out. For now it will simply be a way to conveniently
store the address, type and value.
Although since it will essentially read ~8 bytes max on average per say 100ms, it will be a
pretty insignificant slowdown to open/close the file on read to update the value, a good idea
would probably be to pass the pid of the process to this class each time.
"""
def __init__(self, pid: int, address: int, value: typing.Any, typeof: Type = Type.UINT32):
"""
:param pid: pid of the process the value belongs to
:param address: address of the value
:param value:
:param typeof:
"""
super().__init__(pid)
self.address = address
self.type = typeof
self.value = value
self.previous_value = value
def read(self):
"""
reads a memory address and updates the value
:return: value read
"""
if not os.path.isfile(f"/proc/{self.pid}/mem"):
return self.value
self.value = super()._read(self.address, self.type)
return self.value
def write(self, new_value):
self._write(self.address, self.type.get_format().pack(new_value))
def change_type(self, typeof: Type):
print("Swapping type")
self.type = typeof
def __eq__(self, other):
"""
compares two values
:param other:
:return: returns true if the type and address match
"""
if isinstance(other, Value):
return self.type == other.type and self.address == other.address
if isinstance(other, float):
return math.isclose(self.value, other, abs_tol=0.01)
return self.value == other
def __str__(self):
return str(self.value)