ยปCore Development>Code coverage>Modules/_ctypes/cfield.c

Python code coverage for Modules/_ctypes/cfield.c

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