[Tutor] Get the structure values from a c file

Dave Angel d at davea.name
Tue Dec 18 09:32:41 CET 2012


On 12/17/2012 11:25 PM, Marefe Serentas wrote:
> On 12/18/2012 12:38 AM, tutor-request at python.org wrote:
>> Re: Get the structure values from a c file
> I apologize for some syntax errors in my c file. I would like to correct
> my mistakes and append some lines in the c file to make it clearer.
>
>
> ----
> #define max (3)
> #define max_data_size (9600*2*8)
>
> typedef unsigned char  u8;
> typedef signed int u32;
>
> int some_data[] =
> {
>     -288,   -153,     31,     24,    205,    110,     39,     88,
>     -281,    145,     35,    266,     63,    -79,   -103,    -25,
>       53,    145,   -114,   -274,     46,     60,    220,    205
> };
>
> typedef struct A
> {
>     u32 a;
>     u8 b;
> }A;
>
> typedef struct MainStruct
> {
>     A var1;
>     u32 var2;
>     u32 var3[max];
>     u32 var4;
>     u32 var5[max];
> }MainStruct;
>
> void generate()
> {
>     MainStruct myMain = {0};
>     myMain.var1.a = 1;
>     myMain.var1.b = 'a';
>     myMain.var2 = 3;
>     myMain.var3[0] = -3;
>     myMain.var3[1] = 6;
>     myMain.var3[2] = 18;
>
>     (void)memcpy((void *)myMain.var4,
>                  (void *)some_data,
>                  sizeof(some_data));
>
>     myMain.var5[0] = 1;
> }
>
> ----
>
>
> This is a work-problem. Given a c file as input, the customer wants me
> to write a python script that will retrieve the values of myMain.
> He wants those values converted to binary data and write it in a .bin
> file. As a result, the size of the bin file is equal to the size of
> myMain. The c file input content might change in the future like
> different values assigned, added fields in the MainStruct, etc. Also I
> am not permitted to change the c file.
>
>
> About me, I'm a fresh graduate. I just started learning python a month
> ago. I learned c in school, we had it for 2 years.
>
>
> I'm using python 2.6. I'm running on Windows 7 64-bit OS.
>
> What I did so far is parse the C source code.
> But having a problem parsing the value of myMain.var4.
>

I'd assume you might have trouble automatically deciding the values for
myMain.var5[1] and myMain.var5[2].  (They are probably 31 and 24,
respectively)

And of course, if this function is actually called (assuming you supply
a simple main.c elsewhere that calls it), it'll probably crash when the
return address gets overwritten.

Further, even the size of the binary file will depend on compiler
version and switches.

When I went to my first Boy Scout camporee, they sent me out to find a
left-handed smoke-shifter.  Your customer is pulling your leg.



-- 

DaveA



More information about the Tutor mailing list