Changeset 391 for python/trunk/Modules/_struct.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Modules/_struct.c
r2 r391 18 18 #endif 19 19 20 /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input 21 numbers for explicit endians such that they fit in the given type, much 22 like explicit casting in C. A warning will be raised if the number did 23 not originally fit within the range of the requested type. If it is 24 not defined, then all range errors and overflow will be struct.error 25 exceptions. */ 26 27 #define PY_STRUCT_OVERFLOW_MASKING 1 28 29 #ifdef PY_STRUCT_OVERFLOW_MASKING 30 static PyObject *pylong_ulong_mask = NULL; 31 static PyObject *pyint_zero = NULL; 32 #endif 33 34 /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float 35 arguments for integer formats with a warning for backwards 36 compatibility. */ 37 38 #define PY_STRUCT_FLOAT_COERCE 1 39 40 #ifdef PY_STRUCT_FLOAT_COERCE 41 #define FLOAT_COERCE "integer argument expected, got float" 42 #endif 20 /* warning messages */ 21 #define FLOAT_COERCE_WARN "integer argument expected, got float" 22 #define NON_INTEGER_WARN "integer argument expected, got non-integer " \ 23 "(implicit conversion using __int__ is deprecated)" 43 24 44 25 45 26 /* The translation function for each format character is table driven */ 46 27 typedef struct _formatdef { 47 48 49 50 51 52 53 28 char format; 29 Py_ssize_t size; 30 Py_ssize_t alignment; 31 PyObject* (*unpack)(const char *, 32 const struct _formatdef *); 33 int (*pack)(char *, PyObject *, 34 const struct _formatdef *); 54 35 } formatdef; 55 36 56 37 typedef struct _formatcode { 57 58 59 38 const struct _formatdef *fmtdef; 39 Py_ssize_t offset; 40 Py_ssize_t size; 60 41 } formatcode; 61 42 … … 63 44 64 45 typedef struct { 65 66 67 68 69 70 46 PyObject_HEAD 47 Py_ssize_t s_size; 48 Py_ssize_t s_len; 49 formatcode *s_codes; 50 PyObject *s_format; 51 PyObject *weakreflist; /* List of weak references */ 71 52 } PyStructObject; 72 53 … … 120 101 #endif 121 102 103 static char *integer_codes = "bBhHiIlLqQ"; 104 122 105 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */ 123 106 … … 125 108 get_pylong(PyObject *v) 126 109 { 127 PyNumberMethods *m; 128 129 assert(v != NULL); 130 if (PyInt_Check(v)) 131 return PyLong_FromLong(PyInt_AS_LONG(v)); 132 if (PyLong_Check(v)) { 133 Py_INCREF(v); 134 return v; 135 } 136 m = Py_TYPE(v)->tp_as_number; 137 if (m != NULL && m->nb_long != NULL) { 138 v = m->nb_long(v); 139 if (v == NULL) 140 return NULL; 141 if (PyLong_Check(v)) 142 return v; 143 Py_DECREF(v); 144 } 145 PyErr_SetString(StructError, 146 "cannot convert argument to long"); 147 return NULL; 148 } 149 150 /* Helper routine to get a Python integer and raise the appropriate error 151 if it isn't one */ 110 PyObject *r, *w; 111 int converted = 0; 112 assert(v != NULL); 113 if (!PyInt_Check(v) && !PyLong_Check(v)) { 114 PyNumberMethods *m; 115 /* Not an integer; first try to use __index__ to 116 convert to an integer. If the __index__ method 117 doesn't exist, or raises a TypeError, try __int__. 118 Use of the latter is deprecated, and will fail in 119 Python 3.x. */ 120 121 m = Py_TYPE(v)->tp_as_number; 122 if (PyIndex_Check(v)) { 123 w = PyNumber_Index(v); 124 if (w != NULL) { 125 v = w; 126 /* successfully converted to an integer */ 127 converted = 1; 128 } 129 else if (PyErr_ExceptionMatches(PyExc_TypeError)) { 130 PyErr_Clear(); 131 } 132 else 133 return NULL; 134 } 135 if (!converted && m != NULL && m->nb_int != NULL) { 136 /* Special case warning message for floats, for 137 backwards compatibility. */ 138 if (PyFloat_Check(v)) { 139 if (PyErr_WarnEx( 140 PyExc_DeprecationWarning, 141 FLOAT_COERCE_WARN, 1)) 142 return NULL; 143 } 144 else { 145 if (PyErr_WarnEx( 146 PyExc_DeprecationWarning, 147 NON_INTEGER_WARN, 1)) 148 return NULL; 149 } 150 v = m->nb_int(v); 151 if (v == NULL) 152 return NULL; 153 if (!PyInt_Check(v) && !PyLong_Check(v)) { 154 PyErr_SetString(PyExc_TypeError, 155 "__int__ method returned " 156 "non-integer"); 157 return NULL; 158 } 159 converted = 1; 160 } 161 if (!converted) { 162 PyErr_SetString(StructError, 163 "cannot convert argument " 164 "to integer"); 165 return NULL; 166 } 167 } 168 else 169 /* Ensure we own a reference to v. */ 170 Py_INCREF(v); 171 172 assert(PyInt_Check(v) || PyLong_Check(v)); 173 if (PyInt_Check(v)) { 174 r = PyLong_FromLong(PyInt_AS_LONG(v)); 175 Py_DECREF(v); 176 } 177 else if (PyLong_Check(v)) { 178 assert(PyLong_Check(v)); 179 r = v; 180 } 181 else { 182 r = NULL; /* silence compiler warning about 183 possibly uninitialized variable */ 184 assert(0); /* shouldn't ever get here */ 185 } 186 187 return r; 188 } 189 190 /* Helper to convert a Python object to a C long. Sets an exception 191 (struct.error for an inconvertible type, OverflowError for 192 out-of-range values) and returns -1 on error. */ 152 193 153 194 static int 154 195 get_long(PyObject *v, long *p) 155 196 { 156 long x = PyInt_AsLong(v); 157 if (x == -1 && PyErr_Occurred()) { 158 #ifdef PY_STRUCT_FLOAT_COERCE 159 if (PyFloat_Check(v)) { 160 PyObject *o; 161 int res; 162 PyErr_Clear(); 163 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) 164 return -1; 165 o = PyNumber_Int(v); 166 if (o == NULL) 167 return -1; 168 res = get_long(o, p); 169 Py_DECREF(o); 170 return res; 171 } 197 long x; 198 199 v = get_pylong(v); 200 if (v == NULL) 201 return -1; 202 assert(PyLong_Check(v)); 203 x = PyLong_AsLong(v); 204 Py_DECREF(v); 205 if (x == (long)-1 && PyErr_Occurred()) 206 return -1; 207 *p = x; 208 return 0; 209 } 210 211 /* Same, but handling unsigned long */ 212 213 static int 214 get_ulong(PyObject *v, unsigned long *p) 215 { 216 unsigned long x; 217 218 v = get_pylong(v); 219 if (v == NULL) 220 return -1; 221 assert(PyLong_Check(v)); 222 x = PyLong_AsUnsignedLong(v); 223 Py_DECREF(v); 224 if (x == (unsigned long)-1 && PyErr_Occurred()) 225 return -1; 226 *p = x; 227 return 0; 228 } 229 230 #ifdef HAVE_LONG_LONG 231 232 /* Same, but handling native long long. */ 233 234 static int 235 get_longlong(PyObject *v, PY_LONG_LONG *p) 236 { 237 PY_LONG_LONG x; 238 239 v = get_pylong(v); 240 if (v == NULL) 241 return -1; 242 assert(PyLong_Check(v)); 243 x = PyLong_AsLongLong(v); 244 Py_DECREF(v); 245 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) 246 return -1; 247 *p = x; 248 return 0; 249 } 250 251 /* Same, but handling native unsigned long long. */ 252 253 static int 254 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) 255 { 256 unsigned PY_LONG_LONG x; 257 258 v = get_pylong(v); 259 if (v == NULL) 260 return -1; 261 assert(PyLong_Check(v)); 262 x = PyLong_AsUnsignedLongLong(v); 263 Py_DECREF(v); 264 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) 265 return -1; 266 *p = x; 267 return 0; 268 } 269 172 270 #endif 173 if (PyErr_ExceptionMatches(PyExc_TypeError))174 PyErr_SetString(StructError,175 "required argument is not an integer");176 return -1;177 }178 *p = x;179 return 0;180 }181 182 183 /* Same, but handling unsigned long */184 185 #ifndef PY_STRUCT_OVERFLOW_MASKING186 187 static int188 get_ulong(PyObject *v, unsigned long *p)189 {190 if (PyLong_Check(v)) {191 unsigned long x = PyLong_AsUnsignedLong(v);192 if (x == (unsigned long)(-1) && PyErr_Occurred())193 return -1;194 *p = x;195 return 0;196 }197 if (get_long(v, (long *)p) < 0)198 return -1;199 if (((long)*p) < 0) {200 PyErr_SetString(StructError,201 "unsigned argument is < 0");202 return -1;203 }204 return 0;205 }206 207 #endif /* PY_STRUCT_OVERFLOW_MASKING */208 209 #ifdef HAVE_LONG_LONG210 211 /* Same, but handling native long long. */212 213 static int214 get_longlong(PyObject *v, PY_LONG_LONG *p)215 {216 PY_LONG_LONG x;217 218 v = get_pylong(v);219 if (v == NULL)220 return -1;221 assert(PyLong_Check(v));222 x = PyLong_AsLongLong(v);223 Py_DECREF(v);224 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())225 return -1;226 *p = x;227 return 0;228 }229 230 /* Same, but handling native unsigned long long. */231 232 static int233 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)234 {235 unsigned PY_LONG_LONG x;236 237 v = get_pylong(v);238 if (v == NULL)239 return -1;240 assert(PyLong_Check(v));241 x = PyLong_AsUnsignedLongLong(v);242 Py_DECREF(v);243 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())244 return -1;245 *p = x;246 return 0;247 }248 249 #endif250 251 #ifdef PY_STRUCT_OVERFLOW_MASKING252 253 /* Helper routine to get a Python integer and raise the appropriate error254 if it isn't one */255 256 #define INT_OVERFLOW "struct integer overflow masking is deprecated"257 258 static int259 get_wrapped_long(PyObject *v, long *p)260 {261 if (get_long(v, p) < 0) {262 if (PyLong_Check(v) &&263 PyErr_ExceptionMatches(PyExc_OverflowError)) {264 PyObject *wrapped;265 long x;266 PyErr_Clear();267 #ifdef PY_STRUCT_FLOAT_COERCE268 if (PyFloat_Check(v)) {269 PyObject *o;270 int res;271 PyErr_Clear();272 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)273 return -1;274 o = PyNumber_Int(v);275 if (o == NULL)276 return -1;277 res = get_wrapped_long(o, p);278 Py_DECREF(o);279 return res;280 }281 #endif282 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0)283 return -1;284 wrapped = PyNumber_And(v, pylong_ulong_mask);285 if (wrapped == NULL)286 return -1;287 x = (long)PyLong_AsUnsignedLong(wrapped);288 Py_DECREF(wrapped);289 if (x == -1 && PyErr_Occurred())290 return -1;291 *p = x;292 } else {293 return -1;294 }295 }296 return 0;297 }298 299 static int300 get_wrapped_ulong(PyObject *v, unsigned long *p)301 {302 long x = (long)PyLong_AsUnsignedLong(v);303 if (x == -1 && PyErr_Occurred()) {304 PyObject *wrapped;305 PyErr_Clear();306 #ifdef PY_STRUCT_FLOAT_COERCE307 if (PyFloat_Check(v)) {308 PyObject *o;309 int res;310 PyErr_Clear();311 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)312 return -1;313 o = PyNumber_Int(v);314 if (o == NULL)315 return -1;316 res = get_wrapped_ulong(o, p);317 Py_DECREF(o);318 return res;319 }320 #endif321 wrapped = PyNumber_And(v, pylong_ulong_mask);322 if (wrapped == NULL)323 return -1;324 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) {325 Py_DECREF(wrapped);326 return -1;327 }328 x = (long)PyLong_AsUnsignedLong(wrapped);329 Py_DECREF(wrapped);330 if (x == -1 && PyErr_Occurred())331 return -1;332 }333 *p = (unsigned long)x;334 return 0;335 }336 337 #define RANGE_ERROR(x, f, flag, mask) \338 do { \339 if (_range_error(f, flag) < 0) \340 return -1; \341 else \342 (x) &= (mask); \343 } while (0)344 345 #else346 347 #define get_wrapped_long get_long348 #define get_wrapped_ulong get_ulong349 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)350 351 #endif352 271 353 272 /* Floating point helpers */ … … 355 274 static PyObject * 356 275 unpack_float(const char *p, /* start of 4-byte string */ 357 int le)/* true for little-endian, false for big-endian */358 { 359 360 361 362 363 364 276 int le) /* true for little-endian, false for big-endian */ 277 { 278 double x; 279 280 x = _PyFloat_Unpack4((unsigned char *)p, le); 281 if (x == -1.0 && PyErr_Occurred()) 282 return NULL; 283 return PyFloat_FromDouble(x); 365 284 } 366 285 367 286 static PyObject * 368 287 unpack_double(const char *p, /* start of 8-byte string */ 369 370 { 371 372 373 374 375 376 288 int le) /* true for little-endian, false for big-endian */ 289 { 290 double x; 291 292 x = _PyFloat_Unpack8((unsigned char *)p, le); 293 if (x == -1.0 && PyErr_Occurred()) 294 return NULL; 295 return PyFloat_FromDouble(x); 377 296 } 378 297 … … 381 300 _range_error(const formatdef *f, int is_unsigned) 382 301 { 383 /* ulargest is the largest unsigned value with f->size bytes. 384 * Note that the simpler: 385 * ((size_t)1 << (f->size * 8)) - 1 386 * doesn't work when f->size == sizeof(size_t) because C doesn't 387 * define what happens when a left shift count is >= the number of 388 * bits in the integer being shifted; e.g., on some boxes it doesn't 389 * shift at all when they're equal. 390 */ 391 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); 392 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); 393 if (is_unsigned) 394 PyErr_Format(StructError, 395 "'%c' format requires 0 <= number <= %zu", 396 f->format, 397 ulargest); 398 else { 399 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); 400 PyErr_Format(StructError, 401 "'%c' format requires %zd <= number <= %zd", 402 f->format, 403 ~ largest, 404 largest); 405 } 406 #ifdef PY_STRUCT_OVERFLOW_MASKING 407 { 408 PyObject *ptype, *pvalue, *ptraceback; 409 PyObject *msg; 410 int rval; 411 PyErr_Fetch(&ptype, &pvalue, &ptraceback); 412 assert(pvalue != NULL); 413 msg = PyObject_Str(pvalue); 414 Py_XDECREF(ptype); 415 Py_XDECREF(pvalue); 416 Py_XDECREF(ptraceback); 417 if (msg == NULL) 418 return -1; 419 rval = PyErr_WarnEx(PyExc_DeprecationWarning, 420 PyString_AS_STRING(msg), 2); 421 Py_DECREF(msg); 422 if (rval == 0) 423 return 0; 424 } 425 #endif 426 return -1; 302 /* ulargest is the largest unsigned value with f->size bytes. 303 * Note that the simpler: 304 * ((size_t)1 << (f->size * 8)) - 1 305 * doesn't work when f->size == sizeof(size_t) because C doesn't 306 * define what happens when a left shift count is >= the number of 307 * bits in the integer being shifted; e.g., on some boxes it doesn't 308 * shift at all when they're equal. 309 */ 310 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); 311 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); 312 if (is_unsigned) 313 PyErr_Format(StructError, 314 "'%c' format requires 0 <= number <= %zu", 315 f->format, 316 ulargest); 317 else { 318 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); 319 PyErr_Format(StructError, 320 "'%c' format requires %zd <= number <= %zd", 321 f->format, 322 ~ largest, 323 largest); 324 } 325 return -1; 427 326 } 428 327 … … 451 350 nu_char(const char *p, const formatdef *f) 452 351 { 453 352 return PyString_FromStringAndSize(p, 1); 454 353 } 455 354 … … 457 356 nu_byte(const char *p, const formatdef *f) 458 357 { 459 358 return PyInt_FromLong((long) *(signed char *)p); 460 359 } 461 360 … … 463 362 nu_ubyte(const char *p, const formatdef *f) 464 363 { 465 364 return PyInt_FromLong((long) *(unsigned char *)p); 466 365 } 467 366 … … 469 368 nu_short(const char *p, const formatdef *f) 470 369 { 471 472 473 370 short x; 371 memcpy((char *)&x, p, sizeof x); 372 return PyInt_FromLong((long)x); 474 373 } 475 374 … … 477 376 nu_ushort(const char *p, const formatdef *f) 478 377 { 479 480 481 378 unsigned short x; 379 memcpy((char *)&x, p, sizeof x); 380 return PyInt_FromLong((long)x); 482 381 } 483 382 … … 485 384 nu_int(const char *p, const formatdef *f) 486 385 { 487 488 489 386 int x; 387 memcpy((char *)&x, p, sizeof x); 388 return PyInt_FromLong((long)x); 490 389 } 491 390 … … 493 392 nu_uint(const char *p, const formatdef *f) 494 393 { 495 496 394 unsigned int x; 395 memcpy((char *)&x, p, sizeof x); 497 396 #if (SIZEOF_LONG > SIZEOF_INT) 498 397 return PyInt_FromLong((long)x); 499 398 #else 500 501 502 399 if (x <= ((unsigned int)LONG_MAX)) 400 return PyInt_FromLong((long)x); 401 return PyLong_FromUnsignedLong((unsigned long)x); 503 402 #endif 504 403 } … … 507 406 nu_long(const char *p, const formatdef *f) 508 407 { 509 510 511 408 long x; 409 memcpy((char *)&x, p, sizeof x); 410 return PyInt_FromLong(x); 512 411 } 513 412 … … 515 414 nu_ulong(const char *p, const formatdef *f) 516 415 { 517 518 519 520 521 416 unsigned long x; 417 memcpy((char *)&x, p, sizeof x); 418 if (x <= LONG_MAX) 419 return PyInt_FromLong((long)x); 420 return PyLong_FromUnsignedLong(x); 522 421 } 523 422 … … 530 429 nu_longlong(const char *p, const formatdef *f) 531 430 { 532 533 534 535 536 431 PY_LONG_LONG x; 432 memcpy((char *)&x, p, sizeof x); 433 if (x >= LONG_MIN && x <= LONG_MAX) 434 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); 435 return PyLong_FromLongLong(x); 537 436 } 538 437 … … 540 439 nu_ulonglong(const char *p, const formatdef *f) 541 440 { 542 543 544 545 546 441 unsigned PY_LONG_LONG x; 442 memcpy((char *)&x, p, sizeof x); 443 if (x <= LONG_MAX) 444 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); 445 return PyLong_FromUnsignedLongLong(x); 547 446 } 548 447 … … 552 451 nu_bool(const char *p, const formatdef *f) 553 452 { 554 555 556 453 BOOL_TYPE x; 454 memcpy((char *)&x, p, sizeof x); 455 return PyBool_FromLong(x != 0); 557 456 } 558 457 … … 561 460 nu_float(const char *p, const formatdef *f) 562 461 { 563 564 565 462 float x; 463 memcpy((char *)&x, p, sizeof x); 464 return PyFloat_FromDouble((double)x); 566 465 } 567 466 … … 569 468 nu_double(const char *p, const formatdef *f) 570 469 { 571 572 573 470 double x; 471 memcpy((char *)&x, p, sizeof x); 472 return PyFloat_FromDouble(x); 574 473 } 575 474 … … 577 476 nu_void_p(const char *p, const formatdef *f) 578 477 { 579 580 581 478 void *x; 479 memcpy((char *)&x, p, sizeof x); 480 return PyLong_FromVoidPtr(x); 582 481 } 583 482 … … 585 484 np_byte(char *p, PyObject *v, const formatdef *f) 586 485 { 587 588 589 590 591 592 593 594 595 596 486 long x; 487 if (get_long(v, &x) < 0) 488 return -1; 489 if (x < -128 || x > 127){ 490 PyErr_SetString(StructError, 491 "byte format requires -128 <= number <= 127"); 492 return -1; 493 } 494 *p = (char)x; 495 return 0; 597 496 } 598 497 … … 600 499 np_ubyte(char *p, PyObject *v, const formatdef *f) 601 500 { 602 603 604 605 606 607 608 609 610 611 501 long x; 502 if (get_long(v, &x) < 0) 503 return -1; 504 if (x < 0 || x > 255){ 505 PyErr_SetString(StructError, 506 "ubyte format requires 0 <= number <= 255"); 507 return -1; 508 } 509 *p = (char)x; 510 return 0; 612 511 } 613 512 … … 615 514 np_char(char *p, PyObject *v, const formatdef *f) 616 515 { 617 618 619 620 621 622 623 516 if (!PyString_Check(v) || PyString_Size(v) != 1) { 517 PyErr_SetString(StructError, 518 "char format require string of length 1"); 519 return -1; 520 } 521 *p = *PyString_AsString(v); 522 return 0; 624 523 } 625 524 … … 627 526 np_short(char *p, PyObject *v, const formatdef *f) 628 527 { 629 630 631 632 633 634 635 636 637 638 639 640 641 528 long x; 529 short y; 530 if (get_long(v, &x) < 0) 531 return -1; 532 if (x < SHRT_MIN || x > SHRT_MAX){ 533 PyErr_SetString(StructError, 534 "short format requires " STRINGIFY(SHRT_MIN) 535 " <= number <= " STRINGIFY(SHRT_MAX)); 536 return -1; 537 } 538 y = (short)x; 539 memcpy(p, (char *)&y, sizeof y); 540 return 0; 642 541 } 643 542 … … 645 544 np_ushort(char *p, PyObject *v, const formatdef *f) 646 545 { 647 648 649 650 651 652 653 654 655 656 657 658 546 long x; 547 unsigned short y; 548 if (get_long(v, &x) < 0) 549 return -1; 550 if (x < 0 || x > USHRT_MAX){ 551 PyErr_SetString(StructError, 552 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); 553 return -1; 554 } 555 y = (unsigned short)x; 556 memcpy(p, (char *)&y, sizeof y); 557 return 0; 659 558 } 660 559 … … 662 561 np_int(char *p, PyObject *v, const formatdef *f) 663 562 { 664 665 666 667 563 long x; 564 int y; 565 if (get_long(v, &x) < 0) 566 return -1; 668 567 #if (SIZEOF_LONG > SIZEOF_INT) 669 670 RANGE_ERROR(x, f, 0, -1);568 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) 569 return _range_error(f, 0); 671 570 #endif 672 673 674 571 y = (int)x; 572 memcpy(p, (char *)&y, sizeof y); 573 return 0; 675 574 } 676 575 … … 678 577 np_uint(char *p, PyObject *v, const formatdef *f) 679 578 { 680 681 682 if (get_wrapped_ulong(v, &x) < 0)683 684 579 unsigned long x; 580 unsigned int y; 581 if (get_ulong(v, &x) < 0) 582 return -1; 583 y = (unsigned int)x; 685 584 #if (SIZEOF_LONG > SIZEOF_INT) 686 687 RANGE_ERROR(y, f, 1, -1);585 if (x > ((unsigned long)UINT_MAX)) 586 return _range_error(f, 1); 688 587 #endif 689 690 588 memcpy(p, (char *)&y, sizeof y); 589 return 0; 691 590 } 692 591 … … 694 593 np_long(char *p, PyObject *v, const formatdef *f) 695 594 { 696 697 698 699 700 595 long x; 596 if (get_long(v, &x) < 0) 597 return -1; 598 memcpy(p, (char *)&x, sizeof x); 599 return 0; 701 600 } 702 601 … … 704 603 np_ulong(char *p, PyObject *v, const formatdef *f) 705 604 { 706 707 if (get_wrapped_ulong(v, &x) < 0)708 709 710 605 unsigned long x; 606 if (get_ulong(v, &x) < 0) 607 return -1; 608 memcpy(p, (char *)&x, sizeof x); 609 return 0; 711 610 } 712 611 … … 716 615 np_longlong(char *p, PyObject *v, const formatdef *f) 717 616 { 718 719 720 721 722 617 PY_LONG_LONG x; 618 if (get_longlong(v, &x) < 0) 619 return -1; 620 memcpy(p, (char *)&x, sizeof x); 621 return 0; 723 622 } 724 623 … … 726 625 np_ulonglong(char *p, PyObject *v, const formatdef *f) 727 626 { 728 729 730 731 732 627 unsigned PY_LONG_LONG x; 628 if (get_ulonglong(v, &x) < 0) 629 return -1; 630 memcpy(p, (char *)&x, sizeof x); 631 return 0; 733 632 } 734 633 #endif … … 738 637 np_bool(char *p, PyObject *v, const formatdef *f) 739 638 { 740 BOOL_TYPE y; 741 y = PyObject_IsTrue(v); 742 memcpy(p, (char *)&y, sizeof y); 743 return 0; 639 int y; 640 BOOL_TYPE x; 641 y = PyObject_IsTrue(v); 642 if (y < 0) 643 return -1; 644 x = y; 645 memcpy(p, (char *)&x, sizeof x); 646 return 0; 744 647 } 745 648 … … 747 650 np_float(char *p, PyObject *v, const formatdef *f) 748 651 { 749 750 751 752 753 754 755 756 652 float x = (float)PyFloat_AsDouble(v); 653 if (x == -1 && PyErr_Occurred()) { 654 PyErr_SetString(StructError, 655 "required argument is not a float"); 656 return -1; 657 } 658 memcpy(p, (char *)&x, sizeof x); 659 return 0; 757 660 } 758 661 … … 760 663 np_double(char *p, PyObject *v, const formatdef *f) 761 664 { 762 763 764 765 766 767 768 769 665 double x = PyFloat_AsDouble(v); 666 if (x == -1 && PyErr_Occurred()) { 667 PyErr_SetString(StructError, 668 "required argument is not a float"); 669 return -1; 670 } 671 memcpy(p, (char *)&x, sizeof(double)); 672 return 0; 770 673 } 771 674 … … 773 676 np_void_p(char *p, PyObject *v, const formatdef *f) 774 677 { 775 776 777 778 779 780 781 782 783 784 785 786 678 void *x; 679 680 v = get_pylong(v); 681 if (v == NULL) 682 return -1; 683 assert(PyLong_Check(v)); 684 x = PyLong_AsVoidPtr(v); 685 Py_DECREF(v); 686 if (x == NULL && PyErr_Occurred()) 687 return -1; 688 memcpy(p, (char *)&x, sizeof x); 689 return 0; 787 690 } 788 691 789 692 static formatdef native_table[] = { 790 {'x', sizeof(char), 0,NULL},791 {'b', sizeof(char), 0, nu_byte,np_byte},792 {'B', sizeof(char), 0, nu_ubyte,np_ubyte},793 {'c', sizeof(char), 0, nu_char,np_char},794 {'s', sizeof(char), 0,NULL},795 {'p', sizeof(char), 0,NULL},796 {'h', sizeof(short), SHORT_ALIGN, nu_short,np_short},797 {'H', sizeof(short), SHORT_ALIGN, nu_ushort,np_ushort},798 {'i', sizeof(int), INT_ALIGN, nu_int,np_int},799 {'I', sizeof(int), INT_ALIGN, nu_uint,np_uint},800 {'l', sizeof(long), LONG_ALIGN, nu_long,np_long},801 {'L', sizeof(long), LONG_ALIGN, nu_ulong,np_ulong},693 {'x', sizeof(char), 0, NULL}, 694 {'b', sizeof(char), 0, nu_byte, np_byte}, 695 {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, 696 {'c', sizeof(char), 0, nu_char, np_char}, 697 {'s', sizeof(char), 0, NULL}, 698 {'p', sizeof(char), 0, NULL}, 699 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, 700 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, 701 {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, 702 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, 703 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, 704 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, 802 705 #ifdef HAVE_LONG_LONG 803 {'q',sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},804 {'Q',sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},706 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, 707 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, 805 708 #endif 806 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool,np_bool},807 {'f', sizeof(float), FLOAT_ALIGN, nu_float,np_float},808 {'d', sizeof(double), DOUBLE_ALIGN, nu_double,np_double},809 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p,np_void_p},810 709 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool}, 710 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, 711 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, 712 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, 713 {0} 811 714 }; 812 715 … … 816 719 bu_int(const char *p, const formatdef *f) 817 720 { 818 819 820 821 822 823 824 825 826 827 721 long x = 0; 722 Py_ssize_t i = f->size; 723 const unsigned char *bytes = (const unsigned char *)p; 724 do { 725 x = (x<<8) | *bytes++; 726 } while (--i > 0); 727 /* Extend the sign bit. */ 728 if (SIZEOF_LONG > f->size) 729 x |= -(x & (1L << ((8 * f->size) - 1))); 730 return PyInt_FromLong(x); 828 731 } 829 732 … … 831 734 bu_uint(const char *p, const formatdef *f) 832 735 { 833 834 835 836 837 838 839 840 841 736 unsigned long x = 0; 737 Py_ssize_t i = f->size; 738 const unsigned char *bytes = (const unsigned char *)p; 739 do { 740 x = (x<<8) | *bytes++; 741 } while (--i > 0); 742 if (x <= LONG_MAX) 743 return PyInt_FromLong((long)x); 744 return PyLong_FromUnsignedLong(x); 842 745 } 843 746 … … 846 749 { 847 750 #ifdef HAVE_LONG_LONG 848 849 850 851 852 853 854 855 856 857 858 859 751 PY_LONG_LONG x = 0; 752 Py_ssize_t i = f->size; 753 const unsigned char *bytes = (const unsigned char *)p; 754 do { 755 x = (x<<8) | *bytes++; 756 } while (--i > 0); 757 /* Extend the sign bit. */ 758 if (SIZEOF_LONG_LONG > f->size) 759 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); 760 if (x >= LONG_MIN && x <= LONG_MAX) 761 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); 762 return PyLong_FromLongLong(x); 860 763 #else 861 862 863 864 764 return _PyLong_FromByteArray((const unsigned char *)p, 765 8, 766 0, /* little-endian */ 767 1 /* signed */); 865 768 #endif 866 769 } … … 870 773 { 871 774 #ifdef HAVE_LONG_LONG 872 873 874 875 876 877 878 879 880 775 unsigned PY_LONG_LONG x = 0; 776 Py_ssize_t i = f->size; 777 const unsigned char *bytes = (const unsigned char *)p; 778 do { 779 x = (x<<8) | *bytes++; 780 } while (--i > 0); 781 if (x <= LONG_MAX) 782 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); 783 return PyLong_FromUnsignedLongLong(x); 881 784 #else 882 883 884 885 785 return _PyLong_FromByteArray((const unsigned char *)p, 786 8, 787 0, /* little-endian */ 788 0 /* signed */); 886 789 #endif 887 790 } … … 890 793 bu_float(const char *p, const formatdef *f) 891 794 { 892 795 return unpack_float(p, 0); 893 796 } 894 797 … … 896 799 bu_double(const char *p, const formatdef *f) 897 800 { 898 801 return unpack_double(p, 0); 899 802 } 900 803 … … 902 805 bu_bool(const char *p, const formatdef *f) 903 806 { 904 905 906 807 char x; 808 memcpy((char *)&x, p, sizeof x); 809 return PyBool_FromLong(x != 0); 907 810 } 908 811 … … 910 813 bp_int(char *p, PyObject *v, const formatdef *f) 911 814 { 912 913 914 if (get_wrapped_long(v, &x) < 0)915 916 917 918 919 RANGE_ERROR(x, f, 0, 0xffffL);815 long x; 816 Py_ssize_t i; 817 if (get_long(v, &x) < 0) 818 return -1; 819 i = f->size; 820 if (i != SIZEOF_LONG) { 821 if ((i == 2) && (x < -32768 || x > 32767)) 822 return _range_error(f, 0); 920 823 #if (SIZEOF_LONG != 4) 921 922 RANGE_ERROR(x, f, 0, 0xffffffffL);824 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) 825 return _range_error(f, 0); 923 826 #endif 924 #ifdef PY_STRUCT_OVERFLOW_MASKING 925 else if ((i == 1) && (x < -128 || x > 127)) 926 RANGE_ERROR(x, f, 0, 0xffL); 827 } 828 do { 829 p[--i] = (char)x; 830 x >>= 8; 831 } while (i > 0); 832 return 0; 833 } 834 835 static int 836 bp_uint(char *p, PyObject *v, const formatdef *f) 837 { 838 unsigned long x; 839 Py_ssize_t i; 840 if (get_ulong(v, &x) < 0) 841 return -1; 842 i = f->size; 843 if (i != SIZEOF_LONG) { 844 unsigned long maxint = 1; 845 maxint <<= (unsigned long)(i * 8); 846 if (x >= maxint) 847 return _range_error(f, 1); 848 } 849 do { 850 p[--i] = (char)x; 851 x >>= 8; 852 } while (i > 0); 853 return 0; 854 } 855 856 static int 857 bp_longlong(char *p, PyObject *v, const formatdef *f) 858 { 859 int res; 860 v = get_pylong(v); 861 if (v == NULL) 862 return -1; 863 res = _PyLong_AsByteArray((PyLongObject *)v, 864 (unsigned char *)p, 865 8, 866 0, /* little_endian */ 867 1 /* signed */); 868 Py_DECREF(v); 869 return res; 870 } 871 872 static int 873 bp_ulonglong(char *p, PyObject *v, const formatdef *f) 874 { 875 int res; 876 v = get_pylong(v); 877 if (v == NULL) 878 return -1; 879 res = _PyLong_AsByteArray((PyLongObject *)v, 880 (unsigned char *)p, 881 8, 882 0, /* little_endian */ 883 0 /* signed */); 884 Py_DECREF(v); 885 return res; 886 } 887 888 static int 889 bp_float(char *p, PyObject *v, const formatdef *f) 890 { 891 double x = PyFloat_AsDouble(v); 892 if (x == -1 && PyErr_Occurred()) { 893 PyErr_SetString(StructError, 894 "required argument is not a float"); 895 return -1; 896 } 897 return _PyFloat_Pack4(x, (unsigned char *)p, 0); 898 } 899 900 static int 901 bp_double(char *p, PyObject *v, const formatdef *f) 902 { 903 double x = PyFloat_AsDouble(v); 904 if (x == -1 && PyErr_Occurred()) { 905 PyErr_SetString(StructError, 906 "required argument is not a float"); 907 return -1; 908 } 909 return _PyFloat_Pack8(x, (unsigned char *)p, 0); 910 } 911 912 static int 913 bp_bool(char *p, PyObject *v, const formatdef *f) 914 { 915 int y; 916 y = PyObject_IsTrue(v); 917 if (y < 0) 918 return -1; 919 *p = (char)y; 920 return 0; 921 } 922 923 static formatdef bigendian_table[] = { 924 {'x', 1, 0, NULL}, 925 {'b', 1, 0, nu_byte, np_byte}, 926 {'B', 1, 0, nu_ubyte, np_ubyte}, 927 {'c', 1, 0, nu_char, np_char}, 928 {'s', 1, 0, NULL}, 929 {'p', 1, 0, NULL}, 930 {'h', 2, 0, bu_int, bp_int}, 931 {'H', 2, 0, bu_uint, bp_uint}, 932 {'i', 4, 0, bu_int, bp_int}, 933 {'I', 4, 0, bu_uint, bp_uint}, 934 {'l', 4, 0, bu_int, bp_int}, 935 {'L', 4, 0, bu_uint, bp_uint}, 936 {'q', 8, 0, bu_longlong, bp_longlong}, 937 {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, 938 {'?', 1, 0, bu_bool, bp_bool}, 939 {'f', 4, 0, bu_float, bp_float}, 940 {'d', 8, 0, bu_double, bp_double}, 941 {0} 942 }; 943 944 /* Little-endian routines. *****************************************************/ 945 946 static PyObject * 947 lu_int(const char *p, const formatdef *f) 948 { 949 long x = 0; 950 Py_ssize_t i = f->size; 951 const unsigned char *bytes = (const unsigned char *)p; 952 do { 953 x = (x<<8) | bytes[--i]; 954 } while (i > 0); 955 /* Extend the sign bit. */ 956 if (SIZEOF_LONG > f->size) 957 x |= -(x & (1L << ((8 * f->size) - 1))); 958 return PyInt_FromLong(x); 959 } 960 961 static PyObject * 962 lu_uint(const char *p, const formatdef *f) 963 { 964 unsigned long x = 0; 965 Py_ssize_t i = f->size; 966 const unsigned char *bytes = (const unsigned char *)p; 967 do { 968 x = (x<<8) | bytes[--i]; 969 } while (i > 0); 970 if (x <= LONG_MAX) 971 return PyInt_FromLong((long)x); 972 return PyLong_FromUnsignedLong((long)x); 973 } 974 975 static PyObject * 976 lu_longlong(const char *p, const formatdef *f) 977 { 978 #ifdef HAVE_LONG_LONG 979 PY_LONG_LONG x = 0; 980 Py_ssize_t i = f->size; 981 const unsigned char *bytes = (const unsigned char *)p; 982 do { 983 x = (x<<8) | bytes[--i]; 984 } while (i > 0); 985 /* Extend the sign bit. */ 986 if (SIZEOF_LONG_LONG > f->size) 987 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); 988 if (x >= LONG_MIN && x <= LONG_MAX) 989 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); 990 return PyLong_FromLongLong(x); 991 #else 992 return _PyLong_FromByteArray((const unsigned char *)p, 993 8, 994 1, /* little-endian */ 995 1 /* signed */); 927 996 #endif 928 } 929 do { 930 p[--i] = (char)x; 931 x >>= 8; 932 } while (i > 0); 933 return 0; 934 } 935 936 static int 937 bp_uint(char *p, PyObject *v, const formatdef *f) 938 { 939 unsigned long x; 940 Py_ssize_t i; 941 if (get_wrapped_ulong(v, &x) < 0) 942 return -1; 943 i = f->size; 944 if (i != SIZEOF_LONG) { 945 unsigned long maxint = 1; 946 maxint <<= (unsigned long)(i * 8); 947 if (x >= maxint) 948 RANGE_ERROR(x, f, 1, maxint - 1); 949 } 950 do { 951 p[--i] = (char)x; 952 x >>= 8; 953 } while (i > 0); 954 return 0; 955 } 956 957 static int 958 bp_longlong(char *p, PyObject *v, const formatdef *f) 959 { 960 int res; 961 v = get_pylong(v); 962 if (v == NULL) 963 return -1; 964 res = _PyLong_AsByteArray((PyLongObject *)v, 965 (unsigned char *)p, 966 8, 967 0, /* little_endian */ 968 1 /* signed */); 969 Py_DECREF(v); 970 return res; 971 } 972 973 static int 974 bp_ulonglong(char *p, PyObject *v, const formatdef *f) 975 { 976 int res; 977 v = get_pylong(v); 978 if (v == NULL) 979 return -1; 980 res = _PyLong_AsByteArray((PyLongObject *)v, 981 (unsigned char *)p, 982 8, 983 0, /* little_endian */ 984 0 /* signed */); 985 Py_DECREF(v); 986 return res; 987 } 988 989 static int 990 bp_float(char *p, PyObject *v, const formatdef *f) 991 { 992 double x = PyFloat_AsDouble(v); 993 if (x == -1 && PyErr_Occurred()) { 994 PyErr_SetString(StructError, 995 "required argument is not a float"); 996 return -1; 997 } 998 return _PyFloat_Pack4(x, (unsigned char *)p, 0); 999 } 1000 1001 static int 1002 bp_double(char *p, PyObject *v, const formatdef *f) 1003 { 1004 double x = PyFloat_AsDouble(v); 1005 if (x == -1 && PyErr_Occurred()) { 1006 PyErr_SetString(StructError, 1007 "required argument is not a float"); 1008 return -1; 1009 } 1010 return _PyFloat_Pack8(x, (unsigned char *)p, 0); 1011 } 1012 1013 static int 1014 bp_bool(char *p, PyObject *v, const formatdef *f) 1015 { 1016 char y; 1017 y = PyObject_IsTrue(v); 1018 memcpy(p, (char *)&y, sizeof y); 1019 return 0; 1020 } 1021 1022 static formatdef bigendian_table[] = { 1023 {'x', 1, 0, NULL}, 1024 #ifdef PY_STRUCT_OVERFLOW_MASKING 1025 /* Native packers do range checking without overflow masking. */ 1026 {'b', 1, 0, nu_byte, bp_int}, 1027 {'B', 1, 0, nu_ubyte, bp_uint}, 997 } 998 999 static PyObject * 1000 lu_ulonglong(const char *p, const formatdef *f) 1001 { 1002 #ifdef HAVE_LONG_LONG 1003 unsigned PY_LONG_LONG x = 0; 1004 Py_ssize_t i = f->size; 1005 const unsigned char *bytes = (const unsigned char *)p; 1006 do { 1007 x = (x<<8) | bytes[--i]; 1008 } while (i > 0); 1009 if (x <= LONG_MAX) 1010 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); 1011 return PyLong_FromUnsignedLongLong(x); 1028 1012 #else 1029 {'b', 1, 0, nu_byte, np_byte}, 1030 {'B', 1, 0, nu_ubyte, np_ubyte}, 1013 return _PyLong_FromByteArray((const unsigned char *)p, 1014 8, 1015 1, /* little-endian */ 1016 0 /* signed */); 1031 1017 #endif 1032 {'c', 1, 0, nu_char, np_char}, 1033 {'s', 1, 0, NULL}, 1034 {'p', 1, 0, NULL}, 1035 {'h', 2, 0, bu_int, bp_int}, 1036 {'H', 2, 0, bu_uint, bp_uint}, 1037 {'i', 4, 0, bu_int, bp_int}, 1038 {'I', 4, 0, bu_uint, bp_uint}, 1039 {'l', 4, 0, bu_int, bp_int}, 1040 {'L', 4, 0, bu_uint, bp_uint}, 1041 {'q', 8, 0, bu_longlong, bp_longlong}, 1042 {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, 1043 {'?', 1, 0, bu_bool, bp_bool}, 1044 {'f', 4, 0, bu_float, bp_float}, 1045 {'d', 8, 0, bu_double, bp_double}, 1046 {0} 1047 }; 1048 1049 /* Little-endian routines. *****************************************************/ 1050 1051 static PyObject * 1052 lu_int(const char *p, const formatdef *f) 1053 { 1054 long x = 0; 1055 Py_ssize_t i = f->size; 1056 const unsigned char *bytes = (const unsigned char *)p; 1057 do { 1058 x = (x<<8) | bytes[--i]; 1059 } while (i > 0); 1060 /* Extend the sign bit. */ 1061 if (SIZEOF_LONG > f->size) 1062 x |= -(x & (1L << ((8 * f->size) - 1))); 1063 return PyInt_FromLong(x); 1064 } 1065 1066 static PyObject * 1067 lu_uint(const char *p, const formatdef *f) 1068 { 1069 unsigned long x = 0; 1070 Py_ssize_t i = f->size; 1071 const unsigned char *bytes = (const unsigned char *)p; 1072 do { 1073 x = (x<<8) | bytes[--i]; 1074 } while (i > 0); 1075 if (x <= LONG_MAX) 1076 return PyInt_FromLong((long)x); 1077 return PyLong_FromUnsignedLong((long)x); 1078 } 1079 1080 static PyObject * 1081 lu_longlong(const char *p, const formatdef *f) 1082 { 1083 #ifdef HAVE_LONG_LONG 1084 PY_LONG_LONG x = 0; 1085 Py_ssize_t i = f->size; 1086 const unsigned char *bytes = (const unsigned char *)p; 1087 do { 1088 x = (x<<8) | bytes[--i]; 1089 } while (i > 0); 1090 /* Extend the sign bit. */ 1091 if (SIZEOF_LONG_LONG > f->size) 1092 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); 1093 if (x >= LONG_MIN && x <= LONG_MAX) 1094 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); 1095 return PyLong_FromLongLong(x); 1096 #else 1097 return _PyLong_FromByteArray((const unsigned char *)p, 1098 8, 1099 1, /* little-endian */ 1100 1 /* signed */); 1018 } 1019 1020 static PyObject * 1021 lu_float(const char *p, const formatdef *f) 1022 { 1023 return unpack_float(p, 1); 1024 } 1025 1026 static PyObject * 1027 lu_double(const char *p, const formatdef *f) 1028 { 1029 return unpack_double(p, 1); 1030 } 1031 1032 static int 1033 lp_int(char *p, PyObject *v, const formatdef *f) 1034 { 1035 long x; 1036 Py_ssize_t i; 1037 if (get_long(v, &x) < 0) 1038 return -1; 1039 i = f->size; 1040 if (i != SIZEOF_LONG) { 1041 if ((i == 2) && (x < -32768 || x > 32767)) 1042 return _range_error(f, 0); 1043 #if (SIZEOF_LONG != 4) 1044 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) 1045 return _range_error(f, 0); 1101 1046 #endif 1102 } 1103 1104 static PyObject * 1105 lu_ulonglong(const char *p, const formatdef *f) 1106 { 1107 #ifdef HAVE_LONG_LONG 1108 unsigned PY_LONG_LONG x = 0; 1109 Py_ssize_t i = f->size; 1110 const unsigned char *bytes = (const unsigned char *)p; 1111 do { 1112 x = (x<<8) | bytes[--i]; 1113 } while (i > 0); 1114 if (x <= LONG_MAX) 1115 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); 1116 return PyLong_FromUnsignedLongLong(x); 1117 #else 1118 return _PyLong_FromByteArray((const unsigned char *)p, 1119 8, 1120 1, /* little-endian */ 1121 0 /* signed */); 1122 #endif 1123 } 1124 1125 static PyObject * 1126 lu_float(const char *p, const formatdef *f) 1127 { 1128 return unpack_float(p, 1); 1129 } 1130 1131 static PyObject * 1132 lu_double(const char *p, const formatdef *f) 1133 { 1134 return unpack_double(p, 1); 1135 } 1136 1137 static int 1138 lp_int(char *p, PyObject *v, const formatdef *f) 1139 { 1140 long x; 1141 Py_ssize_t i; 1142 if (get_wrapped_long(v, &x) < 0) 1143 return -1; 1144 i = f->size; 1145 if (i != SIZEOF_LONG) { 1146 if ((i == 2) && (x < -32768 || x > 32767)) 1147 RANGE_ERROR(x, f, 0, 0xffffL); 1148 #if (SIZEOF_LONG != 4) 1149 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) 1150 RANGE_ERROR(x, f, 0, 0xffffffffL); 1151 #endif 1152 #ifdef PY_STRUCT_OVERFLOW_MASKING 1153 else if ((i == 1) && (x < -128 || x > 127)) 1154 RANGE_ERROR(x, f, 0, 0xffL); 1155 #endif 1156 } 1157 do { 1158 *p++ = (char)x; 1159 x >>= 8; 1160 } while (--i > 0); 1161 return 0; 1047 } 1048 do { 1049 *p++ = (char)x; 1050 x >>= 8; 1051 } while (--i > 0); 1052 return 0; 1162 1053 } 1163 1054 … … 1165 1056 lp_uint(char *p, PyObject *v, const formatdef *f) 1166 1057 { 1167 1168 1169 if (get_wrapped_ulong(v, &x) < 0)1170 1171 1172 1173 1174 1175 1176 RANGE_ERROR(x, f, 1, maxint -1);1177 1178 1179 1180 1181 1182 1058 unsigned long x; 1059 Py_ssize_t i; 1060 if (get_ulong(v, &x) < 0) 1061 return -1; 1062 i = f->size; 1063 if (i != SIZEOF_LONG) { 1064 unsigned long maxint = 1; 1065 maxint <<= (unsigned long)(i * 8); 1066 if (x >= maxint) 1067 return _range_error(f, 1); 1068 } 1069 do { 1070 *p++ = (char)x; 1071 x >>= 8; 1072 } while (--i > 0); 1073 return 0; 1183 1074 } 1184 1075 … … 1186 1077 lp_longlong(char *p, PyObject *v, const formatdef *f) 1187 1078 { 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1079 int res; 1080 v = get_pylong(v); 1081 if (v == NULL) 1082 return -1; 1083 res = _PyLong_AsByteArray((PyLongObject*)v, 1084 (unsigned char *)p, 1085 8, 1086 1, /* little_endian */ 1087 1 /* signed */); 1088 Py_DECREF(v); 1089 return res; 1199 1090 } 1200 1091 … … 1202 1093 lp_ulonglong(char *p, PyObject *v, const formatdef *f) 1203 1094 { 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1095 int res; 1096 v = get_pylong(v); 1097 if (v == NULL) 1098 return -1; 1099 res = _PyLong_AsByteArray((PyLongObject*)v, 1100 (unsigned char *)p, 1101 8, 1102 1, /* little_endian */ 1103 0 /* signed */); 1104 Py_DECREF(v); 1105 return res; 1215 1106 } 1216 1107 … … 1218 1109 lp_float(char *p, PyObject *v, const formatdef *f) 1219 1110 { 1220 1221 1222 1223 1224 1225 1226 1111 double x = PyFloat_AsDouble(v); 1112 if (x == -1 && PyErr_Occurred()) { 1113 PyErr_SetString(StructError, 1114 "required argument is not a float"); 1115 return -1; 1116 } 1117 return _PyFloat_Pack4(x, (unsigned char *)p, 1); 1227 1118 } 1228 1119 … … 1230 1121 lp_double(char *p, PyObject *v, const formatdef *f) 1231 1122 { 1232 1233 1234 1235 1236 1237 1238 1123 double x = PyFloat_AsDouble(v); 1124 if (x == -1 && PyErr_Occurred()) { 1125 PyErr_SetString(StructError, 1126 "required argument is not a float"); 1127 return -1; 1128 } 1129 return _PyFloat_Pack8(x, (unsigned char *)p, 1); 1239 1130 } 1240 1131 1241 1132 static formatdef lilendian_table[] = { 1242 {'x', 1, 0, NULL}, 1243 #ifdef PY_STRUCT_OVERFLOW_MASKING 1244 /* Native packers do range checking without overflow masking. */ 1245 {'b', 1, 0, nu_byte, lp_int}, 1246 {'B', 1, 0, nu_ubyte, lp_uint}, 1247 #else 1248 {'b', 1, 0, nu_byte, np_byte}, 1249 {'B', 1, 0, nu_ubyte, np_ubyte}, 1250 #endif 1251 {'c', 1, 0, nu_char, np_char}, 1252 {'s', 1, 0, NULL}, 1253 {'p', 1, 0, NULL}, 1254 {'h', 2, 0, lu_int, lp_int}, 1255 {'H', 2, 0, lu_uint, lp_uint}, 1256 {'i', 4, 0, lu_int, lp_int}, 1257 {'I', 4, 0, lu_uint, lp_uint}, 1258 {'l', 4, 0, lu_int, lp_int}, 1259 {'L', 4, 0, lu_uint, lp_uint}, 1260 {'q', 8, 0, lu_longlong, lp_longlong}, 1261 {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, 1262 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep, 1263 but potentially different from native rep -- reuse bx_bool funcs. */ 1264 {'f', 4, 0, lu_float, lp_float}, 1265 {'d', 8, 0, lu_double, lp_double}, 1266 {0} 1133 {'x', 1, 0, NULL}, 1134 {'b', 1, 0, nu_byte, np_byte}, 1135 {'B', 1, 0, nu_ubyte, np_ubyte}, 1136 {'c', 1, 0, nu_char, np_char}, 1137 {'s', 1, 0, NULL}, 1138 {'p', 1, 0, NULL}, 1139 {'h', 2, 0, lu_int, lp_int}, 1140 {'H', 2, 0, lu_uint, lp_uint}, 1141 {'i', 4, 0, lu_int, lp_int}, 1142 {'I', 4, 0, lu_uint, lp_uint}, 1143 {'l', 4, 0, lu_int, lp_int}, 1144 {'L', 4, 0, lu_uint, lp_uint}, 1145 {'q', 8, 0, lu_longlong, lp_longlong}, 1146 {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, 1147 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep, 1148 but potentially different from native rep -- reuse bx_bool funcs. */ 1149 {'f', 4, 0, lu_float, lp_float}, 1150 {'d', 8, 0, lu_double, lp_double}, 1151 {0} 1267 1152 }; 1268 1153 … … 1271 1156 whichtable(char **pfmt) 1272 1157 { 1273 1274 1275 1276 1277 1278 1279 1280 case '=': { /* Host byte order -- different from native in aligment! */1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1158 const char *fmt = (*pfmt)++; /* May be backed out of later */ 1159 switch (*fmt) { 1160 case '<': 1161 return lilendian_table; 1162 case '>': 1163 case '!': /* Network byte order is big-endian */ 1164 return bigendian_table; 1165 case '=': { /* Host byte order -- different from native in alignment! */ 1166 int n = 1; 1167 char *p = (char *) &n; 1168 if (*p == 1) 1169 return lilendian_table; 1170 else 1171 return bigendian_table; 1172 } 1173 default: 1174 --*pfmt; /* Back out of pointer increment */ 1175 /* Fall through */ 1176 case '@': 1177 return native_table; 1178 } 1294 1179 } 1295 1180 … … 1300 1185 getentry(int c, const formatdef *f) 1301 1186 { 1302 1303 1304 1305 1306 1307 1308 1309 } 1310 1311 1312 /* Align a size according to a format code */1313 1314 static int1187 for (; f->format != '\0'; f++) { 1188 if (f->format == c) { 1189 return f; 1190 } 1191 } 1192 PyErr_SetString(StructError, "bad char in struct format"); 1193 return NULL; 1194 } 1195 1196 1197 /* Align a size according to a format code. Return -1 on overflow. */ 1198 1199 static Py_ssize_t 1315 1200 align(Py_ssize_t size, char c, const formatdef *e) 1316 1201 { 1317 if (e->format == c) { 1318 if (e->alignment) { 1319 size = ((size + e->alignment - 1) 1320 / e->alignment) 1321 * e->alignment; 1322 } 1323 } 1324 return size; 1202 Py_ssize_t extra; 1203 1204 if (e->format == c) { 1205 if (e->alignment && size > 0) { 1206 extra = (e->alignment - 1) - (size - 1) % (e->alignment); 1207 if (extra > PY_SSIZE_T_MAX - size) 1208 return -1; 1209 size += extra; 1210 } 1211 } 1212 return size; 1325 1213 } 1326 1214 … … 1331 1219 prepare_s(PyStructObject *self) 1332 1220 { 1333 const formatdef *f; 1334 const formatdef *e; 1335 formatcode *codes; 1336 1337 const char *s; 1338 const char *fmt; 1339 char c; 1340 Py_ssize_t size, len, num, itemsize, x; 1341 1342 fmt = PyString_AS_STRING(self->s_format); 1343 1344 f = whichtable((char **)&fmt); 1345 1346 s = fmt; 1347 size = 0; 1348 len = 0; 1349 while ((c = *s++) != '\0') { 1350 if (isspace(Py_CHARMASK(c))) 1351 continue; 1352 if ('0' <= c && c <= '9') { 1353 num = c - '0'; 1354 while ('0' <= (c = *s++) && c <= '9') { 1355 x = num*10 + (c - '0'); 1356 if (x/10 != num) { 1357 PyErr_SetString( 1358 StructError, 1359 "overflow in item count"); 1360 return -1; 1361 } 1362 num = x; 1363 } 1364 if (c == '\0') 1365 break; 1366 } 1367 else 1368 num = 1; 1369 1370 e = getentry(c, f); 1371 if (e == NULL) 1372 return -1; 1373 1374 switch (c) { 1375 case 's': /* fall through */ 1376 case 'p': len++; break; 1377 case 'x': break; 1378 default: len += num; break; 1379 } 1380 1381 itemsize = e->size; 1382 size = align(size, c, e); 1383 x = num * itemsize; 1384 size += x; 1385 if (x/itemsize != num || size < 0) { 1386 PyErr_SetString(StructError, 1387 "total struct size too long"); 1388 return -1; 1389 } 1390 } 1391 1392 /* check for overflow */ 1393 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { 1394 PyErr_NoMemory(); 1395 return -1; 1396 } 1397 1398 self->s_size = size; 1399 self->s_len = len; 1400 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); 1401 if (codes == NULL) { 1402 PyErr_NoMemory(); 1403 return -1; 1404 } 1405 self->s_codes = codes; 1406 1407 s = fmt; 1408 size = 0; 1409 while ((c = *s++) != '\0') { 1410 if (isspace(Py_CHARMASK(c))) 1411 continue; 1412 if ('0' <= c && c <= '9') { 1413 num = c - '0'; 1414 while ('0' <= (c = *s++) && c <= '9') 1415 num = num*10 + (c - '0'); 1416 if (c == '\0') 1417 break; 1418 } 1419 else 1420 num = 1; 1421 1422 e = getentry(c, f); 1423 1424 size = align(size, c, e); 1425 if (c == 's' || c == 'p') { 1426 codes->offset = size; 1427 codes->size = num; 1428 codes->fmtdef = e; 1429 codes++; 1430 size += num; 1431 } else if (c == 'x') { 1432 size += num; 1433 } else { 1434 while (--num >= 0) { 1435 codes->offset = size; 1436 codes->size = e->size; 1437 codes->fmtdef = e; 1438 codes++; 1439 size += e->size; 1440 } 1441 } 1442 } 1443 codes->fmtdef = NULL; 1444 codes->offset = size; 1445 codes->size = 0; 1446 1447 return 0; 1221 const formatdef *f; 1222 const formatdef *e; 1223 formatcode *codes; 1224 1225 const char *s; 1226 const char *fmt; 1227 char c; 1228 Py_ssize_t size, len, num, itemsize; 1229 1230 fmt = PyString_AS_STRING(self->s_format); 1231 1232 f = whichtable((char **)&fmt); 1233 1234 s = fmt; 1235 size = 0; 1236 len = 0; 1237 while ((c = *s++) != '\0') { 1238 if (isspace(Py_CHARMASK(c))) 1239 continue; 1240 if ('0' <= c && c <= '9') { 1241 num = c - '0'; 1242 while ('0' <= (c = *s++) && c <= '9') { 1243 /* overflow-safe version of 1244 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */ 1245 if (num >= PY_SSIZE_T_MAX / 10 && ( 1246 num > PY_SSIZE_T_MAX / 10 || 1247 (c - '0') > PY_SSIZE_T_MAX % 10)) 1248 goto overflow; 1249 num = num*10 + (c - '0'); 1250 } 1251 if (c == '\0') 1252 break; 1253 } 1254 else 1255 num = 1; 1256 1257 e = getentry(c, f); 1258 if (e == NULL) 1259 return -1; 1260 1261 switch (c) { 1262 case 's': /* fall through */ 1263 case 'p': len++; break; 1264 case 'x': break; 1265 default: len += num; break; 1266 } 1267 1268 itemsize = e->size; 1269 size = align(size, c, e); 1270 if (size == -1) 1271 goto overflow; 1272 1273 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */ 1274 if (num > (PY_SSIZE_T_MAX - size) / itemsize) 1275 goto overflow; 1276 size += num * itemsize; 1277 } 1278 1279 /* check for overflow */ 1280 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { 1281 PyErr_NoMemory(); 1282 return -1; 1283 } 1284 1285 self->s_size = size; 1286 self->s_len = len; 1287 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); 1288 if (codes == NULL) { 1289 PyErr_NoMemory(); 1290 return -1; 1291 } 1292 /* Free any s_codes value left over from a previous initialization. */ 1293 if (self->s_codes != NULL) 1294 PyMem_FREE(self->s_codes); 1295 self->s_codes = codes; 1296 1297 s = fmt; 1298 size = 0; 1299 while ((c = *s++) != '\0') { 1300 if (isspace(Py_CHARMASK(c))) 1301 continue; 1302 if ('0' <= c && c <= '9') { 1303 num = c - '0'; 1304 while ('0' <= (c = *s++) && c <= '9') 1305 num = num*10 + (c - '0'); 1306 if (c == '\0') 1307 break; 1308 } 1309 else 1310 num = 1; 1311 1312 e = getentry(c, f); 1313 1314 size = align(size, c, e); 1315 if (c == 's' || c == 'p') { 1316 codes->offset = size; 1317 codes->size = num; 1318 codes->fmtdef = e; 1319 codes++; 1320 size += num; 1321 } else if (c == 'x') { 1322 size += num; 1323 } else { 1324 while (--num >= 0) { 1325 codes->offset = size; 1326 codes->size = e->size; 1327 codes->fmtdef = e; 1328 codes++; 1329 size += e->size; 1330 } 1331 } 1332 } 1333 codes->fmtdef = NULL; 1334 codes->offset = size; 1335 codes->size = 0; 1336 1337 return 0; 1338 1339 overflow: 1340 PyErr_SetString(StructError, 1341 "total struct size too long"); 1342 return -1; 1448 1343 } 1449 1344 … … 1451 1346 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1452 1347 { 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1348 PyObject *self; 1349 1350 assert(type != NULL && type->tp_alloc != NULL); 1351 1352 self = type->tp_alloc(type, 0); 1353 if (self != NULL) { 1354 PyStructObject *s = (PyStructObject*)self; 1355 Py_INCREF(Py_None); 1356 s->s_format = Py_None; 1357 s->s_codes = NULL; 1358 s->s_size = -1; 1359 s->s_len = -1; 1360 } 1361 return self; 1467 1362 } 1468 1363 … … 1470 1365 s_init(PyObject *self, PyObject *args, PyObject *kwds) 1471 1366 { 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1367 PyStructObject *soself = (PyStructObject *)self; 1368 PyObject *o_format = NULL; 1369 int ret = 0; 1370 static char *kwlist[] = {"format", 0}; 1371 1372 assert(PyStruct_Check(self)); 1373 1374 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist, 1375 &o_format)) 1376 return -1; 1377 1378 Py_INCREF(o_format); 1379 Py_CLEAR(soself->s_format); 1380 soself->s_format = o_format; 1381 1382 ret = prepare_s(soself); 1383 return ret; 1489 1384 } 1490 1385 … … 1492 1387 s_dealloc(PyStructObject *s) 1493 1388 { 1494 1495 1496 1497 1498 1499 1500 1389 if (s->weakreflist != NULL) 1390 PyObject_ClearWeakRefs((PyObject *)s); 1391 if (s->s_codes != NULL) { 1392 PyMem_FREE(s->s_codes); 1393 } 1394 Py_XDECREF(s->s_format); 1395 Py_TYPE(s)->tp_free((PyObject *)s); 1501 1396 } 1502 1397 1503 1398 static PyObject * 1504 1399 s_unpack_internal(PyStructObject *soself, char *startfrom) { 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1400 formatcode *code; 1401 Py_ssize_t i = 0; 1402 PyObject *result = PyTuple_New(soself->s_len); 1403 if (result == NULL) 1404 return NULL; 1405 1406 for (code = soself->s_codes; code->fmtdef != NULL; code++) { 1407 PyObject *v; 1408 const formatdef *e = code->fmtdef; 1409 const char *res = startfrom + code->offset; 1410 if (e->format == 's') { 1411 v = PyString_FromStringAndSize(res, code->size); 1412 } else if (e->format == 'p') { 1413 Py_ssize_t n = *(unsigned char*)res; 1414 if (n >= code->size) 1415 n = code->size - 1; 1416 v = PyString_FromStringAndSize(res + 1, n); 1417 } else { 1418 v = e->unpack(res, e); 1419 } 1420 if (v == NULL) 1421 goto fail; 1422 PyTuple_SET_ITEM(result, i++, v); 1423 } 1424 1425 return result; 1531 1426 fail: 1532 1533 1427 Py_DECREF(result); 1428 return NULL; 1534 1429 } 1535 1430 … … 1545 1440 s_unpack(PyObject *self, PyObject *inputstr) 1546 1441 { 1547 char *start; 1548 Py_ssize_t len; 1549 PyObject *args=NULL, *result; 1550 PyStructObject *soself = (PyStructObject *)self; 1551 assert(PyStruct_Check(self)); 1552 assert(soself->s_codes != NULL); 1553 if (inputstr == NULL) 1554 goto fail; 1555 if (PyString_Check(inputstr) && 1556 PyString_GET_SIZE(inputstr) == soself->s_size) { 1557 return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); 1558 } 1559 args = PyTuple_Pack(1, inputstr); 1560 if (args == NULL) 1561 return NULL; 1562 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) 1563 goto fail; 1564 if (soself->s_size != len) 1565 goto fail; 1566 result = s_unpack_internal(soself, start); 1567 Py_DECREF(args); 1568 return result; 1442 Py_buffer buf; 1443 char *start; 1444 Py_ssize_t len; 1445 PyObject *args=NULL, *result; 1446 PyStructObject *soself = (PyStructObject *)self; 1447 assert(PyStruct_Check(self)); 1448 assert(soself->s_codes != NULL); 1449 if (inputstr == NULL) 1450 goto fail; 1451 if (PyString_Check(inputstr) && 1452 PyString_GET_SIZE(inputstr) == soself->s_size) { 1453 return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); 1454 } 1455 args = PyTuple_Pack(1, inputstr); 1456 if (args == NULL) 1457 return NULL; 1458 if (!PyArg_ParseTuple(args, "s*:unpack", &buf)) 1459 goto fail; 1460 start = buf.buf; 1461 len = buf.len; 1462 if (soself->s_size != len) { 1463 PyBuffer_Release(&buf); 1464 goto fail; 1465 } 1466 result = s_unpack_internal(soself, start); 1467 Py_DECREF(args); 1468 PyBuffer_Release(&buf); 1469 return result; 1569 1470 1570 1471 fail: 1571 1572 1573 1574 1575 1472 Py_XDECREF(args); 1473 PyErr_Format(StructError, 1474 "unpack requires a string argument of length %zd", 1475 soself->s_size); 1476 return NULL; 1576 1477 } 1577 1478 … … 1587 1488 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) 1588 1489 { 1589 static char *kwlist[] = {"buffer", "offset", 0}; 1590 #if (PY_VERSION_HEX < 0x02050000) 1591 static char *fmt = "z#|i:unpack_from"; 1592 #else 1593 static char *fmt = "z#|n:unpack_from"; 1594 #endif 1595 Py_ssize_t buffer_len = 0, offset = 0; 1596 char *buffer = NULL; 1597 PyStructObject *soself = (PyStructObject *)self; 1598 assert(PyStruct_Check(self)); 1599 assert(soself->s_codes != NULL); 1600 1601 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, 1602 &buffer, &buffer_len, &offset)) 1603 return NULL; 1604 1605 if (buffer == NULL) { 1606 PyErr_Format(StructError, 1607 "unpack_from requires a buffer argument"); 1608 return NULL; 1609 } 1610 1611 if (offset < 0) 1612 offset += buffer_len; 1613 1614 if (offset < 0 || (buffer_len - offset) < soself->s_size) { 1615 PyErr_Format(StructError, 1616 "unpack_from requires a buffer of at least %zd bytes", 1617 soself->s_size); 1618 return NULL; 1619 } 1620 return s_unpack_internal(soself, buffer + offset); 1490 static char *kwlist[] = {"buffer", "offset", 0}; 1491 static char *fmt = "z*|n:unpack_from"; 1492 Py_buffer buf; 1493 Py_ssize_t buffer_len = 0, offset = 0; 1494 char *buffer = NULL; 1495 PyStructObject *soself = (PyStructObject *)self; 1496 PyObject *result; 1497 assert(PyStruct_Check(self)); 1498 assert(soself->s_codes != NULL); 1499 1500 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, 1501 &buf, &offset)) 1502 return NULL; 1503 buffer = buf.buf; 1504 buffer_len = buf.len; 1505 if (buffer == NULL) { 1506 PyErr_Format(StructError, 1507 "unpack_from requires a buffer argument"); 1508 PyBuffer_Release(&buf); 1509 return NULL; 1510 } 1511 1512 if (offset < 0) 1513 offset += buffer_len; 1514 1515 if (offset < 0 || (buffer_len - offset) < soself->s_size) { 1516 PyErr_Format(StructError, 1517 "unpack_from requires a buffer of at least %zd bytes", 1518 soself->s_size); 1519 PyBuffer_Release(&buf); 1520 return NULL; 1521 } 1522 result = s_unpack_internal(soself, buffer + offset); 1523 PyBuffer_Release(&buf); 1524 return result; 1621 1525 } 1622 1526 … … 1635 1539 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) 1636 1540 { 1637 formatcode *code; 1638 /* XXX(nnorwitz): why does i need to be a local? can we use 1639 the offset parameter or do we need the wider width? */ 1640 Py_ssize_t i; 1641 1642 memset(buf, '\0', soself->s_size); 1643 i = offset; 1644 for (code = soself->s_codes; code->fmtdef != NULL; code++) { 1645 Py_ssize_t n; 1646 PyObject *v = PyTuple_GET_ITEM(args, i++); 1647 const formatdef *e = code->fmtdef; 1648 char *res = buf + code->offset; 1649 if (e->format == 's') { 1650 if (!PyString_Check(v)) { 1651 PyErr_SetString(StructError, 1652 "argument for 's' must be a string"); 1653 return -1; 1654 } 1655 n = PyString_GET_SIZE(v); 1656 if (n > code->size) 1657 n = code->size; 1658 if (n > 0) 1659 memcpy(res, PyString_AS_STRING(v), n); 1660 } else if (e->format == 'p') { 1661 if (!PyString_Check(v)) { 1662 PyErr_SetString(StructError, 1663 "argument for 'p' must be a string"); 1664 return -1; 1665 } 1666 n = PyString_GET_SIZE(v); 1667 if (n > (code->size - 1)) 1668 n = code->size - 1; 1669 if (n > 0) 1670 memcpy(res + 1, PyString_AS_STRING(v), n); 1671 if (n > 255) 1672 n = 255; 1673 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); 1674 } else { 1675 if (e->pack(res, v, e) < 0) { 1676 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) 1677 PyErr_SetString(StructError, 1678 "long too large to convert to int"); 1679 return -1; 1680 } 1681 } 1682 } 1683 1684 /* Success */ 1685 return 0; 1541 formatcode *code; 1542 /* XXX(nnorwitz): why does i need to be a local? can we use 1543 the offset parameter or do we need the wider width? */ 1544 Py_ssize_t i; 1545 1546 memset(buf, '\0', soself->s_size); 1547 i = offset; 1548 for (code = soself->s_codes; code->fmtdef != NULL; code++) { 1549 Py_ssize_t n; 1550 PyObject *v = PyTuple_GET_ITEM(args, i++); 1551 const formatdef *e = code->fmtdef; 1552 char *res = buf + code->offset; 1553 if (e->format == 's') { 1554 if (!PyString_Check(v)) { 1555 PyErr_SetString(StructError, 1556 "argument for 's' must " 1557 "be a string"); 1558 return -1; 1559 } 1560 n = PyString_GET_SIZE(v); 1561 if (n > code->size) 1562 n = code->size; 1563 if (n > 0) 1564 memcpy(res, PyString_AS_STRING(v), n); 1565 } else if (e->format == 'p') { 1566 if (!PyString_Check(v)) { 1567 PyErr_SetString(StructError, 1568 "argument for 'p' must " 1569 "be a string"); 1570 return -1; 1571 } 1572 n = PyString_GET_SIZE(v); 1573 if (n > (code->size - 1)) 1574 n = code->size - 1; 1575 if (n > 0) 1576 memcpy(res + 1, PyString_AS_STRING(v), n); 1577 if (n > 255) 1578 n = 255; 1579 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); 1580 } else if (e->pack(res, v, e) < 0) { 1581 if (strchr(integer_codes, e->format) != NULL && 1582 PyErr_ExceptionMatches(PyExc_OverflowError)) 1583 PyErr_Format(StructError, 1584 "integer out of range for " 1585 "'%c' format code", 1586 e->format); 1587 return -1; 1588 } 1589 } 1590 1591 /* Success */ 1592 return 0; 1686 1593 } 1687 1594 … … 1696 1603 s_pack(PyObject *self, PyObject *args) 1697 1604 { 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 "pack requires exactly %zd arguments", soself->s_len);1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1605 PyStructObject *soself; 1606 PyObject *result; 1607 1608 /* Validate arguments. */ 1609 soself = (PyStructObject *)self; 1610 assert(PyStruct_Check(self)); 1611 assert(soself->s_codes != NULL); 1612 if (PyTuple_GET_SIZE(args) != soself->s_len) 1613 { 1614 PyErr_Format(StructError, 1615 "pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args)); 1616 return NULL; 1617 } 1618 1619 /* Allocate a new string */ 1620 result = PyString_FromStringAndSize((char *)NULL, soself->s_size); 1621 if (result == NULL) 1622 return NULL; 1623 1624 /* Call the guts */ 1625 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) { 1626 Py_DECREF(result); 1627 return NULL; 1628 } 1629 1630 return result; 1724 1631 } 1725 1632 … … 1735 1642 s_pack_into(PyObject *self, PyObject *args) 1736 1643 { 1737 PyStructObject *soself; 1738 char *buffer; 1739 Py_ssize_t buffer_len, offset; 1740 1741 /* Validate arguments. +1 is for the first arg as buffer. */ 1742 soself = (PyStructObject *)self; 1743 assert(PyStruct_Check(self)); 1744 assert(soself->s_codes != NULL); 1745 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2)) 1746 { 1747 PyErr_Format(StructError, 1748 "pack_into requires exactly %zd arguments", 1749 (soself->s_len + 2)); 1750 return NULL; 1751 } 1752 1753 /* Extract a writable memory buffer from the first argument */ 1754 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), 1755 (void**)&buffer, &buffer_len) == -1 ) { 1756 return NULL; 1757 } 1758 assert( buffer_len >= 0 ); 1759 1760 /* Extract the offset from the first argument */ 1761 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1)); 1762 if (offset == -1 && PyErr_Occurred()) 1763 return NULL; 1764 1765 /* Support negative offsets. */ 1766 if (offset < 0) 1767 offset += buffer_len; 1768 1769 /* Check boundaries */ 1770 if (offset < 0 || (buffer_len - offset) < soself->s_size) { 1771 PyErr_Format(StructError, 1772 "pack_into requires a buffer of at least %zd bytes", 1773 soself->s_size); 1774 return NULL; 1775 } 1776 1777 /* Call the guts */ 1778 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { 1779 return NULL; 1780 } 1781 1782 Py_RETURN_NONE; 1644 PyStructObject *soself; 1645 char *buffer; 1646 Py_ssize_t buffer_len, offset; 1647 1648 /* Validate arguments. +1 is for the first arg as buffer. */ 1649 soself = (PyStructObject *)self; 1650 assert(PyStruct_Check(self)); 1651 assert(soself->s_codes != NULL); 1652 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2)) 1653 { 1654 if (PyTuple_GET_SIZE(args) == 0) { 1655 PyErr_Format(StructError, 1656 "pack_into expected buffer argument"); 1657 } 1658 else if (PyTuple_GET_SIZE(args) == 1) { 1659 PyErr_Format(StructError, 1660 "pack_into expected offset argument"); 1661 } 1662 else { 1663 PyErr_Format(StructError, 1664 "pack_into expected %zd items for packing (got %zd)", 1665 soself->s_len, (PyTuple_GET_SIZE(args) - 2)); 1666 } 1667 return NULL; 1668 } 1669 1670 /* Extract a writable memory buffer from the first argument */ 1671 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), 1672 (void**)&buffer, &buffer_len) == -1 ) { 1673 return NULL; 1674 } 1675 assert( buffer_len >= 0 ); 1676 1677 /* Extract the offset from the first argument */ 1678 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1)); 1679 if (offset == -1 && PyErr_Occurred()) 1680 return NULL; 1681 1682 /* Support negative offsets. */ 1683 if (offset < 0) 1684 offset += buffer_len; 1685 1686 /* Check boundaries */ 1687 if (offset < 0 || (buffer_len - offset) < soself->s_size) { 1688 PyErr_Format(StructError, 1689 "pack_into requires a buffer of at least %zd bytes", 1690 soself->s_size); 1691 return NULL; 1692 } 1693 1694 /* Call the guts */ 1695 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { 1696 return NULL; 1697 } 1698 1699 Py_RETURN_NONE; 1783 1700 } 1784 1701 … … 1786 1703 s_get_format(PyStructObject *self, void *unused) 1787 1704 { 1788 1789 1705 Py_INCREF(self->s_format); 1706 return self->s_format; 1790 1707 } 1791 1708 … … 1796 1713 } 1797 1714 1715 PyDoc_STRVAR(s_sizeof__doc__, 1716 "S.__sizeof__() -> size of S in memory, in bytes"); 1717 1718 static PyObject * 1719 s_sizeof(PyStructObject *self, void *unused) 1720 { 1721 Py_ssize_t size; 1722 1723 size = sizeof(PyStructObject) + sizeof(formatcode) * (self->s_len + 1); 1724 return PyLong_FromSsize_t(size); 1725 } 1726 1798 1727 /* List of functions */ 1799 1728 1800 1729 static struct PyMethodDef s_methods[] = { 1801 {"pack", s_pack, METH_VARARGS, s_pack__doc__}, 1802 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, 1803 {"unpack", s_unpack, METH_O, s_unpack__doc__}, 1804 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, 1805 s_unpack_from__doc__}, 1806 {NULL, NULL} /* sentinel */ 1730 {"pack", s_pack, METH_VARARGS, s_pack__doc__}, 1731 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, 1732 {"unpack", s_unpack, METH_O, s_unpack__doc__}, 1733 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, 1734 s_unpack_from__doc__}, 1735 {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__}, 1736 {NULL, NULL} /* sentinel */ 1807 1737 }; 1808 1738 … … 1812 1742 1813 1743 static PyGetSetDef s_getsetlist[] = { 1814 1815 1816 1744 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL}, 1745 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL}, 1746 {NULL} /* sentinel */ 1817 1747 }; 1818 1748 1819 1749 static 1820 1750 PyTypeObject PyStructType = { 1821 1822 1823 1824 1825 (destructor)s_dealloc,/* tp_dealloc */1826 0,/* tp_print */1827 0,/* tp_getattr */1828 0,/* tp_setattr */1829 0,/* tp_compare */1830 0,/* tp_repr */1831 0,/* tp_as_number */1832 0,/* tp_as_sequence */1833 0,/* tp_as_mapping */1834 0,/* tp_hash */1835 0,/* tp_call */1836 0,/* tp_str */1837 PyObject_GenericGetAttr,/* tp_getattro */1838 PyObject_GenericSetAttr,/* tp_setattro */1839 0,/* tp_as_buffer */1840 1841 s__doc__,/* tp_doc */1842 0,/* tp_traverse */1843 0,/* tp_clear */1844 0,/* tp_richcompare */1845 offsetof(PyStructObject, weakreflist),/* tp_weaklistoffset */1846 0,/* tp_iter */1847 0,/* tp_iternext */1848 s_methods,/* tp_methods */1849 NULL,/* tp_members */1850 s_getsetlist,/* tp_getset */1851 0,/* tp_base */1852 0,/* tp_dict */1853 0,/* tp_descr_get */1854 0,/* tp_descr_set */1855 0,/* tp_dictoffset */1856 s_init,/* tp_init */1857 1858 s_new,/* tp_new */1859 PyObject_Del,/* tp_free */1751 PyVarObject_HEAD_INIT(NULL, 0) 1752 "Struct", 1753 sizeof(PyStructObject), 1754 0, 1755 (destructor)s_dealloc, /* tp_dealloc */ 1756 0, /* tp_print */ 1757 0, /* tp_getattr */ 1758 0, /* tp_setattr */ 1759 0, /* tp_compare */ 1760 0, /* tp_repr */ 1761 0, /* tp_as_number */ 1762 0, /* tp_as_sequence */ 1763 0, /* tp_as_mapping */ 1764 0, /* tp_hash */ 1765 0, /* tp_call */ 1766 0, /* tp_str */ 1767 PyObject_GenericGetAttr, /* tp_getattro */ 1768 PyObject_GenericSetAttr, /* tp_setattro */ 1769 0, /* tp_as_buffer */ 1770 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */ 1771 s__doc__, /* tp_doc */ 1772 0, /* tp_traverse */ 1773 0, /* tp_clear */ 1774 0, /* tp_richcompare */ 1775 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */ 1776 0, /* tp_iter */ 1777 0, /* tp_iternext */ 1778 s_methods, /* tp_methods */ 1779 NULL, /* tp_members */ 1780 s_getsetlist, /* tp_getset */ 1781 0, /* tp_base */ 1782 0, /* tp_dict */ 1783 0, /* tp_descr_get */ 1784 0, /* tp_descr_set */ 1785 0, /* tp_dictoffset */ 1786 s_init, /* tp_init */ 1787 PyType_GenericAlloc,/* tp_alloc */ 1788 s_new, /* tp_new */ 1789 PyObject_Del, /* tp_free */ 1860 1790 }; 1861 1791 … … 1869 1799 cache_struct(PyObject *fmt) 1870 1800 { 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1801 PyObject * s_object; 1802 1803 if (cache == NULL) { 1804 cache = PyDict_New(); 1805 if (cache == NULL) 1806 return NULL; 1807 } 1808 1809 s_object = PyDict_GetItem(cache, fmt); 1810 if (s_object != NULL) { 1811 Py_INCREF(s_object); 1812 return s_object; 1813 } 1814 1815 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); 1816 if (s_object != NULL) { 1817 if (PyDict_Size(cache) >= MAXCACHE) 1818 PyDict_Clear(cache); 1819 /* Attempt to cache the result */ 1820 if (PyDict_SetItem(cache, fmt, s_object) == -1) 1821 PyErr_Clear(); 1822 } 1823 return s_object; 1894 1824 } 1895 1825 … … 1900 1830 clearcache(PyObject *self) 1901 1831 { 1902 1903 1832 Py_CLEAR(cache); 1833 Py_RETURN_NONE; 1904 1834 } 1905 1835 … … 1910 1840 calcsize(PyObject *self, PyObject *fmt) 1911 1841 { 1912 1913 1914 1915 1916 1917 1918 1842 Py_ssize_t n; 1843 PyObject *s_object = cache_struct(fmt); 1844 if (s_object == NULL) 1845 return NULL; 1846 n = ((PyStructObject *)s_object)->s_size; 1847 Py_DECREF(s_object); 1848 return PyInt_FromSsize_t(n); 1919 1849 } 1920 1850 … … 1925 1855 pack(PyObject *self, PyObject *args) 1926 1856 { 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1857 PyObject *s_object, *fmt, *newargs, *result; 1858 Py_ssize_t n = PyTuple_GET_SIZE(args); 1859 1860 if (n == 0) { 1861 PyErr_SetString(PyExc_TypeError, "missing format argument"); 1862 return NULL; 1863 } 1864 fmt = PyTuple_GET_ITEM(args, 0); 1865 newargs = PyTuple_GetSlice(args, 1, n); 1866 if (newargs == NULL) 1867 return NULL; 1868 1869 s_object = cache_struct(fmt); 1870 if (s_object == NULL) { 1871 Py_DECREF(newargs); 1872 return NULL; 1873 } 1874 result = s_pack(s_object, newargs); 1875 Py_DECREF(newargs); 1876 Py_DECREF(s_object); 1877 return result; 1948 1878 } 1949 1879 … … 1955 1885 pack_into(PyObject *self, PyObject *args) 1956 1886 { 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1887 PyObject *s_object, *fmt, *newargs, *result; 1888 Py_ssize_t n = PyTuple_GET_SIZE(args); 1889 1890 if (n == 0) { 1891 PyErr_SetString(PyExc_TypeError, "missing format argument"); 1892 return NULL; 1893 } 1894 fmt = PyTuple_GET_ITEM(args, 0); 1895 newargs = PyTuple_GetSlice(args, 1, n); 1896 if (newargs == NULL) 1897 return NULL; 1898 1899 s_object = cache_struct(fmt); 1900 if (s_object == NULL) { 1901 Py_DECREF(newargs); 1902 return NULL; 1903 } 1904 result = s_pack_into(s_object, newargs); 1905 Py_DECREF(newargs); 1906 Py_DECREF(s_object); 1907 return result; 1978 1908 } 1979 1909 … … 1985 1915 unpack(PyObject *self, PyObject *args) 1986 1916 { 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1917 PyObject *s_object, *fmt, *inputstr, *result; 1918 1919 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr)) 1920 return NULL; 1921 1922 s_object = cache_struct(fmt); 1923 if (s_object == NULL) 1924 return NULL; 1925 result = s_unpack(s_object, inputstr); 1926 Py_DECREF(s_object); 1927 return result; 1998 1928 } 1999 1929 … … 2005 1935 unpack_from(PyObject *self, PyObject *args, PyObject *kwds) 2006 1936 { 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 1937 PyObject *s_object, *fmt, *newargs, *result; 1938 Py_ssize_t n = PyTuple_GET_SIZE(args); 1939 1940 if (n == 0) { 1941 PyErr_SetString(PyExc_TypeError, "missing format argument"); 1942 return NULL; 1943 } 1944 fmt = PyTuple_GET_ITEM(args, 0); 1945 newargs = PyTuple_GetSlice(args, 1, n); 1946 if (newargs == NULL) 1947 return NULL; 1948 1949 s_object = cache_struct(fmt); 1950 if (s_object == NULL) { 1951 Py_DECREF(newargs); 1952 return NULL; 1953 } 1954 result = s_unpack_from(s_object, newargs, kwds); 1955 Py_DECREF(newargs); 1956 Py_DECREF(s_object); 1957 return result; 2028 1958 } 2029 1959 2030 1960 static struct PyMethodDef module_functions[] = { 2031 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS,clearcache_doc},2032 {"calcsize", calcsize, METH_O,calcsize_doc},2033 {"pack", pack, METH_VARARGS,pack_doc},2034 {"pack_into", pack_into, METH_VARARGS,pack_into_doc},2035 {"unpack", unpack, METH_VARARGS,unpack_doc},2036 {"unpack_from", (PyCFunction)unpack_from, 2037 METH_VARARGS|METH_KEYWORDS,unpack_from_doc},2038 {NULL, NULL}/* sentinel */1961 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc}, 1962 {"calcsize", calcsize, METH_O, calcsize_doc}, 1963 {"pack", pack, METH_VARARGS, pack_doc}, 1964 {"pack_into", pack_into, METH_VARARGS, pack_into_doc}, 1965 {"unpack", unpack, METH_VARARGS, unpack_doc}, 1966 {"unpack_from", (PyCFunction)unpack_from, 1967 METH_VARARGS|METH_KEYWORDS, unpack_from_doc}, 1968 {NULL, NULL} /* sentinel */ 2039 1969 }; 2040 1970 … … 2074 2004 init_struct(void) 2075 2005 { 2076 PyObject *ver, *m; 2077 2078 ver = PyString_FromString("0.2"); 2079 if (ver == NULL) 2080 return; 2081 2082 m = Py_InitModule3("_struct", module_functions, module_doc); 2083 if (m == NULL) 2084 return; 2085 2086 Py_TYPE(&PyStructType) = &PyType_Type; 2087 if (PyType_Ready(&PyStructType) < 0) 2088 return; 2089 2090 #ifdef PY_STRUCT_OVERFLOW_MASKING 2091 if (pyint_zero == NULL) { 2092 pyint_zero = PyInt_FromLong(0); 2093 if (pyint_zero == NULL) 2094 return; 2095 } 2096 if (pylong_ulong_mask == NULL) { 2097 #if (SIZEOF_LONG == 4) 2098 pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16); 2099 #else 2100 pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16); 2101 #endif 2102 if (pylong_ulong_mask == NULL) 2103 return; 2104 } 2105 2106 #else 2107 /* This speed trick can't be used until overflow masking goes away, because 2108 native endian always raises exceptions instead of overflow masking. */ 2109 2110 /* Check endian and swap in faster functions */ 2111 { 2112 int one = 1; 2113 formatdef *native = native_table; 2114 formatdef *other, *ptr; 2115 if ((int)*(unsigned char*)&one) 2116 other = lilendian_table; 2117 else 2118 other = bigendian_table; 2119 /* Scan through the native table, find a matching 2120 entry in the endian table and swap in the 2121 native implementations whenever possible 2122 (64-bit platforms may not have "standard" sizes) */ 2123 while (native->format != '\0' && other->format != '\0') { 2124 ptr = other; 2125 while (ptr->format != '\0') { 2126 if (ptr->format == native->format) { 2127 /* Match faster when formats are 2128 listed in the same order */ 2129 if (ptr == other) 2130 other++; 2131 /* Only use the trick if the 2132 size matches */ 2133 if (ptr->size != native->size) 2134 break; 2135 /* Skip float and double, could be 2136 "unknown" float format */ 2137 if (ptr->format == 'd' || ptr->format == 'f') 2138 break; 2139 ptr->pack = native->pack; 2140 ptr->unpack = native->unpack; 2141 break; 2142 } 2143 ptr++; 2144 } 2145 native++; 2146 } 2147 } 2148 #endif 2149 2150 /* Add some symbolic constants to the module */ 2151 if (StructError == NULL) { 2152 StructError = PyErr_NewException("struct.error", NULL, NULL); 2153 if (StructError == NULL) 2154 return; 2155 } 2156 2157 Py_INCREF(StructError); 2158 PyModule_AddObject(m, "error", StructError); 2159 2160 Py_INCREF((PyObject*)&PyStructType); 2161 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); 2162 2163 PyModule_AddObject(m, "__version__", ver); 2164 2165 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1); 2166 #ifdef PY_STRUCT_OVERFLOW_MASKING 2167 PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1); 2168 #endif 2169 #ifdef PY_STRUCT_FLOAT_COERCE 2170 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1); 2171 #endif 2172 2173 } 2006 PyObject *ver, *m; 2007 2008 ver = PyString_FromString("0.2"); 2009 if (ver == NULL) 2010 return; 2011 2012 m = Py_InitModule3("_struct", module_functions, module_doc); 2013 if (m == NULL) 2014 return; 2015 2016 Py_TYPE(&PyStructType) = &PyType_Type; 2017 if (PyType_Ready(&PyStructType) < 0) 2018 return; 2019 2020 /* This speed trick can't be used until overflow masking goes 2021 away, because native endian always raises exceptions 2022 instead of overflow masking. */ 2023 2024 /* Check endian and swap in faster functions */ 2025 { 2026 int one = 1; 2027 formatdef *native = native_table; 2028 formatdef *other, *ptr; 2029 if ((int)*(unsigned char*)&one) 2030 other = lilendian_table; 2031 else 2032 other = bigendian_table; 2033 /* Scan through the native table, find a matching 2034 entry in the endian table and swap in the 2035 native implementations whenever possible 2036 (64-bit platforms may not have "standard" sizes) */ 2037 while (native->format != '\0' && other->format != '\0') { 2038 ptr = other; 2039 while (ptr->format != '\0') { 2040 if (ptr->format == native->format) { 2041 /* Match faster when formats are 2042 listed in the same order */ 2043 if (ptr == other) 2044 other++; 2045 /* Only use the trick if the 2046 size matches */ 2047 if (ptr->size != native->size) 2048 break; 2049 /* Skip float and double, could be 2050 "unknown" float format */ 2051 if (ptr->format == 'd' || ptr->format == 'f') 2052 break; 2053 ptr->pack = native->pack; 2054 ptr->unpack = native->unpack; 2055 break; 2056 } 2057 ptr++; 2058 } 2059 native++; 2060 } 2061 } 2062 2063 /* Add some symbolic constants to the module */ 2064 if (StructError == NULL) { 2065 StructError = PyErr_NewException("struct.error", NULL, NULL); 2066 if (StructError == NULL) 2067 return; 2068 } 2069 2070 Py_INCREF(StructError); 2071 PyModule_AddObject(m, "error", StructError); 2072 2073 Py_INCREF((PyObject*)&PyStructType); 2074 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); 2075 2076 PyModule_AddObject(m, "__version__", ver); 2077 2078 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1); 2079 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1); 2080 }
Note:
See TracChangeset
for help on using the changeset viewer.