Skip to main content

A powerful declarative symmetric parser/builder for binary data

Project description

Malstruct is a powerful declarative and symmetrical parser and builder for binary data that was forked from construct as of release 2.10.70.

Instead of writing imperative code to parse a piece of data, you declaratively define a data structure that describes your data. As this data structure is not code, you can use it in one direction to parse data into Pythonic objects, and in the other direction, to build objects into binary data.

The library provides both simple, atomic constructs (such as integers of various sizes), as well as composite ones which allow you form hierarchical and sequential structures of increasing complexity. Construct features bit and byte granularity, easy debugging and testing, an easy-to-extend subclass system, and lots of primitive constructs to make your work easier:

  • Fields: raw bytes or numerical types

  • Structs and Sequences: combine simpler constructs into more complex ones

  • Bitwise: splitting bytes into bit-grained fields

  • Adapters: change how data is represented

  • Arrays/Ranges: duplicate constructs

  • Meta-constructs: use the context (history) to compute the size of data

  • If/Switch: branch the computational path based on the context

  • On-demand (lazy) parsing: read and parse only what you require

  • Pointers: jump from here to there in the data stream

  • Tunneling: prefix data with a byte count or compress it

Example

A Struct is a collection of ordered, named fields:

>>> format = Struct(
...     "signature" / Const(b"BMP"),
...     "width" / Int8ub,
...     "height" / Int8ub,
...     "pixels" / Array(this.width * this.height, Byte),
... )
>>> format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13]))
b'BMP\x03\x02\x07\x08\t\x0b\x0c\r'
>>> format.parse(b'BMP\x03\x02\x07\x08\t\x0b\x0c\r')
Container(signature=b'BMP')(width=3)(height=2)(pixels=[7, 8, 9, 11, 12, 13])

A Sequence is a collection of ordered fields, and differs from Array and GreedyRange in that those two are homogenous:

>>> format = Sequence(PascalString(Byte, "utf8"), GreedyRange(Byte))
>>> format.build([u"lalaland", [255,1,2]])
b'\nlalaland\xff\x01\x02'
>>> format.parse(b"\x004361789432197")
['', [52, 51, 54, 49, 55, 56, 57, 52, 51, 50, 49, 57, 55]]

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

malstruct-2.10.71.tar.gz (131.2 kB view details)

Uploaded Source

Built Distribution

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

malstruct-2.10.71-py3-none-any.whl (112.9 kB view details)

Uploaded Python 3

File details

Details for the file malstruct-2.10.71.tar.gz.

File metadata

  • Download URL: malstruct-2.10.71.tar.gz
  • Upload date:
  • Size: 131.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for malstruct-2.10.71.tar.gz
Algorithm Hash digest
SHA256 091967e1aeb2eb193ae5542a62af21d52e8760cec2893053ad19b7795957505e
MD5 8787026213da3c70fe1e4db978376485
BLAKE2b-256 621118e0ccec6f14f1e3df2815927843d3ec1feaae28ce18472cd8ed28e242d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for malstruct-2.10.71.tar.gz:

Publisher: main.yml on ciphertechsolutions/malstruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file malstruct-2.10.71-py3-none-any.whl.

File metadata

  • Download URL: malstruct-2.10.71-py3-none-any.whl
  • Upload date:
  • Size: 112.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for malstruct-2.10.71-py3-none-any.whl
Algorithm Hash digest
SHA256 ce63d10711ee2ddca8f9656d5e06b0952c64762d91a936e612d90a361c147200
MD5 e68373bde095f8f45588ccf725fd5b9b
BLAKE2b-256 b4ab8e9a50bc41b4dfbd8cf4b767ac32b7ccc8e9de300821302580c20be8f0a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for malstruct-2.10.71-py3-none-any.whl:

Publisher: main.yml on ciphertechsolutions/malstruct

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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