dissect.cstruct 5.0.dev24__tar.gz → 5.0.dev25__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.
Files changed (86) hide show
  1. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/CHANGELOG.md +2 -1
  2. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/PKG-INFO +1 -1
  3. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/cstruct.py +48 -9
  4. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/declarative.py +4 -0
  5. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/PKG-INFO +1 -1
  6. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/scm_version.json +2 -2
  7. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_basic.py +60 -2
  8. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_declarative.py +26 -0
  9. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/.git-blame-ignore-revs +0 -0
  10. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/.gitattributes +0 -0
  11. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/COPYRIGHT +0 -0
  12. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/LICENSE +0 -0
  13. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/MANIFEST.in +0 -0
  14. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/README.md +0 -0
  15. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/__init__.py +0 -0
  16. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/bitbuffer.py +0 -0
  17. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/compiler.py +0 -0
  18. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/dump.py +0 -0
  19. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/exception.py +0 -0
  20. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/expression.py +0 -0
  21. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/lexer.py +0 -0
  22. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/parser.py +0 -0
  23. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/tools/__init__.py +0 -0
  24. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/tools/stubgen.py +0 -0
  25. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/__init__.py +0 -0
  26. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/base.py +0 -0
  27. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/char.py +0 -0
  28. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/enum.py +0 -0
  29. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/flag.py +0 -0
  30. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/int.py +0 -0
  31. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/leb128.py +0 -0
  32. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/packed.py +0 -0
  33. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/pointer.py +0 -0
  34. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/structure.py +0 -0
  35. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/void.py +0 -0
  36. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/types/wchar.py +0 -0
  37. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect/cstruct/util.py +0 -0
  38. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/SOURCES.txt +0 -0
  39. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
  40. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/entry_points.txt +0 -0
  41. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/requires.txt +0 -0
  42. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/scm_file_list.json +0 -0
  43. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/dissect.cstruct.egg-info/top_level.txt +0 -0
  44. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/examples/disk.py +0 -0
  45. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/examples/mirai.py +0 -0
  46. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/examples/pe.py +0 -0
  47. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/examples/protobuf.py +0 -0
  48. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/examples/secdesc.py +0 -0
  49. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/pyproject.toml +0 -0
  50. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/setup.cfg +0 -0
  51. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/__init__.py +0 -0
  52. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/_data/testdef.txt +0 -0
  53. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/_docs/Makefile +0 -0
  54. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/_docs/__init__.py +0 -0
  55. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/_docs/conf.py +0 -0
  56. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/_docs/index.rst +0 -0
  57. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/conftest.py +0 -0
  58. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_align.py +0 -0
  59. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_annotations.py +0 -0
  60. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_benchmark.py +0 -0
  61. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_bitbuffer.py +0 -0
  62. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_bitfield.py +0 -0
  63. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_compiler.py +0 -0
  64. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_ctypes.py +0 -0
  65. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_dump.py +0 -0
  66. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_exception.py +0 -0
  67. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_expression.py +0 -0
  68. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_lexer.py +0 -0
  69. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_parser.py +0 -0
  70. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_tools_stubgen.py +0 -0
  71. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_base.py +0 -0
  72. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_char.py +0 -0
  73. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_custom.py +0 -0
  74. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_enum.py +0 -0
  75. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_flag.py +0 -0
  76. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_int.py +0 -0
  77. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_leb128.py +0 -0
  78. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_packed.py +0 -0
  79. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_pointer.py +0 -0
  80. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_structure.py +0 -0
  81. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_union.py +0 -0
  82. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_void.py +0 -0
  83. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_types_wchar.py +0 -0
  84. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/test_util.py +0 -0
  85. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tests/util.py +0 -0
  86. {dissect_cstruct-5.0.dev24 → dissect_cstruct-5.0.dev25}/tox.ini +0 -0
@@ -9,8 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
9
9
  ### Added
10
10
 
11
11
  - Declarative syntax for structures and unions in `dissect.cstruct.declarative`, using classes with type annotations.
12
- - `asdict()` method return the field values of a structure or union as a dict, with nested structures as dicts and arrays as lists. `asjson()` returns them as a JSON string, with bytes as base64 and enum members as numbers. `JSONEncoder` encodes structures as part of other data with `json.dumps()`.
12
+ - `asdict()` returns the field values of a structure or union as a dict, with nested structures as dicts and arrays as lists. `asjson()` returns them as a JSON string, with bytes as base64 and enum members as numbers. `JSONEncoder` encodes structures as part of other data with `json.dumps()`.
13
13
  - `sizeof` and `offsetof` in expressions accept a type name like the type of a field, e.g. `sizeof(struct header *[4])`. An argument that isn't a type name raises an `ExpressionParserError` when the expression is created.
14
+ - `copy()` method (and `copy.copy()`) to copy a `cstruct` instance with its types, constants and settings. Changes to the copy, such as new definitions or a different endianness, don't affect the original. Enums and flags of a copy are separate classes, so their members don't compare equal to those of the original. Compare against the copied enum, or with the value instead: `c2.Color.RED == cs.Color.RED.value`.
14
15
 
