Skip to content

Commit

Permalink
Fix enum behaviour in dumpstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed Sep 23, 2024
1 parent 9adc396 commit 638a567
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dissect/cstruct/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import pprint
import string
import sys
from enum import Enum
from typing import Iterator

from dissect.cstruct.types.pointer import Pointer
from dissect.cstruct.types.structure import Structure

COLOR_RED = "\033[1;31m"
Expand Down Expand Up @@ -158,7 +160,7 @@ def _dumpstruct(
ci += 1

value = getattr(structure, field.name)
if isinstance(value, str):
if isinstance(value, (str, Pointer, Enum)):
value = repr(value)
elif isinstance(value, int):
value = hex(value)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ def test_dumpstruct_anonymous(cs: cstruct, capsys: pytest.CaptureFixture, compil
assert str(excinfo.value) == "Invalid output argument: 'generator' (should be 'print' or 'string')."


def test_dumpstruct_enum(cs: cstruct, compiled: bool) -> None:
cdef = """
enum Test16 : uint16 {
A = 0x1,
B = 0x2
};
struct test {
Test16 testval;
};
"""
cs.load(cdef, compiled=compiled)

assert verify_compiled(cs.test, compiled)

buf = b"\x02\x00"
obj = cs.test(buf)

out1 = utils.dumpstruct(cs.test, buf, output="string")
out2 = utils.dumpstruct(obj, output="string")

assert "<Test16.B: 2>" in out1
assert "<Test16.B: 2>" in out2


def test_pack_unpack() -> None:
endian = "little"
sign = False
Expand Down

0 comments on commit 638a567

Please sign in to comment.