From f50af118fb8ced078da84ac2652bcd1972a7ffc1 Mon Sep 17 00:00:00 2001 From: Luke Paris Date: Sat, 7 Mar 2026 15:31:23 +0100 Subject: [PATCH 1/6] Document dynamic length fields feature Add documentation for dynamic length fields in README. --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 218b7efe..c4bc1032 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,22 @@ The API to access enum and flag members and their values in the same way as the ### Custom types You can implement your own types by subclassing `BaseType`, and adding them to your cstruct instance with `add_custom_type(name, type, size, alignment, ...)` +### Dynamic length fields +The API includes a magic `EOF` size which will make the parser read all remaining data fed to it. For example: + +```python +eof_struct = cstruct().load(""" +struct example { + uint32 magic; + char data[EOF]; +} +""") + +example = eof_struct.example(b"9\x05\x00\x00arbitrary length data") +print(f"{example.magic=}") # >> example.magic=1337 +print(f"{example.data=}") # >> example.data=b'arbitrary length data' +``` + ### Custom definition parsers Don't like the C-like definition syntax? Write your own syntax parser! From 92b86b7c19187bc16673133ab5ab2b7e0bd8da0c Mon Sep 17 00:00:00 2001 From: Luke Paris Date: Sat, 7 Mar 2026 15:34:23 +0100 Subject: [PATCH 2/6] Make dynamic length be more in line with the other examples --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c4bc1032..2819269f 100644 --- a/README.md +++ b/README.md @@ -214,16 +214,20 @@ You can implement your own types by subclassing `BaseType`, and adding them to y The API includes a magic `EOF` size which will make the parser read all remaining data fed to it. For example: ```python -eof_struct = cstruct().load(""" +from dissect.cstruct import cstruct + +eof_def = """ struct example { uint32 magic; char data[EOF]; } -""") +""" + +eof_struct = cstruct().load(eof_def) example = eof_struct.example(b"9\x05\x00\x00arbitrary length data") -print(f"{example.magic=}") # >> example.magic=1337 -print(f"{example.data=}") # >> example.data=b'arbitrary length data' +assert example.magic == 1337 +assert example.data == b"arbitrary length data" ``` ### Custom definition parsers From c7286ace1b642c5ada9c100674ee8027dd691d5b Mon Sep 17 00:00:00 2001 From: Luke Paris Date: Tue, 10 Mar 2026 09:49:52 +0100 Subject: [PATCH 3/6] Update README.md Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2819269f..76d0e721 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,7 @@ The API to access enum and flag members and their values in the same way as the You can implement your own types by subclassing `BaseType`, and adding them to your cstruct instance with `add_custom_type(name, type, size, alignment, ...)` ### Dynamic length fields -The API includes a magic `EOF` size which will make the parser read all remaining data fed to it. For example: +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: ```python from dissect.cstruct import cstruct From 09d89de5bfd05ea3805883bf28d0bb867ece0507 Mon Sep 17 00:00:00 2001 From: Luke Paris Date: Tue, 10 Mar 2026 09:49:59 +0100 Subject: [PATCH 4/6] Update README.md Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 76d0e721..509258c8 100644 --- a/README.md +++ b/README.md @@ -223,7 +223,7 @@ struct example { } """ -eof_struct = cstruct().load(eof_def) +c_eof = cstruct().load(eof_def) example = eof_struct.example(b"9\x05\x00\x00arbitrary length data") assert example.magic == 1337 From 3c691c3b825cc709d9ad49f12f2a362fa38f3a38 Mon Sep 17 00:00:00 2001 From: Luke Paris Date: Tue, 10 Mar 2026 09:50:04 +0100 Subject: [PATCH 5/6] Update README.md Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 509258c8..79b5d096 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ struct example { c_eof = cstruct().load(eof_def) -example = eof_struct.example(b"9\x05\x00\x00arbitrary length data") +example = c_eof.example(b"9\x05\x00\x00arbitrary length data") assert example.magic == 1337 assert example.data == b"arbitrary length data" ``` From cd9b11b5ddc3de4a3279b7ef7e5092bbf8deab40 Mon Sep 17 00:00:00 2001 From: Luke Paris Date: Tue, 10 Mar 2026 09:50:12 +0100 Subject: [PATCH 6/6] Update README.md Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 79b5d096..ef4ab736 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ eof_def = """ struct example { uint32 magic; char data[EOF]; -} +}; """ c_eof = cstruct().load(eof_def)