Skip to content

Commit 2ce458b

Browse files
committed
Add support for pointers of pointers
1 parent ab0500c commit 2ce458b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

dissect/cstruct/parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _tokencollection() -> TokenCollection:
5959
"ENUM",
6060
)
6161
TOK.add(r"(?<=})\s*(?P<defs>(?:[a-zA-Z0-9_]+\s*,\s*)+[a-zA-Z0-9_]+)\s*(?=;)", "DEFS")
62-
TOK.add(r"(?P<name>\*?\s*[a-zA-Z0-9_]+)(?:\s*:\s*(?P<bits>\d+))?(?:\[(?P<count>[^;\n]*)\])?\s*(?=;)", "NAME")
62+
TOK.add(r"(?P<name>\**?\s*[a-zA-Z0-9_]+)(?:\s*:\s*(?P<bits>\d+))?(?:\[(?P<count>[^;\n]*)\])?\s*(?=;)", "NAME")
6363
TOK.add(r"[a-zA-Z_][a-zA-Z0-9_]*", "IDENTIFIER")
6464
TOK.add(r"[{}]", "BLOCK")
6565
TOK.add(r"\$(?P<name>[^\s]+) = (?P<value>{[^}]+})\w*[\r\n]+", "LOOKUP")
@@ -272,7 +272,7 @@ def _parse_field_type(self, type_: MetaType, name: str) -> tuple[MetaType, str,
272272
name = d["name"]
273273
count_expression = d["count"]
274274

275-
if name.startswith("*"):
275+
while name.startswith("*"):
276276
name = name[1:]
277277
type_ = self.cstruct._make_pointer(type_)
278278

tests/test_types_pointer.py

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from dissect.cstruct.cstruct import cstruct
66
from dissect.cstruct.exceptions import NullPointerDereference
7+
from dissect.cstruct.types.pointer import Pointer
78

89
from .utils import verify_compiled
910

@@ -215,3 +216,22 @@ def test_pointer_sys_size():
215216

216217
cs = cstruct(pointer="uint16")
217218
assert cs.pointer is cs.uint16
219+
220+
221+
def test_pointer_of_pointer(cs: cstruct, compiled: bool):
222+
cdef = """
223+
struct test {
224+
uint32 **ptr;
225+
};
226+
"""
227+
cs.pointer = cs.uint8
228+
cs.load(cdef, compiled=compiled)
229+
230+
assert verify_compiled(cs.test, compiled)
231+
232+
obj = cs.test(b"\x01\x02AAAA")
233+
assert isinstance(obj.ptr, Pointer)
234+
assert isinstance(obj.ptr.dereference(), Pointer)
235+
assert obj.ptr == 1
236+
assert obj.ptr.dereference() == 2
237+
assert obj.ptr.dereference().dereference() == 0x41414141

0 commit comments

Comments
 (0)