Let's say that I have a construct.Struct
like this:
my_struct = construct.Struct(
"data_length_in_bytes" / construct.Int32ub,
"login" / construct.CString("ascii"),
"password" / construct.CString("ascii"),
"foo" / construct.Int32ub,
"bar" / construct.Int32ub,
"baz" / construct.Int8ub
)
)
Is there a way to generate/calculate the content of the data_length_in_bytes
field dynamicly based on the size of the other field (plus the 4 bytes for itself)?
Or should I use a Struct without data_length_in_bytes
and then add/remove the field in after/before converting it?
Try using the Prefixed
class. It takes two arguments. The first is a Field
specifying how to store the size in bytes, and the second is what to store.
So in this case, you'd write:
my_struct = construct.Prefixed(construct.Int32ub, construct.Struct(
"login" / construct.CString("ascii"),
"password" / construct.CString("ascii"),
"foo" / construct.Int32ub,
"bar" / construct.Int32ub,
"baz" / construct.Int8ub
))