Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dumpstruct for anonymous structures #50

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dissect/cstruct/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def _dumpstruct(
ci = 0
out = [f"struct {structure.name}:"]
foreground, background = None, None
for field in instance._type.fields:
for field in instance._type.lookup.values():
if getattr(field.type, "anonymous", False):
continue

if color:
foreground, background = colors[ci % len(colors)]
palette.append((instance._size(field.name), background))
Expand Down
43 changes: 26 additions & 17 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,41 @@ def test_hexdump(capsys):


def test_dumpstruct(capsys, compiled):
Schamper marked this conversation as resolved.
Show resolved Hide resolved
cdef = """
cdef_basic = """
struct test {
uint32 testval;
};
"""
cs = cstruct.cstruct()
cs.load(cdef, compiled=compiled)
cdef_anonymous = """
struct test {
struct {
uint32 testval;
};
};
"""

assert verify_compiled(cs.test, compiled)
for cdef in [cdef_basic, cdef_anonymous]:
cs = cstruct.cstruct()
cs.load(cdef, compiled=compiled)

buf = b"\x39\x05\x00\x00"
obj = cs.test(buf)
assert verify_compiled(cs.test, compiled)

dumpstruct(cs.test, buf)
captured_1 = capsys.readouterr()
buf = b"\x39\x05\x00\x00"
obj = cs.test(buf)

dumpstruct(obj)
captured_2 = capsys.readouterr()
dumpstruct(cs.test, buf)
captured_1 = capsys.readouterr()

assert captured_1.out == captured_2.out
dumpstruct(obj)
captured_2 = capsys.readouterr()

out_1 = dumpstruct(cs.test, buf, output="string")
out_2 = dumpstruct(obj, output="string")
assert captured_1.out == captured_2.out

assert out_1 == out_2
out_1 = dumpstruct(cs.test, buf, output="string")
out_2 = dumpstruct(obj, output="string")

with pytest.raises(ValueError) as excinfo:
dumpstruct(obj, output="generator")
assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')."
assert out_1 == out_2

with pytest.raises(ValueError) as excinfo:
dumpstruct(obj, output="generator")
assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')."