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

Add support for newlines inside "NAME" token #111

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion dissect/cstruct/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def tokenize(self) -> list[str]:
self.tokens.append(">>")
elif self.match(expected="<", append=False) and self.match(expected="<", append=False):
self.tokens.append("<<")
elif self.match(expected={" ", "\t"}, append=False):
elif self.match(expected={" ", "\n", "\t"}, append=False):
continue
else:
raise ExpressionTokenizerError(
Expand Down
2 changes: 1 addition & 1 deletion dissect/cstruct/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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>[^;]*)\])?\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
34 changes: 34 additions & 0 deletions tests/test_types_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,37 @@ def __init__(self, _0 = None, _1 = None, _2 = None, _3 = None, _4 = None):
cached = structure._make_structure__init__(5)
assert structure._make_structure__init__.cache_info() == (1, 1, 128, 1)
assert result is cached


def test_structure_definition_newline(cs: cstruct, compiled: bool) -> None:
cdef = """
struct test {
char magic[4
];

wchar wmagic[4
];
uint8 a;
uint16 b;
uint32 c;
char string[];
wchar wstring[];
};
"""
cs.endian = ">"
cs.load(cdef, compiled=compiled)

assert verify_compiled(cs.test, compiled)

buf = b"test\x00t\x00e\x00s\x00t\x01\x02\x03\x04\x05\x06\x07lalala\x00\x00t\x00e\x00s\x00t\x00\x00"

obj = cs.test()
obj.magic = "test"
obj.wmagic = "test"
obj.a = 0x01
obj.b = 0x0203
obj.c = 0x04050607
obj.string = b"lalala"
obj.wstring = "test"

assert obj.dumps() == buf