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