dissect.cstruct 5.0.dev19__py3-none-any.whl → 5.0.dev21__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.
@@ -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.signed)
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.signed)})"
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.signed)
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
- elif issubclass(field_type, (Enum, Flag)):
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_.signed)})"
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={"signed": False}),
94
- "ileb128": self._make_type("ileb128", (LEB128,), None, attrs={"signed": True}),
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={"signed": signed}))
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
 
dissect/cstruct/dump.py CHANGED
@@ -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
- if isinstance(value, UnionProxy):
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
- return cls.__type__._write(stream, data.value, endian=endian)
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[BaseType | int], *, endian: Endianness) -> int:
107
- data = [entry.value if isinstance(entry, _Enum) else entry for entry in array]
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[BaseType | int], *, endian: Endianness) -> int:
111
- data = [entry.value if isinstance(entry, _Enum) else entry for entry in array]
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
- signed: bool
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.signed))
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.signed))
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.signed))
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
- signed: bool
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.signed and b & 0x40 != 0:
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.signed:
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.signed and (data == 0 and byte & 0x40 == 0)) or (data == -1 and byte & 0x40 != 0)) or (
68
- not cls.signed and data == 0
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
- bit_buffer.write(field_type.__type__, value.value, field.bits)
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.dev19
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
@@ -1,9 +1,9 @@
1
1
  dissect/cstruct/__init__.py,sha256=ti1VYFx1MtZuDcMks14haNvUw0IZYjRo346ChzhqPls,1647
2
2
  dissect/cstruct/bitbuffer.py,sha256=aNzY4lkWe8ffAc8j7FODXzwZc8gZwbey77UosyoYCsw,2711
3
- dissect/cstruct/compiler.py,sha256=0u58J5pB4izosBj8G4wiBI1JSAAgHFMLM63IaSG4YO4,54509
4
- dissect/cstruct/cstruct.py,sha256=mJerzPouWV29RdK5LqjGitDDMvNKcadoT2hj1zpExnM,26551
3
+ dissect/cstruct/compiler.py,sha256=cnx9_sAj6YTHgb4XB73FjuWi7Tv2m8Y7txHpuXdu5YQ,54456
4
+ dissect/cstruct/cstruct.py,sha256=mMwnu-0UmCxmuCoQlNvFs3acmQnQYZ9Ds-m6fG06HGs,26592
5
5
  dissect/cstruct/declarative.py,sha256=5ubPtOyy6L5gJMFaPcbW3xYbTijZGMp0XJGxBeed1nE,14505
6
- dissect/cstruct/dump.py,sha256=MxVJmn6v6cJs1Ud2FEmN0DaxKCUYOJk0YX4pWh1-1FQ,19013
6
+ dissect/cstruct/dump.py,sha256=W5ISbaZ-1YmWXKzh-x6pDt5_SlpFRDhD9GGNyv1MnLw,19016
7
7
  dissect/cstruct/exception.py,sha256=box-mw6FoP6c3vndvYLcTeJCZVgIG8IQVPEAg4zW0C8,380
8
8
  dissect/cstruct/expression.py,sha256=SbZRv7ueasYU4k-4k2eV1fjwL3JTAgF2UBaBvNjwiv4,10736
9
9
  dissect/cstruct/lexer.py,sha256=N7eqCOlcaQriUUIswJv16i-CE_sahYXGO-YdFO0IKKY,20421
@@ -14,19 +14,19 @@ dissect/cstruct/tools/stubgen.py,sha256=MeZkLmSeNSLNcAEwlAdIGv1lKvlvK3UQ4WN85ovs
14
14
  dissect/cstruct/types/__init__.py,sha256=6P71Pu94Dv7RNjZpDZ19pqzx-G0QzmQw4q0-3zWALso,911
15
15
  dissect/cstruct/types/base.py,sha256=NdCP9dJk6lrKPERYjR9qeD2gFzWd6krVSN4tAOqqkvI,18357
16
16
  dissect/cstruct/types/char.py,sha256=N19R7Zzn2uMuG-ULW5X2UgmgkLvjaBj1Td7AQQI93Fg,3417
17
- dissect/cstruct/types/enum.py,sha256=nOdI6vGtqTfPMhOBkKuiuOriIB2YHSt8Q9ruri37s3U,9177
17
+ dissect/cstruct/types/enum.py,sha256=VICmKWRuzTicq6pNKpAG_xfFzC5pVLp48qe9lFyXqoc,9029
18
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
19
+ dissect/cstruct/types/int.py,sha256=DpeCVBuNljNARTZ5jkLU8LkDVjuSRXLzIGQH4GTVUIY,2442
20
+ dissect/cstruct/types/leb128.py,sha256=UFGoHD6c6jlfhrgqKi7pcK9OdKT8U8dStCmgM1XwVXw,2776
21
+ dissect/cstruct/types/packed.py,sha256=kfZZm36Kff6iUGefkXsDOU3QrxDpVveTtMWz_a9q-lM,3888
22
22
  dissect/cstruct/types/pointer.py,sha256=FNuB9bfENWRebbKX-R6Vs6xWPMHepjvOhhomeUhr7I8,3985
23
- dissect/cstruct/types/structure.py,sha256=EfsYgRNMcwvnUm6PStPu7yK_9BtRiJTaBlXDj0Eua7Y,43076
23
+ dissect/cstruct/types/structure.py,sha256=cwLzetR0AEDBVqOIeOxo6viWxbTRvt5tYDkbZEISk_U,43165
24
24
  dissect/cstruct/types/void.py,sha256=fV4hKk50uop_EKbR1nfy1cdtAzvUYrSymXsdakCyFk8,1222
25
25
  dissect/cstruct/types/wchar.py,sha256=VrnPXsP_lsyA-1xr4D2aSIVijumMhUuZWNlDHcn12-s,3964
26
- dissect_cstruct-5.0.dev19.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
27
- dissect_cstruct-5.0.dev19.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
28
- dissect_cstruct-5.0.dev19.dist-info/METADATA,sha256=PkvBGlKF3RzyeIbZcPiTfDIo4cAY420raalRrq6vSJg,12661
29
- dissect_cstruct-5.0.dev19.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
30
- dissect_cstruct-5.0.dev19.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
31
- dissect_cstruct-5.0.dev19.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
32
- dissect_cstruct-5.0.dev19.dist-info/RECORD,,
26
+ dissect_cstruct-5.0.dev21.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
27
+ dissect_cstruct-5.0.dev21.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
28
+ dissect_cstruct-5.0.dev21.dist-info/METADATA,sha256=2PXj2VAGAfb8JdVmpv3QVgyYiOgrCTUtPeWZxR42N3o,12661
29
+ dissect_cstruct-5.0.dev21.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
30
+ dissect_cstruct-5.0.dev21.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
31
+ dissect_cstruct-5.0.dev21.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
32
+ dissect_cstruct-5.0.dev21.dist-info/RECORD,,