Skip to content

Commit 96644dd

Browse files
authored
Document dynamic length fields feature in README (#148)
1 parent d967590 commit 96644dd

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ The API to access enum and flag members and their values in the same way as the
210210
### Custom types
211211
You can implement your own types by subclassing `BaseType`, and adding them to your cstruct instance with `add_custom_type(name, type, size, alignment, ...)`
212212

213+
### Dynamic length fields
214+
To read all remaining data in the provided buffer/stream, you can use the magic `EOF` size variable. This will automatically expand until the end of the stream has been reached and works for any field type. For example:
215+
216+
```python
217+
from dissect.cstruct import cstruct
218+
219+
eof_def = """
220+
struct example {
221+
uint32 magic;
222+
char data[EOF];
223+
};
224+
"""
225+
226+
c_eof = cstruct().load(eof_def)
227+
228+
example = c_eof.example(b"9\x05\x00\x00arbitrary length data")
229+
assert example.magic == 1337
230+
assert example.data == b"arbitrary length data"
231+
```
232+
213233
### Custom definition parsers
214234
Don't like the C-like definition syntax? Write your own syntax parser!
215235

0 commit comments

Comments
 (0)