ConsHex 0.2.1
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.
Metadata
Release files for conshex 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| conshex-0.2.3.tar.gz | 89.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| conshex-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 158.0 kB
Release files / conshex-0.2.3.tar.gz
| Download URL | conshex-0.2.3.tar.gz |
|---|---|
| Size | 89.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
202a4a38d3fd74dcf220500c603d8f592e29a2b3aa525e242bba6a6c3338d713
|
|
BLAKE2b-256 checksum How to use checksums |
fbab198708607625b29b44bdf1ec60b8f267cc4a67f34479821a1227c456e13e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / conshex-0.2.3-py3-none-any.whl
| Download URL | conshex-0.2.3-py3-none-any.whl |
|---|---|
| Size | 68.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5706a23eb82e6990d7aaa8981fae81b002b9c7c7f9c7c2e3ad9b68c558ceee84
|
|
BLAKE2b-256 checksum How to use checksums |
40a74d26e87d104c30c8e73e98fee529749564cccb9637fb049214b3b8da1c25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Security for conshex 0.2.3
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.