0

I want to pretty print this struct struct MyStruct { char buffer[16]; }. Depending on buffer[15] I want to print buffer as a 10 byte string or treat it as a pointer. The 10 byte case is simple and works return self.val['buffer'].string(length = 10)

The second case I can't figure out. I want to do something like (*(char**)buffer[0]). I'm not sure how to do that. I was thinking parse_and_eval could be easy even if its not optimal but I couldn't figure out how to access buffer. I also need to cast the buffer to a 32bit int (len = *(int*)(bufer+4);) I couldn't figure that out either.

  • You wrote as example `(*(char**)buffer[0])`, but here you cast only the first byte to a pointer, that can't be right. Did you mean `(*(char**)&buffer[0])`? – ssbssa Sep 23 '22 at 18:27
  • @ssbssa my mistake. I forgot the & or I should have not written `[0]`. The second case I don't know how to do is casting so I can access the struct like this `AltStruct{char*p; int size, flag;};` – Andrew Benor Sep 23 '22 at 21:07

1 Answers1

0

If I interpret your description correctly, I think your data is actually layed out like this:

struct MyStruct {
    union {
        char string[10];
        struct {
            char *p;
            int size;
        } ptr;
    }
    int flag;
}

So I think casting the buffer to a pointer of that type, and then choosing the format based on mystruct->flag would make life easier for you.

Even if my interpretation is not correct, try to find the correct version of that struct that captures the duality of the data.

wkz
  • 2,203
  • 1
  • 15
  • 21
  • I'll accept this because it's what ended up doing. I thought I could cast in the python code but gave up and changed the type in my actual code – Andrew Benor Sep 24 '22 at 18:35