Skip to content

Commit

Permalink
Add support for pointers of pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed Mar 22, 2024
1 parent ab0500c commit 2ce458b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dissect/cstruct/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _tokencollection() -> TokenCollection:
"ENUM",
)
TOK.add(r"(?<=})\s*(?P<defs>(?:[a-zA-Z0-9_]+\s*,\s*)+[a-zA-Z0-9_]+)\s*(?=;)", "DEFS")
TOK.add(r"(?P<name>\*?\s*[a-zA-Z0-9_]+)(?:\s*:\s*(?P<bits>\d+))?(?:\[(?P<count>[^;\n]*)\])?\s*(?=;)", "NAME")
TOK.add(r"(?P<name>\**?\s*[a-zA-Z0-9_]+)(?:\s*:\s*(?P<bits>\d+))?(?:\[(?P<count>[^;\n]*)\])?\s*(?=;)", "NAME")
TOK.add(r"[a-zA-Z_][a-zA-Z0-9_]*", "IDENTIFIER")
TOK.add(r"[{}]", "BLOCK")
TOK.add(r"\$(?P<name>[^\s]+) = (?P<value>{[^}]+})\w*[\r\n]+", "LOOKUP")
Expand Down Expand Up @@ -272,7 +272,7 @@ def _parse_field_type(self, type_: MetaType, name: str) -> tuple[MetaType, str,
name = d["name"]
count_expression = d["count"]

if name.startswith("*"):
while name.startswith("*"):
name = name[1:]
type_ = self.cstruct._make_pointer(type_)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_types_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dissect.cstruct.cstruct import cstruct
from dissect.cstruct.exceptions import NullPointerDereference
from dissect.cstruct.types.pointer import Pointer

from .utils import verify_compiled

Expand Down Expand Up @@ -215,3 +216,22 @@ def test_pointer_sys_size():

cs = cstruct(pointer="uint16")
assert cs.pointer is cs.uint16


def test_pointer_of_pointer(cs: cstruct, compiled: bool):
cdef = """
struct test {
uint32 **ptr;
};
"""
cs.pointer = cs.uint8
cs.load(cdef, compiled=compiled)

assert verify_compiled(cs.test, compiled)

obj = cs.test(b"\x01\x02AAAA")
assert isinstance(obj.ptr, Pointer)
assert isinstance(obj.ptr.dereference(), Pointer)
assert obj.ptr == 1
assert obj.ptr.dereference() == 2
assert obj.ptr.dereference().dereference() == 0x41414141

0 comments on commit 2ce458b

Please sign in to comment.