dissect.cstruct 5.0.dev20__py3-none-any.whl → 5.0.dev22__py3-none-any.whl
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/__init__.py +12 -0
- dissect/cstruct/bitbuffer.py +5 -3
- dissect/cstruct/compiler.py +25 -14
- dissect/cstruct/cstruct.py +12 -10
- dissect/cstruct/declarative.py +3 -2
- dissect/cstruct/exception.py +29 -15
- dissect/cstruct/expression.py +142 -79
- dissect/cstruct/lexer.py +15 -6
- dissect/cstruct/parser.py +77 -34
- dissect/cstruct/types/base.py +27 -5
- dissect/cstruct/types/char.py +8 -4
- dissect/cstruct/types/enum.py +2 -1
- dissect/cstruct/types/int.py +23 -6
- dissect/cstruct/types/leb128.py +19 -6
- dissect/cstruct/types/packed.py +12 -6
- dissect/cstruct/types/pointer.py +6 -2
- dissect/cstruct/types/structure.py +11 -2
- dissect/cstruct/types/wchar.py +16 -8
- dissect/cstruct/util.py +22 -5
- {dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/METADATA +1 -1
- dissect_cstruct-5.0.dev22.dist-info/RECORD +32 -0
- dissect_cstruct-5.0.dev20.dist-info/RECORD +0 -32
- {dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/WHEEL +0 -0
- {dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/entry_points.txt +0 -0
- {dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/licenses/COPYRIGHT +0 -0
- {dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/licenses/LICENSE +0 -0
- {dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/top_level.txt +0 -0
dissect/cstruct/types/base.py
CHANGED
|
@@ -2,12 +2,13 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import contextlib
|
|
4
4
|
import functools
|
|
5
|
+
import struct
|
|
5
6
|
import warnings
|
|
6
7
|
from io import SEEK_CUR, BytesIO
|
|
7
8
|
from types import MethodType
|
|
8
9
|
from typing import TYPE_CHECKING, Any, BinaryIO, ClassVar, TypeVar
|
|
9
10
|
|
|
10
|
-
from dissect.cstruct.exception import
|
|
11
|
+
from dissect.cstruct.exception import EOFError, Error, ReadError, WriteError
|
|
11
12
|
from dissect.cstruct.expression import Expression
|
|
12
13
|
from dissect.cstruct.util import classproperty, normalize_endianness
|
|
13
14
|
|
|
@@ -23,6 +24,9 @@ EOF = -0xE0F # Negative counts are illegal anyway, so abuse that for our EOF se
|
|
|
23
24
|
|
|
24
25
|
_IMMUTABLE_TYPES = (int, float, bytes, str)
|
|
25
26
|
|
|
27
|
+
# Errors of values that can't be written, e.g. a number out of range or a missing field of a structure
|
|
28
|
+
_WRITE_ERRORS = (struct.error, OverflowError, TypeError, ValueError, AttributeError)
|
|
29
|
+
|
|
26
30
|
|
|
27
31
|
class MetaType(type):
|
|
28
32
|
"""Base metaclass for cstruct type classes."""
|
|
@@ -144,8 +148,17 @@ class MetaType(type):
|
|
|
144
148
|
Returns:
|
|
145
149
|
The amount of bytes written.
|
|
146
150
|
"""
|
|
151
|
+
if not hasattr(stream, "write"):
|
|
152
|
+
raise TypeError("Invalid object type")
|
|
153
|
+
|
|
147
154
|
endian = normalize_endianness(endian) if endian is not None else cls.__cs__.endian
|
|
148
|
-
|
|
155
|
+
|
|
156
|
+
try:
|
|
157
|
+
return cls._write(stream, value, endian=endian)
|
|
158
|
+
except (OSError, Error):
|
|
159
|
+
raise
|
|
160
|
+
except _WRITE_ERRORS as e:
|
|
161
|
+
raise WriteError(f"Cannot write {cls.__name__}: {e}") from e
|
|
149
162
|
|
|
150
163
|
def dumps(cls, value: Any, *, endian: AllowedEndianness | None = None) -> bytes:
|
|
151
164
|
"""Dump a value to a byte string.
|
|
@@ -161,7 +174,12 @@ class MetaType(type):
|
|
|
161
174
|
endian = normalize_endianness(endian) if endian is not None else cls.__cs__.endian
|
|
162
175
|
|
|
163
176
|
out = BytesIO()
|
|
164
|
-
|
|
177
|
+
try:
|
|
178
|
+
cls._write(out, value, endian=endian)
|
|
179
|
+
except Error:
|
|
180
|
+
raise
|
|
181
|
+
except _WRITE_ERRORS as e:
|
|
182
|
+
raise WriteError(f"Cannot write {cls.__name__}: {e}") from e
|
|
165
183
|
return out.getvalue()
|
|
166
184
|
|
|
167
185
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self: # type: ignore
|
|
@@ -380,10 +398,14 @@ class BaseArray(BaseType):
|
|
|
380
398
|
|
|
381
399
|
try:
|
|
382
400
|
return max(0, cls.__num_entries__.evaluate(cls.__cs__, context))
|
|
383
|
-
except
|
|
401
|
+
except Error:
|
|
402
|
+
# E.g. an unknown identifier, which is also what EOF evaluates to
|
|
384
403
|
if cls.__num_entries__.expression != "EOF":
|
|
385
404
|
raise
|
|
386
405
|
return EOF
|
|
406
|
+
except (ArithmeticError, ValueError) as e:
|
|
407
|
+
# E.g. a division by a field that is zero
|
|
408
|
+
raise ReadError(f"Invalid array size '{cls.__num_entries__.expression}': {e}") from e
|
|
387
409
|
|
|
388
410
|
@classmethod
|
|
389
411
|
def _write(cls, stream: BinaryIO, data: list[Any], *, endian: Endianness) -> int:
|
|
@@ -404,7 +426,7 @@ class BaseArray(BaseType):
|
|
|
404
426
|
"""
|
|
405
427
|
if remaining := cls.__num_entries__ - (actual_size := len(data)):
|
|
406
428
|
if remaining < 0:
|
|
407
|
-
raise
|
|
429
|
+
raise WriteError(f"Expected static array size {cls.__num_entries__}, got {actual_size} instead.")
|
|
408
430
|
data = data + [cls.__type__.__default__()] * remaining
|
|
409
431
|
|
|
410
432
|
return data
|
dissect/cstruct/types/char.py
CHANGED
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from typing import TYPE_CHECKING, Any, BinaryIO
|
|
4
4
|
|
|
5
|
-
from dissect.cstruct.exception import
|
|
5
|
+
from dissect.cstruct.exception import EOFError, WriteError
|
|
6
6
|
from dissect.cstruct.types.base import EOF, BaseArray, BaseType, _is_seekable, _read_null_terminated
|
|
7
7
|
|
|
8
8
|
if TYPE_CHECKING:
|
|
@@ -35,7 +35,7 @@ class CharArray(bytes, BaseArray):
|
|
|
35
35
|
|
|
36
36
|
if not cls.__dynamic__ and (remaining := cls.__num_entries__ - (actual_size := len(data))):
|
|
37
37
|
if remaining < 0:
|
|
38
|
-
raise
|
|
38
|
+
raise WriteError(f"Expected static array size {cls.__num_entries__}, got {actual_size} instead.")
|
|
39
39
|
data += b"\x00" * remaining
|
|
40
40
|
|
|
41
41
|
return stream.write(data)
|
|
@@ -61,7 +61,11 @@ class Char(bytes, BaseType):
|
|
|
61
61
|
if count == 0:
|
|
62
62
|
return type.__call__(cls, b"")
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
try:
|
|
65
|
+
data = stream.read(-1 if count == EOF else count)
|
|
66
|
+
except OverflowError:
|
|
67
|
+
raise EOFError(f"Read 0 bytes, but expected {count}") from None
|
|
68
|
+
|
|
65
69
|
if count != EOF and len(data) != count:
|
|
66
70
|
raise EOFError(f"Read {len(data)} bytes, but expected {count}")
|
|
67
71
|
|
|
@@ -94,6 +98,6 @@ class Char(bytes, BaseType):
|
|
|
94
98
|
data = data.encode("latin-1")
|
|
95
99
|
|
|
96
100
|
if (actual_size := len(data)) > 1:
|
|
97
|
-
raise
|
|
101
|
+
raise WriteError(f"Expected a single character, got {actual_size} instead.")
|
|
98
102
|
|
|
99
103
|
return stream.write(data or b"\x00")
|
dissect/cstruct/types/enum.py
CHANGED
|
@@ -4,6 +4,7 @@ import sys
|
|
|
4
4
|
from enum import EnumMeta, IntEnum, IntFlag
|
|
5
5
|
from typing import TYPE_CHECKING, Any, BinaryIO, TypeVar, overload
|
|
6
6
|
|
|
7
|
+
from dissect.cstruct.exception import DefinitionError
|
|
7
8
|
from dissect.cstruct.types.base import BaseType, MetaType
|
|
8
9
|
|
|
9
10
|
if TYPE_CHECKING:
|
|
@@ -54,7 +55,7 @@ class EnumMetaType(EnumMeta, MetaType):
|
|
|
54
55
|
# cs is the cstruct instance, but we can't isinstance check it due to circular imports
|
|
55
56
|
cs = value
|
|
56
57
|
if not issubclass(type_, int):
|
|
57
|
-
raise
|
|
58
|
+
raise DefinitionError("Enum can only be created from int type")
|
|
58
59
|
|
|
59
60
|
enum_cls = super().__call__(name, *args, **kwargs)
|
|
60
61
|
enum_cls.__cs__ = cs
|
dissect/cstruct/types/int.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from typing import TYPE_CHECKING, Any, BinaryIO
|
|
4
5
|
|
|
6
|
+
from dissect.cstruct.exception import EOFError
|
|
5
7
|
from dissect.cstruct.types.base import EOF, BaseType, MetaType
|
|
6
|
-
from dissect.cstruct.util import ENDIANNESS_TO_BYTEORDER_MAP
|
|
8
|
+
from dissect.cstruct.util import ENDIANNESS_TO_BYTEORDER_MAP, classproperty
|
|
7
9
|
|
|
8
10
|
if TYPE_CHECKING:
|
|
9
11
|
from typing_extensions import Self
|
|
@@ -14,7 +16,7 @@ if TYPE_CHECKING:
|
|
|
14
16
|
class Int(int, BaseType):
|
|
15
17
|
"""Integer type that can span an arbitrary amount of bytes."""
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
__signed__: bool
|
|
18
20
|
|
|
19
21
|
@classmethod
|
|
20
22
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self:
|
|
@@ -23,7 +25,7 @@ class Int(int, BaseType):
|
|
|
23
25
|
if len(data) != cls.__size__:
|
|
24
26
|
raise EOFError(f"Read {len(data)} bytes, but expected {cls.__size__}")
|
|
25
27
|
|
|
26
|
-
return type.__call__(cls, int.from_bytes(data, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.
|
|
28
|
+
return type.__call__(cls, int.from_bytes(data, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.__signed__))
|
|
27
29
|
|
|
28
30
|
@classmethod
|
|
29
31
|
def _read_array(
|
|
@@ -34,13 +36,18 @@ class Int(int, BaseType):
|
|
|
34
36
|
|
|
35
37
|
size = cls.__size__
|
|
36
38
|
length = size * count
|
|
37
|
-
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
data = stream.read(length)
|
|
42
|
+
except OverflowError:
|
|
43
|
+
raise EOFError(f"Read 0 bytes, but expected {length}") from None
|
|
44
|
+
|
|
38
45
|
if len(data) != length:
|
|
39
46
|
raise EOFError(f"Read {len(data) % size} bytes, but expected {size}")
|
|
40
47
|
|
|
41
48
|
byteorder = ENDIANNESS_TO_BYTEORDER_MAP[endian]
|
|
42
49
|
return [
|
|
43
|
-
type.__call__(cls, int.from_bytes(data[i : i + size], byteorder, signed=cls.
|
|
50
|
+
type.__call__(cls, int.from_bytes(data[i : i + size], byteorder, signed=cls.__signed__))
|
|
44
51
|
for i in range(0, length, size)
|
|
45
52
|
]
|
|
46
53
|
|
|
@@ -58,4 +65,14 @@ class Int(int, BaseType):
|
|
|
58
65
|
|
|
59
66
|
@classmethod
|
|
60
67
|
def _write(cls, stream: BinaryIO, data: int, *, endian: Endianness) -> int:
|
|
61
|
-
return stream.write(data.to_bytes(cls.__size__, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.
|
|
68
|
+
return stream.write(data.to_bytes(cls.__size__, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.__signed__))
|
|
69
|
+
|
|
70
|
+
@classproperty
|
|
71
|
+
def signed(cls) -> bool:
|
|
72
|
+
"""Access whether this integer type is signed."""
|
|
73
|
+
warnings.warn(
|
|
74
|
+
"The 'signed' property is deprecated, use '__signed__' instead.",
|
|
75
|
+
DeprecationWarning,
|
|
76
|
+
stacklevel=2,
|
|
77
|
+
)
|
|
78
|
+
return cls.__signed__
|
dissect/cstruct/types/leb128.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from typing import TYPE_CHECKING, Any, BinaryIO
|
|
4
5
|
|
|
6
|
+
from dissect.cstruct.exception import EOFError, WriteError
|
|
5
7
|
from dissect.cstruct.types.base import BaseType
|
|
8
|
+
from dissect.cstruct.util import classproperty
|
|
6
9
|
|
|
7
10
|
if TYPE_CHECKING:
|
|
8
11
|
from typing_extensions import Self
|
|
@@ -16,7 +19,7 @@ class LEB128(int, BaseType):
|
|
|
16
19
|
See https://en.wikipedia.org/wiki/LEB128 for more information and an explanation of the algorithm.
|
|
17
20
|
"""
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
__signed__: bool
|
|
20
23
|
|
|
21
24
|
@classmethod
|
|
22
25
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self:
|
|
@@ -33,7 +36,7 @@ class LEB128(int, BaseType):
|
|
|
33
36
|
if (b & 0x80) == 0:
|
|
34
37
|
break
|
|
35
38
|
|
|
36
|
-
if cls.
|
|
39
|
+
if cls.__signed__ and b & 0x40 != 0:
|
|
37
40
|
result |= ~0 << shift
|
|
38
41
|
|
|
39
42
|
return cls.__new__(cls, result)
|
|
@@ -53,8 +56,8 @@ class LEB128(int, BaseType):
|
|
|
53
56
|
@classmethod
|
|
54
57
|
def _write(cls, stream: BinaryIO, data: int, *, endian: Endianness) -> int:
|
|
55
58
|
# only write negative numbers when in signed mode
|
|
56
|
-
if data < 0 and not cls.
|
|
57
|
-
raise
|
|
59
|
+
if data < 0 and not cls.__signed__:
|
|
60
|
+
raise WriteError("Attempt to encode a negative integer using unsigned LEB128 encoding")
|
|
58
61
|
|
|
59
62
|
result = bytearray()
|
|
60
63
|
while True:
|
|
@@ -64,8 +67,8 @@ class LEB128(int, BaseType):
|
|
|
64
67
|
|
|
65
68
|
# function works similar for signed- and unsigned integers, except for the check when to stop
|
|
66
69
|
# the encoding process.
|
|
67
|
-
if ((cls.
|
|
68
|
-
not cls.
|
|
70
|
+
if ((cls.__signed__ and (data == 0 and byte & 0x40 == 0)) or (data == -1 and byte & 0x40 != 0)) or (
|
|
71
|
+
not cls.__signed__ and data == 0
|
|
69
72
|
):
|
|
70
73
|
result.append(byte)
|
|
71
74
|
break
|
|
@@ -75,3 +78,13 @@ class LEB128(int, BaseType):
|
|
|
75
78
|
|
|
76
79
|
stream.write(result)
|
|
77
80
|
return len(result)
|
|
81
|
+
|
|
82
|
+
@classproperty
|
|
83
|
+
def signed(cls) -> bool:
|
|
84
|
+
"""Access whether this integer type is signed."""
|
|
85
|
+
warnings.warn(
|
|
86
|
+
"The 'signed' property is deprecated, use '__signed__' instead.",
|
|
87
|
+
DeprecationWarning,
|
|
88
|
+
stacklevel=2,
|
|
89
|
+
)
|
|
90
|
+
return cls.__signed__
|
dissect/cstruct/types/packed.py
CHANGED
|
@@ -4,6 +4,7 @@ import warnings
|
|
|
4
4
|
from itertools import repeat
|
|
5
5
|
from typing import TYPE_CHECKING, Any, BinaryIO, Generic, TypeVar
|
|
6
6
|
|
|
7
|
+
from dissect.cstruct.exception import EOFError
|
|
7
8
|
from dissect.cstruct.types.base import EOF, BaseType, MetaType, _is_buffer_type
|
|
8
9
|
from dissect.cstruct.util import _struct, classproperty
|
|
9
10
|
|
|
@@ -48,6 +49,7 @@ class Packed(BaseType, Generic[T], metaclass=PackedMetaType):
|
|
|
48
49
|
"""Packed type for Python struct (un)packing."""
|
|
49
50
|
|
|
50
51
|
__fmt__: str
|
|
52
|
+
__signed__: bool
|
|
51
53
|
|
|
52
54
|
@classmethod
|
|
53
55
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self:
|
|
@@ -64,17 +66,21 @@ class Packed(BaseType, Generic[T], metaclass=PackedMetaType):
|
|
|
64
66
|
) -> list[Self]:
|
|
65
67
|
if count == EOF:
|
|
66
68
|
data = stream.read()
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
count, remainder = divmod(len(data), cls.__size__)
|
|
70
|
+
if remainder:
|
|
71
|
+
raise EOFError(f"Read {remainder} bytes, but expected {cls.__size__}")
|
|
69
72
|
else:
|
|
70
73
|
length = cls.__size__ * count
|
|
71
|
-
data = stream.read(length)
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
try:
|
|
76
|
+
data = stream.read(length)
|
|
77
|
+
except OverflowError:
|
|
78
|
+
raise EOFError(f"Read 0 bytes, but expected {length}") from None
|
|
74
79
|
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
if len(data) != length:
|
|
81
|
+
raise EOFError(f"Read {len(data)} bytes, but expected {length}")
|
|
77
82
|
|
|
83
|
+
fmt = _struct(endian, f"{count}{cls.__fmt__}")
|
|
78
84
|
return list(map(cls.__new__, repeat(cls), fmt.unpack(data)))
|
|
79
85
|
|
|
80
86
|
@classmethod
|
dissect/cstruct/types/pointer.py
CHANGED
|
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
from functools import cache
|
|
4
4
|
from typing import TYPE_CHECKING, Any, BinaryIO, Generic, TypeVar
|
|
5
5
|
|
|
6
|
-
from dissect.cstruct.exception import NullPointerDereference
|
|
6
|
+
from dissect.cstruct.exception import EOFError, NullPointerDereference
|
|
7
7
|
from dissect.cstruct.types.base import BaseType, normalize_endianness
|
|
8
8
|
from dissect.cstruct.types.char import Char
|
|
9
9
|
from dissect.cstruct.types.void import Void
|
|
@@ -109,7 +109,11 @@ class Pointer(int, BaseType, Generic[T]):
|
|
|
109
109
|
return None
|
|
110
110
|
|
|
111
111
|
position = self.__stream.tell()
|
|
112
|
-
|
|
112
|
+
|
|
113
|
+
try:
|
|
114
|
+
self.__stream.seek(self)
|
|
115
|
+
except OverflowError:
|
|
116
|
+
raise EOFError(f"Offset {int(self):#x} is beyond the end of the stream") from None
|
|
113
117
|
|
|
114
118
|
if issubclass(self.__type__, Char):
|
|
115
119
|
# This makes the assumption that a char pointer is a null-terminated string
|
|
@@ -16,6 +16,7 @@ from types import FunctionType
|
|
|
16
16
|
from typing import TYPE_CHECKING, Any, BinaryIO
|
|
17
17
|
|
|
18
18
|
from dissect.cstruct.bitbuffer import BitBuffer
|
|
19
|
+
from dissect.cstruct.exception import DefinitionError
|
|
19
20
|
from dissect.cstruct.expression import Expression
|
|
20
21
|
from dissect.cstruct.lexer import IDENTIFIER_TYPES
|
|
21
22
|
from dissect.cstruct.types.base import (
|
|
@@ -26,7 +27,10 @@ from dissect.cstruct.types.base import (
|
|
|
26
27
|
MetaType,
|
|
27
28
|
_is_buffer_type,
|
|
28
29
|
)
|
|
30
|
+
from dissect.cstruct.types.char import Char
|
|
29
31
|
from dissect.cstruct.types.enum import EnumMetaType
|
|
32
|
+
from dissect.cstruct.types.int import Int
|
|
33
|
+
from dissect.cstruct.types.packed import Packed
|
|
30
34
|
from dissect.cstruct.types.pointer import Pointer
|
|
31
35
|
from dissect.cstruct.types.void import Void
|
|
32
36
|
|
|
@@ -106,7 +110,7 @@ class StructureMetaType(MetaType):
|
|
|
106
110
|
static_sizes = {}
|
|
107
111
|
for field in fields:
|
|
108
112
|
if field._name in lookup and field._name != "_":
|
|
109
|
-
raise
|
|
113
|
+
raise DefinitionError(f"Duplicate field name: {field._name}")
|
|
110
114
|
|
|
111
115
|
if not field.type.__dynamic__:
|
|
112
116
|
static_sizes[field._name] = field.type.__size__
|
|
@@ -189,6 +193,11 @@ class StructureMetaType(MetaType):
|
|
|
189
193
|
if isinstance(field_type, EnumMetaType):
|
|
190
194
|
field_type = field_type.__type__
|
|
191
195
|
|
|
196
|
+
if not issubclass(field_type, (Int, Char)) and not (
|
|
197
|
+
issubclass(field_type, Packed) and issubclass(field_type, int)
|
|
198
|
+
):
|
|
199
|
+
raise DefinitionError(f"Bit fields of type {field.type.__name__} are unsupported")
|
|
200
|
+
|
|
192
201
|
# Bit fields have special logic
|
|
193
202
|
if (
|
|
194
203
|
# Exhausted a bit field
|
|
@@ -213,7 +222,7 @@ class StructureMetaType(MetaType):
|
|
|
213
222
|
bits_remaining -= field.bits
|
|
214
223
|
|
|
215
224
|
if bits_remaining < 0:
|
|
216
|
-
raise
|
|
225
|
+
raise DefinitionError("Straddled bit fields are unsupported")
|
|
217
226
|
else:
|
|
218
227
|
# Reset bits stuff
|
|
219
228
|
bits_type = None
|
dissect/cstruct/types/wchar.py
CHANGED
|
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import sys
|
|
4
4
|
from typing import TYPE_CHECKING, Any, BinaryIO, ClassVar
|
|
5
5
|
|
|
6
|
-
from dissect.cstruct.exception import
|
|
6
|
+
from dissect.cstruct.exception import EOFError, WriteError
|
|
7
7
|
from dissect.cstruct.types.base import EOF, BaseArray, BaseType, _is_seekable, _read_null_terminated
|
|
8
8
|
|
|
9
9
|
if TYPE_CHECKING:
|
|
@@ -33,7 +33,7 @@ class WcharArray(str, BaseArray):
|
|
|
33
33
|
# A surrogate pair is a single character that takes up two entries of the array
|
|
34
34
|
if not cls.__dynamic__ and (remaining := cls.__num_entries__ - (actual_size := len(buf) // 2)):
|
|
35
35
|
if remaining < 0:
|
|
36
|
-
raise
|
|
36
|
+
raise WriteError(f"Expected static array size {cls.__num_entries__}, got {actual_size} instead.")
|
|
37
37
|
buf += b"\x00\x00" * remaining
|
|
38
38
|
|
|
39
39
|
return stream.write(buf)
|
|
@@ -69,12 +69,20 @@ class Wchar(str, BaseType):
|
|
|
69
69
|
if count == 0:
|
|
70
70
|
return type.__call__(cls, "")
|
|
71
71
|
|
|
72
|
-
if count
|
|
73
|
-
|
|
72
|
+
if count == EOF:
|
|
73
|
+
data = stream.read()
|
|
74
|
+
if len(data) % 2:
|
|
75
|
+
raise EOFError("Read 1 bytes, but expected 2")
|
|
76
|
+
else:
|
|
77
|
+
length = count * 2
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
try:
|
|
80
|
+
data = stream.read(length)
|
|
81
|
+
except OverflowError:
|
|
82
|
+
raise EOFError(f"Read 0 bytes, but expected {length}") from None
|
|
83
|
+
|
|
84
|
+
if len(data) != length:
|
|
85
|
+
raise EOFError(f"Read {len(data)} bytes, but expected {length}")
|
|
78
86
|
|
|
79
87
|
return type.__call__(cls, str(data, cls.__encoding_map__[endian], "surrogatepass"))
|
|
80
88
|
|
|
@@ -103,6 +111,6 @@ class Wchar(str, BaseType):
|
|
|
103
111
|
|
|
104
112
|
# A surrogate pair is a single character that doesn't fit in a single wchar
|
|
105
113
|
if (actual_size := len(buf) // 2) > 1:
|
|
106
|
-
raise
|
|
114
|
+
raise WriteError(f"Expected a single character, got {actual_size} instead.")
|
|
107
115
|
|
|
108
116
|
return stream.write(buf or b"\x00\x00")
|
dissect/cstruct/util.py
CHANGED
|
@@ -279,9 +279,26 @@ def sizeof(type_: type[BaseType] | BaseType) -> int:
|
|
|
279
279
|
|
|
280
280
|
|
|
281
281
|
def offsetof(type_: type[Structure], name: str) -> int:
|
|
282
|
-
"""Get the offset of a field in a structure.
|
|
283
|
-
|
|
282
|
+
"""Get the offset of a field in a structure or union, like ``offsetof`` in C.
|
|
283
|
+
|
|
284
|
+
Like in C, the fields of an anonymous nested structure or union are fields of the type that contains it.
|
|
285
|
+
"""
|
|
286
|
+
from dissect.cstruct.types.structure import Structure, Union
|
|
287
|
+
|
|
288
|
+
if not issubclass(type_, Structure):
|
|
289
|
+
raise TypeError(f"Type '{type_.__name__}' is not a structure")
|
|
290
|
+
if (member := type_.__members__.get(name)) is None:
|
|
284
291
|
raise ValueError(f"Structure '{type_.__name__}' does not have a field named '{name}'")
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
292
|
+
|
|
293
|
+
field = next(
|
|
294
|
+
field
|
|
295
|
+
for field in type_.__fields__
|
|
296
|
+
if field is member or (field.name is None and field.type.__members__.get(name) is member)
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
# The fields of a union start at the start of the union, unless they have an offset of their own
|
|
300
|
+
offset = 0 if field.offset is None and issubclass(type_, Union) else field.offset
|
|
301
|
+
if offset is None:
|
|
302
|
+
raise ValueError(f"Field '{name}' of structure '{type_.__name__}' does not have a known offset")
|
|
303
|
+
|
|
304
|
+
return offset if field is member else offset + offsetof(field.type, name)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.cstruct
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.dev22
|
|
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
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
dissect/cstruct/__init__.py,sha256=6WCBCk9iTJiKjzfkYCZAwWkIh3FnxEuCe_y2TSUJ2bU,1905
|
|
2
|
+
dissect/cstruct/bitbuffer.py,sha256=FpvyoDuio0hLfUtyqb2OUg1SpDVnblxMVNtAbYLPzVk,2781
|
|
3
|
+
dissect/cstruct/compiler.py,sha256=6mK2CdUnfWdQIu0_1qIL1NCaaqlv5eJcvlamMsGfEAg,54781
|
|
4
|
+
dissect/cstruct/cstruct.py,sha256=_oA-nYhaanETPtSyGUpIKKoZ6Rw1PYNmnzLhoQtq2lE,26639
|
|
5
|
+
dissect/cstruct/declarative.py,sha256=GPU208IsslegjFug9udduu-6fhmV2CIfIf6oY76ycEw,14571
|
|
6
|
+
dissect/cstruct/dump.py,sha256=W5ISbaZ-1YmWXKzh-x6pDt5_SlpFRDhD9GGNyv1MnLw,19016
|
|
7
|
+
dissect/cstruct/exception.py,sha256=8b_nLXacWGdQ3BhZN-DOOEr4gp5Vacni6etwDKqYHis,988
|
|
8
|
+
dissect/cstruct/expression.py,sha256=HMDKuZmPElZF88dC6FdEpUO_yUTCSw-WU7wI_XsgYEY,13592
|
|
9
|
+
dissect/cstruct/lexer.py,sha256=INbr9yEVua43VvPmmnWUy392jLgTeLv2-k2M22Pe1zE,20838
|
|
10
|
+
dissect/cstruct/parser.py,sha256=9e1R027YqZw3mhAEORdEzdAUWcifNR3youChwJ_momU,24610
|
|
11
|
+
dissect/cstruct/util.py,sha256=-qBoVSK_uwJ_tFt7bELS-EtipFQ6ebkRZBHAD3iYy5Q,9099
|
|
12
|
+
dissect/cstruct/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
dissect/cstruct/tools/stubgen.py,sha256=MeZkLmSeNSLNcAEwlAdIGv1lKvlvK3UQ4WN85ovsxTM,7672
|
|
14
|
+
dissect/cstruct/types/__init__.py,sha256=6P71Pu94Dv7RNjZpDZ19pqzx-G0QzmQw4q0-3zWALso,911
|
|
15
|
+
dissect/cstruct/types/base.py,sha256=tMwLBArs-lsrjr-gyGP4oRKivhF9PFZER8mC92WsFAc,19292
|
|
16
|
+
dissect/cstruct/types/char.py,sha256=CA9-xVA2UbwV3CnfjYOrCIl-Y41QjhJVU4VFn2ZXe3s,3543
|
|
17
|
+
dissect/cstruct/types/enum.py,sha256=H67ACZM3svwySAW4IgstcnqeuGCzmuKIL2IbQV3RgSk,9089
|
|
18
|
+
dissect/cstruct/types/flag.py,sha256=YxQHix8ZhK1qQX3J_Og7GJp_4JAxCwzUbUibuJeDaWQ,3479
|
|
19
|
+
dissect/cstruct/types/int.py,sha256=DBTpQjF6R2zEy0ItXPfO5OHTUwaJqEMTOu7kxuzMGho,2615
|
|
20
|
+
dissect/cstruct/types/leb128.py,sha256=WJ7FQXRsmsdDZJWm2lOe3SEkEJqXfQTIFYpmOajXv0s,2835
|
|
21
|
+
dissect/cstruct/types/packed.py,sha256=sLMrUjRLzlTR4FzRLsPnnAJANhunhUBzbvcMRnGe94A,4181
|
|
22
|
+
dissect/cstruct/types/pointer.py,sha256=pIxiDGosRu1jhchdcA3Zm7yIXkPUh2UKnIt8kxUygf0,4138
|
|
23
|
+
dissect/cstruct/types/structure.py,sha256=T28ZncDWG2QtkQcpKNwfpNmk5_77IgW0idDxHINzcFY,43638
|
|
24
|
+
dissect/cstruct/types/void.py,sha256=fV4hKk50uop_EKbR1nfy1cdtAzvUYrSymXsdakCyFk8,1222
|
|
25
|
+
dissect/cstruct/types/wchar.py,sha256=eJyOWdT8Y3k8gM9QNE0NeXBwcAGRcp4aUkIBezAYe0c,4225
|
|
26
|
+
dissect_cstruct-5.0.dev22.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
|
|
27
|
+
dissect_cstruct-5.0.dev22.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
|
|
28
|
+
dissect_cstruct-5.0.dev22.dist-info/METADATA,sha256=G5aoe6I38FHK1j0dk2BTVEb1PIzzX75bRRNmswbY4AE,12661
|
|
29
|
+
dissect_cstruct-5.0.dev22.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
30
|
+
dissect_cstruct-5.0.dev22.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
|
|
31
|
+
dissect_cstruct-5.0.dev22.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
|
|
32
|
+
dissect_cstruct-5.0.dev22.dist-info/RECORD,,
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
dissect/cstruct/__init__.py,sha256=ti1VYFx1MtZuDcMks14haNvUw0IZYjRo346ChzhqPls,1647
|
|
2
|
-
dissect/cstruct/bitbuffer.py,sha256=aNzY4lkWe8ffAc8j7FODXzwZc8gZwbey77UosyoYCsw,2711
|
|
3
|
-
dissect/cstruct/compiler.py,sha256=3dlVVzU8-i-G5PKti8sMTKLF3BdQ9g6_tHaKn0KDqJA,54440
|
|
4
|
-
dissect/cstruct/cstruct.py,sha256=mJerzPouWV29RdK5LqjGitDDMvNKcadoT2hj1zpExnM,26551
|
|
5
|
-
dissect/cstruct/declarative.py,sha256=5ubPtOyy6L5gJMFaPcbW3xYbTijZGMp0XJGxBeed1nE,14505
|
|
6
|
-
dissect/cstruct/dump.py,sha256=W5ISbaZ-1YmWXKzh-x6pDt5_SlpFRDhD9GGNyv1MnLw,19016
|
|
7
|
-
dissect/cstruct/exception.py,sha256=box-mw6FoP6c3vndvYLcTeJCZVgIG8IQVPEAg4zW0C8,380
|
|
8
|
-
dissect/cstruct/expression.py,sha256=SbZRv7ueasYU4k-4k2eV1fjwL3JTAgF2UBaBvNjwiv4,10736
|
|
9
|
-
dissect/cstruct/lexer.py,sha256=N7eqCOlcaQriUUIswJv16i-CE_sahYXGO-YdFO0IKKY,20421
|
|
10
|
-
dissect/cstruct/parser.py,sha256=6PovhIbicENg3jBgopdArupmQNd-Ti7rd_te9au4z6U,22569
|
|
11
|
-
dissect/cstruct/util.py,sha256=wdHZL8HhjG8LnATAxpQd50PnUJDM6XBJ9r8WlDzX830,8373
|
|
12
|
-
dissect/cstruct/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
-
dissect/cstruct/tools/stubgen.py,sha256=MeZkLmSeNSLNcAEwlAdIGv1lKvlvK3UQ4WN85ovsxTM,7672
|
|
14
|
-
dissect/cstruct/types/__init__.py,sha256=6P71Pu94Dv7RNjZpDZ19pqzx-G0QzmQw4q0-3zWALso,911
|
|
15
|
-
dissect/cstruct/types/base.py,sha256=NdCP9dJk6lrKPERYjR9qeD2gFzWd6krVSN4tAOqqkvI,18357
|
|
16
|
-
dissect/cstruct/types/char.py,sha256=N19R7Zzn2uMuG-ULW5X2UgmgkLvjaBj1Td7AQQI93Fg,3417
|
|
17
|
-
dissect/cstruct/types/enum.py,sha256=VICmKWRuzTicq6pNKpAG_xfFzC5pVLp48qe9lFyXqoc,9029
|
|
18
|
-
dissect/cstruct/types/flag.py,sha256=YxQHix8ZhK1qQX3J_Og7GJp_4JAxCwzUbUibuJeDaWQ,3479
|
|
19
|
-
dissect/cstruct/types/int.py,sha256=9RbdrkvAI28zATM97VVORQnras0XhLaVMtdPjJwpTH4,2089
|
|
20
|
-
dissect/cstruct/types/leb128.py,sha256=V97XTJT_Xbw1LpTwTN1OkjPHuQQAQUxd8OGuA6yEDmE,2387
|
|
21
|
-
dissect/cstruct/types/packed.py,sha256=NgCX0YWX8SH8z47atq0f39w9MBjU_eBFJtUisUWdO-M,3867
|
|
22
|
-
dissect/cstruct/types/pointer.py,sha256=FNuB9bfENWRebbKX-R6Vs6xWPMHepjvOhhomeUhr7I8,3985
|
|
23
|
-
dissect/cstruct/types/structure.py,sha256=cwLzetR0AEDBVqOIeOxo6viWxbTRvt5tYDkbZEISk_U,43165
|
|
24
|
-
dissect/cstruct/types/void.py,sha256=fV4hKk50uop_EKbR1nfy1cdtAzvUYrSymXsdakCyFk8,1222
|
|
25
|
-
dissect/cstruct/types/wchar.py,sha256=VrnPXsP_lsyA-1xr4D2aSIVijumMhUuZWNlDHcn12-s,3964
|
|
26
|
-
dissect_cstruct-5.0.dev20.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
|
|
27
|
-
dissect_cstruct-5.0.dev20.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
|
|
28
|
-
dissect_cstruct-5.0.dev20.dist-info/METADATA,sha256=PTh5qmB9Hrn-h2yQbC1r_V3eZjE_iit0oL9mKRErugo,12661
|
|
29
|
-
dissect_cstruct-5.0.dev20.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
30
|
-
dissect_cstruct-5.0.dev20.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
|
|
31
|
-
dissect_cstruct-5.0.dev20.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
|
|
32
|
-
dissect_cstruct-5.0.dev20.dist-info/RECORD,,
|
|
File without changes
|
{dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/licenses/COPYRIGHT
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev20.dist-info → dissect_cstruct-5.0.dev22.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|