Skip to main content

Malstruct

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]]

Included in malstruct is the pecon (PE file reconstruction utility) package. Please see the pecon API documentation for more information.

Changelog

The format is based on Keep a Changelog, and this project adheres to Calendar Versioning with the schema MAJOR.MINOR.YYYY0M0D.

3.0.20260429 - 2026-04-29

Changed

  • Split out core functionality across adapters, alignment, analysis, bytes_, conditional, exceptions, expr, helpers, integers, lazy, mappings, miscellaneous, stream, strings, and transforms

  • Moved binary file analysis to malstruct.binaryfiles

  • Moved remaining malstruct.utils functionality to base level

  • Added pecon utility as an installed package

  • Move from “flat” layout to “src” layout

  • Use pyproject.toml configuration file for packaging

Removed

  • Removed usage of __all__ in init

  • Removed usage of compilation feature and benchmarks

  • Removed py3compat functionality

  • Removed pefileutils and elffileutils

  • Removed functionality from machoutils unrelated to malstructs/adapters

2.10.71

Changed

  • Reverted default behavior changed by https://github.com/construct/construct/pull/1015
    • OffsettedEnd, Prefixed, FixedSize, NullTerminated, NullStriped, ProcessXor use offsets relative to the last occurrence of these subconstructs

    • To use offsets relative to the beginning of the stream set absolute=True when constructing these constructs

  • Moved optional dependencies to required dependencies

Metadata

Release files for malstruct 3.0.20260429

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for malstruct 3.0.20260429
File Size Uploaded Provenance Actions
malstruct-3.0.20260429.tar.gz 123.8 kB With provenance Details for malstruct-3.0.20260429.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for malstruct 3.0.20260429
File Interpreter ABI Platform Actions
malstruct-3.0.20260429-py3-none-any.whl Python 3 none any Details for malstruct-3.0.20260429-py3-none-any.whl

Total release size: 235.8 kB

Release files / malstruct-3.0.20260429.tar.gz

Download URL malstruct-3.0.20260429.tar.gz
Size 123.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3b1653e004df79490754c37932fb550961f140d2e429d77311174c5a408f5556
BLAKE2b-256 checksum
How to use checksums
5c7f936cdee8a9d94c3695b26376a373df1a9884ac52d6447a36f8f4c5a9c74c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 29, 2026.

Transparency log

Release files / malstruct-3.0.20260429-py3-none-any.whl

Download URL malstruct-3.0.20260429-py3-none-any.whl
Size 112.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a14d4fb2311fb876d61edbc802d5d0bd65b66f8deb16cea2bd24259bd25367d9
BLAKE2b-256 checksum
How to use checksums
007e3c9f73c2908dd79ddc1ad3e989c851b409fa78debf4f2cd9e9663c17a095
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 29, 2026.

Transparency log

Security for malstruct 3.0.20260429

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for malstruct-3.0.20260429.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page