midstar / pycstruct

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

Nested struct #17

Closed vallsv closed 3 years ago

vallsv commented 3 years ago

Hi,

Thanks a lot for this lib. It saves me a lot of time :-)

I was working on a huge data structure used by a file format. Part of the description is using nested structs.

The parsing of such structure is fine, but it looks like there is no data in the end.

Tested with pycstruct 0.6.1

It's quite easy to refactor the c header definition to make it work, but you maybe care about this nested struct too?

Test

import pycstruct
from pprint import pprint

description = """

struct test1 {
    struct test1_1 {
        int a;
        int b;
        int c;
    } test1;
};

struct test2_1 {
    int a;
    int b;
    int c;
};

struct test2 {
    struct test2_1 test2;
};

"""

structs = pycstruct.parse_str(description)

data = b"\x00\x00\x00\x00\x02\x00\x00\x00\x04\x00\x00\x00"

test1_t = structs["test1"]
result = test1_t.deserialize(data)
pprint(result)

test2_t = structs["test2"]
result = test2_t.deserialize(data)
pprint(result)

Result

{'test1': {}}
{'test2': {'a': 0, 'b': 2, 'c': 4}}

While i am here, another small question: Have you think about named tuple for the result? So what it really could be reused like struct without string indexing. And should also use less memory.

midstar commented 3 years ago

Hi,

Thank you for your feedback. It was a bug in castxml, where the members of the "nested struct" (test1_1) was removed from the produced xml output. I have pycparser to use a different castxml output format that seems to do the trick. It might have unforseen side effects, but all unit tests pass using the new format.

Please try version 0.7.0 of pycstruct.

Regarding named tuples. This seems to be an interesting idea. As you write it would look more like using structs in the c language. However, I'm not sure how to handle unions with named tuples.

vallsv commented 3 years ago

Thanks a lot. It works perfectly now.

Concerning named tuple, i will open other issue.