Skip to main content

Serialize, deserialize and represent packed binary data in a declarative way

Project description

xstruct

This module provides a solution to serialize, deserialize and represent packed binary data in a declarative manner. It is built on top and extends the capabilities of the standard struct module while presenting an higher-level object oriented interface similar to that of the familiar @dataclass decorator. It has optional support for embedded BSON data structures.

Now available on PyPI and GitHub

Installation

The following command should work on most systems:

$ pip install xstruct

Usage

This module provides a class decorator with an interface similar to dataclasses.dataclass.

Basic usage

from xstruct import struct, UInt16, Big

@struct(endianess=Big)
class UDPHeader:
    src_port: UInt16
    dst_port: UInt16
    length:   UInt16
    checksum: UInt16

xstruct provides pseudo-types for common signed and unsigned integer sizes, IEEE-754 floating point numbers, NUL terminated strings and optionally BSON documents.

Struct objects can be created by decoding binary data from a bytes-like object...

>>> UDPHeader.unpack(b"\x00\x00 1\x00\x00\x00{\x00\x00\x00L\x00\x00\x00\x00")
UDPHeader(src_port=8241, dst_port=123, length=76, checksum=0)

...or through the generated constructor, and can be serialized back to binary data.

>>> UDPHeader(src_port=8241, dst_port=123, length=76, checksum=0).pack()
b'\x00\x00 1\x00\x00\x00{\x00\x00\x00L\x00\x00\x00\x00'

Optional members

Default values can be specified for members at the tail end of a struct.

from xstruct import struct, Int32, Little

@struct(endianess=Little)
class SecondsOptional:
    member1: Int32
    member2: Int32 = 0

If a buffer ends prematurely during decoding, default values are used in place of missing struct members.

>>> SecondsOptional.decode(b"*\0\0\0")
SecondsOptional(member1=42, member2=0)

Members with a default value can also be omitted when creating a struct through the generated constructor.

>>> SecondsOptional(42)
SecondsOptional(member1=42, member2=0)

Struct inclusion

Structs can include other structs

from xstruct import struct, Int32, Int64, CString, Big

@struct(endianess=Big)
class Header:
    src:      Int32
    msg_type: Int32
    upd_time: Int64

@struct(endianess=Big)
class Message:
  header: Header
  msg:    CString

Encoding and decoding Message will work as expected.

Struct endianess

The endianess of the numeric members of the struct can optionally be selected by providing the endianess argument to the struct decorator. Valid options are Little, Big and Native. When left unspecified, endianess defaults to Native.

Future work

As of now, this library is fairly complete and in an usable state. Future work could add support for:

  • Decoding network addresses (IPv4, IPv6, MAC) to strings
  • Pascal strings
  • Arrays
  • Fixed width embedded binary payloads
  • Tail end padding
  • Decoding total struct size from/to designated member

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xstruct-1.3.4.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xstruct-1.3.4-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file xstruct-1.3.4.tar.gz.

File metadata

  • Download URL: xstruct-1.3.4.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7

File hashes

Hashes for xstruct-1.3.4.tar.gz
Algorithm Hash digest
SHA256 33b1eaceb672d3dd286bf59e5b67cf25e4a2dcf2b863b36ce0e561d2dea5370c
MD5 9768edf0a3c836a135e10456cd73a5c6
BLAKE2b-256 c6143326a38c82460b372a27dd1f54bb372672cdf087fb1cfaecc6a58a860462

See more details on using hashes here.

File details

Details for the file xstruct-1.3.4-py3-none-any.whl.

File metadata

  • Download URL: xstruct-1.3.4-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7

File hashes

Hashes for xstruct-1.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6399edfadff841c11d996c311c5e6d6dc93a420f9647a1d28c03bda2481ee1d8
MD5 a5079d4df5f5470529b569d2c5ebc1e8
BLAKE2b-256 fd8fc4f6faed724ffc5c210bf54f21944d33871cf92030d1f8423fd6de72623e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page