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