Skip to main content

Declaratively define and parse C data structures

Project description

c_struct_data_parser

Intended to facilitate declarative, dynamic definition of C data structures. The library will autogenerate a parser which you can connect to a BytesReader or some IO based variant.

You're better off looking at https://github.com/fox-it/dissect.cstruct

Examples from test_basic.py:

Struct2Int = create_struct_definition(
    "Struct2Int",
    {
        "value_a": Int4Definition,
        "value_b": Int4Definition,
    },
)


def test_struct2Int() -> None:
    value_a = 0x12345678
    value_b = 0x99887766
    bs = b"".join(map(int_to_le_bytes_4, [value_a, value_b]))
    bytes_reader = BytesReader(address=0, bs=bs)
    struct_2_int, new_reader = Struct2Int.parser(bytes_reader)
    assert struct_2_int.value_a.value == value_a
    assert struct_2_int.value_b.value == value_b

    print(struct_2_int)


BitFieldsExample = create_bit_fields_definition(
    "BitFieldsExample",
    Int4Definition,
    {
        "field_a": 4,
        "reserved_1": 5,
        "field_b": 5,
    },
)


def test_bit_fields() -> None:
    val = 0x123456
    bs = int_to_le_bytes_4(val)
    bytes_reader = BytesReader(address=0, bs=bs)
    bit_fields_example, new_reader = BitFieldsExample.parser(bytes_reader)
    assert bit_fields_example.field_a.value == val & 0xF
    assert bit_fields_example.field_b.value == (val >> 9) & 0x1F
    print(bit_fields_example)


Struct2IntPointer = create_pointer_definition(
    Struct2Int,
    Int4Definition,
)


def test_pointer_type() -> None:
    value_a = 0x12345678
    value_b = 0x99887766
    bs_struct = b"".join(map(int_to_le_bytes_4, [value_a, value_b]))
    bs_pointer = int_to_le_bytes_4(0x2000)
    bs = b"".join([bs_struct, bs_pointer])
    bytes_reader = BytesReader(address=0x2000, bs=bs)

    struct_2_int, new_reader = Struct2Int.parser(bytes_reader)
    assert struct_2_int.value_a.value == value_a
    assert struct_2_int.value_b.value == value_b

    p_struct_2_int, new_reader_2 = Struct2IntPointer.parser(new_reader)

    print(p_struct_2_int)

    struct_2_int_copy = p_struct_2_int.resolve(new_reader_2)

    assert struct_2_int_copy.value_a.value == value_a
    assert struct_2_int_copy.value_b.value == value_b

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

c_struct_data_parser-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

c_struct_data_parser-0.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file c_struct_data_parser-0.2.0.tar.gz.

File metadata

File hashes

Hashes for c_struct_data_parser-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f4aa3b04c87221c05aec47acd2680ece51ca592a185fec7cc9a899b4122ee6e0
MD5 e77f6617c8d63a2f67c8cfb13b24e51c
BLAKE2b-256 f697d619d60f327e9b45360f74a9b3235b7b7aea214f6ca2923873f7d6d32a34

See more details on using hashes here.

File details

Details for the file c_struct_data_parser-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for c_struct_data_parser-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 670d174a6b076e8803211a475f565cfb177774789a7ec374c2c3244550fa41ee
MD5 1c0fb90f5d8bf7d6a8000aee0f28aee6
BLAKE2b-256 54d390e2e700934b3ce4706c28dcac89ce67d19c75c0372742010edbe40f278d

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