472,354 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

Interconvert a ctypes.Structure to/from a binary string?

Basically, I'd like to use the ctypes module as a much more descriptive
"struct" module.

Is there a way to take a ctypes.Structure-based class and convert it
to/from a binary string?

Thanks,
-a
Aug 2 '08 #1
3 11380
On 2 Aug., 08:35, 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.Structure-based class and convert it
to/from a binary string?

Thanks,
-a
My first idea was :

from ctypes import *
from pickle import *

class T(Structure): pass

print dumps(T())

which raises an TypeError, "abstract class".

And then I found

http://osdir.com/ml/python.ctypes/2006-03/msg00009.html

Greetings, Uwe

Aug 2 '08 #2
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.Structure-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(addressof(SomeCtypesClass), length) to get a binary string
out of ctypes while I use:

def analyze_elf_header(binaryData):
headerSize = ctypes.sizeof(Elf32_Ehdr)
header = Elf32_Ehdr()

# Abuse ctypes to initialize from a string
bb = ctypes.create_string_buffer(binaryData[0:headerSize])
ctypes.memmove(ctypes.addressof(header), ctypes.addressof(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
Aug 2 '08 #3
Andrew P. Lentvorski, Jr. <bs****@gmail.comwrote:
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.Structure-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(addressof(SomeCtypesClass), length) to get a binary string
out of ctypes while I use:

def analyze_elf_header(binaryData):
headerSize = ctypes.sizeof(Elf32_Ehdr)
header = Elf32_Ehdr()

# Abuse ctypes to initialize from a string
bb = ctypes.create_string_buffer(binaryData[0:headerSize])
ctypes.memmove(ctypes.addressof(header), ctypes.addressof(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.
I have found myself doing this quite a bit with ctypes. It is common
to get a block of data in which represents a stream of structures
which have to be unpicked and made into ctypes structs before use.
Making that stream is an equal challenge.

I've found a couple of ways of doing it.

Setup
>>from ctypes import *
class A(Structure):
... _fields_ = [("x", c_int)]
...
>>packet="\x02\x01\x00\x00"
Eg to make a struct from a string
>>a = cast(packet, POINTER(A)).contents
a.x
258
>>>
Or (this is identical to your method)
>>a = A()
a.x
0
>>memmove(addressof(a), packet, sizeof(a))
3083811008L
>>a.x
258

I think the second of those methods is promoted by the ctypes
documentation. I'm not sure about the lifetimes of the .contents in
the first method!

And the reverse
>>string_at(addressof(a), sizeof(a))
'\x02\x01\x00\x00'
>>>
Which I think is probably acceptable...

Some to/from binary methods would be nice, or failing that an explicit
section in the docs!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
19
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
0
by: p.lavarre | last post by:
http://wiki.python.org/moin/ctypes now tries to answer: '''FAQ: How do I copy bytes to Python from a ctypes.Structure?''' '''FAQ: How do I copy bytes to a ctypes.Structure from Python?'''...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
4
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.