dissect.cstruct 4.6.dev3__tar.gz → 4.6.dev5__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 (76) hide show
  1. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/CHANGELOG.md +1 -0
  2. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/PKG-INFO +1 -1
  3. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/cstruct.py +4 -1
  4. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/structure.py +39 -9
  5. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect.cstruct.egg-info/PKG-INFO +1 -1
  6. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_basic.py +46 -2
  7. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/.git-blame-ignore-revs +0 -0
  8. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/.gitattributes +0 -0
  9. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/COPYRIGHT +0 -0
  10. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/LICENSE +0 -0
  11. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/MANIFEST.in +0 -0
  12. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/README.md +0 -0
  13. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/__init__.py +0 -0
  14. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/bitbuffer.py +0 -0
  15. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/compiler.py +0 -0
  16. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/exceptions.py +0 -0
  17. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/expression.py +0 -0
  18. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/parser.py +0 -0
  19. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/tools/__init__.py +0 -0
  20. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/tools/stubgen.py +0 -0
  21. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/__init__.py +0 -0
  22. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/base.py +0 -0
  23. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/char.py +0 -0
  24. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/enum.py +0 -0
  25. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/flag.py +0 -0
  26. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/int.py +0 -0
  27. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/leb128.py +0 -0
  28. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/packed.py +0 -0
  29. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/pointer.py +0 -0
  30. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/void.py +0 -0
  31. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/types/wchar.py +0 -0
  32. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect/cstruct/utils.py +0 -0
  33. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect.cstruct.egg-info/SOURCES.txt +0 -0
  34. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
  35. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect.cstruct.egg-info/entry_points.txt +0 -0
  36. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect.cstruct.egg-info/requires.txt +0 -0
  37. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/dissect.cstruct.egg-info/top_level.txt +0 -0
  38. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/examples/disk.py +0 -0
  39. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/examples/mirai.py +0 -0
  40. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/examples/pe.py +0 -0
  41. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/examples/protobuf.py +0 -0
  42. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/examples/secdesc.py +0 -0
  43. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/pyproject.toml +0 -0
  44. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/setup.cfg +0 -0
  45. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/__init__.py +0 -0
  46. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/_data/testdef.txt +0 -0
  47. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/_docs/Makefile +0 -0
  48. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/_docs/__init__.py +0 -0
  49. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/_docs/conf.py +0 -0
  50. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/_docs/index.rst +0 -0
  51. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/conftest.py +0 -0
  52. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_align.py +0 -0
  53. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_annotations.py +0 -0
  54. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_bitbuffer.py +0 -0
  55. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_bitfield.py +0 -0
  56. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_compiler.py +0 -0
  57. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_ctypes.py +0 -0
  58. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_expression.py +0 -0
  59. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_parser.py +0 -0
  60. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_tools_stubgen.py +0 -0
  61. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_base.py +0 -0
  62. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_char.py +0 -0
  63. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_custom.py +0 -0
  64. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_enum.py +0 -0
  65. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_flag.py +0 -0
  66. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_int.py +0 -0
  67. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_leb128.py +0 -0
  68. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_packed.py +0 -0
  69. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_pointer.py +0 -0
  70. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_structure.py +0 -0
  71. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_union.py +0 -0
  72. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_void.py +0 -0
  73. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_types_wchar.py +0 -0
  74. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/test_utils.py +0 -0
  75. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tests/utils.py +0 -0
  76. {dissect_cstruct-4.6.dev3 → dissect_cstruct-4.6.dev5}/tox.ini +0 -0
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
11
11
  - Optimize storage of field sizes.
12
12
  - Rename `_sizes` property of `Structure` to `__sizes__`.
13
13
  - Rename `_values` property of `Structure` to `__values__`.
14
+ - 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.
14
15
 
15
16
  ## [4.5] - 20-05-2025
16
17
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 4.6.dev3
3
+ Version: 4.6.dev5
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: Apache License 2.0
@@ -48,7 +48,7 @@ class cstruct:
48
48
  DEF_CSTYLE = 1
49
49
  DEF_LEGACY = 2
50
50
 
51
- def __init__(self, endian: str = "<", pointer: str | None = None):
51
+ def __init__(self, load: str = "", *, endian: str = "<", pointer: str | None = None):
52
52
  self.endian = endian
53
53
 
54
54
  self.consts = {}
@@ -188,6 +188,9 @@ class cstruct:
188
188
  self.pointer: type[BaseType] = self.resolve(pointer)
189
189
  self._anonymous_count = 0
190
190
 
191
+ if load:
192
+ self.load(load)
193
+
191
194
  def __getattr__(self, attr: str) -> Any:
192
195
  try:
193
196
  return self.consts[attr]
@@ -828,13 +828,13 @@ def _generate_structure__init__(fields: list[Field]) -> FunctionType:
828
828
  Args:
829
829
  fields: List of field names.
830
830
  """
831
- field_names = [field._name for field in fields]
831
+ mapping = _generate_co_mapping(fields)
832
832
 
833
- template: FunctionType = _make_structure__init__(len(field_names))
833
+ template: FunctionType = _make_structure__init__(len(fields))
834
834
  return type(template)(
835
835
  template.__code__.replace(
836
- co_names=tuple(chain.from_iterable(zip((f"__{name}_default__" for name in field_names), field_names))),
837
- co_varnames=("self", *field_names),
836
+ co_names=_remap_co_values(template.__code__.co_names, mapping),
837
+ co_varnames=_remap_co_values(template.__code__.co_varnames, mapping),
838
838
  ),
839
839
  template.__globals__ | {f"__{field._name}_default__": field.type.__default__() for field in fields},
840
840
  argdefs=template.__defaults__,
@@ -847,20 +847,50 @@ def _generate_union__init__(fields: list[Field]) -> FunctionType:
847
847
  Args:
848
848
  fields: List of field names.
849
849
  """
