Skip to content

v2.8.1: Inline syntax & dynamic offset fix

Latest

Choose a tag to compare

@MatrixEditor MatrixEditor released this 08 Feb 07:43

Changes in this release introduce a new inline syntax that can be used to unpack structs (does not include annotated struct classes) from bytes or a stream:

stream = BytesIO(b"\xff\xff@\xe2\x01\x00")

# To affect the endianess, either use 
O_DEFAULT_ENDIAN.value = LittleEndian
# or creating a constant field that represents the typed struct
uint32le = LittleEndian + uint32

# to read/unpack values from a stream/buffer, just use the left shift operator
value1: int = uint32le << stream 
value2: int = uint16 << stream 
value3: str = String(10) << stream 

# Instead of using the special operator, all default struct classes provide
# wrapper functions for packing and unpacking:
value = uint8.from_bytes(stream)
assert uint8.to_bytes(value) == 0xFF

Note

To implement the from_bytes and to_bytes methods for annotated struct/bitfield/union classes, use the struct_factory.mixin type.


Added

  • New inline syntax using the << operator to automatically unpack structs from data
  • All default struct types now implement from_bytes, from_file and to_bytes for direct packing or unpacking
  • Add PackMixin and UnpackMixin to automatically implement the methods mentioned above for struct types

Changes

  • pack, unpack and sizeof were moved into their own module (no changed visibe when importing from caterpillar.models)

Removed

  • Drop support for 'x' format character in PyStructFormattedField

Fixes

  • Pre-computed conditions in the Field class incorrectly evaluated to true or false unconditionally (in case of a context-lambda). This issue has now been fixed in all places.
  • Fix some missing or wrong typing annotations
  • #57: allow dynamic offsets to be used again

New Contributors

Full Changelog: v2.8.0...v2.8.1