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