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