dissect.cstruct 5.0.dev17__tar.gz → 5.0.dev19__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.dev17 → dissect_cstruct-5.0.dev19}/CHANGELOG.md +5 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/PKG-INFO +88 -2
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/README.md +87 -1
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/__init__.py +4 -0
- dissect_cstruct-5.0.dev19/dissect/cstruct/declarative.py +460 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/dump.py +3 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/base.py +8 -2
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/structure.py +90 -2
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/PKG-INFO +88 -2
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/SOURCES.txt +2 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/scm_file_list.json +2 -0
- dissect_cstruct-5.0.dev19/dissect.cstruct.egg-info/scm_version.json +8 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/pyproject.toml +3 -0
- dissect_cstruct-5.0.dev19/tests/test_declarative.py +522 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_base.py +10 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_structure.py +145 -1
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_union.py +45 -1
- dissect_cstruct-5.0.dev17/dissect.cstruct.egg-info/scm_version.json +0 -8
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/.git-blame-ignore-revs +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/.gitattributes +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/COPYRIGHT +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/LICENSE +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/MANIFEST.in +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/bitbuffer.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/compiler.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/cstruct.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/exception.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/expression.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/lexer.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/parser.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/tools/__init__.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/tools/stubgen.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/__init__.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/char.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/enum.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/flag.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/int.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/leb128.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/packed.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/pointer.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/void.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/types/wchar.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect/cstruct/util.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/entry_points.txt +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/requires.txt +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/dissect.cstruct.egg-info/top_level.txt +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/examples/disk.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/examples/mirai.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/examples/pe.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/examples/protobuf.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/examples/secdesc.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/setup.cfg +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/__init__.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/_data/testdef.txt +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/_docs/Makefile +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/_docs/__init__.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/_docs/conf.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/_docs/index.rst +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/conftest.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_align.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_annotations.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_basic.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_benchmark.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_bitbuffer.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_bitfield.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_compiler.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_ctypes.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_dump.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_expression.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_lexer.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_parser.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_tools_stubgen.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_char.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_custom.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_enum.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_flag.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_int.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_leb128.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_packed.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_pointer.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_void.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_types_wchar.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/test_util.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tests/util.py +0 -0
- {dissect_cstruct-5.0.dev17 → dissect_cstruct-5.0.dev19}/tox.ini +0 -0
|
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- Declarative syntax for structures and unions in `dissect.cstruct.declarative`, using classes with type annotations.
|
|
12
|
+
- `asdict()` and the `_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()`.
|
|
13
|
+
|
|
9
14
|
### Changed
|
|
10
15
|
|
|
11
16
|
- Optimize storage of field sizes.
|
|
@@ -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
|
|
@@ -129,7 +129,7 @@ assert instance.dumps() == b"\x05loremipsum\x05\x00\x00\x00"
|
|
|
129
129
|
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
-
By default, all structures are compiled into classes that provide optimised performance. You can disable this by passing a `compiled=False` keyword argument to the `.load()` call. You can
|
|
132
|
+
By default, all structures are compiled into classes that provide optimised performance. You can disable this by passing a `compiled=False` keyword argument to the `.load()` call. You can inspect the resulting source code by accessing the source attribute of the structure: `print(c_parser.some_struct._read.__source__)` or `print(c_parser.some_struct._write_.__source__)`.
|
|
133
133
|
|
|
134
134
|
More examples can be found in the `examples` directory.
|
|
135
135
|
|
|
@@ -137,6 +137,60 @@ More examples can be found in the `examples` directory.
|
|
|
137
137
|
### Structure parsing
|
|
138
138
|
Write simple C-like structures and use them to parse binary data, as can be seen in the examples.
|
|
139
139
|
|
|
140
|
+
### Declarative structure definitions
|
|
141
|
+
Instead of with a C-like definition, structures can also be defined in Python by subclassing `Struct` (or `Union`) and annotating the fields. The resulting classes are regular cstruct structures.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from typing import Annotated
|
|
145
|
+
|
|
146
|
+
from dissect.cstruct import cstruct
|
|
147
|
+
from dissect.cstruct.declarative import EOF, Struct, char, field, pointer, uint8, uint16, uint32
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class Header(Struct):
|
|
151
|
+
magic: uint8[4]
|
|
152
|
+
size: uint32
|
|
153
|
+
data: char["size"] # Array sizes can refer to other fields
|
|
154
|
+
flags: Annotated[uint16, field(bits=4)] # Or field(uint16, bits=4)
|
|
155
|
+
trailer: char[EOF] # Reads until the end of the data
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
data = b"ohai\x0c\x00\x00\x00KUSJESVANSRT\x05\x00<3"
|
|
159
|
+
header = Header(data)
|
|
160
|
+
assert header.data == b"KUSJESVANSRT"
|
|
161
|
+
assert header.flags == 5
|
|
162
|
+
assert header.trailer == b"<3"
|
|
163
|
+
assert header.dumps() == data
|
|
164
|
+
|
|
165
|
+
cs = cstruct(endian=">")
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class Node(Struct, cs=cs):
|
|
169
|
+
value: uint32
|
|
170
|
+
next: pointer["Node"] # A structure can point to itself
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class NamedNode(Node): # A subclass starts with the fields of its parent
|
|
174
|
+
name: char[None] # A list index of None means null terminated
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
cs.load("struct list { uint32 count; NamedNode head; };") # Usable in C-like definitions
|
|
178
|
+
|
|
179
|
+
node = cs.list(b"\x00\x00\x00\x01" + b"\x00\x00\x00\x07" + b"\x00" * 8 + b"<3\x00").head
|
|
180
|
+
assert node.value == 7
|
|
181
|
+
assert node.name == b"<3"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Each structure is registered in a cstruct instance, which can be passed with the `cs` class keyword argument, together with `align=True` for aligned structures. Without one, a structure uses the cstruct instance of the first bound type or structure in its annotations, or a new cstruct instance.
|
|
185
|
+
|
|
186
|
+
> [!NOTE]
|
|
187
|
+
> Field annotations are evaluated when the class is created. If your project uses Ruff's `flake8-type-checking` (`TC`) rules, add `Struct` and `Union` to the runtime-evaluated base classes, so that imports only used in field annotations are not moved into a `TYPE_CHECKING` block:
|
|
188
|
+
>
|
|
189
|
+
> ```toml
|
|
190
|
+
> [tool.ruff.lint.flake8-type-checking]
|
|
191
|
+
> runtime-evaluated-base-classes = ["dissect.cstruct.declarative.Struct", "dissect.cstruct.declarative.Union"]
|
|
192
|
+
> ```
|
|
193
|
+
|
|
140
194
|
### Type parsing
|
|
141
195
|
Aside from loading structure definitions, any of the supported types can be used individually for parsing data. For example, the following is all supported:
|
|
142
196
|
|
|
@@ -258,6 +312,38 @@ assert example.magic == 1337
|
|
|
258
312
|
assert example.data == b"arbitrary length data"
|
|
259
313
|
```
|
|
260
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
|
+
|
|
261
347
|
### Custom definition parsers
|
|
262
348
|
Don't like the C-like definition syntax? Write your own syntax parser!
|
|
263
349
|
|
|
@@ -101,7 +101,7 @@ assert instance.dumps() == b"\x05loremipsum\x05\x00\x00\x00"
|
|
|
101
101
|
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
By default, all structures are compiled into classes that provide optimised performance. You can disable this by passing a `compiled=False` keyword argument to the `.load()` call. You can
|
|
104
|
+
By default, all structures are compiled into classes that provide optimised performance. You can disable this by passing a `compiled=False` keyword argument to the `.load()` call. You can inspect the resulting source code by accessing the source attribute of the structure: `print(c_parser.some_struct._read.__source__)` or `print(c_parser.some_struct._write_.__source__)`.
|
|
105
105
|
|
|
106
106
|
More examples can be found in the `examples` directory.
|
|
107
107
|
|
|
@@ -109,6 +109,60 @@ More examples can be found in the `examples` directory.
|
|
|
109
109
|
### Structure parsing
|
|
110
110
|
Write simple C-like structures and use them to parse binary data, as can be seen in the examples.
|
|
111
111
|
|
|
112
|
+
### Declarative structure definitions
|
|
113
|
+
Instead of with a C-like definition, structures can also be defined in Python by subclassing `Struct` (or `Union`) and annotating the fields. The resulting classes are regular cstruct structures.
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from typing import Annotated
|
|
117
|
+
|
|
118
|
+
from dissect.cstruct import cstruct
|
|
119
|
+
from dissect.cstruct.declarative import EOF, Struct, char, field, pointer, uint8, uint16, uint32
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class Header(Struct):
|
|
123
|
+
magic: uint8[4]
|
|
124
|
+
size: uint32
|
|
125
|
+
data: char["size"] # Array sizes can refer to other fields
|
|
126
|
+
flags: Annotated[uint16, field(bits=4)] # Or field(uint16, bits=4)
|
|
127
|
+
trailer: char[EOF] # Reads until the end of the data
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
data = b"ohai\x0c\x00\x00\x00KUSJESVANSRT\x05\x00<3"
|
|
131
|
+
header = Header(data)
|
|
132
|
+
assert header.data == b"KUSJESVANSRT"
|
|
133
|
+
assert header.flags == 5
|
|
134
|
+
assert header.trailer == b"<3"
|
|
135
|
+
assert header.dumps() == data
|
|
136
|
+
|
|
137
|
+
cs = cstruct(endian=">")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class Node(Struct, cs=cs):
|
|
141
|
+
value: uint32
|
|
142
|
+
next: pointer["Node"] # A structure can point to itself
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class NamedNode(Node): # A subclass starts with the fields of its parent
|
|
146
|
+
name: char[None] # A list index of None means null terminated
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
cs.load("struct list { uint32 count; NamedNode head; };") # Usable in C-like definitions
|
|
150
|
+
|
|
151
|
+
node = cs.list(b"\x00\x00\x00\x01" + b"\x00\x00\x00\x07" + b"\x00" * 8 + b"<3\x00").head
|
|
152
|
+
assert node.value == 7
|
|
153
|
+
assert node.name == b"<3"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Each structure is registered in a cstruct instance, which can be passed with the `cs` class keyword argument, together with `align=True` for aligned structures. Without one, a structure uses the cstruct instance of the first bound type or structure in its annotations, or a new cstruct instance.
|
|
157
|
+
|
|
158
|
+
> [!NOTE]
|
|
159
|
+
> Field annotations are evaluated when the class is created. If your project uses Ruff's `flake8-type-checking` (`TC`) rules, add `Struct` and `Union` to the runtime-evaluated base classes, so that imports only used in field annotations are not moved into a `TYPE_CHECKING` block:
|
|
160
|
+
>
|
|
161
|
+
> ```toml
|
|
162
|
+
> [tool.ruff.lint.flake8-type-checking]
|
|
163
|
+
> runtime-evaluated-base-classes = ["dissect.cstruct.declarative.Struct", "dissect.cstruct.declarative.Union"]
|
|
164
|
+
> ```
|
|
165
|
+
|
|
112
166
|
### Type parsing
|
|
113
167
|
Aside from loading structure definitions, any of the supported types can be used individually for parsing data. For example, the following is all supported:
|
|
114
168
|
|
|
@@ -230,6 +284,38 @@ assert example.magic == 1337
|
|
|
230
284
|
assert example.data == b"arbitrary length data"
|
|
231
285
|
```
|
|
232
286
|
|
|
287
|
+
### Converting to dicts and JSON
|
|
288
|
+
`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.
|
|
289
|
+
|
|
290
|
+
`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()`.
|
|
291
|
+
|
|
292
|
+
```python
|
|
293
|
+
import json
|
|
294
|
+
|
|
295
|
+
from dissect.cstruct import JSONEncoder, asdict, asjson, cstruct
|
|
296
|
+
|
|
297
|
+
c_def = """
|
|
298
|
+
enum Type : uint8 {
|
|
299
|
+
FILE = 1,
|
|
300
|
+
DIRECTORY = 2
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
struct entry {
|
|
304
|
+
Type type;
|
|
305
|
+
char name[4];
|
|
306
|
+
uint32 sizes[2];
|
|
307
|
+
};
|
|
308
|
+
"""
|
|
309
|
+
|
|
310
|
+
c = cstruct().load(c_def)
|
|
311
|
+
|
|
312
|
+
entry = c.entry(b"\x01test\x01\x00\x00\x00\x02\x00\x00\x00")
|
|
313
|
+
assert asdict(entry) == {"type": c.Type.FILE, "name": b"test", "sizes": [1, 2]}
|
|
314
|
+
assert entry._asdict() == asdict(entry)
|
|
315
|
+
assert asjson(entry) == '{"type": 1, "name": "dGVzdA==", "sizes": [1, 2]}'
|
|
316
|
+
assert json.dumps({"entries": [entry]}, cls=JSONEncoder) == f'{{"entries": [{asjson(entry)}]}}'
|
|
317
|
+
```
|
|
318
|
+
|
|
233
319
|
### Custom definition parsers
|
|
234
320
|
Don't like the C-like definition syntax? Write your own syntax parser!
|
|
235
321
|
|
|
@@ -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",
|