1

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?

ralien
  • 1,448
  • 11
  • 24

1 Answers1

0

I believe Rebuild is what you are looking for, in this instance.

From the example currently on Construct's docs:

>>> st = Struct(
...     "count" / Rebuild(Byte, len_(this.items)),
...     "items" / Byte[this.count],
... )
>>> st.build(dict(items=[1,2,3]))
b'\x03\x01\x02\x03'
kuanb
  • 1,618
  • 2
  • 20
  • 42