On Aug 1, 11:35 pm, Andrew Lentvorski <bs...@allcaps. orgwrote:
Basically, I'd like to use the ctypes module as a much more descriptive
"struct" module.
Is there a way to take a ctypes.Structur e-based class and convert it
to/from a binary string?
Thanks,
-a
After chugging through the ctypes source code, I found that I can
abuse ctypes into doing what I want.
Here is how I did it. I can abuse
string_at(addre ssof(SomeCtypes Class), length) to get a binary string
out of ctypes while I use:
def analyze_elf_hea der(binaryData) :
headerSize = ctypes.sizeof(E lf32_Ehdr)
header = Elf32_Ehdr()
# Abuse ctypes to initialize from a string
bb = ctypes.create_s tring_buffer(bi naryData[0:headerSize])
ctypes.memmove( ctypes.addresso f(header), ctypes.addresso f(bb),
headerSize)
To jam stuff into a ctypes class. This seems like an oversight in the
module though. It would really be better if the class itself had
methods to init from/produce to a binary string.
However, I would prefer that somebody who actually knows ctypes to
weigh in here with comments about what I did.
Thanks,
-a