dissect.cstruct 5.0.dev19__tar.gz → 5.0.dev21__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.dev19 → dissect_cstruct-5.0.dev21}/CHANGELOG.md +3 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/PKG-INFO +1 -1
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/compiler.py +6 -9
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/cstruct.py +4 -4
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/dump.py +1 -1
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/enum.py +7 -9
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/int.py +16 -5
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/leb128.py +17 -5
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/packed.py +1 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/structure.py +2 -1
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/PKG-INFO +1 -1
- dissect_cstruct-5.0.dev21/dissect.cstruct.egg-info/scm_version.json +8 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_basic.py +6 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_bitfield.py +3 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_compiler.py +7 -6
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_dump.py +15 -2
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_enum.py +9 -1
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_packed.py +15 -0
- dissect_cstruct-5.0.dev19/dissect.cstruct.egg-info/scm_version.json +0 -8
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/.git-blame-ignore-revs +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/.gitattributes +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/COPYRIGHT +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/LICENSE +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/MANIFEST.in +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/README.md +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/__init__.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/bitbuffer.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/declarative.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/exception.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/expression.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/lexer.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/parser.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/tools/__init__.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/tools/stubgen.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/__init__.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/base.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/char.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/flag.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/pointer.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/void.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/types/wchar.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect/cstruct/util.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/SOURCES.txt +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/entry_points.txt +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/requires.txt +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/scm_file_list.json +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/top_level.txt +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/examples/disk.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/examples/mirai.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/examples/pe.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/examples/protobuf.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/examples/secdesc.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/pyproject.toml +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/setup.cfg +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/__init__.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/_data/testdef.txt +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/_docs/Makefile +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/_docs/__init__.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/_docs/conf.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/_docs/index.rst +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/conftest.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_align.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_annotations.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_benchmark.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_bitbuffer.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_ctypes.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_declarative.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_expression.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_lexer.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_parser.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_tools_stubgen.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_base.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_char.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_custom.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_flag.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_int.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_leb128.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_pointer.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_structure.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_union.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_void.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_types_wchar.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/test_util.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tests/util.py +0 -0
- {dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/tox.ini +0 -0
|
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
16
16
|
- Optimize storage of field sizes.
|
|
17
17
|
- Rename `_sizes` property of `Structure` to `__sizes__`.
|
|
18
18
|
- Rename `_values` property of `Structure` to `__values__`.
|
|
19
|
+
- Rename the `signed` attribute of integer and LEB128 types to `__signed__`, like the other attributes of types. The `signed` property is deprecated.
|
|
19
20
|
- Added `load` argument to `cstruct` class, allowing direct initialization with a definition (i.e. `cstruct(cdef)` instead of `cstruct().load(cdef)`. Other arguments to `cstruct` are now keyword only.
|
|
20
21
|
- Replace `compiler.compile` and the `Compiler` class with `compiler.compile_structure`.
|
|
21
22
|
- Improve parsing performance:
|
|
@@ -43,6 +44,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
43
44
|
- A surrogate pair takes up two entries of a `wchar` array, so writing it no longer writes more bytes than the size of the array.
|
|
44
45
|
- A `wchar` string can hold a lone surrogate, instead of raising an error for the whole structure when reading or writing it. Bytes are written back as they were stored.
|
|
45
46
|
- Writing more than a single character to a `char` or `wchar` raises a `ValueError`, instead of writing all of it and moving the fields after it. An empty value is padded.
|
|
47
|
+
- A plain integer can be written to an enum or flag field, including a bit field.
|
|
48
|
+
- `dumpstruct` shows the fields of a structure in a union in a union, instead of only its `repr`.
|
|
46
49
|
|
|
47
50
|
## [4.5] - 20-05-2025
|
|
48
51
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.cstruct
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.dev21
|
|
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
|
|
@@ -722,7 +722,7 @@ class _ReadSourceGenerator(_SourceGenerator):
|
|
|
722
722
|
if issubclass(read_type, Int):
|
|
723
723
|
# Everything that's read as an integer type is sliced directly out of the buffer
|
|
724
724
|
elem_size = read_type.__size__
|
|
725
|
-
signed = bool(read_type.
|
|
725
|
+
signed = bool(read_type.__signed__)
|
|
726
726
|
lines.append(f"_array_buf = {getter}")
|
|
727
727
|
raw = f"_int_from_bytes(_array_buf[i:i + {elem_size}], _byteorder[endian], signed={signed})"
|
|
728
728
|
getter = f"[{raw} for i in range(0, {count}, {elem_size})]"
|
|
@@ -753,7 +753,7 @@ class _ReadSourceGenerator(_SourceGenerator):
|
|
|
753
753
|
elif issubclass(field_type, (Enum, Flag)):
|
|
754
754
|
# Fast path the member lookup, the slow path takes care of unknown values
|
|
755
755
|
if issubclass(read_type, Int):
|
|
756
|
-
raw = f"_int_from_bytes({getter}, _byteorder[endian], signed={bool(read_type.
|
|
756
|
+
raw = f"_int_from_bytes({getter}, _byteorder[endian], signed={bool(read_type.__signed__)})"
|
|
757
757
|
else:
|
|
758
758
|
raw = getter
|
|
759
759
|
|
|
@@ -762,7 +762,7 @@ class _ReadSourceGenerator(_SourceGenerator):
|
|
|
762
762
|
lines.extend(target(field, "_member"))
|
|
763
763
|
|
|
764
764
|
elif issubclass(read_type, Int):
|
|
765
|
-
signed = bool(read_type.
|
|
765
|
+
signed = bool(read_type.__signed__)
|
|
766
766
|
value = f"_int_from_bytes({getter}, _byteorder[endian], signed={signed})"
|
|
767
767
|
lines.extend(target(field, f"_type_call({self._type_symbol(field.type)}, {value})"))
|
|
768
768
|
|
|
@@ -948,9 +948,8 @@ class _WriteSourceGenerator(_SourceGenerator):
|
|
|
948
948
|
other = []
|
|
949
949
|
remaining = type_.__size__ * 8
|
|
950
950
|
for field in fields:
|
|
951
|
+
# Enum members and plain integers are shifted the same way
|
|
951
952
|
value = self._load_value(field, _get_attribute("data", field._name), pack)
|
|
952
|
-
if issubclass(field.type, (Enum, Flag)):
|
|
953
|
-
value += "._value_"
|
|
954
953
|
|
|
955
954
|
remaining -= field.bits
|
|
956
955
|
little.append(f"{value} << {type_.__size__ * 8 - remaining - field.bits}")
|
|
@@ -1069,9 +1068,7 @@ class _WriteSourceGenerator(_SourceGenerator):
|
|
|
1069
1068
|
|
|
1070
1069
|
pack.add(count, "s", var)
|
|
1071
1070
|
|
|
1072
|
-
|
|
1073
|
-
self._pack_number(read_type, f"{self._load_value(field, value, pack)}._value_", pack)
|
|
1074
|
-
|
|
1071
|
+
# Enum members and plain integers are packed the same way, as the integer type of the enum
|
|
1075
1072
|
elif issubclass(read_type, Packed):
|
|
1076
1073
|
pack.add(1, read_type.__fmt__, value, f"_or_default({value}, {type_})")
|
|
1077
1074
|
|
|
@@ -1211,7 +1208,7 @@ def _has_default_write(type_: type[BaseType], base: type[BaseType]) -> bool:
|
|
|
1211
1208
|
|
|
1212
1209
|
def _int_to_bytes(type_: type[Int], value: str) -> str:
|
|
1213
1210
|
"""Return the code to convert a value of an integer type to bytes."""
|
|
1214
|
-
return f"{value}.to_bytes({type_.__size__}, _byteorder[endian], signed={bool(type_.
|
|
1211
|
+
return f"{value}.to_bytes({type_.__size__}, _byteorder[endian], signed={bool(type_.__signed__)})"
|
|
1215
1212
|
|
|
1216
1213
|
|
|
1217
1214
|
def _get_attribute(var: str, name: str) -> str:
|
|
@@ -90,8 +90,8 @@ class cstruct:
|
|
|
90
90
|
"int128": self._make_int_type("int128", 16, True, alignment=16),
|
|
91
91
|
"uint128": self._make_int_type("uint128", 16, False, alignment=16),
|
|
92
92
|
|
|
93
|
-
"uleb128": self._make_type("uleb128", (LEB128,), None, attrs={"
|
|
94
|
-
"ileb128": self._make_type("ileb128", (LEB128,), None, attrs={"
|
|
93
|
+
"uleb128": self._make_type("uleb128", (LEB128,), None, attrs={"__signed__": False}),
|
|
94
|
+
"ileb128": self._make_type("ileb128", (LEB128,), None, attrs={"__signed__": True}),
|
|
95
95
|
|
|
96
96
|
"void": self._make_type("void", (Void,), 0),
|
|
97
97
|
|
|
@@ -552,7 +552,7 @@ class cstruct:
|
|
|
552
552
|
return cast("type[Array]", self._make_type(name, bases, size, alignment=type_.__alignment__, attrs=attrs))
|
|
553
553
|
|
|
554
554
|
def _make_int_type(self, name: str, size: int, signed: bool, *, alignment: int | None = None) -> type[Int]:
|
|
555
|
-
return cast("type[Int]", self._make_type(name, (Int,), size, alignment=alignment, attrs={"
|
|
555
|
+
return cast("type[Int]", self._make_type(name, (Int,), size, alignment=alignment, attrs={"__signed__": signed}))
|
|
556
556
|
|
|
557
557
|
def _make_packed_type(self, name: str, fmt: str, base: type, *, alignment: int | None = None) -> type[Packed]:
|
|
558
558
|
return cast(
|
|
@@ -562,7 +562,7 @@ class cstruct:
|
|
|
562
562
|
(base, Packed),
|
|
563
563
|
struct.calcsize(fmt),
|
|
564
564
|
alignment=alignment,
|
|
565
|
-
attrs={"__fmt__": fmt},
|
|
565
|
+
attrs={"__fmt__": fmt, "__signed__": fmt.islower()},
|
|
566
566
|
),
|
|
567
567
|
)
|
|
568
568
|
|
|
@@ -318,7 +318,7 @@ def _describe_value(
|
|
|
318
318
|
Structures and unions, and arrays of them, get their fields or elements as child nodes. Other values, including
|
|
319
319
|
arrays of other types, are formatted as a human-readable ``display``.
|
|
320
320
|
"""
|
|
321
|
-
|
|
321
|
+
while isinstance(value, UnionProxy):
|
|
322
322
|
# Nested Structure members inside a Union are proxied; unwrap to the real Structure
|
|
323
323
|
value = value.__target__
|
|
324
324
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import sys
|
|
4
|
-
from enum import Enum as _Enum
|
|
5
4
|
from enum import EnumMeta, IntEnum, IntFlag
|
|
6
5
|
from typing import TYPE_CHECKING, Any, BinaryIO, TypeVar, overload
|
|
7
6
|
|
|
@@ -100,16 +99,15 @@ class EnumMetaType(EnumMeta, MetaType):
|
|
|
100
99
|
def _read_0(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> list[Self]:
|
|
101
100
|
return list(map(cls, cls.__type__._read_0(stream, context=context, endian=endian)))
|
|
102
101
|
|
|
103
|
-
def _write(cls, stream: BinaryIO, data: Enum, *, endian: Endianness) -> int:
|
|
104
|
-
|
|
102
|
+
def _write(cls, stream: BinaryIO, data: Enum | int, *, endian: Endianness) -> int:
|
|
103
|
+
# Enum members are integers, so the integer type writes them like plain integers
|
|
104
|
+
return cls.__type__._write(stream, data, endian=endian)
|
|
105
105
|
|
|
106
|
-
def _write_array(cls, stream: BinaryIO, array: list[
|
|
107
|
-
|
|
108
|
-
return cls.__type__._write_array(stream, data, endian=endian)
|
|
106
|
+
def _write_array(cls, stream: BinaryIO, array: list[Enum | int], *, endian: Endianness) -> int:
|
|
107
|
+
return cls.__type__._write_array(stream, array, endian=endian)
|
|
109
108
|
|
|
110
|
-
def _write_0(cls, stream: BinaryIO, array: list[
|
|
111
|
-
|
|
112
|
-
return cls._write_array(stream, [*data, cls.__type__.__default__()], endian=endian)
|
|
109
|
+
def _write_0(cls, stream: BinaryIO, array: list[Enum | int], *, endian: Endianness) -> int:
|
|
110
|
+
return cls.__type__._write_0(stream, array, endian=endian)
|
|
113
111
|
|
|
114
112
|
def cdef(cls) -> str:
|
|
115
113
|
"""Render this enum (or flag) back to its C-style definition.
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from typing import TYPE_CHECKING, Any, BinaryIO
|
|
4
5
|
|
|
5
6
|
from dissect.cstruct.types.base import EOF, BaseType, MetaType
|
|
6
|
-
from dissect.cstruct.util import ENDIANNESS_TO_BYTEORDER_MAP
|
|
7
|
+
from dissect.cstruct.util import ENDIANNESS_TO_BYTEORDER_MAP, classproperty
|
|
7
8
|
|
|
8
9
|
if TYPE_CHECKING:
|
|
9
10
|
from typing_extensions import Self
|
|
@@ -14,7 +15,7 @@ if TYPE_CHECKING:
|
|
|
14
15
|
class Int(int, BaseType):
|
|
15
16
|
"""Integer type that can span an arbitrary amount of bytes."""
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
__signed__: bool
|
|
18
19
|
|
|
19
20
|
@classmethod
|
|
20
21
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self:
|
|
@@ -23,7 +24,7 @@ class Int(int, BaseType):
|
|
|
23
24
|
if len(data) != cls.__size__:
|
|
24
25
|
raise EOFError(f"Read {len(data)} bytes, but expected {cls.__size__}")
|
|
25
26
|
|
|
26
|
-
return type.__call__(cls, int.from_bytes(data, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.
|
|
27
|
+
return type.__call__(cls, int.from_bytes(data, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.__signed__))
|
|
27
28
|
|
|
28
29
|
@classmethod
|
|
29
30
|
def _read_array(
|
|
@@ -40,7 +41,7 @@ class Int(int, BaseType):
|
|
|
40
41
|
|
|
41
42
|
byteorder = ENDIANNESS_TO_BYTEORDER_MAP[endian]
|
|
42
43
|
return [
|
|
43
|
-
type.__call__(cls, int.from_bytes(data[i : i + size], byteorder, signed=cls.
|
|
44
|
+
type.__call__(cls, int.from_bytes(data[i : i + size], byteorder, signed=cls.__signed__))
|
|
44
45
|
for i in range(0, length, size)
|
|
45
46
|
]
|
|
46
47
|
|
|
@@ -58,4 +59,14 @@ class Int(int, BaseType):
|
|
|
58
59
|
|
|
59
60
|
@classmethod
|
|
60
61
|
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.
|
|
62
|
+
return stream.write(data.to_bytes(cls.__size__, ENDIANNESS_TO_BYTEORDER_MAP[endian], signed=cls.__signed__))
|
|
63
|
+
|
|
64
|
+
@classproperty
|
|
65
|
+
def signed(cls) -> bool:
|
|
66
|
+
"""Access whether this integer type is signed."""
|
|
67
|
+
warnings.warn(
|
|
68
|
+
"The 'signed' property is deprecated, use '__signed__' instead.",
|
|
69
|
+
DeprecationWarning,
|
|
70
|
+
stacklevel=2,
|
|
71
|
+
)
|
|
72
|
+
return cls.__signed__
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from typing import TYPE_CHECKING, Any, BinaryIO
|
|
4
5
|
|
|
5
6
|
from dissect.cstruct.types.base import BaseType
|
|
7
|
+
from dissect.cstruct.util import classproperty
|
|
6
8
|
|
|
7
9
|
if TYPE_CHECKING:
|
|
8
10
|
from typing_extensions import Self
|
|
@@ -16,7 +18,7 @@ class LEB128(int, BaseType):
|
|
|
16
18
|
See https://en.wikipedia.org/wiki/LEB128 for more information and an explanation of the algorithm.
|
|
17
19
|
"""
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
__signed__: bool
|
|
20
22
|
|
|
21
23
|
@classmethod
|
|
22
24
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self:
|
|
@@ -33,7 +35,7 @@ class LEB128(int, BaseType):
|
|
|
33
35
|
if (b & 0x80) == 0:
|
|
34
36
|
break
|
|
35
37
|
|
|
36
|
-
if cls.
|
|
38
|
+
if cls.__signed__ and b & 0x40 != 0:
|
|
37
39
|
result |= ~0 << shift
|
|
38
40
|
|
|
39
41
|
return cls.__new__(cls, result)
|
|
@@ -53,7 +55,7 @@ class LEB128(int, BaseType):
|
|
|
53
55
|
@classmethod
|
|
54
56
|
def _write(cls, stream: BinaryIO, data: int, *, endian: Endianness) -> int:
|
|
55
57
|
# only write negative numbers when in signed mode
|
|
56
|
-
if data < 0 and not cls.
|
|
58
|
+
if data < 0 and not cls.__signed__:
|
|
57
59
|
raise ValueError("Attempt to encode a negative integer using unsigned LEB128 encoding")
|
|
58
60
|
|
|
59
61
|
result = bytearray()
|
|
@@ -64,8 +66,8 @@ class LEB128(int, BaseType):
|
|
|
64
66
|
|
|
65
67
|
# function works similar for signed- and unsigned integers, except for the check when to stop
|
|
66
68
|
# the encoding process.
|
|
67
|
-
if ((cls.
|
|
68
|
-
not cls.
|
|
69
|
+
if ((cls.__signed__ and (data == 0 and byte & 0x40 == 0)) or (data == -1 and byte & 0x40 != 0)) or (
|
|
70
|
+
not cls.__signed__ and data == 0
|
|
69
71
|
):
|
|
70
72
|
result.append(byte)
|
|
71
73
|
break
|
|
@@ -75,3 +77,13 @@ class LEB128(int, BaseType):
|
|
|
75
77
|
|
|
76
78
|
stream.write(result)
|
|
77
79
|
return len(result)
|
|
80
|
+
|
|
81
|
+
@classproperty
|
|
82
|
+
def signed(cls) -> bool:
|
|
83
|
+
"""Access whether this integer type is signed."""
|
|
84
|
+
warnings.warn(
|
|
85
|
+
"The 'signed' property is deprecated, use '__signed__' instead.",
|
|
86
|
+
DeprecationWarning,
|
|
87
|
+
stacklevel=2,
|
|
88
|
+
)
|
|
89
|
+
return cls.__signed__
|
|
@@ -48,6 +48,7 @@ class Packed(BaseType, Generic[T], metaclass=PackedMetaType):
|
|
|
48
48
|
"""Packed type for Python struct (un)packing."""
|
|
49
49
|
|
|
50
50
|
__fmt__: str
|
|
51
|
+
__signed__: bool
|
|
51
52
|
|
|
52
53
|
@classmethod
|
|
53
54
|
def _read(cls, stream: BinaryIO, *, context: dict[str, Any] | None = None, endian: Endianness) -> Self:
|
|
@@ -342,7 +342,8 @@ class StructureMetaType(MetaType):
|
|
|
342
342
|
bit_buffer = BitBuffer(stream, endian=endian)
|
|
343
343
|
|
|
344
344
|
if isinstance(field_type, EnumMetaType):
|
|
345
|
-
|
|
345
|
+
# Enum members and plain integers are shifted into the buffer the same way
|
|
346
|
+
bit_buffer.write(field_type.__type__, value, field.bits)
|
|
346
347
|
else:
|
|
347
348
|
bit_buffer.write(field_type, value, field.bits)
|
|
348
349
|
else:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.cstruct
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.dev21
|
|
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
|
|
@@ -771,5 +771,11 @@ def test_deprecation_warnings(cs: cstruct) -> None:
|
|
|
771
771
|
):
|
|
772
772
|
_ = cs.uint8.packchar
|
|
773
773
|
|
|
774
|
+
for type_ in (cs.int24, cs.uleb128):
|
|
775
|
+
with pytest.warns(
|
|
776
|
+
DeprecationWarning, match=re.escape("The 'signed' property is deprecated, use '__signed__' instead.")
|
|
777
|
+
):
|
|
778
|
+
_ = type_.signed
|
|
779
|
+
|
|
774
780
|
# No test for the pointer type since it also conflicts with a bug fix
|
|
775
781
|
# (accessing a field named 'type' on a pointer type)
|
|
@@ -243,6 +243,9 @@ def test_bitfield_with_enum_or_flag(cs: cstruct, compiled: bool) -> None:
|
|
|
243
243
|
|
|
244
244
|
assert obj.dumps() == buf
|
|
245
245
|
|
|
246
|
+
# Plain integers are written like flag members
|
|
247
|
+
assert cs.test(a=1, b=1, c=3, d=3).dumps() == buf
|
|
248
|
+
|
|
246
249
|
|
|
247
250
|
def test_bitfield_char(cs: cstruct, compiled: bool) -> None:
|
|
248
251
|
cdef = """
|
|
@@ -749,12 +749,11 @@ def test_generate_packed_write_numbers(cs: cstruct, TestEnum: type[Enum]) -> Non
|
|
|
749
749
|
code = next(gen._generate_packed_source(fields))
|
|
750
750
|
|
|
751
751
|
expected = """
|
|
752
|
-
if (_value0 := data.
|
|
753
|
-
if (_value1 := data.c) is None: _value1 = _type2.__default__()
|
|
752
|
+
if (_value0 := data.c) is None: _value0 = _type2.__default__()
|
|
754
753
|
try:
|
|
755
|
-
buf = _pack0[endian](
|
|
754
|
+
buf = _pack0[endian](data.a, data.b, _value0.to_bytes(3, _byteorder[endian], signed=False))
|
|
756
755
|
except _struct_error:
|
|
757
|
-
buf = _pack0[endian](
|
|
756
|
+
buf = _pack0[endian](_or_default(data.a, _type0), _or_default(data.b, _type1), _value0.to_bytes(3, _byteorder[endian], signed=False))
|
|
758
757
|
stream.write(buf)
|
|
759
758
|
""" # noqa: E501
|
|
760
759
|
|
|
@@ -784,8 +783,8 @@ def test_generate_bits_write(cs: cstruct, TestEnum: type[Enum]) -> None:
|
|
|
784
783
|
expected = """
|
|
785
784
|
if (_value0 := data.a) is None: _value0 = _type0.__default__()
|
|
786
785
|
if (_value1 := data.b) is None: _value1 = _type1.__default__()
|
|
787
|
-
stream.write(_pack0[endian]((_value0 << 0 | _value1
|
|
788
|
-
"""
|
|
786
|
+
stream.write(_pack0[endian]((_value0 << 0 | _value1 << 2 if endian == '<' else _value0 << 6 | _value1 << 0)))
|
|
787
|
+
"""
|
|
789
788
|
|
|
790
789
|
assert code == dedent(expected)
|
|
791
790
|
|
|
@@ -1159,6 +1158,7 @@ def test_write_custom_types(cs: cstruct) -> None:
|
|
|
1159
1158
|
|
|
1160
1159
|
class CustomWrite(int, Packed):
|
|
1161
1160
|
__fmt__ = "B"
|
|
1161
|
+
__signed__ = False
|
|
1162
1162
|
|
|
1163
1163
|
@classmethod
|
|
1164
1164
|
def _write(cls, stream: BinaryIO, data: int, *, endian: Endianness) -> int:
|
|
@@ -1166,6 +1166,7 @@ def test_write_custom_types(cs: cstruct) -> None:
|
|
|
1166
1166
|
|
|
1167
1167
|
class CustomWriteArray(int, Packed):
|
|
1168
1168
|
__fmt__ = "B"
|
|
1169
|
+
__signed__ = False
|
|
1169
1170
|
|
|
1170
1171
|
@classmethod
|
|
1171
1172
|
def _write_array(cls, stream: BinaryIO, data: list, *, endian: Endianness) -> int:
|
|
@@ -993,7 +993,7 @@ def test_dumpstruct_union_root(cs: cstruct, compiled: bool) -> None:
|
|
|
993
993
|
|
|
994
994
|
|
|
995
995
|
def test_dumpstruct_union_nested_struct(cs: cstruct, compiled: bool) -> None:
|
|
996
|
-
"""Nested Structure members inside a Union are proxied via UnionProxy; verify unwrap."""
|
|
996
|
+
"""Nested Structure members inside a Union are proxied via UnionProxy, twice in nested unions; verify unwrap."""
|
|
997
997
|
cdef = """
|
|
998
998
|
struct inner {
|
|
999
999
|
uint32 a;
|
|
@@ -1004,6 +1004,11 @@ def test_dumpstruct_union_nested_struct(cs: cstruct, compiled: bool) -> None:
|
|
|
1004
1004
|
uint8 raw[8];
|
|
1005
1005
|
};
|
|
1006
1006
|
|
|
1007
|
+
union outer_outer {
|
|
1008
|
+
outer nested;
|
|
1009
|
+
uint8 raw[8];
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1007
1012
|
union anon_outer {
|
|
1008
1013
|
struct {
|
|
1009
1014
|
uint32 a;
|
|
@@ -1022,9 +1027,17 @@ def test_dumpstruct_union_nested_struct(cs: cstruct, compiled: bool) -> None:
|
|
|
1022
1027
|
]
|
|
1023
1028
|
raw = {"name": "raw[8]", "type": "uint8", "offset": 0, "size": 8, "display": "[1, 0, 0, 0, 2, 0, 0, 0]"}
|
|
1024
1029
|
|
|
1030
|
+
outer_fields = [
|
|
1031
|
+
{"name": "nested", "type": "inner", "offset": 0, "size": 8, "kind": "struct", "fields": inner_fields},
|
|
1032
|
+
raw,
|
|
1033
|
+
]
|
|
1034
|
+
|
|
1025
1035
|
result = dumpstruct(cs.outer, buf, output="dict")
|
|
1036
|
+
assert result["fields"] == outer_fields
|
|
1037
|
+
|
|
1038
|
+
result = dumpstruct(cs.outer_outer, buf, output="dict")
|
|
1026
1039
|
assert result["fields"] == [
|
|
1027
|
-
{"name": "nested", "type": "
|
|
1040
|
+
{"name": "nested", "type": "outer", "offset": 0, "size": 8, "kind": "union", "fields": outer_fields},
|
|
1028
1041
|
raw,
|
|
1029
1042
|
]
|
|
1030
1043
|
|
|
@@ -59,6 +59,7 @@ def test_enum_read(TestEnum: type[Enum]) -> None:
|
|
|
59
59
|
def test_enum_write(TestEnum: type[Enum]) -> None:
|
|
60
60
|
assert TestEnum.B.dumps() == b"\x02"
|
|
61
61
|
assert TestEnum(b"\x02").dumps() == b"\x02"
|
|
62
|
+
assert TestEnum.dumps(2) == b"\x02"
|
|
62
63
|
|
|
63
64
|
|
|
64
65
|
def test_enum_array_read(TestEnum: type[Enum]) -> None:
|
|
@@ -69,6 +70,8 @@ def test_enum_array_read(TestEnum: type[Enum]) -> None:
|
|
|
69
70
|
def test_enum_array_write(TestEnum: type[Enum]) -> None:
|
|
70
71
|
assert TestEnum[2]([TestEnum.B, TestEnum.C]).dumps() == b"\x02\x03"
|
|
71
72
|
assert TestEnum[None]([TestEnum.B, TestEnum.C]).dumps() == b"\x02\x03\x00"
|
|
73
|
+
assert TestEnum[2]([2, 3]).dumps() == b"\x02\x03"
|
|
74
|
+
assert TestEnum[None]([2, 3]).dumps() == b"\x02\x03\x00"
|
|
72
75
|
|
|
73
76
|
|
|
74
77
|
def test_enum_alias(cs: cstruct) -> None:
|
|
@@ -366,7 +369,12 @@ def test_enum_struct_write(cs: cstruct, compiled: bool) -> None:
|
|
|
366
369
|
obj.b32 = cs.Test32.B
|
|
367
370
|
obj.list = [cs.Test16.A, cs.Test16.B]
|
|
368
371
|
|
|
369
|
-
|
|
372
|
+
buf = b"\x01\x00\x02\x00\x01\x00\x00\x02\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x02\x00"
|
|
373
|
+
assert obj.dumps() == buf
|
|
374
|
+
|
|
375
|
+
# Plain integers are written like enum members
|
|
376
|
+
obj = cs.test(a16=1, b16=2, a24=1, b24=2, a32=1, b32=2, list=[1, 2])
|
|
377
|
+
assert obj.dumps() == buf
|
|
370
378
|
|
|
371
379
|
|
|
372
380
|
def test_enum_anonymous(cs: cstruct, compiled: bool) -> None:
|
|
@@ -254,3 +254,18 @@ def test_packed_struct_write_range(cs: cstruct, compiled: bool) -> None:
|
|
|
254
254
|
|
|
255
255
|
with pytest.raises(struct.error):
|
|
256
256
|
cs.test(a=256, b=2, c=3, d=4.0).dumps()
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def test_packed_signed(cs: cstruct) -> None:
|
|
260
|
+
"""Test that the signed attribute is set correctly for packed types."""
|
|
261
|
+
assert cs.int8.__signed__ is True
|
|
262
|
+
assert cs.uint8.__signed__ is False
|
|
263
|
+
assert cs.int16.__signed__ is True
|
|
264
|
+
assert cs.uint16.__signed__ is False
|
|
265
|
+
assert cs.int32.__signed__ is True
|
|
266
|
+
assert cs.uint32.__signed__ is False
|
|
267
|
+
assert cs.int64.__signed__ is True
|
|
268
|
+
assert cs.uint64.__signed__ is False
|
|
269
|
+
assert cs.float16.__signed__ is True
|
|
270
|
+
assert cs.float.__signed__ is True
|
|
271
|
+
assert cs.double.__signed__ is True
|
|
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.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/entry_points.txt
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/requires.txt
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/dissect.cstruct.egg-info/scm_file_list.json
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev19 → dissect_cstruct-5.0.dev21}/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
|