Here's a simple tag-length-value structure defined using the construct
library for Python:
Tlv = Struct(
'tag' / Int16ub,
'length' / Int32ub,
'value' / Array(this.length, Byte)
)
if __name__ == '__main__':
tlv_data = dict(tag=1, length=5, value=b'\xFF\xFF\xFF\xFF\xFF')
encoded = Tlv.build(tlv_data)
print(encoded)
decoded = Tlv.parse(encoded)
print(decoded)
While the example correctly encodes and decodes the data, I need to know the length of the value
field in advance and supply it via the length
parameter.
Is there an approach that allows me to initialize the structure by passing the bare minimum, i.e., tlv_data = dict(tag=1, value=b'\xFF\xFF\xFF\xFF\xFF')
and have it figure out on its own what the adequate value of the length
field should be?
At the same time, when I parse raw data, I expect it to extract the length from the raw data and then ensure that the value
is indeed that many bytes long.
Is this supposed to work in principle, or am I misunderstanding the ideology of construct
?