850
- field_names = [field._name for field in fields]
850
+ mapping = _generate_co_mapping(fields)
851
851
 
852
- template: FunctionType = _make_union__init__(len(field_names))
852
+ template: FunctionType = _make_union__init__(len(fields))
853
853
  return type(template)(
854
854
  template.__code__.replace(
855
- co_consts=(None, *field_names),
856
- co_names=("object", "__setattr__", *(f"__{name}_default__" for name in field_names)),
857
- co_varnames=("self", *field_names),
855
+ co_consts=_remap_co_values(template.__code__.co_consts, mapping),
856
+ co_names=_remap_co_values(template.__code__.co_names, mapping),
857
+ co_varnames=_remap_co_values(template.__code__.co_varnames, mapping),
858
858
  ),
859
859
  template.__globals__ | {f"__{field._name}_default__": field.type.__default__() for field in fields},
860
860
  argdefs=template.__defaults__,
861
861
  )
862
862
 
863
863
 
864
+ def _generate_co_mapping(fields: list[Field]) -> dict[str, str]:
865
+ """Generates a mapping of generated code object names to field names.
866
+
867
+ The generated code uses names like ``_0``, ``_1``, etc. for fields, and ``_0_default``, ``_1_default``, etc.
868
+ for default initializer values. Return a mapping of these names to the actual field names.
869
+
870
+ Args:
871
+ fields: List of field names.
872
+ """
873
+ return {
874
+ key: value
875
+ for i, field in enumerate(fields)
876
+ for key, value in [(f"_{i}", field._name), (f"_{i}_default", f"__{field._name}_default__")]
877
+ }
878
+
879
+
880
+ def _remap_co_values(value: tuple[Any, ...], mapping: dict[str, str]) -> tuple[Any, ...]:
881
+ """Remaps code object values using a mapping.
882
+
883
+ This is used to replace generated code object names with actual field names.
884
+
885
+ Args:
886
+ value: The original code object values.
887
+ mapping: A mapping of generated code object names to field names.
888
+ """
889
+ # Only attempt to remap if the value is a string, otherwise return it as is
890
+ # This is to avoid issues with trying to remap non-hashable types, and we only need to replace strings anyway
891
+ return tuple(mapping.get(v, v) if isinstance(v, str) else v for v in value)
892
+
893
+
864
894
  def _generate__eq__(fields: list[str]) -> FunctionType:
865
895
  """Generates an ``__eq__`` method for a class with the specified fields.
866
896
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 4.6.dev3
3
+ Version: 4.6.dev5
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: Apache License 2.0
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, BinaryIO
6
6
 
7
7
  import pytest
8
8
 
9
+ from dissect.cstruct.cstruct import cstruct
9
10
  from dissect.cstruct.exceptions import ArraySizeError, ParserError, ResolveError
10
11
  from dissect.cstruct.types import BaseType
11
12
 
@@ -14,8 +15,6 @@ from .utils import verify_compiled
14
15
  if TYPE_CHECKING:
15
16
  from pathlib import Path
16
17
 
17
- from dissect.cstruct.cstruct import cstruct
18
-
19
18
 
20
19
  def test_duplicate_type(cs: cstruct, compiled: bool) -> None:
21
20
  cdef = """
@@ -41,6 +40,51 @@ def test_load_file(cs: cstruct, compiled: bool, tmp_path: Path) -> None:
41
40
  assert "test" in cs.typedefs
42
41
 
43
42
 
43
+ def test_load_init() -> None:
44
+ cdef = """
45
+ struct test {
46
+ DWORD a;
47
+ QWORD b;
48
+ };
49
+ """
50
+ # load with first positional argument
51
+ cs = cstruct(cdef)
52
+ assert "test" in cs.typedefs
53
+ assert cs.endian == "<"
54
+
55
+ # load from keyword argument and big endian
56
+ cs = cstruct(load=cdef, endian=">")
57
+ assert "test" in cs.typedefs
58
+ a = cs.test(a=0xBADC0DE, b=0xACCE55ED)
59
+ assert len(bytes(a)) == 12
60
+ assert bytes(a) == a.dumps()
61
+ assert bytes(a) == b"\x0b\xad\xc0\xde\x00\x00\x00\x00\xac\xce\x55\xed"
62
+
63
+ # load using positional argument and little endian
64
+ cs = cstruct(cdef, endian="<")
65
+ assert "test" in cs.typedefs
66
+ a = cs.test(a=0xBADC0DE, b=0xACCE55ED)
67
+ assert len(bytes(a)) == 12
68
+ assert bytes(a) == a.dumps()
69
+ assert bytes(a) == b"\xde\xc0\xad\x0b\xed\x55\xce\xac\x00\x00\x00\x00"
70
+
71
+
72
+ def test_load_init_kwargs_only() -> None:
73
+ cdef = """
74
+ struct test {
75
+ uint32 a;
76
+ };
77
+ """
78
+
79
+ # kwargs only check
80
+ with pytest.raises(TypeError, match="takes from .* positional arguments but .* were given"):
81
+ cs = cstruct(cdef, ">")
82
+
83
+ cs = cstruct(cdef, endian=">")
84
+ assert "test" in cs.typedefs
85
+ assert cs.endian == ">"
86
+
87
+
44
88
  def test_read_type_name(cs: cstruct) -> None:
45
89
  assert cs.read("uint32", b"\x01\x00\x00\x00") == 1
46
90