dissect.cstruct 5.0.dev18__py3-none-any.whl → 5.0.dev19__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 +4 -0
- dissect/cstruct/dump.py +3 -0
- dissect/cstruct/types/structure.py +88 -0
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/METADATA +33 -1
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/RECORD +10 -10
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/WHEEL +0 -0
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/entry_points.txt +0 -0
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/licenses/COPYRIGHT +0 -0
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/licenses/LICENSE +0 -0
- {dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/top_level.txt +0 -0
dissect/cstruct/__init__.py
CHANGED
|
@@ -29,6 +29,7 @@ from dissect.cstruct.types import (
|
|
|
29
29
|
Wchar,
|
|
30
30
|
WcharArray,
|
|
31
31
|
)
|
|
32
|
+
from dissect.cstruct.types.structure import JSONEncoder, asdict, asjson
|
|
32
33
|
from dissect.cstruct.util import (
|
|
33
34
|
f16,
|
|
34
35
|
f32,
|
|
@@ -65,6 +66,7 @@ __all__ = [
|
|
|
65
66
|
"Field",
|
|
66
67
|
"Flag",
|
|
67
68
|
"Int",
|
|
69
|
+
"JSONEncoder",
|
|
68
70
|
"MetaType",
|
|
69
71
|
"NullPointerDereference",
|
|
70
72
|
"Packed",
|
|
@@ -76,6 +78,8 @@ __all__ = [
|
|
|
76
78
|
"Void",
|
|
77
79
|
"Wchar",
|
|
78
80
|
"WcharArray",
|
|
81
|
+
"asdict",
|
|
82
|
+
"asjson",
|
|
79
83
|
"cstruct",
|
|
80
84
|
"ctypes",
|
|
81
85
|
"ctypes_type",
|
dissect/cstruct/dump.py
CHANGED
|
@@ -428,6 +428,9 @@ def dumpstruct(
|
|
|
428
428
|
|
|
429
429
|
Prints a colorized hexdump and parsed structure output.
|
|
430
430
|
|
|
431
|
+
Note that this is different from :func:`asdict` and :func:`asjson`: those functions return the field values of a
|
|
432
|
+
structure or union, while this function describes the layout of the structure (hex bytes, offsets, display strings).
|
|
433
|
+
|
|
431
434
|
The ``dict`` and ``json`` output formats describe the structure as a tree:
|
|
432
435
|
- bytes: hex string of the raw data
|
|
433
436
|
- type: type name of the structure
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import base64
|
|
3
4
|
import io
|
|
5
|
+
import json
|
|
4
6
|
import warnings
|
|
5
7
|
from collections import ChainMap
|
|
6
8
|
from collections.abc import MutableMapping
|
|
@@ -26,6 +28,7 @@ from dissect.cstruct.types.base import (
|
|
|
26
28
|
)
|
|
27
29
|
from dissect.cstruct.types.enum import EnumMetaType
|
|
28
30
|
from dissect.cstruct.types.pointer import Pointer
|
|
31
|
+
from dissect.cstruct.types.void import Void
|
|
29
32
|
|
|
30
33
|
if TYPE_CHECKING:
|
|
31
34
|
from collections.abc import Callable, Iterator, Mapping
|
|
@@ -808,6 +811,91 @@ class UnionProxy:
|
|
|
808
811
|
self.__union__._rebuild(self.__attr__)
|
|
809
812
|
|
|
810
813
|
|
|
814
|
+
def asdict(obj: Structure | UnionProxy) -> dict[str, Any]:
|
|
815
|
+
"""Return the field values of a structure or union as a dict, like :func:`dataclasses.asdict`.
|
|
816
|
+
|
|
817
|
+
The keys are the field names in :attr:`StructureMetaType.__members__`, so the fields of anonymous structures and
|
|
818
|
+
unions are part of the parent, and a union has all of its fields. Nested structures and unions become dicts and
|
|
819
|
+
arrays become lists. Other values are returned as they are: enum members stay enum members, and pointers are not
|
|
820
|
+
dereferenced.
|
|
821
|
+
|
|
822
|
+
Note that this is different from :func:`dumpstruct` called with ``output="dict"``: this function returns a
|
|
823
|
+
dictionary with pairs of field names and values, rather than a dictionary that describes the layout of the
|
|
824
|
+
structure (hex bytes, offsets, display strings).
|
|
825
|
+
|
|
826
|
+
Args:
|
|
827
|
+
obj: The structure or union to convert.
|
|
828
|
+
|
|
829
|
+
Raises:
|
|
830
|
+
TypeError: If ``obj`` is not a structure or union instance.
|
|
831
|
+
"""
|
|
832
|
+
while isinstance(obj, UnionProxy):
|
|
833
|
+
obj = obj.__target__
|
|
834
|
+
|
|
835
|
+
if not isinstance(obj, Structure):
|
|
836
|
+
raise TypeError("asdict() should be called on Structure instances")
|
|
837
|
+
|
|
838
|
+
return _asdict_value(obj)
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
def _asdict_value(value: Any) -> Any:
|
|
842
|
+
# Structures in a union are proxied
|
|
843
|
+
while isinstance(value, UnionProxy):
|
|
844
|
+
value = value.__target__
|
|
845
|
+
|
|
846
|
+
if isinstance(value, Structure):
|
|
847
|
+
return {name: _asdict_value(getattr(value, name)) for name in value.__class__.__members__}
|
|
848
|
+
|
|
849
|
+
if isinstance(value, (list, tuple)):
|
|
850
|
+
# The entries of an array of other types are returned as they are, copying them is much faster
|
|
851
|
+
if isinstance(value, BaseArray) and not issubclass(value.__type__, (Structure, BaseArray)):
|
|
852
|
+
return list(value)
|
|
853
|
+
return [_asdict_value(item) for item in value]
|
|
854
|
+
|
|
855
|
+
return value
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
class JSONEncoder(json.JSONEncoder):
|
|
859
|
+
"""JSON encoder for structures and unions, and the values of their fields.
|
|
860
|
+
|
|
861
|
+
Structures and unions are encoded as objects with the fields of :func:`asdict`. Bytes, such as the values of
|
|
862
|
+
``char`` arrays, are encoded as base64 strings and ``void`` values as ``null``. Enum and flag members and pointers
|
|
863
|
+
are integers, which :mod:`json` encodes as numbers.
|
|
864
|
+
|
|
865
|
+
Example:
|
|
866
|
+
>>> json.dumps({"header": header}, cls=JSONEncoder)
|
|
867
|
+
"""
|
|
868
|
+
|
|
869
|
+
def default(self, o: Any) -> Any:
|
|
870
|
+
if isinstance(o, (Structure, UnionProxy)):
|
|
871
|
+
return asdict(o)
|
|
872
|
+
|
|
873
|
+
if isinstance(o, _BUFFER_TYPES):
|
|
874
|
+
return base64.b64encode(o).decode()
|
|
875
|
+
|
|
876
|
+
if isinstance(o, Void):
|
|
877
|
+
return None
|
|
878
|
+
|
|
879
|
+
return super().default(o)
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
def asjson(obj: Structure | UnionProxy, **kwargs) -> str:
|
|
883
|
+
"""Return the field values of a structure or union as a JSON string, encoded with :class:`JSONEncoder`.
|
|
884
|
+
|
|
885
|
+
Note that this is different from :func:`dumpstruct` called with ``output="json"``: this function returns a
|
|
886
|
+
JSON string with pairs of field names and values, rather than a JSON string that describes the layout of the
|
|
887
|
+
structure (hex bytes, offsets, display strings).
|
|
888
|
+
|
|
889
|
+
Args:
|
|
890
|
+
obj: The structure or union to convert.
|
|
891
|
+
**kwargs: Keyword arguments for :func:`json.dumps`, e.g. ``indent``.
|
|
892
|
+
|
|
893
|
+
Raises:
|
|
894
|
+
TypeError: If ``obj`` is not a structure or union instance.
|
|
895
|
+
"""
|
|
896
|
+
return json.dumps(asdict(obj), cls=JSONEncoder, **kwargs)
|
|
897
|
+
|
|
898
|
+
|
|
811
899
|
def attrsetter(path: str) -> Callable[[Any], Any]:
|
|
812
900
|
path, _, attr = path.rpartition(".")
|
|
813
901
|
path = path.split(".")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.cstruct
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.dev19
|
|
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
|
|
@@ -312,6 +312,38 @@ assert example.magic == 1337
|
|
|
312
312
|
assert example.data == b"arbitrary length data"
|
|
313
313
|
```
|
|
314
314
|
|
|
315
|
+
### Converting to dicts and JSON
|
|
316
|
+
`asdict()` returns the field values of a structure or union as a dict, like `dataclasses.asdict()`. The method `_asdict()` does the same, like `namedtuple`. Nested structures and unions become dicts and arrays become lists. Other values are returned as they are, so enum members stay enum members and pointers are not dereferenced.
|
|
317
|
+
|
|
318
|
+
`asjson()` returns the same values as a JSON string. Bytes are encoded as base64 strings, and enum and flag members and pointers as numbers. To encode structures as part of other data, use `JSONEncoder` with `json.dumps()`.
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
import json
|
|
322
|
+
|
|
323
|
+
from dissect.cstruct import JSONEncoder, asdict, asjson, cstruct
|
|
324
|
+
|
|
325
|
+
c_def = """
|
|
326
|
+
enum Type : uint8 {
|
|
327
|
+
FILE = 1,
|
|
328
|
+
DIRECTORY = 2
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
struct entry {
|
|
332
|
+
Type type;
|
|
333
|
+
char name[4];
|
|
334
|
+
uint32 sizes[2];
|
|
335
|
+
};
|
|
336
|
+
"""
|
|
337
|
+
|
|
338
|
+
c = cstruct().load(c_def)
|
|
339
|
+
|
|
340
|
+
entry = c.entry(b"\x01test\x01\x00\x00\x00\x02\x00\x00\x00")
|
|
341
|
+
assert asdict(entry) == {"type": c.Type.FILE, "name": b"test", "sizes": [1, 2]}
|
|
342
|
+
assert entry._asdict() == asdict(entry)
|
|
343
|
+
assert asjson(entry) == '{"type": 1, "name": "dGVzdA==", "sizes": [1, 2]}'
|
|
344
|
+
assert json.dumps({"entries": [entry]}, cls=JSONEncoder) == f'{{"entries": [{asjson(entry)}]}}'
|
|
345
|
+
```
|
|
346
|
+
|
|
315
347
|
### Custom definition parsers
|
|
316
348
|
Don't like the C-like definition syntax? Write your own syntax parser!
|
|
317
349
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
dissect/cstruct/__init__.py,sha256=
|
|
1
|
+
dissect/cstruct/__init__.py,sha256=ti1VYFx1MtZuDcMks14haNvUw0IZYjRo346ChzhqPls,1647
|
|
2
2
|
dissect/cstruct/bitbuffer.py,sha256=aNzY4lkWe8ffAc8j7FODXzwZc8gZwbey77UosyoYCsw,2711
|
|
3
3
|
dissect/cstruct/compiler.py,sha256=0u58J5pB4izosBj8G4wiBI1JSAAgHFMLM63IaSG4YO4,54509
|
|
4
4
|
dissect/cstruct/cstruct.py,sha256=mJerzPouWV29RdK5LqjGitDDMvNKcadoT2hj1zpExnM,26551
|
|
5
5
|
dissect/cstruct/declarative.py,sha256=5ubPtOyy6L5gJMFaPcbW3xYbTijZGMp0XJGxBeed1nE,14505
|
|
6
|
-
dissect/cstruct/dump.py,sha256=
|
|
6
|
+
dissect/cstruct/dump.py,sha256=MxVJmn6v6cJs1Ud2FEmN0DaxKCUYOJk0YX4pWh1-1FQ,19013
|
|
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
|
|
@@ -20,13 +20,13 @@ dissect/cstruct/types/int.py,sha256=9RbdrkvAI28zATM97VVORQnras0XhLaVMtdPjJwpTH4,
|
|
|
20
20
|
dissect/cstruct/types/leb128.py,sha256=V97XTJT_Xbw1LpTwTN1OkjPHuQQAQUxd8OGuA6yEDmE,2387
|
|
21
21
|
dissect/cstruct/types/packed.py,sha256=NgCX0YWX8SH8z47atq0f39w9MBjU_eBFJtUisUWdO-M,3867
|
|
22
22
|
dissect/cstruct/types/pointer.py,sha256=FNuB9bfENWRebbKX-R6Vs6xWPMHepjvOhhomeUhr7I8,3985
|
|
23
|
-
dissect/cstruct/types/structure.py,sha256=
|
|
23
|
+
dissect/cstruct/types/structure.py,sha256=EfsYgRNMcwvnUm6PStPu7yK_9BtRiJTaBlXDj0Eua7Y,43076
|
|
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.
|
|
27
|
-
dissect_cstruct-5.0.
|
|
28
|
-
dissect_cstruct-5.0.
|
|
29
|
-
dissect_cstruct-5.0.
|
|
30
|
-
dissect_cstruct-5.0.
|
|
31
|
-
dissect_cstruct-5.0.
|
|
32
|
-
dissect_cstruct-5.0.
|
|
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,,
|
|
File without changes
|
{dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/licenses/COPYRIGHT
RENAMED
|
File without changes
|
{dissect_cstruct-5.0.dev18.dist-info → dissect_cstruct-5.0.dev19.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|