Skip to main content

A powerful declarative symmetric parser/builder for binary data with XML de- and encoding

Project description

ConsHex is a powerful declarative and symmetrical parser and builder for binary data. It is a fork of Construct 2.1, which removes the parser generator features, but adds preprocessing and XML de- and encoding. It is build mainly with reverse engineering data formats in mind.

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

Most constructs can be build into XML and parsed back from XML:

>>> s = Struct(
...    "a" / Int32ul,
...    "b" / Int32ul,
...    "s" / Struct(
...        "c" / Int32ul,
...        "d" / Int32ul,
...    ),
...    )
>>> data = {"a": 1, "b": 2, "s": {"c": 3, "d": 4}}
>>> xml = s.toET(obj=data, name="test")
>>> assert(ET.tostring(xml) == b'<test a="1" b="2"><s c="3" d="4" /></test>')
>>> s = "test" / Struct(
...     "a" / Int32ul,
...     "b" / Int32ul,
... )
>>> xml = ET.fromstring(b'<test a="1" b="2" />')
>>> obj = s.fromET(xml=xml)
>>> assert(obj == {"a": 1, "b": 2})

However some constructs, like Switch or FocusedSeq have some caveats, because they use the XML tag name for identifying the corresponding construct.

This is mainly build for easy and quick describing of datastructures with an easy, human readable and changeable XML representation, rather than completeness of all possible constructs.

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

conshex-0.2.2.tar.gz (89.1 kB view details)

Uploaded Source

Built Distribution

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

conshex-0.2.2-py3-none-any.whl (68.5 kB view details)

Uploaded Python 3

File details

Details for the file conshex-0.2.2.tar.gz.

File metadata

  • Download URL: conshex-0.2.2.tar.gz
  • Upload date:
  • Size: 89.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for conshex-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7776d5e28695c5551e2eab8b10b9c73ec9ee66ca8ed7f148db3f227e6531b8bc
MD5 f71ab347a9a64eb5a6f12ca1357a3da0
BLAKE2b-256 498e1e414b6c573a3d9205b169daa7d4684713d76ac2b010f0a1a1f4fd296bf2

See more details on using hashes here.

File details

Details for the file conshex-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: conshex-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 68.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for conshex-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0c986c01e18caf249123faf01c6a6f93b098d04672a0772b639072caeec06f01
MD5 6cc6079ebbebf16f79bd5f1974578c3e
BLAKE2b-256 44fa91151c6b9b82f9023e33364f8fc89ae6100f828942438056d2eb7351f45b

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