dissect.cstruct 5.0.dev15__tar.gz → 5.0.dev16__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/PKG-INFO +1 -1
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/exception.py +4 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/expression.py +14 -18
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/lexer.py +10 -5
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/parser.py +21 -11
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/PKG-INFO +1 -1
- dissect_cstruct-5.0.dev16/dissect.cstruct.egg-info/scm_version.json +8 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_compiler.py +2 -2
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_expression.py +9 -9
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_parser.py +72 -0
- dissect_cstruct-5.0.dev15/dissect.cstruct.egg-info/scm_version.json +0 -8
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/.git-blame-ignore-revs +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/.gitattributes +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/CHANGELOG.md +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/COPYRIGHT +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/LICENSE +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/MANIFEST.in +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/README.md +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/__init__.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/bitbuffer.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/compiler.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/cstruct.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/tools/__init__.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/tools/stubgen.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/__init__.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/base.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/char.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/enum.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/flag.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/int.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/leb128.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/packed.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/pointer.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/structure.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/void.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/types/wchar.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect/cstruct/util.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/SOURCES.txt +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/entry_points.txt +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/requires.txt +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/scm_file_list.json +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/top_level.txt +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/examples/disk.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/examples/mirai.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/examples/pe.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/examples/protobuf.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/examples/secdesc.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/pyproject.toml +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/setup.cfg +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/__init__.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/_data/testdef.txt +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/_docs/Makefile +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/_docs/__init__.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/_docs/conf.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/_docs/index.rst +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/conftest.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_align.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_annotations.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_basic.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_benchmark.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_bitbuffer.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_bitfield.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_ctypes.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_lexer.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_tools_stubgen.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_base.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_char.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_custom.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_enum.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_flag.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_int.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_leb128.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_packed.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_pointer.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_structure.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_union.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_void.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_types_wchar.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/test_utils.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tests/utils.py +0 -0
- {dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/tox.ini +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.cstruct
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.dev16
|
|
4
4
|
Summary: A Dissect module implementing a parser for C-like structures: structure parsing in Python made easy
|
|
5
5
|
Author-email: Dissect Team <dissect@fox-it.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -2,8 +2,8 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from typing import TYPE_CHECKING, Any
|
|
4
4
|
|
|
5
|
-
from dissect.cstruct.exception import ExpressionParserError
|
|
6
|
-
from dissect.cstruct.lexer import IDENTIFIER_TYPES, Lexer, TokenCursor, TokenType
|
|
5
|
+
from dissect.cstruct.exception import ExpressionParserError, UnknownIdentifierError
|
|
6
|
+
from dissect.cstruct.lexer import IDENTIFIER_TYPES, Lexer, TokenCursor, TokenType, join_tokens
|
|
7
7
|
from dissect.cstruct.util import offsetof, sizeof
|
|
8
8
|
|
|
9
9
|
if TYPE_CHECKING:
|
|
@@ -87,11 +87,7 @@ FUNCTIONS: dict[TokenType, Callable[..., int]] = {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
|
|
90
|
-
def
|
|
91
|
-
return ExpressionParserError(f"line {line}: {msg}")
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def _identifier(cs: cstruct, context: dict[str, Any], ident: str, line: int) -> int:
|
|
90
|
+
def _identifier(cs: cstruct, context: dict[str, Any], ident: str) -> int:
|
|
95
91
|
"""Resolve an identifier to an integer value."""
|
|
96
92
|
obj = None
|
|
97
93
|
for i, part in enumerate(ident.split(".")):
|
|
@@ -103,16 +99,16 @@ def _identifier(cs: cstruct, context: dict[str, Any], ident: str, line: int) ->
|
|
|
103
99
|
elif part in cs.types:
|
|
104
100
|
obj = cs.types[part]
|
|
105
101
|
else:
|
|
106
|
-
raise
|
|
102
|
+
raise UnknownIdentifierError(f"Unknown identifier: '{ident}'")
|
|
107
103
|
else:
|
|
108
104
|
obj = obj.get(part, _MISSING) if isinstance(obj, dict) else getattr(obj, part, _MISSING)
|
|
109
105
|
if obj is _MISSING:
|
|
110
|
-
raise
|
|
106
|
+
raise UnknownIdentifierError(f"Unknown identifier: '{ident}'")
|
|
111
107
|
|
|
112
108
|
try:
|
|
113
109
|
return int(obj)
|
|
114
110
|
except (ValueError, TypeError):
|
|
115
|
-
raise
|
|
111
|
+
raise ExpressionParserError(f"Identifier '{ident}' does not resolve to an integer")
|
|
116
112
|
|
|
117
113
|
|
|
118
114
|
# Globals used by the source code of an expression
|
|
@@ -129,8 +125,8 @@ class _Evaluator:
|
|
|
129
125
|
def number(self, value: int) -> int:
|
|
130
126
|
return value
|
|
131
127
|
|
|
132
|
-
def identifier(self, ident: str
|
|
133
|
-
return _identifier(self.cs, self.context, ident
|
|
128
|
+
def identifier(self, ident: str) -> int:
|
|
129
|
+
return _identifier(self.cs, self.context, ident)
|
|
134
130
|
|
|
135
131
|
def unary(self, operator: TokenType, value: int) -> int:
|
|
136
132
|
return UNARY_OPERATORS[operator](value)
|
|
@@ -152,10 +148,10 @@ class _SourceBuilder:
|
|
|
152
148
|
def number(self, value: int) -> str:
|
|
153
149
|
return repr(value)
|
|
154
150
|
|
|
155
|
-
def identifier(self, ident: str
|
|
151
|
+
def identifier(self, ident: str) -> str:
|
|
156
152
|
if ident in self.variables:
|
|
157
153
|
return self.variables[ident]
|
|
158
|
-
return f"_identifier(cs, {self.context}, {ident!r}
|
|
154
|
+
return f"_identifier(cs, {self.context}, {ident!r})"
|
|
159
155
|
|
|
160
156
|
def unary(self, operator: TokenType, value: str) -> str:
|
|
161
157
|
return f"({OPERATOR_SOURCE[operator]}{value})"
|
|
@@ -195,7 +191,7 @@ class Expression(TokenCursor):
|
|
|
195
191
|
self._queue = []
|
|
196
192
|
|
|
197
193
|
def _error(self, msg: str, *, token: Token | None = None) -> ExpressionParserError:
|
|
198
|
-
return
|
|
194
|
+
return ExpressionParserError(msg)
|
|
199
195
|
|
|
200
196
|
def _evaluate_expression(self, backend: _Evaluator | _SourceBuilder) -> None:
|
|
201
197
|
operator = self._stack.pop(-1)
|
|
@@ -266,18 +262,18 @@ class Expression(TokenCursor):
|
|
|
266
262
|
|
|
267
263
|
num_args = FUNCTION_TOKENS[func]
|
|
268
264
|
while num_args > 1:
|
|
269
|
-
self._queue.append(self._collect_until(TokenType.COMMA))
|
|
265
|
+
self._queue.append(join_tokens(self._collect_until(TokenType.COMMA)))
|
|
270
266
|
self._expect(TokenType.COMMA)
|
|
271
267
|
num_args -= 1
|
|
272
268
|
|
|
273
|
-
self._queue.append(self._collect_until(TokenType.RPAREN))
|
|
269
|
+
self._queue.append(join_tokens(self._collect_until(TokenType.RPAREN)))
|
|
274
270
|
self._expect(TokenType.RPAREN)
|
|
275
271
|
|
|
276
272
|
# Evaluate immediately
|
|
277
273
|
self._evaluate_expression(backend)
|
|
278
274
|
|
|
279
275
|
elif token.type in IDENTIFIER_TYPES:
|
|
280
|
-
self._queue.append(backend.identifier(self._take().value
|
|
276
|
+
self._queue.append(backend.identifier(self._take().value))
|
|
281
277
|
|
|
282
278
|
elif token.type == TokenType.LPAREN:
|
|
283
279
|
if self._previous().type == TokenType.NUMBER:
|
|
@@ -143,6 +143,11 @@ def format_error_context(data: str, lineno: int, window: int = 1) -> str:
|
|
|
143
143
|
return "\n".join(f"{start + i + 1:>{no_len}}: {line}" for i, line in enumerate(context))
|
|
144
144
|
|
|
145
145
|
|
|
146
|
+
def join_tokens(tokens: list[Token]) -> str:
|
|
147
|
+
"""Join the values of the tokens with spaces, e.g. to evaluate them as an expression."""
|
|
148
|
+
return " ".join(token.value for token in tokens)
|
|
149
|
+
|
|
150
|
+
|
|
146
151
|
class Lexer:
|
|
147
152
|
"""Lexer compatible with C-like syntax for struct definitions and preprocessor directives."""
|
|
148
153
|
|
|
@@ -566,11 +571,11 @@ class TokenCursor:
|
|
|
566
571
|
raise self._error(f"expected {expected}, got {actual}")
|
|
567
572
|
return self._take()
|
|
568
573
|
|
|
569
|
-
def _collect_until(self, *terminators: TokenType) ->
|
|
570
|
-
"""Collect
|
|
574
|
+
def _collect_until(self, *terminators: TokenType) -> list[Token]:
|
|
575
|
+
"""Collect tokens until one of the terminators is reached, tracking nesting."""
|
|
571
576
|
terminators_set = set(terminators)
|
|
572
577
|
start = self._current()
|
|
573
|
-
|
|
578
|
+
tokens: list[Token] = []
|
|
574
579
|
depth = 0
|
|
575
580
|
|
|
576
581
|
while (token := self._current()).type != TokenType.EOF:
|
|
@@ -584,13 +589,13 @@ class TokenCursor:
|
|
|
584
589
|
if depth < 0:
|
|
585
590
|
raise self._error("unmatched closing bracket", token=token)
|
|
586
591
|
|
|
587
|
-
|
|
592
|
+
tokens.append(token)
|
|
588
593
|
self._pos += 1
|
|
589
594
|
|
|
590
595
|
if depth > 0:
|
|
591
596
|
raise self._error("unclosed opening bracket", token=start)
|
|
592
597
|
|
|
593
|
-
return
|
|
598
|
+
return tokens
|
|
594
599
|
|
|
595
600
|
def _error(self, msg: str, *, token: Token | None = None) -> Exception:
|
|
596
601
|
"""Subclasses should override to return an appropriate exception."""
|
|
@@ -9,9 +9,10 @@ from dissect.cstruct.exception import (
|
|
|
9
9
|
ExpressionParserError,
|
|
10
10
|
LexerError,
|
|
11
11
|
ParserError,
|
|
12
|
+
UnknownIdentifierError,
|
|
12
13
|
)
|
|
13
14
|
from dissect.cstruct.expression import Expression
|
|
14
|
-
from dissect.cstruct.lexer import IDENTIFIER_TYPES, TokenCursor, TokenType, format_error_context, tokenize
|
|
15
|
+
from dissect.cstruct.lexer import IDENTIFIER_TYPES, TokenCursor, TokenType, format_error_context, join_tokens, tokenize
|
|
15
16
|
from dissect.cstruct.types import BaseArray, BaseType, Field, Structure
|
|
16
17
|
|
|
17
18
|
if TYPE_CHECKING:
|
|
@@ -422,8 +423,13 @@ class CStyleParser(Parser):
|
|
|
422
423
|
member_name = self._expect(TokenType.IDENTIFIER).value
|
|
423
424
|
|
|
424
425
|
if self._match(TokenType.EQUALS):
|
|
425
|
-
|
|
426
|
-
|
|
426
|
+
if not (tokens := self._collect_until(TokenType.COMMA, TokenType.RBRACE)):
|
|
427
|
+
raise self._error("expected an expression")
|
|
428
|
+
|
|
429
|
+
try:
|
|
430
|
+
value = Expression(join_tokens(tokens)).evaluate(self.cs, values)
|
|
431
|
+
except ExpressionParserError as e:
|
|
432
|
+
raise self._error(str(e), token=tokens[0]) from None
|
|
427
433
|
else:
|
|
428
434
|
value = next_value
|
|
429
435
|
|
|
@@ -508,17 +514,21 @@ class CStyleParser(Parser):
|
|
|
508
514
|
dimensions: list[int | Expression] = []
|
|
509
515
|
|
|
510
516
|
while self._match(TokenType.LBRACKET):
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
517
|
+
tokens = self._collect_until(TokenType.RBRACKET)
|
|
518
|
+
self._expect(TokenType.RBRACKET)
|
|
519
|
+
|
|
520
|
+
count = None
|
|
521
|
+
if tokens:
|
|
522
|
+
count = Expression(join_tokens(tokens))
|
|
516
523
|
try:
|
|
517
524
|
count = count.evaluate(self.cs)
|
|
518
|
-
except
|
|
525
|
+
except UnknownIdentifierError:
|
|
526
|
+
# The size refers to a field, so it's evaluated when reading the structure
|
|
519
527
|
pass
|
|
520
|
-
|
|
521
|
-
|
|
528
|
+
except ExpressionParserError as e:
|
|
529
|
+
raise self._error(str(e), token=tokens[0]) from None
|
|
530
|
+
|
|
531
|
+
dimensions.append(count)
|
|
522
532
|
|
|
523
533
|
type_ = base_type
|
|
524
534
|
for count in reversed(dimensions):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.cstruct
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.dev16
|
|
4
4
|
Summary: A Dissect module implementing a parser for C-like structures: structure parsing in Python made easy
|
|
5
5
|
Author-email: Dissect Team <dissect@fox-it.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -423,11 +423,11 @@ def test_compiled_dynamic_array_sizes() -> None:
|
|
|
423
423
|
assert [line for line in lines if line.startswith("_count = ") or "._read" in line] == [
|
|
424
424
|
"_count = max(0, int(_value0))",
|
|
425
425
|
"_value3 = values['a'] = _type_call(_type3, _type4._read_array(stream, _count, context=values, endian=endian))",
|
|
426
|
-
"_count = max(0, (int(_value0) - _identifier(cs, values, 'HDR'
|
|
426
|
+
"_count = max(0, (int(_value0) - _identifier(cs, values, 'HDR')))",
|
|
427
427
|
"_value4 = values['b'] = _type_call(_type5, _type6._read_array(stream, _count, context=values, endian=endian))",
|
|
428
428
|
"_count = max(0, (int(_value1) * 2))",
|
|
429
429
|
"_value5 = values['c'] = _type_call(_type7, _type8._read_array(stream, _count, context=values, endian=endian))",
|
|
430
|
-
"_count = max(0, _identifier(cs, values, 'hdr.size'
|
|
430
|
+
"_count = max(0, _identifier(cs, values, 'hdr.size'))",
|
|
431
431
|
"_value6 = values['d'] = _type_call(_type9, _type0._read_array(stream, _count, context=values, endian=endian))",
|
|
432
432
|
"_count = max(0, (int(_value0) + _sizeof(cs, 'inner')))",
|
|
433
433
|
"_value7 = values['e'] = _type_call(_type10, _type2._read_array("
|
|
@@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Any
|
|
|
4
4
|
|
|
5
5
|
import pytest
|
|
6
6
|
|
|
7
|
-
from dissect.cstruct.exception import ExpressionParserError, LexerError, ResolveError
|
|
7
|
+
from dissect.cstruct.exception import ExpressionParserError, LexerError, ResolveError, UnknownIdentifierError
|
|
8
8
|
from dissect.cstruct.expression import SOURCE_GLOBALS, Expression
|
|
9
9
|
|
|
10
10
|
if TYPE_CHECKING:
|
|
@@ -174,13 +174,13 @@ def test_expression(cs_with_consts: cstruct, expression: str, answer: int) -> No
|
|
|
174
174
|
),
|
|
175
175
|
pytest.param(
|
|
176
176
|
"UNKNOWN",
|
|
177
|
-
|
|
177
|
+
UnknownIdentifierError,
|
|
178
178
|
"Unknown identifier: 'UNKNOWN'",
|
|
179
179
|
id="unknown-identifier",
|
|
180
180
|
),
|
|
181
181
|
pytest.param(
|
|
182
182
|
"UNKNOWN.UNKNOWN",
|
|
183
|
-
|
|
183
|
+
UnknownIdentifierError,
|
|
184
184
|
"Unknown identifier: 'UNKNOWN.UNKNOWN'",
|
|
185
185
|
id="unknown-identifier",
|
|
186
186
|
),
|
|
@@ -261,7 +261,7 @@ def test_dotted_identifier_unknown_member(cs: cstruct) -> None:
|
|
|
261
261
|
};
|
|
262
262
|
""")
|
|
263
263
|
|
|
264
|
-
with pytest.raises(
|
|
264
|
+
with pytest.raises(UnknownIdentifierError, match="Unknown identifier: 'color\\.NOTAMEMBER'"):
|
|
265
265
|
Expression("color.NOTAMEMBER").evaluate(cs)
|
|
266
266
|
|
|
267
267
|
|
|
@@ -354,9 +354,9 @@ def test_expression_source_invalid(cs: cstruct, source: str) -> None:
|
|
|
354
354
|
# Numbers in other bases
|
|
355
355
|
("0x10 + 0b101 - 0o7", "((16 + 5) - 7)"),
|
|
356
356
|
# Functions, and identifiers that are resolved from the context
|
|
357
|
-
("sizeof(test) * X - -1", "((_sizeof(cs, 'test') * _identifier(cs, context, 'X'
|
|
357
|
+
("sizeof(test) * X - -1", "((_sizeof(cs, 'test') * _identifier(cs, context, 'X')) - (-1))"),
|
|
358
358
|
("offsetof(test, b) + a", "(_offsetof(cs, 'test', 'b') + a)"),
|
|
359
|
-
("hdr.size * 4", "(_identifier(cs, context, 'hdr.size'
|
|
359
|
+
("hdr.size * 4", "(_identifier(cs, context, 'hdr.size') * 4)"),
|
|
360
360
|
],
|
|
361
361
|
)
|
|
362
362
|
def test_expression_source_code(cs: cstruct, expression: str, source: str) -> None:
|
|
@@ -380,6 +380,6 @@ def test_expression_source_code(cs: cstruct, expression: str, source: str) -> No
|
|
|
380
380
|
|
|
381
381
|
|
|
382
382
|
def test_expression_source_context() -> None:
|
|
383
|
-
"""Test that identifiers are resolved from the context with the given name
|
|
384
|
-
expected = "(_identifier(cs, values, 'X'
|
|
385
|
-
assert Expression("X
|
|
383
|
+
"""Test that identifiers are resolved from the context with the given name."""
|
|
384
|
+
expected = "(_identifier(cs, values, 'X') + _identifier(cs, values, 'b'))"
|
|
385
|
+
assert Expression("X + b").source(context="values") == expected
|
|
@@ -630,6 +630,78 @@ def test_preprocessor_define_from_enum_in_struct(cs: cstruct) -> None:
|
|
|
630
630
|
assert cs.packet.__members__["payload"].type.__num_entries__ == 20
|
|
631
631
|
|
|
632
632
|
|
|
633
|
+
@pytest.mark.parametrize(
|
|
634
|
+
("cdef", "message"),
|
|
635
|
+
[
|
|
636
|
+
(
|
|
637
|
+
"""
|
|
638
|
+
struct test {
|
|
639
|
+
uint8 a;
|
|
640
|
+
char b[N];
|
|
641
|
+
};
|
|
642
|
+
""",
|
|
643
|
+
"""\
|
|
644
|
+
line 4: Identifier 'N' does not resolve to an integer
|
|
645
|
+
2: struct test {
|
|
646
|
+
3: uint8 a;
|
|
647
|
+
4: char b[N];
|
|
648
|
+
""",
|
|
649
|
+
),
|
|
650
|
+
(
|
|
651
|
+
"""
|
|
652
|
+
struct test {
|
|
653
|
+
uint8 a;
|
|
654
|
+
char b[4 4];
|
|
655
|
+
};
|
|
656
|
+
""",
|
|
657
|
+
"""\
|
|
658
|
+
line 4: Invalid expression: too many operands
|
|
659
|
+
2: struct test {
|
|
660
|
+
3: uint8 a;
|
|
661
|
+
4: char b[4 4];
|
|
662
|
+
""",
|
|
663
|
+
),
|
|
664
|
+
(
|
|
665
|
+
"""
|
|
666
|
+
enum test : uint8 {
|
|
667
|
+
A = 1,
|
|
668
|
+
B = A +,
|
|
669
|
+
C
|
|
670
|
+
};
|
|
671
|
+
""",
|
|
672
|
+
"""\
|
|
673
|
+
line 4: Invalid expression: not enough operands
|
|
674
|
+
2: enum test : uint8 {
|
|
675
|
+
3: A = 1,
|
|
676
|
+
4: B = A +,
|
|
677
|
+
""",
|
|
678
|
+
),
|
|
679
|
+
(
|
|
680
|
+
"""
|
|
681
|
+
enum test : uint8 {
|
|
682
|
+
A = 1,
|
|
683
|
+
B = ,
|
|
684
|
+
C
|
|
685
|
+
};
|
|
686
|
+
""",
|
|
687
|
+
"""\
|
|
688
|
+
line 4: expected an expression
|
|
689
|
+
2: enum test : uint8 {
|
|
690
|
+
3: A = 1,
|
|
691
|
+
4: B = ,
|
|
692
|
+
""",
|
|
693
|
+
),
|
|
694
|
+
],
|
|
695
|
+
)
|
|
696
|
+
def test_expression_invalid(cs: cstruct, cdef: str, message: str) -> None:
|
|
697
|
+
"""Test that an array size or enum value that is missing or can't be evaluated is an error at its line."""
|
|
698
|
+
cs.load("#define N LENGTH")
|
|
699
|
+
|
|
700
|
+
with pytest.raises(ParserError) as exc_info:
|
|
701
|
+
cs.load(textwrap.dedent(cdef))
|
|
702
|
+
assert str(exc_info.value) == textwrap.dedent(message.rstrip())
|
|
703
|
+
|
|
704
|
+
|
|
633
705
|
def test_error_context(cs: cstruct) -> None:
|
|
634
706
|
"""Test the context window around errors: 1 line before, the error line, up to 2 lines after."""
|
|
635
707
|
# Error on line 1: no preceding lines available; shows lines 1, 2, 3
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/entry_points.txt
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/requires.txt
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/scm_file_list.json
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev15 → dissect_cstruct-5.0.dev16}/dissect.cstruct.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|