forked from ZeroIntensity/pointers.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_pointer.py
More file actions
253 lines (198 loc) · 7.23 KB
/
Copy pathc_pointer.py
File metadata and controls
253 lines (198 loc) · 7.23 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from .pointer import Pointer, dereference_address
import ctypes
from typing import (
Optional,
Any,
Dict,
Type,
TypeVar,
Generic,
TYPE_CHECKING,
Union,
Tuple,
)
if TYPE_CHECKING:
from .struct import Struct
from .exceptions import InvalidSizeError
T = TypeVar("T")
A = TypeVar("A", bound="Struct")
__all__ = (
"TypedCPointer",
"StructPointer",
"VoidPointer",
"cast",
"to_c_ptr",
"attempt_decode",
"to_struct_ptr",
)
def attempt_decode(data: bytes) -> Union[str, bytes]:
"""Attempt to decode a string of bytes."""
try:
return data.decode()
except UnicodeDecodeError:
return data
class StructPointer(Pointer[A]):
"""Class representing a pointer to a struct."""
def __init__(self, address: int, data_type: Type[A]):
super().__init__(address, data_type, True)
self.__ref = dereference_address(
address
) # this needs to be here to stop garbage collection
def __getattr__(self, name: str):
if name == "__ref":
raise Exception
return super().__getattribute__(name)
@property
def _as_parameter_(self):
return self._address
class _BaseCPointer(Pointer[Any]):
def __init__(self, address: int, size: int):
super().__init__(address, int, False)
self._size = size
@property
def size(self):
"""Size of the pointer."""
return self._size
def _make_stream_and_ptr(
self,
data: "_BaseCPointer",
) -> Tuple[ctypes.pointer, bytes]:
bytes_a = (ctypes.c_ubyte * data.size).from_address(data.address)
return self.make_ct_pointer(), bytes(bytes_a)
def move(self, data: Pointer[Any]) -> None:
"""Move data to the allocated memory."""
if not isinstance(data, _BaseCPointer):
raise ValueError(
f'"{type(data).__name__}" object is not a valid C pointer',
)
ptr, byte_stream = self._make_stream_and_ptr(data)
try:
ptr.contents[:] = byte_stream
except ValueError as e:
raise InvalidSizeError(
f"object is of size {len(byte_stream)}, while object size is {len(ptr.contents)}" # noqa
) from e
def make_ct_pointer(self):
return ctypes.cast(
self.address,
ctypes.POINTER(ctypes.c_char * self.size),
)
@classmethod
def map_type(cls, data: Any) -> "ctypes._CData":
"""Map the specified data to a C type."""
typ = cls.get_mapped(type(data))
return typ(data)
@staticmethod
def get_mapped(typ: Any):
"""Get the C mapped value of the given type."""
types: Dict[type, Type["ctypes._CData"]] = {
bytes: ctypes.c_char_p,
str: ctypes.c_wchar_p,
int: ctypes.c_int,
float: ctypes.c_float,
bool: ctypes.c_bool,
}
res = types.get(typ)
if not res:
raise ValueError(f'"{typ.__name__}" is not mappable to a c type')
return res
@classmethod
def is_mappable(cls, typ: Any) -> bool:
"""Whether the specified type is mappable to C."""
try:
cls.get_mapped(typ)
return True
except ValueError:
return False
@staticmethod
def get_py(data: Type["ctypes._CData"]) -> Type:
"""Map the specified C type to a Python type."""
types: Dict[Type["ctypes._CData"], type] = {
ctypes.c_bool: bool,
ctypes.c_char: bytes,
ctypes.c_wchar: str,
ctypes.c_ubyte: int,
ctypes.c_short: int,
ctypes.c_int: int,
ctypes.c_uint: int,
ctypes.c_long: int,
ctypes.c_ulong: int,
ctypes.c_longlong: int,
ctypes.c_ulonglong: int,
ctypes.c_size_t: int,
ctypes.c_ssize_t: int,
ctypes.c_float: float,
ctypes.c_double: float,
ctypes.c_longdouble: float,
ctypes.c_char_p: bytes,
ctypes.c_wchar_p: str,
ctypes.c_void_p: int,
}
return types[data]
@classmethod
def make_py(cls, data: "ctypes._CData"):
"""Convert the target C value to a Python object."""
typ = cls.get_py(type(data))
res = typ(data)
if typ is bytes:
res = attempt_decode(res)
return res
def __lshift__(self, data: Any):
"""Move data from another pointer to this pointer.""" # noqa
self.move(data if isinstance(data, _BaseCPointer) else to_c_ptr(data))
return self
class VoidPointer(_BaseCPointer):
"""Class representing a void pointer to a C object."""
@property
def _as_parameter_(self) -> int:
return self.address
def dereference(self) -> Optional[int]:
"""Dereference the pointer."""
deref = ctypes.c_void_p.from_address(self.address)
return deref.value
def __repr__(self) -> str:
return f"<void pointer to {hex(self.address)}>" # noqa
def __rich__(self):
return (
f"<[green]void[/green] pointer to [cyan]{hex(self.address)}[/cyan]>" # noqa
)
class TypedCPointer(_BaseCPointer, Generic[T]):
"""Class representing a pointer with a known type."""
def __init__(self, address: int, data_type: Type[T], size: int):
super().__init__(address, size)
self._type = data_type
@property
def _as_parameter_(self):
ctype = self.get_mapped(self.type)
deref = ctype.from_address(self.address)
return ctypes.pointer(deref)
def dereference(self) -> Optional[T]:
"""Dereference the pointer."""
ctype = self.get_mapped(self.type)
deref = ctype.from_address(self.address)
return deref.value # type: ignore
def move(self, data: Pointer) -> None:
"""Move data to the target C object."""
if data.type is not self.type:
raise ValueError("pointer must be the same type")
super().move(data)
def __repr__(self) -> str:
return f"<typed c pointer to {hex(self.address)}>" # noqa
def __rich__(self):
return f"<[green]typed c[/green] pointer to [cyan]{hex(self.address)}[/cyan]>" # noqa
def cast(ptr: VoidPointer, data_type: Type[T]) -> TypedCPointer[T]:
"""Cast a void pointer to a typed pointer."""
return TypedCPointer(ptr.address, data_type, ptr.size)
def to_c_ptr(data: T) -> TypedCPointer[T]:
"""Convert a python type to a pointer to a C type."""
ct = TypedCPointer.map_type(
data if not isinstance(data, str) else data.encode(),
)
address = ctypes.addressof(ct)
ptr = ctypes.cast(address, ctypes.c_void_p)
ptr2 = ctypes.pointer(TypedCPointer.map_type(data))
ctypes.memmove(ptr, ptr2, ctypes.sizeof(ptr2))
return TypedCPointer(address, type(data), ctypes.sizeof(ct))
def to_struct_ptr(struct: A) -> StructPointer[A]:
"""Convert a struct to a pointer."""
return StructPointer(id(struct), type(struct))