Updated documentation for findCaller() to indicate that a 3-tuple is now returned...
[python.git] / Modules / _ctypes / cfield.c
blobad83195276f94d5ccec3f954ce956b6a807dc2b7
1 /*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
5 #include "Python.h"
7 #include <ffi.h>
8 #ifdef MS_WIN32
9 #include <windows.h>
10 #endif
11 #include "ctypes.h"
13 /******************************************************************/
15 CField_Type
17 static PyObject *
18 CField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20 CFieldObject *obj;
21 obj = (CFieldObject *)type->tp_alloc(type, 0);
22 return (PyObject *)obj;
26 * Expects the size, index and offset for the current field in *psize and
27 * *poffset, stores the total size so far in *psize, the offset for the next
28 * field in *poffset, the alignment requirements for the current field in
29 * *palign, and returns a field desriptor for this field.
32 * bitfields extension:
33 * bitsize != 0: this is a bit field.
34 * pbitofs points to the current bit offset, this will be updated.
35 * prev_desc points to the type of the previous bitfield, if any.
37 PyObject *
38 CField_FromDesc(PyObject *desc, int index,
39 int *pfield_size, int bitsize, int *pbitofs,
40 int *psize, int *poffset, int *palign,
41 int pack, int big_endian)
43 CFieldObject *self;
44 PyObject *proto;
45 int size, align, length;
46 SETFUNC setfunc = NULL;
47 GETFUNC getfunc = NULL;
48 StgDictObject *dict;
49 int fieldtype;
50 #define NO_BITFIELD 0
51 #define NEW_BITFIELD 1
52 #define CONT_BITFIELD 2
53 #define EXPAND_BITFIELD 3
55 self = (CFieldObject *)PyObject_CallObject((PyObject *)&CField_Type,
56 NULL);
57 if (self == NULL)
58 return NULL;
59 dict = PyType_stgdict(desc);
60 if (!dict) {
61 PyErr_SetString(PyExc_TypeError,
62 "has no _stginfo_");
63 Py_DECREF(self);
64 return NULL;
66 if (bitsize /* this is a bitfield request */
67 && *pfield_size /* we have a bitfield open */
68 #if defined(MS_WIN32) && !defined(__MINGW32__)
69 && dict->size * 8 == *pfield_size /* MSVC */
70 #else
71 && dict->size * 8 <= *pfield_size /* GCC, MINGW */
72 #endif
73 && (*pbitofs + bitsize) <= *pfield_size) {
74 /* continue bit field */
75 fieldtype = CONT_BITFIELD;
76 #ifndef MS_WIN32
77 } else if (bitsize /* this is a bitfield request */
78 && *pfield_size /* we have a bitfield open */
79 && dict->size * 8 >= *pfield_size
80 && (*pbitofs + bitsize) <= dict->size * 8) {
81 /* expand bit field */
82 fieldtype = EXPAND_BITFIELD;
83 #endif
84 } else if (bitsize) {
85 /* start new bitfield */
86 fieldtype = NEW_BITFIELD;
87 *pbitofs = 0;
88 *pfield_size = dict->size * 8;
89 } else {
90 /* not a bit field */
91 fieldtype = NO_BITFIELD;
92 *pbitofs = 0;
93 *pfield_size = 0;
96 size = dict->size;
97 length = dict->length;
98 proto = desc;
100 /* Field descriptors for 'c_char * n' are be scpecial cased to
101 return a Python string instead of an Array object instance...
103 if (ArrayTypeObject_Check(proto)) {
104 StgDictObject *adict = PyType_stgdict(proto);
105 StgDictObject *idict;
106 if (adict && adict->proto) {
107 idict = PyType_stgdict(adict->proto);
108 if (!idict) {
109 PyErr_SetString(PyExc_TypeError,
110 "has no _stginfo_");
111 Py_DECREF(self);
112 return NULL;
114 if (idict->getfunc == getentry("c")->getfunc) {
115 struct fielddesc *fd = getentry("s");
116 getfunc = fd->getfunc;
117 setfunc = fd->setfunc;
119 #ifdef CTYPES_UNICODE
120 if (idict->getfunc == getentry("u")->getfunc) {
121 struct fielddesc *fd = getentry("U");
122 getfunc = fd->getfunc;
123 setfunc = fd->setfunc;
125 #endif
129 self->setfunc = setfunc;
130 self->getfunc = getfunc;
131 self->index = index;
133 Py_INCREF(proto);
134 self->proto = proto;
136 switch (fieldtype) {
137 case NEW_BITFIELD:
138 if (big_endian)
139 self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
140 else
141 self->size = (bitsize << 16) + *pbitofs;
142 *pbitofs = bitsize;
143 /* fall through */
144 case NO_BITFIELD:
145 if (pack)
146 align = min(pack, dict->align);
147 else
148 align = dict->align;
149 if (align && *poffset % align) {
150 int delta = align - (*poffset % align);
151 *psize += delta;
152 *poffset += delta;
155 if (bitsize == 0)
156 self->size = size;
157 *psize += size;
159 self->offset = *poffset;
160 *poffset += size;
162 *palign = align;
163 break;
165 case EXPAND_BITFIELD:
166 /* XXX needs more */
167 *psize += dict->size - *pfield_size/8;
169 *pfield_size = dict->size * 8;
171 if (big_endian)
172 self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
173 else
174 self->size = (bitsize << 16) + *pbitofs;
176 self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
177 *pbitofs += bitsize;
178 break;
180 case CONT_BITFIELD:
181 if (big_endian)
182 self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
183 else
184 self->size = (bitsize << 16) + *pbitofs;
186 self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
187 *pbitofs += bitsize;
188 break;
191 return (PyObject *)self;
194 static int
195 CField_set(CFieldObject *self, PyObject *inst, PyObject *value)
197 CDataObject *dst;
198 char *ptr;
199 assert(CDataObject_Check(inst));
200 dst = (CDataObject *)inst;
201 ptr = dst->b_ptr + self->offset;
202 return CData_set(inst, self->proto, self->setfunc, value,
203 self->index, self->size, ptr);
206 static PyObject *
207 CField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type)
209 CDataObject *src;
210 if (inst == NULL) {
211 Py_INCREF(self);
212 return (PyObject *)self;
214 assert(CDataObject_Check(inst));
215 src = (CDataObject *)inst;
216 return CData_get(self->proto, self->getfunc, inst,
217 self->index, self->size, src->b_ptr + self->offset);
220 static PyObject *
221 CField_get_offset(PyObject *self, void *data)
223 #if (PY_VERSION_HEX < 0x02050000)
224 return PyInt_FromLong(((CFieldObject *)self)->offset);
225 #else
226 return PyInt_FromSsize_t(((CFieldObject *)self)->offset);
227 #endif
230 static PyObject *
231 CField_get_size(PyObject *self, void *data)
233 #if (PY_VERSION_HEX < 0x02050000)
234 return PyInt_FromLong(((CFieldObject *)self)->size);
235 #else
236 return PyInt_FromSsize_t(((CFieldObject *)self)->size);
237 #endif
240 static PyGetSetDef CField_getset[] = {
241 { "offset", CField_get_offset, NULL, "offset in bytes of this field" },
242 { "size", CField_get_size, NULL, "size in bytes of this field" },
243 { NULL, NULL, NULL, NULL },
246 static int
247 CField_traverse(CFieldObject *self, visitproc visit, void *arg)
249 Py_VISIT(self->proto);
250 return 0;
253 static int
254 CField_clear(CFieldObject *self)
256 Py_CLEAR(self->proto);
257 return 0;
260 static void
261 CField_dealloc(PyObject *self)
263 CField_clear((CFieldObject *)self);
264 self->ob_type->tp_free((PyObject *)self);
267 static PyObject *
268 CField_repr(CFieldObject *self)
270 PyObject *result;
271 int bits = self->size >> 16;
272 int size = self->size & 0xFFFF;
273 const char *name;
275 name = ((PyTypeObject *)self->proto)->tp_name;
277 if (bits)
278 result = PyString_FromFormat(
279 #if (PY_VERSION_HEX < 0x02050000)
280 "<Field type=%s, ofs=%d:%d, bits=%d>",
281 #else
282 "<Field type=%s, ofs=%zd:%d, bits=%d>",
283 #endif
284 name, self->offset, size, bits);
285 else
286 result = PyString_FromFormat(
287 #if (PY_VERSION_HEX < 0x02050000)
288 "<Field type=%s, ofs=%d, size=%d>",
289 #else
290 "<Field type=%s, ofs=%zd, size=%d>",
291 #endif
292 name, self->offset, size);
293 return result;
296 PyTypeObject CField_Type = {
297 PyObject_HEAD_INIT(NULL)
298 0, /* ob_size */
299 "_ctypes.CField", /* tp_name */
300 sizeof(CFieldObject), /* tp_basicsize */
301 0, /* tp_itemsize */
302 CField_dealloc, /* tp_dealloc */
303 0, /* tp_print */
304 0, /* tp_getattr */
305 0, /* tp_setattr */
306 0, /* tp_compare */
307 (reprfunc)CField_repr, /* tp_repr */
308 0, /* tp_as_number */
309 0, /* tp_as_sequence */
310 0, /* tp_as_mapping */
311 0, /* tp_hash */
312 0, /* tp_call */
313 0, /* tp_str */
314 0, /* tp_getattro */
315 0, /* tp_setattro */
316 0, /* tp_as_buffer */
317 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
318 "Structure/Union member", /* tp_doc */
319 (traverseproc)CField_traverse, /* tp_traverse */
320 (inquiry)CField_clear, /* tp_clear */
321 0, /* tp_richcompare */
322 0, /* tp_weaklistoffset */
323 0, /* tp_iter */
324 0, /* tp_iternext */
325 0, /* tp_methods */
326 0, /* tp_members */
327 CField_getset, /* tp_getset */
328 0, /* tp_base */
329 0, /* tp_dict */
330 (descrgetfunc)CField_get, /* tp_descr_get */
331 (descrsetfunc)CField_set, /* tp_descr_set */
332 0, /* tp_dictoffset */
333 0, /* tp_init */
334 0, /* tp_alloc */
335 CField_new, /* tp_new */
336 0, /* tp_free */
340 /******************************************************************/
342 Accessor functions
345 /* Derived from Modules/structmodule.c:
346 Helper routine to get a Python integer and raise the appropriate error
347 if it isn't one */
349 static int
350 get_long(PyObject *v, long *p)
352 long x;
353 if (!PyInt_Check(v) && !PyLong_Check(v)) {
354 PyErr_Format(PyExc_TypeError,
355 "int expected instead of %s instance",
356 v->ob_type->tp_name);
357 return -1;
359 x = PyInt_AsUnsignedLongMask(v);
360 if (x == -1 && PyErr_Occurred())
361 return -1;
362 *p = x;
363 return 0;
366 /* Same, but handling unsigned long */
368 static int
369 get_ulong(PyObject *v, unsigned long *p)
371 unsigned long x;
372 if (!PyInt_Check(v) && !PyLong_Check(v)) {
373 PyErr_Format(PyExc_TypeError,
374 "int expected instead of %s instance",
375 v->ob_type->tp_name);
376 return -1;
378 x = PyInt_AsUnsignedLongMask(v);
379 if (x == -1 && PyErr_Occurred())
380 return -1;
381 *p = x;
382 return 0;
385 #ifdef HAVE_LONG_LONG
387 /* Same, but handling native long long. */
389 static int
390 get_longlong(PyObject *v, PY_LONG_LONG *p)
392 PY_LONG_LONG x;
393 if (!PyInt_Check(v) && !PyLong_Check(v)) {
394 PyErr_Format(PyExc_TypeError,
395 "int expected instead of %s instance",
396 v->ob_type->tp_name);
397 return -1;
399 x = PyInt_AsUnsignedLongLongMask(v);
400 if (x == -1 && PyErr_Occurred())
401 return -1;
402 *p = x;
403 return 0;
406 /* Same, but handling native unsigned long long. */
408 static int
409 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
411 unsigned PY_LONG_LONG x;
412 if (!PyInt_Check(v) && !PyLong_Check(v)) {
413 PyErr_Format(PyExc_TypeError,
414 "int expected instead of %s instance",
415 v->ob_type->tp_name);
416 return -1;
418 x = PyInt_AsUnsignedLongLongMask(v);
419 if (x == -1 && PyErr_Occurred())
420 return -1;
421 *p = x;
422 return 0;
425 #endif
427 /*****************************************************************
428 * Integer fields, with bitfield support
431 /* how to decode the size field, for integer get/set functions */
432 #define LOW_BIT(x) ((x) & 0xFFFF)
433 #define NUM_BITS(x) ((x) >> 16)
435 /* This seems nore a compiler issue than a Windows/non-Windows one */
436 #ifdef MS_WIN32
437 # define BIT_MASK(size) ((1 << NUM_BITS(size))-1)
438 #else
439 # define BIT_MASK(size) ((1LL << NUM_BITS(size))-1)
440 #endif
442 /* This macro CHANGES the first parameter IN PLACE. For proper sign handling,
443 we must first shift left, then right.
445 #define GET_BITFIELD(v, size) \
446 if (NUM_BITS(size)) { \
447 v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size)); \
448 v >>= (sizeof(v)*8 - NUM_BITS(size)); \
451 /* This macro RETURNS the first parameter with the bit field CHANGED. */
452 #define SET(x, v, size) \
453 (NUM_BITS(size) ? \
454 ( ( x & ~(BIT_MASK(size) << LOW_BIT(size)) ) | ( (v & BIT_MASK(size)) << LOW_BIT(size) ) ) \
455 : v)
457 /* byte swapping macros */
458 #define SWAP_2(v) \
459 ( ( (v >> 8) & 0x00FF) | \
460 ( (v << 8) & 0xFF00) )
462 #define SWAP_4(v) \
463 ( ( (v & 0x000000FF) << 24 ) | \
464 ( (v & 0x0000FF00) << 8 ) | \
465 ( (v & 0x00FF0000) >> 8 ) | \
466 ( ((v >> 24) & 0xFF)) )
468 #ifdef _MSC_VER
469 #define SWAP_8(v) \
470 ( ( (v & 0x00000000000000FFL) << 56 ) | \
471 ( (v & 0x000000000000FF00L) << 40 ) | \
472 ( (v & 0x0000000000FF0000L) << 24 ) | \
473 ( (v & 0x00000000FF000000L) << 8 ) | \
474 ( (v & 0x000000FF00000000L) >> 8 ) | \
475 ( (v & 0x0000FF0000000000L) >> 24 ) | \
476 ( (v & 0x00FF000000000000L) >> 40 ) | \
477 ( ((v >> 56) & 0xFF)) )
478 #else
479 #define SWAP_8(v) \
480 ( ( (v & 0x00000000000000FFLL) << 56 ) | \
481 ( (v & 0x000000000000FF00LL) << 40 ) | \
482 ( (v & 0x0000000000FF0000LL) << 24 ) | \
483 ( (v & 0x00000000FF000000LL) << 8 ) | \
484 ( (v & 0x000000FF00000000LL) >> 8 ) | \
485 ( (v & 0x0000FF0000000000LL) >> 24 ) | \
486 ( (v & 0x00FF000000000000LL) >> 40 ) | \
487 ( ((v >> 56) & 0xFF)) )
488 #endif
490 #define SWAP_INT SWAP_4
492 #if SIZEOF_LONG == 4
493 # define SWAP_LONG SWAP_4
494 #elif SIZEOF_LONG == 8
495 # define SWAP_LONG SWAP_8
496 #endif
497 /*****************************************************************
498 * The setter methods return an object which must be kept alive, to keep the
499 * data valid which has been stored in the memory block. The ctypes object
500 * instance inserts this object into its 'b_objects' list.
502 * For simple Python types like integers or characters, there is nothing that
503 * has to been kept alive, so Py_None is returned in these cases. But this
504 * makes inspecting the 'b_objects' list, which is accessible from Python for
505 * debugging, less useful.
507 * So, defining the _CTYPES_DEBUG_KEEP symbol returns the original value
508 * instead of Py_None.
511 #ifdef _CTYPES_DEBUG_KEEP
512 #define _RET(x) Py_INCREF(x); return x
513 #else
514 #define _RET(X) Py_INCREF(Py_None); return Py_None
515 #endif
517 /*****************************************************************
518 * integer accessor methods, supporting bit fields
521 static PyObject *
522 b_set(void *ptr, PyObject *value, unsigned size)
524 long val;
525 if (get_long(value, &val) < 0)
526 return NULL;
527 *(signed char *)ptr = (signed char)SET(*(signed char *)ptr, (signed char)val, size);
528 _RET(value);
532 static PyObject *
533 b_get(void *ptr, unsigned size)
535 signed char val = *(signed char *)ptr;
536 GET_BITFIELD(val, size);
537 return PyInt_FromLong(val);
540 static PyObject *
541 B_set(void *ptr, PyObject *value, unsigned size)
543 unsigned long val;
544 if (get_ulong(value, &val) < 0)
545 return NULL;
546 *(unsigned char *)ptr = (unsigned char)SET(*(unsigned char*)ptr,
547 (unsigned short)val, size);
548 _RET(value);
552 static PyObject *
553 B_get(void *ptr, unsigned size)
555 unsigned char val = *(unsigned char *)ptr;
556 GET_BITFIELD(val, size);
557 return PyInt_FromLong(val);
560 static PyObject *
561 h_set(void *ptr, PyObject *value, unsigned size)
563 long val;
564 short x;
565 if (get_long(value, &val) < 0)
566 return NULL;
567 memcpy(&x, ptr, sizeof(x));
568 x = SET(x, (short)val, size);
569 memcpy(ptr, &x, sizeof(x));
570 _RET(value);
574 static PyObject *
575 h_set_sw(void *ptr, PyObject *value, unsigned size)
577 long val;
578 short field;
579 if (get_long(value, &val) < 0)
580 return NULL;
581 memcpy(&field, ptr, sizeof(field));
582 field = SWAP_2(field);
583 field = SET(field, (short)val, size);
584 field = SWAP_2(field);
585 memcpy(ptr, &field, sizeof(field));
586 _RET(value);
589 static PyObject *
590 h_get(void *ptr, unsigned size)
592 short val;
593 memcpy(&val, ptr, sizeof(val));
594 GET_BITFIELD(val, size);
595 return PyInt_FromLong((long)val);
598 static PyObject *
599 h_get_sw(void *ptr, unsigned size)
601 short val;
602 memcpy(&val, ptr, sizeof(val));
603 val = SWAP_2(val);
604 GET_BITFIELD(val, size);
605 return PyInt_FromLong(val);
608 static PyObject *
609 H_set(void *ptr, PyObject *value, unsigned size)
611 unsigned long val;
612 unsigned short x;
613 if (get_ulong(value, &val) < 0)
614 return NULL;
615 memcpy(&x, ptr, sizeof(x));
616 x = SET(x, (unsigned short)val, size);
617 memcpy(ptr, &x, sizeof(x));
618 _RET(value);
621 static PyObject *
622 H_set_sw(void *ptr, PyObject *value, unsigned size)
624 unsigned long val;
625 unsigned short field;
626 if (get_ulong(value, &val) < 0)
627 return NULL;
628 memcpy(&field, ptr, sizeof(field));
629 field = SWAP_2(field);
630 field = SET(field, (unsigned short)val, size);
631 field = SWAP_2(field);
632 memcpy(ptr, &field, sizeof(field));
633 _RET(value);
637 static PyObject *
638 H_get(void *ptr, unsigned size)
640 unsigned short val;
641 memcpy(&val, ptr, sizeof(val));
642 GET_BITFIELD(val, size);
643 return PyInt_FromLong(val);
646 static PyObject *
647 H_get_sw(void *ptr, unsigned size)
649 unsigned short val;
650 memcpy(&val, ptr, sizeof(val));
651 val = SWAP_2(val);
652 GET_BITFIELD(val, size);
653 return PyInt_FromLong(val);
656 static PyObject *
657 i_set(void *ptr, PyObject *value, unsigned size)
659 long val;
660 int x;
661 if (get_long(value, &val) < 0)
662 return NULL;
663 memcpy(&x, ptr, sizeof(x));
664 x = SET(x, (int)val, size);
665 memcpy(ptr, &x, sizeof(x));
666 _RET(value);
669 static PyObject *
670 i_set_sw(void *ptr, PyObject *value, unsigned size)
672 long val;
673 int field;
674 if (get_long(value, &val) < 0)
675 return NULL;
676 memcpy(&field, ptr, sizeof(field));
677 field = SWAP_INT(field);
678 field = SET(field, (int)val, size);
679 field = SWAP_INT(field);
680 memcpy(ptr, &field, sizeof(field));
681 _RET(value);
685 static PyObject *
686 i_get(void *ptr, unsigned size)
688 int val;
689 memcpy(&val, ptr, sizeof(val));
690 GET_BITFIELD(val, size);
691 return PyInt_FromLong(val);
694 static PyObject *
695 i_get_sw(void *ptr, unsigned size)
697 int val;
698 memcpy(&val, ptr, sizeof(val));
699 val = SWAP_INT(val);
700 GET_BITFIELD(val, size);
701 return PyInt_FromLong(val);
704 #ifdef MS_WIN32
705 /* short BOOL - VARIANT_BOOL */
706 static PyObject *
707 vBOOL_set(void *ptr, PyObject *value, unsigned size)
709 switch (PyObject_IsTrue(value)) {
710 case -1:
711 return NULL;
712 case 0:
713 *(short int *)ptr = VARIANT_FALSE;
714 _RET(value);
715 default:
716 *(short int *)ptr = VARIANT_TRUE;
717 _RET(value);
721 static PyObject *
722 vBOOL_get(void *ptr, unsigned size)
724 return PyBool_FromLong((long)*(short int *)ptr);
726 #endif
728 static PyObject *
729 I_set(void *ptr, PyObject *value, unsigned size)
731 unsigned long val;
732 unsigned int x;
733 if (get_ulong(value, &val) < 0)
734 return NULL;
735 memcpy(&x, ptr, sizeof(x));
736 x = SET(x, (unsigned int)val, size);
737 memcpy(ptr, &x, sizeof(x));
738 _RET(value);
741 static PyObject *
742 I_set_sw(void *ptr, PyObject *value, unsigned size)
744 unsigned long val;
745 unsigned int field;
746 if (get_ulong(value, &val) < 0)
747 return NULL;
748 memcpy(&field, ptr, sizeof(field));
749 field = (unsigned int)SET(field, (unsigned int)val, size);
750 field = SWAP_INT(field);
751 memcpy(ptr, &field, sizeof(field));
752 _RET(value);
756 static PyObject *
757 I_get(void *ptr, unsigned size)
759 unsigned int val;
760 memcpy(&val, ptr, sizeof(val));
761 GET_BITFIELD(val, size);
762 return PyLong_FromUnsignedLong(val);
765 static PyObject *
766 I_get_sw(void *ptr, unsigned size)
768 unsigned int val;
769 memcpy(&val, ptr, sizeof(val));
770 val = SWAP_INT(val);
771 GET_BITFIELD(val, size);
772 return PyLong_FromUnsignedLong(val);
775 static PyObject *
776 l_set(void *ptr, PyObject *value, unsigned size)
778 long val;
779 long x;
780 if (get_long(value, &val) < 0)
781 return NULL;
782 memcpy(&x, ptr, sizeof(x));
783 x = SET(x, val, size);
784 memcpy(ptr, &x, sizeof(x));
785 _RET(value);
788 static PyObject *
789 l_set_sw(void *ptr, PyObject *value, unsigned size)
791 long val;
792 long field;
793 if (get_long(value, &val) < 0)
794 return NULL;
795 memcpy(&field, ptr, sizeof(field));
796 field = SWAP_LONG(field);
797 field = (long)SET(field, val, size);
798 field = SWAP_LONG(field);
799 memcpy(ptr, &field, sizeof(field));
800 _RET(value);
804 static PyObject *
805 l_get(void *ptr, unsigned size)
807 long val;
808 memcpy(&val, ptr, sizeof(val));
809 GET_BITFIELD(val, size);
810 return PyInt_FromLong(val);
813 static PyObject *
814 l_get_sw(void *ptr, unsigned size)
816 long val;
817 memcpy(&val, ptr, sizeof(val));
818 val = SWAP_LONG(val);
819 GET_BITFIELD(val, size);
820 return PyInt_FromLong(val);
823 static PyObject *
824 L_set(void *ptr, PyObject *value, unsigned size)
826 unsigned long val;
827 unsigned long x;
828 if (get_ulong(value, &val) < 0)
829 return NULL;
830 memcpy(&x, ptr, sizeof(x));
831 x = SET(x, val, size);
832 memcpy(ptr, &x, sizeof(x));
833 _RET(value);
836 static PyObject *
837 L_set_sw(void *ptr, PyObject *value, unsigned size)
839 unsigned long val;
840 unsigned long field;
841 if (get_ulong(value, &val) < 0)
842 return NULL;
843 memcpy(&field, ptr, sizeof(field));
844 field = SWAP_LONG(field);
845 field = (unsigned long)SET(field, val, size);
846 field = SWAP_LONG(field);
847 memcpy(ptr, &field, sizeof(field));
848 _RET(value);
852 static PyObject *
853 L_get(void *ptr, unsigned size)
855 unsigned long val;
856 memcpy(&val, ptr, sizeof(val));
857 GET_BITFIELD(val, size);
858 return PyLong_FromUnsignedLong(val);
861 static PyObject *
862 L_get_sw(void *ptr, unsigned size)
864 unsigned long val;
865 memcpy(&val, ptr, sizeof(val));
866 val = SWAP_LONG(val);
867 GET_BITFIELD(val, size);
868 return PyLong_FromUnsignedLong(val);
871 #ifdef HAVE_LONG_LONG
872 static PyObject *
873 q_set(void *ptr, PyObject *value, unsigned size)
875 PY_LONG_LONG val;
876 PY_LONG_LONG x;
877 if (get_longlong(value, &val) < 0)
878 return NULL;
879 memcpy(&x, ptr, sizeof(x));
880 x = SET(x, val, size);
881 memcpy(ptr, &x, sizeof(x));
882 _RET(value);
885 static PyObject *
886 q_set_sw(void *ptr, PyObject *value, unsigned size)
888 PY_LONG_LONG val;
889 PY_LONG_LONG field;
890 if (get_longlong(value, &val) < 0)
891 return NULL;
892 memcpy(&field, ptr, sizeof(field));
893 field = SWAP_8(field);
894 field = (PY_LONG_LONG)SET(field, val, size);
895 field = SWAP_8(field);
896 memcpy(ptr, &field, sizeof(field));
897 _RET(value);
900 static PyObject *
901 q_get(void *ptr, unsigned size)
903 PY_LONG_LONG val;
904 memcpy(&val, ptr, sizeof(val));
905 GET_BITFIELD(val, size);
906 return PyLong_FromLongLong(val);
909 static PyObject *
910 q_get_sw(void *ptr, unsigned size)
912 PY_LONG_LONG val;
913 memcpy(&val, ptr, sizeof(val));
914 val = SWAP_8(val);
915 GET_BITFIELD(val, size);
916 return PyLong_FromLongLong(val);
919 static PyObject *
920 Q_set(void *ptr, PyObject *value, unsigned size)
922 unsigned PY_LONG_LONG val;
923 unsigned PY_LONG_LONG x;
924 if (get_ulonglong(value, &val) < 0)
925 return NULL;
926 memcpy(&x, ptr, sizeof(x));
927 x = SET(x, val, size);
928 memcpy(ptr, &x, sizeof(x));
929 _RET(value);
932 static PyObject *
933 Q_set_sw(void *ptr, PyObject *value, unsigned size)
935 unsigned PY_LONG_LONG val;
936 unsigned PY_LONG_LONG field;
937 if (get_ulonglong(value, &val) < 0)
938 return NULL;
939 memcpy(&field, ptr, sizeof(field));
940 field = SWAP_8(field);
941 field = (unsigned PY_LONG_LONG)SET(field, val, size);
942 field = SWAP_8(field);
943 memcpy(ptr, &field, sizeof(field));
944 _RET(value);
947 static PyObject *
948 Q_get(void *ptr, unsigned size)
950 unsigned PY_LONG_LONG val;
951 memcpy(&val, ptr, sizeof(val));
952 GET_BITFIELD(val, size);
953 return PyLong_FromUnsignedLongLong(val);
956 static PyObject *
957 Q_get_sw(void *ptr, unsigned size)
959 unsigned PY_LONG_LONG val;
960 memcpy(&val, ptr, sizeof(val));
961 val = SWAP_8(val);
962 GET_BITFIELD(val, size);
963 return PyLong_FromUnsignedLongLong(val);
965 #endif
967 /*****************************************************************
968 * non-integer accessor methods, not supporting bit fields
973 static PyObject *
974 d_set(void *ptr, PyObject *value, unsigned size)
976 double x;
978 x = PyFloat_AsDouble(value);
979 if (x == -1 && PyErr_Occurred()) {
980 PyErr_Format(PyExc_TypeError,
981 " float expected instead of %s instance",
982 value->ob_type->tp_name);
983 return NULL;
985 memcpy(ptr, &x, sizeof(double));
986 _RET(value);
989 static PyObject *
990 d_get(void *ptr, unsigned size)
992 double val;
993 memcpy(&val, ptr, sizeof(val));
994 return PyFloat_FromDouble(val);
997 static PyObject *
998 d_set_sw(void *ptr, PyObject *value, unsigned size)
1000 double x;
1002 x = PyFloat_AsDouble(value);
1003 if (x == -1 && PyErr_Occurred()) {
1004 PyErr_Format(PyExc_TypeError,
1005 " float expected instead of %s instance",
1006 value->ob_type->tp_name);
1007 return NULL;
1009 #ifdef WORDS_BIGENDIAN
1010 if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
1011 return NULL;
1012 #else
1013 if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0))
1014 return NULL;
1015 #endif
1016 _RET(value);
1019 static PyObject *
1020 d_get_sw(void *ptr, unsigned size)
1022 #ifdef WORDS_BIGENDIAN
1023 return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1));
1024 #else
1025 return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0));
1026 #endif
1029 static PyObject *
1030 f_set(void *ptr, PyObject *value, unsigned size)
1032 float x;
1034 x = (float)PyFloat_AsDouble(value);
1035 if (x == -1 && PyErr_Occurred()) {
1036 PyErr_Format(PyExc_TypeError,
1037 " float expected instead of %s instance",
1038 value->ob_type->tp_name);
1039 return NULL;
1041 memcpy(ptr, &x, sizeof(x));
1042 _RET(value);
1045 static PyObject *
1046 f_get(void *ptr, unsigned size)
1048 float val;
1049 memcpy(&val, ptr, sizeof(val));
1050 return PyFloat_FromDouble(val);
1053 static PyObject *
1054 f_set_sw(void *ptr, PyObject *value, unsigned size)
1056 float x;
1058 x = (float)PyFloat_AsDouble(value);
1059 if (x == -1 && PyErr_Occurred()) {
1060 PyErr_Format(PyExc_TypeError,
1061 " float expected instead of %s instance",
1062 value->ob_type->tp_name);
1063 return NULL;
1065 #ifdef WORDS_BIGENDIAN
1066 if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
1067 return NULL;
1068 #else
1069 if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0))
1070 return NULL;
1071 #endif
1072 _RET(value);
1075 static PyObject *
1076 f_get_sw(void *ptr, unsigned size)
1078 #ifdef WORDS_BIGENDIAN
1079 return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1));
1080 #else
1081 return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0));
1082 #endif
1086 py_object refcounts:
1088 1. If we have a py_object instance, O_get must Py_INCREF the returned
1089 object, of course. If O_get is called from a function result, no py_object
1090 instance is created - so callproc.c::GetResult has to call Py_DECREF.
1092 2. The memory block in py_object owns a refcount. So, py_object must call
1093 Py_DECREF on destruction. Maybe only when b_needsfree is non-zero.
1095 static PyObject *
1096 O_get(void *ptr, unsigned size)
1098 PyObject *ob = *(PyObject **)ptr;
1099 if (ob == NULL) {
1100 if (!PyErr_Occurred())
1101 /* Set an error if not yet set */
1102 PyErr_SetString(PyExc_ValueError,
1103 "PyObject is NULL");
1104 return NULL;
1106 Py_INCREF(ob);
1107 return ob;
1110 static PyObject *
1111 O_set(void *ptr, PyObject *value, unsigned size)
1113 /* Hm, does the memory block need it's own refcount or not? */
1114 *(PyObject **)ptr = value;
1115 Py_INCREF(value);
1116 return value;
1120 static PyObject *
1121 c_set(void *ptr, PyObject *value, unsigned size)
1123 if (!PyString_Check(value) || (1 != PyString_Size(value))) {
1124 PyErr_Format(PyExc_TypeError,
1125 "one character string expected");
1126 return NULL;
1128 *(char *)ptr = PyString_AS_STRING(value)[0];
1129 _RET(value);
1133 static PyObject *
1134 c_get(void *ptr, unsigned size)
1136 return PyString_FromStringAndSize((char *)ptr, 1);
1139 #ifdef CTYPES_UNICODE
1140 /* u - a single wchar_t character */
1141 static PyObject *
1142 u_set(void *ptr, PyObject *value, unsigned size)
1144 int len;
1146 if (PyString_Check(value)) {
1147 value = PyUnicode_FromEncodedObject(value,
1148 conversion_mode_encoding,
1149 conversion_mode_errors);
1150 if (!value)
1151 return NULL;
1152 } else if (!PyUnicode_Check(value)) {
1153 PyErr_Format(PyExc_TypeError,
1154 "unicode string expected instead of %s instance",
1155 value->ob_type->tp_name);
1156 return NULL;
1157 } else
1158 Py_INCREF(value);
1160 len = PyUnicode_GET_SIZE(value);
1161 if (len != 1) {
1162 Py_DECREF(value);
1163 PyErr_SetString(PyExc_TypeError,
1164 "one character unicode string expected");
1165 return NULL;
1168 *(wchar_t *)ptr = PyUnicode_AS_UNICODE(value)[0];
1169 Py_DECREF(value);
1171 _RET(value);
1175 static PyObject *
1176 u_get(void *ptr, unsigned size)
1178 return PyUnicode_FromWideChar((wchar_t *)ptr, 1);
1181 /* U - a unicode string */
1182 static PyObject *
1183 U_get(void *ptr, unsigned size)
1185 PyObject *result;
1186 unsigned int len;
1187 Py_UNICODE *p;
1189 size /= sizeof(wchar_t); /* we count character units here, not bytes */
1191 result = PyUnicode_FromWideChar((wchar_t *)ptr, size);
1192 if (!result)
1193 return NULL;
1194 /* We need 'result' to be able to count the characters with wcslen,
1195 since ptr may not be NUL terminated. If the length is smaller (if
1196 it was actually NUL terminated, we construct a new one and throw
1197 away the result.
1199 /* chop off at the first NUL character, if any. */
1200 p = PyUnicode_AS_UNICODE(result);
1201 for (len = 0; len < size; ++len)
1202 if (!p[len])
1203 break;
1205 if (len < size) {
1206 PyObject *ob = PyUnicode_FromWideChar((wchar_t *)ptr, len);
1207 Py_DECREF(result);
1208 return ob;
1210 return result;
1213 static PyObject *
1214 U_set(void *ptr, PyObject *value, unsigned length)
1216 unsigned int size;
1218 /* It's easier to calculate in characters than in bytes */
1219 length /= sizeof(wchar_t);
1221 if (PyString_Check(value)) {
1222 value = PyUnicode_FromEncodedObject(value,
1223 conversion_mode_encoding,
1224 conversion_mode_errors);
1225 if (!value)
1226 return NULL;
1227 } else if (!PyUnicode_Check(value)) {
1228 PyErr_Format(PyExc_TypeError,
1229 "unicode string expected instead of %s instance",
1230 value->ob_type->tp_name);
1231 return NULL;
1232 } else
1233 Py_INCREF(value);
1234 size = PyUnicode_GET_SIZE(value);
1235 if (size > length) {
1236 PyErr_Format(PyExc_ValueError,
1237 "string too long (%d, maximum length %d)",
1238 size, length);
1239 Py_DECREF(value);
1240 return NULL;
1241 } else if (size < length-1)
1242 /* copy terminating NUL character if there is space */
1243 size += 1;
1244 PyUnicode_AsWideChar((PyUnicodeObject *)value, (wchar_t *)ptr, size);
1245 return value;
1248 #endif
1250 static PyObject *
1251 s_get(void *ptr, unsigned size)
1253 PyObject *result;
1255 result = PyString_FromString((char *)ptr);
1256 if (!result)
1257 return NULL;
1258 /* chop off at the first NUL character, if any.
1259 * On error, result will be deallocated and set to NULL.
1261 size = min(size, strlen(PyString_AS_STRING(result)));
1262 if (result->ob_refcnt == 1) {
1263 /* shorten the result */
1264 _PyString_Resize(&result, size);
1265 return result;
1266 } else
1267 /* cannot shorten the result */
1268 return PyString_FromStringAndSize(ptr, size);
1271 static PyObject *
1272 s_set(void *ptr, PyObject *value, unsigned length)
1274 char *data;
1275 unsigned size;
1277 data = PyString_AsString(value);
1278 if (!data)
1279 return NULL;
1280 size = strlen(data);
1281 if (size < length) {
1282 /* This will copy the leading NUL character
1283 * if there is space for it.
1285 ++size;
1286 } else if (size > length) {
1287 PyErr_Format(PyExc_ValueError,
1288 "string too long (%d, maximum length %d)",
1289 size, length);
1290 return NULL;
1292 /* Also copy the terminating NUL character if there is space */
1293 memcpy((char *)ptr, data, size);
1294 _RET(value);
1297 static PyObject *
1298 z_set(void *ptr, PyObject *value, unsigned size)
1300 if (value == Py_None) {
1301 *(char **)ptr = NULL;
1302 Py_INCREF(value);
1303 return value;
1305 if (PyString_Check(value)) {
1306 *(char **)ptr = PyString_AS_STRING(value);
1307 Py_INCREF(value);
1308 return value;
1309 } else if (PyUnicode_Check(value)) {
1310 PyObject *str = PyUnicode_AsEncodedString(value,
1311 conversion_mode_encoding,
1312 conversion_mode_errors);
1313 if (str == NULL)
1314 return NULL;
1315 *(char **)ptr = PyString_AS_STRING(str);
1316 return str;
1317 } else if (PyInt_Check(value) || PyLong_Check(value)) {
1318 #if SIZEOF_VOID_P == SIZEOF_LONG_LONG
1319 *(char **)ptr = (char *)PyInt_AsUnsignedLongLongMask(value);
1320 #else
1321 *(char **)ptr = (char *)PyInt_AsUnsignedLongMask(value);
1322 #endif
1323 _RET(value);
1325 PyErr_Format(PyExc_TypeError,
1326 "string or integer address expected instead of %s instance",
1327 value->ob_type->tp_name);
1328 return NULL;
1331 static PyObject *
1332 z_get(void *ptr, unsigned size)
1334 /* XXX What about invalid pointers ??? */
1335 if (*(void **)ptr) {
1336 #if defined(MS_WIN32) && !defined(_WIN32_WCE)
1337 if (IsBadStringPtrA(*(char **)ptr, -1)) {
1338 PyErr_Format(PyExc_ValueError,
1339 "invalid string pointer %p",
1340 ptr);
1341 return NULL;
1343 #endif
1344 return PyString_FromString(*(char **)ptr);
1345 } else {
1346 Py_INCREF(Py_None);
1347 return Py_None;
1351 #ifdef CTYPES_UNICODE
1352 static PyObject *
1353 Z_set(void *ptr, PyObject *value, unsigned size)
1355 if (value == Py_None) {
1356 *(wchar_t **)ptr = NULL;
1357 Py_INCREF(value);
1358 return value;
1360 if (PyString_Check(value)) {
1361 value = PyUnicode_FromEncodedObject(value,
1362 conversion_mode_encoding,
1363 conversion_mode_errors);
1364 if (!value)
1365 return NULL;
1366 } else if (PyInt_Check(value) || PyLong_Check(value)) {
1367 #if SIZEOF_VOID_P == SIZEOF_LONG_LONG
1368 *(wchar_t **)ptr = (wchar_t *)PyInt_AsUnsignedLongLongMask(value);
1369 #else
1370 *(wchar_t **)ptr = (wchar_t *)PyInt_AsUnsignedLongMask(value);
1371 #endif
1372 Py_INCREF(Py_None);
1373 return Py_None;
1374 } else if (!PyUnicode_Check(value)) {
1375 PyErr_Format(PyExc_TypeError,
1376 "unicode string or integer address expected instead of %s instance",
1377 value->ob_type->tp_name);
1378 return NULL;
1379 } else
1380 Py_INCREF(value);
1381 #ifdef HAVE_USABLE_WCHAR_T
1382 /* HAVE_USABLE_WCHAR_T means that Py_UNICODE and wchar_t is the same
1383 type. So we can copy directly. Hm, are unicode objects always NUL
1384 terminated in Python, internally?
1386 *(wchar_t **)ptr = PyUnicode_AS_UNICODE(value);
1387 return value;
1388 #else
1390 /* We must create a wchar_t* buffer from the unicode object,
1391 and keep it alive */
1392 PyObject *keep;
1393 wchar_t *buffer;
1395 int size = PyUnicode_GET_SIZE(value);
1396 size += 1; /* terminating NUL */
1397 size *= sizeof(wchar_t);
1398 buffer = (wchar_t *)PyMem_Malloc(size);
1399 if (!buffer)
1400 return NULL;
1401 memset(buffer, 0, size);
1402 keep = PyCObject_FromVoidPtr(buffer, PyMem_Free);
1403 if (!keep) {
1404 PyMem_Free(buffer);
1405 return NULL;
1407 *(wchar_t **)ptr = (wchar_t *)buffer;
1408 if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)value,
1409 buffer, PyUnicode_GET_SIZE(value))) {
1410 Py_DECREF(value);
1411 Py_DECREF(keep);
1412 return NULL;
1414 Py_DECREF(value);
1415 return keep;
1417 #endif
1420 static PyObject *
1421 Z_get(void *ptr, unsigned size)
1423 wchar_t *p;
1424 p = *(wchar_t **)ptr;
1425 if (p)
1426 return PyUnicode_FromWideChar(p, wcslen(p));
1427 else {
1428 Py_INCREF(Py_None);
1429 return Py_None;
1432 #endif
1434 #ifdef MS_WIN32
1435 static PyObject *
1436 BSTR_set(void *ptr, PyObject *value, unsigned size)
1438 BSTR bstr;
1440 /* convert value into a PyUnicodeObject or NULL */
1441 if (Py_None == value) {
1442 value = NULL;
1443 } else if (PyString_Check(value)) {
1444 value = PyUnicode_FromEncodedObject(value,
1445 conversion_mode_encoding,
1446 conversion_mode_errors);
1447 if (!value)
1448 return NULL;
1449 } else if (PyUnicode_Check(value)) {
1450 Py_INCREF(value); /* for the descref below */
1451 } else {
1452 PyErr_Format(PyExc_TypeError,
1453 "unicode string expected instead of %s instance",
1454 value->ob_type->tp_name);
1455 return NULL;
1458 /* create a BSTR from value */
1459 if (value) {
1460 bstr = SysAllocStringLen(PyUnicode_AS_UNICODE(value),
1461 PyUnicode_GET_SIZE(value));
1462 Py_DECREF(value);
1463 } else
1464 bstr = NULL;
1466 /* free the previous contents, if any */
1467 if (*(BSTR *)ptr)
1468 SysFreeString(*(BSTR *)ptr);
1470 /* and store it */
1471 *(BSTR *)ptr = bstr;
1473 /* We don't need to keep any other object */
1474 _RET(value);
1478 static PyObject *
1479 BSTR_get(void *ptr, unsigned size)
1481 BSTR p;
1482 p = *(BSTR *)ptr;
1483 if (p)
1484 return PyUnicode_FromWideChar(p, SysStringLen(p));
1485 else {
1486 /* Hm, it seems NULL pointer and zero length string are the
1487 same in BSTR, see Don Box, p 81
1489 Py_INCREF(Py_None);
1490 return Py_None;
1493 #endif
1495 static PyObject *
1496 P_set(void *ptr, PyObject *value, unsigned size)
1498 void *v;
1499 if (value == Py_None) {
1500 *(void **)ptr = NULL;
1501 _RET(value);
1504 if (!PyInt_Check(value) && !PyLong_Check(value)) {
1505 PyErr_SetString(PyExc_TypeError,
1506 "cannot be converted to pointer");
1507 return NULL;
1510 #if SIZEOF_VOID_P <= SIZEOF_LONG
1511 v = (void *)PyInt_AsUnsignedLongMask(value);
1512 #else
1513 #ifndef HAVE_LONG_LONG
1514 # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
1515 #elif SIZEOF_LONG_LONG < SIZEOF_VOID_P
1516 # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
1517 #endif
1518 v = (void *)PyInt_AsUnsignedLongLongMask(value);
1519 #endif
1521 if (PyErr_Occurred())
1522 return NULL;
1524 *(void **)ptr = v;
1525 _RET(value);
1528 static PyObject *
1529 P_get(void *ptr, unsigned size)
1531 if (*(void **)ptr == NULL) {
1532 Py_INCREF(Py_None);
1533 return Py_None;
1535 return PyLong_FromVoidPtr(*(void **)ptr);
1538 static struct fielddesc formattable[] = {
1539 { 's', s_set, s_get, &ffi_type_pointer},
1540 { 'b', b_set, b_get, &ffi_type_schar},
1541 { 'B', B_set, B_get, &ffi_type_uchar},
1542 { 'c', c_set, c_get, &ffi_type_schar},
1543 { 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw},
1544 { 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw},
1545 { 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw},
1546 { 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw},
1547 { 'i', i_set, i_get, &ffi_type_sint, i_set_sw, i_get_sw},
1548 { 'I', I_set, I_get, &ffi_type_uint, I_set_sw, I_get_sw},
1549 /* XXX Hm, sizeof(int) == sizeof(long) doesn't hold on every platform */
1550 /* As soon as we can get rid of the type codes, this is no longer a problem */
1551 #if SIZEOF_LONG == 4
1552 { 'l', l_set, l_get, &ffi_type_sint, l_set_sw, l_get_sw},
1553 { 'L', L_set, L_get, &ffi_type_uint, L_set_sw, L_get_sw},
1554 #elif SIZEOF_LONG == 8
1555 { 'l', l_set, l_get, &ffi_type_slong, l_set_sw, l_get_sw},
1556 { 'L', L_set, L_get, &ffi_type_ulong, L_set_sw, L_get_sw},
1557 #else
1558 # error
1559 #endif
1560 #ifdef HAVE_LONG_LONG
1561 { 'q', q_set, q_get, &ffi_type_slong, q_set_sw, q_get_sw},
1562 { 'Q', Q_set, Q_get, &ffi_type_ulong, Q_set_sw, Q_get_sw},
1563 #endif
1564 { 'P', P_set, P_get, &ffi_type_pointer},
1565 { 'z', z_set, z_get, &ffi_type_pointer},
1566 #ifdef CTYPES_UNICODE
1567 { 'u', u_set, u_get, NULL}, /* ffi_type set later */
1568 { 'U', U_set, U_get, &ffi_type_pointer},
1569 { 'Z', Z_set, Z_get, &ffi_type_pointer},
1570 #endif
1571 #ifdef MS_WIN32
1572 { 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
1573 { 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
1574 #endif
1575 { 'O', O_set, O_get, &ffi_type_pointer},
1576 { 0, NULL, NULL, NULL},
1580 Ideas: Implement VARIANT in this table, using 'V' code.
1581 Use '?' as code for BOOL.
1584 struct fielddesc *
1585 getentry(char *fmt)
1587 static int initialized = 0;
1588 struct fielddesc *table = formattable;
1590 if (!initialized) {
1591 initialized = 1;
1592 #ifdef CTYPES_UNICODE
1593 if (sizeof(wchar_t) == sizeof(short))
1594 getentry("u")->pffi_type = &ffi_type_sshort;
1595 else if (sizeof(wchar_t) == sizeof(int))
1596 getentry("u")->pffi_type = &ffi_type_sint;
1597 else if (sizeof(wchar_t) == sizeof(long))
1598 getentry("u")->pffi_type = &ffi_type_slong;
1599 #endif
1602 for (; table->code; ++table) {
1603 if (table->code == fmt[0])
1604 return table;
1606 return NULL;
1609 typedef struct { char c; char x; } s_char;
1610 typedef struct { char c; short x; } s_short;
1611 typedef struct { char c; int x; } s_int;
1612 typedef struct { char c; long x; } s_long;
1613 typedef struct { char c; float x; } s_float;
1614 typedef struct { char c; double x; } s_double;
1615 typedef struct { char c; char *x; } s_char_p;
1616 typedef struct { char c; void *x; } s_void_p;
1619 #define CHAR_ALIGN (sizeof(s_char) - sizeof(char))
1620 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
1621 #define INT_ALIGN (sizeof(s_int) - sizeof(int))
1622 #define LONG_ALIGN (sizeof(s_long) - sizeof(long))
1624 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
1625 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
1626 /* #define CHAR_P_ALIGN (sizeof(s_char_p) - sizeof(char*)) */
1627 #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void*))
1630 #ifdef HAVE_USABLE_WCHAR_T
1631 typedef struct { char c; wchar_t x; } s_wchar;
1632 typedef struct { char c; wchar_t *x; } s_wchar_p;
1634 #define WCHAR_ALIGN (sizeof(s_wchar) - sizeof(wchar_t))
1635 #define WCHAR_P_ALIGN (sizeof(s_wchar_p) - sizeof(wchar_t*))
1636 #endif
1639 #ifdef HAVE_LONG_LONG
1640 typedef struct { char c; PY_LONG_LONG x; } s_long_long;
1641 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
1642 #endif
1644 /* from ffi.h:
1645 typedef struct _ffi_type
1647 size_t size;
1648 unsigned short alignment;
1649 unsigned short type;
1650 struct _ffi_type **elements;
1651 } ffi_type;
1654 /* align and size are bogus for void, but they must not be zero */
1655 ffi_type ffi_type_void = { 1, 1, FFI_TYPE_VOID };
1657 ffi_type ffi_type_uint8 = { 1, 1, FFI_TYPE_UINT8 };
1658 ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 };
1660 ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 };
1661 ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 };
1663 ffi_type ffi_type_uint32 = { 4, 4, FFI_TYPE_UINT32 };
1664 ffi_type ffi_type_sint32 = { 4, 4, FFI_TYPE_SINT32 };
1666 ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 };
1667 ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 };
1669 ffi_type ffi_type_float = { sizeof(float), FLOAT_ALIGN, FFI_TYPE_FLOAT };
1670 ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE };
1672 /* ffi_type ffi_type_longdouble */
1674 ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER };
1676 /*---------------- EOF ----------------*/