1 | n/a | #include "Python.h" |
---|
2 | n/a | |
---|
3 | n/a | #include <ffi.h> |
---|
4 | n/a | #ifdef MS_WIN32 |
---|
5 | n/a | #include <windows.h> |
---|
6 | n/a | #endif |
---|
7 | n/a | #include "ctypes.h" |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | #define CTYPES_CFIELD_CAPSULE_NAME_PYMEM "_ctypes/cfield.c pymem" |
---|
11 | n/a | |
---|
12 | n/a | static void pymem_destructor(PyObject *ptr) |
---|
13 | n/a | { |
---|
14 | n/a | void *p = PyCapsule_GetPointer(ptr, CTYPES_CFIELD_CAPSULE_NAME_PYMEM); |
---|
15 | n/a | if (p) { |
---|
16 | n/a | PyMem_Free(p); |
---|
17 | n/a | } |
---|
18 | n/a | } |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | /******************************************************************/ |
---|
22 | n/a | /* |
---|
23 | n/a | PyCField_Type |
---|
24 | n/a | */ |
---|
25 | n/a | static PyObject * |
---|
26 | n/a | PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
27 | n/a | { |
---|
28 | n/a | CFieldObject *obj; |
---|
29 | n/a | obj = (CFieldObject *)type->tp_alloc(type, 0); |
---|
30 | n/a | return (PyObject *)obj; |
---|
31 | n/a | } |
---|
32 | n/a | |
---|
33 | n/a | /* |
---|
34 | n/a | * Expects the size, index and offset for the current field in *psize and |
---|
35 | n/a | * *poffset, stores the total size so far in *psize, the offset for the next |
---|
36 | n/a | * field in *poffset, the alignment requirements for the current field in |
---|
37 | n/a | * *palign, and returns a field desriptor for this field. |
---|
38 | n/a | */ |
---|
39 | n/a | /* |
---|
40 | n/a | * bitfields extension: |
---|
41 | n/a | * bitsize != 0: this is a bit field. |
---|
42 | n/a | * pbitofs points to the current bit offset, this will be updated. |
---|
43 | n/a | * prev_desc points to the type of the previous bitfield, if any. |
---|
44 | n/a | */ |
---|
45 | n/a | PyObject * |
---|
46 | n/a | PyCField_FromDesc(PyObject *desc, Py_ssize_t index, |
---|
47 | n/a | Py_ssize_t *pfield_size, int bitsize, int *pbitofs, |
---|
48 | n/a | Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign, |
---|
49 | n/a | int pack, int big_endian) |
---|
50 | n/a | { |
---|
51 | n/a | CFieldObject *self; |
---|
52 | n/a | PyObject *proto; |
---|
53 | n/a | Py_ssize_t size, align; |
---|
54 | n/a | SETFUNC setfunc = NULL; |
---|
55 | n/a | GETFUNC getfunc = NULL; |
---|
56 | n/a | StgDictObject *dict; |
---|
57 | n/a | int fieldtype; |
---|
58 | n/a | #define NO_BITFIELD 0 |
---|
59 | n/a | #define NEW_BITFIELD 1 |
---|
60 | n/a | #define CONT_BITFIELD 2 |
---|
61 | n/a | #define EXPAND_BITFIELD 3 |
---|
62 | n/a | |
---|
63 | n/a | self = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type, |
---|
64 | n/a | NULL); |
---|
65 | n/a | if (self == NULL) |
---|
66 | n/a | return NULL; |
---|
67 | n/a | dict = PyType_stgdict(desc); |
---|
68 | n/a | if (!dict) { |
---|
69 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
70 | n/a | "has no _stginfo_"); |
---|
71 | n/a | Py_DECREF(self); |
---|
72 | n/a | return NULL; |
---|
73 | n/a | } |
---|
74 | n/a | if (bitsize /* this is a bitfield request */ |
---|
75 | n/a | && *pfield_size /* we have a bitfield open */ |
---|
76 | n/a | #ifdef MS_WIN32 |
---|
77 | n/a | /* MSVC, GCC with -mms-bitfields */ |
---|
78 | n/a | && dict->size * 8 == *pfield_size |
---|
79 | n/a | #else |
---|
80 | n/a | /* GCC */ |
---|
81 | n/a | && dict->size * 8 <= *pfield_size |
---|
82 | n/a | #endif |
---|
83 | n/a | && (*pbitofs + bitsize) <= *pfield_size) { |
---|
84 | n/a | /* continue bit field */ |
---|
85 | n/a | fieldtype = CONT_BITFIELD; |
---|
86 | n/a | #ifndef MS_WIN32 |
---|
87 | n/a | } else if (bitsize /* this is a bitfield request */ |
---|
88 | n/a | && *pfield_size /* we have a bitfield open */ |
---|
89 | n/a | && dict->size * 8 >= *pfield_size |
---|
90 | n/a | && (*pbitofs + bitsize) <= dict->size * 8) { |
---|
91 | n/a | /* expand bit field */ |
---|
92 | n/a | fieldtype = EXPAND_BITFIELD; |
---|
93 | n/a | #endif |
---|
94 | n/a | } else if (bitsize) { |
---|
95 | n/a | /* start new bitfield */ |
---|
96 | n/a | fieldtype = NEW_BITFIELD; |
---|
97 | n/a | *pbitofs = 0; |
---|
98 | n/a | *pfield_size = dict->size * 8; |
---|
99 | n/a | } else { |
---|
100 | n/a | /* not a bit field */ |
---|
101 | n/a | fieldtype = NO_BITFIELD; |
---|
102 | n/a | *pbitofs = 0; |
---|
103 | n/a | *pfield_size = 0; |
---|
104 | n/a | } |
---|
105 | n/a | |
---|
106 | n/a | size = dict->size; |
---|
107 | n/a | proto = desc; |
---|
108 | n/a | |
---|
109 | n/a | /* Field descriptors for 'c_char * n' are be scpecial cased to |
---|
110 | n/a | return a Python string instead of an Array object instance... |
---|
111 | n/a | */ |
---|
112 | n/a | if (PyCArrayTypeObject_Check(proto)) { |
---|
113 | n/a | StgDictObject *adict = PyType_stgdict(proto); |
---|
114 | n/a | StgDictObject *idict; |
---|
115 | n/a | if (adict && adict->proto) { |
---|
116 | n/a | idict = PyType_stgdict(adict->proto); |
---|
117 | n/a | if (!idict) { |
---|
118 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
119 | n/a | "has no _stginfo_"); |
---|
120 | n/a | Py_DECREF(self); |
---|
121 | n/a | return NULL; |
---|
122 | n/a | } |
---|
123 | n/a | if (idict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
---|
124 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("s"); |
---|
125 | n/a | getfunc = fd->getfunc; |
---|
126 | n/a | setfunc = fd->setfunc; |
---|
127 | n/a | } |
---|
128 | n/a | #ifdef CTYPES_UNICODE |
---|
129 | n/a | if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
---|
130 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("U"); |
---|
131 | n/a | getfunc = fd->getfunc; |
---|
132 | n/a | setfunc = fd->setfunc; |
---|
133 | n/a | } |
---|
134 | n/a | #endif |
---|
135 | n/a | } |
---|
136 | n/a | } |
---|
137 | n/a | |
---|
138 | n/a | self->setfunc = setfunc; |
---|
139 | n/a | self->getfunc = getfunc; |
---|
140 | n/a | self->index = index; |
---|
141 | n/a | |
---|
142 | n/a | Py_INCREF(proto); |
---|
143 | n/a | self->proto = proto; |
---|
144 | n/a | |
---|
145 | n/a | switch (fieldtype) { |
---|
146 | n/a | case NEW_BITFIELD: |
---|
147 | n/a | if (big_endian) |
---|
148 | n/a | self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize; |
---|
149 | n/a | else |
---|
150 | n/a | self->size = (bitsize << 16) + *pbitofs; |
---|
151 | n/a | *pbitofs = bitsize; |
---|
152 | n/a | /* fall through */ |
---|
153 | n/a | case NO_BITFIELD: |
---|
154 | n/a | if (pack) |
---|
155 | n/a | align = min(pack, dict->align); |
---|
156 | n/a | else |
---|
157 | n/a | align = dict->align; |
---|
158 | n/a | if (align && *poffset % align) { |
---|
159 | n/a | Py_ssize_t delta = align - (*poffset % align); |
---|
160 | n/a | *psize += delta; |
---|
161 | n/a | *poffset += delta; |
---|
162 | n/a | } |
---|
163 | n/a | |
---|
164 | n/a | if (bitsize == 0) |
---|
165 | n/a | self->size = size; |
---|
166 | n/a | *psize += size; |
---|
167 | n/a | |
---|
168 | n/a | self->offset = *poffset; |
---|
169 | n/a | *poffset += size; |
---|
170 | n/a | |
---|
171 | n/a | *palign = align; |
---|
172 | n/a | break; |
---|
173 | n/a | |
---|
174 | n/a | case EXPAND_BITFIELD: |
---|
175 | n/a | *poffset += dict->size - *pfield_size/8; |
---|
176 | n/a | *psize += dict->size - *pfield_size/8; |
---|
177 | n/a | |
---|
178 | n/a | *pfield_size = dict->size * 8; |
---|
179 | n/a | |
---|
180 | n/a | if (big_endian) |
---|
181 | n/a | self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize; |
---|
182 | n/a | else |
---|
183 | n/a | self->size = (bitsize << 16) + *pbitofs; |
---|
184 | n/a | |
---|
185 | n/a | self->offset = *poffset - size; /* poffset is already updated for the NEXT field */ |
---|
186 | n/a | *pbitofs += bitsize; |
---|
187 | n/a | break; |
---|
188 | n/a | |
---|
189 | n/a | case CONT_BITFIELD: |
---|
190 | n/a | if (big_endian) |
---|
191 | n/a | self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize; |
---|
192 | n/a | else |
---|
193 | n/a | self->size = (bitsize << 16) + *pbitofs; |
---|
194 | n/a | |
---|
195 | n/a | self->offset = *poffset - size; /* poffset is already updated for the NEXT field */ |
---|
196 | n/a | *pbitofs += bitsize; |
---|
197 | n/a | break; |
---|
198 | n/a | } |
---|
199 | n/a | |
---|
200 | n/a | return (PyObject *)self; |
---|
201 | n/a | } |
---|
202 | n/a | |
---|
203 | n/a | static int |
---|
204 | n/a | PyCField_set(CFieldObject *self, PyObject *inst, PyObject *value) |
---|
205 | n/a | { |
---|
206 | n/a | CDataObject *dst; |
---|
207 | n/a | char *ptr; |
---|
208 | n/a | assert(CDataObject_Check(inst)); |
---|
209 | n/a | dst = (CDataObject *)inst; |
---|
210 | n/a | ptr = dst->b_ptr + self->offset; |
---|
211 | n/a | if (value == NULL) { |
---|
212 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
213 | n/a | "can't delete attribute"); |
---|
214 | n/a | return -1; |
---|
215 | n/a | } |
---|
216 | n/a | return PyCData_set(inst, self->proto, self->setfunc, value, |
---|
217 | n/a | self->index, self->size, ptr); |
---|
218 | n/a | } |
---|
219 | n/a | |
---|
220 | n/a | static PyObject * |
---|
221 | n/a | PyCField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type) |
---|
222 | n/a | { |
---|
223 | n/a | CDataObject *src; |
---|
224 | n/a | if (inst == NULL) { |
---|
225 | n/a | Py_INCREF(self); |
---|
226 | n/a | return (PyObject *)self; |
---|
227 | n/a | } |
---|
228 | n/a | assert(CDataObject_Check(inst)); |
---|
229 | n/a | src = (CDataObject *)inst; |
---|
230 | n/a | return PyCData_get(self->proto, self->getfunc, inst, |
---|
231 | n/a | self->index, self->size, src->b_ptr + self->offset); |
---|
232 | n/a | } |
---|
233 | n/a | |
---|
234 | n/a | static PyObject * |
---|
235 | n/a | PyCField_get_offset(PyObject *self, void *data) |
---|
236 | n/a | { |
---|
237 | n/a | return PyLong_FromSsize_t(((CFieldObject *)self)->offset); |
---|
238 | n/a | } |
---|
239 | n/a | |
---|
240 | n/a | static PyObject * |
---|
241 | n/a | PyCField_get_size(PyObject *self, void *data) |
---|
242 | n/a | { |
---|
243 | n/a | return PyLong_FromSsize_t(((CFieldObject *)self)->size); |
---|
244 | n/a | } |
---|
245 | n/a | |
---|
246 | n/a | static PyGetSetDef PyCField_getset[] = { |
---|
247 | n/a | { "offset", PyCField_get_offset, NULL, "offset in bytes of this field" }, |
---|
248 | n/a | { "size", PyCField_get_size, NULL, "size in bytes of this field" }, |
---|
249 | n/a | { NULL, NULL, NULL, NULL }, |
---|
250 | n/a | }; |
---|
251 | n/a | |
---|
252 | n/a | static int |
---|
253 | n/a | PyCField_traverse(CFieldObject *self, visitproc visit, void *arg) |
---|
254 | n/a | { |
---|
255 | n/a | Py_VISIT(self->proto); |
---|
256 | n/a | return 0; |
---|
257 | n/a | } |
---|
258 | n/a | |
---|
259 | n/a | static int |
---|
260 | n/a | PyCField_clear(CFieldObject *self) |
---|
261 | n/a | { |
---|
262 | n/a | Py_CLEAR(self->proto); |
---|
263 | n/a | return 0; |
---|
264 | n/a | } |
---|
265 | n/a | |
---|
266 | n/a | static void |
---|
267 | n/a | PyCField_dealloc(PyObject *self) |
---|
268 | n/a | { |
---|
269 | n/a | PyCField_clear((CFieldObject *)self); |
---|
270 | n/a | self->ob_type->tp_free((PyObject *)self); |
---|
271 | n/a | } |
---|
272 | n/a | |
---|
273 | n/a | static PyObject * |
---|
274 | n/a | PyCField_repr(CFieldObject *self) |
---|
275 | n/a | { |
---|
276 | n/a | PyObject *result; |
---|
277 | n/a | Py_ssize_t bits = self->size >> 16; |
---|
278 | n/a | Py_ssize_t size = self->size & 0xFFFF; |
---|
279 | n/a | const char *name; |
---|
280 | n/a | |
---|
281 | n/a | name = ((PyTypeObject *)self->proto)->tp_name; |
---|
282 | n/a | |
---|
283 | n/a | if (bits) |
---|
284 | n/a | result = PyUnicode_FromFormat( |
---|
285 | n/a | "<Field type=%s, ofs=%zd:%zd, bits=%zd>", |
---|
286 | n/a | name, self->offset, size, bits); |
---|
287 | n/a | else |
---|
288 | n/a | result = PyUnicode_FromFormat( |
---|
289 | n/a | "<Field type=%s, ofs=%zd, size=%zd>", |
---|
290 | n/a | name, self->offset, size); |
---|
291 | n/a | return result; |
---|
292 | n/a | } |
---|
293 | n/a | |
---|
294 | n/a | PyTypeObject PyCField_Type = { |
---|
295 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
296 | n/a | "_ctypes.CField", /* tp_name */ |
---|
297 | n/a | sizeof(CFieldObject), /* tp_basicsize */ |
---|
298 | n/a | 0, /* tp_itemsize */ |
---|
299 | n/a | PyCField_dealloc, /* tp_dealloc */ |
---|
300 | n/a | 0, /* tp_print */ |
---|
301 | n/a | 0, /* tp_getattr */ |
---|
302 | n/a | 0, /* tp_setattr */ |
---|
303 | n/a | 0, /* tp_reserved */ |
---|
304 | n/a | (reprfunc)PyCField_repr, /* tp_repr */ |
---|
305 | n/a | 0, /* tp_as_number */ |
---|
306 | n/a | 0, /* tp_as_sequence */ |
---|
307 | n/a | 0, /* tp_as_mapping */ |
---|
308 | n/a | 0, /* tp_hash */ |
---|
309 | n/a | 0, /* tp_call */ |
---|
310 | n/a | 0, /* tp_str */ |
---|
311 | n/a | 0, /* tp_getattro */ |
---|
312 | n/a | 0, /* tp_setattro */ |
---|
313 | n/a | 0, /* tp_as_buffer */ |
---|
314 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
315 | n/a | "Structure/Union member", /* tp_doc */ |
---|
316 | n/a | (traverseproc)PyCField_traverse, /* tp_traverse */ |
---|
317 | n/a | (inquiry)PyCField_clear, /* tp_clear */ |
---|
318 | n/a | 0, /* tp_richcompare */ |
---|
319 | n/a | 0, /* tp_weaklistoffset */ |
---|
320 | n/a | 0, /* tp_iter */ |
---|
321 | n/a | 0, /* tp_iternext */ |
---|
322 | n/a | 0, /* tp_methods */ |
---|
323 | n/a | 0, /* tp_members */ |
---|
324 | n/a | PyCField_getset, /* tp_getset */ |
---|
325 | n/a | 0, /* tp_base */ |
---|
326 | n/a | 0, /* tp_dict */ |
---|
327 | n/a | (descrgetfunc)PyCField_get, /* tp_descr_get */ |
---|
328 | n/a | (descrsetfunc)PyCField_set, /* tp_descr_set */ |
---|
329 | n/a | 0, /* tp_dictoffset */ |
---|
330 | n/a | 0, /* tp_init */ |
---|
331 | n/a | 0, /* tp_alloc */ |
---|
332 | n/a | PyCField_new, /* tp_new */ |
---|
333 | n/a | 0, /* tp_free */ |
---|
334 | n/a | }; |
---|
335 | n/a | |
---|
336 | n/a | |
---|
337 | n/a | /******************************************************************/ |
---|
338 | n/a | /* |
---|
339 | n/a | Accessor functions |
---|
340 | n/a | */ |
---|
341 | n/a | |
---|
342 | n/a | /* Derived from Modules/structmodule.c: |
---|
343 | n/a | Helper routine to get a Python integer and raise the appropriate error |
---|
344 | n/a | if it isn't one */ |
---|
345 | n/a | |
---|
346 | n/a | static int |
---|
347 | n/a | get_long(PyObject *v, long *p) |
---|
348 | n/a | { |
---|
349 | n/a | long x; |
---|
350 | n/a | |
---|
351 | n/a | if (PyFloat_Check(v)) { |
---|
352 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
353 | n/a | "int expected instead of float"); |
---|
354 | n/a | return -1; |
---|
355 | n/a | } |
---|
356 | n/a | x = PyLong_AsUnsignedLongMask(v); |
---|
357 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
358 | n/a | return -1; |
---|
359 | n/a | *p = x; |
---|
360 | n/a | return 0; |
---|
361 | n/a | } |
---|
362 | n/a | |
---|
363 | n/a | /* Same, but handling unsigned long */ |
---|
364 | n/a | |
---|
365 | n/a | static int |
---|
366 | n/a | get_ulong(PyObject *v, unsigned long *p) |
---|
367 | n/a | { |
---|
368 | n/a | unsigned long x; |
---|
369 | n/a | |
---|
370 | n/a | if (PyFloat_Check(v)) { |
---|
371 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
372 | n/a | "int expected instead of float"); |
---|
373 | n/a | return -1; |
---|
374 | n/a | } |
---|
375 | n/a | x = PyLong_AsUnsignedLongMask(v); |
---|
376 | n/a | if (x == (unsigned long)-1 && PyErr_Occurred()) |
---|
377 | n/a | return -1; |
---|
378 | n/a | *p = x; |
---|
379 | n/a | return 0; |
---|
380 | n/a | } |
---|
381 | n/a | |
---|
382 | n/a | /* Same, but handling native long long. */ |
---|
383 | n/a | |
---|
384 | n/a | static int |
---|
385 | n/a | get_longlong(PyObject *v, long long *p) |
---|
386 | n/a | { |
---|
387 | n/a | long long x; |
---|
388 | n/a | if (PyFloat_Check(v)) { |
---|
389 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
390 | n/a | "int expected instead of float"); |
---|
391 | n/a | return -1; |
---|
392 | n/a | } |
---|
393 | n/a | x = PyLong_AsUnsignedLongLongMask(v); |
---|
394 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
395 | n/a | return -1; |
---|
396 | n/a | *p = x; |
---|
397 | n/a | return 0; |
---|
398 | n/a | } |
---|
399 | n/a | |
---|
400 | n/a | /* Same, but handling native unsigned long long. */ |
---|
401 | n/a | |
---|
402 | n/a | static int |
---|
403 | n/a | get_ulonglong(PyObject *v, unsigned long long *p) |
---|
404 | n/a | { |
---|
405 | n/a | unsigned long long x; |
---|
406 | n/a | if (PyFloat_Check(v)) { |
---|
407 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
408 | n/a | "int expected instead of float"); |
---|
409 | n/a | return -1; |
---|
410 | n/a | } |
---|
411 | n/a | x = PyLong_AsUnsignedLongLongMask(v); |
---|
412 | n/a | if (x == (unsigned long long)-1 && PyErr_Occurred()) |
---|
413 | n/a | return -1; |
---|
414 | n/a | *p = x; |
---|
415 | n/a | return 0; |
---|
416 | n/a | } |
---|
417 | n/a | |
---|
418 | n/a | /***************************************************************** |
---|
419 | n/a | * Integer fields, with bitfield support |
---|
420 | n/a | */ |
---|
421 | n/a | |
---|
422 | n/a | /* how to decode the size field, for integer get/set functions */ |
---|
423 | n/a | #define LOW_BIT(x) ((x) & 0xFFFF) |
---|
424 | n/a | #define NUM_BITS(x) ((x) >> 16) |
---|
425 | n/a | |
---|
426 | n/a | /* Doesn't work if NUM_BITS(size) == 0, but it never happens in SET() call. */ |
---|
427 | n/a | #define BIT_MASK(type, size) (((((type)1 << (NUM_BITS(size) - 1)) - 1) << 1) + 1) |
---|
428 | n/a | |
---|
429 | n/a | /* This macro CHANGES the first parameter IN PLACE. For proper sign handling, |
---|
430 | n/a | we must first shift left, then right. |
---|
431 | n/a | */ |
---|
432 | n/a | #define GET_BITFIELD(v, size) \ |
---|
433 | n/a | if (NUM_BITS(size)) { \ |
---|
434 | n/a | v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size)); \ |
---|
435 | n/a | v >>= (sizeof(v)*8 - NUM_BITS(size)); \ |
---|
436 | n/a | } |
---|
437 | n/a | |
---|
438 | n/a | /* This macro RETURNS the first parameter with the bit field CHANGED. */ |
---|
439 | n/a | #define SET(type, x, v, size) \ |
---|
440 | n/a | (NUM_BITS(size) ? \ |
---|
441 | n/a | ( ( (type)x & ~(BIT_MASK(type, size) << LOW_BIT(size)) ) | ( ((type)v & BIT_MASK(type, size)) << LOW_BIT(size) ) ) \ |
---|
442 | n/a | : (type)v) |
---|
443 | n/a | |
---|
444 | n/a | /* byte swapping macros */ |
---|
445 | n/a | #define SWAP_2(v) \ |
---|
446 | n/a | ( ( (v >> 8) & 0x00FF) | \ |
---|
447 | n/a | ( (v << 8) & 0xFF00) ) |
---|
448 | n/a | |
---|
449 | n/a | #define SWAP_4(v) \ |
---|
450 | n/a | ( ( (v & 0x000000FF) << 24 ) | \ |
---|
451 | n/a | ( (v & 0x0000FF00) << 8 ) | \ |
---|
452 | n/a | ( (v & 0x00FF0000) >> 8 ) | \ |
---|
453 | n/a | ( ((v >> 24) & 0xFF)) ) |
---|
454 | n/a | |
---|
455 | n/a | #ifdef _MSC_VER |
---|
456 | n/a | #define SWAP_8(v) \ |
---|
457 | n/a | ( ( (v & 0x00000000000000FFL) << 56 ) | \ |
---|
458 | n/a | ( (v & 0x000000000000FF00L) << 40 ) | \ |
---|
459 | n/a | ( (v & 0x0000000000FF0000L) << 24 ) | \ |
---|
460 | n/a | ( (v & 0x00000000FF000000L) << 8 ) | \ |
---|
461 | n/a | ( (v & 0x000000FF00000000L) >> 8 ) | \ |
---|
462 | n/a | ( (v & 0x0000FF0000000000L) >> 24 ) | \ |
---|
463 | n/a | ( (v & 0x00FF000000000000L) >> 40 ) | \ |
---|
464 | n/a | ( ((v >> 56) & 0xFF)) ) |
---|
465 | n/a | #else |
---|
466 | n/a | #define SWAP_8(v) \ |
---|
467 | n/a | ( ( (v & 0x00000000000000FFLL) << 56 ) | \ |
---|
468 | n/a | ( (v & 0x000000000000FF00LL) << 40 ) | \ |
---|
469 | n/a | ( (v & 0x0000000000FF0000LL) << 24 ) | \ |
---|
470 | n/a | ( (v & 0x00000000FF000000LL) << 8 ) | \ |
---|
471 | n/a | ( (v & 0x000000FF00000000LL) >> 8 ) | \ |
---|
472 | n/a | ( (v & 0x0000FF0000000000LL) >> 24 ) | \ |
---|
473 | n/a | ( (v & 0x00FF000000000000LL) >> 40 ) | \ |
---|
474 | n/a | ( ((v >> 56) & 0xFF)) ) |
---|
475 | n/a | #endif |
---|
476 | n/a | |
---|
477 | n/a | #define SWAP_INT SWAP_4 |
---|
478 | n/a | |
---|
479 | n/a | #if SIZEOF_LONG == 4 |
---|
480 | n/a | # define SWAP_LONG SWAP_4 |
---|
481 | n/a | #elif SIZEOF_LONG == 8 |
---|
482 | n/a | # define SWAP_LONG SWAP_8 |
---|
483 | n/a | #endif |
---|
484 | n/a | /***************************************************************** |
---|
485 | n/a | * The setter methods return an object which must be kept alive, to keep the |
---|
486 | n/a | * data valid which has been stored in the memory block. The ctypes object |
---|
487 | n/a | * instance inserts this object into its 'b_objects' list. |
---|
488 | n/a | * |
---|
489 | n/a | * For simple Python types like integers or characters, there is nothing that |
---|
490 | n/a | * has to been kept alive, so Py_None is returned in these cases. But this |
---|
491 | n/a | * makes inspecting the 'b_objects' list, which is accessible from Python for |
---|
492 | n/a | * debugging, less useful. |
---|
493 | n/a | * |
---|
494 | n/a | * So, defining the _CTYPES_DEBUG_KEEP symbol returns the original value |
---|
495 | n/a | * instead of Py_None. |
---|
496 | n/a | */ |
---|
497 | n/a | |
---|
498 | n/a | #ifdef _CTYPES_DEBUG_KEEP |
---|
499 | n/a | #define _RET(x) Py_INCREF(x); return x |
---|
500 | n/a | #else |
---|
501 | n/a | #define _RET(X) Py_RETURN_NONE |
---|
502 | n/a | #endif |
---|
503 | n/a | |
---|
504 | n/a | /***************************************************************** |
---|
505 | n/a | * integer accessor methods, supporting bit fields |
---|
506 | n/a | */ |
---|
507 | n/a | |
---|
508 | n/a | static PyObject * |
---|
509 | n/a | b_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
510 | n/a | { |
---|
511 | n/a | long val; |
---|
512 | n/a | if (get_long(value, &val) < 0) |
---|
513 | n/a | return NULL; |
---|
514 | n/a | *(signed char *)ptr = SET(signed char, *(signed char *)ptr, val, size); |
---|
515 | n/a | _RET(value); |
---|
516 | n/a | } |
---|
517 | n/a | |
---|
518 | n/a | |
---|
519 | n/a | static PyObject * |
---|
520 | n/a | b_get(void *ptr, Py_ssize_t size) |
---|
521 | n/a | { |
---|
522 | n/a | signed char val = *(signed char *)ptr; |
---|
523 | n/a | GET_BITFIELD(val, size); |
---|
524 | n/a | return PyLong_FromLong(val); |
---|
525 | n/a | } |
---|
526 | n/a | |
---|
527 | n/a | static PyObject * |
---|
528 | n/a | B_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
529 | n/a | { |
---|
530 | n/a | unsigned long val; |
---|
531 | n/a | if (get_ulong(value, &val) < 0) |
---|
532 | n/a | return NULL; |
---|
533 | n/a | *(unsigned char *)ptr = SET(unsigned char, *(unsigned char*)ptr, val, size); |
---|
534 | n/a | _RET(value); |
---|
535 | n/a | } |
---|
536 | n/a | |
---|
537 | n/a | |
---|
538 | n/a | static PyObject * |
---|
539 | n/a | B_get(void *ptr, Py_ssize_t size) |
---|
540 | n/a | { |
---|
541 | n/a | unsigned char val = *(unsigned char *)ptr; |
---|
542 | n/a | GET_BITFIELD(val, size); |
---|
543 | n/a | return PyLong_FromLong(val); |
---|
544 | n/a | } |
---|
545 | n/a | |
---|
546 | n/a | static PyObject * |
---|
547 | n/a | h_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
548 | n/a | { |
---|
549 | n/a | long val; |
---|
550 | n/a | short x; |
---|
551 | n/a | if (get_long(value, &val) < 0) |
---|
552 | n/a | return NULL; |
---|
553 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
554 | n/a | x = SET(short, x, val, size); |
---|
555 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
556 | n/a | _RET(value); |
---|
557 | n/a | } |
---|
558 | n/a | |
---|
559 | n/a | |
---|
560 | n/a | static PyObject * |
---|
561 | n/a | h_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
562 | n/a | { |
---|
563 | n/a | long val; |
---|
564 | n/a | short field; |
---|
565 | n/a | if (get_long(value, &val) < 0) |
---|
566 | n/a | return NULL; |
---|
567 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
568 | n/a | field = SWAP_2(field); |
---|
569 | n/a | field = SET(short, field, val, size); |
---|
570 | n/a | field = SWAP_2(field); |
---|
571 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
572 | n/a | _RET(value); |
---|
573 | n/a | } |
---|
574 | n/a | |
---|
575 | n/a | static PyObject * |
---|
576 | n/a | h_get(void *ptr, Py_ssize_t size) |
---|
577 | n/a | { |
---|
578 | n/a | short val; |
---|
579 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
580 | n/a | GET_BITFIELD(val, size); |
---|
581 | n/a | return PyLong_FromLong((long)val); |
---|
582 | n/a | } |
---|
583 | n/a | |
---|
584 | n/a | static PyObject * |
---|
585 | n/a | h_get_sw(void *ptr, Py_ssize_t size) |
---|
586 | n/a | { |
---|
587 | n/a | short val; |
---|
588 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
589 | n/a | val = SWAP_2(val); |
---|
590 | n/a | GET_BITFIELD(val, size); |
---|
591 | n/a | return PyLong_FromLong(val); |
---|
592 | n/a | } |
---|
593 | n/a | |
---|
594 | n/a | static PyObject * |
---|
595 | n/a | H_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
596 | n/a | { |
---|
597 | n/a | unsigned long val; |
---|
598 | n/a | unsigned short x; |
---|
599 | n/a | if (get_ulong(value, &val) < 0) |
---|
600 | n/a | return NULL; |
---|
601 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
602 | n/a | x = SET(unsigned short, x, val, size); |
---|
603 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
604 | n/a | _RET(value); |
---|
605 | n/a | } |
---|
606 | n/a | |
---|
607 | n/a | static PyObject * |
---|
608 | n/a | H_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
609 | n/a | { |
---|
610 | n/a | unsigned long val; |
---|
611 | n/a | unsigned short field; |
---|
612 | n/a | if (get_ulong(value, &val) < 0) |
---|
613 | n/a | return NULL; |
---|
614 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
615 | n/a | field = SWAP_2(field); |
---|
616 | n/a | field = SET(unsigned short, field, val, size); |
---|
617 | n/a | field = SWAP_2(field); |
---|
618 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
619 | n/a | _RET(value); |
---|
620 | n/a | } |
---|
621 | n/a | |
---|
622 | n/a | |
---|
623 | n/a | static PyObject * |
---|
624 | n/a | H_get(void *ptr, Py_ssize_t size) |
---|
625 | n/a | { |
---|
626 | n/a | unsigned short val; |
---|
627 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
628 | n/a | GET_BITFIELD(val, size); |
---|
629 | n/a | return PyLong_FromLong(val); |
---|
630 | n/a | } |
---|
631 | n/a | |
---|
632 | n/a | static PyObject * |
---|
633 | n/a | H_get_sw(void *ptr, Py_ssize_t size) |
---|
634 | n/a | { |
---|
635 | n/a | unsigned short val; |
---|
636 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
637 | n/a | val = SWAP_2(val); |
---|
638 | n/a | GET_BITFIELD(val, size); |
---|
639 | n/a | return PyLong_FromLong(val); |
---|
640 | n/a | } |
---|
641 | n/a | |
---|
642 | n/a | static PyObject * |
---|
643 | n/a | i_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
644 | n/a | { |
---|
645 | n/a | long val; |
---|
646 | n/a | int x; |
---|
647 | n/a | if (get_long(value, &val) < 0) |
---|
648 | n/a | return NULL; |
---|
649 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
650 | n/a | x = SET(int, x, val, size); |
---|
651 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
652 | n/a | _RET(value); |
---|
653 | n/a | } |
---|
654 | n/a | |
---|
655 | n/a | static PyObject * |
---|
656 | n/a | i_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
657 | n/a | { |
---|
658 | n/a | long val; |
---|
659 | n/a | int field; |
---|
660 | n/a | if (get_long(value, &val) < 0) |
---|
661 | n/a | return NULL; |
---|
662 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
663 | n/a | field = SWAP_INT(field); |
---|
664 | n/a | field = SET(int, field, val, size); |
---|
665 | n/a | field = SWAP_INT(field); |
---|
666 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
667 | n/a | _RET(value); |
---|
668 | n/a | } |
---|
669 | n/a | |
---|
670 | n/a | |
---|
671 | n/a | static PyObject * |
---|
672 | n/a | i_get(void *ptr, Py_ssize_t size) |
---|
673 | n/a | { |
---|
674 | n/a | int val; |
---|
675 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
676 | n/a | GET_BITFIELD(val, size); |
---|
677 | n/a | return PyLong_FromLong(val); |
---|
678 | n/a | } |
---|
679 | n/a | |
---|
680 | n/a | static PyObject * |
---|
681 | n/a | i_get_sw(void *ptr, Py_ssize_t size) |
---|
682 | n/a | { |
---|
683 | n/a | int val; |
---|
684 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
685 | n/a | val = SWAP_INT(val); |
---|
686 | n/a | GET_BITFIELD(val, size); |
---|
687 | n/a | return PyLong_FromLong(val); |
---|
688 | n/a | } |
---|
689 | n/a | |
---|
690 | n/a | #ifdef MS_WIN32 |
---|
691 | n/a | /* short BOOL - VARIANT_BOOL */ |
---|
692 | n/a | static PyObject * |
---|
693 | n/a | vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
694 | n/a | { |
---|
695 | n/a | switch (PyObject_IsTrue(value)) { |
---|
696 | n/a | case -1: |
---|
697 | n/a | return NULL; |
---|
698 | n/a | case 0: |
---|
699 | n/a | *(short int *)ptr = VARIANT_FALSE; |
---|
700 | n/a | _RET(value); |
---|
701 | n/a | default: |
---|
702 | n/a | *(short int *)ptr = VARIANT_TRUE; |
---|
703 | n/a | _RET(value); |
---|
704 | n/a | } |
---|
705 | n/a | } |
---|
706 | n/a | |
---|
707 | n/a | static PyObject * |
---|
708 | n/a | vBOOL_get(void *ptr, Py_ssize_t size) |
---|
709 | n/a | { |
---|
710 | n/a | return PyBool_FromLong((long)*(short int *)ptr); |
---|
711 | n/a | } |
---|
712 | n/a | #endif |
---|
713 | n/a | |
---|
714 | n/a | static PyObject * |
---|
715 | n/a | bool_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
716 | n/a | { |
---|
717 | n/a | switch (PyObject_IsTrue(value)) { |
---|
718 | n/a | case -1: |
---|
719 | n/a | return NULL; |
---|
720 | n/a | case 0: |
---|
721 | n/a | *(_Bool *)ptr = 0; |
---|
722 | n/a | _RET(value); |
---|
723 | n/a | default: |
---|
724 | n/a | *(_Bool *)ptr = 1; |
---|
725 | n/a | _RET(value); |
---|
726 | n/a | } |
---|
727 | n/a | } |
---|
728 | n/a | |
---|
729 | n/a | static PyObject * |
---|
730 | n/a | bool_get(void *ptr, Py_ssize_t size) |
---|
731 | n/a | { |
---|
732 | n/a | return PyBool_FromLong((long)*(_Bool *)ptr); |
---|
733 | n/a | } |
---|
734 | n/a | |
---|
735 | n/a | static PyObject * |
---|
736 | n/a | I_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
737 | n/a | { |
---|
738 | n/a | unsigned long val; |
---|
739 | n/a | unsigned int x; |
---|
740 | n/a | if (get_ulong(value, &val) < 0) |
---|
741 | n/a | return NULL; |
---|
742 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
743 | n/a | x = SET(unsigned int, x, val, size); |
---|
744 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
745 | n/a | _RET(value); |
---|
746 | n/a | } |
---|
747 | n/a | |
---|
748 | n/a | static PyObject * |
---|
749 | n/a | I_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
750 | n/a | { |
---|
751 | n/a | unsigned long val; |
---|
752 | n/a | unsigned int field; |
---|
753 | n/a | if (get_ulong(value, &val) < 0) |
---|
754 | n/a | return NULL; |
---|
755 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
756 | n/a | field = SWAP_INT(field); |
---|
757 | n/a | field = SET(unsigned int, field, (unsigned int)val, size); |
---|
758 | n/a | field = SWAP_INT(field); |
---|
759 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
760 | n/a | _RET(value); |
---|
761 | n/a | } |
---|
762 | n/a | |
---|
763 | n/a | |
---|
764 | n/a | static PyObject * |
---|
765 | n/a | I_get(void *ptr, Py_ssize_t size) |
---|
766 | n/a | { |
---|
767 | n/a | unsigned int val; |
---|
768 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
769 | n/a | GET_BITFIELD(val, size); |
---|
770 | n/a | return PyLong_FromUnsignedLong(val); |
---|
771 | n/a | } |
---|
772 | n/a | |
---|
773 | n/a | static PyObject * |
---|
774 | n/a | I_get_sw(void *ptr, Py_ssize_t size) |
---|
775 | n/a | { |
---|
776 | n/a | unsigned int val; |
---|
777 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
778 | n/a | val = SWAP_INT(val); |
---|
779 | n/a | GET_BITFIELD(val, size); |
---|
780 | n/a | return PyLong_FromUnsignedLong(val); |
---|
781 | n/a | } |
---|
782 | n/a | |
---|
783 | n/a | static PyObject * |
---|
784 | n/a | l_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
785 | n/a | { |
---|
786 | n/a | long val; |
---|
787 | n/a | long x; |
---|
788 | n/a | if (get_long(value, &val) < 0) |
---|
789 | n/a | return NULL; |
---|
790 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
791 | n/a | x = SET(long, x, val, size); |
---|
792 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
793 | n/a | _RET(value); |
---|
794 | n/a | } |
---|
795 | n/a | |
---|
796 | n/a | static PyObject * |
---|
797 | n/a | l_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
798 | n/a | { |
---|
799 | n/a | long val; |
---|
800 | n/a | long field; |
---|
801 | n/a | if (get_long(value, &val) < 0) |
---|
802 | n/a | return NULL; |
---|
803 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
804 | n/a | field = SWAP_LONG(field); |
---|
805 | n/a | field = SET(long, field, val, size); |
---|
806 | n/a | field = SWAP_LONG(field); |
---|
807 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
808 | n/a | _RET(value); |
---|
809 | n/a | } |
---|
810 | n/a | |
---|
811 | n/a | |
---|
812 | n/a | static PyObject * |
---|
813 | n/a | l_get(void *ptr, Py_ssize_t size) |
---|
814 | n/a | { |
---|
815 | n/a | long val; |
---|
816 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
817 | n/a | GET_BITFIELD(val, size); |
---|
818 | n/a | return PyLong_FromLong(val); |
---|
819 | n/a | } |
---|
820 | n/a | |
---|
821 | n/a | static PyObject * |
---|
822 | n/a | l_get_sw(void *ptr, Py_ssize_t size) |
---|
823 | n/a | { |
---|
824 | n/a | long val; |
---|
825 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
826 | n/a | val = SWAP_LONG(val); |
---|
827 | n/a | GET_BITFIELD(val, size); |
---|
828 | n/a | return PyLong_FromLong(val); |
---|
829 | n/a | } |
---|
830 | n/a | |
---|
831 | n/a | static PyObject * |
---|
832 | n/a | L_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
833 | n/a | { |
---|
834 | n/a | unsigned long val; |
---|
835 | n/a | unsigned long x; |
---|
836 | n/a | if (get_ulong(value, &val) < 0) |
---|
837 | n/a | return NULL; |
---|
838 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
839 | n/a | x = SET(unsigned long, x, val, size); |
---|
840 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
841 | n/a | _RET(value); |
---|
842 | n/a | } |
---|
843 | n/a | |
---|
844 | n/a | static PyObject * |
---|
845 | n/a | L_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
846 | n/a | { |
---|
847 | n/a | unsigned long val; |
---|
848 | n/a | unsigned long field; |
---|
849 | n/a | if (get_ulong(value, &val) < 0) |
---|
850 | n/a | return NULL; |
---|
851 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
852 | n/a | field = SWAP_LONG(field); |
---|
853 | n/a | field = SET(unsigned long, field, val, size); |
---|
854 | n/a | field = SWAP_LONG(field); |
---|
855 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
856 | n/a | _RET(value); |
---|
857 | n/a | } |
---|
858 | n/a | |
---|
859 | n/a | |
---|
860 | n/a | static PyObject * |
---|
861 | n/a | L_get(void *ptr, Py_ssize_t size) |
---|
862 | n/a | { |
---|
863 | n/a | unsigned long val; |
---|
864 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
865 | n/a | GET_BITFIELD(val, size); |
---|
866 | n/a | return PyLong_FromUnsignedLong(val); |
---|
867 | n/a | } |
---|
868 | n/a | |
---|
869 | n/a | static PyObject * |
---|
870 | n/a | L_get_sw(void *ptr, Py_ssize_t size) |
---|
871 | n/a | { |
---|
872 | n/a | unsigned long val; |
---|
873 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
874 | n/a | val = SWAP_LONG(val); |
---|
875 | n/a | GET_BITFIELD(val, size); |
---|
876 | n/a | return PyLong_FromUnsignedLong(val); |
---|
877 | n/a | } |
---|
878 | n/a | |
---|
879 | n/a | static PyObject * |
---|
880 | n/a | q_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
881 | n/a | { |
---|
882 | n/a | long long val; |
---|
883 | n/a | long long x; |
---|
884 | n/a | if (get_longlong(value, &val) < 0) |
---|
885 | n/a | return NULL; |
---|
886 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
887 | n/a | x = SET(long long, x, val, size); |
---|
888 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
889 | n/a | _RET(value); |
---|
890 | n/a | } |
---|
891 | n/a | |
---|
892 | n/a | static PyObject * |
---|
893 | n/a | q_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
894 | n/a | { |
---|
895 | n/a | long long val; |
---|
896 | n/a | long long field; |
---|
897 | n/a | if (get_longlong(value, &val) < 0) |
---|
898 | n/a | return NULL; |
---|
899 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
900 | n/a | field = SWAP_8(field); |
---|
901 | n/a | field = SET(long long, field, val, size); |
---|
902 | n/a | field = SWAP_8(field); |
---|
903 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
904 | n/a | _RET(value); |
---|
905 | n/a | } |
---|
906 | n/a | |
---|
907 | n/a | static PyObject * |
---|
908 | n/a | q_get(void *ptr, Py_ssize_t size) |
---|
909 | n/a | { |
---|
910 | n/a | long long val; |
---|
911 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
912 | n/a | GET_BITFIELD(val, size); |
---|
913 | n/a | return PyLong_FromLongLong(val); |
---|
914 | n/a | } |
---|
915 | n/a | |
---|
916 | n/a | static PyObject * |
---|
917 | n/a | q_get_sw(void *ptr, Py_ssize_t size) |
---|
918 | n/a | { |
---|
919 | n/a | long long val; |
---|
920 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
921 | n/a | val = SWAP_8(val); |
---|
922 | n/a | GET_BITFIELD(val, size); |
---|
923 | n/a | return PyLong_FromLongLong(val); |
---|
924 | n/a | } |
---|
925 | n/a | |
---|
926 | n/a | static PyObject * |
---|
927 | n/a | Q_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
928 | n/a | { |
---|
929 | n/a | unsigned long long val; |
---|
930 | n/a | unsigned long long x; |
---|
931 | n/a | if (get_ulonglong(value, &val) < 0) |
---|
932 | n/a | return NULL; |
---|
933 | n/a | memcpy(&x, ptr, sizeof(x)); |
---|
934 | n/a | x = SET(long long, x, val, size); |
---|
935 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
936 | n/a | _RET(value); |
---|
937 | n/a | } |
---|
938 | n/a | |
---|
939 | n/a | static PyObject * |
---|
940 | n/a | Q_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
941 | n/a | { |
---|
942 | n/a | unsigned long long val; |
---|
943 | n/a | unsigned long long field; |
---|
944 | n/a | if (get_ulonglong(value, &val) < 0) |
---|
945 | n/a | return NULL; |
---|
946 | n/a | memcpy(&field, ptr, sizeof(field)); |
---|
947 | n/a | field = SWAP_8(field); |
---|
948 | n/a | field = SET(unsigned long long, field, val, size); |
---|
949 | n/a | field = SWAP_8(field); |
---|
950 | n/a | memcpy(ptr, &field, sizeof(field)); |
---|
951 | n/a | _RET(value); |
---|
952 | n/a | } |
---|
953 | n/a | |
---|
954 | n/a | static PyObject * |
---|
955 | n/a | Q_get(void *ptr, Py_ssize_t size) |
---|
956 | n/a | { |
---|
957 | n/a | unsigned long long val; |
---|
958 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
959 | n/a | GET_BITFIELD(val, size); |
---|
960 | n/a | return PyLong_FromUnsignedLongLong(val); |
---|
961 | n/a | } |
---|
962 | n/a | |
---|
963 | n/a | static PyObject * |
---|
964 | n/a | Q_get_sw(void *ptr, Py_ssize_t size) |
---|
965 | n/a | { |
---|
966 | n/a | unsigned long long val; |
---|
967 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
968 | n/a | val = SWAP_8(val); |
---|
969 | n/a | GET_BITFIELD(val, size); |
---|
970 | n/a | return PyLong_FromUnsignedLongLong(val); |
---|
971 | n/a | } |
---|
972 | n/a | |
---|
973 | n/a | /***************************************************************** |
---|
974 | n/a | * non-integer accessor methods, not supporting bit fields |
---|
975 | n/a | */ |
---|
976 | n/a | |
---|
977 | n/a | |
---|
978 | n/a | static PyObject * |
---|
979 | n/a | g_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
980 | n/a | { |
---|
981 | n/a | long double x; |
---|
982 | n/a | |
---|
983 | n/a | x = PyFloat_AsDouble(value); |
---|
984 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
985 | n/a | return NULL; |
---|
986 | n/a | memcpy(ptr, &x, sizeof(long double)); |
---|
987 | n/a | _RET(value); |
---|
988 | n/a | } |
---|
989 | n/a | |
---|
990 | n/a | static PyObject * |
---|
991 | n/a | g_get(void *ptr, Py_ssize_t size) |
---|
992 | n/a | { |
---|
993 | n/a | long double val; |
---|
994 | n/a | memcpy(&val, ptr, sizeof(long double)); |
---|
995 | n/a | return PyFloat_FromDouble(val); |
---|
996 | n/a | } |
---|
997 | n/a | |
---|
998 | n/a | static PyObject * |
---|
999 | n/a | d_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1000 | n/a | { |
---|
1001 | n/a | double x; |
---|
1002 | n/a | |
---|
1003 | n/a | x = PyFloat_AsDouble(value); |
---|
1004 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
1005 | n/a | return NULL; |
---|
1006 | n/a | memcpy(ptr, &x, sizeof(double)); |
---|
1007 | n/a | _RET(value); |
---|
1008 | n/a | } |
---|
1009 | n/a | |
---|
1010 | n/a | static PyObject * |
---|
1011 | n/a | d_get(void *ptr, Py_ssize_t size) |
---|
1012 | n/a | { |
---|
1013 | n/a | double val; |
---|
1014 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
1015 | n/a | return PyFloat_FromDouble(val); |
---|
1016 | n/a | } |
---|
1017 | n/a | |
---|
1018 | n/a | static PyObject * |
---|
1019 | n/a | d_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1020 | n/a | { |
---|
1021 | n/a | double x; |
---|
1022 | n/a | |
---|
1023 | n/a | x = PyFloat_AsDouble(value); |
---|
1024 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
1025 | n/a | return NULL; |
---|
1026 | n/a | #ifdef WORDS_BIGENDIAN |
---|
1027 | n/a | if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1)) |
---|
1028 | n/a | return NULL; |
---|
1029 | n/a | #else |
---|
1030 | n/a | if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0)) |
---|
1031 | n/a | return NULL; |
---|
1032 | n/a | #endif |
---|
1033 | n/a | _RET(value); |
---|
1034 | n/a | } |
---|
1035 | n/a | |
---|
1036 | n/a | static PyObject * |
---|
1037 | n/a | d_get_sw(void *ptr, Py_ssize_t size) |
---|
1038 | n/a | { |
---|
1039 | n/a | #ifdef WORDS_BIGENDIAN |
---|
1040 | n/a | return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1)); |
---|
1041 | n/a | #else |
---|
1042 | n/a | return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0)); |
---|
1043 | n/a | #endif |
---|
1044 | n/a | } |
---|
1045 | n/a | |
---|
1046 | n/a | static PyObject * |
---|
1047 | n/a | f_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1048 | n/a | { |
---|
1049 | n/a | float x; |
---|
1050 | n/a | |
---|
1051 | n/a | x = (float)PyFloat_AsDouble(value); |
---|
1052 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
1053 | n/a | return NULL; |
---|
1054 | n/a | memcpy(ptr, &x, sizeof(x)); |
---|
1055 | n/a | _RET(value); |
---|
1056 | n/a | } |
---|
1057 | n/a | |
---|
1058 | n/a | static PyObject * |
---|
1059 | n/a | f_get(void *ptr, Py_ssize_t size) |
---|
1060 | n/a | { |
---|
1061 | n/a | float val; |
---|
1062 | n/a | memcpy(&val, ptr, sizeof(val)); |
---|
1063 | n/a | return PyFloat_FromDouble(val); |
---|
1064 | n/a | } |
---|
1065 | n/a | |
---|
1066 | n/a | static PyObject * |
---|
1067 | n/a | f_set_sw(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1068 | n/a | { |
---|
1069 | n/a | float x; |
---|
1070 | n/a | |
---|
1071 | n/a | x = (float)PyFloat_AsDouble(value); |
---|
1072 | n/a | if (x == -1 && PyErr_Occurred()) |
---|
1073 | n/a | return NULL; |
---|
1074 | n/a | #ifdef WORDS_BIGENDIAN |
---|
1075 | n/a | if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1)) |
---|
1076 | n/a | return NULL; |
---|
1077 | n/a | #else |
---|
1078 | n/a | if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0)) |
---|
1079 | n/a | return NULL; |
---|
1080 | n/a | #endif |
---|
1081 | n/a | _RET(value); |
---|
1082 | n/a | } |
---|
1083 | n/a | |
---|
1084 | n/a | static PyObject * |
---|
1085 | n/a | f_get_sw(void *ptr, Py_ssize_t size) |
---|
1086 | n/a | { |
---|
1087 | n/a | #ifdef WORDS_BIGENDIAN |
---|
1088 | n/a | return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1)); |
---|
1089 | n/a | #else |
---|
1090 | n/a | return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0)); |
---|
1091 | n/a | #endif |
---|
1092 | n/a | } |
---|
1093 | n/a | |
---|
1094 | n/a | /* |
---|
1095 | n/a | py_object refcounts: |
---|
1096 | n/a | |
---|
1097 | n/a | 1. If we have a py_object instance, O_get must Py_INCREF the returned |
---|
1098 | n/a | object, of course. If O_get is called from a function result, no py_object |
---|
1099 | n/a | instance is created - so callproc.c::GetResult has to call Py_DECREF. |
---|
1100 | n/a | |
---|
1101 | n/a | 2. The memory block in py_object owns a refcount. So, py_object must call |
---|
1102 | n/a | Py_DECREF on destruction. Maybe only when b_needsfree is non-zero. |
---|
1103 | n/a | */ |
---|
1104 | n/a | static PyObject * |
---|
1105 | n/a | O_get(void *ptr, Py_ssize_t size) |
---|
1106 | n/a | { |
---|
1107 | n/a | PyObject *ob = *(PyObject **)ptr; |
---|
1108 | n/a | if (ob == NULL) { |
---|
1109 | n/a | if (!PyErr_Occurred()) |
---|
1110 | n/a | /* Set an error if not yet set */ |
---|
1111 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1112 | n/a | "PyObject is NULL"); |
---|
1113 | n/a | return NULL; |
---|
1114 | n/a | } |
---|
1115 | n/a | Py_INCREF(ob); |
---|
1116 | n/a | return ob; |
---|
1117 | n/a | } |
---|
1118 | n/a | |
---|
1119 | n/a | static PyObject * |
---|
1120 | n/a | O_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1121 | n/a | { |
---|
1122 | n/a | /* Hm, does the memory block need it's own refcount or not? */ |
---|
1123 | n/a | *(PyObject **)ptr = value; |
---|
1124 | n/a | Py_INCREF(value); |
---|
1125 | n/a | return value; |
---|
1126 | n/a | } |
---|
1127 | n/a | |
---|
1128 | n/a | |
---|
1129 | n/a | static PyObject * |
---|
1130 | n/a | c_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1131 | n/a | { |
---|
1132 | n/a | if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) { |
---|
1133 | n/a | *(char *)ptr = PyBytes_AS_STRING(value)[0]; |
---|
1134 | n/a | _RET(value); |
---|
1135 | n/a | } |
---|
1136 | n/a | if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) { |
---|
1137 | n/a | *(char *)ptr = PyByteArray_AS_STRING(value)[0]; |
---|
1138 | n/a | _RET(value); |
---|
1139 | n/a | } |
---|
1140 | n/a | if (PyLong_Check(value)) |
---|
1141 | n/a | { |
---|
1142 | n/a | long longval = PyLong_AS_LONG(value); |
---|
1143 | n/a | if (longval < 0 || longval >= 256) |
---|
1144 | n/a | goto error; |
---|
1145 | n/a | *(char *)ptr = (char)longval; |
---|
1146 | n/a | _RET(value); |
---|
1147 | n/a | } |
---|
1148 | n/a | error: |
---|
1149 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1150 | n/a | "one character bytes, bytearray or integer expected"); |
---|
1151 | n/a | return NULL; |
---|
1152 | n/a | } |
---|
1153 | n/a | |
---|
1154 | n/a | |
---|
1155 | n/a | static PyObject * |
---|
1156 | n/a | c_get(void *ptr, Py_ssize_t size) |
---|
1157 | n/a | { |
---|
1158 | n/a | return PyBytes_FromStringAndSize((char *)ptr, 1); |
---|
1159 | n/a | } |
---|
1160 | n/a | |
---|
1161 | n/a | #ifdef CTYPES_UNICODE |
---|
1162 | n/a | /* u - a single wchar_t character */ |
---|
1163 | n/a | static PyObject * |
---|
1164 | n/a | u_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1165 | n/a | { |
---|
1166 | n/a | Py_ssize_t len; |
---|
1167 | n/a | wchar_t chars[2]; |
---|
1168 | n/a | if (!PyUnicode_Check(value)) { |
---|
1169 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1170 | n/a | "unicode string expected instead of %s instance", |
---|
1171 | n/a | value->ob_type->tp_name); |
---|
1172 | n/a | return NULL; |
---|
1173 | n/a | } else |
---|
1174 | n/a | Py_INCREF(value); |
---|
1175 | n/a | |
---|
1176 | n/a | len = PyUnicode_AsWideChar(value, chars, 2); |
---|
1177 | n/a | if (len != 1) { |
---|
1178 | n/a | Py_DECREF(value); |
---|
1179 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1180 | n/a | "one character unicode string expected"); |
---|
1181 | n/a | return NULL; |
---|
1182 | n/a | } |
---|
1183 | n/a | |
---|
1184 | n/a | *(wchar_t *)ptr = chars[0]; |
---|
1185 | n/a | Py_DECREF(value); |
---|
1186 | n/a | |
---|
1187 | n/a | _RET(value); |
---|
1188 | n/a | } |
---|
1189 | n/a | |
---|
1190 | n/a | |
---|
1191 | n/a | static PyObject * |
---|
1192 | n/a | u_get(void *ptr, Py_ssize_t size) |
---|
1193 | n/a | { |
---|
1194 | n/a | return PyUnicode_FromWideChar((wchar_t *)ptr, 1); |
---|
1195 | n/a | } |
---|
1196 | n/a | |
---|
1197 | n/a | /* U - a unicode string */ |
---|
1198 | n/a | static PyObject * |
---|
1199 | n/a | U_get(void *ptr, Py_ssize_t size) |
---|
1200 | n/a | { |
---|
1201 | n/a | Py_ssize_t len; |
---|
1202 | n/a | wchar_t *p; |
---|
1203 | n/a | |
---|
1204 | n/a | size /= sizeof(wchar_t); /* we count character units here, not bytes */ |
---|
1205 | n/a | |
---|
1206 | n/a | /* We need 'result' to be able to count the characters with wcslen, |
---|
1207 | n/a | since ptr may not be NUL terminated. If the length is smaller (if |
---|
1208 | n/a | it was actually NUL terminated, we construct a new one and throw |
---|
1209 | n/a | away the result. |
---|
1210 | n/a | */ |
---|
1211 | n/a | /* chop off at the first NUL character, if any. */ |
---|
1212 | n/a | p = (wchar_t*)ptr; |
---|
1213 | n/a | for (len = 0; len < size; ++len) { |
---|
1214 | n/a | if (!p[len]) |
---|
1215 | n/a | break; |
---|
1216 | n/a | } |
---|
1217 | n/a | |
---|
1218 | n/a | return PyUnicode_FromWideChar((wchar_t *)ptr, len); |
---|
1219 | n/a | } |
---|
1220 | n/a | |
---|
1221 | n/a | static PyObject * |
---|
1222 | n/a | U_set(void *ptr, PyObject *value, Py_ssize_t length) |
---|
1223 | n/a | { |
---|
1224 | n/a | Py_UNICODE *wstr; |
---|
1225 | n/a | Py_ssize_t size; |
---|
1226 | n/a | |
---|
1227 | n/a | /* It's easier to calculate in characters than in bytes */ |
---|
1228 | n/a | length /= sizeof(wchar_t); |
---|
1229 | n/a | |
---|
1230 | n/a | if (!PyUnicode_Check(value)) { |
---|
1231 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1232 | n/a | "unicode string expected instead of %s instance", |
---|
1233 | n/a | value->ob_type->tp_name); |
---|
1234 | n/a | return NULL; |
---|
1235 | n/a | } |
---|
1236 | n/a | |
---|
1237 | n/a | wstr = PyUnicode_AsUnicodeAndSize(value, &size); |
---|
1238 | n/a | if (wstr == NULL) |
---|
1239 | n/a | return NULL; |
---|
1240 | n/a | if (size > length) { |
---|
1241 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1242 | n/a | "string too long (%zd, maximum length %zd)", |
---|
1243 | n/a | size, length); |
---|
1244 | n/a | return NULL; |
---|
1245 | n/a | } else if (size < length-1) |
---|
1246 | n/a | /* copy terminating NUL character if there is space */ |
---|
1247 | n/a | size += 1; |
---|
1248 | n/a | |
---|
1249 | n/a | if (PyUnicode_AsWideChar(value, (wchar_t *)ptr, size) == -1) { |
---|
1250 | n/a | return NULL; |
---|
1251 | n/a | } |
---|
1252 | n/a | |
---|
1253 | n/a | Py_INCREF(value); |
---|
1254 | n/a | return value; |
---|
1255 | n/a | } |
---|
1256 | n/a | |
---|
1257 | n/a | #endif |
---|
1258 | n/a | |
---|
1259 | n/a | static PyObject * |
---|
1260 | n/a | s_get(void *ptr, Py_ssize_t size) |
---|
1261 | n/a | { |
---|
1262 | n/a | Py_ssize_t i; |
---|
1263 | n/a | char *p; |
---|
1264 | n/a | |
---|
1265 | n/a | p = (char *)ptr; |
---|
1266 | n/a | for (i = 0; i < size; ++i) { |
---|
1267 | n/a | if (*p++ == '\0') |
---|
1268 | n/a | break; |
---|
1269 | n/a | } |
---|
1270 | n/a | |
---|
1271 | n/a | return PyBytes_FromStringAndSize((char *)ptr, (Py_ssize_t)i); |
---|
1272 | n/a | } |
---|
1273 | n/a | |
---|
1274 | n/a | static PyObject * |
---|
1275 | n/a | s_set(void *ptr, PyObject *value, Py_ssize_t length) |
---|
1276 | n/a | { |
---|
1277 | n/a | char *data; |
---|
1278 | n/a | Py_ssize_t size; |
---|
1279 | n/a | |
---|
1280 | n/a | if(!PyBytes_Check(value)) { |
---|
1281 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1282 | n/a | "expected bytes, %s found", |
---|
1283 | n/a | value->ob_type->tp_name); |
---|
1284 | n/a | return NULL; |
---|
1285 | n/a | } |
---|
1286 | n/a | |
---|
1287 | n/a | data = PyBytes_AS_STRING(value); |
---|
1288 | n/a | size = strlen(data); /* XXX Why not Py_SIZE(value)? */ |
---|
1289 | n/a | if (size < length) { |
---|
1290 | n/a | /* This will copy the terminating NUL character |
---|
1291 | n/a | * if there is space for it. |
---|
1292 | n/a | */ |
---|
1293 | n/a | ++size; |
---|
1294 | n/a | } else if (size > length) { |
---|
1295 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1296 | n/a | "bytes too long (%zd, maximum length %zd)", |
---|
1297 | n/a | size, length); |
---|
1298 | n/a | return NULL; |
---|
1299 | n/a | } |
---|
1300 | n/a | /* Also copy the terminating NUL character if there is space */ |
---|
1301 | n/a | memcpy((char *)ptr, data, size); |
---|
1302 | n/a | |
---|
1303 | n/a | _RET(value); |
---|
1304 | n/a | } |
---|
1305 | n/a | |
---|
1306 | n/a | static PyObject * |
---|
1307 | n/a | z_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1308 | n/a | { |
---|
1309 | n/a | if (value == Py_None) { |
---|
1310 | n/a | *(char **)ptr = NULL; |
---|
1311 | n/a | Py_INCREF(value); |
---|
1312 | n/a | return value; |
---|
1313 | n/a | } |
---|
1314 | n/a | if (PyBytes_Check(value)) { |
---|
1315 | n/a | *(char **)ptr = PyBytes_AsString(value); |
---|
1316 | n/a | Py_INCREF(value); |
---|
1317 | n/a | return value; |
---|
1318 | n/a | } else if (PyLong_Check(value)) { |
---|
1319 | n/a | #if SIZEOF_VOID_P == SIZEOF_LONG_LONG |
---|
1320 | n/a | *(char **)ptr = (char *)PyLong_AsUnsignedLongLongMask(value); |
---|
1321 | n/a | #else |
---|
1322 | n/a | *(char **)ptr = (char *)PyLong_AsUnsignedLongMask(value); |
---|
1323 | n/a | #endif |
---|
1324 | n/a | _RET(value); |
---|
1325 | n/a | } |
---|
1326 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1327 | n/a | "bytes or integer address expected instead of %s instance", |
---|
1328 | n/a | value->ob_type->tp_name); |
---|
1329 | n/a | return NULL; |
---|
1330 | n/a | } |
---|
1331 | n/a | |
---|
1332 | n/a | static PyObject * |
---|
1333 | n/a | z_get(void *ptr, Py_ssize_t size) |
---|
1334 | n/a | { |
---|
1335 | n/a | /* XXX What about invalid pointers ??? */ |
---|
1336 | n/a | if (*(void **)ptr) { |
---|
1337 | n/a | return PyBytes_FromStringAndSize(*(char **)ptr, |
---|
1338 | n/a | strlen(*(char **)ptr)); |
---|
1339 | n/a | } else { |
---|
1340 | n/a | Py_RETURN_NONE; |
---|
1341 | n/a | } |
---|
1342 | n/a | } |
---|
1343 | n/a | |
---|
1344 | n/a | #ifdef CTYPES_UNICODE |
---|
1345 | n/a | static PyObject * |
---|
1346 | n/a | Z_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1347 | n/a | { |
---|
1348 | n/a | PyObject *keep; |
---|
1349 | n/a | wchar_t *buffer; |
---|
1350 | n/a | |
---|
1351 | n/a | if (value == Py_None) { |
---|
1352 | n/a | *(wchar_t **)ptr = NULL; |
---|
1353 | n/a | Py_INCREF(value); |
---|
1354 | n/a | return value; |
---|
1355 | n/a | } |
---|
1356 | n/a | if (PyLong_Check(value)) { |
---|
1357 | n/a | #if SIZEOF_VOID_P == SIZEOF_LONG_LONG |
---|
1358 | n/a | *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongLongMask(value); |
---|
1359 | n/a | #else |
---|
1360 | n/a | *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongMask(value); |
---|
1361 | n/a | #endif |
---|
1362 | n/a | Py_RETURN_NONE; |
---|
1363 | n/a | } |
---|
1364 | n/a | if (!PyUnicode_Check(value)) { |
---|
1365 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1366 | n/a | "unicode string or integer address expected instead of %s instance", |
---|
1367 | n/a | value->ob_type->tp_name); |
---|
1368 | n/a | return NULL; |
---|
1369 | n/a | } |
---|
1370 | n/a | |
---|
1371 | n/a | /* We must create a wchar_t* buffer from the unicode object, |
---|
1372 | n/a | and keep it alive */ |
---|
1373 | n/a | buffer = PyUnicode_AsWideCharString(value, NULL); |
---|
1374 | n/a | if (!buffer) |
---|
1375 | n/a | return NULL; |
---|
1376 | n/a | keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor); |
---|
1377 | n/a | if (!keep) { |
---|
1378 | n/a | PyMem_Free(buffer); |
---|
1379 | n/a | return NULL; |
---|
1380 | n/a | } |
---|
1381 | n/a | *(wchar_t **)ptr = buffer; |
---|
1382 | n/a | return keep; |
---|
1383 | n/a | } |
---|
1384 | n/a | |
---|
1385 | n/a | static PyObject * |
---|
1386 | n/a | Z_get(void *ptr, Py_ssize_t size) |
---|
1387 | n/a | { |
---|
1388 | n/a | wchar_t *p; |
---|
1389 | n/a | p = *(wchar_t **)ptr; |
---|
1390 | n/a | if (p) { |
---|
1391 | n/a | return PyUnicode_FromWideChar(p, wcslen(p)); |
---|
1392 | n/a | } else { |
---|
1393 | n/a | Py_RETURN_NONE; |
---|
1394 | n/a | } |
---|
1395 | n/a | } |
---|
1396 | n/a | #endif |
---|
1397 | n/a | |
---|
1398 | n/a | #ifdef MS_WIN32 |
---|
1399 | n/a | static PyObject * |
---|
1400 | n/a | BSTR_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1401 | n/a | { |
---|
1402 | n/a | BSTR bstr; |
---|
1403 | n/a | |
---|
1404 | n/a | /* convert value into a PyUnicodeObject or NULL */ |
---|
1405 | n/a | if (Py_None == value) { |
---|
1406 | n/a | value = NULL; |
---|
1407 | n/a | } else if (!PyUnicode_Check(value)) { |
---|
1408 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1409 | n/a | "unicode string expected instead of %s instance", |
---|
1410 | n/a | value->ob_type->tp_name); |
---|
1411 | n/a | return NULL; |
---|
1412 | n/a | } |
---|
1413 | n/a | |
---|
1414 | n/a | /* create a BSTR from value */ |
---|
1415 | n/a | if (value) { |
---|
1416 | n/a | wchar_t* wvalue; |
---|
1417 | n/a | Py_ssize_t wsize; |
---|
1418 | n/a | wvalue = PyUnicode_AsUnicodeAndSize(value, &wsize); |
---|
1419 | n/a | if (wvalue == NULL) |
---|
1420 | n/a | return NULL; |
---|
1421 | n/a | if ((unsigned) wsize != wsize) { |
---|
1422 | n/a | PyErr_SetString(PyExc_ValueError, "String too long for BSTR"); |
---|
1423 | n/a | return NULL; |
---|
1424 | n/a | } |
---|
1425 | n/a | bstr = SysAllocStringLen(wvalue, (unsigned)wsize); |
---|
1426 | n/a | } else |
---|
1427 | n/a | bstr = NULL; |
---|
1428 | n/a | |
---|
1429 | n/a | /* free the previous contents, if any */ |
---|
1430 | n/a | if (*(BSTR *)ptr) |
---|
1431 | n/a | SysFreeString(*(BSTR *)ptr); |
---|
1432 | n/a | |
---|
1433 | n/a | /* and store it */ |
---|
1434 | n/a | *(BSTR *)ptr = bstr; |
---|
1435 | n/a | |
---|
1436 | n/a | /* We don't need to keep any other object */ |
---|
1437 | n/a | _RET(value); |
---|
1438 | n/a | } |
---|
1439 | n/a | |
---|
1440 | n/a | |
---|
1441 | n/a | static PyObject * |
---|
1442 | n/a | BSTR_get(void *ptr, Py_ssize_t size) |
---|
1443 | n/a | { |
---|
1444 | n/a | BSTR p; |
---|
1445 | n/a | p = *(BSTR *)ptr; |
---|
1446 | n/a | if (p) |
---|
1447 | n/a | return PyUnicode_FromWideChar(p, SysStringLen(p)); |
---|
1448 | n/a | else { |
---|
1449 | n/a | /* Hm, it seems NULL pointer and zero length string are the |
---|
1450 | n/a | same in BSTR, see Don Box, p 81 |
---|
1451 | n/a | */ |
---|
1452 | n/a | Py_RETURN_NONE; |
---|
1453 | n/a | } |
---|
1454 | n/a | } |
---|
1455 | n/a | #endif |
---|
1456 | n/a | |
---|
1457 | n/a | static PyObject * |
---|
1458 | n/a | P_set(void *ptr, PyObject *value, Py_ssize_t size) |
---|
1459 | n/a | { |
---|
1460 | n/a | void *v; |
---|
1461 | n/a | if (value == Py_None) { |
---|
1462 | n/a | *(void **)ptr = NULL; |
---|
1463 | n/a | _RET(value); |
---|
1464 | n/a | } |
---|
1465 | n/a | |
---|
1466 | n/a | if (!PyLong_Check(value)) { |
---|
1467 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1468 | n/a | "cannot be converted to pointer"); |
---|
1469 | n/a | return NULL; |
---|
1470 | n/a | } |
---|
1471 | n/a | |
---|
1472 | n/a | #if SIZEOF_VOID_P <= SIZEOF_LONG |
---|
1473 | n/a | v = (void *)PyLong_AsUnsignedLongMask(value); |
---|
1474 | n/a | #else |
---|
1475 | n/a | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
---|
1476 | n/a | # error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)" |
---|
1477 | n/a | #endif |
---|
1478 | n/a | v = (void *)PyLong_AsUnsignedLongLongMask(value); |
---|
1479 | n/a | #endif |
---|
1480 | n/a | |
---|
1481 | n/a | if (PyErr_Occurred()) |
---|
1482 | n/a | return NULL; |
---|
1483 | n/a | |
---|
1484 | n/a | *(void **)ptr = v; |
---|
1485 | n/a | _RET(value); |
---|
1486 | n/a | } |
---|
1487 | n/a | |
---|
1488 | n/a | static PyObject * |
---|
1489 | n/a | P_get(void *ptr, Py_ssize_t size) |
---|
1490 | n/a | { |
---|
1491 | n/a | if (*(void **)ptr == NULL) { |
---|
1492 | n/a | Py_RETURN_NONE; |
---|
1493 | n/a | } |
---|
1494 | n/a | return PyLong_FromVoidPtr(*(void **)ptr); |
---|
1495 | n/a | } |
---|
1496 | n/a | |
---|
1497 | n/a | static struct fielddesc formattable[] = { |
---|
1498 | n/a | { 's', s_set, s_get, &ffi_type_pointer}, |
---|
1499 | n/a | { 'b', b_set, b_get, &ffi_type_schar}, |
---|
1500 | n/a | { 'B', B_set, B_get, &ffi_type_uchar}, |
---|
1501 | n/a | { 'c', c_set, c_get, &ffi_type_schar}, |
---|
1502 | n/a | { 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw}, |
---|
1503 | n/a | { 'g', g_set, g_get, &ffi_type_longdouble}, |
---|
1504 | n/a | { 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw}, |
---|
1505 | n/a | { 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw}, |
---|
1506 | n/a | { 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw}, |
---|
1507 | n/a | { 'i', i_set, i_get, &ffi_type_sint, i_set_sw, i_get_sw}, |
---|
1508 | n/a | { 'I', I_set, I_get, &ffi_type_uint, I_set_sw, I_get_sw}, |
---|
1509 | n/a | /* XXX Hm, sizeof(int) == sizeof(long) doesn't hold on every platform */ |
---|
1510 | n/a | /* As soon as we can get rid of the type codes, this is no longer a problem */ |
---|
1511 | n/a | #if SIZEOF_LONG == 4 |
---|
1512 | n/a | { 'l', l_set, l_get, &ffi_type_sint32, l_set_sw, l_get_sw}, |
---|
1513 | n/a | { 'L', L_set, L_get, &ffi_type_uint32, L_set_sw, L_get_sw}, |
---|
1514 | n/a | #elif SIZEOF_LONG == 8 |
---|
1515 | n/a | { 'l', l_set, l_get, &ffi_type_sint64, l_set_sw, l_get_sw}, |
---|
1516 | n/a | { 'L', L_set, L_get, &ffi_type_uint64, L_set_sw, L_get_sw}, |
---|
1517 | n/a | #else |
---|
1518 | n/a | # error |
---|
1519 | n/a | #endif |
---|
1520 | n/a | #if SIZEOF_LONG_LONG == 8 |
---|
1521 | n/a | { 'q', q_set, q_get, &ffi_type_sint64, q_set_sw, q_get_sw}, |
---|
1522 | n/a | { 'Q', Q_set, Q_get, &ffi_type_uint64, Q_set_sw, Q_get_sw}, |
---|
1523 | n/a | #else |
---|
1524 | n/a | # error |
---|
1525 | n/a | #endif |
---|
1526 | n/a | { 'P', P_set, P_get, &ffi_type_pointer}, |
---|
1527 | n/a | { 'z', z_set, z_get, &ffi_type_pointer}, |
---|
1528 | n/a | #ifdef CTYPES_UNICODE |
---|
1529 | n/a | { 'u', u_set, u_get, NULL}, /* ffi_type set later */ |
---|
1530 | n/a | { 'U', U_set, U_get, &ffi_type_pointer}, |
---|
1531 | n/a | { 'Z', Z_set, Z_get, &ffi_type_pointer}, |
---|
1532 | n/a | #endif |
---|
1533 | n/a | #ifdef MS_WIN32 |
---|
1534 | n/a | { 'X', BSTR_set, BSTR_get, &ffi_type_pointer}, |
---|
1535 | n/a | { 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort}, |
---|
1536 | n/a | #endif |
---|
1537 | n/a | #if SIZEOF__BOOL == 1 |
---|
1538 | n/a | { '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */ |
---|
1539 | n/a | #elif SIZEOF__BOOL == SIZEOF_SHORT |
---|
1540 | n/a | { '?', bool_set, bool_get, &ffi_type_ushort}, |
---|
1541 | n/a | #elif SIZEOF__BOOL == SIZEOF_INT |
---|
1542 | n/a | { '?', bool_set, bool_get, &ffi_type_uint, I_set_sw, I_get_sw}, |
---|
1543 | n/a | #elif SIZEOF__BOOL == SIZEOF_LONG |
---|
1544 | n/a | { '?', bool_set, bool_get, &ffi_type_ulong, L_set_sw, L_get_sw}, |
---|
1545 | n/a | #elif SIZEOF__BOOL == SIZEOF_LONG_LONG |
---|
1546 | n/a | { '?', bool_set, bool_get, &ffi_type_ulong, Q_set_sw, Q_get_sw}, |
---|
1547 | n/a | #endif /* SIZEOF__BOOL */ |
---|
1548 | n/a | { 'O', O_set, O_get, &ffi_type_pointer}, |
---|
1549 | n/a | { 0, NULL, NULL, NULL}, |
---|
1550 | n/a | }; |
---|
1551 | n/a | |
---|
1552 | n/a | /* |
---|
1553 | n/a | Ideas: Implement VARIANT in this table, using 'V' code. |
---|
1554 | n/a | Use '?' as code for BOOL. |
---|
1555 | n/a | */ |
---|
1556 | n/a | |
---|
1557 | n/a | struct fielddesc * |
---|
1558 | n/a | _ctypes_get_fielddesc(const char *fmt) |
---|
1559 | n/a | { |
---|
1560 | n/a | static int initialized = 0; |
---|
1561 | n/a | struct fielddesc *table = formattable; |
---|
1562 | n/a | |
---|
1563 | n/a | if (!initialized) { |
---|
1564 | n/a | initialized = 1; |
---|
1565 | n/a | #ifdef CTYPES_UNICODE |
---|
1566 | n/a | if (sizeof(wchar_t) == sizeof(short)) |
---|
1567 | n/a | _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort; |
---|
1568 | n/a | else if (sizeof(wchar_t) == sizeof(int)) |
---|
1569 | n/a | _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint; |
---|
1570 | n/a | else if (sizeof(wchar_t) == sizeof(long)) |
---|
1571 | n/a | _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong; |
---|
1572 | n/a | #endif |
---|
1573 | n/a | } |
---|
1574 | n/a | |
---|
1575 | n/a | for (; table->code; ++table) { |
---|
1576 | n/a | if (table->code == fmt[0]) |
---|
1577 | n/a | return table; |
---|
1578 | n/a | } |
---|
1579 | n/a | return NULL; |
---|
1580 | n/a | } |
---|
1581 | n/a | |
---|
1582 | n/a | typedef struct { char c; char x; } s_char; |
---|
1583 | n/a | typedef struct { char c; short x; } s_short; |
---|
1584 | n/a | typedef struct { char c; int x; } s_int; |
---|
1585 | n/a | typedef struct { char c; long x; } s_long; |
---|
1586 | n/a | typedef struct { char c; float x; } s_float; |
---|
1587 | n/a | typedef struct { char c; double x; } s_double; |
---|
1588 | n/a | typedef struct { char c; long double x; } s_long_double; |
---|
1589 | n/a | typedef struct { char c; char *x; } s_char_p; |
---|
1590 | n/a | typedef struct { char c; void *x; } s_void_p; |
---|
1591 | n/a | |
---|
1592 | n/a | /* |
---|
1593 | n/a | #define CHAR_ALIGN (sizeof(s_char) - sizeof(char)) |
---|
1594 | n/a | #define SHORT_ALIGN (sizeof(s_short) - sizeof(short)) |
---|
1595 | n/a | #define LONG_ALIGN (sizeof(s_long) - sizeof(long)) |
---|
1596 | n/a | */ |
---|
1597 | n/a | #define INT_ALIGN (sizeof(s_int) - sizeof(int)) |
---|
1598 | n/a | #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float)) |
---|
1599 | n/a | #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double)) |
---|
1600 | n/a | #define LONGDOUBLE_ALIGN (sizeof(s_long_double) - sizeof(long double)) |
---|
1601 | n/a | |
---|
1602 | n/a | /* #define CHAR_P_ALIGN (sizeof(s_char_p) - sizeof(char*)) */ |
---|
1603 | n/a | #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void*)) |
---|
1604 | n/a | |
---|
1605 | n/a | /* |
---|
1606 | n/a | #ifdef HAVE_USABLE_WCHAR_T |
---|
1607 | n/a | typedef struct { char c; wchar_t x; } s_wchar; |
---|
1608 | n/a | typedef struct { char c; wchar_t *x; } s_wchar_p; |
---|
1609 | n/a | |
---|
1610 | n/a | #define WCHAR_ALIGN (sizeof(s_wchar) - sizeof(wchar_t)) |
---|
1611 | n/a | #define WCHAR_P_ALIGN (sizeof(s_wchar_p) - sizeof(wchar_t*)) |
---|
1612 | n/a | #endif |
---|
1613 | n/a | */ |
---|
1614 | n/a | |
---|
1615 | n/a | typedef struct { char c; long long x; } s_long_long; |
---|
1616 | n/a | #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(long long)) |
---|
1617 | n/a | |
---|
1618 | n/a | /* from ffi.h: |
---|
1619 | n/a | typedef struct _ffi_type |
---|
1620 | n/a | { |
---|
1621 | n/a | size_t size; |
---|
1622 | n/a | unsigned short alignment; |
---|
1623 | n/a | unsigned short type; |
---|
1624 | n/a | struct _ffi_type **elements; |
---|
1625 | n/a | } ffi_type; |
---|
1626 | n/a | */ |
---|
1627 | n/a | |
---|
1628 | n/a | /* align and size are bogus for void, but they must not be zero */ |
---|
1629 | n/a | ffi_type ffi_type_void = { 1, 1, FFI_TYPE_VOID }; |
---|
1630 | n/a | |
---|
1631 | n/a | ffi_type ffi_type_uint8 = { 1, 1, FFI_TYPE_UINT8 }; |
---|
1632 | n/a | ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 }; |
---|
1633 | n/a | |
---|
1634 | n/a | ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 }; |
---|
1635 | n/a | ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 }; |
---|
1636 | n/a | |
---|
1637 | n/a | ffi_type ffi_type_uint32 = { 4, INT_ALIGN, FFI_TYPE_UINT32 }; |
---|
1638 | n/a | ffi_type ffi_type_sint32 = { 4, INT_ALIGN, FFI_TYPE_SINT32 }; |
---|
1639 | n/a | |
---|
1640 | n/a | ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 }; |
---|
1641 | n/a | ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 }; |
---|
1642 | n/a | |
---|
1643 | n/a | ffi_type ffi_type_float = { sizeof(float), FLOAT_ALIGN, FFI_TYPE_FLOAT }; |
---|
1644 | n/a | ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE }; |
---|
1645 | n/a | |
---|
1646 | n/a | #ifdef ffi_type_longdouble |
---|
1647 | n/a | #undef ffi_type_longdouble |
---|
1648 | n/a | #endif |
---|
1649 | n/a | /* This is already defined on OSX */ |
---|
1650 | n/a | ffi_type ffi_type_longdouble = { sizeof(long double), LONGDOUBLE_ALIGN, |
---|
1651 | n/a | FFI_TYPE_LONGDOUBLE }; |
---|
1652 | n/a | |
---|
1653 | n/a | ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER }; |
---|
1654 | n/a | |
---|
1655 | n/a | /*---------------- EOF ----------------*/ |
---|