Skip to content

Commit bde6186

Browse files
authored
Merge pull request #77 from mrexodia/improved-types
Improve the P[T] to support indexed writes
2 parents 7bc153d + 55f7863 commit bde6186

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/dumpulator/dumpulator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,8 @@ def _arg_to_string(dp: Dumpulator, arg):
15261526
if tstr is not None:
15271527
str += f" /* {tstr} */"
15281528
return str
1529+
elif isinstance(arg, Int):
1530+
return arg.__str__()
15291531
elif isinstance(arg, int):
15301532
return hex(arg)
15311533
raise NotImplemented()

src/dumpulator/ntprimitives.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def read(self, size: int) -> bytes:
117117
def write(self, data: Union[SupportsBytes, bytes]):
118118
self.arch.write(self.ptr, data)
119119

120-
def __getitem__(self, index) -> T:
120+
def __getitem__(self, index: int) -> T:
121121
ptype = self.type
122122
if ptype is None:
123123
raise TypeError(f"No type associated with pointer")
@@ -137,6 +137,27 @@ def __getitem__(self, index) -> T:
137137
value = ctype.from_buffer(data)
138138
return ptype(value)
139139

140+
def __setitem__(self, index: int, value: T):
141+
ptype = self.type
142+
if ptype is None:
143+
raise TypeError(f"No type associated with pointer")
144+
if P.is_ptr(ptype):
145+
ptr = self.ptr + index * self.arch.ptr_size()
146+
self.arch.write_ptr(ptr, int(value))
147+
else:
148+
size = Struct.sizeof(ptype, self.arch)
149+
ptr = self.ptr + index * size
150+
if issubclass(ptype, Struct):
151+
if isinstance(value, ptype):
152+
raise TypeError(f"Expected {ptype}, got {type(value)}")
153+
self.arch.write(ptr, value)
154+
else:
155+
# TODO: support bool/enum?
156+
if not isinstance(value, int):
157+
raise TypeError(f"Expected int, got {type(value)}")
158+
ctype = Struct.translate_ctype(self.arch.ptr_type(), ptype)
159+
self.arch.write(ptr, ctype(int(value)))
160+
140161
def deref(self) -> T:
141162
return self[0]
142163

@@ -431,4 +452,4 @@ class SAL:
431452
comment: str = ""
432453

433454
def __str__(self):
434-
return self.annotation
455+
return self.annotation.split(" ")[0]

0 commit comments

Comments
 (0)