Skip to main content

Create structured binary data easily.

Project description

Coverage Pytest

BitFactory

A module designed to facilitate quick and easy structured binary data creation allowing for the automatic adjustments of types such as lengths, checksums, etc of the modeled data.

Install

pip install bitfactory

Usage

An example for data strucured as follows:

  • Type: Unsigned Byte
  • Length: Unsigned Big-Endian Short that encompasses the Data and Checksum
  • Data:
    • Unsigned 32 bit Int
    • Unsigned Byte
  • Checksum: Unsigned Short that is calculated over the above Data portion only
def csum(data: bytes) -> int:
    checksum = 0
    for value in data:
        checksum += value
    return checksum

data = BFContainer()
data.type = BFUInt8(1)
data.body = BFLength(BFUInt16(endian=BFEndian.BIG), BFContainer())
data.body.checksumed = BFContainer()
data.body.checksumed.data = BFUInt32(0xAABBCCDD)
data.body.checksumed.data2 = BFUInt8(10)
data.body.checksum = BFCallableRef(BFUInt16(), csum, "checksumed")
assert b"\x01\x00\x07\xdd\xcc\xbb\xaa\n\x18\x03" == data.pack()

>>> print(data)
+None
| |- Unsigned Byte 0x01 : type
| +body length: 0x7
|  +checksumed
|   |- Unsigned Long 0xAABBCCDD : data
|   |- Unsigned Byte 0x0A : data2
|  +checksum value: 0x318

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

bitfactory-0.1.0.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

bitfactory-0.1.0-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

Supported by

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