15
16
  ### Changed
16
17
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 5.0.dev24
3
+ Version: 5.0.dev25
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
@@ -21,6 +21,7 @@ from dissect.cstruct.types import (
21
21
  BaseType,
22
22
  Char,
23
23
  Enum,
24
+ Field,
24
25
  Flag,
25
26
  Int,
26
27
  Packed,
@@ -36,10 +37,7 @@ if TYPE_CHECKING:
36
37
  from collections.abc import Generator, Iterable
37
38
  from typing import TypeAlias
38
39
 
39
- from dissect.cstruct.types import (
40
- Array,
41
- Field,
42
- )
40
+ from dissect.cstruct.types import Array
43
41
 
44
42
 
45
43
  T = TypeVar("T", bound=BaseType)
@@ -245,21 +243,62 @@ class cstruct:
245
243
  raise AttributeError(f"'{type(self).__name__}' object has no attribute {attr!r}")
246
244
 
247
245
  def __copy__(self) -> cstruct:
248
- cs = cstruct(endian=self.endian, pointer=self.pointer.__name__)
246
+ cs = cstruct(endian=self.endian)
249
247
  cs._anonymous_count = self._anonymous_count
250
248
  cs.includes = self.includes.copy()
251
249
 
252
- # Update types to point to the new cstruct instance
250
+ # Copy the types as new classes, including the unregistered types they refer to
251
+ copies = {}
253
252
  for name, type_ in self.types.items():
254
- new_type = copy.copy(type_)
255
- new_type.__cs__ = cs
256
- cs.add_type(name, new_type, replace=True)
253
+ cs.add_type(name, cs._copy_type(type_, copies), replace=True)
254
+ cs.pointer = cs._copy_type(self.pointer, copies)
257
255
 
258
256
  for name, value in self.consts.items():
259
257
  cs.add_const(name, value)
260
258
 
261
259
  return cs
262
260
 
261
+ def _copy_type(self, type_: type[BaseType], copies: dict[type[BaseType], type[BaseType]]) -> type[BaseType]:
262
+ if type_ in copies:
263
+ return copies[type_]
264
+
265
+ if issubclass(type_, (Enum, Flag)):
266
+ # Enum classes with members can't be created from their namespace
267
+ factory = self._make_flag if issubclass(type_, Flag) else self._make_enum
268
+ values = {name: member.value for name, member in type_.__members__.items()}
269
+ copies[type_] = factory(type_.__name__, self._copy_type(type_.__type__, copies), values)
270
+ return copies[type_]
271
+
272
+ # A type is registered before its subclasses, e.g. a declarative structure, so the copy of its base exists
273
+ bases = tuple(copies.get(base, base) for base in type_.__bases__)
274
+
275
+ # The namespace of a class doesn't hold its qualified name, e.g. of a declarative structure in a function
276
+ attrs = {**vars(type_), "__qualname__": type_.__qualname__}
277
+
278
+ # Register the copy before copying the types it refers to, so that a structure can point to itself
279
+ if issubclass(type_, Structure):
280
+ attrs["__members__"] = []
281
+
282
+ new_type = copies[type_] = self._make_type(
283
+ type_.__name__,
284
+ bases,
285
+ type_.__size__,
286
+ alignment=type_.__alignment__,
287
+ attrs=attrs,
288
+ )
289
+
290
+ if issubclass(type_, (BaseArray, Pointer)):
291
+ new_type.__type__ = self._copy_type(type_.__type__, copies)
292
+
293
+ if issubclass(type_, Structure):
294
+ new_type.__fields__ = [
295
+ Field(field.name, self._copy_type(field.type, copies), field.bits, field.offset)
296
+ for field in type_.__fields__
297
+ ]
298
+ new_type.commit()
299
+
300
+ return new_type
301
+
263
302
  # Hidden from type checkers, which otherwise accept assigning any attribute, e.g. a misspelled one
264
303
  if not TYPE_CHECKING:
265
304
 
@@ -395,6 +395,10 @@ class _Declarative:
395
395
  def __init_subclass__(cls, *, cs: cstruct | None = None, align: bool | None = None, **kwargs):
396
396
  super().__init_subclass__(**kwargs)
397
397
 
398
+ if "__fields__" in vars(cls):
399
+ # Created by cstruct with its fields, e.g. a copy made by cstruct.copy()
400
+ return
401
+
398
402
  if cs is not None:
399
403
  cls.__cs__ = cs
400
404
  if align is not None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 5.0.dev24
3
+ Version: 5.0.dev25
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,7 +1,7 @@
1
1
  {
2
2
  "tag": "5.0.dev0",
3
- "distance": 24,
4
- "node": "gc1a3840bbc14a23b46014914668f2bef19975db7",
3
+ "distance": 25,
4
+ "node": "g4c837b5e0bd6774544615e30a48dad9d6bfed2ba",
5
5
  "dirty": true,
6
6
  "branch": "main",
7
7
  "node_date": "2026-11-23"
@@ -588,14 +588,24 @@ def test_linked_list(cs: cstruct) -> None:
588
588
  assert obj.next.data == 2
589
589
 
590
590
 
591
- def test_copy(cs: cstruct) -> None:
591
+ def test_copy(cs: cstruct, compiled: bool) -> None:
592
592
  """Test that copying a cstruct instance works as expected."""
593
593
  cdef = """
594
+ enum Color : uint16 {
595
+ RED = 1
596
+ };
597
+
594
598
  struct test {
595
599
  uint32 a;
596
600
  };
601
+
602
+ struct node {
603
+ Color color;
604
+ struct node *next;
605
+ };
597
606
  """
598
- cs.load(cdef)
607
+ cs.pointer = cs.uint16
608
+ cs.load(cdef, compiled=compiled)
599
609
 
600
610
  cs_copy = cs.copy()
601
611
 
@@ -616,6 +626,54 @@ def test_copy(cs: cstruct) -> None:
616
626
  assert cs_copy.resolve("test").__cs__ is cs_copy
617
627
  assert cs_copy.resolve("test2").__cs__ is cs_copy
618
628
 
629
+ # Verify that the types are copied, including the types they refer to, and the original is unchanged
630
+ assert cs.test.__cs__ is cs
631
+ assert cs_copy.test is not cs.test
632
+ assert cs_copy.test.__members__["a"].type is cs_copy.uint32
633
+ assert cs_copy.node.__members__["next"].type.__type__ is cs_copy.node
634
+ assert cs_copy.Color.__type__ is cs_copy.uint16
635
+ assert cs_copy.pointer is cs_copy.uint16
636
+
637
+ # Enum classes are copied and no longer compare against the original
638
+ assert cs_copy.Color.RED != cs.Color.RED
639
+ assert cs.Color.RED.value == cs_copy.Color.RED
640
+
641
+ cs_copy.endian = ">"
642
+ assert cs.uint16(b"\x01\x00") == 1
643
+ assert cs.test(b"\x01\x00\x00\x00").a == 1
644
+ assert cs.node(b"\x01\x00\x02\x00").color is cs.Color.RED
645
+
646
+ obj = cs_copy.node(b"\x00\x01\x00\x02")
647
+ assert obj.color is cs_copy.Color.RED
648
+ assert obj.color is not cs.Color.RED
649
+ assert obj.next == 2
650
+
651
+
652
+ def test_copy_after_use(cs: cstruct) -> None:
653
+ """Test that copying a cstruct instance works as expected, also after compilation."""
654
+ cdef = """
655
+ struct inner {
656
+ uint16 x;
657
+ };
658
+
659
+ struct outer {
660
+ inner i;
661
+ };
662
+ """
663
+ cs.load(cdef, compiled=True)
664
+
665
+ # Compilation is lazy, so reading once forces a compile
666
+ cs.outer(b"\x01\x00")
667
+ assert cs.outer._read.__source__
668
+
669
+ cs_copy = cs.copy()
670
+ cs_copy.endian = ">"
671
+
672
+ obj = cs_copy.outer(b"\x00\x01")
673
+ assert type(obj.i) is cs_copy.inner
674
+ assert obj.i.x == 1
675
+ assert cs.outer(b"\x01\x00").i.x == 1
676
+
619
677
 
620
678
  def test_cdef(cs: cstruct) -> None:
621
679
  """Test that a cstruct instance can render all its definitions back to C-style definitions."""
@@ -168,6 +168,32 @@ def test_self_referential_pointer_string(cs: cstruct) -> None:
168
168
  assert Node.__members__["next"].type.__type__ is Node
169
169
 
170
170
 
171
+ def test_copy(cs: cstruct) -> None:
172
+ """Test that a copy of a cstruct instance has copies of its structures, without evaluating the annotations again."""
173
+
174
+ class Node(Struct, cs=cs):
175
+ data: uint32
176
+ next: pointer["Node"]
177
+
178
+ def double(self) -> int:
179
+ return self.data * 2
180
+
181
+ class Extended(Node):
182
+ flags: uint16
183
+
184
+ cs_copy = cs.copy()
185
+
186
+ assert cs_copy.Node is not Node
187
+ assert cs_copy.Node.__qualname__ == "test_copy.<locals>.Node"
188
+ assert cs_copy.Node.__cs__ is cs_copy
189
+ assert cs_copy.Node.__members__["next"].type.__type__ is cs_copy.Node
190
+ assert cs_copy.Node(b"\x02\x00\x00\x00" + b"\x00" * 8).double() == 4
191
+
192
+ # A subclass is a subclass of the copy of its parent
193
+ assert cs_copy.Extended.__bases__ == (cs_copy.Node,)
194
+ assert Extended.__bases__ == (Node,)
195
+
196
+
171
197
  def test_self_referential_embed(cs: cstruct) -> None:
172
198
  with pytest.raises(DefinitionError, match=r"^Field 'inner' embeds 'Recursive' in itself, use a pointer instead$"):
173
199