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

Alignment based on OS #40

Closed agat-asensus closed 6 months ago

agat-asensus commented 1 year ago

Hi,

I am using pycstruct.parse_file, to parse a cpp file that defines a nested struct. When I run it on my local (Windows) pc, I get the results I am expecting, but when I run it on a Linux machine the alignment changes and the unpacking of the data later is wrong.

I tried using CastXML arguments: '-fms-compatibility' and '-fpack-struct=4', but it still didn't get me the right result on the Linux machine.

Is there any way to make it work?

Thank you.

midstar commented 1 year ago

Hi,

Could you try to add

pragma pack(1)

To your C files alternatively use '-fpack-struct=1' in CastXML to secure you have packet data on byte level. This should give you an answer if the alignment is the problem or if it is something else.

Also, the StructDef class has a string representation (using the Python str() function). Could you try to use this representation in the Windows and Linux environment and compare the results?

Best regards, Joel

Den sön 9 apr. 2023 kl 15:56 skrev agat-asensus @.***>:

Hi,

I am using pycstruct.parse_file, to parse a cpp file that defines a nested struct. When I run it on my local (Windows) pc, I get the results I am expecting, but when I run it on a Linux machine the alignment changes and the unpacking of the data later is wrong.

I tried using CastXML arguments: '-fms-compatibility' and '-fpack-struct=4', but it still didn't get me the right result on the Linux machine.

Is there any way to make it work?

Thank you.

— Reply to this email directly, view it on GitHub https://github.com/midstar/pycstruct/issues/40, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWUW3C4BLNNJFY4G6ND233XAK5Y7ANCNFSM6AAAAAAWYEDK2Y . You are receiving this because you are subscribed to this thread.Message ID: @.***>

agat-asensus commented 1 year ago

Hi,

I used the string representation in both machines to compare the results and they were different, for example unsigned long was uint32 on Windows and uint64 on Linux.

midstar commented 1 year ago

Nice you have found the issue. It might be possible to give CastXML arguments to define the sizes of different data types to override the default ones, but I'm not sure. The standard way of setting fixed size data types independently of platform / architecture is to use #define diretives and define your own INT64 / UINT64 etc. and define these differently depending on platform / architecture. This might be you only option. I see no easy way to create a workaround in pycstruct.

midstar commented 6 months ago

No change needed