midstar / pycstruct

A python library for reading and writing binary data similar to what is done in C language structs
MIT License
23 stars 4 forks source link

Unable to parse C header file #13

Closed hecko closed 3 years ago

hecko commented 3 years ago
Traceback (most recent call last):
  File "/Users/marcel/git/packet-tester/./moje.py", line 8, in <module>
    d = pycstruct.parse_file('packet_structs.h')
  File "/usr/local/lib/python3.9/site-packages/pycstruct/cparser.py", line 484, in parse_file
    type_meta = castxml_parser.parse()
  File "/usr/local/lib/python3.9/site-packages/pycstruct/cparser.py", line 96, in parse
    supported_types[id] = self._parse_bitfield(xml_struct_or_bitfield)
  File "/usr/local/lib/python3.9/site-packages/pycstruct/cparser.py", line 182, in _parse_bitfield
    member['bits'] = int(field.attrib['bits'])
KeyError: 'bits'

Its very hard for me to add any details as the code is under NDA, but the bits definition play role somehow (havent found any pattern yet)

Bit fields are defined as such:

typedef struct __attribute__((packed)) {
  uint8_t asd;
  MetaData meta;
  uint16_t asdasd;
  uint32_t sdfgdfg;
  uint8_t asdasdasdasd;
  uint32_t erofmweroi;
  uint8_t status : 1; /**< example bit field */
  uint8_t erfkpoksdf;
} BinaryPacket;
midstar commented 3 years ago

Hi. Unfortunately pycstruct don't support mix of "bitfields" and "normal" struct members. To be honest I have not seen that mix before and did not even know it was supported by the C standard. It might be something we could add support for but it would take some time and require quite large changes in how pycstruct handles structs/bitfields.

hecko commented 3 years ago

Well this is a no-go for me then :( I'll probably write something myself. The use case is obvious - I have lots of C header files that are used in embedded programming and I need to write lots of Python code without the requirement of manually reflecting changes in C headers into Python dictionaries. I really like your approach though.

Is there an alternative way of bitfield definition that will be correctly parsed by pystruct? Splitting the above example into two structs with the bitfield in a substruct alone?

midstar commented 3 years ago

Hi again. I have now included support for mixed bitfields in structs. Please have a try. Version is 0.6.0.

Note that you might get problems with alignment depending on your struct layout and compiler/architecture. See:

https://pycstruct.readthedocs.io/en/latest/limitations.html

Let me know if it works out for you.

hecko commented 3 years ago

Yup! This works like a charm! Great work!