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