• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define PY_SSIZE_T_CLEAN
2 #include <Python.h>
3 #include "structmember.h"
4 
5 #define CFFI_VERSION  "1.12.2"
6 
7 #ifdef MS_WIN32
8 #include <windows.h>
9 #include "misc_win32.h"
10 #else
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <dlfcn.h>
14 #include <errno.h>
15 #include <ffi.h>
16 #include <sys/mman.h>
17 #endif
18 
19 /* this block of #ifs should be kept exactly identical between
20    c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py */
21 #if defined(_MSC_VER)
22 # include <malloc.h>   /* for alloca() */
23 # if _MSC_VER < 1600   /* MSVC < 2010 */
24    typedef __int8 int8_t;
25    typedef __int16 int16_t;
26    typedef __int32 int32_t;
27    typedef __int64 int64_t;
28    typedef unsigned __int8 uint8_t;
29    typedef unsigned __int16 uint16_t;
30    typedef unsigned __int32 uint32_t;
31    typedef unsigned __int64 uint64_t;
32    typedef __int8 int_least8_t;
33    typedef __int16 int_least16_t;
34    typedef __int32 int_least32_t;
35    typedef __int64 int_least64_t;
36    typedef unsigned __int8 uint_least8_t;
37    typedef unsigned __int16 uint_least16_t;
38    typedef unsigned __int32 uint_least32_t;
39    typedef unsigned __int64 uint_least64_t;
40    typedef __int8 int_fast8_t;
41    typedef __int16 int_fast16_t;
42    typedef __int32 int_fast32_t;
43    typedef __int64 int_fast64_t;
44    typedef unsigned __int8 uint_fast8_t;
45    typedef unsigned __int16 uint_fast16_t;
46    typedef unsigned __int32 uint_fast32_t;
47    typedef unsigned __int64 uint_fast64_t;
48    typedef __int64 intmax_t;
49    typedef unsigned __int64 uintmax_t;
50 # else
51 #  include <stdint.h>
52 # endif
53 # if _MSC_VER < 1800   /* MSVC < 2013 */
54    typedef unsigned char _Bool;
55 # endif
56 #else
57 # include <stdint.h>
58 # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
59 #  include <alloca.h>
60 # endif
61 #endif
62 
63 
64 /* Define the following macro ONLY if you trust libffi's version of
65  * ffi_closure_alloc() more than the code in malloc_closure.h.
66  * IMPORTANT: DO NOT ENABLE THIS ON LINUX, unless you understand exactly
67  * why I recommend against it and decide that you trust it more than my
68  * analysis below.
69  *
70  * There are two versions of this code: one inside libffi itself, and
71  * one inside malloc_closure.h here.  Both should be fine as long as the
72  * Linux distribution does _not_ enable extra security features.  If it
73  * does, then the code in malloc_closure.h will cleanly crash because
74  * there is no reasonable way to obtain a read-write-execute memory
75  * page.  On the other hand, the code in libffi will appear to
76  * work---but will actually randomly crash after a fork() if the child
77  * does not immediately call exec().  This second crash is of the kind
78  * that can be turned into an attack vector by a motivated attacker.
79  * So, _enabling_ extra security features _opens_ an attack vector.
80  * That sounds like a horribly bad idea to me, and is the reason for why
81  * I prefer CFFI crashing cleanly.
82  *
83  * Currently, we use libffi's ffi_closure_alloc() only on NetBSD.  It is
84  * known that on the NetBSD kernel, a different strategy is used which
85  * should not be open to the fork() bug.
86  */
87 #ifdef __NetBSD__
88 # define CFFI_TRUST_LIBFFI
89 #endif
90 
91 #ifndef CFFI_TRUST_LIBFFI
92 # include "malloc_closure.h"
93 #endif
94 
95 
96 #if PY_MAJOR_VERSION >= 3
97 # define STR_OR_BYTES "bytes"
98 # define PyText_Type PyUnicode_Type
99 # define PyText_Check PyUnicode_Check
100 # define PyTextAny_Check PyUnicode_Check
101 # define PyText_FromFormat PyUnicode_FromFormat
102 # define PyText_AsUTF8 _PyUnicode_AsString   /* PyUnicode_AsUTF8 in Py3.3 */
103 # define PyText_AS_UTF8 _PyUnicode_AsString
104 # if PY_VERSION_HEX >= 0x03030000
105 #  define PyText_GetSize PyUnicode_GetLength
106 # else
107 #  define PyText_GetSize PyUnicode_GetSize
108 # endif
109 # define PyText_FromString PyUnicode_FromString
110 # define PyText_FromStringAndSize PyUnicode_FromStringAndSize
111 # define PyText_InternInPlace PyUnicode_InternInPlace
112 # define PyText_InternFromString PyUnicode_InternFromString
113 # define PyIntOrLong_Check PyLong_Check
114 #else
115 # define STR_OR_BYTES "str"
116 # define PyText_Type PyString_Type
117 # define PyText_Check PyString_Check
118 # define PyTextAny_Check(op) (PyString_Check(op) || PyUnicode_Check(op))
119 # define PyText_FromFormat PyString_FromFormat
120 # define PyText_AsUTF8 PyString_AsString
121 # define PyText_AS_UTF8 PyString_AS_STRING
122 # define PyText_GetSize PyString_Size
123 # define PyText_FromString PyString_FromString
124 # define PyText_FromStringAndSize PyString_FromStringAndSize
125 # define PyText_InternInPlace PyString_InternInPlace
126 # define PyText_InternFromString PyString_InternFromString
127 # define PyIntOrLong_Check(op) (PyInt_Check(op) || PyLong_Check(op))
128 #endif
129 
130 #if PY_MAJOR_VERSION >= 3
131 # define PyInt_FromLong PyLong_FromLong
132 # define PyInt_FromSsize_t PyLong_FromSsize_t
133 # define PyInt_AsSsize_t PyLong_AsSsize_t
134 # define PyInt_AsLong PyLong_AsLong
135 #endif
136 
137 #if PY_MAJOR_VERSION >= 3
138 /* This is the default on Python3 and constant has been removed. */
139 # define Py_TPFLAGS_CHECKTYPES 0
140 #endif
141 
142 #if PY_MAJOR_VERSION < 3
143 # undef PyCapsule_GetPointer
144 # undef PyCapsule_New
145 # define PyCapsule_GetPointer(capsule, name) \
146     (PyCObject_AsVoidPtr(capsule))
147 # define PyCapsule_New(pointer, name, destructor) \
148     (PyCObject_FromVoidPtr(pointer, destructor))
149 #endif
150 
151 /************************************************************/
152 
153 /* base type flag: exactly one of the following: */
154 #define CT_PRIMITIVE_SIGNED   0x001   /* signed integer */
155 #define CT_PRIMITIVE_UNSIGNED 0x002   /* unsigned integer */
156 #define CT_PRIMITIVE_CHAR     0x004   /* char, wchar_t, charN_t */
157 #define CT_PRIMITIVE_FLOAT    0x008   /* float, double, long double */
158 #define CT_POINTER            0x010   /* pointer, excluding ptr-to-func */
159 #define CT_ARRAY              0x020   /* array */
160 #define CT_STRUCT             0x040   /* struct */
161 #define CT_UNION              0x080   /* union */
162 #define CT_FUNCTIONPTR        0x100   /* pointer to function */
163 #define CT_VOID               0x200   /* void */
164 #define CT_PRIMITIVE_COMPLEX  0x400   /* float _Complex, double _Complex */
165 
166 /* other flags that may also be set in addition to the base flag: */
167 #define CT_IS_VOIDCHAR_PTR     0x00001000
168 #define CT_PRIMITIVE_FITS_LONG 0x00002000
169 #define CT_IS_OPAQUE           0x00004000
170 #define CT_IS_ENUM             0x00008000
171 #define CT_IS_PTR_TO_OWNED     0x00010000 /* only owned if CDataOwning_Type */
172 #define CT_CUSTOM_FIELD_POS    0x00020000
173 #define CT_IS_LONGDOUBLE       0x00040000
174 #define CT_IS_BOOL             0x00080000
175 #define CT_IS_FILE             0x00100000
176 #define CT_IS_VOID_PTR         0x00200000
177 #define CT_WITH_VAR_ARRAY      0x00400000
178 /* unused                      0x00800000 */
179 #define CT_LAZY_FIELD_LIST     0x01000000
180 #define CT_WITH_PACKED_CHANGE  0x02000000
181 #define CT_IS_SIGNED_WCHAR     0x04000000
182 #define CT_PRIMITIVE_ANY  (CT_PRIMITIVE_SIGNED |        \
183                            CT_PRIMITIVE_UNSIGNED |      \
184                            CT_PRIMITIVE_CHAR |          \
185                            CT_PRIMITIVE_FLOAT |         \
186                            CT_PRIMITIVE_COMPLEX)
187 
188 typedef struct _ctypedescr {
189     PyObject_VAR_HEAD
190 
191     struct _ctypedescr *ct_itemdescr;  /* ptrs and arrays: the item type */
192     PyObject *ct_stuff;                /* structs: dict of the fields
193                                           arrays: ctypedescr of the ptr type
194                                           function: tuple(abi, ctres, ctargs..)
195                                           enum: pair {"name":x},{x:"name"}
196                                           ptrs: lazily, ctypedescr of array */
197     void *ct_extra;                    /* structs: first field (not a ref!)
198                                           function types: cif_description
199                                           primitives: prebuilt "cif" object */
200 
201     PyObject *ct_weakreflist;    /* weakref support */
202 
203     PyObject *ct_unique_key;    /* key in unique_cache (a string, but not
204                                    human-readable) */
205 
206     Py_ssize_t ct_size;     /* size of instances, or -1 if unknown */
207     Py_ssize_t ct_length;   /* length of arrays, or -1 if unknown;
208                                or alignment of primitive and struct types;
209                                always -1 for pointers */
210     int ct_flags;           /* CT_xxx flags */
211 
212     int ct_name_position;   /* index in ct_name of where to put a var name */
213     char ct_name[1];        /* string, e.g. "int *" for pointers to ints */
214 } CTypeDescrObject;
215 
216 typedef struct {
217     PyObject_HEAD
218     CTypeDescrObject *c_type;
219     char *c_data;
220     PyObject *c_weakreflist;
221 } CDataObject;
222 
223 typedef struct cfieldobject_s {
224     PyObject_HEAD
225     CTypeDescrObject *cf_type;
226     Py_ssize_t cf_offset;
227     short cf_bitshift;   /* >= 0: bitshift; or BS_REGULAR or BS_EMPTY_ARRAY */
228     short cf_bitsize;
229     unsigned char cf_flags;   /* BF_... */
230     struct cfieldobject_s *cf_next;
231 } CFieldObject;
232 #define BS_REGULAR            (-1) /* a regular field, not with bitshift */
233 #define BS_EMPTY_ARRAY        (-2) /* a field declared 'type[0]' or 'type[]' */
234 #define BF_IGNORE_IN_CTOR     0x01 /* union field not in the first place */
235 
236 static PyTypeObject CTypeDescr_Type;
237 static PyTypeObject CField_Type;
238 static PyTypeObject CData_Type;
239 static PyTypeObject CDataOwning_Type;
240 static PyTypeObject CDataOwningGC_Type;
241 static PyTypeObject CDataGCP_Type;
242 
243 #define CTypeDescr_Check(ob)  (Py_TYPE(ob) == &CTypeDescr_Type)
244 #define CData_Check(ob)       (Py_TYPE(ob) == &CData_Type ||            \
245                                Py_TYPE(ob) == &CDataOwning_Type ||      \
246                                Py_TYPE(ob) == &CDataOwningGC_Type ||    \
247                                Py_TYPE(ob) == &CDataGCP_Type)
248 #define CDataOwn_Check(ob)    (Py_TYPE(ob) == &CDataOwning_Type ||      \
249                                Py_TYPE(ob) == &CDataOwningGC_Type)
250 
251 typedef union {
252     unsigned char m_char;
253     unsigned short m_short;
254     unsigned int m_int;
255     unsigned long m_long;
256     unsigned long long m_longlong;
257     float m_float;
258     double m_double;
259     long double m_longdouble;
260 } union_alignment;
261 
262 typedef struct {
263     CDataObject head;
264     union_alignment alignment;
265 } CDataObject_casted_primitive;
266 
267 typedef struct {
268     CDataObject head;
269     union_alignment alignment;
270 } CDataObject_own_nolength;
271 
272 typedef struct {
273     CDataObject head;
274     Py_ssize_t length;
275     union_alignment alignment;
276 } CDataObject_own_length;
277 
278 typedef struct {
279     CDataObject head;
280     PyObject *structobj;
281 } CDataObject_own_structptr;
282 
283 typedef struct {
284     CDataObject head;
285     Py_ssize_t length;     /* same as CDataObject_own_length up to here */
286     Py_buffer *bufferview;
287 } CDataObject_owngc_frombuf;
288 
289 typedef struct {
290     CDataObject head;
291     Py_ssize_t length;     /* same as CDataObject_own_length up to here */
292     PyObject *origobj;
293     PyObject *destructor;
294 } CDataObject_gcp;
295 
296 typedef struct {
297     CDataObject head;
298     ffi_closure *closure;
299 } CDataObject_closure;
300 
301 typedef struct {
302     ffi_cif cif;
303     /* the following information is used when doing the call:
304        - a buffer of size 'exchange_size' is malloced
305        - the arguments are converted from Python objects to raw data
306        - the i'th raw data is stored at 'buffer + exchange_offset_arg[1+i]'
307        - the call is done
308        - the result is read back from 'buffer + exchange_offset_arg[0]' */
309     Py_ssize_t exchange_size;
310     Py_ssize_t exchange_offset_arg[1];
311 } cif_description_t;
312 
313 #define ADD_WRAPAROUND(x, y)  ((Py_ssize_t)(((size_t)(x)) + ((size_t)(y))))
314 #define MUL_WRAPAROUND(x, y)  ((Py_ssize_t)(((size_t)(x)) * ((size_t)(y))))
315 
316 
317 /* whenever running Python code, the errno is saved in this thread-local
318    variable */
319 #ifndef MS_WIN32
320 # include "misc_thread_posix.h"
321 #endif
322 
323 #include "minibuffer.h"
324 
325 #if PY_MAJOR_VERSION >= 3
326 # include "file_emulator.h"
327 #endif
328 
329 #ifdef PyUnicode_KIND     /* Python >= 3.3 */
330 # include "wchar_helper_3.h"
331 #else
332 # include "wchar_helper.h"
333 #endif
334 
335 #include "../cffi/_cffi_errors.h"
336 
337 typedef struct _cffi_allocator_s {
338     PyObject *ca_alloc, *ca_free;
339     int ca_dont_clear;
340 } cffi_allocator_t;
341 static const cffi_allocator_t default_allocator = { NULL, NULL, 0 };
342 static PyObject *FFIError;
343 static PyObject *unique_cache;
344 
345 /************************************************************/
346 
347 static CTypeDescrObject *
ctypedescr_new(int name_size)348 ctypedescr_new(int name_size)
349 {
350     CTypeDescrObject *ct = PyObject_GC_NewVar(CTypeDescrObject,
351                                               &CTypeDescr_Type,
352                                               name_size);
353     if (ct == NULL)
354         return NULL;
355 
356     ct->ct_itemdescr = NULL;
357     ct->ct_stuff = NULL;
358     ct->ct_weakreflist = NULL;
359     ct->ct_unique_key = NULL;
360     PyObject_GC_Track(ct);
361     return ct;
362 }
363 
364 static CTypeDescrObject *
ctypedescr_new_on_top(CTypeDescrObject * ct_base,const char * extra_text,int extra_position)365 ctypedescr_new_on_top(CTypeDescrObject *ct_base, const char *extra_text,
366                       int extra_position)
367 {
368     int base_name_len = strlen(ct_base->ct_name);
369     int extra_name_len = strlen(extra_text);
370     CTypeDescrObject *ct = ctypedescr_new(base_name_len + extra_name_len + 1);
371     char *p;
372     if (ct == NULL)
373         return NULL;
374 
375     Py_INCREF(ct_base);
376     ct->ct_itemdescr = ct_base;
377     ct->ct_name_position = ct_base->ct_name_position + extra_position;
378 
379     p = ct->ct_name;
380     memcpy(p, ct_base->ct_name, ct_base->ct_name_position);
381     p += ct_base->ct_name_position;
382     memcpy(p, extra_text, extra_name_len);
383     p += extra_name_len;
384     memcpy(p, ct_base->ct_name + ct_base->ct_name_position,
385            base_name_len - ct_base->ct_name_position + 1);
386 
387     return ct;
388 }
389 
390 static PyObject *
ctypedescr_repr(CTypeDescrObject * ct)391 ctypedescr_repr(CTypeDescrObject *ct)
392 {
393     return PyText_FromFormat("<ctype '%s'>", ct->ct_name);
394 }
395 
396 static void
ctypedescr_dealloc(CTypeDescrObject * ct)397 ctypedescr_dealloc(CTypeDescrObject *ct)
398 {
399     PyObject_GC_UnTrack(ct);
400     if (ct->ct_weakreflist != NULL)
401         PyObject_ClearWeakRefs((PyObject *) ct);
402 
403     if (ct->ct_unique_key != NULL) {
404         /* revive dead object temporarily for DelItem */
405         Py_REFCNT(ct) = 43;
406         PyDict_DelItem(unique_cache, ct->ct_unique_key);
407         assert(Py_REFCNT(ct) == 42);
408         Py_REFCNT(ct) = 0;
409         Py_DECREF(ct->ct_unique_key);
410     }
411     Py_XDECREF(ct->ct_itemdescr);
412     Py_XDECREF(ct->ct_stuff);
413     if (ct->ct_flags & CT_FUNCTIONPTR)
414         PyObject_Free(ct->ct_extra);
415     Py_TYPE(ct)->tp_free((PyObject *)ct);
416 }
417 
418 static int
ctypedescr_traverse(CTypeDescrObject * ct,visitproc visit,void * arg)419 ctypedescr_traverse(CTypeDescrObject *ct, visitproc visit, void *arg)
420 {
421     Py_VISIT(ct->ct_itemdescr);
422     Py_VISIT(ct->ct_stuff);
423     return 0;
424 }
425 
426 static int
ctypedescr_clear(CTypeDescrObject * ct)427 ctypedescr_clear(CTypeDescrObject *ct)
428 {
429     Py_CLEAR(ct->ct_itemdescr);
430     Py_CLEAR(ct->ct_stuff);
431     return 0;
432 }
433 
434 
nosuchattr(const char * attr)435 static PyObject *nosuchattr(const char *attr)
436 {
437     PyErr_SetString(PyExc_AttributeError, attr);
438     return NULL;
439 }
440 
ctypeget_kind(CTypeDescrObject * ct,void * context)441 static PyObject *ctypeget_kind(CTypeDescrObject *ct, void *context)
442 {
443     char *result;
444     if (ct->ct_flags & CT_PRIMITIVE_ANY) {
445         if (ct->ct_flags & CT_IS_ENUM)
446             result = "enum";
447         else
448             result = "primitive";
449     }
450     else if (ct->ct_flags & CT_POINTER) {
451         result = "pointer";
452     }
453     else if (ct->ct_flags & CT_ARRAY) {
454         result = "array";
455     }
456     else if (ct->ct_flags & CT_VOID) {
457         result = "void";
458     }
459     else if (ct->ct_flags & CT_STRUCT) {
460         result = "struct";
461     }
462     else if (ct->ct_flags & CT_UNION) {
463         result = "union";
464     }
465     else if (ct->ct_flags & CT_FUNCTIONPTR) {
466         result = "function";
467     }
468     else
469         result = "?";
470 
471     return PyText_FromString(result);
472 }
473 
ctypeget_cname(CTypeDescrObject * ct,void * context)474 static PyObject *ctypeget_cname(CTypeDescrObject *ct, void *context)
475 {
476     return PyText_FromString(ct->ct_name);
477 }
478 
ctypeget_item(CTypeDescrObject * ct,void * context)479 static PyObject *ctypeget_item(CTypeDescrObject *ct, void *context)
480 {
481     if (ct->ct_flags & (CT_POINTER | CT_ARRAY)) {
482         Py_INCREF(ct->ct_itemdescr);
483         return (PyObject *)ct->ct_itemdescr;
484     }
485     return nosuchattr("item");
486 }
487 
ctypeget_length(CTypeDescrObject * ct,void * context)488 static PyObject *ctypeget_length(CTypeDescrObject *ct, void *context)
489 {
490     if (ct->ct_flags & CT_ARRAY) {
491         if (ct->ct_length >= 0) {
492             return PyInt_FromSsize_t(ct->ct_length);
493         }
494         else {
495             Py_INCREF(Py_None);
496             return Py_None;
497         }
498     }
499     return nosuchattr("length");
500 }
501 
502 static PyObject *
503 get_field_name(CTypeDescrObject *ct, CFieldObject *cf);   /* forward */
504 
505 /* returns 0 if the struct ctype is opaque, 1 if it is not, or -1 if
506    an exception occurs */
507 #define force_lazy_struct(ct)                                           \
508     ((ct)->ct_stuff != NULL ? 1 : do_realize_lazy_struct(ct))
509 
510 static int do_realize_lazy_struct(CTypeDescrObject *ct);
511 /* forward, implemented in realize_c_type.c */
512 
ctypeget_fields(CTypeDescrObject * ct,void * context)513 static PyObject *ctypeget_fields(CTypeDescrObject *ct, void *context)
514 {
515     if (ct->ct_flags & (CT_STRUCT | CT_UNION)) {
516         if (!(ct->ct_flags & CT_IS_OPAQUE)) {
517             CFieldObject *cf;
518             PyObject *res;
519             if (force_lazy_struct(ct) < 0)
520                 return NULL;
521             res = PyList_New(0);
522             if (res == NULL)
523                 return NULL;
524             for (cf = (CFieldObject *)ct->ct_extra;
525                  cf != NULL; cf = cf->cf_next) {
526                 PyObject *o = PyTuple_Pack(2, get_field_name(ct, cf),
527                                            (PyObject *)cf);
528                 int err = (o != NULL) ? PyList_Append(res, o) : -1;
529                 Py_XDECREF(o);
530                 if (err < 0) {
531                     Py_DECREF(res);
532                     return NULL;
533                 }
534             }
535             return res;
536         }
537         else {
538             Py_INCREF(Py_None);
539             return Py_None;
540         }
541     }
542     return nosuchattr("fields");
543 }
544 
ctypeget_args(CTypeDescrObject * ct,void * context)545 static PyObject *ctypeget_args(CTypeDescrObject *ct, void *context)
546 {
547     if (ct->ct_flags & CT_FUNCTIONPTR) {
548         PyObject *t = ct->ct_stuff;
549         return PyTuple_GetSlice(t, 2, PyTuple_GET_SIZE(t));
550     }
551     return nosuchattr("args");
552 }
553 
ctypeget_result(CTypeDescrObject * ct,void * context)554 static PyObject *ctypeget_result(CTypeDescrObject *ct, void *context)
555 {
556     if (ct->ct_flags & CT_FUNCTIONPTR) {
557         PyObject *res = PyTuple_GetItem(ct->ct_stuff, 1);
558         Py_XINCREF(res);
559         return res;
560     }
561     return nosuchattr("result");
562 }
563 
ctypeget_ellipsis(CTypeDescrObject * ct,void * context)564 static PyObject *ctypeget_ellipsis(CTypeDescrObject *ct, void *context)
565 {
566     if (ct->ct_flags & CT_FUNCTIONPTR) {
567         PyObject *res = ct->ct_extra ? Py_False : Py_True;
568         Py_INCREF(res);
569         return res;
570     }
571     return nosuchattr("ellipsis");
572 }
573 
ctypeget_abi(CTypeDescrObject * ct,void * context)574 static PyObject *ctypeget_abi(CTypeDescrObject *ct, void *context)
575 {
576     if (ct->ct_flags & CT_FUNCTIONPTR) {
577         PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0);
578         Py_XINCREF(res);
579         return res;
580     }
581     return nosuchattr("abi");
582 }
583 
ctypeget_elements(CTypeDescrObject * ct,void * context)584 static PyObject *ctypeget_elements(CTypeDescrObject *ct, void *context)
585 {
586     if (ct->ct_flags & CT_IS_ENUM) {
587         PyObject *res = PyTuple_GetItem(ct->ct_stuff, 1);
588         if (res) res = PyDict_Copy(res);
589         return res;
590     }
591     return nosuchattr("elements");
592 }
593 
ctypeget_relements(CTypeDescrObject * ct,void * context)594 static PyObject *ctypeget_relements(CTypeDescrObject *ct, void *context)
595 {
596     if (ct->ct_flags & CT_IS_ENUM) {
597         PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0);
598         if (res) res = PyDict_Copy(res);
599         return res;
600     }
601     return nosuchattr("relements");
602 }
603 
604 static PyGetSetDef ctypedescr_getsets[] = {
605     {"kind", (getter)ctypeget_kind, NULL, "kind"},
606     {"cname", (getter)ctypeget_cname, NULL, "C name"},
607     {"item", (getter)ctypeget_item, NULL, "pointer to, or array of"},
608     {"length", (getter)ctypeget_length, NULL, "array length or None"},
609     {"fields", (getter)ctypeget_fields, NULL, "struct or union fields"},
610     {"args", (getter)ctypeget_args, NULL, "function argument types"},
611     {"result", (getter)ctypeget_result, NULL, "function result type"},
612     {"ellipsis", (getter)ctypeget_ellipsis, NULL, "function has '...'"},
613     {"abi", (getter)ctypeget_abi, NULL, "function ABI"},
614     {"elements", (getter)ctypeget_elements, NULL, "enum elements"},
615     {"relements", (getter)ctypeget_relements, NULL, "enum elements, reverse"},
616     {NULL}                        /* sentinel */
617 };
618 
619 static PyObject *
ctypedescr_dir(PyObject * ct,PyObject * noarg)620 ctypedescr_dir(PyObject *ct, PyObject *noarg)
621 {
622     int err;
623     struct PyGetSetDef *gsdef;
624     PyObject *res = PyList_New(0);
625     if (res == NULL)
626         return NULL;
627 
628     for (gsdef = ctypedescr_getsets; gsdef->name; gsdef++) {
629         PyObject *x = PyObject_GetAttrString(ct, gsdef->name);
630         if (x == NULL) {
631             PyErr_Clear();
632         }
633         else {
634             Py_DECREF(x);
635             x = PyText_FromString(gsdef->name);
636             err = (x != NULL) ? PyList_Append(res, x) : -1;
637             Py_XDECREF(x);
638             if (err < 0) {
639                 Py_DECREF(res);
640                 return NULL;
641             }
642         }
643     }
644     return res;
645 }
646 
647 static PyMethodDef ctypedescr_methods[] = {
648     {"__dir__",   ctypedescr_dir,  METH_NOARGS},
649     {NULL,        NULL}           /* sentinel */
650 };
651 
652 static PyTypeObject CTypeDescr_Type = {
653     PyVarObject_HEAD_INIT(NULL, 0)
654     "_cffi_backend.CTypeDescr",
655     offsetof(CTypeDescrObject, ct_name),
656     sizeof(char),
657     (destructor)ctypedescr_dealloc,             /* tp_dealloc */
658     0,                                          /* tp_print */
659     0,                                          /* tp_getattr */
660     0,                                          /* tp_setattr */
661     0,                                          /* tp_compare */
662     (reprfunc)ctypedescr_repr,                  /* tp_repr */
663     0,                                          /* tp_as_number */
664     0,                                          /* tp_as_sequence */
665     0,                                          /* tp_as_mapping */
666     0,                                          /* tp_hash */
667     0,                                          /* tp_call */
668     0,                                          /* tp_str */
669     PyObject_GenericGetAttr,                    /* tp_getattro */
670     0,                                          /* tp_setattro */
671     0,                                          /* tp_as_buffer */
672     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
673     0,                                          /* tp_doc */
674     (traverseproc)ctypedescr_traverse,          /* tp_traverse */
675     (inquiry)ctypedescr_clear,                  /* tp_clear */
676     0,                                          /* tp_richcompare */
677     offsetof(CTypeDescrObject, ct_weakreflist), /* tp_weaklistoffset */
678     0,                                          /* tp_iter */
679     0,                                          /* tp_iternext */
680     ctypedescr_methods,                         /* tp_methods */
681     0,                                          /* tp_members */
682     ctypedescr_getsets,                         /* tp_getset */
683 };
684 
685 /************************************************************/
686 
687 static PyObject *
get_field_name(CTypeDescrObject * ct,CFieldObject * cf)688 get_field_name(CTypeDescrObject *ct, CFieldObject *cf)
689 {
690     Py_ssize_t i = 0;
691     PyObject *d_key, *d_value;
692     while (PyDict_Next(ct->ct_stuff, &i, &d_key, &d_value)) {
693         if (d_value == (PyObject *)cf)
694             return d_key;
695     }
696     Py_FatalError("_cffi_backend: get_field_name()");
697     return NULL;
698 }
699 
700 static void
cfield_dealloc(CFieldObject * cf)701 cfield_dealloc(CFieldObject *cf)
702 {
703     Py_DECREF(cf->cf_type);
704     PyObject_Del(cf);
705 }
706 
707 #undef OFF
708 #define OFF(x) offsetof(CFieldObject, x)
709 
710 static PyMemberDef cfield_members[] = {
711     {"type", T_OBJECT, OFF(cf_type), READONLY},
712     {"offset", T_PYSSIZET, OFF(cf_offset), READONLY},
713     {"bitshift", T_SHORT, OFF(cf_bitshift), READONLY},
714     {"bitsize", T_SHORT, OFF(cf_bitsize), READONLY},
715     {"flags", T_UBYTE, OFF(cf_flags), READONLY},
716     {NULL}      /* Sentinel */
717 };
718 #undef OFF
719 
720 static PyTypeObject CField_Type = {
721     PyVarObject_HEAD_INIT(NULL, 0)
722     "_cffi_backend.CField",
723     sizeof(CFieldObject),
724     0,
725     (destructor)cfield_dealloc,                 /* tp_dealloc */
726     0,                                          /* tp_print */
727     0,                                          /* tp_getattr */
728     0,                                          /* tp_setattr */
729     0,                                          /* tp_compare */
730     0,                                          /* tp_repr */
731     0,                                          /* tp_as_number */
732     0,                                          /* tp_as_sequence */
733     0,                                          /* tp_as_mapping */
734     0,                                          /* tp_hash */
735     0,                                          /* tp_call */
736     0,                                          /* tp_str */
737     PyObject_GenericGetAttr,                    /* tp_getattro */
738     0,                                          /* tp_setattro */
739     0,                                          /* tp_as_buffer */
740     Py_TPFLAGS_DEFAULT,                         /* tp_flags */
741     0,                                          /* tp_doc */
742     0,                                          /* tp_traverse */
743     0,                                          /* tp_clear */
744     0,                                          /* tp_richcompare */
745     0,                                          /* tp_weaklistoffset */
746     0,                                          /* tp_iter */
747     0,                                          /* tp_iternext */
748     0,                                          /* tp_methods */
749     cfield_members,                             /* tp_members */
750 };
751 
752 /************************************************************/
753 
754 static int
CDataObject_Or_PyFloat_Check(PyObject * ob)755 CDataObject_Or_PyFloat_Check(PyObject *ob)
756 {
757     return (PyFloat_Check(ob) ||
758             (CData_Check(ob) &&
759              (((CDataObject *)ob)->c_type->ct_flags & CT_PRIMITIVE_FLOAT)));
760 }
761 
762 static PY_LONG_LONG
_my_PyLong_AsLongLong(PyObject * ob)763 _my_PyLong_AsLongLong(PyObject *ob)
764 {
765     /* (possibly) convert and cast a Python object to a long long.
766        Like PyLong_AsLongLong(), this version accepts a Python int too, and
767        does convertions from other types of objects.  The difference is that
768        this version refuses floats. */
769 #if PY_MAJOR_VERSION < 3
770     if (PyInt_Check(ob)) {
771         return PyInt_AS_LONG(ob);
772     }
773     else
774 #endif
775     if (PyLong_Check(ob)) {
776         return PyLong_AsLongLong(ob);
777     }
778     else {
779         PyObject *io;
780         PY_LONG_LONG res;
781         PyNumberMethods *nb = ob->ob_type->tp_as_number;
782 
783         if (CDataObject_Or_PyFloat_Check(ob) ||
784                 nb == NULL || nb->nb_int == NULL) {
785             PyErr_SetString(PyExc_TypeError, "an integer is required");
786             return -1;
787         }
788         io = (*nb->nb_int) (ob);
789         if (io == NULL)
790             return -1;
791 
792         if (PyIntOrLong_Check(io)) {
793             res = _my_PyLong_AsLongLong(io);
794         }
795         else {
796             PyErr_SetString(PyExc_TypeError, "integer conversion failed");
797             res = -1;
798         }
799         Py_DECREF(io);
800         return res;
801     }
802 }
803 
804 static unsigned PY_LONG_LONG
_my_PyLong_AsUnsignedLongLong(PyObject * ob,int strict)805 _my_PyLong_AsUnsignedLongLong(PyObject *ob, int strict)
806 {
807     /* (possibly) convert and cast a Python object to an unsigned long long.
808        Like PyLong_AsLongLong(), this version accepts a Python int too, and
809        does convertions from other types of objects.  If 'strict', complains
810        with OverflowError and refuses floats.  If '!strict', rounds floats
811        and masks the result. */
812 #if PY_MAJOR_VERSION < 3
813     if (PyInt_Check(ob)) {
814         long value1 = PyInt_AS_LONG(ob);
815         if (strict && value1 < 0)
816             goto negative;
817         return (unsigned PY_LONG_LONG)(PY_LONG_LONG)value1;
818     }
819     else
820 #endif
821     if (PyLong_Check(ob)) {
822         if (strict) {
823             if (_PyLong_Sign(ob) < 0)
824                 goto negative;
825             return PyLong_AsUnsignedLongLong(ob);
826         }
827         else {
828             return PyLong_AsUnsignedLongLongMask(ob);
829         }
830     }
831     else {
832         PyObject *io;
833         unsigned PY_LONG_LONG res;
834         PyNumberMethods *nb = ob->ob_type->tp_as_number;
835 
836         if ((strict && CDataObject_Or_PyFloat_Check(ob)) ||
837                 nb == NULL || nb->nb_int == NULL) {
838             PyErr_SetString(PyExc_TypeError, "an integer is required");
839             return (unsigned PY_LONG_LONG)-1;
840         }
841         io = (*nb->nb_int) (ob);
842         if (io == NULL)
843             return (unsigned PY_LONG_LONG)-1;
844 
845         if (PyIntOrLong_Check(io)) {
846             res = _my_PyLong_AsUnsignedLongLong(io, strict);
847         }
848         else {
849             PyErr_SetString(PyExc_TypeError, "integer conversion failed");
850             res = (unsigned PY_LONG_LONG)-1;
851         }
852         Py_DECREF(io);
853         return res;
854     }
855 
856  negative:
857     PyErr_SetString(PyExc_OverflowError,
858                     "can't convert negative number to unsigned");
859     return (unsigned PY_LONG_LONG)-1;
860 }
861 
862 #define _read_raw_data(type)                    \
863     do {                                        \
864         if (size == sizeof(type)) {             \
865             type r;                             \
866             memcpy(&r, target, sizeof(type));   \
867             return r;                           \
868         }                                       \
869     } while(0)
870 
871 static PY_LONG_LONG
read_raw_signed_data(char * target,int size)872 read_raw_signed_data(char *target, int size)
873 {
874     _read_raw_data(signed char);
875     _read_raw_data(short);
876     _read_raw_data(int);
877     _read_raw_data(long);
878     _read_raw_data(PY_LONG_LONG);
879     Py_FatalError("read_raw_signed_data: bad integer size");
880     return 0;
881 }
882 
883 static unsigned PY_LONG_LONG
read_raw_unsigned_data(char * target,int size)884 read_raw_unsigned_data(char *target, int size)
885 {
886     _read_raw_data(unsigned char);
887     _read_raw_data(unsigned short);
888     _read_raw_data(unsigned int);
889     _read_raw_data(unsigned long);
890     _read_raw_data(unsigned PY_LONG_LONG);
891     Py_FatalError("read_raw_unsigned_data: bad integer size");
892     return 0;
893 }
894 
895 #ifdef __GNUC__
896 /* This is a workaround for what I think is a GCC bug on several
897    platforms.  See issue #378. */
898 __attribute__((noinline))
899 #endif
_cffi_memcpy(char * target,const void * src,size_t size)900 void _cffi_memcpy(char *target, const void *src, size_t size)
901 {
902     memcpy(target, src, size);
903 }
904 
905 #define _write_raw_data(type)                           \
906     do {                                                \
907         if (size == sizeof(type)) {                     \
908             type r = (type)source;                      \
909             _cffi_memcpy(target, &r, sizeof(type));           \
910             return;                                     \
911         }                                               \
912     } while(0)
913 
914 static void
write_raw_integer_data(char * target,unsigned PY_LONG_LONG source,int size)915 write_raw_integer_data(char *target, unsigned PY_LONG_LONG source, int size)
916 {
917     _write_raw_data(unsigned char);
918     _write_raw_data(unsigned short);
919     _write_raw_data(unsigned int);
920     _write_raw_data(unsigned long);
921     _write_raw_data(unsigned PY_LONG_LONG);
922     Py_FatalError("write_raw_integer_data: bad integer size");
923 }
924 
925 static double
read_raw_float_data(char * target,int size)926 read_raw_float_data(char *target, int size)
927 {
928     _read_raw_data(float);
929     _read_raw_data(double);
930     Py_FatalError("read_raw_float_data: bad float size");
931     return 0;
932 }
933 
934 static long double
read_raw_longdouble_data(char * target)935 read_raw_longdouble_data(char *target)
936 {
937     int size = sizeof(long double);
938     _read_raw_data(long double);
939     Py_FatalError("read_raw_longdouble_data: bad long double size");
940     return 0;
941 }
942 
943 static Py_complex
read_raw_complex_data(char * target,int size)944 read_raw_complex_data(char *target, int size)
945 {
946     Py_complex r = {0.0, 0.0};
947     if (size == 2*sizeof(float)) {
948         float real_part, imag_part;
949         memcpy(&real_part, target + 0,             sizeof(float));
950         memcpy(&imag_part, target + sizeof(float), sizeof(float));
951         r.real = real_part;
952         r.imag = imag_part;
953         return r;
954     }
955     if (size == 2*sizeof(double)) {
956         memcpy(&r, target, 2*sizeof(double));
957         return r;
958     }
959     Py_FatalError("read_raw_complex_data: bad complex size");
960     return r;
961 }
962 
963 static void
write_raw_float_data(char * target,double source,int size)964 write_raw_float_data(char *target, double source, int size)
965 {
966     _write_raw_data(float);
967     _write_raw_data(double);
968     Py_FatalError("write_raw_float_data: bad float size");
969 }
970 
971 static void
write_raw_longdouble_data(char * target,long double source)972 write_raw_longdouble_data(char *target, long double source)
973 {
974     int size = sizeof(long double);
975     _write_raw_data(long double);
976 }
977 
978 #define _write_raw_complex_data(type)                      \
979     do {                                                   \
980         if (size == 2*sizeof(type)) {                      \
981             type r = (type)source.real;                    \
982             type i = (type)source.imag;                    \
983             _cffi_memcpy(target, &r, sizeof(type));              \
984             _cffi_memcpy(target+sizeof(type), &i, sizeof(type)); \
985             return;                                        \
986         }                                                  \
987     } while(0)
988 
989 static void
write_raw_complex_data(char * target,Py_complex source,int size)990 write_raw_complex_data(char *target, Py_complex source, int size)
991 {
992     _write_raw_complex_data(float);
993     _write_raw_complex_data(double);
994     Py_FatalError("write_raw_complex_data: bad complex size");
995 }
996 
997 static PyObject *
new_simple_cdata(char * data,CTypeDescrObject * ct)998 new_simple_cdata(char *data, CTypeDescrObject *ct)
999 {
1000     CDataObject *cd = PyObject_New(CDataObject, &CData_Type);
1001     if (cd == NULL)
1002         return NULL;
1003     Py_INCREF(ct);
1004     cd->c_data = data;
1005     cd->c_type = ct;
1006     cd->c_weakreflist = NULL;
1007     return (PyObject *)cd;
1008 }
1009 
1010 static PyObject *
new_sized_cdata(char * data,CTypeDescrObject * ct,Py_ssize_t length)1011 new_sized_cdata(char *data, CTypeDescrObject *ct, Py_ssize_t length)
1012 {
1013     CDataObject_own_length *scd;
1014 
1015     scd = (CDataObject_own_length *)PyObject_Malloc(
1016         offsetof(CDataObject_own_length, alignment));
1017     if (PyObject_Init((PyObject *)scd, &CData_Type) == NULL)
1018         return NULL;
1019     Py_INCREF(ct);
1020     scd->head.c_type = ct;
1021     scd->head.c_data = data;
1022     scd->head.c_weakreflist = NULL;
1023     scd->length = length;
1024     return (PyObject *)scd;
1025 }
1026 
1027 static CDataObject *_new_casted_primitive(CTypeDescrObject *ct);  /*forward*/
1028 
1029 static PyObject *
convert_to_object(char * data,CTypeDescrObject * ct)1030 convert_to_object(char *data, CTypeDescrObject *ct)
1031 {
1032     if (!(ct->ct_flags & CT_PRIMITIVE_ANY)) {
1033         /* non-primitive types (check done just for performance) */
1034         if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) {
1035             char *ptrdata = *(char **)data;
1036             /*READ(data, sizeof(char *))*/
1037             return new_simple_cdata(ptrdata, ct);
1038         }
1039         else if (ct->ct_flags & CT_IS_OPAQUE) {
1040             PyErr_Format(PyExc_TypeError, "cdata '%s' is opaque",
1041                          ct->ct_name);
1042             return NULL;
1043         }
1044         else if (ct->ct_flags & (CT_STRUCT|CT_UNION)) {
1045             return new_simple_cdata(data, ct);
1046         }
1047         else if (ct->ct_flags & CT_ARRAY) {
1048             if (ct->ct_length < 0) {
1049                 /* we can't return a <cdata 'int[]'> here, because we don't
1050                    know the length to give it.  As a compromize, returns
1051                    <cdata 'int *'> in this case. */
1052                 ct = (CTypeDescrObject *)ct->ct_stuff;
1053             }
1054             return new_simple_cdata(data, ct);
1055         }
1056     }
1057     else if (ct->ct_flags & CT_PRIMITIVE_SIGNED) {
1058         PY_LONG_LONG value;
1059         /*READ(data, ct->ct_size)*/
1060         value = read_raw_signed_data(data, ct->ct_size);
1061         if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG)
1062             return PyInt_FromLong((long)value);
1063         else
1064             return PyLong_FromLongLong(value);
1065     }
1066     else if (ct->ct_flags & CT_PRIMITIVE_UNSIGNED) {
1067         unsigned PY_LONG_LONG value;
1068         /*READ(data, ct->ct_size)*/
1069         value = read_raw_unsigned_data(data, ct->ct_size);
1070 
1071         if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) {
1072             if (ct->ct_flags & CT_IS_BOOL) {
1073                 PyObject *x;
1074                 switch ((int)value) {
1075                 case 0: x = Py_False; break;
1076                 case 1: x = Py_True; break;
1077                 default:
1078                     PyErr_Format(PyExc_ValueError,
1079                                  "got a _Bool of value %d, expected 0 or 1",
1080                                  (int)value);
1081                     return NULL;
1082                 }
1083                 Py_INCREF(x);
1084                 return x;
1085             }
1086             return PyInt_FromLong((long)value);
1087         }
1088         else
1089             return PyLong_FromUnsignedLongLong(value);
1090     }
1091     else if (ct->ct_flags & CT_PRIMITIVE_FLOAT) {
1092         /*READ(data, ct->ct_size)*/
1093         if (!(ct->ct_flags & CT_IS_LONGDOUBLE)) {
1094             double value = read_raw_float_data(data, ct->ct_size);
1095             return PyFloat_FromDouble(value);
1096         }
1097         else {
1098             long double value = read_raw_longdouble_data(data);
1099             CDataObject *cd = _new_casted_primitive(ct);
1100             if (cd != NULL)
1101                 write_raw_longdouble_data(cd->c_data, value);
1102             return (PyObject *)cd;
1103         }
1104     }
1105     else if (ct->ct_flags & CT_PRIMITIVE_CHAR) {
1106         /*READ(data, ct->ct_size)*/
1107         switch (ct->ct_size) {
1108         case sizeof(char):
1109             return PyBytes_FromStringAndSize(data, 1);
1110         case 2:
1111             return _my_PyUnicode_FromChar16((cffi_char16_t *)data, 1);
1112         case 4:
1113             return _my_PyUnicode_FromChar32((cffi_char32_t *)data, 1);
1114         }
1115     }
1116     else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
1117         Py_complex value = read_raw_complex_data(data, ct->ct_size);
1118         return PyComplex_FromCComplex(value);
1119     }
1120 
1121     PyErr_Format(PyExc_SystemError,
1122                  "convert_to_object: '%s'", ct->ct_name);
1123     return NULL;
1124 }
1125 
1126 static PyObject *
convert_to_object_bitfield(char * data,CFieldObject * cf)1127 convert_to_object_bitfield(char *data, CFieldObject *cf)
1128 {
1129     CTypeDescrObject *ct = cf->cf_type;
1130     /*READ(data, ct->ct_size)*/
1131 
1132     if (ct->ct_flags & CT_PRIMITIVE_SIGNED) {
1133         unsigned PY_LONG_LONG value, valuemask, shiftforsign;
1134         PY_LONG_LONG result;
1135 
1136         value = (unsigned PY_LONG_LONG)read_raw_signed_data(data, ct->ct_size);
1137         valuemask = (1ULL << cf->cf_bitsize) - 1ULL;
1138         shiftforsign = 1ULL << (cf->cf_bitsize - 1);
1139         value = ((value >> cf->cf_bitshift) + shiftforsign) & valuemask;
1140         result = ((PY_LONG_LONG)value) - (PY_LONG_LONG)shiftforsign;
1141 
1142         if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG)
1143             return PyInt_FromLong((long)result);
1144         else
1145             return PyLong_FromLongLong(result);
1146     }
1147     else {
1148         unsigned PY_LONG_LONG value, valuemask;
1149 
1150         value = read_raw_unsigned_data(data, ct->ct_size);
1151         valuemask = (1ULL << cf->cf_bitsize) - 1ULL;
1152         value = (value >> cf->cf_bitshift) & valuemask;
1153 
1154         if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG)
1155             return PyInt_FromLong((long)value);
1156         else
1157             return PyLong_FromUnsignedLongLong(value);
1158     }
1159 }
1160 
_convert_overflow(PyObject * init,const char * ct_name)1161 static int _convert_overflow(PyObject *init, const char *ct_name)
1162 {
1163     PyObject *s;
1164     if (PyErr_Occurred())   /* already an exception pending */
1165         return -1;
1166     s = PyObject_Str(init);
1167     if (s == NULL)
1168         return -1;
1169     PyErr_Format(PyExc_OverflowError, "integer %s does not fit '%s'",
1170                  PyText_AS_UTF8(s), ct_name);
1171     Py_DECREF(s);
1172     return -1;
1173 }
1174 
_convert_to_char(PyObject * init)1175 static int _convert_to_char(PyObject *init)
1176 {
1177     if (PyBytes_Check(init) && PyBytes_GET_SIZE(init) == 1) {
1178         return (unsigned char)(PyBytes_AS_STRING(init)[0]);
1179     }
1180     if (CData_Check(init) &&
1181            (((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) &&
1182            (((CDataObject *)init)->c_type->ct_size == sizeof(char))) {
1183         char *data = ((CDataObject *)init)->c_data;
1184         /*READ(data, 1)*/
1185         return *(unsigned char *)data;
1186     }
1187     PyErr_Format(PyExc_TypeError,
1188                  "initializer for ctype 'char' must be a "STR_OR_BYTES
1189                  " of length 1, not %.200s", Py_TYPE(init)->tp_name);
1190     return -1;
1191 }
1192 
_convert_to_char16_t(PyObject * init)1193 static cffi_char16_t _convert_to_char16_t(PyObject *init)
1194 {
1195     char err_got[80];
1196     err_got[0] = 0;
1197 
1198     if (PyUnicode_Check(init)) {
1199         cffi_char16_t ordinal;
1200         if (_my_PyUnicode_AsSingleChar16(init, &ordinal, err_got) == 0)
1201             return ordinal;
1202     }
1203     if (CData_Check(init) &&
1204            (((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) &&
1205            (((CDataObject *)init)->c_type->ct_size == 2)) {
1206         char *data = ((CDataObject *)init)->c_data;
1207         /*READ(data, 2)*/
1208         return *(cffi_char16_t *)data;
1209     }
1210     PyErr_Format(PyExc_TypeError,
1211                  "initializer for ctype 'char16_t' must be a unicode string "
1212                  "of length 1, not %.200s",
1213                  err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got);
1214     return (cffi_char16_t)-1;
1215 }
1216 
_convert_to_char32_t(PyObject * init)1217 static cffi_char32_t _convert_to_char32_t(PyObject *init)
1218 {
1219     char err_got[80];
1220     err_got[0] = 0;
1221 
1222     if (PyUnicode_Check(init)) {
1223         cffi_char32_t ordinal;
1224         if (_my_PyUnicode_AsSingleChar32(init, &ordinal, err_got) == 0)
1225             return ordinal;
1226     }
1227     if (CData_Check(init) &&
1228            (((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) &&
1229            (((CDataObject *)init)->c_type->ct_size == 4)) {
1230         char *data = ((CDataObject *)init)->c_data;
1231         /*READ(data, 4)*/
1232         return *(cffi_char32_t *)data;
1233     }
1234     PyErr_Format(PyExc_TypeError,
1235                  "initializer for ctype 'char32_t' must be a unicode string "
1236                  "of length 1, not %.200s",
1237                  err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got);
1238     return (cffi_char32_t)-1;
1239 }
1240 
_convert_error(PyObject * init,CTypeDescrObject * ct,const char * expected)1241 static int _convert_error(PyObject *init, CTypeDescrObject *ct,
1242                           const char *expected)
1243 {
1244     if (CData_Check(init)) {
1245         CTypeDescrObject *ct2 = ((CDataObject *)init)->c_type;
1246         if (strcmp(ct->ct_name, ct2->ct_name) != 0)
1247             PyErr_Format(PyExc_TypeError,
1248                          "initializer for ctype '%s' must be a %s, "
1249                          "not cdata '%s'",
1250                          ct->ct_name, expected, ct2->ct_name);
1251         else if (ct != ct2) {
1252             /* in case we'd give the error message "initializer for
1253                ctype 'A' must be a pointer to same type, not cdata
1254                'B'", but with A=B, then give instead a different error
1255                message to try to clear up the confusion */
1256             PyErr_Format(PyExc_TypeError,
1257                          "initializer for ctype '%s' appears indeed to be '%s',"
1258                          " but the types are different (check that you are not"
1259                          " e.g. mixing up different ffi instances)",
1260                          ct->ct_name, ct2->ct_name);
1261         }
1262         else
1263         {
1264             PyErr_Format(PyExc_SystemError,
1265                          "initializer for ctype '%s' is correct, but we get "
1266                          "an internal mismatch--please report a bug",
1267                          ct->ct_name);
1268         }
1269     }
1270     else
1271         PyErr_Format(PyExc_TypeError,
1272                      "initializer for ctype '%s' must be a %s, "
1273                      "not %.200s",
1274                      ct->ct_name, expected, Py_TYPE(init)->tp_name);
1275     return -1;
1276 }
1277 
1278 static int    /* forward */
1279 convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init);
1280 static int    /* forward */
1281 convert_from_object_bitfield(char *data, CFieldObject *cf, PyObject *init);
1282 
1283 static Py_ssize_t
get_new_array_length(CTypeDescrObject * ctitem,PyObject ** pvalue)1284 get_new_array_length(CTypeDescrObject *ctitem, PyObject **pvalue)
1285 {
1286     PyObject *value = *pvalue;
1287 
1288     if (PyList_Check(value) || PyTuple_Check(value)) {
1289         return PySequence_Fast_GET_SIZE(value);
1290     }
1291     else if (PyBytes_Check(value)) {
1292         /* from a string, we add the null terminator */
1293         return PyBytes_GET_SIZE(value) + 1;
1294     }
1295     else if (PyUnicode_Check(value)) {
1296         /* from a unicode, we add the null terminator */
1297         int length;
1298         if (ctitem->ct_size == 2)
1299             length = _my_PyUnicode_SizeAsChar16(value);
1300         else
1301             length = _my_PyUnicode_SizeAsChar32(value);
1302         return length + 1;
1303     }
1304     else {
1305         Py_ssize_t explicitlength;
1306         explicitlength = PyNumber_AsSsize_t(value, PyExc_OverflowError);
1307         if (explicitlength < 0) {
1308             if (PyErr_Occurred()) {
1309                 if (PyErr_ExceptionMatches(PyExc_TypeError))
1310                     PyErr_Format(PyExc_TypeError,
1311                         "expected new array length or list/tuple/str, "
1312                         "not %.200s", Py_TYPE(value)->tp_name);
1313             }
1314             else
1315                 PyErr_SetString(PyExc_ValueError, "negative array length");
1316             return -1;
1317         }
1318         *pvalue = Py_None;
1319         return explicitlength;
1320     }
1321 }
1322 
1323 static int
convert_field_from_object(char * data,CFieldObject * cf,PyObject * value)1324 convert_field_from_object(char *data, CFieldObject *cf, PyObject *value)
1325 {
1326     data += cf->cf_offset;
1327     if (cf->cf_bitshift >= 0)
1328         return convert_from_object_bitfield(data, cf, value);
1329     else
1330         return convert_from_object(data, cf->cf_type, value);
1331 }
1332 
1333 static int
convert_vfield_from_object(char * data,CFieldObject * cf,PyObject * value,Py_ssize_t * optvarsize)1334 convert_vfield_from_object(char *data, CFieldObject *cf, PyObject *value,
1335                            Py_ssize_t *optvarsize)
1336 {
1337     /* a special case for var-sized C99 arrays */
1338     if ((cf->cf_type->ct_flags & CT_ARRAY) && cf->cf_type->ct_size < 0) {
1339         Py_ssize_t varsizelength = get_new_array_length(
1340                                       cf->cf_type->ct_itemdescr, &value);
1341         if (varsizelength < 0)
1342             return -1;
1343         if (optvarsize != NULL) {
1344             /* in this mode, the only purpose of this function is to compute
1345                the real size of the structure from a var-sized C99 array */
1346             Py_ssize_t size, itemsize;
1347             assert(data == NULL);
1348             itemsize = cf->cf_type->ct_itemdescr->ct_size;
1349             size = ADD_WRAPAROUND(cf->cf_offset,
1350                                   MUL_WRAPAROUND(itemsize, varsizelength));
1351             if (size < 0 ||
1352                 ((size - cf->cf_offset) / itemsize) != varsizelength) {
1353                 PyErr_SetString(PyExc_OverflowError,
1354                                 "array size would overflow a Py_ssize_t");
1355                 return -1;
1356             }
1357             if (size > *optvarsize)
1358                 *optvarsize = size;
1359             return 0;
1360         }
1361         /* if 'value' was only an integer, get_new_array_length() returns
1362            it and convert 'value' to be None.  Detect if this was the case,
1363            and if so, stop here, leaving the content uninitialized
1364            (it should be zero-initialized from somewhere else). */
1365         if (value == Py_None)
1366             return 0;
1367     }
1368     if (optvarsize == NULL)
1369         return convert_field_from_object(data, cf, value);
1370     else
1371         return 0;
1372 }
1373 
1374 static int
must_be_array_of_zero_or_one(const char * data,Py_ssize_t n)1375 must_be_array_of_zero_or_one(const char *data, Py_ssize_t n)
1376 {
1377     Py_ssize_t i;
1378     for (i = 0; i < n; i++) {
1379         if (((unsigned char)data[i]) > 1) {
1380             PyErr_SetString(PyExc_ValueError,
1381                 "an array of _Bool can only contain \\x00 or \\x01");
1382             return -1;
1383         }
1384     }
1385     return 0;
1386 }
1387 
1388 static Py_ssize_t
get_array_length(CDataObject * cd)1389 get_array_length(CDataObject *cd)
1390 {
1391     if (cd->c_type->ct_length < 0)
1392         return ((CDataObject_own_length *)cd)->length;
1393     else
1394         return cd->c_type->ct_length;
1395 }
1396 
1397 static int
convert_array_from_object(char * data,CTypeDescrObject * ct,PyObject * init)1398 convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
1399 {
1400     /* used by convert_from_object(), and also to decode lists/tuples/unicodes
1401        passed as function arguments.  'ct' is an CT_ARRAY in the first case
1402        and a CT_POINTER in the second case. */
1403     const char *expected;
1404     CTypeDescrObject *ctitem = ct->ct_itemdescr;
1405 
1406     if (PyList_Check(init) || PyTuple_Check(init)) {
1407         PyObject **items;
1408         Py_ssize_t i, n;
1409         n = PySequence_Fast_GET_SIZE(init);
1410         if (ct->ct_length >= 0 && n > ct->ct_length) {
1411             PyErr_Format(PyExc_IndexError,
1412                          "too many initializers for '%s' (got %zd)",
1413                          ct->ct_name, n);
1414             return -1;
1415         }
1416         items = PySequence_Fast_ITEMS(init);
1417         for (i=0; i<n; i++) {
1418             if (convert_from_object(data, ctitem, items[i]) < 0)
1419                 return -1;
1420             data += ctitem->ct_size;
1421         }
1422         return 0;
1423     }
1424     else if ((ctitem->ct_flags & CT_PRIMITIVE_CHAR) ||
1425              ((ctitem->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED))
1426               && (ctitem->ct_size == sizeof(char)))) {
1427         if (ctitem->ct_size == sizeof(char)) {
1428             char *srcdata;
1429             Py_ssize_t n;
1430             if (!PyBytes_Check(init)) {
1431                 expected = STR_OR_BYTES" or list or tuple";
1432                 goto cannot_convert;
1433             }
1434             n = PyBytes_GET_SIZE(init);
1435             if (ct->ct_length >= 0 && n > ct->ct_length) {
1436                 PyErr_Format(PyExc_IndexError,
1437                              "initializer "STR_OR_BYTES" is too long for '%s' "
1438                              "(got %zd characters)", ct->ct_name, n);
1439                 return -1;
1440             }
1441             if (n != ct->ct_length)
1442                 n++;
1443             srcdata = PyBytes_AS_STRING(init);
1444             if (ctitem->ct_flags & CT_IS_BOOL)
1445                 if (must_be_array_of_zero_or_one(srcdata, n) < 0)
1446                     return -1;
1447             memcpy(data, srcdata, n);
1448             return 0;
1449         }
1450         else {
1451             Py_ssize_t n;
1452             if (!PyUnicode_Check(init)) {
1453                 expected = "unicode or list or tuple";
1454                 goto cannot_convert;
1455             }
1456 
1457             if (ctitem->ct_size == 4)
1458                 n = _my_PyUnicode_SizeAsChar32(init);
1459             else
1460                 n = _my_PyUnicode_SizeAsChar16(init);
1461 
1462             if (ct->ct_length >= 0 && n > ct->ct_length) {
1463                 PyErr_Format(PyExc_IndexError,
1464                              "initializer unicode is too long for '%s' "
1465                              "(got %zd characters)", ct->ct_name, n);
1466                 return -1;
1467             }
1468             if (n != ct->ct_length)
1469                 n++;
1470             if (ctitem->ct_size == 4)
1471                 return _my_PyUnicode_AsChar32(init, (cffi_char32_t *)data, n);
1472             else
1473                 return _my_PyUnicode_AsChar16(init, (cffi_char16_t *)data, n);
1474         }
1475     }
1476     else {
1477         expected = "list or tuple";
1478         goto cannot_convert;
1479     }
1480 
1481  cannot_convert:
1482     if ((ct->ct_flags & CT_ARRAY) && CData_Check(init))
1483     {
1484         CDataObject *cd = (CDataObject *)init;
1485         if (cd->c_type == ct)
1486         {
1487             Py_ssize_t n = get_array_length(cd);
1488             memcpy(data, cd->c_data, n * ctitem->ct_size);
1489             return 0;
1490         }
1491     }
1492     return _convert_error(init, ct, expected);
1493 }
1494 
1495 static int
convert_struct_from_object(char * data,CTypeDescrObject * ct,PyObject * init,Py_ssize_t * optvarsize)1496 convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init,
1497                            Py_ssize_t *optvarsize)
1498 {
1499     /* does not accept 'init' being already a CData */
1500     const char *expected;
1501 
1502     if (force_lazy_struct(ct) <= 0) {
1503         if (!PyErr_Occurred())
1504             PyErr_Format(PyExc_TypeError, "'%s' is opaque", ct->ct_name);
1505         return -1;
1506     }
1507 
1508     if (PyList_Check(init) || PyTuple_Check(init)) {
1509         PyObject **items = PySequence_Fast_ITEMS(init);
1510         Py_ssize_t i, n = PySequence_Fast_GET_SIZE(init);
1511         CFieldObject *cf = (CFieldObject *)ct->ct_extra;
1512 
1513         for (i=0; i<n; i++) {
1514             while (cf != NULL && (cf->cf_flags & BF_IGNORE_IN_CTOR))
1515                 cf = cf->cf_next;
1516             if (cf == NULL) {
1517                 PyErr_Format(PyExc_ValueError,
1518                              "too many initializers for '%s' (got %zd)",
1519                              ct->ct_name, n);
1520                 return -1;
1521             }
1522             if (convert_vfield_from_object(data, cf, items[i], optvarsize) < 0)
1523                 return -1;
1524             cf = cf->cf_next;
1525         }
1526         return 0;
1527     }
1528     if (PyDict_Check(init)) {
1529         PyObject *d_key, *d_value;
1530         Py_ssize_t i = 0;
1531         CFieldObject *cf;
1532 
1533         while (PyDict_Next(init, &i, &d_key, &d_value)) {
1534             cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, d_key);
1535             if (cf == NULL) {
1536                 PyErr_SetObject(PyExc_KeyError, d_key);
1537                 return -1;
1538             }
1539             if (convert_vfield_from_object(data, cf, d_value, optvarsize) < 0)
1540                 return -1;
1541         }
1542         return 0;
1543     }
1544     expected = optvarsize == NULL ? "list or tuple or dict or struct-cdata"
1545                                   : "list or tuple or dict";
1546     return _convert_error(init, ct, expected);
1547 }
1548 
1549 #ifdef __GNUC__
1550 # if __GNUC__ >= 4
1551 /* Don't go inlining this huge function.  Needed because occasionally
1552    it gets inlined in places where is causes a warning: call to
1553    __builtin___memcpy_chk will always overflow destination buffer
1554    (which is places where the 'ct' should never represent such a large
1555    primitive type anyway). */
1556 __attribute__((noinline))
1557 # endif
1558 #endif
1559 static int
convert_from_object(char * data,CTypeDescrObject * ct,PyObject * init)1560 convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init)
1561 {
1562     const char *expected;
1563     char buf[sizeof(PY_LONG_LONG)];
1564 
1565     /*if (ct->ct_size > 0)*/
1566         /*WRITE(data, ct->ct_size)*/
1567 
1568     if (ct->ct_flags & CT_ARRAY) {
1569         return convert_array_from_object(data, ct, init);
1570     }
1571     if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) {
1572         char *ptrdata;
1573         CTypeDescrObject *ctinit;
1574 
1575         if (!CData_Check(init)) {
1576             expected = "cdata pointer";
1577             goto cannot_convert;
1578         }
1579         ctinit = ((CDataObject *)init)->c_type;
1580         if (!(ctinit->ct_flags & (CT_POINTER|CT_FUNCTIONPTR))) {
1581             if (ctinit->ct_flags & CT_ARRAY)
1582                 ctinit = (CTypeDescrObject *)ctinit->ct_stuff;
1583             else {
1584                 expected = "pointer or array";
1585                 goto cannot_convert;
1586             }
1587         }
1588         if (ctinit != ct) {
1589             int combined_flags = ct->ct_flags | ctinit->ct_flags;
1590             if (combined_flags & CT_IS_VOID_PTR)
1591                 ;   /* accept "void *" as either source or target */
1592             else if (combined_flags & CT_IS_VOIDCHAR_PTR) {
1593                 /* for backward compatibility, accept "char *" as either
1594                    source of target.  This is not what C does, though,
1595                    so emit a warning that will eventually turn into an
1596                    error.  The warning is turned off if both types are
1597                    pointers to single bytes. */
1598                 char *msg = (ct->ct_flags & CT_IS_VOIDCHAR_PTR ?
1599                     "implicit cast to 'char *' from a different pointer type: "
1600                     "will be forbidden in the future (check that the types "
1601                     "are as you expect; use an explicit ffi.cast() if they "
1602                     "are correct)" :
1603                     "implicit cast from 'char *' to a different pointer type: "
1604                     "will be forbidden in the future (check that the types "
1605                     "are as you expect; use an explicit ffi.cast() if they "
1606                     "are correct)");
1607                 if ((ct->ct_flags & ctinit->ct_flags & CT_POINTER) &&
1608                     ct->ct_itemdescr->ct_size == 1 &&
1609                     ctinit->ct_itemdescr->ct_size == 1) {
1610                     /* no warning */
1611                 }
1612                 else if (PyErr_WarnEx(PyExc_UserWarning, msg, 1))
1613                     return -1;
1614             }
1615             else {
1616                 expected = "pointer to same type";
1617                 goto cannot_convert;
1618             }
1619         }
1620         ptrdata = ((CDataObject *)init)->c_data;
1621 
1622         *(char **)data = ptrdata;
1623         return 0;
1624     }
1625     if (ct->ct_flags & CT_PRIMITIVE_SIGNED) {
1626         PY_LONG_LONG value = _my_PyLong_AsLongLong(init);
1627         if (value == -1 && PyErr_Occurred())
1628             return -1;
1629         write_raw_integer_data(buf, value, ct->ct_size);
1630         if (value != read_raw_signed_data(buf, ct->ct_size))
1631             goto overflow;
1632         write_raw_integer_data(data, value, ct->ct_size);
1633         return 0;
1634     }
1635     if (ct->ct_flags & CT_PRIMITIVE_UNSIGNED) {
1636         unsigned PY_LONG_LONG value = _my_PyLong_AsUnsignedLongLong(init, 1);
1637         if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
1638             return -1;
1639         if (ct->ct_flags & CT_IS_BOOL) {
1640             if (value > 1ULL)      /* value != 0 && value != 1 */
1641                 goto overflow;
1642         }
1643         else {
1644             write_raw_integer_data(buf, value, ct->ct_size);
1645             if (value != read_raw_unsigned_data(buf, ct->ct_size))
1646                 goto overflow;
1647         }
1648         write_raw_integer_data(data, value, ct->ct_size);
1649         return 0;
1650     }
1651     if (ct->ct_flags & CT_PRIMITIVE_FLOAT) {
1652         double value;
1653         if ((ct->ct_flags & CT_IS_LONGDOUBLE) &&
1654                 CData_Check(init) &&
1655                 (((CDataObject *)init)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
1656             long double lvalue;
1657             char *initdata = ((CDataObject *)init)->c_data;
1658             /*READ(initdata, sizeof(long double))*/
1659             lvalue = read_raw_longdouble_data(initdata);
1660             write_raw_longdouble_data(data, lvalue);
1661             return 0;
1662         }
1663         value = PyFloat_AsDouble(init);
1664         if (value == -1.0 && PyErr_Occurred())
1665             return -1;
1666         if (!(ct->ct_flags & CT_IS_LONGDOUBLE))
1667             write_raw_float_data(data, value, ct->ct_size);
1668         else
1669             write_raw_longdouble_data(data, (long double)value);
1670         return 0;
1671     }
1672     if (ct->ct_flags & CT_PRIMITIVE_CHAR) {
1673         switch (ct->ct_size) {
1674         case sizeof(char): {
1675             int res = _convert_to_char(init);
1676             if (res < 0)
1677                 return -1;
1678             data[0] = res;
1679             return 0;
1680         }
1681         case 2: {
1682             cffi_char16_t res = _convert_to_char16_t(init);
1683             if (res == (cffi_char16_t)-1 && PyErr_Occurred())
1684                 return -1;
1685             *(cffi_char16_t *)data = res;
1686             return 0;
1687         }
1688         case 4: {
1689             cffi_char32_t res = _convert_to_char32_t(init);
1690             if (res == (cffi_char32_t)-1 && PyErr_Occurred())
1691                 return -1;
1692             *(cffi_char32_t *)data = res;
1693             return 0;
1694         }
1695         }
1696     }
1697     if (ct->ct_flags & (CT_STRUCT|CT_UNION)) {
1698 
1699         if (CData_Check(init)) {
1700             if (((CDataObject *)init)->c_type == ct && ct->ct_size >= 0) {
1701                 memcpy(data, ((CDataObject *)init)->c_data, ct->ct_size);
1702                 return 0;
1703             }
1704         }
1705         return convert_struct_from_object(data, ct, init, NULL);
1706     }
1707     if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
1708         Py_complex value = PyComplex_AsCComplex(init);
1709         if (PyErr_Occurred())
1710             return -1;
1711         write_raw_complex_data(data, value, ct->ct_size);
1712         return 0;
1713     }
1714     PyErr_Format(PyExc_SystemError,
1715                  "convert_from_object: '%s'", ct->ct_name);
1716     return -1;
1717 
1718  overflow:
1719     return _convert_overflow(init, ct->ct_name);
1720 
1721  cannot_convert:
1722     return _convert_error(init, ct, expected);
1723 }
1724 
1725 static int
convert_from_object_bitfield(char * data,CFieldObject * cf,PyObject * init)1726 convert_from_object_bitfield(char *data, CFieldObject *cf, PyObject *init)
1727 {
1728     CTypeDescrObject *ct = cf->cf_type;
1729     PY_LONG_LONG fmin, fmax, value = PyLong_AsLongLong(init);
1730     unsigned PY_LONG_LONG rawfielddata, rawvalue, rawmask;
1731     if (value == -1 && PyErr_Occurred())
1732         return -1;
1733 
1734     if (ct->ct_flags & CT_PRIMITIVE_SIGNED) {
1735         fmin = -(1LL << (cf->cf_bitsize-1));
1736         fmax = (1LL << (cf->cf_bitsize-1)) - 1LL;
1737         if (fmax == 0)
1738             fmax = 1;    /* special case to let "int x:1" receive "1" */
1739     }
1740     else {
1741         fmin = 0LL;
1742         fmax = (PY_LONG_LONG)((1ULL << cf->cf_bitsize) - 1ULL);
1743     }
1744     if (value < fmin || value > fmax) {
1745         /* phew, PyErr_Format does not support "%lld" in Python 2.6 */
1746         PyObject *svalue = NULL, *sfmin = NULL, *sfmax = NULL;
1747         PyObject *lfmin = NULL, *lfmax = NULL;
1748         svalue = PyObject_Str(init);
1749         if (svalue == NULL) goto skip;
1750         lfmin = PyLong_FromLongLong(fmin);
1751         if (lfmin == NULL) goto skip;
1752         sfmin = PyObject_Str(lfmin);
1753         if (sfmin == NULL) goto skip;
1754         lfmax = PyLong_FromLongLong(fmax);
1755         if (lfmax == NULL) goto skip;
1756         sfmax = PyObject_Str(lfmax);
1757         if (sfmax == NULL) goto skip;
1758         PyErr_Format(PyExc_OverflowError,
1759                      "value %s outside the range allowed by the "
1760                      "bit field width: %s <= x <= %s",
1761                      PyText_AS_UTF8(svalue),
1762                      PyText_AS_UTF8(sfmin),
1763                      PyText_AS_UTF8(sfmax));
1764        skip:
1765         Py_XDECREF(svalue);
1766         Py_XDECREF(sfmin);
1767         Py_XDECREF(sfmax);
1768         Py_XDECREF(lfmin);
1769         Py_XDECREF(lfmax);
1770         return -1;
1771     }
1772 
1773     rawmask = ((1ULL << cf->cf_bitsize) - 1ULL) << cf->cf_bitshift;
1774     rawvalue = ((unsigned PY_LONG_LONG)value) << cf->cf_bitshift;
1775     /*WRITE(data, ct->ct_size)*/
1776     rawfielddata = read_raw_unsigned_data(data, ct->ct_size);
1777     rawfielddata = (rawfielddata & ~rawmask) | (rawvalue & rawmask);
1778     write_raw_integer_data(data, rawfielddata, ct->ct_size);
1779     return 0;
1780 }
1781 
1782 static int
get_alignment(CTypeDescrObject * ct)1783 get_alignment(CTypeDescrObject *ct)
1784 {
1785     int align;
1786  retry:
1787     if ((ct->ct_flags & (CT_PRIMITIVE_ANY|CT_STRUCT|CT_UNION)) &&
1788         !(ct->ct_flags & CT_IS_OPAQUE)) {
1789         align = ct->ct_length;
1790         if (align == -1 && (ct->ct_flags & CT_LAZY_FIELD_LIST)) {
1791             force_lazy_struct(ct);
1792             align = ct->ct_length;
1793         }
1794     }
1795     else if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) {
1796         struct aligncheck_ptr { char x; char *y; };
1797         align = offsetof(struct aligncheck_ptr, y);
1798     }
1799     else if (ct->ct_flags & CT_ARRAY) {
1800         ct = ct->ct_itemdescr;
1801         goto retry;
1802     }
1803     else {
1804         PyErr_Format(PyExc_ValueError, "ctype '%s' is of unknown alignment",
1805                      ct->ct_name);
1806         return -1;
1807     }
1808 
1809     if ((align < 1) || (align & (align-1))) {
1810         PyErr_Format(PyExc_SystemError,
1811                      "found for ctype '%s' bogus alignment '%d'",
1812                      ct->ct_name, align);
1813         return -1;
1814     }
1815     return align;
1816 }
1817 
cdata_dealloc(CDataObject * cd)1818 static void cdata_dealloc(CDataObject *cd)
1819 {
1820     if (cd->c_weakreflist != NULL)
1821         PyObject_ClearWeakRefs((PyObject *) cd);
1822 
1823     Py_DECREF(cd->c_type);
1824 #ifndef CFFI_MEM_LEAK     /* never release anything, tests only */
1825     Py_TYPE(cd)->tp_free((PyObject *)cd);
1826 #endif
1827 }
1828 
cdataowning_dealloc(CDataObject * cd)1829 static void cdataowning_dealloc(CDataObject *cd)
1830 {
1831     assert(!(cd->c_type->ct_flags & (CT_IS_VOID_PTR | CT_FUNCTIONPTR)));
1832 
1833     if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) {
1834         Py_DECREF(((CDataObject_own_structptr *)cd)->structobj);
1835     }
1836 #if defined(CFFI_MEM_DEBUG) || defined(CFFI_MEM_LEAK)
1837     if (cd->c_type->ct_flags & (CT_PRIMITIVE_ANY | CT_STRUCT | CT_UNION)) {
1838         assert(cd->c_type->ct_size >= 0);
1839         memset(cd->c_data, 0xDD, cd->c_type->ct_size);
1840     }
1841     else if (cd->c_type->ct_flags & CT_ARRAY) {
1842         Py_ssize_t x = get_array_length(cd);
1843         assert(x >= 0);
1844         x *= cd->c_type->ct_itemdescr->ct_size;
1845         assert(x >= 0);
1846         memset(cd->c_data, 0xDD, x);
1847     }
1848 #endif
1849     cdata_dealloc(cd);
1850 }
1851 
cdataowninggc_dealloc(CDataObject * cd)1852 static void cdataowninggc_dealloc(CDataObject *cd)
1853 {
1854     assert(!(cd->c_type->ct_flags & (CT_IS_PTR_TO_OWNED |
1855                                      CT_PRIMITIVE_ANY |
1856                                      CT_STRUCT | CT_UNION)));
1857     PyObject_GC_UnTrack(cd);
1858 
1859     if (cd->c_type->ct_flags & CT_IS_VOID_PTR) {        /* a handle */
1860         PyObject *x = ((CDataObject_own_structptr *)cd)->structobj;
1861         Py_DECREF(x);
1862     }
1863     else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) {   /* a callback */
1864         ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
1865         PyObject *args = (PyObject *)(closure->user_data);
1866         Py_XDECREF(args);
1867 #ifdef CFFI_TRUST_LIBFFI
1868         ffi_closure_free(closure);
1869 #else
1870         cffi_closure_free(closure);
1871 #endif
1872     }
1873     else if (cd->c_type->ct_flags & CT_ARRAY) {         /* from_buffer */
1874         Py_buffer *view = ((CDataObject_owngc_frombuf *)cd)->bufferview;
1875         PyBuffer_Release(view);
1876         PyObject_Free(view);
1877     }
1878     cdata_dealloc(cd);
1879 }
1880 
cdataowninggc_traverse(CDataObject * cd,visitproc visit,void * arg)1881 static int cdataowninggc_traverse(CDataObject *cd, visitproc visit, void *arg)
1882 {
1883     if (cd->c_type->ct_flags & CT_IS_VOID_PTR) {        /* a handle */
1884         PyObject *x = ((CDataObject_own_structptr *)cd)->structobj;
1885         Py_VISIT(x);
1886     }
1887     else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) {   /* a callback */
1888         ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
1889         PyObject *args = (PyObject *)(closure->user_data);
1890         Py_VISIT(args);
1891     }
1892     else if (cd->c_type->ct_flags & CT_ARRAY) {         /* from_buffer */
1893         Py_buffer *view = ((CDataObject_owngc_frombuf *)cd)->bufferview;
1894         Py_VISIT(view->obj);
1895     }
1896     return 0;
1897 }
1898 
cdataowninggc_clear(CDataObject * cd)1899 static int cdataowninggc_clear(CDataObject *cd)
1900 {
1901     if (cd->c_type->ct_flags & CT_IS_VOID_PTR) {        /* a handle */
1902         CDataObject_own_structptr *cd1 = (CDataObject_own_structptr *)cd;
1903         PyObject *x = cd1->structobj;
1904         Py_INCREF(Py_None);
1905         cd1->structobj = Py_None;
1906         Py_DECREF(x);
1907     }
1908     else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) {   /* a callback */
1909         ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
1910         PyObject *args = (PyObject *)(closure->user_data);
1911         closure->user_data = NULL;
1912         Py_XDECREF(args);
1913     }
1914     else if (cd->c_type->ct_flags & CT_ARRAY) {         /* from_buffer */
1915         Py_buffer *view = ((CDataObject_owngc_frombuf *)cd)->bufferview;
1916         PyBuffer_Release(view);
1917     }
1918     return 0;
1919 }
1920 
1921 /* forward */
1922 static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
1923                                       char *objdescr, PyObject *obj,
1924                                       char *extra_error_line);
1925 
1926 
gcp_finalize(PyObject * destructor,PyObject * origobj)1927 static void gcp_finalize(PyObject *destructor, PyObject *origobj)
1928 {
1929     /* NOTE: this decrements the reference count of the two arguments */
1930 
1931     if (destructor != NULL) {
1932         PyObject *result;
1933         PyObject *error_type, *error_value, *error_traceback;
1934 
1935         /* Save the current exception */
1936         PyErr_Fetch(&error_type, &error_value, &error_traceback);
1937 
1938         result = PyObject_CallFunctionObjArgs(destructor, origobj, NULL);
1939         if (result != NULL) {
1940             Py_DECREF(result);
1941         }
1942         else {
1943             PyObject *t, *v, *tb;
1944             PyErr_Fetch(&t, &v, &tb);
1945             /* Don't use error capture here, because it is very much
1946              * like errors at __del__(), and these ones are not captured
1947              * either */
1948             /* ecap = _cffi_start_error_capture(); */
1949             _my_PyErr_WriteUnraisable(t, v, tb, "From callback for ffi.gc ",
1950                                       origobj, NULL);
1951             /* _cffi_stop_error_capture(ecap); */
1952         }
1953         Py_DECREF(destructor);
1954 
1955         /* Restore the saved exception */
1956         PyErr_Restore(error_type, error_value, error_traceback);
1957     }
1958     Py_XDECREF(origobj);
1959 }
1960 
cdatagcp_finalize(CDataObject_gcp * cd)1961 static void cdatagcp_finalize(CDataObject_gcp *cd)
1962 {
1963     PyObject *destructor = cd->destructor;
1964     PyObject *origobj = cd->origobj;
1965     cd->destructor = NULL;
1966     cd->origobj = NULL;
1967     gcp_finalize(destructor, origobj);
1968 }
1969 
cdatagcp_dealloc(CDataObject_gcp * cd)1970 static void cdatagcp_dealloc(CDataObject_gcp *cd)
1971 {
1972     PyObject *destructor = cd->destructor;
1973     PyObject *origobj = cd->origobj;
1974     cdata_dealloc((CDataObject *)cd);
1975 
1976     gcp_finalize(destructor, origobj);
1977 }
1978 
cdatagcp_traverse(CDataObject_gcp * cd,visitproc visit,void * arg)1979 static int cdatagcp_traverse(CDataObject_gcp *cd, visitproc visit, void *arg)
1980 {
1981     Py_VISIT(cd->destructor);
1982     Py_VISIT(cd->origobj);
1983     return 0;
1984 }
1985 
1986 static PyObject *cdata_float(CDataObject *cd);  /*forward*/
1987 
convert_cdata_to_enum_string(CDataObject * cd,int both)1988 static PyObject *convert_cdata_to_enum_string(CDataObject *cd, int both)
1989 {
1990     PyObject *d_key, *d_value;
1991     CTypeDescrObject *ct = cd->c_type;
1992 
1993     assert(ct->ct_flags & CT_IS_ENUM);
1994     d_key = convert_to_object(cd->c_data, ct);
1995     if (d_key == NULL)
1996         return NULL;
1997 
1998     d_value = PyDict_GetItem(PyTuple_GET_ITEM(ct->ct_stuff, 1), d_key);
1999     if (d_value != NULL) {
2000         if (both) {
2001             PyObject *o = PyObject_Str(d_key);
2002             if (o == NULL)
2003                 d_value = NULL;
2004             else {
2005                 d_value = PyText_FromFormat("%s: %s",
2006                                             PyText_AS_UTF8(o),
2007                                             PyText_AS_UTF8(d_value));
2008                 Py_DECREF(o);
2009             }
2010         }
2011         else
2012             Py_INCREF(d_value);
2013     }
2014     else
2015         d_value = PyObject_Str(d_key);
2016     Py_DECREF(d_key);
2017     return d_value;
2018 }
2019 
cdata_repr(CDataObject * cd)2020 static PyObject *cdata_repr(CDataObject *cd)
2021 {
2022     char *extra;
2023     PyObject *result, *s;
2024 
2025     if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) {
2026         if (cd->c_type->ct_flags & CT_IS_ENUM) {
2027             s = convert_cdata_to_enum_string(cd, 1);
2028         }
2029         else if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE) {
2030             long double lvalue;
2031             char buffer[128];   /* big enough */
2032             /*READ(cd->c_data, sizeof(long double)*/
2033             lvalue = read_raw_longdouble_data(cd->c_data);
2034             sprintf(buffer, "%LE", lvalue);
2035             s = PyText_FromString(buffer);
2036         }
2037         else {
2038             PyObject *o = convert_to_object(cd->c_data, cd->c_type);
2039             if (o == NULL)
2040                 return NULL;
2041             s = PyObject_Repr(o);
2042             Py_DECREF(o);
2043         }
2044     }
2045     else if ((cd->c_type->ct_flags & CT_ARRAY) && cd->c_type->ct_length < 0) {
2046         s = PyText_FromFormat("sliced length %zd", get_array_length(cd));
2047     }
2048     else {
2049         if (cd->c_data != NULL) {
2050             s = PyText_FromFormat("%p", cd->c_data);
2051         }
2052         else
2053             s = PyText_FromString("NULL");
2054     }
2055     if (s == NULL)
2056         return NULL;
2057     /* it's slightly confusing to get "<cdata 'struct foo' 0x...>" because the
2058        struct foo is not owned.  Trying to make it clearer, write in this
2059        case "<cdata 'struct foo &' 0x...>". */
2060     if (cd->c_type->ct_flags & (CT_STRUCT|CT_UNION))
2061         extra = " &";
2062     else
2063         extra = "";
2064     result = PyText_FromFormat("<cdata '%s%s' %s>",
2065                                cd->c_type->ct_name, extra,
2066                                PyText_AsUTF8(s));
2067     Py_DECREF(s);
2068     return result;
2069 }
2070 
_cdata_repr2(CDataObject * cd,char * text,PyObject * x)2071 static PyObject *_cdata_repr2(CDataObject *cd, char *text, PyObject *x)
2072 {
2073     PyObject *res, *s = PyObject_Repr(x);
2074     if (s == NULL)
2075         return NULL;
2076     res = PyText_FromFormat("<cdata '%s' %s %s>",
2077                             cd->c_type->ct_name, text, PyText_AsUTF8(s));
2078     Py_DECREF(s);
2079     return res;
2080 }
2081 
_cdata_var_byte_size(CDataObject * cd)2082 static Py_ssize_t _cdata_var_byte_size(CDataObject *cd)
2083 {
2084     /* If 'cd' is a 'struct foo' or 'struct foo *' allocated with
2085        ffi.new(), and if the struct foo contains a varsize array,
2086        then return the real allocated size.  Otherwise, return -1. */
2087     if (!CDataOwn_Check(cd))
2088         return -1;
2089 
2090     if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) {
2091         cd = (CDataObject *)((CDataObject_own_structptr *)cd)->structobj;
2092     }
2093     if (cd->c_type->ct_flags & CT_WITH_VAR_ARRAY) {
2094         return ((CDataObject_own_length *)cd)->length;
2095     }
2096     return -1;
2097 }
2098 
cdataowning_repr(CDataObject * cd)2099 static PyObject *cdataowning_repr(CDataObject *cd)
2100 {
2101     Py_ssize_t size = _cdata_var_byte_size(cd);
2102     if (size < 0) {
2103         if (cd->c_type->ct_flags & CT_POINTER)
2104             size = cd->c_type->ct_itemdescr->ct_size;
2105         else if (cd->c_type->ct_flags & CT_ARRAY)
2106             size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size;
2107         else
2108             size = cd->c_type->ct_size;
2109     }
2110     return PyText_FromFormat("<cdata '%s' owning %zd bytes>",
2111                              cd->c_type->ct_name, size);
2112 }
2113 
cdataowninggc_repr(CDataObject * cd)2114 static PyObject *cdataowninggc_repr(CDataObject *cd)
2115 {
2116     if (cd->c_type->ct_flags & CT_IS_VOID_PTR) {        /* a handle */
2117         PyObject *x = ((CDataObject_own_structptr *)cd)->structobj;
2118         return _cdata_repr2(cd, "handle to", x);
2119     }
2120     else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) {   /* a callback */
2121         ffi_closure *closure = ((CDataObject_closure *)cd)->closure;
2122         PyObject *args = (PyObject *)closure->user_data;
2123         if (args == NULL)
2124             return cdata_repr(cd);
2125         else
2126             return _cdata_repr2(cd, "calling", PyTuple_GET_ITEM(args, 1));
2127     }
2128     else if (cd->c_type->ct_flags & CT_ARRAY) {         /* from_buffer */
2129         Py_buffer *view = ((CDataObject_owngc_frombuf *)cd)->bufferview;
2130         Py_ssize_t buflen = get_array_length(cd);
2131         return PyText_FromFormat(
2132             "<cdata '%s' buffer len %zd from '%.200s' object>",
2133             cd->c_type->ct_name,
2134             buflen,
2135             view->obj ? Py_TYPE(view->obj)->tp_name : "(null)");
2136     }
2137     return cdataowning_repr(cd);
2138 }
2139 
cdata_nonzero(CDataObject * cd)2140 static int cdata_nonzero(CDataObject *cd)
2141 {
2142     if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) {
2143         if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED |
2144                                     CT_PRIMITIVE_UNSIGNED |
2145                                     CT_PRIMITIVE_CHAR))
2146             return read_raw_unsigned_data(cd->c_data, cd->c_type->ct_size) != 0;
2147 
2148         if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
2149             if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE)
2150                 return read_raw_longdouble_data(cd->c_data) != 0.0;
2151             return read_raw_float_data(cd->c_data, cd->c_type->ct_size) != 0.0;
2152         }
2153         if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
2154             Py_complex value = read_raw_complex_data(cd->c_data,
2155                                                      cd->c_type->ct_size);
2156             return value.real != 0.0 || value.imag != 0.0;
2157         }
2158     }
2159     return cd->c_data != NULL;
2160 }
2161 
cdata_int(CDataObject * cd)2162 static PyObject *cdata_int(CDataObject *cd)
2163 {
2164     if ((cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_FITS_LONG))
2165                              == (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_FITS_LONG)) {
2166         /* this case is to handle enums, but also serves as a slight
2167            performance improvement for some other primitive types */
2168         long value;
2169         /*READ(cd->c_data, cd->c_type->ct_size)*/
2170         value = (long)read_raw_signed_data(cd->c_data, cd->c_type->ct_size);
2171         return PyInt_FromLong(value);
2172     }
2173     if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED)) {
2174         return convert_to_object(cd->c_data, cd->c_type);
2175     }
2176     else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) {
2177         /*READ(cd->c_data, cd->c_type->ct_size)*/
2178         switch (cd->c_type->ct_size) {
2179         case sizeof(char):
2180             return PyInt_FromLong((unsigned char)cd->c_data[0]);
2181         case 2:
2182             return PyInt_FromLong((long)*(cffi_char16_t *)cd->c_data);
2183         case 4:
2184             if (cd->c_type->ct_flags & CT_IS_SIGNED_WCHAR)
2185                 return PyInt_FromLong((long)*(int32_t *)cd->c_data);
2186             else if (sizeof(long) > 4)
2187                 return PyInt_FromLong(*(uint32_t *)cd->c_data);
2188             else
2189                 return PyLong_FromUnsignedLong(*(uint32_t *)cd->c_data);
2190         }
2191     }
2192     else if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
2193         PyObject *o = cdata_float(cd);
2194 #if PY_MAJOR_VERSION < 3
2195         PyObject *r = o ? PyNumber_Int(o) : NULL;
2196 #else
2197         PyObject *r = o ? PyNumber_Long(o) : NULL;
2198 #endif
2199         Py_XDECREF(o);
2200         return r;
2201     }
2202     PyErr_Format(PyExc_TypeError, "int() not supported on cdata '%s'",
2203                  cd->c_type->ct_name);
2204     return NULL;
2205 }
2206 
2207 #if PY_MAJOR_VERSION < 3
cdata_long(CDataObject * cd)2208 static PyObject *cdata_long(CDataObject *cd)
2209 {
2210     PyObject *res = cdata_int(cd);
2211     if (res != NULL && PyInt_CheckExact(res)) {
2212         PyObject *o = PyLong_FromLong(PyInt_AS_LONG(res));
2213         Py_DECREF(res);
2214         res = o;
2215     }
2216     return res;
2217 }
2218 #endif
2219 
cdata_float(CDataObject * cd)2220 static PyObject *cdata_float(CDataObject *cd)
2221 {
2222     if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
2223         double value;
2224         /*READ(cd->c_data, cd->c_type->ct_size)*/
2225         if (!(cd->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
2226             value = read_raw_float_data(cd->c_data, cd->c_type->ct_size);
2227         }
2228         else {
2229             value = (double)read_raw_longdouble_data(cd->c_data);
2230         }
2231         return PyFloat_FromDouble(value);
2232     }
2233     PyErr_Format(PyExc_TypeError, "float() not supported on cdata '%s'",
2234                  cd->c_type->ct_name);
2235     return NULL;
2236 }
2237 
cdata_richcompare(PyObject * v,PyObject * w,int op)2238 static PyObject *cdata_richcompare(PyObject *v, PyObject *w, int op)
2239 {
2240     int v_is_ptr, w_is_ptr;
2241     PyObject *pyres;
2242 
2243     assert(CData_Check(v));
2244 
2245     /* Comparisons involving a primitive cdata work differently than
2246      * comparisons involving a struct/array/pointer.
2247      *
2248      * If v or w is a struct/array/pointer, then the other must be too
2249      * (otherwise we return NotImplemented and leave the case to
2250      * Python).  If both are, then we compare the addresses.
2251      *
2252      * If v and/or w is a primitive cdata, then we convert the cdata(s)
2253      * to regular Python objects and redo the comparison there.
2254      */
2255 
2256     v_is_ptr = !(((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY);
2257     w_is_ptr = CData_Check(w) &&
2258                   !(((CDataObject *)w)->c_type->ct_flags & CT_PRIMITIVE_ANY);
2259 
2260     if (v_is_ptr && w_is_ptr) {
2261         int res;
2262         char *v_cdata = ((CDataObject *)v)->c_data;
2263         char *w_cdata = ((CDataObject *)w)->c_data;
2264 
2265         switch (op) {
2266         case Py_EQ: res = (v_cdata == w_cdata); break;
2267         case Py_NE: res = (v_cdata != w_cdata); break;
2268         case Py_LT: res = (v_cdata <  w_cdata); break;
2269         case Py_LE: res = (v_cdata <= w_cdata); break;
2270         case Py_GT: res = (v_cdata >  w_cdata); break;
2271         case Py_GE: res = (v_cdata >= w_cdata); break;
2272         default: res = -1;
2273         }
2274         pyres = res ? Py_True : Py_False;
2275     }
2276     else if (v_is_ptr || w_is_ptr) {
2277         pyres = Py_NotImplemented;
2278     }
2279     else {
2280         PyObject *aa[2];
2281         int i;
2282 
2283         aa[0] = v; Py_INCREF(v);
2284         aa[1] = w; Py_INCREF(w);
2285         pyres = NULL;
2286 
2287         for (i = 0; i < 2; i++) {
2288             v = aa[i];
2289             if (!CData_Check(v))
2290                 continue;
2291             w = convert_to_object(((CDataObject *)v)->c_data,
2292                                   ((CDataObject *)v)->c_type);
2293             if (w == NULL)
2294                 goto error;
2295             if (CData_Check(w)) {
2296                 Py_DECREF(w);
2297                 PyErr_Format(PyExc_NotImplementedError,
2298                              "cannot use <cdata '%s'> in a comparison",
2299                              ((CDataObject *)v)->c_type->ct_name);
2300                 goto error;
2301             }
2302             aa[i] = w;
2303             Py_DECREF(v);
2304         }
2305         pyres = PyObject_RichCompare(aa[0], aa[1], op);
2306      error:
2307         Py_DECREF(aa[1]);
2308         Py_DECREF(aa[0]);
2309         return pyres;
2310     }
2311 
2312     Py_INCREF(pyres);
2313     return pyres;
2314 }
2315 
cdata_hash(CDataObject * v)2316 static long cdata_hash(CDataObject *v)
2317 {
2318     if (((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY) {
2319         PyObject *vv = convert_to_object(((CDataObject *)v)->c_data,
2320                                          ((CDataObject *)v)->c_type);
2321         if (vv == NULL)
2322             return -1;
2323         if (!CData_Check(vv)) {
2324             long hash = PyObject_Hash(vv);
2325             Py_DECREF(vv);
2326             return hash;
2327         }
2328         Py_DECREF(vv);
2329     }
2330     return _Py_HashPointer(v->c_data);
2331 }
2332 
2333 static Py_ssize_t
cdata_length(CDataObject * cd)2334 cdata_length(CDataObject *cd)
2335 {
2336     if (cd->c_type->ct_flags & CT_ARRAY) {
2337         return get_array_length(cd);
2338     }
2339     PyErr_Format(PyExc_TypeError, "cdata of type '%s' has no len()",
2340                  cd->c_type->ct_name);
2341     return -1;
2342 }
2343 
2344 static char *
_cdata_get_indexed_ptr(CDataObject * cd,PyObject * key)2345 _cdata_get_indexed_ptr(CDataObject *cd, PyObject *key)
2346 {
2347     Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
2348     if (i == -1 && PyErr_Occurred())
2349         return NULL;
2350 
2351     if (cd->c_type->ct_flags & CT_POINTER) {
2352         if (CDataOwn_Check(cd)) {
2353             if (i != 0) {
2354                 PyErr_Format(PyExc_IndexError,
2355                              "cdata '%s' can only be indexed by 0",
2356                              cd->c_type->ct_name);
2357                 return NULL;
2358             }
2359         }
2360         else {
2361             if (cd->c_data == NULL) {
2362                 PyErr_Format(PyExc_RuntimeError,
2363                              "cannot dereference null pointer from cdata '%s'",
2364                              cd->c_type->ct_name);
2365                 return NULL;
2366             }
2367         }
2368     }
2369     else if (cd->c_type->ct_flags & CT_ARRAY) {
2370         if (i < 0) {
2371             PyErr_SetString(PyExc_IndexError,
2372                             "negative index");
2373             return NULL;
2374         }
2375         if (i >= get_array_length(cd)) {
2376             PyErr_Format(PyExc_IndexError,
2377                          "index too large for cdata '%s' (expected %zd < %zd)",
2378                          cd->c_type->ct_name,
2379                          i, get_array_length(cd));
2380             return NULL;
2381         }
2382     }
2383     else {
2384         PyErr_Format(PyExc_TypeError, "cdata of type '%s' cannot be indexed",
2385                      cd->c_type->ct_name);
2386         return NULL;
2387     }
2388     return cd->c_data + i * cd->c_type->ct_itemdescr->ct_size;
2389 }
2390 
2391 static PyObject *
2392 new_array_type(CTypeDescrObject *ctptr, Py_ssize_t length);   /* forward */
2393 
2394 static CTypeDescrObject *
_cdata_getslicearg(CDataObject * cd,PySliceObject * slice,Py_ssize_t bounds[])2395 _cdata_getslicearg(CDataObject *cd, PySliceObject *slice, Py_ssize_t bounds[])
2396 {
2397     Py_ssize_t start, stop;
2398     CTypeDescrObject *ct;
2399 
2400     start = PyInt_AsSsize_t(slice->start);
2401     if (start == -1 && PyErr_Occurred()) {
2402         if (slice->start == Py_None)
2403             PyErr_SetString(PyExc_IndexError, "slice start must be specified");
2404         return NULL;
2405     }
2406     stop = PyInt_AsSsize_t(slice->stop);
2407     if (stop == -1 && PyErr_Occurred()) {
2408         if (slice->stop == Py_None)
2409             PyErr_SetString(PyExc_IndexError, "slice stop must be specified");
2410         return NULL;
2411     }
2412     if (slice->step != Py_None) {
2413         PyErr_SetString(PyExc_IndexError, "slice with step not supported");
2414         return NULL;
2415     }
2416     if (start > stop) {
2417         PyErr_SetString(PyExc_IndexError, "slice start > stop");
2418         return NULL;
2419     }
2420 
2421     ct = cd->c_type;
2422     if (ct->ct_flags & CT_ARRAY) {
2423         if (start < 0) {
2424             PyErr_SetString(PyExc_IndexError,
2425                             "negative index");
2426             return NULL;
2427         }
2428         if (stop > get_array_length(cd)) {
2429             PyErr_Format(PyExc_IndexError,
2430                          "index too large (expected %zd <= %zd)",
2431                          stop, get_array_length(cd));
2432             return NULL;
2433         }
2434         ct = (CTypeDescrObject *)ct->ct_stuff;
2435     }
2436     else if (!(ct->ct_flags & CT_POINTER)) {
2437         PyErr_Format(PyExc_TypeError, "cdata of type '%s' cannot be indexed",
2438                      ct->ct_name);
2439         return NULL;
2440     }
2441 
2442     bounds[0] = start;
2443     bounds[1] = stop - start;
2444     return ct;
2445 }
2446 
2447 static PyObject *
cdata_slice(CDataObject * cd,PySliceObject * slice)2448 cdata_slice(CDataObject *cd, PySliceObject *slice)
2449 {
2450     char *cdata;
2451     Py_ssize_t bounds[2];
2452     CTypeDescrObject *ct = _cdata_getslicearg(cd, slice, bounds);
2453     if (ct == NULL)
2454         return NULL;
2455 
2456     if (ct->ct_stuff == NULL) {
2457         ct->ct_stuff = new_array_type(ct, -1);
2458         if (ct->ct_stuff == NULL)
2459             return NULL;
2460     }
2461     ct = (CTypeDescrObject *)ct->ct_stuff;
2462 
2463     cdata = cd->c_data + ct->ct_itemdescr->ct_size * bounds[0];
2464     return new_sized_cdata(cdata, ct, bounds[1]);
2465 }
2466 
2467 static int
cdata_ass_slice(CDataObject * cd,PySliceObject * slice,PyObject * v)2468 cdata_ass_slice(CDataObject *cd, PySliceObject *slice, PyObject *v)
2469 {
2470     Py_ssize_t bounds[2], i, length, itemsize;
2471     PyObject *it, *item;
2472     PyObject *(*iternext)(PyObject *);
2473     char *cdata;
2474     int err;
2475     CTypeDescrObject *ct = _cdata_getslicearg(cd, slice, bounds);
2476     if (ct == NULL)
2477         return -1;
2478     ct = ct->ct_itemdescr;
2479     itemsize = ct->ct_size;
2480     cdata = cd->c_data + itemsize * bounds[0];
2481     length = bounds[1];
2482 
2483     if (CData_Check(v)) {
2484         CTypeDescrObject *ctv = ((CDataObject *)v)->c_type;
2485         if ((ctv->ct_flags & CT_ARRAY) && (ctv->ct_itemdescr == ct) &&
2486             (get_array_length((CDataObject *)v) == length)) {
2487             /* fast path: copying from exactly the correct type */
2488             memmove(cdata, ((CDataObject *)v)->c_data, itemsize * length);
2489             return 0;
2490         }
2491     }
2492 
2493     /* A fast path for <char[]>[0:N] = b"somestring" or bytearray, which
2494        also adds support for Python 3: otherwise, you get integers while
2495        enumerating the string, and you can't set them to characters :-/
2496     */
2497     if ((ct->ct_flags & CT_PRIMITIVE_CHAR) && itemsize == sizeof(char)) {
2498         char *src;
2499         Py_ssize_t srclen;
2500         if (PyBytes_Check(v)) {
2501             srclen = PyBytes_GET_SIZE(v);
2502             src = PyBytes_AS_STRING(v);
2503         }
2504         else if (PyByteArray_Check(v)) {
2505             srclen = PyByteArray_GET_SIZE(v);
2506             src = PyByteArray_AS_STRING(v);
2507         }
2508         else
2509             goto other_types;
2510 
2511         if (srclen != length) {
2512             PyErr_Format(PyExc_ValueError,
2513                          "need a string of length %zd, got %zd",
2514                          length, srclen);
2515             return -1;
2516         }
2517         memcpy(cdata, src, length);
2518         return 0;
2519     }
2520    other_types:
2521 
2522     it = PyObject_GetIter(v);
2523     if (it == NULL)
2524         return -1;
2525     iternext = *it->ob_type->tp_iternext;
2526 
2527     for (i = 0; i < length; i++) {
2528         item = iternext(it);
2529         if (item == NULL) {
2530             if (!PyErr_Occurred())
2531                 PyErr_Format(PyExc_ValueError,
2532                              "need %zd values to unpack, got %zd",
2533                              length, i);
2534             goto error;
2535         }
2536         err = convert_from_object(cdata, ct, item);
2537         Py_DECREF(item);
2538         if (err < 0)
2539             goto error;
2540 
2541         cdata += itemsize;
2542     }
2543     item = iternext(it);
2544     if (item != NULL) {
2545         Py_DECREF(item);
2546         PyErr_Format(PyExc_ValueError,
2547                      "got more than %zd values to unpack", length);
2548     }
2549  error:
2550     Py_DECREF(it);
2551     return PyErr_Occurred() ? -1 : 0;
2552 }
2553 
2554 static PyObject *
cdataowning_subscript(CDataObject * cd,PyObject * key)2555 cdataowning_subscript(CDataObject *cd, PyObject *key)
2556 {
2557     char *c;
2558     if (PySlice_Check(key))
2559         return cdata_slice(cd, (PySliceObject *)key);
2560 
2561     c = _cdata_get_indexed_ptr(cd, key);
2562     /* use 'mp_subscript' instead of 'sq_item' because we don't want
2563        negative indexes to be corrected automatically */
2564     if (c == NULL && PyErr_Occurred())
2565         return NULL;
2566 
2567     if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) {
2568         PyObject *res = ((CDataObject_own_structptr *)cd)->structobj;
2569         Py_INCREF(res);
2570         return res;
2571     }
2572     else {
2573         return convert_to_object(c, cd->c_type->ct_itemdescr);
2574     }
2575 }
2576 
2577 static PyObject *
cdata_subscript(CDataObject * cd,PyObject * key)2578 cdata_subscript(CDataObject *cd, PyObject *key)
2579 {
2580     char *c;
2581     if (PySlice_Check(key))
2582         return cdata_slice(cd, (PySliceObject *)key);
2583 
2584     c = _cdata_get_indexed_ptr(cd, key);
2585     /* use 'mp_subscript' instead of 'sq_item' because we don't want
2586        negative indexes to be corrected automatically */
2587     if (c == NULL && PyErr_Occurred())
2588         return NULL;
2589     return convert_to_object(c, cd->c_type->ct_itemdescr);
2590 }
2591 
2592 static int
cdata_ass_sub(CDataObject * cd,PyObject * key,PyObject * v)2593 cdata_ass_sub(CDataObject *cd, PyObject *key, PyObject *v)
2594 {
2595     char *c;
2596     CTypeDescrObject *ctitem;
2597     if (PySlice_Check(key))
2598         return cdata_ass_slice(cd, (PySliceObject *)key, v);
2599 
2600     c = _cdata_get_indexed_ptr(cd, key);
2601     ctitem = cd->c_type->ct_itemdescr;
2602     /* use 'mp_ass_subscript' instead of 'sq_ass_item' because we don't want
2603        negative indexes to be corrected automatically */
2604     if (c == NULL && PyErr_Occurred())
2605         return -1;
2606     if (v == NULL) {
2607         PyErr_SetString(PyExc_TypeError,
2608                         "'del x[n]' not supported for cdata objects");
2609         return -1;
2610     }
2611     return convert_from_object(c, ctitem, v);
2612 }
2613 
2614 static PyObject *
_cdata_add_or_sub(PyObject * v,PyObject * w,int sign)2615 _cdata_add_or_sub(PyObject *v, PyObject *w, int sign)
2616 {
2617     Py_ssize_t i, itemsize;
2618     CDataObject *cd;
2619     CTypeDescrObject *ctptr;
2620 
2621     if (!CData_Check(v)) {
2622         PyObject *swap;
2623         assert(CData_Check(w));
2624         if (sign != 1)
2625             goto not_implemented;
2626         swap = v;
2627         v = w;
2628         w = swap;
2629     }
2630 
2631     i = PyNumber_AsSsize_t(w, PyExc_OverflowError);
2632     if (i == -1 && PyErr_Occurred())
2633         return NULL;
2634     i *= sign;
2635 
2636     cd = (CDataObject *)v;
2637     if (cd->c_type->ct_flags & CT_POINTER)
2638         ctptr = cd->c_type;
2639     else if (cd->c_type->ct_flags & CT_ARRAY) {
2640         ctptr = (CTypeDescrObject *)cd->c_type->ct_stuff;
2641     }
2642     else {
2643         PyErr_Format(PyExc_TypeError, "cannot add a cdata '%s' and a number",
2644                      cd->c_type->ct_name);
2645         return NULL;
2646     }
2647     itemsize = ctptr->ct_itemdescr->ct_size;
2648     if (itemsize < 0) {
2649         if (ctptr->ct_flags & CT_IS_VOID_PTR) {
2650             itemsize = 1;
2651         }
2652         else {
2653             PyErr_Format(PyExc_TypeError,
2654                          "ctype '%s' points to items of unknown size",
2655                          cd->c_type->ct_name);
2656             return NULL;
2657         }
2658     }
2659     return new_simple_cdata(cd->c_data + i * itemsize, ctptr);
2660 
2661  not_implemented:
2662     Py_INCREF(Py_NotImplemented);
2663     return Py_NotImplemented;
2664 }
2665 
2666 static PyObject *
cdata_add(PyObject * v,PyObject * w)2667 cdata_add(PyObject *v, PyObject *w)
2668 {
2669     return _cdata_add_or_sub(v, w, +1);
2670 }
2671 
2672 static PyObject *
cdata_sub(PyObject * v,PyObject * w)2673 cdata_sub(PyObject *v, PyObject *w)
2674 {
2675     if (CData_Check(v) && CData_Check(w)) {
2676         CDataObject *cdv = (CDataObject *)v;
2677         CDataObject *cdw = (CDataObject *)w;
2678         CTypeDescrObject *ct = cdw->c_type;
2679         Py_ssize_t diff, itemsize;
2680 
2681         if (ct->ct_flags & CT_ARRAY)     /* ptr_to_T - array_of_T: ok */
2682             ct = (CTypeDescrObject *)ct->ct_stuff;
2683 
2684         if (ct != cdv->c_type || !(ct->ct_flags & CT_POINTER) ||
2685                 (ct->ct_itemdescr->ct_size <= 0 &&
2686                  !(ct->ct_flags & CT_IS_VOID_PTR))) {
2687             PyErr_Format(PyExc_TypeError,
2688                          "cannot subtract cdata '%s' and cdata '%s'",
2689                          cdv->c_type->ct_name, ct->ct_name);
2690             return NULL;
2691         }
2692         itemsize = ct->ct_itemdescr->ct_size;
2693         diff = cdv->c_data - cdw->c_data;
2694         if (itemsize > 1) {
2695             if (diff % itemsize) {
2696                 PyErr_SetString(PyExc_ValueError,
2697                      "pointer subtraction: the distance between the two "
2698                      "pointers is not a multiple of the item size");
2699                 return NULL;
2700             }
2701             diff = diff / itemsize;
2702         }
2703 #if PY_MAJOR_VERSION < 3
2704         return PyInt_FromSsize_t(diff);
2705 #else
2706         return PyLong_FromSsize_t(diff);
2707 #endif
2708     }
2709 
2710     return _cdata_add_or_sub(v, w, -1);
2711 }
2712 
2713 static void
_cdata_attr_errmsg(char * errmsg,CDataObject * cd,PyObject * attr)2714 _cdata_attr_errmsg(char *errmsg, CDataObject *cd, PyObject *attr)
2715 {
2716     const char *text;
2717     if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2718         return;
2719     PyErr_Clear();
2720     text = PyText_AsUTF8(attr);
2721     if (text == NULL)
2722         return;
2723     PyErr_Format(PyExc_AttributeError, errmsg, cd->c_type->ct_name, text);
2724 }
2725 
2726 static PyObject *
cdata_getattro(CDataObject * cd,PyObject * attr)2727 cdata_getattro(CDataObject *cd, PyObject *attr)
2728 {
2729     CFieldObject *cf;
2730     CTypeDescrObject *ct = cd->c_type;
2731     char *errmsg = "cdata '%s' has no attribute '%s'";
2732     PyObject *x;
2733 
2734     if (ct->ct_flags & CT_POINTER)
2735         ct = ct->ct_itemdescr;
2736 
2737     if (ct->ct_flags & (CT_STRUCT|CT_UNION)) {
2738         switch (force_lazy_struct(ct)) {
2739         case 1:
2740             cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, attr);
2741             if (cf != NULL) {
2742                 /* read the field 'cf' */
2743                 char *data = cd->c_data + cf->cf_offset;
2744                 Py_ssize_t array_len, size;
2745 
2746                 if (cf->cf_bitshift == BS_REGULAR) {
2747                     return convert_to_object(data, cf->cf_type);
2748                 }
2749                 else if (cf->cf_bitshift != BS_EMPTY_ARRAY) {
2750                     return convert_to_object_bitfield(data, cf);
2751                 }
2752 
2753                 /* variable-length array: */
2754                 /* if reading variable length array from variable length
2755                    struct, calculate array type from allocated length */
2756                 size = _cdata_var_byte_size(cd) - cf->cf_offset;
2757                 if (size >= 0) {
2758                     array_len = size / cf->cf_type->ct_itemdescr->ct_size;
2759                     return new_sized_cdata(data, cf->cf_type, array_len);
2760                 }
2761                 return new_simple_cdata(data,
2762                     (CTypeDescrObject *)cf->cf_type->ct_stuff);
2763             }
2764             errmsg = "cdata '%s' has no field '%s'";
2765             break;
2766         case -1:
2767             return NULL;
2768         default:
2769             errmsg = "cdata '%s' points to an opaque type: cannot read fields";
2770             break;
2771         }
2772     }
2773     x = PyObject_GenericGetAttr((PyObject *)cd, attr);
2774     if (x == NULL)
2775         _cdata_attr_errmsg(errmsg, cd, attr);
2776     return x;
2777 }
2778 
2779 static int
cdata_setattro(CDataObject * cd,PyObject * attr,PyObject * value)2780 cdata_setattro(CDataObject *cd, PyObject *attr, PyObject *value)
2781 {
2782     CFieldObject *cf;
2783     CTypeDescrObject *ct = cd->c_type;
2784     char *errmsg = "cdata '%s' has no attribute '%s'";
2785     int x;
2786 
2787     if (ct->ct_flags & CT_POINTER)
2788         ct = ct->ct_itemdescr;
2789 
2790     if (ct->ct_flags & (CT_STRUCT|CT_UNION)) {
2791         switch (force_lazy_struct(ct)) {
2792         case 1:
2793             cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, attr);
2794             if (cf != NULL) {
2795                 /* write the field 'cf' */
2796                 if (value != NULL) {
2797                     return convert_field_from_object(cd->c_data, cf, value);
2798                 }
2799                 else {
2800                     PyErr_SetString(PyExc_AttributeError,
2801                                     "cannot delete struct field");
2802                     return -1;
2803                 }
2804             }
2805             errmsg = "cdata '%s' has no field '%s'";
2806             break;
2807         case -1:
2808             return -1;
2809         default:
2810             errmsg = "cdata '%s' points to an opaque type: cannot write fields";
2811             break;
2812         }
2813     }
2814     x = PyObject_GenericSetAttr((PyObject *)cd, attr, value);
2815     if (x < 0)
2816         _cdata_attr_errmsg(errmsg, cd, attr);
2817     return x;
2818 }
2819 
2820 static PyObject *
2821 convert_struct_to_owning_object(char *data, CTypeDescrObject *ct); /*forward*/
2822 
2823 static cif_description_t *
2824 fb_prepare_cif(PyObject *fargs, CTypeDescrObject *, ffi_abi);      /*forward*/
2825 
2826 static PyObject *new_primitive_type(const char *name);             /*forward*/
2827 
_get_ct_int(void)2828 static CTypeDescrObject *_get_ct_int(void)
2829 {
2830     static CTypeDescrObject *ct_int = NULL;
2831     if (ct_int == NULL) {
2832         ct_int = (CTypeDescrObject *)new_primitive_type("int");
2833     }
2834     return ct_int;
2835 }
2836 
2837 static Py_ssize_t
_prepare_pointer_call_argument(CTypeDescrObject * ctptr,PyObject * init,char ** output_data)2838 _prepare_pointer_call_argument(CTypeDescrObject *ctptr, PyObject *init,
2839                                char **output_data)
2840 {
2841     /* 'ctptr' is here a pointer type 'ITEM *'.  Accept as argument an
2842        initializer for an array 'ITEM[]'.  This includes the case of
2843        passing a Python byte string to a 'char *' argument.
2844 
2845        This function returns -1 if an error occurred,
2846        0 if conversion succeeded (into *output_data),
2847        or N > 0 if conversion would require N bytes of storage.
2848     */
2849     Py_ssize_t length, datasize;
2850     CTypeDescrObject *ctitem;
2851 
2852     if (CData_Check(init))
2853         goto convert_default;
2854 
2855     ctitem = ctptr->ct_itemdescr;
2856     /* XXX some code duplication, how to avoid it? */
2857     if (PyBytes_Check(init)) {
2858         /* from a string: just returning the string here is fine.
2859            We assume that the C code won't modify the 'char *' data. */
2860         if ((ctptr->ct_flags & CT_IS_VOIDCHAR_PTR) ||
2861             ((ctitem->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED))
2862              && (ctitem->ct_size == sizeof(char)))) {
2863 #if defined(CFFI_MEM_DEBUG) || defined(CFFI_MEM_LEAK)
2864             length = PyBytes_GET_SIZE(init) + 1;
2865 #else
2866             *output_data = PyBytes_AS_STRING(init);
2867             if (ctitem->ct_flags & CT_IS_BOOL)
2868                 if (must_be_array_of_zero_or_one(*output_data,
2869                                                  PyBytes_GET_SIZE(init)) < 0)
2870                     return -1;
2871             return 0;
2872 #endif
2873         }
2874         else
2875             goto convert_default;
2876     }
2877     else if (PyList_Check(init) || PyTuple_Check(init)) {
2878         length = PySequence_Fast_GET_SIZE(init);
2879     }
2880     else if (PyUnicode_Check(init)) {
2881         /* from a unicode, we add the null terminator */
2882         if (ctitem->ct_size == 2)
2883             length = _my_PyUnicode_SizeAsChar16(init);
2884         else
2885             length = _my_PyUnicode_SizeAsChar32(init);
2886         length += 1;
2887     }
2888     else if ((ctitem->ct_flags & CT_IS_FILE) && PyFile_Check(init)) {
2889         *output_data = (char *)PyFile_AsFile(init);
2890         if (*output_data == NULL && PyErr_Occurred())
2891             return -1;
2892         return 0;
2893     }
2894     else {
2895         /* refuse to receive just an integer (and interpret it
2896            as the array size) */
2897         goto convert_default;
2898     }
2899 
2900     if (ctitem->ct_size <= 0)
2901         goto convert_default;
2902     datasize = MUL_WRAPAROUND(length, ctitem->ct_size);
2903     if ((datasize / ctitem->ct_size) != length) {
2904         PyErr_SetString(PyExc_OverflowError,
2905                         "array size would overflow a Py_ssize_t");
2906         return -1;
2907     }
2908     if (datasize <= 0)
2909         datasize = 1;
2910     return datasize;
2911 
2912  convert_default:
2913     return convert_from_object((char *)output_data, ctptr, init);
2914 }
2915 
2916 static PyObject*
cdata_call(CDataObject * cd,PyObject * args,PyObject * kwds)2917 cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds)
2918 {
2919     char *buffer;
2920     void** buffer_array;
2921     cif_description_t *cif_descr;
2922     Py_ssize_t i, nargs, nargs_declared;
2923     PyObject *signature, *res = NULL, *fvarargs;
2924     CTypeDescrObject *fresult;
2925     char *resultdata;
2926     char *errormsg;
2927 
2928     if (!(cd->c_type->ct_flags & CT_FUNCTIONPTR)) {
2929         PyErr_Format(PyExc_TypeError, "cdata '%s' is not callable",
2930                      cd->c_type->ct_name);
2931         return NULL;
2932     }
2933     if (kwds != NULL && PyDict_Size(kwds) != 0) {
2934         PyErr_SetString(PyExc_TypeError,
2935                 "a cdata function cannot be called with keyword arguments");
2936         return NULL;
2937     }
2938     signature = cd->c_type->ct_stuff;
2939     nargs = PyTuple_Size(args);
2940     if (nargs < 0)
2941         return NULL;
2942     nargs_declared = PyTuple_GET_SIZE(signature) - 2;
2943     fresult = (CTypeDescrObject *)PyTuple_GET_ITEM(signature, 1);
2944     fvarargs = NULL;
2945     buffer = NULL;
2946 
2947     cif_descr = (cif_description_t *)cd->c_type->ct_extra;
2948 
2949     if (cif_descr != NULL) {
2950         /* regular case: this function does not take '...' arguments */
2951         if (nargs != nargs_declared) {
2952             errormsg = "'%s' expects %zd arguments, got %zd";
2953           bad_number_of_arguments:
2954             PyErr_Format(PyExc_TypeError, errormsg,
2955                          cd->c_type->ct_name, nargs_declared, nargs);
2956             goto error;
2957         }
2958     }
2959     else {
2960         /* call of a variadic function */
2961         ffi_abi fabi;
2962         if (nargs < nargs_declared) {
2963             errormsg = "'%s' expects at least %zd arguments, got %zd";
2964             goto bad_number_of_arguments;
2965         }
2966         fvarargs = PyTuple_New(nargs);
2967         if (fvarargs == NULL)
2968             goto error;
2969         for (i = 0; i < nargs_declared; i++) {
2970             PyObject *o = PyTuple_GET_ITEM(signature, 2 + i);
2971             Py_INCREF(o);
2972             PyTuple_SET_ITEM(fvarargs, i, o);
2973         }
2974         for (i = nargs_declared; i < nargs; i++) {
2975             PyObject *obj = PyTuple_GET_ITEM(args, i);
2976             CTypeDescrObject *ct;
2977 
2978             if (CData_Check(obj)) {
2979                 ct = ((CDataObject *)obj)->c_type;
2980                 if (ct->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_UNSIGNED |
2981                                     CT_PRIMITIVE_SIGNED)) {
2982                     if (ct->ct_size < (Py_ssize_t)sizeof(int)) {
2983                         ct = _get_ct_int();
2984                         if (ct == NULL)
2985                             goto error;
2986                     }
2987                 }
2988                 else if (ct->ct_flags & CT_ARRAY) {
2989                     ct = (CTypeDescrObject *)ct->ct_stuff;
2990                 }
2991                 Py_INCREF(ct);
2992             }
2993             else {
2994                 PyErr_Format(PyExc_TypeError,
2995                              "argument %zd passed in the variadic part "
2996                              "needs to be a cdata object (got %.200s)",
2997                              i + 1, Py_TYPE(obj)->tp_name);
2998                 goto error;
2999             }
3000             PyTuple_SET_ITEM(fvarargs, i, (PyObject *)ct);
3001         }
3002 #if PY_MAJOR_VERSION < 3
3003         fabi = PyInt_AS_LONG(PyTuple_GET_ITEM(signature, 0));
3004 #else
3005         fabi = PyLong_AS_LONG(PyTuple_GET_ITEM(signature, 0));
3006 #endif
3007         cif_descr = fb_prepare_cif(fvarargs, fresult, fabi);
3008         if (cif_descr == NULL)
3009             goto error;
3010     }
3011 
3012     buffer = PyObject_Malloc(cif_descr->exchange_size);
3013     if (buffer == NULL) {
3014         PyErr_NoMemory();
3015         goto error;
3016     }
3017 
3018     buffer_array = (void **)buffer;
3019 
3020     for (i=0; i<nargs; i++) {
3021         CTypeDescrObject *argtype;
3022         char *data = buffer + cif_descr->exchange_offset_arg[1 + i];
3023         PyObject *obj = PyTuple_GET_ITEM(args, i);
3024 
3025         buffer_array[i] = data;
3026 
3027         if (i < nargs_declared)
3028             argtype = (CTypeDescrObject *)PyTuple_GET_ITEM(signature, 2 + i);
3029         else
3030             argtype = (CTypeDescrObject *)PyTuple_GET_ITEM(fvarargs, i);
3031 
3032         if (argtype->ct_flags & CT_POINTER) {
3033             char *tmpbuf;
3034             Py_ssize_t datasize = _prepare_pointer_call_argument(
3035                                             argtype, obj, (char **)data);
3036             if (datasize == 0)
3037                 ;    /* successfully filled '*data' */
3038             else if (datasize < 0)
3039                 goto error;
3040             else {
3041                 tmpbuf = alloca(datasize);
3042                 memset(tmpbuf, 0, datasize);
3043                 *(char **)data = tmpbuf;
3044                 if (convert_array_from_object(tmpbuf, argtype, obj) < 0)
3045                     goto error;
3046             }
3047         }
3048         else if (convert_from_object(data, argtype, obj) < 0)
3049             goto error;
3050     }
3051 
3052     resultdata = buffer + cif_descr->exchange_offset_arg[0];
3053     /*READ(cd->c_data, sizeof(void(*)(void)))*/
3054 
3055     Py_BEGIN_ALLOW_THREADS
3056     restore_errno();
3057     ffi_call(&cif_descr->cif, (void (*)(void))(cd->c_data),
3058              resultdata, buffer_array);
3059     save_errno();
3060     Py_END_ALLOW_THREADS
3061 
3062     if (fresult->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_SIGNED |
3063                              CT_PRIMITIVE_UNSIGNED)) {
3064 #ifdef WORDS_BIGENDIAN
3065         /* For results of precisely these types, libffi has a strange
3066            rule that they will be returned as a whole 'ffi_arg' if they
3067            are smaller.  The difference only matters on big-endian. */
3068         if (fresult->ct_size < sizeof(ffi_arg))
3069             resultdata += (sizeof(ffi_arg) - fresult->ct_size);
3070 #endif
3071         res = convert_to_object(resultdata, fresult);
3072     }
3073     else if (fresult->ct_flags & CT_VOID) {
3074         res = Py_None;
3075         Py_INCREF(res);
3076     }
3077     else if (fresult->ct_flags & CT_STRUCT) {
3078         res = convert_struct_to_owning_object(resultdata, fresult);
3079     }
3080     else {
3081         res = convert_to_object(resultdata, fresult);
3082     }
3083     /* fall-through */
3084 
3085  error:
3086     if (buffer)
3087         PyObject_Free(buffer);
3088     if (fvarargs != NULL) {
3089         Py_DECREF(fvarargs);
3090         if (cif_descr != NULL)  /* but only if fvarargs != NULL, if variadic */
3091             PyObject_Free(cif_descr);
3092     }
3093     return res;
3094 }
3095 
cdata_dir(PyObject * cd,PyObject * noarg)3096 static PyObject *cdata_dir(PyObject *cd, PyObject *noarg)
3097 {
3098     CTypeDescrObject *ct = ((CDataObject *)cd)->c_type;
3099 
3100     /* replace the type 'pointer-to-t' with just 't' */
3101     if (ct->ct_flags & CT_POINTER) {
3102         ct = ct->ct_itemdescr;
3103     }
3104     if ((ct->ct_flags & (CT_STRUCT | CT_UNION)) &&
3105         !(ct->ct_flags & CT_IS_OPAQUE)) {
3106 
3107         /* for non-opaque structs or unions */
3108         if (force_lazy_struct(ct) < 0)
3109             return NULL;
3110         return PyDict_Keys(ct->ct_stuff);
3111     }
3112     else {
3113         return PyList_New(0);   /* empty list for the other cases */
3114     }
3115 }
3116 
cdata_complex(PyObject * cd_,PyObject * noarg)3117 static PyObject *cdata_complex(PyObject *cd_, PyObject *noarg)
3118 {
3119     CDataObject *cd = (CDataObject *)cd_;
3120 
3121     if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
3122         Py_complex value = read_raw_complex_data(cd->c_data, cd->c_type->ct_size);
3123         PyObject *op = PyComplex_FromCComplex(value);
3124         return op;
3125     }
3126     /* <cdata 'float'> or <cdata 'int'> cannot be directly converted by
3127        calling complex(), just like <cdata 'int'> cannot be directly
3128        converted by calling float() */
3129 
3130     PyErr_Format(PyExc_TypeError, "complex() not supported on cdata '%s'",
3131                  cd->c_type->ct_name);
3132     return NULL;
3133 }
3134 
explicit_release_case(PyObject * cd)3135 static int explicit_release_case(PyObject *cd)
3136 {
3137     CTypeDescrObject *ct = ((CDataObject *)cd)->c_type;
3138     if (Py_TYPE(cd) == &CDataOwning_Type) {
3139         if ((ct->ct_flags & (CT_POINTER | CT_ARRAY)) != 0)   /* ffi.new() */
3140             return 0;
3141     }
3142     else if (Py_TYPE(cd) == &CDataOwningGC_Type) {
3143         if (ct->ct_flags & CT_ARRAY)      /* ffi.from_buffer() */
3144             return 1;
3145     }
3146     else if (Py_TYPE(cd) == &CDataGCP_Type) {
3147         return 2;    /* ffi.gc() */
3148     }
3149     PyErr_SetString(PyExc_ValueError,
3150         "only 'cdata' object from ffi.new(), ffi.gc(), ffi.from_buffer() "
3151         "or ffi.new_allocator()() can be used with the 'with' keyword or "
3152         "ffi.release()");
3153     return -1;
3154 }
3155 
cdata_enter(PyObject * cd,PyObject * noarg)3156 static PyObject *cdata_enter(PyObject *cd, PyObject *noarg)
3157 {
3158     if (explicit_release_case(cd) < 0)   /* only to check the ctype */
3159         return NULL;
3160     Py_INCREF(cd);
3161     return cd;
3162 }
3163 
cdata_exit(PyObject * cd,PyObject * args)3164 static PyObject *cdata_exit(PyObject *cd, PyObject *args)
3165 {
3166     /* 'args' ignored */
3167     CTypeDescrObject *ct;
3168     Py_buffer *view;
3169     switch (explicit_release_case(cd))
3170     {
3171         case 0:    /* ffi.new() */
3172             /* no effect on CPython: raw memory is allocated with the
3173                same malloc() as the object itself, so it can't be
3174                released independently.  If we use a custom allocator,
3175                then it's implemented with ffi.gc(). */
3176             ct = ((CDataObject *)cd)->c_type;
3177             if (ct->ct_flags & CT_IS_PTR_TO_OWNED) {
3178                 PyObject *x = ((CDataObject_own_structptr *)cd)->structobj;
3179                 if (Py_TYPE(x) == &CDataGCP_Type) {
3180                     /* this is a special case for
3181                        ffi.new_allocator()("struct-or-union") */
3182                     cdatagcp_finalize((CDataObject_gcp *)x);
3183                 }
3184             }
3185             break;
3186 
3187         case 1:    /* ffi.from_buffer() */
3188             view = ((CDataObject_owngc_frombuf *)cd)->bufferview;
3189             PyBuffer_Release(view);
3190             break;
3191 
3192         case 2:    /* ffi.gc() or ffi.new_allocator()("not-struct-nor-union") */
3193             /* call the destructor immediately */
3194             cdatagcp_finalize((CDataObject_gcp *)cd);
3195             break;
3196 
3197         default:
3198             return NULL;
3199     }
3200     Py_INCREF(Py_None);
3201     return Py_None;
3202 }
3203 
3204 static PyObject *cdata_iter(CDataObject *);
3205 
3206 static PyNumberMethods CData_as_number = {
3207     (binaryfunc)cdata_add,      /*nb_add*/
3208     (binaryfunc)cdata_sub,      /*nb_subtract*/
3209     0,                          /*nb_multiply*/
3210 #if PY_MAJOR_VERSION < 3
3211     0,                          /*nb_divide*/
3212 #endif
3213     0,                          /*nb_remainder*/
3214     0,                          /*nb_divmod*/
3215     0,                          /*nb_power*/
3216     0,                          /*nb_negative*/
3217     0,                          /*nb_positive*/
3218     0,                          /*nb_absolute*/
3219     (inquiry)cdata_nonzero,     /*nb_nonzero*/
3220     0,                          /*nb_invert*/
3221     0,                          /*nb_lshift*/
3222     0,                          /*nb_rshift*/
3223     0,                          /*nb_and*/
3224     0,                          /*nb_xor*/
3225     0,                          /*nb_or*/
3226 #if PY_MAJOR_VERSION < 3
3227     0,                          /*nb_coerce*/
3228 #endif
3229     (unaryfunc)cdata_int,       /*nb_int*/
3230 #if PY_MAJOR_VERSION < 3
3231     (unaryfunc)cdata_long,      /*nb_long*/
3232 #else
3233     0,
3234 #endif
3235     (unaryfunc)cdata_float,     /*nb_float*/
3236     0,                          /*nb_oct*/
3237     0,                          /*nb_hex*/
3238 };
3239 
3240 static PyMappingMethods CData_as_mapping = {
3241     (lenfunc)cdata_length, /*mp_length*/
3242     (binaryfunc)cdata_subscript, /*mp_subscript*/
3243     (objobjargproc)cdata_ass_sub, /*mp_ass_subscript*/
3244 };
3245 
3246 static PyMappingMethods CDataOwn_as_mapping = {
3247     (lenfunc)cdata_length, /*mp_length*/
3248     (binaryfunc)cdataowning_subscript, /*mp_subscript*/
3249     (objobjargproc)cdata_ass_sub, /*mp_ass_subscript*/
3250 };
3251 
3252 static PyMethodDef cdata_methods[] = {
3253     {"__dir__",     cdata_dir,      METH_NOARGS},
3254     {"__complex__", cdata_complex,  METH_NOARGS},
3255     {"__enter__",   cdata_enter,    METH_NOARGS},
3256     {"__exit__",    cdata_exit,     METH_VARARGS},
3257     {NULL,          NULL}           /* sentinel */
3258 };
3259 
3260 static PyTypeObject CData_Type = {
3261     PyVarObject_HEAD_INIT(NULL, 0)
3262     "_cffi_backend.CData",
3263     sizeof(CDataObject),
3264     0,
3265     (destructor)cdata_dealloc,                  /* tp_dealloc */
3266     0,                                          /* tp_print */
3267     0,                                          /* tp_getattr */
3268     0,                                          /* tp_setattr */
3269     0,                                          /* tp_compare */
3270     (reprfunc)cdata_repr,                       /* tp_repr */
3271     &CData_as_number,                           /* tp_as_number */
3272     0,                                          /* tp_as_sequence */
3273     &CData_as_mapping,                          /* tp_as_mapping */
3274     (hashfunc)cdata_hash,                       /* tp_hash */
3275     (ternaryfunc)cdata_call,                    /* tp_call */
3276     0,                                          /* tp_str */
3277     (getattrofunc)cdata_getattro,               /* tp_getattro */
3278     (setattrofunc)cdata_setattro,               /* tp_setattro */
3279     0,                                          /* tp_as_buffer */
3280     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
3281     0,                                          /* tp_doc */
3282     0,                                          /* tp_traverse */
3283     0,                                          /* tp_clear */
3284     cdata_richcompare,                          /* tp_richcompare */
3285     offsetof(CDataObject, c_weakreflist),       /* tp_weaklistoffset */
3286     (getiterfunc)cdata_iter,                    /* tp_iter */
3287     0,                                          /* tp_iternext */
3288     cdata_methods,                              /* tp_methods */
3289     0,                                          /* tp_members */
3290     0,                                          /* tp_getset */
3291     0,                                          /* tp_base */
3292     0,                                          /* tp_dict */
3293     0,                                          /* tp_descr_get */
3294     0,                                          /* tp_descr_set */
3295     0,                                          /* tp_dictoffset */
3296     0,                                          /* tp_init */
3297     0,                                          /* tp_alloc */
3298     0,                                          /* tp_new */
3299     PyObject_Del,                               /* tp_free */
3300 };
3301 
3302 static PyTypeObject CDataOwning_Type = {
3303     PyVarObject_HEAD_INIT(NULL, 0)
3304     "_cffi_backend.CDataOwn",
3305     sizeof(CDataObject),
3306     0,
3307     (destructor)cdataowning_dealloc,            /* tp_dealloc */
3308     0,                                          /* tp_print */
3309     0,                                          /* tp_getattr */
3310     0,                                          /* tp_setattr */
3311     0,                                          /* tp_compare */
3312     (reprfunc)cdataowning_repr,                 /* tp_repr */
3313     0,  /* inherited */                         /* tp_as_number */
3314     0,                                          /* tp_as_sequence */
3315     &CDataOwn_as_mapping,                       /* tp_as_mapping */
3316     0,  /* inherited */                         /* tp_hash */
3317     0,  /* inherited */                         /* tp_call */
3318     0,                                          /* tp_str */
3319     0,  /* inherited */                         /* tp_getattro */
3320     0,  /* inherited */                         /* tp_setattro */
3321     0,                                          /* tp_as_buffer */
3322     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
3323     0,                                          /* tp_doc */
3324     0,                                          /* tp_traverse */
3325     0,                                          /* tp_clear */
3326     0,  /* inherited */                         /* tp_richcompare */
3327     0,  /* inherited */                         /* tp_weaklistoffset */
3328     0,  /* inherited */                         /* tp_iter */
3329     0,                                          /* tp_iternext */
3330     0,  /* inherited */                         /* tp_methods */
3331     0,                                          /* tp_members */
3332     0,                                          /* tp_getset */
3333     &CData_Type,                                /* tp_base */
3334     0,                                          /* tp_dict */
3335     0,                                          /* tp_descr_get */
3336     0,                                          /* tp_descr_set */
3337     0,                                          /* tp_dictoffset */
3338     0,                                          /* tp_init */
3339     0,                                          /* tp_alloc */
3340     0,                                          /* tp_new */
3341     free,                                       /* tp_free */
3342 };
3343 
3344 static PyTypeObject CDataOwningGC_Type = {
3345     PyVarObject_HEAD_INIT(NULL, 0)
3346     "_cffi_backend.CDataOwnGC",
3347     sizeof(CDataObject_owngc_frombuf),
3348     0,
3349     (destructor)cdataowninggc_dealloc,          /* tp_dealloc */
3350     0,                                          /* tp_print */
3351     0,                                          /* tp_getattr */
3352     0,                                          /* tp_setattr */
3353     0,                                          /* tp_compare */
3354     (reprfunc)cdataowninggc_repr,               /* tp_repr */
3355     0,  /* inherited */                         /* tp_as_number */
3356     0,                                          /* tp_as_sequence */
3357     0,  /* inherited */                         /* tp_as_mapping */
3358     0,  /* inherited */                         /* tp_hash */
3359     0,  /* inherited */                         /* tp_call */
3360     0,                                          /* tp_str */
3361     0,  /* inherited */                         /* tp_getattro */
3362     0,  /* inherited */                         /* tp_setattro */
3363     0,                                          /* tp_as_buffer */
3364     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES  /* tp_flags */
3365                        | Py_TPFLAGS_HAVE_GC,
3366     0,                                          /* tp_doc */
3367     (traverseproc)cdataowninggc_traverse,       /* tp_traverse */
3368     (inquiry)cdataowninggc_clear,               /* tp_clear */
3369     0,  /* inherited */                         /* tp_richcompare */
3370     0,  /* inherited */                         /* tp_weaklistoffset */
3371     0,  /* inherited */                         /* tp_iter */
3372     0,                                          /* tp_iternext */
3373     0,  /* inherited */                         /* tp_methods */
3374     0,                                          /* tp_members */
3375     0,                                          /* tp_getset */
3376     &CDataOwning_Type,                          /* tp_base */
3377     0,                                          /* tp_dict */
3378     0,                                          /* tp_descr_get */
3379     0,                                          /* tp_descr_set */
3380     0,                                          /* tp_dictoffset */
3381     0,                                          /* tp_init */
3382     0,                                          /* tp_alloc */
3383     0,                                          /* tp_new */
3384     PyObject_GC_Del,                            /* tp_free */
3385 };
3386 
3387 static PyTypeObject CDataGCP_Type = {
3388     PyVarObject_HEAD_INIT(NULL, 0)
3389     "_cffi_backend.CDataGCP",
3390     sizeof(CDataObject_gcp),
3391     0,
3392     (destructor)cdatagcp_dealloc,               /* tp_dealloc */
3393     0,                                          /* tp_print */
3394     0,                                          /* tp_getattr */
3395     0,                                          /* tp_setattr */
3396     0,                                          /* tp_compare */
3397     0,  /* inherited */                         /* tp_repr */
3398     0,  /* inherited */                         /* tp_as_number */
3399     0,                                          /* tp_as_sequence */
3400     0,  /* inherited */                         /* tp_as_mapping */
3401     0,  /* inherited */                         /* tp_hash */
3402     0,  /* inherited */                         /* tp_call */
3403     0,                                          /* tp_str */
3404     0,  /* inherited */                         /* tp_getattro */
3405     0,  /* inherited */                         /* tp_setattro */
3406     0,                                          /* tp_as_buffer */
3407     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES  /* tp_flags */
3408 #ifdef Py_TPFLAGS_HAVE_FINALIZE
3409                        | Py_TPFLAGS_HAVE_FINALIZE
3410 #endif
3411                        | Py_TPFLAGS_HAVE_GC,
3412     0,                                          /* tp_doc */
3413     (traverseproc)cdatagcp_traverse,            /* tp_traverse */
3414     0,                                          /* tp_clear */
3415     0,  /* inherited */                         /* tp_richcompare */
3416     0,  /* inherited */                         /* tp_weaklistoffset */
3417     0,  /* inherited */                         /* tp_iter */
3418     0,                                          /* tp_iternext */
3419     0,  /* inherited */                         /* tp_methods */
3420     0,                                          /* tp_members */
3421     0,                                          /* tp_getset */
3422     &CData_Type,                                /* tp_base */
3423 #ifdef Py_TPFLAGS_HAVE_FINALIZE  /* CPython >= 3.4 */
3424     0,                                          /* tp_dict */
3425     0,                                          /* tp_descr_get */
3426     0,                                          /* tp_descr_set */
3427     0,                                          /* tp_dictoffset */
3428     0,                                          /* tp_init */
3429     0,                                          /* tp_alloc */
3430     0,                                          /* tp_new */
3431     0,  /* inherited */                         /* tp_free */
3432     0,                                          /* tp_is_gc */
3433     0,                                          /* tp_bases */
3434     0,                                          /* tp_mro */
3435     0,                                          /* tp_cache */
3436     0,                                          /* tp_subclasses */
3437     0,                                          /* tp_weaklist */
3438     0,                                          /* tp_del */
3439     0,                                          /* version_tag */
3440     (destructor)cdatagcp_finalize,              /* tp_finalize */
3441 #endif
3442 };
3443 
3444 /************************************************************/
3445 
3446 typedef struct {
3447     PyObject_HEAD
3448     char *di_next, *di_stop;
3449     CDataObject *di_object;
3450     CTypeDescrObject *di_itemtype;
3451 } CDataIterObject;
3452 
3453 static PyObject *
cdataiter_next(CDataIterObject * it)3454 cdataiter_next(CDataIterObject *it)
3455 {
3456     char *result = it->di_next;
3457     if (result != it->di_stop) {
3458         it->di_next = result + it->di_itemtype->ct_size;
3459         return convert_to_object(result, it->di_itemtype);
3460     }
3461     return NULL;
3462 }
3463 
3464 static void
cdataiter_dealloc(CDataIterObject * it)3465 cdataiter_dealloc(CDataIterObject *it)
3466 {
3467     Py_DECREF(it->di_object);
3468     PyObject_Del(it);
3469 }
3470 
3471 static PyTypeObject CDataIter_Type = {
3472     PyVarObject_HEAD_INIT(NULL, 0)
3473     "_cffi_backend.CDataIter",              /* tp_name */
3474     sizeof(CDataIterObject),                /* tp_basicsize */
3475     0,                                      /* tp_itemsize */
3476     /* methods */
3477     (destructor)cdataiter_dealloc,          /* tp_dealloc */
3478     0,                                      /* tp_print */
3479     0,                                      /* tp_getattr */
3480     0,                                      /* tp_setattr */
3481     0,                                      /* tp_compare */
3482     0,                                      /* tp_repr */
3483     0,                                      /* tp_as_number */
3484     0,                                      /* tp_as_sequence */
3485     0,                                      /* tp_as_mapping */
3486     0,                                      /* tp_hash */
3487     0,                                      /* tp_call */
3488     0,                                      /* tp_str */
3489     PyObject_GenericGetAttr,                /* tp_getattro */
3490     0,                                      /* tp_setattro */
3491     0,                                      /* tp_as_buffer */
3492     Py_TPFLAGS_DEFAULT,                     /* tp_flags */
3493     0,                                      /* tp_doc */
3494     0,                                      /* tp_traverse */
3495     0,                                      /* tp_clear */
3496     0,                                      /* tp_richcompare */
3497     0,                                      /* tp_weaklistoffset */
3498     PyObject_SelfIter,                      /* tp_iter */
3499     (iternextfunc)cdataiter_next,           /* tp_iternext */
3500 };
3501 
3502 static PyObject *
cdata_iter(CDataObject * cd)3503 cdata_iter(CDataObject *cd)
3504 {
3505     CDataIterObject *it;
3506 
3507     if (!(cd->c_type->ct_flags & CT_ARRAY)) {
3508         PyErr_Format(PyExc_TypeError, "cdata '%s' does not support iteration",
3509                      cd->c_type->ct_name);
3510         return NULL;
3511     }
3512 
3513     it = PyObject_New(CDataIterObject, &CDataIter_Type);
3514     if (it == NULL)
3515         return NULL;
3516 
3517     Py_INCREF(cd);
3518     it->di_object = cd;
3519     it->di_itemtype = cd->c_type->ct_itemdescr;
3520     it->di_next = cd->c_data;
3521     it->di_stop = cd->c_data + get_array_length(cd) * it->di_itemtype->ct_size;
3522     return (PyObject *)it;
3523 }
3524 
3525 /************************************************************/
3526 
allocate_owning_object(Py_ssize_t size,CTypeDescrObject * ct,int dont_clear)3527 static CDataObject *allocate_owning_object(Py_ssize_t size,
3528                                            CTypeDescrObject *ct,
3529                                            int dont_clear)
3530 {
3531     /* note: objects with &CDataOwning_Type are always allocated with
3532        either a plain malloc() or calloc(), and freed with free(). */
3533     CDataObject *cd;
3534     if (dont_clear)
3535         cd = malloc(size);
3536     else
3537         cd = calloc(size, 1);
3538     if (PyObject_Init((PyObject *)cd, &CDataOwning_Type) == NULL)
3539         return NULL;
3540 
3541     Py_INCREF(ct);
3542     cd->c_type = ct;
3543     cd->c_weakreflist = NULL;
3544     return cd;
3545 }
3546 
3547 static PyObject *
convert_struct_to_owning_object(char * data,CTypeDescrObject * ct)3548 convert_struct_to_owning_object(char *data, CTypeDescrObject *ct)
3549 {
3550     /* also accepts unions, for the API mode */
3551     CDataObject *cd;
3552     Py_ssize_t dataoffset = offsetof(CDataObject_own_nolength, alignment);
3553     Py_ssize_t datasize = ct->ct_size;
3554 
3555     if (datasize < 0) {
3556         PyErr_SetString(PyExc_TypeError,
3557                         "return type is an opaque structure or union");
3558         return NULL;
3559     }
3560     if (ct->ct_flags & CT_WITH_VAR_ARRAY) {
3561         PyErr_SetString(PyExc_TypeError,
3562                   "return type is a struct/union with a varsize array member");
3563         return NULL;
3564     }
3565     cd = allocate_owning_object(dataoffset + datasize, ct, /*dont_clear=*/1);
3566     if (cd == NULL)
3567         return NULL;
3568     cd->c_data = ((char *)cd) + dataoffset;
3569 
3570     memcpy(cd->c_data, data, datasize);
3571     return (PyObject *)cd;
3572 }
3573 
allocate_gcp_object(CDataObject * origobj,CTypeDescrObject * ct,PyObject * destructor)3574 static CDataObject *allocate_gcp_object(CDataObject *origobj,
3575                                         CTypeDescrObject *ct,
3576                                         PyObject *destructor)
3577 {
3578     CDataObject_gcp *cd = PyObject_GC_New(CDataObject_gcp, &CDataGCP_Type);
3579     if (cd == NULL)
3580         return NULL;
3581 
3582     Py_XINCREF(destructor);
3583     Py_INCREF(origobj);
3584     Py_INCREF(ct);
3585     cd->head.c_data = origobj->c_data;
3586     cd->head.c_type = ct;
3587     cd->head.c_weakreflist = NULL;
3588     cd->origobj = (PyObject *)origobj;
3589     cd->destructor = destructor;
3590 
3591     PyObject_GC_Track(cd);
3592     return (CDataObject *)cd;
3593 }
3594 
allocate_with_allocator(Py_ssize_t basesize,Py_ssize_t datasize,CTypeDescrObject * ct,const cffi_allocator_t * allocator)3595 static CDataObject *allocate_with_allocator(Py_ssize_t basesize,
3596                                             Py_ssize_t datasize,
3597                                             CTypeDescrObject *ct,
3598                                             const cffi_allocator_t *allocator)
3599 {
3600     CDataObject *cd;
3601 
3602     if (allocator->ca_alloc == NULL) {
3603         cd = allocate_owning_object(basesize + datasize, ct,
3604                                     allocator->ca_dont_clear);
3605         if (cd == NULL)
3606             return NULL;
3607         cd->c_data = ((char *)cd) + basesize;
3608     }
3609     else {
3610         PyObject *res = PyObject_CallFunction(allocator->ca_alloc, "n", datasize);
3611         if (res == NULL)
3612             return NULL;
3613 
3614         if (!CData_Check(res)) {
3615             PyErr_Format(PyExc_TypeError,
3616                          "alloc() must return a cdata object (got %.200s)",
3617                          Py_TYPE(res)->tp_name);
3618             Py_DECREF(res);
3619             return NULL;
3620         }
3621         cd = (CDataObject *)res;
3622         if (!(cd->c_type->ct_flags & (CT_POINTER|CT_ARRAY))) {
3623             PyErr_Format(PyExc_TypeError,
3624                          "alloc() must return a cdata pointer, not '%s'",
3625                          cd->c_type->ct_name);
3626             Py_DECREF(res);
3627             return NULL;
3628         }
3629         if (!cd->c_data) {
3630             PyErr_SetString(PyExc_MemoryError, "alloc() returned NULL");
3631             Py_DECREF(res);
3632             return NULL;
3633         }
3634 
3635         cd = allocate_gcp_object(cd, ct, allocator->ca_free);
3636         Py_DECREF(res);
3637         if (!allocator->ca_dont_clear)
3638             memset(cd->c_data, 0, datasize);
3639     }
3640     return cd;
3641 }
3642 
direct_newp(CTypeDescrObject * ct,PyObject * init,const cffi_allocator_t * allocator)3643 static PyObject *direct_newp(CTypeDescrObject *ct, PyObject *init,
3644                              const cffi_allocator_t *allocator)
3645 {
3646     CTypeDescrObject *ctitem;
3647     CDataObject *cd;
3648     Py_ssize_t dataoffset, datasize, explicitlength;
3649 
3650     explicitlength = -1;
3651     if (ct->ct_flags & CT_POINTER) {
3652         dataoffset = offsetof(CDataObject_own_nolength, alignment);
3653         ctitem = ct->ct_itemdescr;
3654         datasize = ctitem->ct_size;
3655         if (datasize < 0) {
3656             PyErr_Format(PyExc_TypeError,
3657                          "cannot instantiate ctype '%s' of unknown size",
3658                          ctitem->ct_name);
3659             return NULL;
3660         }
3661         if (ctitem->ct_flags & CT_PRIMITIVE_CHAR)
3662             datasize *= 2;   /* forcefully add another character: a null */
3663 
3664         if (ctitem->ct_flags & (CT_STRUCT | CT_UNION)) {
3665             if (force_lazy_struct(ctitem) < 0)   /* for CT_WITH_VAR_ARRAY */
3666                 return NULL;
3667 
3668             if (ctitem->ct_flags & CT_WITH_VAR_ARRAY) {
3669                 assert(ct->ct_flags & CT_IS_PTR_TO_OWNED);
3670                 dataoffset = offsetof(CDataObject_own_length, alignment);
3671 
3672                 if (init != Py_None) {
3673                     Py_ssize_t optvarsize = datasize;
3674                     if (convert_struct_from_object(NULL, ctitem, init,
3675                                                    &optvarsize) < 0)
3676                         return NULL;
3677                     datasize = optvarsize;
3678                 }
3679             }
3680         }
3681     }
3682     else if (ct->ct_flags & CT_ARRAY) {
3683         dataoffset = offsetof(CDataObject_own_nolength, alignment);
3684         datasize = ct->ct_size;
3685         if (datasize < 0) {
3686             explicitlength = get_new_array_length(ct->ct_itemdescr, &init);
3687             if (explicitlength < 0)
3688                 return NULL;
3689             ctitem = ct->ct_itemdescr;
3690             dataoffset = offsetof(CDataObject_own_length, alignment);
3691             datasize = MUL_WRAPAROUND(explicitlength, ctitem->ct_size);
3692             if (explicitlength > 0 &&
3693                     (datasize / explicitlength) != ctitem->ct_size) {
3694                 PyErr_SetString(PyExc_OverflowError,
3695                                 "array size would overflow a Py_ssize_t");
3696                 return NULL;
3697             }
3698         }
3699     }
3700     else {
3701         PyErr_Format(PyExc_TypeError,
3702                      "expected a pointer or array ctype, got '%s'",
3703                      ct->ct_name);
3704         return NULL;
3705     }
3706 
3707     if (ct->ct_flags & CT_IS_PTR_TO_OWNED) {
3708         /* common case of ptr-to-struct (or ptr-to-union): for this case
3709            we build two objects instead of one, with the memory-owning
3710            one being really the struct (or union) and the returned one
3711            having a strong reference to it */
3712         CDataObject *cds;
3713 
3714         cds = allocate_with_allocator(dataoffset, datasize, ct->ct_itemdescr,
3715                                       allocator);
3716         if (cds == NULL)
3717             return NULL;
3718 
3719         cd = allocate_owning_object(sizeof(CDataObject_own_structptr), ct,
3720                                     /*dont_clear=*/1);
3721         if (cd == NULL) {
3722             Py_DECREF(cds);
3723             return NULL;
3724         }
3725         /* store the only reference to cds into cd */
3726         ((CDataObject_own_structptr *)cd)->structobj = (PyObject *)cds;
3727         /* store information about the allocated size of the struct */
3728         if (dataoffset == offsetof(CDataObject_own_length, alignment)) {
3729             ((CDataObject_own_length *)cds)->length = datasize;
3730         }
3731         assert(explicitlength < 0);
3732 
3733         cd->c_data = cds->c_data;
3734     }
3735     else {
3736         cd = allocate_with_allocator(dataoffset, datasize, ct, allocator);
3737         if (cd == NULL)
3738             return NULL;
3739 
3740         if (explicitlength >= 0)
3741             ((CDataObject_own_length*)cd)->length = explicitlength;
3742     }
3743 
3744     if (init != Py_None) {
3745         if (convert_from_object(cd->c_data,
3746               (ct->ct_flags & CT_POINTER) ? ct->ct_itemdescr : ct, init) < 0) {
3747             Py_DECREF(cd);
3748             return NULL;
3749         }
3750     }
3751     return (PyObject *)cd;
3752 }
3753 
b_newp(PyObject * self,PyObject * args)3754 static PyObject *b_newp(PyObject *self, PyObject *args)
3755 {
3756     CTypeDescrObject *ct;
3757     PyObject *init = Py_None;
3758     if (!PyArg_ParseTuple(args, "O!|O:newp", &CTypeDescr_Type, &ct, &init))
3759         return NULL;
3760     return direct_newp(ct, init, &default_allocator);
3761 }
3762 
3763 static int
_my_PyObject_AsBool(PyObject * ob)3764 _my_PyObject_AsBool(PyObject *ob)
3765 {
3766     /* convert and cast a Python object to a boolean.  Accept an integer
3767        or a float object, up to a CData 'long double'. */
3768     PyObject *io;
3769     PyNumberMethods *nb;
3770     int res;
3771 
3772 #if PY_MAJOR_VERSION < 3
3773     if (PyInt_Check(ob)) {
3774         return PyInt_AS_LONG(ob) != 0;
3775     }
3776     else
3777 #endif
3778     if (PyLong_Check(ob)) {
3779         return _PyLong_Sign(ob) != 0;
3780     }
3781     else if (PyFloat_Check(ob)) {
3782         return PyFloat_AS_DOUBLE(ob) != 0.0;
3783     }
3784     else if (CData_Check(ob)) {
3785         CDataObject *cd = (CDataObject *)ob;
3786         if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
3787             /*READ(cd->c_data, cd->c_type->ct_size)*/
3788             if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE) {
3789                 /* 'long double' objects: return the answer directly */
3790                 return read_raw_longdouble_data(cd->c_data) != 0.0;
3791             }
3792             else {
3793                 /* 'float'/'double' objects: return the answer directly */
3794                 return read_raw_float_data(cd->c_data,
3795                                            cd->c_type->ct_size) != 0.0;
3796             }
3797         }
3798     }
3799     nb = ob->ob_type->tp_as_number;
3800     if (nb == NULL || (nb->nb_float == NULL && nb->nb_int == NULL)) {
3801         PyErr_SetString(PyExc_TypeError, "integer/float expected");
3802         return -1;
3803     }
3804     if (nb->nb_float && !CData_Check(ob))
3805         io = (*nb->nb_float) (ob);
3806     else
3807         io = (*nb->nb_int) (ob);
3808     if (io == NULL)
3809         return -1;
3810 
3811     if (PyIntOrLong_Check(io) || PyFloat_Check(io)) {
3812         res = _my_PyObject_AsBool(io);
3813     }
3814     else {
3815         PyErr_SetString(PyExc_TypeError, "integer/float conversion failed");
3816         res = -1;
3817     }
3818     Py_DECREF(io);
3819     return res;
3820 }
3821 
_new_casted_primitive(CTypeDescrObject * ct)3822 static CDataObject *_new_casted_primitive(CTypeDescrObject *ct)
3823 {
3824     int dataoffset = offsetof(CDataObject_casted_primitive, alignment);
3825     CDataObject *cd = (CDataObject *)PyObject_Malloc(dataoffset + ct->ct_size);
3826     if (PyObject_Init((PyObject *)cd, &CData_Type) == NULL)
3827         return NULL;
3828     Py_INCREF(ct);
3829     cd->c_type = ct;
3830     cd->c_data = ((char*)cd) + dataoffset;
3831     cd->c_weakreflist = NULL;
3832     return cd;
3833 }
3834 
cast_to_integer_or_char(CTypeDescrObject * ct,PyObject * ob)3835 static CDataObject *cast_to_integer_or_char(CTypeDescrObject *ct, PyObject *ob)
3836 {
3837     unsigned PY_LONG_LONG value;
3838     CDataObject *cd;
3839 
3840     if (CData_Check(ob) &&
3841         ((CDataObject *)ob)->c_type->ct_flags &
3842                                  (CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY)) {
3843         value = (Py_intptr_t)((CDataObject *)ob)->c_data;
3844     }
3845 #if PY_MAJOR_VERSION < 3
3846     else if (PyString_Check(ob)) {
3847         if (PyString_GET_SIZE(ob) != 1) {
3848             PyErr_Format(PyExc_TypeError,
3849                          "cannot cast string of length %zd to ctype '%s'",
3850                          PyString_GET_SIZE(ob), ct->ct_name);
3851             return NULL;
3852         }
3853         value = (unsigned char)PyString_AS_STRING(ob)[0];
3854     }
3855 #endif
3856     else if (PyUnicode_Check(ob)) {
3857         char err_buf[80];
3858         cffi_char32_t ordinal;
3859         if (_my_PyUnicode_AsSingleChar32(ob, &ordinal, err_buf) < 0) {
3860             PyErr_Format(PyExc_TypeError,
3861                          "cannot cast %s to ctype '%s'", err_buf, ct->ct_name);
3862             return NULL;
3863         }
3864         /* the types char16_t and char32_t are both unsigned.  However,
3865            wchar_t might be signed.  In theory it does not matter,
3866            because 'ordinal' comes from a regular Python unicode. */
3867 #ifdef HAVE_WCHAR_H
3868         if (ct->ct_flags & CT_IS_SIGNED_WCHAR)
3869             value = (wchar_t)ordinal;
3870         else
3871 #endif
3872             value = ordinal;
3873     }
3874     else if (PyBytes_Check(ob)) {
3875         int res = _convert_to_char(ob);
3876         if (res < 0)
3877             return NULL;
3878         value = (unsigned char)res;
3879     }
3880     else if (ct->ct_flags & CT_IS_BOOL) {
3881         int res = _my_PyObject_AsBool(ob);
3882         if (res < 0)
3883             return NULL;
3884         value = res;
3885     }
3886     else {
3887         value = _my_PyLong_AsUnsignedLongLong(ob, 0);
3888         if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
3889             return NULL;
3890     }
3891     if (ct->ct_flags & CT_IS_BOOL)
3892         value = !!value;
3893     cd = _new_casted_primitive(ct);
3894     if (cd != NULL)
3895         write_raw_integer_data(cd->c_data, value, ct->ct_size);
3896     return cd;
3897 }
3898 
3899 /* returns -1 if cannot cast, 0 if we don't get a value, 1 if we do */
check_bytes_for_float_compatible(PyObject * io,double * out_value)3900 static int check_bytes_for_float_compatible(PyObject *io, double *out_value)
3901 {
3902     if (PyBytes_Check(io)) {
3903         if (PyBytes_GET_SIZE(io) != 1)
3904             goto error;
3905         *out_value = (unsigned char)PyBytes_AS_STRING(io)[0];
3906         return 1;
3907     }
3908     else if (PyUnicode_Check(io)) {
3909         char ignored[80];
3910         cffi_char32_t ordinal;
3911         if (_my_PyUnicode_AsSingleChar32(io, &ordinal, ignored) < 0)
3912             goto error;
3913         /* the signness of the 32-bit version of wide chars should not
3914          * matter here, because 'ordinal' comes from a normal Python
3915          * unicode string */
3916         *out_value = ordinal;
3917         return 1;
3918     }
3919     *out_value = 0;   /* silence a gcc warning if this function is inlined */
3920     return 0;
3921 
3922  error:
3923     Py_DECREF(io);
3924     *out_value = 0;   /* silence a gcc warning if this function is inlined */
3925     return -1;
3926 }
3927 
do_cast(CTypeDescrObject * ct,PyObject * ob)3928 static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob)
3929 {
3930     CDataObject *cd;
3931 
3932     if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY) &&
3933         ct->ct_size >= 0) {
3934         /* cast to a pointer, to a funcptr, or to an array.
3935            Note that casting to an array is an extension to the C language,
3936            which seems to be necessary in order to sanely get a
3937            <cdata 'int[3]'> at some address. */
3938         unsigned PY_LONG_LONG value;
3939 
3940         if (CData_Check(ob)) {
3941             CDataObject *cdsrc = (CDataObject *)ob;
3942             if (cdsrc->c_type->ct_flags &
3943                     (CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY)) {
3944                 return new_simple_cdata(cdsrc->c_data, ct);
3945             }
3946         }
3947         if ((ct->ct_flags & CT_POINTER) &&
3948                 (ct->ct_itemdescr->ct_flags & CT_IS_FILE) &&
3949                 PyFile_Check(ob)) {
3950             FILE *f = PyFile_AsFile(ob);
3951             if (f == NULL && PyErr_Occurred())
3952                 return NULL;
3953             return new_simple_cdata((char *)f, ct);
3954         }
3955         value = _my_PyLong_AsUnsignedLongLong(ob, 0);
3956         if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
3957             return NULL;
3958         return new_simple_cdata((char *)(Py_intptr_t)value, ct);
3959     }
3960     else if (ct->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED
3961                              |CT_PRIMITIVE_CHAR)) {
3962         /* cast to an integer type or a char */
3963         return (PyObject *)cast_to_integer_or_char(ct, ob);
3964     }
3965     else if (ct->ct_flags & CT_PRIMITIVE_FLOAT) {
3966         /* cast to a float */
3967         double value;
3968         PyObject *io;
3969         int res;
3970 
3971         if (CData_Check(ob)) {
3972             CDataObject *cdsrc = (CDataObject *)ob;
3973 
3974             if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY))
3975                 goto cannot_cast;
3976             io = convert_to_object(cdsrc->c_data, cdsrc->c_type);
3977             if (io == NULL)
3978                 return NULL;
3979         }
3980         else {
3981             io = ob;
3982             Py_INCREF(io);
3983         }
3984 
3985         res = check_bytes_for_float_compatible(io, &value);
3986         if (res == -1)
3987             goto cannot_cast;
3988         if (res == 0) {
3989             if ((ct->ct_flags & CT_IS_LONGDOUBLE) &&
3990                  CData_Check(io) &&
3991                  (((CDataObject *)io)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
3992                 long double lvalue;
3993                 char *data = ((CDataObject *)io)->c_data;
3994                 /*READ(data, sizeof(long double)*/
3995                 lvalue = read_raw_longdouble_data(data);
3996                 Py_DECREF(io);
3997                 cd = _new_casted_primitive(ct);
3998                 if (cd != NULL)
3999                     write_raw_longdouble_data(cd->c_data, lvalue);
4000                 return (PyObject *)cd;
4001             }
4002             value = PyFloat_AsDouble(io);
4003         }
4004         Py_DECREF(io);
4005         if (value == -1.0 && PyErr_Occurred())
4006             return NULL;
4007 
4008         cd = _new_casted_primitive(ct);
4009         if (cd != NULL) {
4010             if (!(ct->ct_flags & CT_IS_LONGDOUBLE))
4011                 write_raw_float_data(cd->c_data, value, ct->ct_size);
4012             else
4013                 write_raw_longdouble_data(cd->c_data, (long double)value);
4014         }
4015         return (PyObject *)cd;
4016     }
4017     else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
4018         /* cast to a complex */
4019         Py_complex value;
4020         PyObject *io;
4021         int res;
4022 
4023         if (CData_Check(ob)) {
4024             CDataObject *cdsrc = (CDataObject *)ob;
4025 
4026             if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY))
4027                 goto cannot_cast;
4028             io = convert_to_object(cdsrc->c_data, cdsrc->c_type);
4029             if (io == NULL)
4030                 return NULL;
4031         }
4032         else {
4033             io = ob;
4034             Py_INCREF(io);
4035         }
4036 
4037         res = check_bytes_for_float_compatible(io, &value.real);
4038         if (res == -1)
4039             goto cannot_cast;
4040         if (res == 1) {
4041             // got it from string
4042             value.imag = 0.0;
4043         } else {
4044             value = PyComplex_AsCComplex(io);
4045         }
4046         Py_DECREF(io);
4047         if (PyErr_Occurred()) {
4048             return NULL;
4049         }
4050         cd = _new_casted_primitive(ct);
4051         if (cd != NULL) {
4052             write_raw_complex_data(cd->c_data, value, ct->ct_size);
4053         }
4054         return (PyObject *)cd;
4055     }
4056     else {
4057         PyErr_Format(PyExc_TypeError, "cannot cast to ctype '%s'",
4058                      ct->ct_name);
4059         return NULL;
4060     }
4061 
4062  cannot_cast:
4063     if (CData_Check(ob))
4064         PyErr_Format(PyExc_TypeError, "cannot cast ctype '%s' to ctype '%s'",
4065                      ((CDataObject *)ob)->c_type->ct_name, ct->ct_name);
4066     else
4067         PyErr_Format(PyExc_TypeError,
4068                      "cannot cast %.200s object to ctype '%s'",
4069                      Py_TYPE(ob)->tp_name, ct->ct_name);
4070     return NULL;
4071 }
4072 
b_cast(PyObject * self,PyObject * args)4073 static PyObject *b_cast(PyObject *self, PyObject *args)
4074 {
4075     CTypeDescrObject *ct;
4076     PyObject *ob;
4077     if (!PyArg_ParseTuple(args, "O!O:cast", &CTypeDescr_Type, &ct, &ob))
4078         return NULL;
4079 
4080     return do_cast(ct, ob);
4081 }
4082 
4083 /************************************************************/
4084 
4085 typedef struct {
4086     PyObject_HEAD
4087     void *dl_handle;
4088     char *dl_name;
4089 } DynLibObject;
4090 
dl_dealloc(DynLibObject * dlobj)4091 static void dl_dealloc(DynLibObject *dlobj)
4092 {
4093     if (dlobj->dl_handle != NULL)
4094         dlclose(dlobj->dl_handle);
4095     free(dlobj->dl_name);
4096     PyObject_Del(dlobj);
4097 }
4098 
dl_repr(DynLibObject * dlobj)4099 static PyObject *dl_repr(DynLibObject *dlobj)
4100 {
4101     return PyText_FromFormat("<clibrary '%s'>", dlobj->dl_name);
4102 }
4103 
dl_check_closed(DynLibObject * dlobj)4104 static int dl_check_closed(DynLibObject *dlobj)
4105 {
4106     if (dlobj->dl_handle == NULL)
4107     {
4108         PyErr_Format(PyExc_ValueError, "library '%s' has already been closed",
4109                      dlobj->dl_name);
4110         return -1;
4111     }
4112     return 0;
4113 }
4114 
dl_load_function(DynLibObject * dlobj,PyObject * args)4115 static PyObject *dl_load_function(DynLibObject *dlobj, PyObject *args)
4116 {
4117     CTypeDescrObject *ct;
4118     char *funcname;
4119     void *funcptr;
4120 
4121     if (!PyArg_ParseTuple(args, "O!s:load_function",
4122                           &CTypeDescr_Type, &ct, &funcname))
4123         return NULL;
4124 
4125     if (dl_check_closed(dlobj) < 0)
4126         return NULL;
4127 
4128     if (!(ct->ct_flags & (CT_FUNCTIONPTR | CT_POINTER | CT_ARRAY))) {
4129         PyErr_Format(PyExc_TypeError,
4130                      "function or pointer or array cdata expected, got '%s'",
4131                      ct->ct_name);
4132         return NULL;
4133     }
4134     dlerror();   /* clear error condition */
4135     funcptr = dlsym(dlobj->dl_handle, funcname);
4136     if (funcptr == NULL) {
4137         const char *error = dlerror();
4138         PyErr_Format(PyExc_AttributeError,
4139                      "function/symbol '%s' not found in library '%s': %s",
4140                      funcname, dlobj->dl_name, error);
4141         return NULL;
4142     }
4143 
4144     if ((ct->ct_flags & CT_ARRAY) && ct->ct_length < 0) {
4145         ct = (CTypeDescrObject *)ct->ct_stuff;
4146     }
4147     return new_simple_cdata(funcptr, ct);
4148 }
4149 
dl_read_variable(DynLibObject * dlobj,PyObject * args)4150 static PyObject *dl_read_variable(DynLibObject *dlobj, PyObject *args)
4151 {
4152     CTypeDescrObject *ct;
4153     char *varname;
4154     char *data;
4155 
4156     if (!PyArg_ParseTuple(args, "O!s:read_variable",
4157                           &CTypeDescr_Type, &ct, &varname))
4158         return NULL;
4159 
4160     if (dl_check_closed(dlobj) < 0)
4161         return NULL;
4162 
4163     dlerror();   /* clear error condition */
4164     data = dlsym(dlobj->dl_handle, varname);
4165     if (data == NULL) {
4166         const char *error = dlerror();
4167         if (error != NULL) {
4168             PyErr_Format(PyExc_KeyError,
4169                          "variable '%s' not found in library '%s': %s",
4170                          varname, dlobj->dl_name, error);
4171             return NULL;
4172         }
4173     }
4174     return convert_to_object(data, ct);
4175 }
4176 
dl_write_variable(DynLibObject * dlobj,PyObject * args)4177 static PyObject *dl_write_variable(DynLibObject *dlobj, PyObject *args)
4178 {
4179     CTypeDescrObject *ct;
4180     PyObject *value;
4181     char *varname;
4182     char *data;
4183 
4184     if (!PyArg_ParseTuple(args, "O!sO:write_variable",
4185                           &CTypeDescr_Type, &ct, &varname, &value))
4186         return NULL;
4187 
4188     if (dl_check_closed(dlobj) < 0)
4189         return NULL;
4190 
4191     dlerror();   /* clear error condition */
4192     data = dlsym(dlobj->dl_handle, varname);
4193     if (data == NULL) {
4194         const char *error = dlerror();
4195         PyErr_Format(PyExc_KeyError,
4196                      "variable '%s' not found in library '%s': %s",
4197                      varname, dlobj->dl_name, error);
4198         return NULL;
4199     }
4200     if (convert_from_object(data, ct, value) < 0)
4201         return NULL;
4202     Py_INCREF(Py_None);
4203     return Py_None;
4204 }
4205 
dl_close_lib(DynLibObject * dlobj,PyObject * no_args)4206 static PyObject *dl_close_lib(DynLibObject *dlobj, PyObject *no_args)
4207 {
4208     if (dlobj->dl_handle != NULL)
4209     {
4210         dlclose(dlobj->dl_handle);
4211         dlobj->dl_handle = NULL;
4212     }
4213     Py_INCREF(Py_None);
4214     return Py_None;
4215 }
4216 
4217 static PyMethodDef dl_methods[] = {
4218     {"load_function",   (PyCFunction)dl_load_function,  METH_VARARGS},
4219     {"read_variable",   (PyCFunction)dl_read_variable,  METH_VARARGS},
4220     {"write_variable",  (PyCFunction)dl_write_variable, METH_VARARGS},
4221     {"close_lib",       (PyCFunction)dl_close_lib,      METH_NOARGS},
4222     {NULL,              NULL}           /* sentinel */
4223 };
4224 
4225 static PyTypeObject dl_type = {
4226     PyVarObject_HEAD_INIT(NULL, 0)
4227     "_cffi_backend.Library",            /* tp_name */
4228     sizeof(DynLibObject),               /* tp_basicsize */
4229     0,                                  /* tp_itemsize */
4230     /* methods */
4231     (destructor)dl_dealloc,             /* tp_dealloc */
4232     0,                                  /* tp_print */
4233     0,                                  /* tp_getattr */
4234     0,                                  /* tp_setattr */
4235     0,                                  /* tp_compare */
4236     (reprfunc)dl_repr,                  /* tp_repr */
4237     0,                                  /* tp_as_number */
4238     0,                                  /* tp_as_sequence */
4239     0,                                  /* tp_as_mapping */
4240     0,                                  /* tp_hash */
4241     0,                                  /* tp_call */
4242     0,                                  /* tp_str */
4243     PyObject_GenericGetAttr,            /* tp_getattro */
4244     0,                                  /* tp_setattro */
4245     0,                                  /* tp_as_buffer */
4246     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
4247     0,                                  /* tp_doc */
4248     0,                                  /* tp_traverse */
4249     0,                                  /* tp_clear */
4250     0,                                  /* tp_richcompare */
4251     0,                                  /* tp_weaklistoffset */
4252     0,                                  /* tp_iter */
4253     0,                                  /* tp_iternext */
4254     dl_methods,                         /* tp_methods */
4255 };
4256 
b_do_dlopen(PyObject * args,const char ** p_printable_filename,PyObject ** p_temp)4257 static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
4258                          PyObject **p_temp)
4259 {
4260     /* Logic to call the correct version of dlopen().  Returns NULL in case of error.
4261        Otherwise, '*p_printable_filename' will point to a printable char version of
4262        the filename (maybe utf-8-encoded).  '*p_temp' will be set either to NULL or
4263        to a temporary object that must be freed after looking at printable_filename.
4264     */
4265     void *handle;
4266     char *filename_or_null;
4267     int flags = 0;
4268     *p_temp = NULL;
4269 
4270     if (PyTuple_GET_SIZE(args) == 0 || PyTuple_GET_ITEM(args, 0) == Py_None) {
4271         PyObject *dummy;
4272         if (!PyArg_ParseTuple(args, "|Oi:load_library",
4273                               &dummy, &flags))
4274             return NULL;
4275         filename_or_null = NULL;
4276         *p_printable_filename = "<None>";
4277     }
4278     else
4279     {
4280         PyObject *s = PyTuple_GET_ITEM(args, 0);
4281 #ifdef MS_WIN32
4282         Py_UNICODE *filenameW;
4283         if (PyArg_ParseTuple(args, "u|i:load_library", &filenameW, &flags))
4284         {
4285 #if PY_MAJOR_VERSION < 3
4286             s = PyUnicode_AsUTF8String(s);
4287             if (s == NULL)
4288                 return NULL;
4289             *p_temp = s;
4290 #endif
4291             *p_printable_filename = PyText_AsUTF8(s);
4292             if (*p_printable_filename == NULL)
4293                 return NULL;
4294 
4295             handle = dlopenW(filenameW);
4296             goto got_handle;
4297         }
4298         PyErr_Clear();
4299 #endif
4300         if (!PyArg_ParseTuple(args, "et|i:load_library",
4301                      Py_FileSystemDefaultEncoding, &filename_or_null, &flags))
4302             return NULL;
4303 #if PY_MAJOR_VERSION < 3
4304         if (PyUnicode_Check(s))
4305         {
4306             s = PyUnicode_AsUTF8String(s);
4307             if (s == NULL)
4308                 return NULL;
4309             *p_temp = s;
4310         }
4311 #endif
4312         *p_printable_filename = PyText_AsUTF8(s);
4313         if (*p_printable_filename == NULL)
4314             return NULL;
4315     }
4316     if ((flags & (RTLD_NOW | RTLD_LAZY)) == 0)
4317         flags |= RTLD_NOW;
4318 
4319     handle = dlopen(filename_or_null, flags);
4320 
4321 #ifdef MS_WIN32
4322   got_handle:
4323 #endif
4324     if (handle == NULL) {
4325         const char *error = dlerror();
4326         PyErr_Format(PyExc_OSError, "cannot load library '%s': %s",
4327                      *p_printable_filename, error);
4328         return NULL;
4329     }
4330     return handle;
4331 }
4332 
b_load_library(PyObject * self,PyObject * args)4333 static PyObject *b_load_library(PyObject *self, PyObject *args)
4334 {
4335     const char *printable_filename;
4336     PyObject *temp;
4337     void *handle;
4338     DynLibObject *dlobj = NULL;
4339 
4340     handle = b_do_dlopen(args, &printable_filename, &temp);
4341     if (handle == NULL)
4342         goto error;
4343 
4344     dlobj = PyObject_New(DynLibObject, &dl_type);
4345     if (dlobj == NULL) {
4346         dlclose(handle);
4347         goto error;
4348     }
4349     dlobj->dl_handle = handle;
4350     dlobj->dl_name = strdup(printable_filename);
4351 
4352  error:
4353     Py_XDECREF(temp);
4354     return (PyObject *)dlobj;
4355 }
4356 
4357 /************************************************************/
4358 
get_unique_type(CTypeDescrObject * x,const void * unique_key[],long keylength)4359 static PyObject *get_unique_type(CTypeDescrObject *x,
4360                                  const void *unique_key[], long keylength)
4361 {
4362     /* Replace the CTypeDescrObject 'x' with a standardized one.
4363        This either just returns x, or x is decrefed and a new reference
4364        to the already-existing equivalent is returned.
4365 
4366        In this function, 'x' always contains a reference that must be
4367        either decrefed or returned.
4368 
4369        Keys:
4370            void       ["void"]
4371            primitive  [&static_struct]
4372            pointer    [ctype]
4373            array      [ctype, length]
4374            funcptr    [ctresult, ellipsis+abi, num_args, ctargs...]
4375     */
4376     PyObject *key, *y;
4377     void *pkey;
4378 
4379     key = PyBytes_FromStringAndSize(NULL, keylength * sizeof(void *));
4380     if (key == NULL)
4381         goto error;
4382 
4383     pkey = PyBytes_AS_STRING(key);
4384     memcpy(pkey, unique_key, keylength * sizeof(void *));
4385 
4386     y = PyDict_GetItem(unique_cache, key);
4387     if (y != NULL) {
4388         Py_DECREF(key);
4389         Py_INCREF(y);
4390         Py_DECREF(x);
4391         return y;
4392     }
4393     if (PyDict_SetItem(unique_cache, key, (PyObject *)x) < 0) {
4394         Py_DECREF(key);
4395         goto error;
4396     }
4397     /* Haaaack for our reference count hack: gcmodule.c must not see this
4398        dictionary.  The problem is that any PyDict_SetItem() notices that
4399        'x' is tracked and re-tracks the unique_cache dictionary.  So here
4400        we re-untrack it again... */
4401     PyObject_GC_UnTrack(unique_cache);
4402 
4403     assert(x->ct_unique_key == NULL);
4404     x->ct_unique_key = key; /* the key will be freed in ctypedescr_dealloc() */
4405     /* the 'value' in unique_cache doesn't count as 1, but don't use
4406        Py_DECREF(x) here because it will confuse debug builds into thinking
4407        there was an extra DECREF in total. */
4408     ((PyObject *)x)->ob_refcnt--;
4409     return (PyObject *)x;
4410 
4411  error:
4412     Py_DECREF(x);
4413     return NULL;
4414 }
4415 
4416 /* according to the C standard, these types should be equivalent to the
4417    _Complex types for the purposes of storage (not arguments in calls!) */
4418 typedef float cffi_float_complex_t[2];
4419 typedef double cffi_double_complex_t[2];
4420 
new_primitive_type(const char * name)4421 static PyObject *new_primitive_type(const char *name)
4422 {
4423 #define ENUM_PRIMITIVE_TYPES                                    \
4424        EPTYPE(c, char, CT_PRIMITIVE_CHAR)                       \
4425        EPTYPE(s, short, CT_PRIMITIVE_SIGNED )                   \
4426        EPTYPE(i, int, CT_PRIMITIVE_SIGNED )                     \
4427        EPTYPE(l, long, CT_PRIMITIVE_SIGNED )                    \
4428        EPTYPE(ll, long long, CT_PRIMITIVE_SIGNED )              \
4429        EPTYPE(sc, signed char, CT_PRIMITIVE_SIGNED )            \
4430        EPTYPE(uc, unsigned char, CT_PRIMITIVE_UNSIGNED )        \
4431        EPTYPE(us, unsigned short, CT_PRIMITIVE_UNSIGNED )       \
4432        EPTYPE(ui, unsigned int, CT_PRIMITIVE_UNSIGNED )         \
4433        EPTYPE(ul, unsigned long, CT_PRIMITIVE_UNSIGNED )        \
4434        EPTYPE(ull, unsigned long long, CT_PRIMITIVE_UNSIGNED )  \
4435        EPTYPE(f, float, CT_PRIMITIVE_FLOAT )                    \
4436        EPTYPE(d, double, CT_PRIMITIVE_FLOAT )                   \
4437        EPTYPE(ld, long double, CT_PRIMITIVE_FLOAT | CT_IS_LONGDOUBLE ) \
4438        EPTYPE2(fc, "float _Complex", cffi_float_complex_t, CT_PRIMITIVE_COMPLEX ) \
4439        EPTYPE2(dc, "double _Complex", cffi_double_complex_t, CT_PRIMITIVE_COMPLEX ) \
4440        ENUM_PRIMITIVE_TYPES_WCHAR                               \
4441        EPTYPE2(c16, "char16_t", cffi_char16_t, CT_PRIMITIVE_CHAR ) \
4442        EPTYPE2(c32, "char32_t", cffi_char32_t, CT_PRIMITIVE_CHAR ) \
4443        EPTYPE(b, _Bool, CT_PRIMITIVE_UNSIGNED | CT_IS_BOOL )    \
4444      /* the following types are not primitive in the C sense */ \
4445        EPTYPE(i8, int8_t, CT_PRIMITIVE_SIGNED)                  \
4446        EPTYPE(u8, uint8_t, CT_PRIMITIVE_UNSIGNED)               \
4447        EPTYPE(i16, int16_t, CT_PRIMITIVE_SIGNED)                \
4448        EPTYPE(u16, uint16_t, CT_PRIMITIVE_UNSIGNED)             \
4449        EPTYPE(i32, int32_t, CT_PRIMITIVE_SIGNED)                \
4450        EPTYPE(u32, uint32_t, CT_PRIMITIVE_UNSIGNED)             \
4451        EPTYPE(i64, int64_t, CT_PRIMITIVE_SIGNED)                \
4452        EPTYPE(u64, uint64_t, CT_PRIMITIVE_UNSIGNED)             \
4453        EPTYPE(il8, int_least8_t, CT_PRIMITIVE_SIGNED)           \
4454        EPTYPE(ul8, uint_least8_t, CT_PRIMITIVE_UNSIGNED)        \
4455        EPTYPE(il16, int_least16_t, CT_PRIMITIVE_SIGNED)         \
4456        EPTYPE(ul16, uint_least16_t, CT_PRIMITIVE_UNSIGNED)      \
4457        EPTYPE(il32, int_least32_t, CT_PRIMITIVE_SIGNED)         \
4458        EPTYPE(ul32, uint_least32_t, CT_PRIMITIVE_UNSIGNED)      \
4459        EPTYPE(il64, int_least64_t, CT_PRIMITIVE_SIGNED)         \
4460        EPTYPE(ul64, uint_least64_t, CT_PRIMITIVE_UNSIGNED)      \
4461        EPTYPE(if8, int_fast8_t, CT_PRIMITIVE_SIGNED)            \
4462        EPTYPE(uf8, uint_fast8_t, CT_PRIMITIVE_UNSIGNED)         \
4463        EPTYPE(if16, int_fast16_t, CT_PRIMITIVE_SIGNED)          \
4464        EPTYPE(uf16, uint_fast16_t, CT_PRIMITIVE_UNSIGNED)       \
4465        EPTYPE(if32, int_fast32_t, CT_PRIMITIVE_SIGNED)          \
4466        EPTYPE(uf32, uint_fast32_t, CT_PRIMITIVE_UNSIGNED)       \
4467        EPTYPE(if64, int_fast64_t, CT_PRIMITIVE_SIGNED)          \
4468        EPTYPE(uf64, uint_fast64_t, CT_PRIMITIVE_UNSIGNED)       \
4469        EPTYPE(ip, intptr_t, CT_PRIMITIVE_SIGNED)                \
4470        EPTYPE(up, uintptr_t, CT_PRIMITIVE_UNSIGNED)             \
4471        EPTYPE(im, intmax_t, CT_PRIMITIVE_SIGNED)                \
4472        EPTYPE(um, uintmax_t, CT_PRIMITIVE_UNSIGNED)             \
4473        EPTYPE(pd, ptrdiff_t, CT_PRIMITIVE_SIGNED)               \
4474        EPTYPE(sz, size_t, CT_PRIMITIVE_UNSIGNED)                \
4475        EPTYPE2(ssz, "ssize_t", Py_ssize_t, CT_PRIMITIVE_SIGNED)
4476 
4477 #ifdef HAVE_WCHAR_H
4478 # define ENUM_PRIMITIVE_TYPES_WCHAR                             \
4479        EPTYPE(wc, wchar_t, CT_PRIMITIVE_CHAR |                  \
4480                            (((wchar_t)-1) > 0 ? 0 : CT_IS_SIGNED_WCHAR))
4481 #else
4482 # define ENUM_PRIMITIVE_TYPES_WCHAR   /* nothing */
4483 #endif
4484 
4485 #define EPTYPE(code, typename, flags)  EPTYPE2(code, #typename, typename, flags)
4486 
4487 #define EPTYPE2(code, export_name, typename, flags)     \
4488     struct aligncheck_##code { char x; typename y; };
4489     ENUM_PRIMITIVE_TYPES
4490 #undef EPTYPE2
4491 
4492     CTypeDescrObject *td;
4493     static const struct descr_s { const char *name; int size, align, flags; }
4494     types[] = {
4495 #define EPTYPE2(code, export_name, typename, flags)     \
4496         { export_name,                                  \
4497           sizeof(typename),                             \
4498           offsetof(struct aligncheck_##code, y),        \
4499           flags                                         \
4500         },
4501     ENUM_PRIMITIVE_TYPES
4502 #undef EPTYPE2
4503 #undef EPTYPE
4504 #undef ENUM_PRIMITIVE_TYPES_WCHAR
4505 #undef ENUM_PRIMITIVE_TYPES
4506         { NULL }
4507     };
4508     const struct descr_s *ptypes;
4509     const void *unique_key[1];
4510     int name_size;
4511     ffi_type *ffitype;
4512 
4513     for (ptypes=types; ; ptypes++) {
4514         if (ptypes->name == NULL) {
4515 #ifndef HAVE_WCHAR_H
4516             if (strcmp(name, "wchar_t"))
4517                 PyErr_SetString(PyExc_NotImplementedError, name);
4518             else
4519 #endif
4520             PyErr_SetString(PyExc_KeyError, name);
4521             return NULL;
4522         }
4523         if (strcmp(name, ptypes->name) == 0)
4524             break;
4525     }
4526 
4527     if (ptypes->flags & CT_PRIMITIVE_SIGNED) {
4528         switch (ptypes->size) {
4529         case 1: ffitype = &ffi_type_sint8; break;
4530         case 2: ffitype = &ffi_type_sint16; break;
4531         case 4: ffitype = &ffi_type_sint32; break;
4532         case 8: ffitype = &ffi_type_sint64; break;
4533         default: goto bad_ffi_type;
4534         }
4535     }
4536     else if (ptypes->flags & CT_PRIMITIVE_FLOAT) {
4537         if (strcmp(ptypes->name, "float") == 0)
4538             ffitype = &ffi_type_float;
4539         else if (strcmp(ptypes->name, "double") == 0)
4540             ffitype = &ffi_type_double;
4541         else if (strcmp(ptypes->name, "long double") == 0) {
4542             /* assume that if sizeof(double) == sizeof(long double), then
4543                the two types are equivalent for C.  libffi bugs on Win64
4544                if a function's return type is ffi_type_longdouble... */
4545             if (sizeof(double) == sizeof(long double))
4546                 ffitype = &ffi_type_double;
4547             else
4548                 ffitype = &ffi_type_longdouble;
4549         }
4550         else
4551             goto bad_ffi_type;
4552     }
4553     else if (ptypes->flags & CT_PRIMITIVE_COMPLEX) {
4554         /* As of March 2017, still no libffi support for complex.
4555            It fails silently if we try to use ffi_type_complex_float
4556            or ffi_type_complex_double.  Better not use it at all.
4557          */
4558         ffitype = NULL;
4559     }
4560     else {
4561         switch (ptypes->size) {
4562         case 1: ffitype = &ffi_type_uint8; break;
4563         case 2: ffitype = &ffi_type_uint16; break;
4564         case 4: ffitype = &ffi_type_uint32; break;
4565         case 8: ffitype = &ffi_type_uint64; break;
4566         default: goto bad_ffi_type;
4567         }
4568     }
4569 
4570     name_size = strlen(ptypes->name) + 1;
4571     td = ctypedescr_new(name_size);
4572     if (td == NULL)
4573         return NULL;
4574 
4575     memcpy(td->ct_name, name, name_size);
4576     td->ct_size = ptypes->size;
4577     td->ct_length = ptypes->align;
4578     td->ct_extra = ffitype;
4579     td->ct_flags = ptypes->flags;
4580     if (td->ct_flags & (CT_PRIMITIVE_SIGNED | CT_PRIMITIVE_CHAR)) {
4581         if (td->ct_size <= (Py_ssize_t)sizeof(long))
4582             td->ct_flags |= CT_PRIMITIVE_FITS_LONG;
4583     }
4584     else if (td->ct_flags & CT_PRIMITIVE_UNSIGNED) {
4585         if (td->ct_size < (Py_ssize_t)sizeof(long))
4586             td->ct_flags |= CT_PRIMITIVE_FITS_LONG;
4587     }
4588     td->ct_name_position = strlen(td->ct_name);
4589     unique_key[0] = ptypes;
4590     return get_unique_type(td, unique_key, 1);
4591 
4592  bad_ffi_type:
4593     PyErr_Format(PyExc_NotImplementedError,
4594                  "primitive type '%s' has size %d; "
4595                  "the supported sizes are 1, 2, 4, 8",
4596                  name, (int)ptypes->size);
4597     return NULL;
4598 }
4599 
b_new_primitive_type(PyObject * self,PyObject * args)4600 static PyObject *b_new_primitive_type(PyObject *self, PyObject *args)
4601 {
4602     char *name;
4603     if (!PyArg_ParseTuple(args, "s:new_primitive_type", &name))
4604         return NULL;
4605     return new_primitive_type(name);
4606 }
4607 
new_pointer_type(CTypeDescrObject * ctitem)4608 static PyObject *new_pointer_type(CTypeDescrObject *ctitem)
4609 {
4610     CTypeDescrObject *td;
4611     const char *extra;
4612     const void *unique_key[1];
4613 
4614     if (ctitem->ct_flags & CT_ARRAY)
4615         extra = "(*)";   /* obscure case: see test_array_add */
4616     else
4617         extra = " *";
4618     td = ctypedescr_new_on_top(ctitem, extra, 2);
4619     if (td == NULL)
4620         return NULL;
4621 
4622     td->ct_size = sizeof(void *);
4623     td->ct_length = -1;
4624     td->ct_flags = CT_POINTER;
4625     if (ctitem->ct_flags & (CT_STRUCT|CT_UNION))
4626         td->ct_flags |= CT_IS_PTR_TO_OWNED;
4627     if (ctitem->ct_flags & CT_VOID)
4628         td->ct_flags |= CT_IS_VOID_PTR;
4629     if ((ctitem->ct_flags & CT_VOID) ||
4630         ((ctitem->ct_flags & CT_PRIMITIVE_CHAR) &&
4631          ctitem->ct_size == sizeof(char)))
4632         td->ct_flags |= CT_IS_VOIDCHAR_PTR;   /* 'void *' or 'char *' only */
4633     unique_key[0] = ctitem;
4634     return get_unique_type(td, unique_key, 1);
4635 }
4636 
b_new_pointer_type(PyObject * self,PyObject * args)4637 static PyObject *b_new_pointer_type(PyObject *self, PyObject *args)
4638 {
4639     CTypeDescrObject *ctitem;
4640     if (!PyArg_ParseTuple(args, "O!:new_pointer_type",
4641                           &CTypeDescr_Type, &ctitem))
4642         return NULL;
4643     return new_pointer_type(ctitem);
4644 }
4645 
b_new_array_type(PyObject * self,PyObject * args)4646 static PyObject *b_new_array_type(PyObject *self, PyObject *args)
4647 {
4648     PyObject *lengthobj;
4649     Py_ssize_t length;
4650     CTypeDescrObject *ctptr;
4651 
4652     if (!PyArg_ParseTuple(args, "O!O:new_array_type",
4653                           &CTypeDescr_Type, &ctptr, &lengthobj))
4654         return NULL;
4655 
4656     if (lengthobj == Py_None) {
4657         length = -1;
4658     }
4659     else {
4660         length = PyNumber_AsSsize_t(lengthobj, PyExc_OverflowError);
4661         if (length < 0) {
4662             if (!PyErr_Occurred())
4663                 PyErr_SetString(PyExc_ValueError, "negative array length");
4664             return NULL;
4665         }
4666     }
4667     return new_array_type(ctptr, length);
4668 }
4669 
4670 static PyObject *
new_array_type(CTypeDescrObject * ctptr,Py_ssize_t length)4671 new_array_type(CTypeDescrObject *ctptr, Py_ssize_t length)
4672 {
4673     CTypeDescrObject *td, *ctitem;
4674     char extra_text[32];
4675     Py_ssize_t arraysize;
4676     int flags = CT_ARRAY;
4677     const void *unique_key[2];
4678 
4679     if (!(ctptr->ct_flags & CT_POINTER)) {
4680         PyErr_SetString(PyExc_TypeError, "first arg must be a pointer ctype");
4681         return NULL;
4682     }
4683     ctitem = ctptr->ct_itemdescr;
4684     if (ctitem->ct_size < 0) {
4685         PyErr_Format(PyExc_ValueError, "array item of unknown size: '%s'",
4686                      ctitem->ct_name);
4687         return NULL;
4688     }
4689 
4690     if (length < 0) {
4691         sprintf(extra_text, "[]");
4692         length = -1;
4693         arraysize = -1;
4694     }
4695     else {
4696         sprintf(extra_text, "[%llu]", (unsigned PY_LONG_LONG)length);
4697         arraysize = MUL_WRAPAROUND(length, ctitem->ct_size);
4698         if (length > 0 && (arraysize / length) != ctitem->ct_size) {
4699             PyErr_SetString(PyExc_OverflowError,
4700                             "array size would overflow a Py_ssize_t");
4701             return NULL;
4702         }
4703     }
4704     td = ctypedescr_new_on_top(ctitem, extra_text, 0);
4705     if (td == NULL)
4706         return NULL;
4707 
4708     Py_INCREF(ctptr);
4709     td->ct_stuff = (PyObject *)ctptr;
4710     td->ct_size = arraysize;
4711     td->ct_length = length;
4712     td->ct_flags = flags;
4713     unique_key[0] = ctptr;
4714     unique_key[1] = (void *)length;
4715     return get_unique_type(td, unique_key, 2);
4716 }
4717 
new_void_type(void)4718 static PyObject *new_void_type(void)
4719 {
4720     int name_size = strlen("void") + 1;
4721     const void *unique_key[1];
4722     CTypeDescrObject *td = ctypedescr_new(name_size);
4723     if (td == NULL)
4724         return NULL;
4725 
4726     memcpy(td->ct_name, "void", name_size);
4727     td->ct_size = -1;
4728     td->ct_flags = CT_VOID | CT_IS_OPAQUE;
4729     td->ct_name_position = strlen("void");
4730     unique_key[0] = "void";
4731     return get_unique_type(td, unique_key, 1);
4732 }
4733 
b_new_void_type(PyObject * self,PyObject * args)4734 static PyObject *b_new_void_type(PyObject *self, PyObject *args)
4735 {
4736     return new_void_type();
4737 }
4738 
new_struct_or_union_type(const char * name,int flag)4739 static PyObject *new_struct_or_union_type(const char *name, int flag)
4740 {
4741     int namelen = strlen(name);
4742     CTypeDescrObject *td = ctypedescr_new(namelen + 1);
4743     if (td == NULL)
4744         return NULL;
4745 
4746     td->ct_size = -1;
4747     td->ct_length = -1;
4748     td->ct_flags = flag | CT_IS_OPAQUE;
4749     td->ct_extra = NULL;
4750     memcpy(td->ct_name, name, namelen + 1);
4751     td->ct_name_position = namelen;
4752     return (PyObject *)td;
4753 }
4754 
b_new_struct_type(PyObject * self,PyObject * args)4755 static PyObject *b_new_struct_type(PyObject *self, PyObject *args)
4756 {
4757     char *name;
4758     int flag;
4759     if (!PyArg_ParseTuple(args, "s:new_struct_type", &name))
4760         return NULL;
4761 
4762     flag = CT_STRUCT;
4763     if (strcmp(name, "struct _IO_FILE") == 0 || strcmp(name, "FILE") == 0)
4764         flag |= CT_IS_FILE;
4765     return new_struct_or_union_type(name, flag);
4766 }
4767 
b_new_union_type(PyObject * self,PyObject * args)4768 static PyObject *b_new_union_type(PyObject *self, PyObject *args)
4769 {
4770     char *name;
4771     if (!PyArg_ParseTuple(args, "s:new_union_type", &name))
4772         return NULL;
4773     return new_struct_or_union_type(name, CT_UNION);
4774 }
4775 
4776 static CFieldObject *
_add_field(PyObject * interned_fields,PyObject * fname,CTypeDescrObject * ftype,Py_ssize_t offset,int bitshift,int fbitsize,int flags)4777 _add_field(PyObject *interned_fields, PyObject *fname, CTypeDescrObject *ftype,
4778            Py_ssize_t offset, int bitshift, int fbitsize, int flags)
4779 {
4780     int err;
4781     Py_ssize_t prev_size;
4782     CFieldObject *cf = PyObject_New(CFieldObject, &CField_Type);
4783     if (cf == NULL)
4784         return NULL;
4785 
4786     Py_INCREF(ftype);
4787     cf->cf_type = ftype;
4788     cf->cf_offset = offset;
4789     cf->cf_bitshift = bitshift;
4790     cf->cf_bitsize = fbitsize;
4791     cf->cf_flags = flags;
4792 
4793     Py_INCREF(fname);
4794     PyText_InternInPlace(&fname);
4795     prev_size = PyDict_Size(interned_fields);
4796     err = PyDict_SetItem(interned_fields, fname, (PyObject *)cf);
4797     Py_DECREF(fname);
4798     Py_DECREF(cf);
4799     if (err < 0)
4800         return NULL;
4801 
4802     if (PyDict_Size(interned_fields) != prev_size + 1) {
4803         PyErr_Format(PyExc_KeyError, "duplicate field name '%s'",
4804                      PyText_AS_UTF8(fname));
4805         return NULL;
4806     }
4807     return cf;   /* borrowed reference */
4808 }
4809 
4810 #define SF_MSVC_BITFIELDS     0x01
4811 #define SF_GCC_ARM_BITFIELDS  0x02
4812 #define SF_GCC_X86_BITFIELDS  0x10
4813 
4814 #define SF_GCC_BIG_ENDIAN     0x04
4815 #define SF_GCC_LITTLE_ENDIAN  0x40
4816 
4817 #define SF_PACKED             0x08
4818 #define SF_STD_FIELD_POS      0x80
4819 
4820 #ifdef MS_WIN32
4821 #  define SF_DEFAULT_PACKING     8
4822 #else
4823 #  define SF_DEFAULT_PACKING   0x40000000   /* a huge power of two */
4824 #endif
4825 
complete_sflags(int sflags)4826 static int complete_sflags(int sflags)
4827 {
4828     /* add one of the SF_xxx_BITFIELDS flags if none is specified */
4829     if (!(sflags & (SF_MSVC_BITFIELDS | SF_GCC_ARM_BITFIELDS |
4830                     SF_GCC_X86_BITFIELDS))) {
4831 #ifdef MS_WIN32
4832         sflags |= SF_MSVC_BITFIELDS;
4833 #else
4834 # if defined(__arm__) || defined(__aarch64__)
4835         sflags |= SF_GCC_ARM_BITFIELDS;
4836 # else
4837         sflags |= SF_GCC_X86_BITFIELDS;
4838 # endif
4839 #endif
4840     }
4841     /* add one of SF_GCC_xx_ENDIAN if none is specified */
4842     if (!(sflags & (SF_GCC_BIG_ENDIAN | SF_GCC_LITTLE_ENDIAN))) {
4843         int _check_endian = 1;
4844         if (*(char *)&_check_endian == 0)
4845             sflags |= SF_GCC_BIG_ENDIAN;
4846         else
4847             sflags |= SF_GCC_LITTLE_ENDIAN;
4848     }
4849     return sflags;
4850 }
4851 
detect_custom_layout(CTypeDescrObject * ct,int sflags,Py_ssize_t cdef_value,Py_ssize_t compiler_value,const char * msg1,const char * txt,const char * msg2)4852 static int detect_custom_layout(CTypeDescrObject *ct, int sflags,
4853                                 Py_ssize_t cdef_value,
4854                                 Py_ssize_t compiler_value,
4855                                 const char *msg1, const char *txt,
4856                                 const char *msg2)
4857 {
4858     if (compiler_value != cdef_value) {
4859         if (sflags & SF_STD_FIELD_POS) {
4860             PyErr_Format(FFIError,
4861                          "%s: %s%s%s (cdef says %zd, but C compiler says %zd)."
4862                          " fix it or use \"...;\" in the cdef for %s to "
4863                          "make it flexible",
4864                          ct->ct_name, msg1, txt, msg2,
4865                          cdef_value, compiler_value,
4866                          ct->ct_name);
4867             return -1;
4868         }
4869         ct->ct_flags |= CT_CUSTOM_FIELD_POS;
4870     }
4871     return 0;
4872 }
4873 
b_complete_struct_or_union(PyObject * self,PyObject * args)4874 static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args)
4875 {
4876     CTypeDescrObject *ct;
4877     PyObject *fields, *interned_fields, *ignored;
4878     int is_union, alignment;
4879     Py_ssize_t boffset, i, nb_fields, boffsetmax, alignedsize, boffsetorg;
4880     Py_ssize_t totalsize = -1;
4881     int totalalignment = -1;
4882     CFieldObject **previous;
4883     int prev_bitfield_size, prev_bitfield_free;
4884     int sflags = 0, fflags;
4885     int pack = 0;
4886 
4887     if (!PyArg_ParseTuple(args, "O!O!|Oniii:complete_struct_or_union",
4888                           &CTypeDescr_Type, &ct,
4889                           &PyList_Type, &fields,
4890                           &ignored, &totalsize, &totalalignment, &sflags,
4891                           &pack))
4892         return NULL;
4893 
4894     sflags = complete_sflags(sflags);
4895     if (sflags & SF_PACKED)
4896         pack = 1;
4897     else if (pack <= 0)
4898         pack = SF_DEFAULT_PACKING;
4899     else
4900         sflags |= SF_PACKED;
4901 
4902     if ((ct->ct_flags & (CT_STRUCT|CT_IS_OPAQUE)) ==
4903                         (CT_STRUCT|CT_IS_OPAQUE)) {
4904         is_union = 0;
4905     }
4906     else if ((ct->ct_flags & (CT_UNION|CT_IS_OPAQUE)) ==
4907                              (CT_UNION|CT_IS_OPAQUE)) {
4908         is_union = 1;
4909     }
4910     else {
4911         PyErr_SetString(PyExc_TypeError,
4912                   "first arg must be a non-initialized struct or union ctype");
4913         return NULL;
4914     }
4915     ct->ct_flags &= ~(CT_CUSTOM_FIELD_POS | CT_WITH_PACKED_CHANGE);
4916 
4917     alignment = 1;
4918     boffset = 0;         /* this number is in *bits*, not bytes! */
4919     boffsetmax = 0;      /* the maximum value of boffset, in bits too */
4920     prev_bitfield_size = 0;
4921     prev_bitfield_free = 0;
4922     nb_fields = PyList_GET_SIZE(fields);
4923     interned_fields = PyDict_New();
4924     if (interned_fields == NULL)
4925         return NULL;
4926 
4927     previous = (CFieldObject **)&ct->ct_extra;
4928 
4929     for (i=0; i<nb_fields; i++) {
4930         PyObject *fname;
4931         CTypeDescrObject *ftype;
4932         int fbitsize = -1, falign, falignorg, do_align;
4933         Py_ssize_t foffset = -1;
4934 
4935         if (!PyArg_ParseTuple(PyList_GET_ITEM(fields, i), "O!O!|in:list item",
4936                               &PyText_Type, &fname,
4937                               &CTypeDescr_Type, &ftype,
4938                               &fbitsize, &foffset))
4939             goto error;
4940 
4941         if (ftype->ct_size < 0) {
4942             if ((ftype->ct_flags & CT_ARRAY) && fbitsize < 0
4943                     && (i == nb_fields - 1 || foffset != -1)) {
4944                 ct->ct_flags |= CT_WITH_VAR_ARRAY;
4945             }
4946             else {
4947                 PyErr_Format(PyExc_TypeError,
4948                              "field '%s.%s' has ctype '%s' of unknown size",
4949                              ct->ct_name, PyText_AS_UTF8(fname),
4950                              ftype->ct_name);
4951                 goto error;
4952             }
4953         }
4954 
4955         if (is_union)
4956             boffset = 0;   /* reset each field at offset 0 */
4957 
4958         /* update the total alignment requirement, but skip it if the
4959            field is an anonymous bitfield or if SF_PACKED */
4960         falignorg = get_alignment(ftype);
4961         if (falignorg < 0)
4962             goto error;
4963         falign = (pack < falignorg) ? pack : falignorg;
4964 
4965         do_align = 1;
4966         if (!(sflags & SF_GCC_ARM_BITFIELDS) && fbitsize >= 0) {
4967             if (!(sflags & SF_MSVC_BITFIELDS)) {
4968                 /* GCC: anonymous bitfields (of any size) don't cause alignment */
4969                 do_align = PyText_GetSize(fname) > 0;
4970             }
4971             else {
4972                 /* MSVC: zero-sized bitfields don't cause alignment */
4973                 do_align = fbitsize > 0;
4974             }
4975         }
4976         if (alignment < falign && do_align)
4977             alignment = falign;
4978 
4979         fflags = (is_union && i > 0) ? BF_IGNORE_IN_CTOR : 0;
4980 
4981         if (fbitsize < 0) {
4982             /* not a bitfield: common case */
4983             int bs_flag;
4984 
4985             if ((ftype->ct_flags & CT_ARRAY) && ftype->ct_length <= 0)
4986                 bs_flag = BS_EMPTY_ARRAY;
4987             else
4988                 bs_flag = BS_REGULAR;
4989 
4990             /* align this field to its own 'falign' by inserting padding */
4991             boffsetorg = (boffset + falignorg*8-1) & ~(falignorg*8-1); /*bits!*/
4992             boffset = (boffset + falign*8-1) & ~(falign*8-1); /* bits! */
4993             if (boffsetorg != boffset) {
4994                 ct->ct_flags |= CT_WITH_PACKED_CHANGE;
4995             }
4996 
4997             if (foffset >= 0) {
4998                 /* a forced field position: ignore the offset just computed,
4999                    except to know if we must set CT_CUSTOM_FIELD_POS */
5000                 if (detect_custom_layout(ct, sflags, boffset / 8, foffset,
5001                                          "wrong offset for field '",
5002                                          PyText_AS_UTF8(fname), "'") < 0)
5003                     goto error;
5004                 boffset = foffset * 8;
5005             }
5006 
5007             if (PyText_GetSize(fname) == 0 &&
5008                     ftype->ct_flags & (CT_STRUCT|CT_UNION)) {
5009                 /* a nested anonymous struct or union */
5010                 CFieldObject *cfsrc = (CFieldObject *)ftype->ct_extra;
5011                 for (; cfsrc != NULL; cfsrc = cfsrc->cf_next) {
5012                     /* broken complexity in the call to get_field_name(),
5013                        but we'll assume you never do that with nested
5014                        anonymous structures with thousand of fields */
5015                     *previous = _add_field(interned_fields,
5016                                            get_field_name(ftype, cfsrc),
5017                                            cfsrc->cf_type,
5018                                            boffset / 8 + cfsrc->cf_offset,
5019                                            cfsrc->cf_bitshift,
5020                                            cfsrc->cf_bitsize,
5021                                            cfsrc->cf_flags | fflags);
5022                     if (*previous == NULL)
5023                         goto error;
5024                     previous = &(*previous)->cf_next;
5025                 }
5026                 /* always forbid such structures from being passed by value */
5027                 ct->ct_flags |= CT_CUSTOM_FIELD_POS;
5028             }
5029             else {
5030                 *previous = _add_field(interned_fields, fname, ftype,
5031                                         boffset / 8, bs_flag, -1, fflags);
5032                 if (*previous == NULL)
5033                     goto error;
5034                 previous = &(*previous)->cf_next;
5035             }
5036             if (ftype->ct_size >= 0)
5037                 boffset += ftype->ct_size * 8;
5038             prev_bitfield_size = 0;
5039         }
5040         else {
5041             /* this is the case of a bitfield */
5042             Py_ssize_t field_offset_bytes;
5043             int bits_already_occupied, bitshift;
5044 
5045             if (foffset >= 0) {
5046                 PyErr_Format(PyExc_TypeError,
5047                              "field '%s.%s' is a bitfield, "
5048                              "but a fixed offset is specified",
5049                              ct->ct_name, PyText_AS_UTF8(fname));
5050                 goto error;
5051             }
5052 
5053             if (!(ftype->ct_flags & (CT_PRIMITIVE_SIGNED |
5054                                      CT_PRIMITIVE_UNSIGNED |
5055                                      CT_PRIMITIVE_CHAR))) {
5056                 PyErr_Format(PyExc_TypeError,
5057                         "field '%s.%s' declared as '%s' cannot be a bit field",
5058                              ct->ct_name, PyText_AS_UTF8(fname),
5059                              ftype->ct_name);
5060                 goto error;
5061             }
5062             if (fbitsize > 8 * ftype->ct_size) {
5063                 PyErr_Format(PyExc_TypeError,
5064                              "bit field '%s.%s' is declared '%s:%d', which "
5065                              "exceeds the width of the type",
5066                              ct->ct_name, PyText_AS_UTF8(fname),
5067                              ftype->ct_name, fbitsize);
5068                 goto error;
5069             }
5070 
5071             /* compute the starting position of the theoretical field
5072                that covers a complete 'ftype', inside of which we will
5073                locate the real bitfield */
5074             field_offset_bytes = boffset / 8;
5075             field_offset_bytes &= ~(falign - 1);
5076 
5077             if (fbitsize == 0) {
5078                 if (PyText_GetSize(fname) > 0) {
5079                     PyErr_Format(PyExc_TypeError,
5080                                  "field '%s.%s' is declared with :0",
5081                                  ct->ct_name, PyText_AS_UTF8(fname));
5082                     goto error;
5083                 }
5084                 if (!(sflags & SF_MSVC_BITFIELDS)) {
5085                     /* GCC's notion of "ftype :0;" */
5086 
5087                     /* pad boffset to a value aligned for "ftype" */
5088                     if (boffset > field_offset_bytes * 8) {
5089                         field_offset_bytes += falign;
5090                         assert(boffset < field_offset_bytes * 8);
5091                     }
5092                     boffset = field_offset_bytes * 8;
5093                 }
5094                 else {
5095                     /* MSVC's notion of "ftype :0;" */
5096 
5097                     /* Mostly ignored.  It seems they only serve as
5098                        separator between other bitfields, to force them
5099                        into separate words. */
5100                 }
5101                 prev_bitfield_size = 0;
5102             }
5103             else {
5104                 if (!(sflags & SF_MSVC_BITFIELDS)) {
5105                     /* GCC's algorithm */
5106 
5107                     /* Can the field start at the offset given by 'boffset'?  It
5108                        can if it would entirely fit into an aligned ftype field. */
5109                     bits_already_occupied = boffset - (field_offset_bytes * 8);
5110 
5111                     if (bits_already_occupied + fbitsize > 8 * ftype->ct_size) {
5112                         /* it would not fit, we need to start at the next
5113                            allowed position */
5114                         if ((sflags & SF_PACKED) &&
5115                             (bits_already_occupied & 7)) {
5116                             PyErr_Format(PyExc_NotImplementedError,
5117                                 "with 'packed', gcc would compile field "
5118                                 "'%s.%s' to reuse some bits in the previous "
5119                                 "field", ct->ct_name, PyText_AS_UTF8(fname));
5120                             goto error;
5121                         }
5122                         field_offset_bytes += falign;
5123                         assert(boffset < field_offset_bytes * 8);
5124                         boffset = field_offset_bytes * 8;
5125                         bitshift = 0;
5126                     }
5127                     else {
5128                         bitshift = bits_already_occupied;
5129                         assert(bitshift >= 0);
5130                     }
5131                     boffset += fbitsize;
5132                 }
5133                 else {
5134                     /* MSVC's algorithm */
5135 
5136                     /* A bitfield is considered as taking the full width
5137                        of their declared type.  It can share some bits
5138                        with the previous field only if it was also a
5139                        bitfield and used a type of the same size. */
5140                     if (prev_bitfield_size == ftype->ct_size &&
5141                         prev_bitfield_free >= fbitsize) {
5142                         /* yes: reuse */
5143                         bitshift = 8 * prev_bitfield_size - prev_bitfield_free;
5144                     }
5145                     else {
5146                         /* no: start a new full field */
5147                         boffset = (boffset + falign*8-1) & ~(falign*8-1); /*align*/
5148                         boffset += ftype->ct_size * 8;
5149                         bitshift = 0;
5150                         prev_bitfield_size = ftype->ct_size;
5151                         prev_bitfield_free = 8 * prev_bitfield_size;
5152                     }
5153                     prev_bitfield_free -= fbitsize;
5154                     field_offset_bytes = boffset / 8 - ftype->ct_size;
5155                 }
5156 
5157                 if (sflags & SF_GCC_BIG_ENDIAN)
5158                     bitshift = 8 * ftype->ct_size - fbitsize - bitshift;
5159 
5160                 *previous = _add_field(interned_fields, fname, ftype,
5161                                        field_offset_bytes, bitshift, fbitsize,
5162                                        fflags);
5163                 if (*previous == NULL)
5164                     goto error;
5165                 previous = &(*previous)->cf_next;
5166             }
5167         }
5168 
5169         if (boffset > boffsetmax)
5170             boffsetmax = boffset;
5171     }
5172     *previous = NULL;
5173 
5174     /* Like C, if the size of this structure would be zero, we compute it
5175        as 1 instead.  But for ctypes support, we allow the manually-
5176        specified totalsize to be zero in this case. */
5177     boffsetmax = (boffsetmax + 7) / 8;        /* bits -> bytes */
5178     alignedsize = (boffsetmax + alignment - 1) & ~(alignment-1);
5179     if (alignedsize == 0)
5180         alignedsize = 1;
5181 
5182     if (totalsize < 0) {
5183         totalsize = alignedsize;
5184     }
5185     else {
5186         if (detect_custom_layout(ct, sflags, alignedsize,
5187                                  totalsize, "wrong total size", "", "") < 0)
5188             goto error;
5189         if (totalsize < boffsetmax) {
5190             PyErr_Format(PyExc_TypeError,
5191                          "%s cannot be of size %zd: there are fields at least "
5192                          "up to %zd", ct->ct_name, totalsize, boffsetmax);
5193             goto error;
5194         }
5195     }
5196     if (totalalignment < 0) {
5197         totalalignment = alignment;
5198     }
5199     else {
5200         if (detect_custom_layout(ct, sflags, alignment, totalalignment,
5201                                  "wrong total alignment", "", "") < 0)
5202             goto error;
5203     }
5204 
5205     ct->ct_size = totalsize;
5206     ct->ct_length = totalalignment;
5207     ct->ct_stuff = interned_fields;
5208     ct->ct_flags &= ~CT_IS_OPAQUE;
5209 
5210     Py_INCREF(Py_None);
5211     return Py_None;
5212 
5213  error:
5214     ct->ct_extra = NULL;
5215     Py_DECREF(interned_fields);
5216     return NULL;
5217 }
5218 
5219 struct funcbuilder_s {
5220     Py_ssize_t nb_bytes;
5221     char *bufferp;
5222     ffi_type **atypes;
5223     ffi_type *rtype;
5224     Py_ssize_t nargs;
5225     CTypeDescrObject *fct;
5226 };
5227 
fb_alloc(struct funcbuilder_s * fb,Py_ssize_t size)5228 static void *fb_alloc(struct funcbuilder_s *fb, Py_ssize_t size)
5229 {
5230     if (fb->bufferp == NULL) {
5231         fb->nb_bytes += size;
5232         return NULL;
5233     }
5234     else {
5235         char *result = fb->bufferp;
5236         fb->bufferp += size;
5237         return result;
5238     }
5239 }
5240 
5241 #define SUPPORTED_IN_API_MODE                                            \
5242         " are only supported as %s if the function is "                  \
5243         "'API mode' and non-variadic (i.e. declared inside ffibuilder"   \
5244         ".cdef()+ffibuilder.set_source() and not taking a final '...' "  \
5245         "argument)"
5246 
fb_unsupported(CTypeDescrObject * ct,const char * place,const char * detail)5247 static ffi_type *fb_unsupported(CTypeDescrObject *ct, const char *place,
5248                                 const char *detail)
5249 {
5250     PyErr_Format(PyExc_NotImplementedError,
5251         "ctype '%s' not supported as %s.  %s.  "
5252         "Such structs" SUPPORTED_IN_API_MODE,
5253         ct->ct_name, place, detail, place);
5254     return NULL;
5255 }
5256 
fb_fill_type(struct funcbuilder_s * fb,CTypeDescrObject * ct,int is_result_type)5257 static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct,
5258                               int is_result_type)
5259 {
5260     const char *place = is_result_type ? "return value" : "argument";
5261 
5262     if (ct->ct_flags & (CT_PRIMITIVE_ANY & ~CT_PRIMITIVE_COMPLEX)) {
5263         return (ffi_type *)ct->ct_extra;
5264     }
5265     else if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) {
5266         return &ffi_type_pointer;
5267     }
5268     else if ((ct->ct_flags & CT_VOID) && is_result_type) {
5269         return &ffi_type_void;
5270     }
5271 
5272     if (ct->ct_size <= 0) {
5273         PyErr_Format(PyExc_TypeError,
5274                      ct->ct_size < 0 ? "ctype '%s' has incomplete type"
5275                                      : "ctype '%s' has size 0",
5276                      ct->ct_name);
5277         return NULL;
5278     }
5279     if (ct->ct_flags & CT_STRUCT) {
5280         ffi_type *ffistruct, *ffifield;
5281         ffi_type **elements;
5282         Py_ssize_t i, n, nflat;
5283         CFieldObject *cf;
5284 
5285         /* We can't pass a struct that was completed by verify().
5286            Issue: assume verify() is given "struct { long b; ...; }".
5287            Then it will complete it in the same way whether it is actually
5288            "struct { long a, b; }" or "struct { double a; long b; }".
5289            But on 64-bit UNIX, these two structs are passed by value
5290            differently: e.g. on x86-64, "b" ends up in register "rsi" in
5291            the first case and "rdi" in the second case.
5292 
5293            Another reason for CT_CUSTOM_FIELD_POS would be anonymous
5294            nested structures: we lost the information about having it
5295            here, so better safe (and forbid it) than sorry (and maybe
5296            crash).  Note: it seems we only get in this case with
5297            ffi.verify().
5298         */
5299         if (force_lazy_struct(ct) < 0)
5300             return NULL;
5301         if (ct->ct_flags & CT_CUSTOM_FIELD_POS) {
5302             /* these NotImplementedErrors may be caught and ignored until
5303                a real call is made to a function of this type */
5304             return fb_unsupported(ct, place,
5305                 "It is a struct declared with \"...;\", but the C "
5306                 "calling convention may depend on the missing fields; "
5307                 "or, it contains anonymous struct/unions");
5308         }
5309         /* Another reason: __attribute__((packed)) is not supported by libffi.
5310         */
5311         if (ct->ct_flags & CT_WITH_PACKED_CHANGE) {
5312             return fb_unsupported(ct, place,
5313                 "It is a 'packed' structure, with a different layout than "
5314                 "expected by libffi");
5315         }
5316 
5317         n = PyDict_Size(ct->ct_stuff);
5318         nflat = 0;
5319 
5320         /* walk the fields, expanding arrays into repetitions; first,
5321            only count how many flattened fields there are */
5322         cf = (CFieldObject *)ct->ct_extra;
5323         for (i=0; i<n; i++) {
5324             Py_ssize_t flat;
5325             CTypeDescrObject *ct1;
5326             assert(cf != NULL);
5327             if (cf->cf_bitshift >= 0) {
5328                 return fb_unsupported(ct, place,
5329                     "It is a struct with bit fields, which libffi does not "
5330                     "support");
5331             }
5332             flat = 1;
5333             ct1 = cf->cf_type;
5334             while (ct1->ct_flags & CT_ARRAY) {
5335                 flat *= ct1->ct_length;
5336                 ct1 = ct1->ct_itemdescr;
5337             }
5338             if (flat <= 0) {
5339                 return fb_unsupported(ct, place,
5340                     "It is a struct with a zero-length array, which libffi "
5341                     "does not support");
5342             }
5343             nflat += flat;
5344             cf = cf->cf_next;
5345         }
5346         assert(cf == NULL);
5347 
5348         /* next, allocate and fill the flattened list */
5349         elements = fb_alloc(fb, (nflat + 1) * sizeof(ffi_type*));
5350         nflat = 0;
5351         cf = (CFieldObject *)ct->ct_extra;
5352         for (i=0; i<n; i++) {
5353             Py_ssize_t j, flat = 1;
5354             CTypeDescrObject *ct = cf->cf_type;
5355             while (ct->ct_flags & CT_ARRAY) {
5356                 flat *= ct->ct_length;
5357                 ct = ct->ct_itemdescr;
5358             }
5359             ffifield = fb_fill_type(fb, ct, 0);
5360             if (PyErr_Occurred())
5361                 return NULL;
5362             if (elements != NULL) {
5363                 for (j=0; j<flat; j++)
5364                     elements[nflat++] = ffifield;
5365             }
5366             cf = cf->cf_next;
5367         }
5368 
5369         /* finally, allocate the FFI_TYPE_STRUCT */
5370         ffistruct = fb_alloc(fb, sizeof(ffi_type));
5371         if (ffistruct != NULL) {
5372             elements[nflat] = NULL;
5373             ffistruct->size = ct->ct_size;
5374             ffistruct->alignment = ct->ct_length;
5375             ffistruct->type = FFI_TYPE_STRUCT;
5376             ffistruct->elements = elements;
5377         }
5378         return ffistruct;
5379     }
5380     else if (ct->ct_flags & CT_UNION) {
5381         PyErr_Format(PyExc_NotImplementedError,
5382                      "ctype '%s' not supported as %s by libffi.  "
5383                      "Unions" SUPPORTED_IN_API_MODE,
5384                      ct->ct_name, place, place);
5385         return NULL;
5386     }
5387     else {
5388         char *extra = "";
5389         if (ct->ct_flags & CT_PRIMITIVE_COMPLEX)
5390             extra = " (the support for complex types inside libffi "
5391                     "is mostly missing at this point, so CFFI only "
5392                     "supports complex types as arguments or return "
5393                     "value in API-mode functions)";
5394 
5395         PyErr_Format(PyExc_NotImplementedError,
5396                      "ctype '%s' (size %zd) not supported as %s%s",
5397                      ct->ct_name, ct->ct_size, place, extra);
5398         return NULL;
5399     }
5400 }
5401 
5402 #define ALIGN_ARG(n)  ((n) + 7) & ~7
5403 
fb_build(struct funcbuilder_s * fb,PyObject * fargs,CTypeDescrObject * fresult)5404 static int fb_build(struct funcbuilder_s *fb, PyObject *fargs,
5405                     CTypeDescrObject *fresult)
5406 {
5407     Py_ssize_t i, nargs = PyTuple_GET_SIZE(fargs);
5408     Py_ssize_t exchange_offset;
5409     cif_description_t *cif_descr;
5410 
5411     /* ffi buffer: start with a cif_description */
5412     cif_descr = fb_alloc(fb, sizeof(cif_description_t) +
5413                              nargs * sizeof(Py_ssize_t));
5414 
5415     /* ffi buffer: next comes an array of 'ffi_type*', one per argument */
5416     fb->atypes = fb_alloc(fb, nargs * sizeof(ffi_type*));
5417     fb->nargs = nargs;
5418 
5419     /* ffi buffer: next comes the result type */
5420     fb->rtype = fb_fill_type(fb, fresult, 1);
5421     if (PyErr_Occurred())
5422         return -1;
5423     if (cif_descr != NULL) {
5424         /* exchange data size */
5425         /* first, enough room for an array of 'nargs' pointers */
5426         exchange_offset = nargs * sizeof(void*);
5427         exchange_offset = ALIGN_ARG(exchange_offset);
5428         cif_descr->exchange_offset_arg[0] = exchange_offset;
5429         /* then enough room for the result --- which means at least
5430            sizeof(ffi_arg), according to the ffi docs */
5431         i = fb->rtype->size;
5432         if (i < (Py_ssize_t)sizeof(ffi_arg))
5433             i = sizeof(ffi_arg);
5434         exchange_offset += i;
5435     }
5436     else
5437         exchange_offset = 0;   /* not used */
5438 
5439     /* loop over the arguments */
5440     for (i=0; i<nargs; i++) {
5441         CTypeDescrObject *farg;
5442         ffi_type *atype;
5443 
5444         farg = (CTypeDescrObject *)PyTuple_GET_ITEM(fargs, i);
5445         /* convert arrays to pointers */
5446         if (farg->ct_flags & CT_ARRAY)
5447             farg = (CTypeDescrObject *)farg->ct_stuff;
5448 
5449         /* ffi buffer: fill in the ffi for the i'th argument */
5450         assert(farg != NULL);
5451         atype = fb_fill_type(fb, farg, 0);
5452         if (PyErr_Occurred())
5453             return -1;
5454 
5455         if (fb->atypes != NULL) {
5456             fb->atypes[i] = atype;
5457             /* exchange data size */
5458             exchange_offset = ALIGN_ARG(exchange_offset);
5459             cif_descr->exchange_offset_arg[1 + i] = exchange_offset;
5460             exchange_offset += atype->size;
5461         }
5462     }
5463 
5464     if (cif_descr != NULL) {
5465         /* exchange data size */
5466         /* we also align it to the next multiple of 8, in an attempt to
5467            work around bugs(?) of libffi like #241 */
5468         cif_descr->exchange_size = ALIGN_ARG(exchange_offset);
5469     }
5470     return 0;
5471 }
5472 
5473 #undef ALIGN_ARG
5474 
fb_cat_name(struct funcbuilder_s * fb,const char * piece,int piecelen)5475 static void fb_cat_name(struct funcbuilder_s *fb, const char *piece,
5476                         int piecelen)
5477 {
5478     if (fb->bufferp == NULL) {
5479         fb->nb_bytes += piecelen;
5480     }
5481     else {
5482         memcpy(fb->bufferp, piece, piecelen);
5483         fb->bufferp += piecelen;
5484     }
5485 }
5486 
fb_build_name(struct funcbuilder_s * fb,const char * repl,CTypeDescrObject ** pfargs,Py_ssize_t nargs,CTypeDescrObject * fresult,int ellipsis)5487 static int fb_build_name(struct funcbuilder_s *fb, const char *repl,
5488                          CTypeDescrObject **pfargs, Py_ssize_t nargs,
5489                          CTypeDescrObject *fresult, int ellipsis)
5490 {
5491     Py_ssize_t i;
5492     fb->nargs = nargs;
5493 
5494     /* name: the function type name we build here is, like in C, made
5495        as follows:
5496 
5497          RESULT_TYPE_HEAD (*)(ARG_1_TYPE, ARG_2_TYPE, etc) RESULT_TYPE_TAIL
5498     */
5499     fb_cat_name(fb, fresult->ct_name, fresult->ct_name_position);
5500     if (repl[0] != '(' &&
5501         fresult->ct_name[fresult->ct_name_position - 1] != '*')
5502         fb_cat_name(fb, " ", 1);   /* add a space */
5503     fb_cat_name(fb, repl, strlen(repl));
5504     if (fb->fct) {
5505         i = strlen(repl) - 1;      /* between '(*' and ')' */
5506         assert(repl[i] == ')');
5507         fb->fct->ct_name_position = fresult->ct_name_position + i;
5508     }
5509     fb_cat_name(fb, "(", 1);
5510 
5511     /* loop over the arguments */
5512     for (i=0; i<nargs; i++) {
5513         CTypeDescrObject *farg;
5514 
5515         farg = pfargs[i];
5516         if (!CTypeDescr_Check(farg)) {
5517             PyErr_SetString(PyExc_TypeError, "expected a tuple of ctypes");
5518             return -1;
5519         }
5520         /* name: concatenate the name of the i'th argument's type */
5521         if (i > 0)
5522             fb_cat_name(fb, ", ", 2);
5523         fb_cat_name(fb, farg->ct_name, strlen(farg->ct_name));
5524     }
5525 
5526     /* name: add the '...' if needed */
5527     if (ellipsis) {
5528         if (nargs > 0)
5529             fb_cat_name(fb, ", ", 2);
5530         fb_cat_name(fb, "...", 3);
5531     }
5532 
5533     /* name: concatenate the tail of the result type */
5534     fb_cat_name(fb, ")", 1);
5535     fb_cat_name(fb, fresult->ct_name + fresult->ct_name_position,
5536                 strlen(fresult->ct_name) - fresult->ct_name_position + 1);
5537     return 0;
5538 }
5539 
fb_prepare_ctype(struct funcbuilder_s * fb,PyObject * fargs,CTypeDescrObject * fresult,int ellipsis,int fabi)5540 static CTypeDescrObject *fb_prepare_ctype(struct funcbuilder_s *fb,
5541                                           PyObject *fargs,
5542                                           CTypeDescrObject *fresult,
5543                                           int ellipsis, int fabi)
5544 {
5545     CTypeDescrObject *fct, **pfargs;
5546     Py_ssize_t nargs;
5547     char *repl = "(*)";
5548 
5549     fb->nb_bytes = 0;
5550     fb->bufferp = NULL;
5551     fb->fct = NULL;
5552 
5553     pfargs = (CTypeDescrObject **)&PyTuple_GET_ITEM(fargs, 0);
5554     nargs = PyTuple_GET_SIZE(fargs);
5555 #if defined(MS_WIN32) && !defined(_WIN64)
5556     if (fabi == FFI_STDCALL)
5557         repl = "(__stdcall *)";
5558 #endif
5559 
5560     /* compute the total size needed for the name */
5561     if (fb_build_name(fb, repl, pfargs, nargs, fresult, ellipsis) < 0)
5562         return NULL;
5563 
5564     /* allocate the function type */
5565     fct = ctypedescr_new(fb->nb_bytes);
5566     if (fct == NULL)
5567         return NULL;
5568     fb->fct = fct;
5569 
5570     /* call again fb_build_name() to really build the ct_name */
5571     fb->bufferp = fct->ct_name;
5572     if (fb_build_name(fb, repl, pfargs, nargs, fresult, ellipsis) < 0)
5573         goto error;
5574     assert(fb->bufferp == fct->ct_name + fb->nb_bytes);
5575 
5576     fct->ct_extra = NULL;
5577     fct->ct_size = sizeof(void(*)(void));
5578     fct->ct_flags = CT_FUNCTIONPTR;
5579     return fct;
5580 
5581  error:
5582     Py_DECREF(fct);
5583     return NULL;
5584 }
5585 
fb_prepare_cif(PyObject * fargs,CTypeDescrObject * fresult,ffi_abi fabi)5586 static cif_description_t *fb_prepare_cif(PyObject *fargs,
5587                                          CTypeDescrObject *fresult,
5588                                          ffi_abi fabi)
5589 {
5590     char *buffer;
5591     cif_description_t *cif_descr;
5592     struct funcbuilder_s funcbuffer;
5593 
5594     funcbuffer.nb_bytes = 0;
5595     funcbuffer.bufferp = NULL;
5596 
5597     /* compute the total size needed in the buffer for libffi */
5598     if (fb_build(&funcbuffer, fargs, fresult) < 0)
5599         return NULL;
5600 
5601     /* allocate the buffer */
5602     buffer = PyObject_Malloc(funcbuffer.nb_bytes);
5603     if (buffer == NULL) {
5604         PyErr_NoMemory();
5605         return NULL;
5606     }
5607 
5608     /* call again fb_build() to really build the libffi data structures */
5609     funcbuffer.bufferp = buffer;
5610     if (fb_build(&funcbuffer, fargs, fresult) < 0)
5611         goto error;
5612     assert(funcbuffer.bufferp == buffer + funcbuffer.nb_bytes);
5613 
5614     cif_descr = (cif_description_t *)buffer;
5615     if (ffi_prep_cif(&cif_descr->cif, fabi, funcbuffer.nargs,
5616                      funcbuffer.rtype, funcbuffer.atypes) != FFI_OK) {
5617         PyErr_SetString(PyExc_SystemError,
5618                         "libffi failed to build this function type");
5619         goto error;
5620     }
5621     return cif_descr;
5622 
5623  error:
5624     PyObject_Free(buffer);
5625     return NULL;
5626 }
5627 
new_function_type(PyObject * fargs,CTypeDescrObject * fresult,int ellipsis,int fabi)5628 static PyObject *new_function_type(PyObject *fargs,   /* tuple */
5629                                    CTypeDescrObject *fresult,
5630                                    int ellipsis, int fabi)
5631 {
5632     PyObject *fabiobj;
5633     CTypeDescrObject *fct;
5634     struct funcbuilder_s funcbuilder;
5635     Py_ssize_t i;
5636     const void **unique_key;
5637 
5638     if ((fresult->ct_size < 0 && !(fresult->ct_flags & CT_VOID)) ||
5639         (fresult->ct_flags & CT_ARRAY)) {
5640         char *msg;
5641         if (fresult->ct_flags & CT_IS_OPAQUE)
5642             msg = "result type '%s' is opaque";
5643         else
5644             msg = "invalid result type: '%s'";
5645         PyErr_Format(PyExc_TypeError, msg, fresult->ct_name);
5646         return NULL;
5647     }
5648 
5649     fct = fb_prepare_ctype(&funcbuilder, fargs, fresult, ellipsis, fabi);
5650     if (fct == NULL)
5651         return NULL;
5652 
5653     if (!ellipsis) {
5654         /* Functions with '...' varargs are stored without a cif_descr
5655            at all.  The cif is computed on every call from the actual
5656            types passed in.  For all other functions, the cif_descr
5657            is computed here. */
5658         cif_description_t *cif_descr;
5659 
5660         cif_descr = fb_prepare_cif(fargs, fresult, fabi);
5661         if (cif_descr == NULL) {
5662             if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) {
5663                 PyErr_Clear();   /* will get the exception if we see an
5664                                     actual call */
5665             }
5666             else
5667                 goto error;
5668         }
5669 
5670         fct->ct_extra = (char *)cif_descr;
5671     }
5672 
5673     /* build the signature, given by a tuple of ctype objects */
5674     fct->ct_stuff = PyTuple_New(2 + funcbuilder.nargs);
5675     if (fct->ct_stuff == NULL)
5676         goto error;
5677     fabiobj = PyInt_FromLong(fabi);
5678     if (fabiobj == NULL)
5679         goto error;
5680     PyTuple_SET_ITEM(fct->ct_stuff, 0, fabiobj);
5681 
5682     Py_INCREF(fresult);
5683     PyTuple_SET_ITEM(fct->ct_stuff, 1, (PyObject *)fresult);
5684     for (i=0; i<funcbuilder.nargs; i++) {
5685         PyObject *o = PyTuple_GET_ITEM(fargs, i);
5686         /* convert arrays into pointers */
5687         if (((CTypeDescrObject *)o)->ct_flags & CT_ARRAY)
5688             o = ((CTypeDescrObject *)o)->ct_stuff;
5689         Py_INCREF(o);
5690         PyTuple_SET_ITEM(fct->ct_stuff, 2 + i, o);
5691     }
5692 
5693     /* [ctresult, ellipsis+abi, num_args, ctargs...] */
5694     unique_key = alloca((3 + funcbuilder.nargs) * sizeof(void *));
5695     unique_key[0] = fresult;
5696     unique_key[1] = (const void *)(Py_ssize_t)((fabi << 1) | !!ellipsis);
5697     unique_key[2] = (const void *)(Py_ssize_t)(funcbuilder.nargs);
5698     for (i=0; i<funcbuilder.nargs; i++)
5699         unique_key[3 + i] = PyTuple_GET_ITEM(fct->ct_stuff, 2 + i);
5700     return get_unique_type(fct, unique_key, 3 + funcbuilder.nargs);
5701 
5702  error:
5703     Py_DECREF(fct);
5704     return NULL;
5705 }
5706 
b_new_function_type(PyObject * self,PyObject * args)5707 static PyObject *b_new_function_type(PyObject *self, PyObject *args)
5708 {
5709     PyObject *fargs;
5710     CTypeDescrObject *fresult;
5711     int ellipsis = 0, fabi = FFI_DEFAULT_ABI;
5712 
5713     if (!PyArg_ParseTuple(args, "O!O!|ii:new_function_type",
5714                           &PyTuple_Type, &fargs,
5715                           &CTypeDescr_Type, &fresult,
5716                           &ellipsis,
5717                           &fabi))
5718         return NULL;
5719 
5720     return new_function_type(fargs, fresult, ellipsis, fabi);
5721 }
5722 
convert_from_object_fficallback(char * result,CTypeDescrObject * ctype,PyObject * pyobj,int encode_result_for_libffi)5723 static int convert_from_object_fficallback(char *result,
5724                                            CTypeDescrObject *ctype,
5725                                            PyObject *pyobj,
5726                                            int encode_result_for_libffi)
5727 {
5728     /* work work work around a libffi irregularity: for integer return
5729        types we have to fill at least a complete 'ffi_arg'-sized result
5730        buffer. */
5731     if (ctype->ct_size < (Py_ssize_t)sizeof(ffi_arg)) {
5732         if (ctype->ct_flags & CT_VOID) {
5733             if (pyobj == Py_None) {
5734                 return 0;
5735             }
5736             else {
5737                 PyErr_SetString(PyExc_TypeError,
5738                     "callback with the return type 'void' must return None");
5739                 return -1;
5740             }
5741         }
5742         if (!encode_result_for_libffi)
5743             goto skip;
5744         if (ctype->ct_flags & CT_PRIMITIVE_SIGNED) {
5745             PY_LONG_LONG value;
5746             /* It's probably fine to always zero-extend, but you never
5747                know: maybe some code somewhere expects a negative
5748                'short' result to be returned into EAX as a 32-bit
5749                negative number.  Better safe than sorry.  This code
5750                is about that case.  Let's ignore this for enums.
5751             */
5752             /* do a first conversion only to detect overflows.  This
5753                conversion produces stuff that is otherwise ignored. */
5754             if (convert_from_object(result, ctype, pyobj) < 0)
5755                 return -1;
5756             /* manual inlining and tweaking of convert_from_object()
5757                in order to write a whole 'ffi_arg'. */
5758             value = _my_PyLong_AsLongLong(pyobj);
5759             if (value == -1 && PyErr_Occurred())
5760                 return -1;
5761             write_raw_integer_data(result, value, sizeof(ffi_arg));
5762             return 0;
5763         }
5764         else if (ctype->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_SIGNED |
5765                                     CT_PRIMITIVE_UNSIGNED |
5766                                     CT_POINTER | CT_FUNCTIONPTR)) {
5767             /* zero extension: fill the '*result' with zeros, and (on big-
5768                endian machines) correct the 'result' pointer to write to.
5769                We also do that for pointers, even though we're normally not
5770                in this branch because ctype->ct_size == sizeof(ffi_arg) for
5771                pointers---except on some architectures like x32 (issue #372).
5772              */
5773             memset(result, 0, sizeof(ffi_arg));
5774 #ifdef WORDS_BIGENDIAN
5775             result += (sizeof(ffi_arg) - ctype->ct_size);
5776 #endif
5777         }
5778     }
5779  skip:
5780     return convert_from_object(result, ctype, pyobj);
5781 }
5782 
_my_PyErr_WriteUnraisable(PyObject * t,PyObject * v,PyObject * tb,char * objdescr,PyObject * obj,char * extra_error_line)5783 static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb,
5784                                       char *objdescr, PyObject *obj,
5785                                       char *extra_error_line)
5786 {
5787     /* like PyErr_WriteUnraisable(), but write a full traceback */
5788     PyObject *f;
5789 #if PY_MAJOR_VERSION >= 3
5790     /* jump through hoops to ensure the tb is attached to v, on Python 3 */
5791     PyErr_NormalizeException(&t, &v, &tb);
5792     if (tb == NULL) {
5793         tb = Py_None;
5794         Py_INCREF(tb);
5795     }
5796     PyException_SetTraceback(v, tb);
5797 #endif
5798     f = PySys_GetObject("stderr");
5799     if (f != NULL) {
5800         if (obj != NULL) {
5801             PyFile_WriteString(objdescr, f);
5802             PyFile_WriteObject(obj, f, 0);
5803             PyFile_WriteString(":\n", f);
5804         }
5805         if (extra_error_line != NULL)
5806             PyFile_WriteString(extra_error_line, f);
5807         PyErr_Display(t, v, tb);
5808     }
5809     Py_XDECREF(t);
5810     Py_XDECREF(v);
5811     Py_XDECREF(tb);
5812 }
5813 
general_invoke_callback(int decode_args_from_libffi,void * result,char * args,void * userdata)5814 static void general_invoke_callback(int decode_args_from_libffi,
5815                                     void *result, char *args, void *userdata)
5816 {
5817     PyObject *cb_args = (PyObject *)userdata;
5818     CTypeDescrObject *ct = (CTypeDescrObject *)PyTuple_GET_ITEM(cb_args, 0);
5819     PyObject *signature = ct->ct_stuff;
5820     PyObject *py_ob = PyTuple_GET_ITEM(cb_args, 1);
5821     PyObject *py_args = NULL;
5822     PyObject *py_res = NULL;
5823     PyObject *py_rawerr;
5824     PyObject *onerror_cb;
5825     Py_ssize_t i, n;
5826     char *extra_error_line = NULL;
5827 
5828 #define SIGNATURE(i)  ((CTypeDescrObject *)PyTuple_GET_ITEM(signature, i))
5829 
5830     Py_INCREF(cb_args);
5831 
5832     n = PyTuple_GET_SIZE(signature) - 2;
5833     py_args = PyTuple_New(n);
5834     if (py_args == NULL)
5835         goto error;
5836 
5837     for (i=0; i<n; i++) {
5838         char *a_src;
5839         PyObject *a;
5840         CTypeDescrObject *a_ct = SIGNATURE(2 + i);
5841 
5842         if (decode_args_from_libffi) {
5843             a_src = ((void **)args)[i];
5844         }
5845         else {
5846             a_src = args + i * 8;
5847             if (a_ct->ct_flags & (CT_IS_LONGDOUBLE | CT_STRUCT | CT_UNION))
5848                 a_src = *(char **)a_src;
5849         }
5850         a = convert_to_object(a_src, a_ct);
5851         if (a == NULL)
5852             goto error;
5853         PyTuple_SET_ITEM(py_args, i, a);
5854     }
5855 
5856     py_res = PyObject_Call(py_ob, py_args, NULL);
5857     if (py_res == NULL)
5858         goto error;
5859     if (convert_from_object_fficallback(result, SIGNATURE(1), py_res,
5860                                         decode_args_from_libffi) < 0) {
5861         extra_error_line = "Trying to convert the result back to C:\n";
5862         goto error;
5863     }
5864  done:
5865     Py_XDECREF(py_args);
5866     Py_XDECREF(py_res);
5867     Py_DECREF(cb_args);
5868     return;
5869 
5870  error:
5871     if (SIGNATURE(1)->ct_size > 0) {
5872         py_rawerr = PyTuple_GET_ITEM(cb_args, 2);
5873         memcpy(result, PyBytes_AS_STRING(py_rawerr),
5874                        PyBytes_GET_SIZE(py_rawerr));
5875     }
5876     onerror_cb = PyTuple_GET_ITEM(cb_args, 3);
5877     if (onerror_cb == Py_None) {
5878         PyObject *ecap, *t, *v, *tb;
5879         PyErr_Fetch(&t, &v, &tb);
5880         ecap = _cffi_start_error_capture();
5881         _my_PyErr_WriteUnraisable(t, v, tb, "From cffi callback ", py_ob,
5882                                   extra_error_line);
5883         _cffi_stop_error_capture(ecap);
5884     }
5885     else {
5886         PyObject *exc1, *val1, *tb1, *res1, *exc2, *val2, *tb2;
5887         PyErr_Fetch(&exc1, &val1, &tb1);
5888         PyErr_NormalizeException(&exc1, &val1, &tb1);
5889         res1 = PyObject_CallFunctionObjArgs(onerror_cb,
5890                                             exc1 ? exc1 : Py_None,
5891                                             val1 ? val1 : Py_None,
5892                                             tb1  ? tb1  : Py_None,
5893                                             NULL);
5894         if (res1 != NULL) {
5895             if (res1 != Py_None)
5896                 convert_from_object_fficallback(result, SIGNATURE(1), res1,
5897                                                 decode_args_from_libffi);
5898             Py_DECREF(res1);
5899         }
5900         if (!PyErr_Occurred()) {
5901             Py_XDECREF(exc1);
5902             Py_XDECREF(val1);
5903             Py_XDECREF(tb1);
5904         }
5905         else {
5906             /* double exception! print a double-traceback... */
5907             PyObject *ecap;
5908             PyErr_Fetch(&exc2, &val2, &tb2);
5909             ecap = _cffi_start_error_capture();
5910             _my_PyErr_WriteUnraisable(exc1, val1, tb1,
5911                                       "From cffi callback ", py_ob,
5912                                       extra_error_line);
5913             extra_error_line = ("\nDuring the call to 'onerror', "
5914                                 "another exception occurred:\n\n");
5915             _my_PyErr_WriteUnraisable(exc2, val2, tb2,
5916                                       NULL, NULL, extra_error_line);
5917             _cffi_stop_error_capture(ecap);
5918         }
5919     }
5920     goto done;
5921 
5922 #undef SIGNATURE
5923 }
5924 
invoke_callback(ffi_cif * cif,void * result,void ** args,void * userdata)5925 static void invoke_callback(ffi_cif *cif, void *result, void **args,
5926                             void *userdata)
5927 {
5928     save_errno();
5929     {
5930         PyGILState_STATE state = gil_ensure();
5931         general_invoke_callback(1, result, (char *)args, userdata);
5932         gil_release(state);
5933     }
5934     restore_errno();
5935 }
5936 
prepare_callback_info_tuple(CTypeDescrObject * ct,PyObject * ob,PyObject * error_ob,PyObject * onerror_ob,int decode_args_from_libffi)5937 static PyObject *prepare_callback_info_tuple(CTypeDescrObject *ct,
5938                                              PyObject *ob,
5939                                              PyObject *error_ob,
5940                                              PyObject *onerror_ob,
5941                                              int decode_args_from_libffi)
5942 {
5943     CTypeDescrObject *ctresult;
5944     PyObject *py_rawerr, *infotuple;
5945     Py_ssize_t size;
5946 
5947     if (!(ct->ct_flags & CT_FUNCTIONPTR)) {
5948         PyErr_Format(PyExc_TypeError, "expected a function ctype, got '%s'",
5949                      ct->ct_name);
5950         return NULL;
5951     }
5952     if (!PyCallable_Check(ob)) {
5953         PyErr_Format(PyExc_TypeError,
5954                      "expected a callable object, not %.200s",
5955                      Py_TYPE(ob)->tp_name);
5956         return NULL;
5957     }
5958     if (onerror_ob != Py_None && !PyCallable_Check(onerror_ob)) {
5959         PyErr_Format(PyExc_TypeError,
5960                      "expected a callable object for 'onerror', not %.200s",
5961                      Py_TYPE(onerror_ob)->tp_name);
5962         return NULL;
5963     }
5964 
5965     ctresult = (CTypeDescrObject *)PyTuple_GET_ITEM(ct->ct_stuff, 1);
5966     size = ctresult->ct_size;
5967     if (size < (Py_ssize_t)sizeof(ffi_arg))
5968         size = sizeof(ffi_arg);
5969     py_rawerr = PyBytes_FromStringAndSize(NULL, size);
5970     if (py_rawerr == NULL)
5971         return NULL;
5972     memset(PyBytes_AS_STRING(py_rawerr), 0, size);
5973     if (error_ob != Py_None) {
5974         if (convert_from_object_fficallback(
5975                 PyBytes_AS_STRING(py_rawerr), ctresult, error_ob,
5976                 decode_args_from_libffi) < 0) {
5977             Py_DECREF(py_rawerr);
5978             return NULL;
5979         }
5980     }
5981     infotuple = Py_BuildValue("OOOO", ct, ob, py_rawerr, onerror_ob);
5982     Py_DECREF(py_rawerr);
5983 
5984 #ifdef WITH_THREAD
5985     /* We must setup the GIL here, in case the callback is invoked in
5986        some other non-Pythonic thread.  This is the same as ctypes. */
5987     PyEval_InitThreads();
5988 #endif
5989 
5990     return infotuple;
5991 }
5992 
b_callback(PyObject * self,PyObject * args)5993 static PyObject *b_callback(PyObject *self, PyObject *args)
5994 {
5995     CTypeDescrObject *ct;
5996     CDataObject_closure *cd;
5997     PyObject *ob, *error_ob = Py_None, *onerror_ob = Py_None;
5998     PyObject *infotuple;
5999     cif_description_t *cif_descr;
6000     ffi_closure *closure;
6001     void *closure_exec;
6002 
6003     if (!PyArg_ParseTuple(args, "O!O|OO:callback", &CTypeDescr_Type, &ct, &ob,
6004                           &error_ob, &onerror_ob))
6005         return NULL;
6006 
6007     infotuple = prepare_callback_info_tuple(ct, ob, error_ob, onerror_ob, 1);
6008     if (infotuple == NULL)
6009         return NULL;
6010 
6011 #ifdef CFFI_TRUST_LIBFFI
6012     closure = ffi_closure_alloc(sizeof(ffi_closure), &closure_exec);
6013 #else
6014     closure = cffi_closure_alloc();
6015     closure_exec = closure;
6016 #endif
6017     if (closure == NULL) {
6018         Py_DECREF(infotuple);
6019         return NULL;
6020     }
6021     cd = PyObject_GC_New(CDataObject_closure, &CDataOwningGC_Type);
6022     if (cd == NULL)
6023         goto error;
6024     Py_INCREF(ct);
6025     cd->head.c_type = ct;
6026     cd->head.c_data = (char *)closure_exec;
6027     cd->head.c_weakreflist = NULL;
6028     cd->closure = closure;
6029     PyObject_GC_Track(cd);
6030 
6031     cif_descr = (cif_description_t *)ct->ct_extra;
6032     if (cif_descr == NULL) {
6033         PyErr_Format(PyExc_NotImplementedError,
6034                      "%s: callback with unsupported argument or "
6035                      "return type or with '...'", ct->ct_name);
6036         goto error;
6037     }
6038 #ifdef CFFI_TRUST_LIBFFI
6039     if (ffi_prep_closure_loc(closure, &cif_descr->cif,
6040                          invoke_callback, infotuple, closure_exec) != FFI_OK) {
6041 #else
6042     if (ffi_prep_closure(closure, &cif_descr->cif,
6043                          invoke_callback, infotuple) != FFI_OK) {
6044 #endif
6045         PyErr_SetString(PyExc_SystemError,
6046                         "libffi failed to build this callback");
6047         goto error;
6048     }
6049     if (closure->user_data != infotuple) {
6050         /* Issue #266.  Should not occur, but could, if we are using
6051            at runtime a version of libffi compiled with a different
6052            'ffi_closure' structure than the one we expect from ffi.h
6053            (e.g. difference in details of the platform): a difference
6054            in FFI_TRAMPOLINE_SIZE means that the 'user_data' field
6055            ends up somewhere else, and so the test above fails.
6056         */
6057         PyErr_SetString(PyExc_SystemError,
6058             "ffi_prep_closure(): bad user_data (it seems that the "
6059             "version of the libffi library seen at runtime is "
6060             "different from the 'ffi.h' file seen at compile-time)");
6061         goto error;
6062     }
6063     return (PyObject *)cd;
6064 
6065  error:
6066     closure->user_data = NULL;
6067     if (cd == NULL) {
6068 #ifdef CFFI_TRUST_LIBFFI
6069         ffi_closure_free(closure);
6070 #else
6071         cffi_closure_free(closure);
6072 #endif
6073     }
6074     else
6075         Py_DECREF(cd);
6076     Py_XDECREF(infotuple);
6077     return NULL;
6078 }
6079 
6080 static PyObject *b_new_enum_type(PyObject *self, PyObject *args)
6081 {
6082     char *ename;
6083     PyObject *enumerators, *enumvalues;
6084     PyObject *dict1 = NULL, *dict2 = NULL, *combined = NULL, *tmpkey = NULL;
6085     int name_size;
6086     CTypeDescrObject *td, *basetd;
6087     Py_ssize_t i, n;
6088 
6089     if (!PyArg_ParseTuple(args, "sO!O!O!:new_enum_type",
6090                           &ename,
6091                           &PyTuple_Type, &enumerators,
6092                           &PyTuple_Type, &enumvalues,
6093                           &CTypeDescr_Type, &basetd))
6094         return NULL;
6095 
6096     n = PyTuple_GET_SIZE(enumerators);
6097     if (n != PyTuple_GET_SIZE(enumvalues)) {
6098         PyErr_SetString(PyExc_ValueError,
6099                         "tuple args must have the same size");
6100         return NULL;
6101     }
6102 
6103     if (!(basetd->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED))) {
6104         PyErr_SetString(PyExc_TypeError,
6105                         "expected a primitive signed or unsigned base type");
6106         return NULL;
6107     }
6108 
6109     dict1 = PyDict_New();
6110     if (dict1 == NULL)
6111         goto error;
6112     dict2 = PyDict_New();
6113     if (dict2 == NULL)
6114         goto error;
6115 
6116     for (i=n; --i >= 0; ) {
6117         long long lvalue;
6118         PyObject *value = PyTuple_GET_ITEM(enumvalues, i);
6119         tmpkey = PyTuple_GET_ITEM(enumerators, i);
6120         Py_INCREF(tmpkey);
6121         if (!PyText_Check(tmpkey)) {
6122 #if PY_MAJOR_VERSION < 3
6123             if (PyUnicode_Check(tmpkey)) {
6124                 const char *text = PyText_AsUTF8(tmpkey);
6125                 if (text == NULL)
6126                     goto error;
6127                 Py_DECREF(tmpkey);
6128                 tmpkey = PyString_FromString(text);
6129                 if (tmpkey == NULL)
6130                     goto error;
6131             }
6132             else
6133 #endif
6134             {
6135                 PyErr_SetString(PyExc_TypeError,
6136                                 "enumerators must be a list of strings");
6137                 goto error;
6138             }
6139         }
6140         if (convert_from_object((char*)&lvalue, basetd, value) < 0)
6141             goto error;     /* out-of-range or badly typed 'value' */
6142         if (PyDict_SetItem(dict1, tmpkey, value) < 0)
6143             goto error;
6144         if (PyDict_SetItem(dict2, value, tmpkey) < 0)
6145             goto error;
6146         Py_DECREF(tmpkey);
6147         tmpkey = NULL;
6148     }
6149 
6150     combined = PyTuple_Pack(2, dict1, dict2);
6151     if (combined == NULL)
6152         goto error;
6153 
6154     Py_CLEAR(dict2);
6155     Py_CLEAR(dict1);
6156 
6157     name_size = strlen(ename) + 1;
6158     td = ctypedescr_new(name_size);
6159     if (td == NULL)
6160         goto error;
6161 
6162     memcpy(td->ct_name, ename, name_size);
6163     td->ct_stuff = combined;
6164     td->ct_size = basetd->ct_size;
6165     td->ct_length = basetd->ct_length;   /* alignment */
6166     td->ct_extra = basetd->ct_extra;     /* ffi type  */
6167     td->ct_flags = basetd->ct_flags | CT_IS_ENUM;
6168     td->ct_name_position = name_size - 1;
6169     return (PyObject *)td;
6170 
6171  error:
6172     Py_XDECREF(tmpkey);
6173     Py_XDECREF(combined);
6174     Py_XDECREF(dict2);
6175     Py_XDECREF(dict1);
6176     return NULL;
6177 }
6178 
6179 static PyObject *b_alignof(PyObject *self, PyObject *arg)
6180 {
6181     int align;
6182     if (!CTypeDescr_Check(arg)) {
6183         PyErr_SetString(PyExc_TypeError, "expected a 'ctype' object");
6184         return NULL;
6185     }
6186     align = get_alignment((CTypeDescrObject *)arg);
6187     if (align < 0)
6188         return NULL;
6189     return PyInt_FromLong(align);
6190 }
6191 
6192 static Py_ssize_t direct_sizeof_cdata(CDataObject *cd)
6193 {
6194     Py_ssize_t size;
6195     if (cd->c_type->ct_flags & CT_ARRAY)
6196         size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size;
6197     else {
6198         size = -1;
6199         if (cd->c_type->ct_flags & (CT_STRUCT | CT_UNION))
6200             size = _cdata_var_byte_size(cd);
6201         if (size < 0)
6202             size = cd->c_type->ct_size;
6203     }
6204     return size;
6205 }
6206 
6207 static PyObject *b_sizeof(PyObject *self, PyObject *arg)
6208 {
6209     Py_ssize_t size;
6210 
6211     if (CData_Check(arg)) {
6212         size = direct_sizeof_cdata((CDataObject *)arg);
6213     }
6214     else if (CTypeDescr_Check(arg)) {
6215         size = ((CTypeDescrObject *)arg)->ct_size;
6216         if (size < 0) {
6217             PyErr_Format(PyExc_ValueError, "ctype '%s' is of unknown size",
6218                          ((CTypeDescrObject *)arg)->ct_name);
6219             return NULL;
6220         }
6221     }
6222     else {
6223         PyErr_SetString(PyExc_TypeError,
6224                         "expected a 'cdata' or 'ctype' object");
6225         return NULL;
6226     }
6227     return PyInt_FromSsize_t(size);
6228 }
6229 
6230 static PyObject *b_typeof(PyObject *self, PyObject *arg)
6231 {
6232     PyObject *res;
6233 
6234     if (!CData_Check(arg)) {
6235         PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object");
6236         return NULL;
6237     }
6238     res = (PyObject *)((CDataObject *)arg)->c_type;
6239     Py_INCREF(res);
6240     return res;
6241 }
6242 
6243 static CTypeDescrObject *direct_typeoffsetof(CTypeDescrObject *ct,
6244                                              PyObject *fieldname,
6245                                              int following, Py_ssize_t *offset)
6246 {
6247     /* Does not return a new reference! */
6248     CTypeDescrObject *res;
6249     CFieldObject *cf;
6250 
6251     if (PyTextAny_Check(fieldname)) {
6252         if (!following && (ct->ct_flags & CT_POINTER))
6253             ct = ct->ct_itemdescr;
6254         if (!(ct->ct_flags & (CT_STRUCT|CT_UNION))) {
6255             PyErr_SetString(PyExc_TypeError,
6256                             "with a field name argument, expected a "
6257                             "struct or union ctype");
6258             return NULL;
6259         }
6260         if (force_lazy_struct(ct) <= 0) {
6261             if (!PyErr_Occurred())
6262                 PyErr_SetString(PyExc_TypeError, "struct/union is opaque");
6263             return NULL;
6264         }
6265         cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, fieldname);
6266         if (cf == NULL) {
6267             PyErr_SetObject(PyExc_KeyError, fieldname);
6268             return NULL;
6269         }
6270         if (cf->cf_bitshift >= 0) {
6271             PyErr_SetString(PyExc_TypeError, "not supported for bitfields");
6272             return NULL;
6273         }
6274         res = cf->cf_type;
6275         *offset = cf->cf_offset;
6276     }
6277     else {
6278         Py_ssize_t index = PyInt_AsSsize_t(fieldname);
6279         if (index < 0 && PyErr_Occurred()) {
6280             PyErr_SetString(PyExc_TypeError,
6281                             "field name or array index expected");
6282             return NULL;
6283         }
6284 
6285         if (!(ct->ct_flags & (CT_ARRAY|CT_POINTER)) ||
6286                 ct->ct_itemdescr->ct_size < 0) {
6287             PyErr_SetString(PyExc_TypeError, "with an integer argument, "
6288                                              "expected an array ctype or a "
6289                                              "pointer to non-opaque");
6290             return NULL;
6291         }
6292         res = ct->ct_itemdescr;
6293         *offset = MUL_WRAPAROUND(index, ct->ct_itemdescr->ct_size);
6294         if ((*offset / ct->ct_itemdescr->ct_size) != index) {
6295             PyErr_SetString(PyExc_OverflowError,
6296                             "array offset would overflow a Py_ssize_t");
6297             return NULL;
6298         }
6299     }
6300     return res;
6301 }
6302 
6303 static PyObject *b_typeoffsetof(PyObject *self, PyObject *args)
6304 {
6305     PyObject *res, *fieldname;
6306     CTypeDescrObject *ct;
6307     Py_ssize_t offset;
6308     int following = 0;
6309 
6310     if (!PyArg_ParseTuple(args, "O!O|i:typeoffsetof",
6311                           &CTypeDescr_Type, &ct, &fieldname, &following))
6312         return NULL;
6313 
6314     res = (PyObject *)direct_typeoffsetof(ct, fieldname, following, &offset);
6315     if (res == NULL)
6316         return NULL;
6317 
6318     return Py_BuildValue("(On)", res, offset);
6319 }
6320 
6321 static PyObject *b_rawaddressof(PyObject *self, PyObject *args)
6322 {
6323     CTypeDescrObject *ct;
6324     CDataObject *cd;
6325     Py_ssize_t offset;
6326     int accepted_flags;
6327 
6328     if (!PyArg_ParseTuple(args, "O!O!n:rawaddressof",
6329                           &CTypeDescr_Type, &ct,
6330                           &CData_Type, &cd,
6331                           &offset))
6332         return NULL;
6333 
6334     accepted_flags = CT_STRUCT | CT_UNION | CT_ARRAY | CT_POINTER;
6335     if ((cd->c_type->ct_flags & accepted_flags) == 0) {
6336         PyErr_SetString(PyExc_TypeError,
6337                         "expected a cdata struct/union/array/pointer object");
6338         return NULL;
6339     }
6340     if ((ct->ct_flags & CT_POINTER) == 0) {
6341         PyErr_SetString(PyExc_TypeError,
6342                         "expected a pointer ctype");
6343         return NULL;
6344     }
6345     return new_simple_cdata(cd->c_data + offset, ct);
6346 }
6347 
6348 static PyObject *b_getcname(PyObject *self, PyObject *args)
6349 {
6350     CTypeDescrObject *ct;
6351     char *replace_with, *p, *s;
6352     Py_ssize_t namelen, replacelen;
6353 
6354     if (!PyArg_ParseTuple(args, "O!s:getcname",
6355                           &CTypeDescr_Type, &ct, &replace_with))
6356         return NULL;
6357 
6358     namelen = strlen(ct->ct_name);
6359     replacelen = strlen(replace_with);
6360     s = p = alloca(namelen + replacelen + 1);
6361     memcpy(p, ct->ct_name, ct->ct_name_position);
6362     p += ct->ct_name_position;
6363     memcpy(p, replace_with, replacelen);
6364     p += replacelen;
6365     memcpy(p, ct->ct_name + ct->ct_name_position,
6366            namelen - ct->ct_name_position);
6367 
6368     return PyText_FromStringAndSize(s, namelen + replacelen);
6369 }
6370 
6371 static PyObject *b_string(PyObject *self, PyObject *args, PyObject *kwds)
6372 {
6373     CDataObject *cd;
6374     Py_ssize_t maxlen = -1;
6375     static char *keywords[] = {"cdata", "maxlen", NULL};
6376 
6377     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|n:string", keywords,
6378                                      &CData_Type, &cd, &maxlen))
6379         return NULL;
6380 
6381     if (cd->c_type->ct_itemdescr != NULL &&
6382         cd->c_type->ct_itemdescr->ct_flags & (CT_PRIMITIVE_CHAR |
6383                                               CT_PRIMITIVE_SIGNED |
6384                                               CT_PRIMITIVE_UNSIGNED) &&
6385         !(cd->c_type->ct_itemdescr->ct_flags & CT_IS_BOOL)) {
6386         Py_ssize_t length = maxlen;
6387         if (cd->c_data == NULL) {
6388             PyObject *s = cdata_repr(cd);
6389             if (s != NULL) {
6390                 PyErr_Format(PyExc_RuntimeError,
6391                              "cannot use string() on %s",
6392                              PyText_AS_UTF8(s));
6393                 Py_DECREF(s);
6394             }
6395             return NULL;
6396         }
6397         if (length < 0 && cd->c_type->ct_flags & CT_ARRAY) {
6398             length = get_array_length(cd);
6399         }
6400         if (cd->c_type->ct_itemdescr->ct_size == sizeof(char)) {
6401             const char *start = cd->c_data;
6402             if (length < 0) {
6403                 /*READ(start, 1)*/
6404                 length = strlen(start);
6405                 /*READ(start, length)*/
6406             }
6407             else {
6408                 const char *end;
6409                 /*READ(start, length)*/
6410                 end = (const char *)memchr(start, 0, length);
6411                 if (end != NULL)
6412                     length = end - start;
6413             }
6414             return PyBytes_FromStringAndSize(start, length);
6415         }
6416         else if (cd->c_type->ct_itemdescr->ct_flags & CT_PRIMITIVE_CHAR) {
6417             switch (cd->c_type->ct_itemdescr->ct_size) {
6418             case 2: {
6419                 const cffi_char16_t *start = (cffi_char16_t *)cd->c_data;
6420                 if (length < 0) {
6421                     /*READ(start, 2)*/
6422                     length = 0;
6423                     while (start[length])
6424                         length++;
6425                     /*READ(start, 2 * length)*/
6426                 }
6427                 else {
6428                     /*READ(start, 2 * length)*/
6429                     maxlen = length;
6430                     length = 0;
6431                     while (length < maxlen && start[length])
6432                         length++;
6433                 }
6434                 return _my_PyUnicode_FromChar16(start, length);
6435             }
6436             case 4: {
6437                 const cffi_char32_t *start = (cffi_char32_t *)cd->c_data;
6438                 if (length < 0) {
6439                     /*READ(start, 4)*/
6440                     length = 0;
6441                     while (start[length])
6442                         length++;
6443                     /*READ(start, 4 * length)*/
6444                 }
6445                 else {
6446                     /*READ(start, 4 * length)*/
6447                     maxlen = length;
6448                     length = 0;
6449                     while (length < maxlen && start[length])
6450                         length++;
6451                 }
6452                 return _my_PyUnicode_FromChar32(start, length);
6453             }
6454             }
6455         }
6456     }
6457     else if (cd->c_type->ct_flags & CT_IS_ENUM) {
6458         return convert_cdata_to_enum_string(cd, 0);
6459     }
6460     else if (cd->c_type->ct_flags & CT_IS_BOOL) {
6461         /* fall through to TypeError */
6462     }
6463     else if (cd->c_type->ct_flags & (CT_PRIMITIVE_CHAR |
6464                                      CT_PRIMITIVE_SIGNED |
6465                                      CT_PRIMITIVE_UNSIGNED)) {
6466         /*READ(cd->c_data, cd->c_type->ct_size)*/
6467         if (cd->c_type->ct_size == sizeof(char))
6468             return PyBytes_FromStringAndSize(cd->c_data, 1);
6469         else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) {
6470             switch (cd->c_type->ct_size) {
6471             case 2:
6472                 return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data, 1);
6473             case 4:
6474                 return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data, 1);
6475             }
6476         }
6477     }
6478     PyErr_Format(PyExc_TypeError, "string(): unexpected cdata '%s' argument",
6479                  cd->c_type->ct_name);
6480     return NULL;
6481 }
6482 
6483 static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds)
6484 {
6485     CDataObject *cd;
6486     CTypeDescrObject *ctitem;
6487     Py_ssize_t i, length, itemsize;
6488     PyObject *result;
6489     char *src;
6490     int casenum;
6491     static char *keywords[] = {"cdata", "length", NULL};
6492 
6493     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!n:unpack", keywords,
6494                                      &CData_Type, &cd, &length))
6495         return NULL;
6496 
6497     if (!(cd->c_type->ct_flags & (CT_ARRAY|CT_POINTER))) {
6498         PyErr_Format(PyExc_TypeError,
6499                      "expected a pointer or array, got '%s'",
6500                      cd->c_type->ct_name);
6501         return NULL;
6502     }
6503     if (length < 0) {
6504         PyErr_SetString(PyExc_ValueError, "'length' cannot be negative");
6505         return NULL;
6506     }
6507     if (cd->c_data == NULL) {
6508         PyObject *s = cdata_repr(cd);
6509         if (s != NULL) {
6510             PyErr_Format(PyExc_RuntimeError,
6511                          "cannot use unpack() on %s",
6512                          PyText_AS_UTF8(s));
6513             Py_DECREF(s);
6514         }
6515         return NULL;
6516     }
6517 
6518     /* byte- and unicode strings */
6519     ctitem = cd->c_type->ct_itemdescr;
6520     if (ctitem->ct_flags & CT_PRIMITIVE_CHAR) {
6521         switch (ctitem->ct_size) {
6522         case sizeof(char):
6523             return PyBytes_FromStringAndSize(cd->c_data, length);
6524         case 2:
6525             return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data,length);
6526         case 4:
6527             return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data,length);
6528         }
6529     }
6530 
6531     /* else, the result is a list.  This implementation should be
6532        equivalent to but much faster than '[p[i] for i in range(length)]'.
6533        (Note that on PyPy, 'list(p[0:length])' should be equally fast,
6534        but arguably, finding out that there *is* such an unexpected way
6535        to write things down is the real problem.)
6536     */
6537     result = PyList_New(length);
6538     if (result == NULL)
6539         return NULL;
6540 
6541     src = cd->c_data;
6542     itemsize = ctitem->ct_size;
6543     if (itemsize < 0) {
6544         Py_DECREF(result);
6545         PyErr_Format(PyExc_ValueError, "'%s' points to items of unknown size",
6546                      cd->c_type->ct_name);
6547         return NULL;
6548     }
6549 
6550     /* Determine some common fast-paths for the loop below.  The case -1
6551        is the fall-back, which always gives the right answer. */
6552 
6553 #define ALIGNMENT_CHECK(align)                          \
6554         (((align) & ((align) - 1)) == 0 &&              \
6555          (((uintptr_t)src) & ((align) - 1)) == 0)
6556 
6557     casenum = -1;
6558 
6559     if ((ctitem->ct_flags & CT_PRIMITIVE_ANY) &&
6560             ALIGNMENT_CHECK(ctitem->ct_length)) {
6561         /* Source data is fully aligned; we can directly read without
6562            memcpy().  The unaligned case is expected to be rare; in
6563            this situation it is ok to fall back to the general
6564            convert_to_object() in the loop.  For now we also use this
6565            fall-back for types that are too large.
6566         */
6567         if (ctitem->ct_flags & CT_PRIMITIVE_SIGNED) {
6568             if (itemsize == sizeof(long))             casenum = 3;
6569             else if (itemsize == sizeof(int))         casenum = 2;
6570             else if (itemsize == sizeof(short))       casenum = 1;
6571             else if (itemsize == sizeof(signed char)) casenum = 0;
6572         }
6573         else if (ctitem->ct_flags & CT_PRIMITIVE_UNSIGNED) {
6574             /* Note: we never pick case 6 if sizeof(int) == sizeof(long),
6575                so that case 6 below can assume that the 'unsigned int' result
6576                would always fit in a 'signed long'. */
6577             if (ctitem->ct_flags & CT_IS_BOOL)           casenum = 11;
6578             else if (itemsize == sizeof(unsigned long))  casenum = 7;
6579             else if (itemsize == sizeof(unsigned int))   casenum = 6;
6580             else if (itemsize == sizeof(unsigned short)) casenum = 5;
6581             else if (itemsize == sizeof(unsigned char))  casenum = 4;
6582         }
6583         else if (ctitem->ct_flags & CT_PRIMITIVE_FLOAT) {
6584             if      (itemsize == sizeof(double)) casenum = 9;
6585             else if (itemsize == sizeof(float))  casenum = 8;
6586         }
6587     }
6588     else if (ctitem->ct_flags & (CT_POINTER | CT_FUNCTIONPTR)) {
6589         casenum = 10;    /* any pointer */
6590     }
6591 #undef ALIGNMENT_CHECK
6592 
6593     for (i = 0; i < length; i++) {
6594         PyObject *x;
6595         switch (casenum) {
6596             /* general case */
6597         default: x = convert_to_object(src, ctitem); break;
6598 
6599             /* special cases for performance only */
6600         case 0: x = PyInt_FromLong(*(signed char *)src); break;
6601         case 1: x = PyInt_FromLong(*(short *)src); break;
6602         case 2: x = PyInt_FromLong(*(int *)src); break;
6603         case 3: x = PyInt_FromLong(*(long *)src); break;
6604         case 4: x = PyInt_FromLong(*(unsigned char *)src); break;
6605         case 5: x = PyInt_FromLong(*(unsigned short *)src); break;
6606         case 6: x = PyInt_FromLong((long)*(unsigned int *)src); break;
6607         case 7: x = PyLong_FromUnsignedLong(*(unsigned long *)src); break;
6608         case 8: x = PyFloat_FromDouble(*(float *)src); break;
6609         case 9: x = PyFloat_FromDouble(*(double *)src); break;
6610         case 10: x = new_simple_cdata(*(char **)src, ctitem); break;
6611         case 11:
6612             switch (*(unsigned char *)src) {
6613             case 0: x = Py_False; Py_INCREF(x); break;
6614             case 1: x = Py_True;  Py_INCREF(x); break;
6615             default: x = convert_to_object(src, ctitem); /* error */
6616             }
6617             break;
6618         }
6619         if (x == NULL) {
6620             Py_DECREF(result);
6621             return NULL;
6622         }
6623         PyList_SET_ITEM(result, i, x);
6624         src += itemsize;
6625     }
6626     return result;
6627 }
6628 
6629 static PyObject *
6630 b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6631 {
6632     /* this is the constructor of the type implemented in minibuffer.h */
6633     CDataObject *cd;
6634     Py_ssize_t size = -1;
6635     static char *keywords[] = {"cdata", "size", NULL};
6636 
6637     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|n:buffer", keywords,
6638                                      &CData_Type, &cd, &size))
6639         return NULL;
6640 
6641     if (size < 0)
6642         size = _cdata_var_byte_size(cd);
6643 
6644     if (cd->c_type->ct_flags & CT_POINTER) {
6645         if (size < 0)
6646             size = cd->c_type->ct_itemdescr->ct_size;
6647     }
6648     else if (cd->c_type->ct_flags & CT_ARRAY) {
6649         if (size < 0)
6650             size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size;
6651     }
6652     else {
6653         PyErr_Format(PyExc_TypeError,
6654                      "expected a pointer or array cdata, got '%s'",
6655                      cd->c_type->ct_name);
6656         return NULL;
6657     }
6658     if (size < 0) {
6659         PyErr_Format(PyExc_TypeError,
6660                      "don't know the size pointed to by '%s'",
6661                      cd->c_type->ct_name);
6662         return NULL;
6663     }
6664     /*WRITE(cd->c_data, size)*/
6665     return minibuffer_new(cd->c_data, size, (PyObject *)cd);
6666 }
6667 
6668 static PyObject *b_get_errno(PyObject *self, PyObject *noarg)
6669 {
6670     int err;
6671     restore_errno_only();
6672     err = errno;
6673     errno = 0;
6674     return PyInt_FromLong(err);
6675 }
6676 
6677 static PyObject *b_set_errno(PyObject *self, PyObject *arg)
6678 {
6679     long ival = PyInt_AsLong(arg);
6680     if (ival == -1 && PyErr_Occurred())
6681         return NULL;
6682     else if (ival < INT_MIN || ival > INT_MAX) {
6683         PyErr_SetString(PyExc_OverflowError, "errno value too large");
6684         return NULL;
6685     }
6686     errno = (int)ival;
6687     save_errno_only();
6688     errno = 0;
6689     Py_INCREF(Py_None);
6690     return Py_None;
6691 }
6692 
6693 static PyObject *newp_handle(CTypeDescrObject *ct_voidp, PyObject *x)
6694 {
6695     CDataObject_own_structptr *cd;
6696     cd = (CDataObject_own_structptr *)PyObject_GC_New(CDataObject_own_structptr,
6697                                                       &CDataOwningGC_Type);
6698     if (cd == NULL)
6699         return NULL;
6700     Py_INCREF(ct_voidp);
6701     cd->head.c_type = ct_voidp;
6702     cd->head.c_data = (char *)cd;
6703     cd->head.c_weakreflist = NULL;
6704     Py_INCREF(x);
6705     cd->structobj = x;
6706     PyObject_GC_Track(cd);
6707     return (PyObject *)cd;
6708 }
6709 
6710 static PyObject *b_newp_handle(PyObject *self, PyObject *args)
6711 {
6712     CTypeDescrObject *ct;
6713     PyObject *x;
6714     if (!PyArg_ParseTuple(args, "O!O", &CTypeDescr_Type, &ct, &x))
6715         return NULL;
6716 
6717     if (!(ct->ct_flags & CT_IS_VOID_PTR)) {
6718         PyErr_Format(PyExc_TypeError, "needs 'void *', got '%s'", ct->ct_name);
6719         return NULL;
6720     }
6721 
6722     return newp_handle(ct, x);
6723 }
6724 
6725 static PyObject *b_from_handle(PyObject *self, PyObject *arg)
6726 {
6727     CTypeDescrObject *ct;
6728     CDataObject_own_structptr *orgcd;
6729     PyObject *x;
6730     if (!CData_Check(arg)) {
6731         PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object");
6732         return NULL;
6733     }
6734     ct = ((CDataObject *)arg)->c_type;
6735     if (!(ct->ct_flags & CT_IS_VOIDCHAR_PTR)) {
6736         PyErr_Format(PyExc_TypeError,
6737                      "expected a 'cdata' object with a 'void *' out of "
6738                      "new_handle(), got '%s'", ct->ct_name);
6739         return NULL;
6740     }
6741     orgcd = (CDataObject_own_structptr *)((CDataObject *)arg)->c_data;
6742     if (!orgcd) {
6743         PyErr_SetString(PyExc_RuntimeError,
6744                         "cannot use from_handle() on NULL pointer");
6745         return NULL;
6746     }
6747     if (Py_REFCNT(orgcd) <= 0 || Py_TYPE(orgcd) != &CDataOwningGC_Type) {
6748         Py_FatalError("ffi.from_handle() detected that the address passed "
6749                       "points to garbage. If it is really the result of "
6750                       "ffi.new_handle(), then the Python object has already "
6751                       "been garbage collected");
6752     }
6753     x = orgcd->structobj;
6754     Py_INCREF(x);
6755     return x;
6756 }
6757 
6758 static int _my_PyObject_GetContiguousBuffer(PyObject *x, Py_buffer *view,
6759                                             int writable_only)
6760 {
6761 #if PY_MAJOR_VERSION < 3
6762     /* Some objects only support the buffer interface and CPython doesn't
6763        translate it into the memoryview interface, mess.  Hack a very
6764        minimal content for 'view'.  Don't care if the other fields are
6765        uninitialized: we only call PyBuffer_Release(), which only reads
6766        'view->obj'. */
6767     PyBufferProcs *pb = x->ob_type->tp_as_buffer;
6768     if (pb && !pb->bf_releasebuffer) {
6769         /* we used to try all three in some vaguely sensible order,
6770            i.e. first the write.  But trying to call the write on a
6771            read-only buffer fails with TypeError.  So we use a less-
6772            sensible order now.  See test_from_buffer_more_cases.
6773 
6774            If 'writable_only', we only try bf_getwritebuffer.
6775         */
6776         readbufferproc proc = NULL;
6777         if (!writable_only) {
6778             proc = (readbufferproc)pb->bf_getreadbuffer;
6779             if (!proc)
6780                 proc = (readbufferproc)pb->bf_getcharbuffer;
6781         }
6782         if (!proc)
6783             proc = (readbufferproc)pb->bf_getwritebuffer;
6784 
6785         if (proc && pb->bf_getsegcount) {
6786             if ((*pb->bf_getsegcount)(x, NULL) != 1) {
6787                 PyErr_SetString(PyExc_TypeError,
6788                                 "expected a single-segment buffer object");
6789                 return -1;
6790             }
6791             view->len = (*proc)(x, 0, &view->buf);
6792             if (view->len < 0)
6793                 return -1;
6794             view->obj = x;
6795             Py_INCREF(x);
6796             return 0;
6797         }
6798     }
6799 #endif
6800 
6801     if (PyObject_GetBuffer(x, view, writable_only ? PyBUF_WRITABLE
6802                                                   : PyBUF_SIMPLE) < 0)
6803         return -1;
6804 
6805     if (!PyBuffer_IsContiguous(view, 'A')) {
6806         PyBuffer_Release(view);
6807         PyErr_SetString(PyExc_TypeError, "contiguous buffer expected");
6808         return -1;
6809     }
6810     return 0;
6811 }
6812 
6813 static PyObject *direct_from_buffer(CTypeDescrObject *ct, PyObject *x,
6814                                     int require_writable)
6815 {
6816     CDataObject *cd;
6817     Py_buffer *view;
6818     Py_ssize_t arraylength;
6819 
6820     if (!(ct->ct_flags & CT_ARRAY)) {
6821         PyErr_Format(PyExc_TypeError, "expected an array ctype, got '%s'",
6822                      ct->ct_name);
6823         return NULL;
6824     }
6825 
6826     /* PyPy 5.7 can obtain buffers for string (python 2)
6827        or bytes (python 3). from_buffer(u"foo") is disallowed.
6828      */
6829     if (PyUnicode_Check(x)) {
6830         PyErr_SetString(PyExc_TypeError,
6831                         "from_buffer() cannot return the address "
6832                         "of a unicode object");
6833         return NULL;
6834     }
6835 
6836     view = PyObject_Malloc(sizeof(Py_buffer));
6837     if (view == NULL) {
6838         PyErr_NoMemory();
6839         return NULL;
6840     }
6841     if (_my_PyObject_GetContiguousBuffer(x, view, require_writable) < 0)
6842         goto error1;
6843 
6844     if (ct->ct_length >= 0) {
6845         /* it's an array with a fixed length; make sure that the
6846            buffer contains enough bytes. */
6847         if (view->len < ct->ct_size) {
6848             PyErr_Format(PyExc_ValueError,
6849                 "buffer is too small (%zd bytes) for '%s' (%zd bytes)",
6850                 view->len, ct->ct_name, ct->ct_size);
6851             goto error2;
6852         }
6853         arraylength = ct->ct_length;
6854     }
6855     else {
6856         /* it's an open 'array[]' */
6857         if (ct->ct_itemdescr->ct_size == 1) {
6858             /* fast path, performance only */
6859             arraylength = view->len;
6860         }
6861         else if (ct->ct_itemdescr->ct_size > 0) {
6862             /* give it as many items as fit the buffer.  Ignore a
6863                partial last element. */
6864             arraylength = view->len / ct->ct_itemdescr->ct_size;
6865         }
6866         else {
6867             /* it's an array 'empty[]'.  Unsupported obscure case:
6868                the problem is that setting the length of the result
6869                to anything large (like SSIZE_T_MAX) is dangerous,
6870                because if someone tries to loop over it, it will
6871                turn effectively into an infinite loop. */
6872             PyErr_Format(PyExc_ZeroDivisionError,
6873                 "from_buffer('%s', ..): the actual length of the array "
6874                 "cannot be computed", ct->ct_name);
6875             goto error2;
6876         }
6877     }
6878 
6879     cd = (CDataObject *)PyObject_GC_New(CDataObject_owngc_frombuf,
6880                                         &CDataOwningGC_Type);
6881     if (cd == NULL)
6882         goto error2;
6883 
6884     Py_INCREF(ct);
6885     cd->c_type = ct;
6886     cd->c_data = view->buf;
6887     cd->c_weakreflist = NULL;
6888     ((CDataObject_owngc_frombuf *)cd)->length = arraylength;
6889     ((CDataObject_owngc_frombuf *)cd)->bufferview = view;
6890     PyObject_GC_Track(cd);
6891     return (PyObject *)cd;
6892 
6893  error2:
6894     PyBuffer_Release(view);
6895  error1:
6896     PyObject_Free(view);
6897     return NULL;
6898 }
6899 
6900 static PyObject *b_from_buffer(PyObject *self, PyObject *args)
6901 {
6902     CTypeDescrObject *ct;
6903     PyObject *x;
6904     int require_writable = 0;
6905 
6906     if (!PyArg_ParseTuple(args, "O!O|i", &CTypeDescr_Type, &ct, &x,
6907                           &require_writable))
6908         return NULL;
6909 
6910     return direct_from_buffer(ct, x, require_writable);
6911 }
6912 
6913 static int _fetch_as_buffer(PyObject *x, Py_buffer *view, int writable_only)
6914 {
6915     if (CData_Check(x)) {
6916         CTypeDescrObject *ct = ((CDataObject *)x)->c_type;
6917         if (!(ct->ct_flags & (CT_POINTER|CT_ARRAY))) {
6918             PyErr_Format(PyExc_TypeError,
6919                          "expected a pointer or array ctype, got '%s'",
6920                          ct->ct_name);
6921             return -1;
6922         }
6923         view->buf = ((CDataObject *)x)->c_data;
6924         view->obj = NULL;
6925         return 0;
6926     }
6927     else {
6928         return _my_PyObject_GetContiguousBuffer(x, view, writable_only);
6929     }
6930 }
6931 
6932 static PyObject *b_memmove(PyObject *self, PyObject *args, PyObject *kwds)
6933 {
6934     PyObject *dest_obj, *src_obj;
6935     Py_buffer dest_view, src_view;
6936     Py_ssize_t n;
6937     static char *keywords[] = {"dest", "src", "n", NULL};
6938 
6939     if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOn", keywords,
6940                                      &dest_obj, &src_obj, &n))
6941         return NULL;
6942     if (n < 0) {
6943         PyErr_SetString(PyExc_ValueError, "negative size");
6944         return NULL;
6945     }
6946 
6947     if (_fetch_as_buffer(src_obj, &src_view, 0) < 0) {
6948         return NULL;
6949     }
6950     if (_fetch_as_buffer(dest_obj, &dest_view, 1) < 0) {
6951         PyBuffer_Release(&src_view);
6952         return NULL;
6953     }
6954 
6955     memmove(dest_view.buf, src_view.buf, n);
6956 
6957     PyBuffer_Release(&dest_view);
6958     PyBuffer_Release(&src_view);
6959     Py_INCREF(Py_None);
6960     return Py_None;
6961 }
6962 
6963 static PyObject *b__get_types(PyObject *self, PyObject *noarg)
6964 {
6965     return PyTuple_Pack(2, (PyObject *)&CData_Type,
6966                            (PyObject *)&CTypeDescr_Type);
6967 }
6968 
6969 /* forward, in commontypes.c */
6970 static PyObject *b__get_common_types(PyObject *self, PyObject *arg);
6971 
6972 static PyObject *b_gcp(PyObject *self, PyObject *args, PyObject *kwds)
6973 {
6974     CDataObject *cd;
6975     CDataObject *origobj;
6976     PyObject *destructor;
6977     Py_ssize_t ignored;   /* for pypy */
6978     static char *keywords[] = {"cdata", "destructor", "size", NULL};
6979 
6980     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|n:gc", keywords,
6981                                      &CData_Type, &origobj, &destructor,
6982                                      &ignored))
6983         return NULL;
6984 
6985     if (destructor == Py_None) {
6986 	if (!PyObject_TypeCheck(origobj, &CDataGCP_Type)) {
6987 	    PyErr_SetString(PyExc_TypeError,
6988 			    "Can remove destructor only on a object "
6989 			    "previously returned by ffi.gc()");
6990 	    return NULL;
6991 	}
6992 	Py_CLEAR(((CDataObject_gcp *)origobj)->destructor);
6993 	Py_RETURN_NONE;
6994     }
6995 
6996     cd = allocate_gcp_object(origobj, origobj->c_type, destructor);
6997     return (PyObject *)cd;
6998 }
6999 
7000 static PyObject *b_release(PyObject *self, PyObject *arg)
7001 {
7002     if (!CData_Check(arg)) {
7003         PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object");
7004         return NULL;
7005     }
7006     return cdata_exit(arg, NULL);
7007 }
7008 
7009 /************************************************************/
7010 
7011 static char _testfunc0(char a, char b)
7012 {
7013     return a + b;
7014 }
7015 static long _testfunc1(int a, long b)
7016 {
7017     return (long)a + b;
7018 }
7019 static PY_LONG_LONG _testfunc2(PY_LONG_LONG a, PY_LONG_LONG b)
7020 {
7021     return a + b;
7022 }
7023 static double _testfunc3(float a, double b)
7024 {
7025     return a + b;
7026 }
7027 static float _testfunc4(float a, double b)
7028 {
7029     return (float)(a + b);
7030 }
7031 static void _testfunc5(void)
7032 {
7033     errno = errno + 15;
7034 }
7035 static int *_testfunc6(int *x)
7036 {
7037     static int y;
7038     y = *x - 1000;
7039     return &y;
7040 }
7041 struct _testfunc7_s { unsigned char a1; short a2; };
7042 static short _testfunc7(struct _testfunc7_s inlined)
7043 {
7044     return inlined.a1 + inlined.a2;
7045 }
7046 static int _testfunc9(int num, ...)
7047 {
7048     va_list vargs;
7049     int i, total = 0;
7050     va_start(vargs, num);
7051     for (i=0; i<num; i++) {
7052         int value = va_arg(vargs, int);
7053         if (value == 0)
7054             value = -66666666;
7055         total += value;
7056     }
7057     va_end(vargs);
7058     return total;
7059 }
7060 
7061 static struct _testfunc7_s _testfunc10(int n)
7062 {
7063     struct _testfunc7_s result;
7064     result.a1 = n;
7065     result.a2 = n * n;
7066     return result;
7067 }
7068 
7069 struct _testfunc11_s { int a1, a2; };
7070 static struct _testfunc11_s _testfunc11(int n)
7071 {
7072     struct _testfunc11_s result;
7073     result.a1 = n;
7074     result.a2 = n * n;
7075     return result;
7076 }
7077 
7078 struct _testfunc12_s { double a1; };
7079 static struct _testfunc12_s _testfunc12(int n)
7080 {
7081     struct _testfunc12_s result;
7082     result.a1 = n;
7083     return result;
7084 }
7085 
7086 struct _testfunc13_s { int a1, a2, a3; };
7087 static struct _testfunc13_s _testfunc13(int n)
7088 {
7089     struct _testfunc13_s result;
7090     result.a1 = n;
7091     result.a2 = n * n;
7092     result.a3 = n * n * n;
7093     return result;
7094 }
7095 
7096 struct _testfunc14_s { float a1; };
7097 static struct _testfunc14_s _testfunc14(int n)
7098 {
7099     struct _testfunc14_s result;
7100     result.a1 = (float)n;
7101     return result;
7102 }
7103 
7104 struct _testfunc15_s { float a1; int a2; };
7105 static struct _testfunc15_s _testfunc15(int n)
7106 {
7107     struct _testfunc15_s result;
7108     result.a1 = (float)n;
7109     result.a2 = n * n;
7110     return result;
7111 }
7112 
7113 struct _testfunc16_s { float a1, a2; };
7114 static struct _testfunc16_s _testfunc16(int n)
7115 {
7116     struct _testfunc16_s result;
7117     result.a1 = (float)n;
7118     result.a2 = -(float)n;
7119     return result;
7120 }
7121 
7122 struct _testfunc17_s { int a1; float a2; };
7123 static struct _testfunc17_s _testfunc17(int n)
7124 {
7125     struct _testfunc17_s result;
7126     result.a1 = n;
7127     result.a2 = (float)n * (float)n;
7128     return result;
7129 }
7130 
7131 static int _testfunc18(struct _testfunc17_s *ptr)
7132 {
7133     return ptr->a1 + (int)ptr->a2;
7134 }
7135 
7136 static long double _testfunc19(long double x, int count)
7137 {
7138     int i;
7139     for (i=0; i<count; i++) {
7140         x = 4*x - x*x;
7141     }
7142     return x;
7143 }
7144 
7145 static short _testfunc20(struct _testfunc7_s *ptr)
7146 {
7147     return ptr->a1 + ptr->a2;
7148 }
7149 
7150 struct _testfunc21_s { int a, b, c, d, e, f, g, h, i, j; };
7151 static int _testfunc21(struct _testfunc21_s inlined)
7152 {
7153     return ((inlined.a << 0) +
7154             (inlined.b << 1) +
7155             (inlined.c << 2) +
7156             (inlined.d << 3) +
7157             (inlined.e << 4) +
7158             (inlined.f << 5) +
7159             (inlined.g << 6) +
7160             (inlined.h << 7) +
7161             (inlined.i << 8) +
7162             (inlined.j << 9));
7163 }
7164 
7165 struct _testfunc22_s { int a[10]; };
7166 static struct _testfunc22_s _testfunc22(struct _testfunc22_s s1,
7167                                         struct _testfunc22_s s2)
7168 {
7169     struct _testfunc22_s result;
7170     int i;
7171     for (i=0; i<10; i++)
7172         result.a[i] = s1.a[i] - s2.a[i];
7173     return result;
7174 }
7175 
7176 static int _testfunc23(char *p)
7177 {
7178     if (p)
7179         return 1000 * p[0];
7180     return -42;
7181 }
7182 
7183 #if 0   /* libffi doesn't properly support complexes currently */
7184         /* also, MSVC might not support _Complex... */
7185         /* if this is enabled one day, remember to also add _Complex
7186          * arguments in addition to return values. */
7187 static float _Complex _testfunc24(float a, float b)
7188 {
7189     return a + I*2.0*b;
7190 }
7191 static double _Complex _testfunc25(double a, double b)
7192 {
7193     return a + I*2.0*b;
7194 }
7195 #endif
7196 
7197 static PyObject *b__testfunc(PyObject *self, PyObject *args)
7198 {
7199     /* for testing only */
7200     int i;
7201     void *f;
7202     if (!PyArg_ParseTuple(args, "i:_testfunc", &i))
7203         return NULL;
7204     switch (i) {
7205     case 0: f = &_testfunc0; break;
7206     case 1: f = &_testfunc1; break;
7207     case 2: f = &_testfunc2; break;
7208     case 3: f = &_testfunc3; break;
7209     case 4: f = &_testfunc4; break;
7210     case 5: f = &_testfunc5; break;
7211     case 6: f = &_testfunc6; break;
7212     case 7: f = &_testfunc7; break;
7213     case 8: f = stderr; break;
7214     case 9: f = &_testfunc9; break;
7215     case 10: f = &_testfunc10; break;
7216     case 11: f = &_testfunc11; break;
7217     case 12: f = &_testfunc12; break;
7218     case 13: f = &_testfunc13; break;
7219     case 14: f = &_testfunc14; break;
7220     case 15: f = &_testfunc15; break;
7221     case 16: f = &_testfunc16; break;
7222     case 17: f = &_testfunc17; break;
7223     case 18: f = &_testfunc18; break;
7224     case 19: f = &_testfunc19; break;
7225     case 20: f = &_testfunc20; break;
7226     case 21: f = &_testfunc21; break;
7227     case 22: f = &_testfunc22; break;
7228     case 23: f = &_testfunc23; break;
7229 #if 0
7230     case 24: f = &_testfunc24; break;
7231     case 25: f = &_testfunc25; break;
7232 #endif
7233     default:
7234         PyErr_SetNone(PyExc_ValueError);
7235         return NULL;
7236     }
7237     return PyLong_FromVoidPtr(f);
7238 }
7239 
7240 #if PY_MAJOR_VERSION < 3
7241 static Py_ssize_t _test_segcountproc(PyObject *o, Py_ssize_t *ignored)
7242 {
7243     return 1;
7244 }
7245 static Py_ssize_t _test_getreadbuf(PyObject *o, Py_ssize_t i, void **r)
7246 {
7247     static char buf[] = "RDB";
7248     *r = buf;
7249     return 3;
7250 }
7251 static Py_ssize_t _test_getwritebuf(PyObject *o, Py_ssize_t i, void **r)
7252 {
7253     static char buf[] = "WRB";
7254     *r = buf;
7255     return 3;
7256 }
7257 static Py_ssize_t _test_getcharbuf(PyObject *o, Py_ssize_t i, char **r)
7258 {
7259     static char buf[] = "CHB";
7260     *r = buf;
7261     return 3;
7262 }
7263 #endif
7264 static int _test_getbuf(PyObject *self, Py_buffer *view, int flags)
7265 {
7266     static char buf[] = "GTB";
7267     return PyBuffer_FillInfo(view, self, buf, 3, /*readonly=*/0, flags);
7268 }
7269 static int _test_getbuf_ro(PyObject *self, Py_buffer *view, int flags)
7270 {
7271     static char buf[] = "ROB";
7272     return PyBuffer_FillInfo(view, self, buf, 3, /*readonly=*/1, flags);
7273 }
7274 
7275 
7276 static PyObject *b__testbuff(PyObject *self, PyObject *args)
7277 {
7278     /* for testing only */
7279     int methods;
7280     PyTypeObject *obj;
7281     if (!PyArg_ParseTuple(args, "O!i|_testbuff", &PyType_Type, &obj, &methods))
7282         return NULL;
7283 
7284     assert(obj->tp_as_buffer != NULL);
7285 
7286 #if PY_MAJOR_VERSION < 3
7287     obj->tp_as_buffer->bf_getsegcount = &_test_segcountproc;
7288     obj->tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER;
7289     obj->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
7290     if (methods & 1)  obj->tp_as_buffer->bf_getreadbuffer  = &_test_getreadbuf;
7291     if (methods & 2)  obj->tp_as_buffer->bf_getwritebuffer = &_test_getwritebuf;
7292     if (methods & 4)  obj->tp_as_buffer->bf_getcharbuffer  = &_test_getcharbuf;
7293 #endif
7294     if (methods & 8)  obj->tp_as_buffer->bf_getbuffer      = &_test_getbuf;
7295     if (methods & 16) obj->tp_as_buffer->bf_getbuffer      = &_test_getbuf_ro;
7296 
7297     Py_INCREF(Py_None);
7298     return Py_None;
7299 }
7300 
7301 static PyObject *b_init_cffi_1_0_external_module(PyObject *, PyObject *);
7302 /* forward, see cffi1_module.c */
7303 
7304 
7305 static PyMethodDef FFIBackendMethods[] = {
7306     {"load_library", b_load_library, METH_VARARGS},
7307     {"new_primitive_type", b_new_primitive_type, METH_VARARGS},
7308     {"new_pointer_type", b_new_pointer_type, METH_VARARGS},
7309     {"new_array_type", b_new_array_type, METH_VARARGS},
7310     {"new_void_type", b_new_void_type, METH_NOARGS},
7311     {"new_struct_type", b_new_struct_type, METH_VARARGS},
7312     {"new_union_type", b_new_union_type, METH_VARARGS},
7313     {"complete_struct_or_union", b_complete_struct_or_union, METH_VARARGS},
7314     {"new_function_type", b_new_function_type, METH_VARARGS},
7315     {"new_enum_type", b_new_enum_type, METH_VARARGS},
7316     {"newp", b_newp, METH_VARARGS},
7317     {"cast", b_cast, METH_VARARGS},
7318     {"callback", b_callback, METH_VARARGS},
7319     {"alignof", b_alignof, METH_O},
7320     {"sizeof", b_sizeof, METH_O},
7321     {"typeof", b_typeof, METH_O},
7322     {"typeoffsetof", b_typeoffsetof, METH_VARARGS},
7323     {"rawaddressof", b_rawaddressof, METH_VARARGS},
7324     {"getcname", b_getcname, METH_VARARGS},
7325     {"string", (PyCFunction)b_string, METH_VARARGS | METH_KEYWORDS},
7326     {"unpack", (PyCFunction)b_unpack, METH_VARARGS | METH_KEYWORDS},
7327     {"get_errno", b_get_errno, METH_NOARGS},
7328     {"set_errno", b_set_errno, METH_O},
7329     {"newp_handle", b_newp_handle, METH_VARARGS},
7330     {"from_handle", b_from_handle, METH_O},
7331     {"from_buffer", b_from_buffer, METH_VARARGS},
7332     {"memmove", (PyCFunction)b_memmove, METH_VARARGS | METH_KEYWORDS},
7333     {"gcp", (PyCFunction)b_gcp, METH_VARARGS | METH_KEYWORDS},
7334     {"release", b_release, METH_O},
7335 #ifdef MS_WIN32
7336     {"getwinerror", (PyCFunction)b_getwinerror, METH_VARARGS | METH_KEYWORDS},
7337 #endif
7338     {"_get_types", b__get_types, METH_NOARGS},
7339     {"_get_common_types", b__get_common_types, METH_O},
7340     {"_testfunc", b__testfunc, METH_VARARGS},
7341     {"_testbuff", b__testbuff, METH_VARARGS},
7342     {"_init_cffi_1_0_external_module", b_init_cffi_1_0_external_module, METH_O},
7343     {NULL,     NULL}    /* Sentinel */
7344 };
7345 
7346 /************************************************************/
7347 /* Functions used by '_cffi_N.so', the generated modules    */
7348 
7349 #define _cffi_to_c_SIGNED_FN(RETURNTYPE, SIZE)                          \
7350 static RETURNTYPE _cffi_to_c_i##SIZE(PyObject *obj) {                   \
7351     PY_LONG_LONG tmp = _my_PyLong_AsLongLong(obj);                      \
7352     if ((tmp > (PY_LONG_LONG)((1ULL<<(SIZE-1)) - 1)) ||                 \
7353         (tmp < (PY_LONG_LONG)(0ULL-(1ULL<<(SIZE-1)))))                  \
7354         if (!PyErr_Occurred())                                          \
7355             return (RETURNTYPE)_convert_overflow(obj, #SIZE "-bit int"); \
7356     return (RETURNTYPE)tmp;                                             \
7357 }
7358 
7359 #define _cffi_to_c_UNSIGNED_FN(RETURNTYPE, SIZE)                        \
7360 static RETURNTYPE _cffi_to_c_u##SIZE(PyObject *obj) {                   \
7361     unsigned PY_LONG_LONG tmp = _my_PyLong_AsUnsignedLongLong(obj, 1);  \
7362     if (tmp > ~(((unsigned PY_LONG_LONG)-2) << (SIZE-1)))               \
7363         if (!PyErr_Occurred())                                          \
7364             return (RETURNTYPE)_convert_overflow(obj,                   \
7365                                    #SIZE "-bit unsigned int");          \
7366     return (RETURNTYPE)tmp;                                             \
7367 }
7368 
7369 _cffi_to_c_SIGNED_FN(int, 8)
7370 _cffi_to_c_SIGNED_FN(int, 16)
7371 _cffi_to_c_SIGNED_FN(int, 32)
7372 _cffi_to_c_SIGNED_FN(PY_LONG_LONG, 64)
7373 _cffi_to_c_UNSIGNED_FN(int, 8)
7374 _cffi_to_c_UNSIGNED_FN(int, 16)
7375 _cffi_to_c_UNSIGNED_FN(unsigned int, 32)
7376 _cffi_to_c_UNSIGNED_FN(unsigned PY_LONG_LONG, 64)
7377 
7378 static PyObject *_cffi_from_c_pointer(char *ptr, CTypeDescrObject *ct)
7379 {
7380     return convert_to_object((char *)&ptr, ct);
7381 }
7382 
7383 static char *_cffi_to_c_pointer(PyObject *obj, CTypeDescrObject *ct)
7384 {
7385     char *result;
7386     if (convert_from_object((char *)&result, ct, obj) < 0) {
7387         if ((ct->ct_flags & CT_POINTER) &&
7388                 (ct->ct_itemdescr->ct_flags & CT_IS_FILE) &&
7389                 PyFile_Check(obj)) {
7390             PyErr_Clear();
7391             return (char *)PyFile_AsFile(obj);
7392         }
7393         return NULL;
7394     }
7395     return result;
7396 }
7397 
7398 static long double _cffi_to_c_long_double(PyObject *obj)
7399 {
7400     if (CData_Check(obj) &&
7401             (((CDataObject *)obj)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
7402         char *data = ((CDataObject *)obj)->c_data;
7403         /*READ(data, sizeof(long double))*/
7404         return read_raw_longdouble_data(data);
7405     }
7406     else
7407         return PyFloat_AsDouble(obj);
7408 }
7409 
7410 static _Bool _cffi_to_c__Bool(PyObject *obj)
7411 {
7412     PY_LONG_LONG tmp = _my_PyLong_AsLongLong(obj);
7413     if (tmp == 0)
7414         return 0;
7415     else if (tmp == 1)
7416         return 1;
7417     else if (PyErr_Occurred())
7418         return (_Bool)-1;
7419     else
7420         return (_Bool)_convert_overflow(obj, "_Bool");
7421 }
7422 
7423 static PyObject *_cffi_get_struct_layout(Py_ssize_t nums[])
7424 {
7425     PyObject *result;
7426     int count = 0;
7427     while (nums[count] >= 0)
7428         count++;
7429 
7430     result = PyList_New(count);
7431     if (result == NULL)
7432         return NULL;
7433 
7434     while (--count >= 0) {
7435         PyObject *o = PyInt_FromSsize_t(nums[count]);
7436         if (o == NULL) {
7437             Py_DECREF(result);
7438             return NULL;
7439         }
7440         PyList_SET_ITEM(result, count, o);
7441     }
7442     return result;
7443 }
7444 
7445 static PyObject *_cffi_from_c_char(char x) {
7446     return PyBytes_FromStringAndSize(&x, 1);
7447 }
7448 
7449 /* backward-compatibility hack: instead of _cffi_to_c_char16_t() and
7450  * _cffi_to_c_char32_t(), we have _cffi_to_c_wchar_t() handling whatever
7451  * size is wchar_t, and _cffi_to_c_wchar3216_t() handling the opposite.
7452  */
7453 #ifdef HAVE_WCHAR_H
7454 typedef wchar_t cffi_wchar_t;
7455 #else
7456 typedef uint16_t cffi_wchar_t;   /* random pick... */
7457 #endif
7458 
7459 static cffi_wchar_t _cffi_to_c_wchar_t(PyObject *init)
7460 {
7461     if (sizeof(cffi_wchar_t) == 2)
7462         return (cffi_wchar_t)_convert_to_char16_t(init);
7463     else
7464         return (cffi_wchar_t)_convert_to_char32_t(init);
7465 }
7466 static PyObject *_cffi_from_c_wchar_t(cffi_wchar_t x) {
7467     if (sizeof(cffi_wchar_t) == 2) {
7468         cffi_char16_t input = x;
7469         return _my_PyUnicode_FromChar16(&input, 1);
7470     }
7471     else {
7472         cffi_char32_t input = x;
7473         return _my_PyUnicode_FromChar32(&input, 1);
7474     }
7475 }
7476 static int _cffi_to_c_wchar3216_t(PyObject *init)
7477 {
7478     if (sizeof(cffi_wchar_t) == 4)
7479         return (int)_convert_to_char16_t(init);
7480     else
7481         return (int)_convert_to_char32_t(init);
7482 }
7483 static PyObject *_cffi_from_c_wchar3216_t(int x) {
7484     if (sizeof(cffi_wchar_t) == 4) {
7485         cffi_char16_t input = x;
7486         return _my_PyUnicode_FromChar16(&input, 1);
7487     }
7488     else {
7489         cffi_char32_t input = x;
7490         return _my_PyUnicode_FromChar32(&input, 1);
7491     }
7492 }
7493 
7494 struct _cffi_externpy_s;      /* forward declaration */
7495 static void cffi_call_python(struct _cffi_externpy_s *, char *args);
7496 
7497 static void *cffi_exports[] = {
7498     NULL,
7499     _cffi_to_c_i8,
7500     _cffi_to_c_u8,
7501     _cffi_to_c_i16,
7502     _cffi_to_c_u16,
7503     _cffi_to_c_i32,
7504     _cffi_to_c_u32,
7505     _cffi_to_c_i64,
7506     _cffi_to_c_u64,
7507     _convert_to_char,
7508     _cffi_from_c_pointer,
7509     _cffi_to_c_pointer,
7510     _cffi_get_struct_layout,
7511     restore_errno,
7512     save_errno,
7513     _cffi_from_c_char,
7514     convert_to_object,
7515     convert_from_object,
7516     convert_struct_to_owning_object,
7517     _cffi_to_c_wchar_t,
7518     _cffi_from_c_wchar_t,
7519     _cffi_to_c_long_double,
7520     _cffi_to_c__Bool,
7521     _prepare_pointer_call_argument,
7522     convert_array_from_object,
7523     cffi_call_python,
7524     _cffi_to_c_wchar3216_t,
7525     _cffi_from_c_wchar3216_t,
7526 };
7527 
7528 static struct { const char *name; int value; } all_dlopen_flags[] = {
7529     { "RTLD_LAZY",     RTLD_LAZY     },
7530     { "RTLD_NOW",      RTLD_NOW      },
7531     { "RTLD_GLOBAL",   RTLD_GLOBAL   },
7532 #ifdef RTLD_LOCAL
7533     { "RTLD_LOCAL",    RTLD_LOCAL    },
7534 #else
7535     { "RTLD_LOCAL",    0             },
7536 #endif
7537 #ifdef RTLD_NODELETE
7538     { "RTLD_NODELETE", RTLD_NODELETE },
7539 #endif
7540 #ifdef RTLD_NOLOAD
7541     { "RTLD_NOLOAD",   RTLD_NOLOAD   },
7542 #endif
7543 #ifdef RTLD_DEEPBIND
7544     { "RTLD_DEEPBIND", RTLD_DEEPBIND },
7545 #endif
7546     { NULL, 0 }
7547 };
7548 
7549 
7550 /************************************************************/
7551 
7552 #include "cffi1_module.c"
7553 
7554 /************************************************************/
7555 
7556 #if PY_MAJOR_VERSION >= 3
7557 static struct PyModuleDef FFIBackendModuleDef = {
7558   PyModuleDef_HEAD_INIT,
7559   "_cffi_backend",
7560   NULL,
7561   -1,
7562   FFIBackendMethods,
7563   NULL, NULL, NULL, NULL
7564 };
7565 #define INITERROR return NULL
7566 
7567 PyMODINIT_FUNC
7568 PyInit__cffi_backend(void)
7569 #else
7570 #define INITERROR return
7571 
7572 PyMODINIT_FUNC
7573 init_cffi_backend(void)
7574 #endif
7575 {
7576     PyObject *m, *v;
7577     int i;
7578     static char init_done = 0;
7579 
7580     v = PySys_GetObject("version");
7581     if (v == NULL || !PyText_Check(v) ||
7582             strncmp(PyText_AS_UTF8(v), PY_VERSION, 3) != 0) {
7583         PyErr_Format(PyExc_ImportError,
7584                      "this module was compiled for Python %c%c%c",
7585                      PY_VERSION[0], PY_VERSION[1], PY_VERSION[2]);
7586         INITERROR;
7587     }
7588 
7589 #if PY_MAJOR_VERSION >= 3
7590     m = PyModule_Create(&FFIBackendModuleDef);
7591 #else
7592     m = Py_InitModule("_cffi_backend", FFIBackendMethods);
7593 #endif
7594 
7595     if (m == NULL)
7596         INITERROR;
7597 
7598     if (unique_cache == NULL) {
7599         unique_cache = PyDict_New();
7600         if (unique_cache == NULL)
7601             INITERROR;
7602     }
7603 
7604     if (PyType_Ready(&dl_type) < 0)
7605         INITERROR;
7606     if (PyType_Ready(&CTypeDescr_Type) < 0)
7607         INITERROR;
7608     if (PyType_Ready(&CField_Type) < 0)
7609         INITERROR;
7610     if (PyType_Ready(&CData_Type) < 0)
7611         INITERROR;
7612     if (PyType_Ready(&CDataOwning_Type) < 0)
7613         INITERROR;
7614     if (PyType_Ready(&CDataOwningGC_Type) < 0)
7615         INITERROR;
7616     if (PyType_Ready(&CDataGCP_Type) < 0)
7617         INITERROR;
7618     if (PyType_Ready(&CDataIter_Type) < 0)
7619         INITERROR;
7620     if (PyType_Ready(&MiniBuffer_Type) < 0)
7621         INITERROR;
7622 
7623     if (!init_done) {
7624         v = PyText_FromString("_cffi_backend");
7625         if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict,
7626                                               "__module__", v) < 0)
7627             INITERROR;
7628         v = PyText_FromString("<cdata>");
7629         if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict,
7630                                               "__name__", v) < 0)
7631             INITERROR;
7632         init_done = 1;
7633     }
7634 
7635     /* this is for backward compatibility only */
7636     v = PyCapsule_New((void *)cffi_exports, "cffi", NULL);
7637     if (v == NULL || PyModule_AddObject(m, "_C_API", v) < 0)
7638         INITERROR;
7639 
7640     v = PyText_FromString(CFFI_VERSION);
7641     if (v == NULL || PyModule_AddObject(m, "__version__", v) < 0)
7642         INITERROR;
7643 
7644     if (PyModule_AddIntConstant(m, "FFI_DEFAULT_ABI", FFI_DEFAULT_ABI) < 0 ||
7645 #if defined(MS_WIN32) && !defined(_WIN64)
7646         PyModule_AddIntConstant(m, "FFI_STDCALL", FFI_STDCALL) < 0 ||
7647 #endif
7648         PyModule_AddIntConstant(m, "FFI_CDECL", FFI_DEFAULT_ABI) < 0 ||
7649 
7650 #ifdef MS_WIN32
7651 #  ifdef _WIN64
7652         PyModule_AddIntConstant(m, "_WIN", 64) < 0 ||   /* win64 */
7653 #  else
7654         PyModule_AddIntConstant(m, "_WIN", 32) < 0 ||   /* win32 */
7655 #  endif
7656 #endif
7657         0)
7658       INITERROR;
7659 
7660     for (i = 0; all_dlopen_flags[i].name != NULL; i++) {
7661         if (PyModule_AddIntConstant(m,
7662                                     all_dlopen_flags[i].name,
7663                                     all_dlopen_flags[i].value) < 0)
7664             INITERROR;
7665     }
7666 
7667     Py_INCREF(&MiniBuffer_Type);
7668     if (PyModule_AddObject(m, "buffer", (PyObject *)&MiniBuffer_Type) < 0)
7669         INITERROR;
7670 
7671     init_cffi_tls();
7672     if (PyErr_Occurred())
7673         INITERROR;
7674     init_cffi_tls_zombie();
7675     if (PyErr_Occurred())
7676         INITERROR;
7677 
7678     if (init_ffi_lib(m) < 0)
7679         INITERROR;
7680 
7681 #if PY_MAJOR_VERSION >= 3
7682     if (init_file_emulator() < 0)
7683         INITERROR;
7684     return m;
7685 #endif
7686 }
7687