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