Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1 | /* struct module -- pack values into and (out of) strings */ |
| 2 | |
| 3 | /* New version supporting byte order, alignment and size options, |
| 4 | character strings, and unsigned numbers */ |
| 5 | |
| 6 | #include "Python.h" |
| 7 | #include "structseq.h" |
| 8 | #include "structmember.h" |
| 9 | #include <ctype.h> |
| 10 | |
Bob Ippolito | d3611eb | 2006-05-23 19:31:23 +0000 | [diff] [blame] | 11 | static PyTypeObject PyStructType; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 12 | |
| 13 | /* compatibility macros */ |
| 14 | #if (PY_VERSION_HEX < 0x02050000) |
| 15 | typedef int Py_ssize_t; |
| 16 | #endif |
| 17 | |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 18 | /* PY_USE_INT_WHEN_POSSIBLE is an experimental flag that changes the |
| 19 | struct API to return int instead of long when possible. This is |
| 20 | often a significant performance improvement. */ |
| 21 | /* |
| 22 | #define PY_USE_INT_WHEN_POSSIBLE 1 |
| 23 | */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | /* The translation function for each format character is table driven */ |
| 27 | |
| 28 | typedef struct _formatdef { |
| 29 | char format; |
| 30 | int size; |
| 31 | int alignment; |
| 32 | PyObject* (*unpack)(const char *, |
| 33 | const struct _formatdef *); |
| 34 | int (*pack)(char *, PyObject *, |
| 35 | const struct _formatdef *); |
| 36 | } formatdef; |
| 37 | |
| 38 | typedef struct _formatcode { |
| 39 | const struct _formatdef *fmtdef; |
| 40 | int offset; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 41 | int size; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 42 | } formatcode; |
| 43 | |
| 44 | /* Struct object interface */ |
| 45 | |
| 46 | typedef struct { |
| 47 | PyObject_HEAD |
| 48 | int s_size; |
| 49 | int s_len; |
| 50 | formatcode *s_codes; |
| 51 | PyObject *s_format; |
| 52 | PyObject *weakreflist; /* List of weak references */ |
| 53 | } PyStructObject; |
| 54 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 55 | |
Bob Ippolito | 07c023b | 2006-05-23 19:32:25 +0000 | [diff] [blame] | 56 | #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType) |
| 57 | #define PyStruct_CheckExact(op) ((op)->ob_type == &PyStructType) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | /* Exception */ |
| 61 | |
| 62 | static PyObject *StructError; |
| 63 | |
| 64 | |
| 65 | /* Define various structs to figure out the alignments of types */ |
| 66 | |
| 67 | |
| 68 | typedef struct { char c; short x; } st_short; |
| 69 | typedef struct { char c; int x; } st_int; |
| 70 | typedef struct { char c; long x; } st_long; |
| 71 | typedef struct { char c; float x; } st_float; |
| 72 | typedef struct { char c; double x; } st_double; |
| 73 | typedef struct { char c; void *x; } st_void_p; |
| 74 | |
| 75 | #define SHORT_ALIGN (sizeof(st_short) - sizeof(short)) |
| 76 | #define INT_ALIGN (sizeof(st_int) - sizeof(int)) |
| 77 | #define LONG_ALIGN (sizeof(st_long) - sizeof(long)) |
| 78 | #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float)) |
| 79 | #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double)) |
| 80 | #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *)) |
| 81 | |
| 82 | /* We can't support q and Q in native mode unless the compiler does; |
| 83 | in std mode, they're 8 bytes on all platforms. */ |
| 84 | #ifdef HAVE_LONG_LONG |
| 85 | typedef struct { char c; PY_LONG_LONG x; } s_long_long; |
| 86 | #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) |
| 87 | #endif |
| 88 | |
| 89 | #define STRINGIFY(x) #x |
| 90 | |
| 91 | #ifdef __powerc |
| 92 | #pragma options align=reset |
| 93 | #endif |
| 94 | |
| 95 | /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */ |
| 96 | |
| 97 | static PyObject * |
| 98 | get_pylong(PyObject *v) |
| 99 | { |
| 100 | PyNumberMethods *m; |
| 101 | |
| 102 | assert(v != NULL); |
| 103 | if (PyInt_Check(v)) |
| 104 | return PyLong_FromLong(PyInt_AS_LONG(v)); |
| 105 | if (PyLong_Check(v)) { |
| 106 | Py_INCREF(v); |
| 107 | return v; |
| 108 | } |
| 109 | m = v->ob_type->tp_as_number; |
| 110 | if (m != NULL && m->nb_long != NULL) { |
| 111 | v = m->nb_long(v); |
| 112 | if (v == NULL) |
| 113 | return NULL; |
| 114 | if (PyLong_Check(v)) |
| 115 | return v; |
| 116 | Py_DECREF(v); |
| 117 | } |
| 118 | PyErr_SetString(StructError, |
| 119 | "cannot convert argument to long"); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | /* Helper routine to get a Python integer and raise the appropriate error |
| 124 | if it isn't one */ |
| 125 | |
| 126 | static int |
| 127 | get_long(PyObject *v, long *p) |
| 128 | { |
| 129 | long x = PyInt_AsLong(v); |
| 130 | if (x == -1 && PyErr_Occurred()) { |
| 131 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 132 | PyErr_SetString(StructError, |
| 133 | "required argument is not an integer"); |
| 134 | return -1; |
| 135 | } |
| 136 | *p = x; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* Same, but handling unsigned long */ |
| 142 | |
| 143 | static int |
| 144 | get_ulong(PyObject *v, unsigned long *p) |
| 145 | { |
| 146 | if (PyLong_Check(v)) { |
| 147 | unsigned long x = PyLong_AsUnsignedLong(v); |
| 148 | if (x == (unsigned long)(-1) && PyErr_Occurred()) |
| 149 | return -1; |
| 150 | *p = x; |
| 151 | return 0; |
| 152 | } |
| 153 | else { |
| 154 | return get_long(v, (long *)p); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #ifdef HAVE_LONG_LONG |
| 159 | |
| 160 | /* Same, but handling native long long. */ |
| 161 | |
| 162 | static int |
| 163 | get_longlong(PyObject *v, PY_LONG_LONG *p) |
| 164 | { |
| 165 | PY_LONG_LONG x; |
| 166 | |
| 167 | v = get_pylong(v); |
| 168 | if (v == NULL) |
| 169 | return -1; |
| 170 | assert(PyLong_Check(v)); |
| 171 | x = PyLong_AsLongLong(v); |
| 172 | Py_DECREF(v); |
| 173 | if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 174 | return -1; |
| 175 | *p = x; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* Same, but handling native unsigned long long. */ |
| 180 | |
| 181 | static int |
| 182 | get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) |
| 183 | { |
| 184 | unsigned PY_LONG_LONG x; |
| 185 | |
| 186 | v = get_pylong(v); |
| 187 | if (v == NULL) |
| 188 | return -1; |
| 189 | assert(PyLong_Check(v)); |
| 190 | x = PyLong_AsUnsignedLongLong(v); |
| 191 | Py_DECREF(v); |
| 192 | if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 193 | return -1; |
| 194 | *p = x; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | #endif |
| 199 | |
| 200 | /* Floating point helpers */ |
| 201 | |
| 202 | static PyObject * |
| 203 | unpack_float(const char *p, /* start of 4-byte string */ |
| 204 | int le) /* true for little-endian, false for big-endian */ |
| 205 | { |
| 206 | double x; |
| 207 | |
| 208 | x = _PyFloat_Unpack4((unsigned char *)p, le); |
| 209 | if (x == -1.0 && PyErr_Occurred()) |
| 210 | return NULL; |
| 211 | return PyFloat_FromDouble(x); |
| 212 | } |
| 213 | |
| 214 | static PyObject * |
| 215 | unpack_double(const char *p, /* start of 8-byte string */ |
| 216 | int le) /* true for little-endian, false for big-endian */ |
| 217 | { |
| 218 | double x; |
| 219 | |
| 220 | x = _PyFloat_Unpack8((unsigned char *)p, le); |
| 221 | if (x == -1.0 && PyErr_Occurred()) |
| 222 | return NULL; |
| 223 | return PyFloat_FromDouble(x); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* A large number of small routines follow, with names of the form |
| 228 | |
| 229 | [bln][up]_TYPE |
| 230 | |
| 231 | [bln] distiguishes among big-endian, little-endian and native. |
| 232 | [pu] distiguishes between pack (to struct) and unpack (from struct). |
| 233 | TYPE is one of char, byte, ubyte, etc. |
| 234 | */ |
| 235 | |
| 236 | /* Native mode routines. ****************************************************/ |
| 237 | /* NOTE: |
| 238 | In all n[up]_<type> routines handling types larger than 1 byte, there is |
| 239 | *no* guarantee that the p pointer is properly aligned for each type, |
| 240 | therefore memcpy is called. An intermediate variable is used to |
| 241 | compensate for big-endian architectures. |
| 242 | Normally both the intermediate variable and the memcpy call will be |
| 243 | skipped by C optimisation in little-endian architectures (gcc >= 2.91 |
| 244 | does this). */ |
| 245 | |
| 246 | static PyObject * |
| 247 | nu_char(const char *p, const formatdef *f) |
| 248 | { |
| 249 | return PyString_FromStringAndSize(p, 1); |
| 250 | } |
| 251 | |
| 252 | static PyObject * |
| 253 | nu_byte(const char *p, const formatdef *f) |
| 254 | { |
| 255 | return PyInt_FromLong((long) *(signed char *)p); |
| 256 | } |
| 257 | |
| 258 | static PyObject * |
| 259 | nu_ubyte(const char *p, const formatdef *f) |
| 260 | { |
| 261 | return PyInt_FromLong((long) *(unsigned char *)p); |
| 262 | } |
| 263 | |
| 264 | static PyObject * |
| 265 | nu_short(const char *p, const formatdef *f) |
| 266 | { |
| 267 | short x; |
| 268 | memcpy((char *)&x, p, sizeof x); |
| 269 | return PyInt_FromLong((long)x); |
| 270 | } |
| 271 | |
| 272 | static PyObject * |
| 273 | nu_ushort(const char *p, const formatdef *f) |
| 274 | { |
| 275 | unsigned short x; |
| 276 | memcpy((char *)&x, p, sizeof x); |
| 277 | return PyInt_FromLong((long)x); |
| 278 | } |
| 279 | |
| 280 | static PyObject * |
| 281 | nu_int(const char *p, const formatdef *f) |
| 282 | { |
| 283 | int x; |
| 284 | memcpy((char *)&x, p, sizeof x); |
| 285 | return PyInt_FromLong((long)x); |
| 286 | } |
| 287 | |
| 288 | static PyObject * |
| 289 | nu_uint(const char *p, const formatdef *f) |
| 290 | { |
| 291 | unsigned int x; |
| 292 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 293 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 294 | if (x <= INT_MAX) |
| 295 | return PyInt_FromLong((long)x); |
| 296 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 297 | return PyLong_FromUnsignedLong((unsigned long)x); |
| 298 | } |
| 299 | |
| 300 | static PyObject * |
| 301 | nu_long(const char *p, const formatdef *f) |
| 302 | { |
| 303 | long x; |
| 304 | memcpy((char *)&x, p, sizeof x); |
| 305 | return PyInt_FromLong(x); |
| 306 | } |
| 307 | |
| 308 | static PyObject * |
| 309 | nu_ulong(const char *p, const formatdef *f) |
| 310 | { |
| 311 | unsigned long x; |
| 312 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 313 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 314 | if (x <= INT_MAX) |
| 315 | return PyInt_FromLong((long)x); |
| 316 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 317 | return PyLong_FromUnsignedLong(x); |
| 318 | } |
| 319 | |
| 320 | /* Native mode doesn't support q or Q unless the platform C supports |
| 321 | long long (or, on Windows, __int64). */ |
| 322 | |
| 323 | #ifdef HAVE_LONG_LONG |
| 324 | |
| 325 | static PyObject * |
| 326 | nu_longlong(const char *p, const formatdef *f) |
| 327 | { |
| 328 | PY_LONG_LONG x; |
| 329 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 330 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 331 | if (x >= INT_MIN && x <= INT_MAX) |
| 332 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
| 333 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 334 | return PyLong_FromLongLong(x); |
| 335 | } |
| 336 | |
| 337 | static PyObject * |
| 338 | nu_ulonglong(const char *p, const formatdef *f) |
| 339 | { |
| 340 | unsigned PY_LONG_LONG x; |
| 341 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 342 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 343 | if (x <= INT_MAX) |
| 344 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
| 345 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 346 | return PyLong_FromUnsignedLongLong(x); |
| 347 | } |
| 348 | |
| 349 | #endif |
| 350 | |
| 351 | static PyObject * |
| 352 | nu_float(const char *p, const formatdef *f) |
| 353 | { |
| 354 | float x; |
| 355 | memcpy((char *)&x, p, sizeof x); |
| 356 | return PyFloat_FromDouble((double)x); |
| 357 | } |
| 358 | |
| 359 | static PyObject * |
| 360 | nu_double(const char *p, const formatdef *f) |
| 361 | { |
| 362 | double x; |
| 363 | memcpy((char *)&x, p, sizeof x); |
| 364 | return PyFloat_FromDouble(x); |
| 365 | } |
| 366 | |
| 367 | static PyObject * |
| 368 | nu_void_p(const char *p, const formatdef *f) |
| 369 | { |
| 370 | void *x; |
| 371 | memcpy((char *)&x, p, sizeof x); |
| 372 | return PyLong_FromVoidPtr(x); |
| 373 | } |
| 374 | |
| 375 | static int |
| 376 | np_byte(char *p, PyObject *v, const formatdef *f) |
| 377 | { |
| 378 | long x; |
| 379 | if (get_long(v, &x) < 0) |
| 380 | return -1; |
| 381 | if (x < -128 || x > 127){ |
| 382 | PyErr_SetString(StructError, |
| 383 | "byte format requires -128<=number<=127"); |
| 384 | return -1; |
| 385 | } |
| 386 | *p = (char)x; |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int |
| 391 | np_ubyte(char *p, PyObject *v, const formatdef *f) |
| 392 | { |
| 393 | long x; |
| 394 | if (get_long(v, &x) < 0) |
| 395 | return -1; |
| 396 | if (x < 0 || x > 255){ |
| 397 | PyErr_SetString(StructError, |
| 398 | "ubyte format requires 0<=number<=255"); |
| 399 | return -1; |
| 400 | } |
| 401 | *p = (char)x; |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int |
| 406 | np_char(char *p, PyObject *v, const formatdef *f) |
| 407 | { |
| 408 | if (!PyString_Check(v) || PyString_Size(v) != 1) { |
| 409 | PyErr_SetString(StructError, |
| 410 | "char format require string of length 1"); |
| 411 | return -1; |
| 412 | } |
| 413 | *p = *PyString_AsString(v); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int |
| 418 | np_short(char *p, PyObject *v, const formatdef *f) |
| 419 | { |
| 420 | long x; |
| 421 | short y; |
| 422 | if (get_long(v, &x) < 0) |
| 423 | return -1; |
| 424 | if (x < SHRT_MIN || x > SHRT_MAX){ |
| 425 | PyErr_SetString(StructError, |
| 426 | "short format requires " STRINGIFY(SHRT_MIN) |
| 427 | "<=number<=" STRINGIFY(SHRT_MAX)); |
| 428 | return -1; |
| 429 | } |
| 430 | y = (short)x; |
| 431 | memcpy(p, (char *)&y, sizeof y); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int |
| 436 | np_ushort(char *p, PyObject *v, const formatdef *f) |
| 437 | { |
| 438 | long x; |
| 439 | unsigned short y; |
| 440 | if (get_long(v, &x) < 0) |
| 441 | return -1; |
| 442 | if (x < 0 || x > USHRT_MAX){ |
| 443 | PyErr_SetString(StructError, |
| 444 | "short format requires 0<=number<=" STRINGIFY(USHRT_MAX)); |
| 445 | return -1; |
| 446 | } |
| 447 | y = (unsigned short)x; |
| 448 | memcpy(p, (char *)&y, sizeof y); |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int |
| 453 | np_int(char *p, PyObject *v, const formatdef *f) |
| 454 | { |
| 455 | long x; |
| 456 | int y; |
| 457 | if (get_long(v, &x) < 0) |
| 458 | return -1; |
| 459 | y = (int)x; |
| 460 | memcpy(p, (char *)&y, sizeof y); |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int |
| 465 | np_uint(char *p, PyObject *v, const formatdef *f) |
| 466 | { |
| 467 | unsigned long x; |
| 468 | unsigned int y; |
| 469 | if (get_ulong(v, &x) < 0) |
| 470 | return -1; |
| 471 | y = (unsigned int)x; |
| 472 | memcpy(p, (char *)&y, sizeof y); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int |
| 477 | np_long(char *p, PyObject *v, const formatdef *f) |
| 478 | { |
| 479 | long x; |
| 480 | if (get_long(v, &x) < 0) |
| 481 | return -1; |
| 482 | memcpy(p, (char *)&x, sizeof x); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int |
| 487 | np_ulong(char *p, PyObject *v, const formatdef *f) |
| 488 | { |
| 489 | unsigned long x; |
| 490 | if (get_ulong(v, &x) < 0) |
| 491 | return -1; |
| 492 | memcpy(p, (char *)&x, sizeof x); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | #ifdef HAVE_LONG_LONG |
| 497 | |
| 498 | static int |
| 499 | np_longlong(char *p, PyObject *v, const formatdef *f) |
| 500 | { |
| 501 | PY_LONG_LONG x; |
| 502 | if (get_longlong(v, &x) < 0) |
| 503 | return -1; |
| 504 | memcpy(p, (char *)&x, sizeof x); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int |
| 509 | np_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 510 | { |
| 511 | unsigned PY_LONG_LONG x; |
| 512 | if (get_ulonglong(v, &x) < 0) |
| 513 | return -1; |
| 514 | memcpy(p, (char *)&x, sizeof x); |
| 515 | return 0; |
| 516 | } |
| 517 | #endif |
| 518 | |
| 519 | static int |
| 520 | np_float(char *p, PyObject *v, const formatdef *f) |
| 521 | { |
| 522 | float x = (float)PyFloat_AsDouble(v); |
| 523 | if (x == -1 && PyErr_Occurred()) { |
| 524 | PyErr_SetString(StructError, |
| 525 | "required argument is not a float"); |
| 526 | return -1; |
| 527 | } |
| 528 | memcpy(p, (char *)&x, sizeof x); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int |
| 533 | np_double(char *p, PyObject *v, const formatdef *f) |
| 534 | { |
| 535 | double x = PyFloat_AsDouble(v); |
| 536 | if (x == -1 && PyErr_Occurred()) { |
| 537 | PyErr_SetString(StructError, |
| 538 | "required argument is not a float"); |
| 539 | return -1; |
| 540 | } |
| 541 | memcpy(p, (char *)&x, sizeof(double)); |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int |
| 546 | np_void_p(char *p, PyObject *v, const formatdef *f) |
| 547 | { |
| 548 | void *x; |
| 549 | |
| 550 | v = get_pylong(v); |
| 551 | if (v == NULL) |
| 552 | return -1; |
| 553 | assert(PyLong_Check(v)); |
| 554 | x = PyLong_AsVoidPtr(v); |
| 555 | Py_DECREF(v); |
| 556 | if (x == NULL && PyErr_Occurred()) |
| 557 | return -1; |
| 558 | memcpy(p, (char *)&x, sizeof x); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static formatdef native_table[] = { |
| 563 | {'x', sizeof(char), 0, NULL}, |
| 564 | {'b', sizeof(char), 0, nu_byte, np_byte}, |
| 565 | {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
| 566 | {'c', sizeof(char), 0, nu_char, np_char}, |
| 567 | {'s', sizeof(char), 0, NULL}, |
| 568 | {'p', sizeof(char), 0, NULL}, |
| 569 | {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
| 570 | {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, |
| 571 | {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
| 572 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
| 573 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
| 574 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
| 575 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 576 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
| 577 | {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
| 578 | #ifdef HAVE_LONG_LONG |
| 579 | {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, |
| 580 | {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, |
| 581 | #endif |
| 582 | {0} |
| 583 | }; |
| 584 | |
| 585 | /* Big-endian routines. *****************************************************/ |
| 586 | |
| 587 | static PyObject * |
| 588 | bu_int(const char *p, const formatdef *f) |
| 589 | { |
| 590 | long x = 0; |
| 591 | int i = f->size; |
| 592 | do { |
| 593 | x = (x<<8) | (*p++ & 0xFF); |
| 594 | } while (--i > 0); |
| 595 | /* Extend the sign bit. */ |
| 596 | if (SIZEOF_LONG > f->size) |
| 597 | x |= -(x & (1L << (8*f->size - 1))); |
| 598 | return PyInt_FromLong(x); |
| 599 | } |
| 600 | |
| 601 | static PyObject * |
| 602 | bu_uint(const char *p, const formatdef *f) |
| 603 | { |
| 604 | unsigned long x = 0; |
| 605 | int i = f->size; |
| 606 | do { |
| 607 | x = (x<<8) | (*p++ & 0xFF); |
| 608 | } while (--i > 0); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 609 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 610 | if (x <= INT_MAX) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 611 | return PyInt_FromLong((long)x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 612 | #endif |
| 613 | return PyLong_FromUnsignedLong(x); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static PyObject * |
| 617 | bu_longlong(const char *p, const formatdef *f) |
| 618 | { |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 619 | #if HAVE_LONG_LONG |
| 620 | PY_LONG_LONG x = 0; |
| 621 | int i = f->size; |
| 622 | do { |
| 623 | x = (x<<8) | (*p++ & 0xFF); |
| 624 | } while (--i > 0); |
| 625 | /* Extend the sign bit. */ |
| 626 | if (SIZEOF_LONG_LONG > f->size) |
| 627 | x |= -(x & (1L << (8 * f->size - 1))); |
| 628 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 629 | if (x >= INT_MIN && x <= INT_MAX) |
| 630 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
| 631 | #endif |
| 632 | return PyLong_FromLongLong(x); |
| 633 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 634 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 635 | 8, |
| 636 | 0, /* little-endian */ |
| 637 | 1 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 638 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static PyObject * |
| 642 | bu_ulonglong(const char *p, const formatdef *f) |
| 643 | { |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 644 | #if HAVE_LONG_LONG |
| 645 | unsigned PY_LONG_LONG x = 0; |
| 646 | int i = f->size; |
| 647 | do { |
| 648 | x = (x<<8) | (*p++ & 0xFF); |
| 649 | } while (--i > 0); |
| 650 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 651 | if (x <= INT_MAX) |
| 652 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
| 653 | #endif |
| 654 | return PyLong_FromUnsignedLongLong(x); |
| 655 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 656 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 657 | 8, |
| 658 | 0, /* little-endian */ |
| 659 | 0 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 660 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static PyObject * |
| 664 | bu_float(const char *p, const formatdef *f) |
| 665 | { |
| 666 | return unpack_float(p, 0); |
| 667 | } |
| 668 | |
| 669 | static PyObject * |
| 670 | bu_double(const char *p, const formatdef *f) |
| 671 | { |
| 672 | return unpack_double(p, 0); |
| 673 | } |
| 674 | |
| 675 | static int |
| 676 | bp_int(char *p, PyObject *v, const formatdef *f) |
| 677 | { |
| 678 | long x; |
| 679 | int i; |
| 680 | if (get_long(v, &x) < 0) |
| 681 | return -1; |
| 682 | i = f->size; |
| 683 | do { |
| 684 | p[--i] = (char)x; |
| 685 | x >>= 8; |
| 686 | } while (i > 0); |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static int |
| 691 | bp_uint(char *p, PyObject *v, const formatdef *f) |
| 692 | { |
| 693 | unsigned long x; |
| 694 | int i; |
| 695 | if (get_ulong(v, &x) < 0) |
| 696 | return -1; |
| 697 | i = f->size; |
| 698 | do { |
| 699 | p[--i] = (char)x; |
| 700 | x >>= 8; |
| 701 | } while (i > 0); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int |
| 706 | bp_longlong(char *p, PyObject *v, const formatdef *f) |
| 707 | { |
| 708 | int res; |
| 709 | v = get_pylong(v); |
| 710 | if (v == NULL) |
| 711 | return -1; |
| 712 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 713 | (unsigned char *)p, |
| 714 | 8, |
| 715 | 0, /* little_endian */ |
| 716 | 1 /* signed */); |
| 717 | Py_DECREF(v); |
| 718 | return res; |
| 719 | } |
| 720 | |
| 721 | static int |
| 722 | bp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 723 | { |
| 724 | int res; |
| 725 | v = get_pylong(v); |
| 726 | if (v == NULL) |
| 727 | return -1; |
| 728 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 729 | (unsigned char *)p, |
| 730 | 8, |
| 731 | 0, /* little_endian */ |
| 732 | 0 /* signed */); |
| 733 | Py_DECREF(v); |
| 734 | return res; |
| 735 | } |
| 736 | |
| 737 | static int |
| 738 | bp_float(char *p, PyObject *v, const formatdef *f) |
| 739 | { |
| 740 | double x = PyFloat_AsDouble(v); |
| 741 | if (x == -1 && PyErr_Occurred()) { |
| 742 | PyErr_SetString(StructError, |
| 743 | "required argument is not a float"); |
| 744 | return -1; |
| 745 | } |
| 746 | return _PyFloat_Pack4(x, (unsigned char *)p, 0); |
| 747 | } |
| 748 | |
| 749 | static int |
| 750 | bp_double(char *p, PyObject *v, const formatdef *f) |
| 751 | { |
| 752 | double x = PyFloat_AsDouble(v); |
| 753 | if (x == -1 && PyErr_Occurred()) { |
| 754 | PyErr_SetString(StructError, |
| 755 | "required argument is not a float"); |
| 756 | return -1; |
| 757 | } |
| 758 | return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
| 759 | } |
| 760 | |
| 761 | static formatdef bigendian_table[] = { |
| 762 | {'x', 1, 0, NULL}, |
| 763 | {'b', 1, 0, bu_int, bp_int}, |
| 764 | {'B', 1, 0, bu_uint, bp_int}, |
| 765 | {'c', 1, 0, nu_char, np_char}, |
| 766 | {'s', 1, 0, NULL}, |
| 767 | {'p', 1, 0, NULL}, |
| 768 | {'h', 2, 0, bu_int, bp_int}, |
| 769 | {'H', 2, 0, bu_uint, bp_uint}, |
| 770 | {'i', 4, 0, bu_int, bp_int}, |
| 771 | {'I', 4, 0, bu_uint, bp_uint}, |
| 772 | {'l', 4, 0, bu_int, bp_int}, |
| 773 | {'L', 4, 0, bu_uint, bp_uint}, |
| 774 | {'q', 8, 0, bu_longlong, bp_longlong}, |
| 775 | {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, |
| 776 | {'f', 4, 0, bu_float, bp_float}, |
| 777 | {'d', 8, 0, bu_double, bp_double}, |
| 778 | {0} |
| 779 | }; |
| 780 | |
| 781 | /* Little-endian routines. *****************************************************/ |
| 782 | |
| 783 | static PyObject * |
| 784 | lu_int(const char *p, const formatdef *f) |
| 785 | { |
| 786 | long x = 0; |
| 787 | int i = f->size; |
| 788 | do { |
| 789 | x = (x<<8) | (p[--i] & 0xFF); |
| 790 | } while (i > 0); |
| 791 | /* Extend the sign bit. */ |
| 792 | if (SIZEOF_LONG > f->size) |
| 793 | x |= -(x & (1L << (8*f->size - 1))); |
| 794 | return PyInt_FromLong(x); |
| 795 | } |
| 796 | |
| 797 | static PyObject * |
| 798 | lu_uint(const char *p, const formatdef *f) |
| 799 | { |
| 800 | unsigned long x = 0; |
| 801 | int i = f->size; |
| 802 | do { |
| 803 | x = (x<<8) | (p[--i] & 0xFF); |
| 804 | } while (i > 0); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 805 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 806 | if (x <= INT_MAX) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 807 | return PyInt_FromLong((long)x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 808 | #endif |
| 809 | return PyLong_FromUnsignedLong((long)x); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static PyObject * |
| 813 | lu_longlong(const char *p, const formatdef *f) |
| 814 | { |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 815 | #if HAVE_LONG_LONG |
| 816 | PY_LONG_LONG x = 0; |
| 817 | int i = f->size; |
| 818 | do { |
| 819 | x = (x<<8) | (p[--i] & 0xFF); |
| 820 | } while (i > 0); |
| 821 | /* Extend the sign bit. */ |
| 822 | if (SIZEOF_LONG_LONG > f->size) |
| 823 | x |= -(x & (1L << (8 * f->size - 1))); |
| 824 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 825 | if (x >= INT_MIN && x <= INT_MAX) |
| 826 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
| 827 | #endif |
| 828 | return PyLong_FromLongLong(x); |
| 829 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 830 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 831 | 8, |
| 832 | 1, /* little-endian */ |
| 833 | 1 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 834 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static PyObject * |
| 838 | lu_ulonglong(const char *p, const formatdef *f) |
| 839 | { |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 840 | #if HAVE_LONG_LONG |
| 841 | unsigned PY_LONG_LONG x = 0; |
| 842 | int i = f->size; |
| 843 | do { |
| 844 | x = (x<<8) | (p[--i] & 0xFF); |
| 845 | } while (i > 0); |
| 846 | #ifdef PY_USE_INT_WHEN_POSSIBLE |
| 847 | if (x <= INT_MAX) |
| 848 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
| 849 | #endif |
| 850 | return PyLong_FromUnsignedLongLong(x); |
| 851 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 852 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 853 | 8, |
| 854 | 1, /* little-endian */ |
| 855 | 0 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 856 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static PyObject * |
| 860 | lu_float(const char *p, const formatdef *f) |
| 861 | { |
| 862 | return unpack_float(p, 1); |
| 863 | } |
| 864 | |
| 865 | static PyObject * |
| 866 | lu_double(const char *p, const formatdef *f) |
| 867 | { |
| 868 | return unpack_double(p, 1); |
| 869 | } |
| 870 | |
| 871 | static int |
| 872 | lp_int(char *p, PyObject *v, const formatdef *f) |
| 873 | { |
| 874 | long x; |
| 875 | int i; |
| 876 | if (get_long(v, &x) < 0) |
| 877 | return -1; |
| 878 | i = f->size; |
| 879 | do { |
| 880 | *p++ = (char)x; |
| 881 | x >>= 8; |
| 882 | } while (--i > 0); |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static int |
| 887 | lp_uint(char *p, PyObject *v, const formatdef *f) |
| 888 | { |
| 889 | unsigned long x; |
| 890 | int i; |
| 891 | if (get_ulong(v, &x) < 0) |
| 892 | return -1; |
| 893 | i = f->size; |
| 894 | do { |
| 895 | *p++ = (char)x; |
| 896 | x >>= 8; |
| 897 | } while (--i > 0); |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | static int |
| 902 | lp_longlong(char *p, PyObject *v, const formatdef *f) |
| 903 | { |
| 904 | int res; |
| 905 | v = get_pylong(v); |
| 906 | if (v == NULL) |
| 907 | return -1; |
| 908 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 909 | (unsigned char *)p, |
| 910 | 8, |
| 911 | 1, /* little_endian */ |
| 912 | 1 /* signed */); |
| 913 | Py_DECREF(v); |
| 914 | return res; |
| 915 | } |
| 916 | |
| 917 | static int |
| 918 | lp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 919 | { |
| 920 | int res; |
| 921 | v = get_pylong(v); |
| 922 | if (v == NULL) |
| 923 | return -1; |
| 924 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 925 | (unsigned char *)p, |
| 926 | 8, |
| 927 | 1, /* little_endian */ |
| 928 | 0 /* signed */); |
| 929 | Py_DECREF(v); |
| 930 | return res; |
| 931 | } |
| 932 | |
| 933 | static int |
| 934 | lp_float(char *p, PyObject *v, const formatdef *f) |
| 935 | { |
| 936 | double x = PyFloat_AsDouble(v); |
| 937 | if (x == -1 && PyErr_Occurred()) { |
| 938 | PyErr_SetString(StructError, |
| 939 | "required argument is not a float"); |
| 940 | return -1; |
| 941 | } |
| 942 | return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
| 943 | } |
| 944 | |
| 945 | static int |
| 946 | lp_double(char *p, PyObject *v, const formatdef *f) |
| 947 | { |
| 948 | double x = PyFloat_AsDouble(v); |
| 949 | if (x == -1 && PyErr_Occurred()) { |
| 950 | PyErr_SetString(StructError, |
| 951 | "required argument is not a float"); |
| 952 | return -1; |
| 953 | } |
| 954 | return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
| 955 | } |
| 956 | |
| 957 | static formatdef lilendian_table[] = { |
| 958 | {'x', 1, 0, NULL}, |
| 959 | {'b', 1, 0, lu_int, lp_int}, |
| 960 | {'B', 1, 0, lu_uint, lp_int}, |
| 961 | {'c', 1, 0, nu_char, np_char}, |
| 962 | {'s', 1, 0, NULL}, |
| 963 | {'p', 1, 0, NULL}, |
| 964 | {'h', 2, 0, lu_int, lp_int}, |
| 965 | {'H', 2, 0, lu_uint, lp_uint}, |
| 966 | {'i', 4, 0, lu_int, lp_int}, |
| 967 | {'I', 4, 0, lu_uint, lp_uint}, |
| 968 | {'l', 4, 0, lu_int, lp_int}, |
| 969 | {'L', 4, 0, lu_uint, lp_uint}, |
| 970 | {'q', 8, 0, lu_longlong, lp_longlong}, |
| 971 | {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, |
| 972 | {'f', 4, 0, lu_float, lp_float}, |
| 973 | {'d', 8, 0, lu_double, lp_double}, |
| 974 | {0} |
| 975 | }; |
| 976 | |
| 977 | |
| 978 | static const formatdef * |
| 979 | whichtable(char **pfmt) |
| 980 | { |
| 981 | const char *fmt = (*pfmt)++; /* May be backed out of later */ |
| 982 | switch (*fmt) { |
| 983 | case '<': |
| 984 | return lilendian_table; |
| 985 | case '>': |
| 986 | case '!': /* Network byte order is big-endian */ |
| 987 | return bigendian_table; |
| 988 | case '=': { /* Host byte order -- different from native in aligment! */ |
| 989 | int n = 1; |
| 990 | char *p = (char *) &n; |
| 991 | if (*p == 1) |
| 992 | return lilendian_table; |
| 993 | else |
| 994 | return bigendian_table; |
| 995 | } |
| 996 | default: |
| 997 | --*pfmt; /* Back out of pointer increment */ |
| 998 | /* Fall through */ |
| 999 | case '@': |
| 1000 | return native_table; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | /* Get the table entry for a format code */ |
| 1006 | |
| 1007 | static const formatdef * |
| 1008 | getentry(int c, const formatdef *f) |
| 1009 | { |
| 1010 | for (; f->format != '\0'; f++) { |
| 1011 | if (f->format == c) { |
| 1012 | return f; |
| 1013 | } |
| 1014 | } |
| 1015 | PyErr_SetString(StructError, "bad char in struct format"); |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | /* Align a size according to a format code */ |
| 1021 | |
| 1022 | static int |
| 1023 | align(int size, int c, const formatdef *e) |
| 1024 | { |
| 1025 | if (e->format == c) { |
| 1026 | if (e->alignment) { |
| 1027 | size = ((size + e->alignment - 1) |
| 1028 | / e->alignment) |
| 1029 | * e->alignment; |
| 1030 | } |
| 1031 | } |
| 1032 | return size; |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | /* calculate the size of a format string */ |
| 1037 | |
| 1038 | static int |
| 1039 | prepare_s(PyStructObject *self) |
| 1040 | { |
| 1041 | const formatdef *f; |
| 1042 | const formatdef *e; |
| 1043 | formatcode *codes; |
| 1044 | |
| 1045 | const char *s; |
| 1046 | const char *fmt; |
| 1047 | char c; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1048 | int size, len, num, itemsize, x; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1049 | |
| 1050 | fmt = PyString_AS_STRING(self->s_format); |
| 1051 | |
| 1052 | f = whichtable((char **)&fmt); |
| 1053 | |
| 1054 | s = fmt; |
| 1055 | size = 0; |
| 1056 | len = 0; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1057 | while ((c = *s++) != '\0') { |
| 1058 | if (isspace(Py_CHARMASK(c))) |
| 1059 | continue; |
| 1060 | if ('0' <= c && c <= '9') { |
| 1061 | num = c - '0'; |
| 1062 | while ('0' <= (c = *s++) && c <= '9') { |
| 1063 | x = num*10 + (c - '0'); |
| 1064 | if (x/10 != num) { |
| 1065 | PyErr_SetString( |
| 1066 | StructError, |
| 1067 | "overflow in item count"); |
| 1068 | return -1; |
| 1069 | } |
| 1070 | num = x; |
| 1071 | } |
| 1072 | if (c == '\0') |
| 1073 | break; |
| 1074 | } |
| 1075 | else |
| 1076 | num = 1; |
| 1077 | |
| 1078 | e = getentry(c, f); |
| 1079 | if (e == NULL) |
| 1080 | return -1; |
| 1081 | |
| 1082 | switch (c) { |
| 1083 | case 's': /* fall through */ |
| 1084 | case 'p': len++; break; |
| 1085 | case 'x': break; |
| 1086 | default: len += num; break; |
| 1087 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1088 | |
| 1089 | itemsize = e->size; |
| 1090 | size = align(size, c, e); |
| 1091 | x = num * itemsize; |
| 1092 | size += x; |
| 1093 | if (x/itemsize != num || size < 0) { |
| 1094 | PyErr_SetString(StructError, |
| 1095 | "total struct size too long"); |
| 1096 | return -1; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | self->s_size = size; |
| 1101 | self->s_len = len; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1102 | codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1103 | if (codes == NULL) { |
| 1104 | PyErr_NoMemory(); |
| 1105 | return -1; |
| 1106 | } |
| 1107 | self->s_codes = codes; |
| 1108 | |
| 1109 | s = fmt; |
| 1110 | size = 0; |
| 1111 | while ((c = *s++) != '\0') { |
| 1112 | if (isspace(Py_CHARMASK(c))) |
| 1113 | continue; |
| 1114 | if ('0' <= c && c <= '9') { |
| 1115 | num = c - '0'; |
| 1116 | while ('0' <= (c = *s++) && c <= '9') |
| 1117 | num = num*10 + (c - '0'); |
| 1118 | if (c == '\0') |
| 1119 | break; |
| 1120 | } |
| 1121 | else |
| 1122 | num = 1; |
| 1123 | |
| 1124 | e = getentry(c, f); |
| 1125 | |
| 1126 | size = align(size, c, e); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1127 | if (c == 's' || c == 'p') { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1128 | codes->offset = size; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1129 | codes->size = num; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1130 | codes->fmtdef = e; |
| 1131 | codes++; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1132 | size += num; |
| 1133 | } else if (c == 'x') { |
| 1134 | size += num; |
| 1135 | } else { |
| 1136 | while (--num >= 0) { |
| 1137 | codes->offset = size; |
| 1138 | codes->size = e->size; |
| 1139 | codes->fmtdef = e; |
| 1140 | codes++; |
| 1141 | size += e->size; |
| 1142 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1143 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1144 | } |
| 1145 | codes->fmtdef = NULL; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1146 | codes->offset = size; |
| 1147 | codes->size = 0; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | static PyObject * |
| 1153 | s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1154 | { |
| 1155 | PyObject *self; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1156 | |
| 1157 | assert(type != NULL && type->tp_alloc != NULL); |
| 1158 | |
| 1159 | self = type->tp_alloc(type, 0); |
| 1160 | if (self != NULL) { |
| 1161 | PyStructObject *s = (PyStructObject*)self; |
| 1162 | Py_INCREF(Py_None); |
| 1163 | s->s_format = Py_None; |
| 1164 | s->s_codes = NULL; |
| 1165 | s->s_size = -1; |
| 1166 | s->s_len = -1; |
| 1167 | } |
| 1168 | return self; |
| 1169 | } |
| 1170 | |
| 1171 | static int |
| 1172 | s_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1173 | { |
| 1174 | PyStructObject *soself = (PyStructObject *)self; |
| 1175 | PyObject *o_format = NULL; |
| 1176 | int ret = 0; |
| 1177 | static char *kwlist[] = {"format", 0}; |
| 1178 | |
| 1179 | assert(PyStruct_Check(self)); |
| 1180 | |
| 1181 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist, |
| 1182 | &o_format)) |
| 1183 | return -1; |
| 1184 | |
| 1185 | Py_INCREF(o_format); |
| 1186 | Py_XDECREF(soself->s_format); |
| 1187 | soself->s_format = o_format; |
| 1188 | |
| 1189 | ret = prepare_s(soself); |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | static void |
| 1194 | s_dealloc(PyStructObject *s) |
| 1195 | { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1196 | if (s->weakreflist != NULL) |
| 1197 | PyObject_ClearWeakRefs((PyObject *)s); |
| 1198 | if (s->s_codes != NULL) { |
| 1199 | PyMem_FREE(s->s_codes); |
| 1200 | } |
| 1201 | Py_XDECREF(s->s_format); |
| 1202 | s->ob_type->tp_free((PyObject *)s); |
| 1203 | } |
| 1204 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1205 | static PyObject * |
| 1206 | s_unpack_internal(PyStructObject *soself, char *startfrom) { |
| 1207 | formatcode *code; |
| 1208 | Py_ssize_t i = 0; |
| 1209 | PyObject *result = PyTuple_New(soself->s_len); |
| 1210 | if (result == NULL) |
| 1211 | return NULL; |
| 1212 | |
| 1213 | for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
| 1214 | PyObject *v; |
| 1215 | const formatdef *e = code->fmtdef; |
| 1216 | const char *res = startfrom + code->offset; |
| 1217 | if (e->format == 's') { |
| 1218 | v = PyString_FromStringAndSize(res, code->size); |
| 1219 | if (v == NULL) |
| 1220 | goto fail; |
| 1221 | PyTuple_SET_ITEM(result, i++, v); |
| 1222 | } else if (e->format == 'p') { |
| 1223 | Py_ssize_t n = *(unsigned char*)res; |
| 1224 | if (n >= code->size) |
| 1225 | n = code->size - 1; |
| 1226 | v = PyString_FromStringAndSize(res + 1, n); |
| 1227 | if (v == NULL) |
| 1228 | goto fail; |
| 1229 | PyTuple_SET_ITEM(result, i++, v); |
| 1230 | } else { |
| 1231 | v = e->unpack(res, e); |
| 1232 | if (v == NULL) |
| 1233 | goto fail; |
| 1234 | PyTuple_SET_ITEM(result, i++, v); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return result; |
| 1239 | fail: |
| 1240 | Py_DECREF(result); |
| 1241 | return NULL; |
| 1242 | }; |
| 1243 | |
| 1244 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1245 | PyDoc_STRVAR(s_unpack__doc__, |
| 1246 | "unpack(str) -> (v1, v2, ...)\n\ |
| 1247 | \n\ |
| 1248 | Return tuple containing values unpacked according to this Struct's format.\n\ |
| 1249 | Requires len(str) == self.size. See struct.__doc__ for more on format\n\ |
| 1250 | strings."); |
| 1251 | |
| 1252 | static PyObject * |
| 1253 | s_unpack(PyObject *self, PyObject *inputstr) |
| 1254 | { |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1255 | PyStructObject *soself = (PyStructObject *)self; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1256 | assert(PyStruct_Check(self)); |
| 1257 | assert(soself->s_codes != NULL); |
| 1258 | if (inputstr == NULL || !PyString_Check(inputstr) || |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1259 | PyString_GET_SIZE(inputstr) != soself->s_size) { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1260 | PyErr_Format(StructError, |
| 1261 | "unpack requires a string argument of length %d", soself->s_size); |
| 1262 | return NULL; |
| 1263 | } |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1264 | return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); |
| 1265 | } |
| 1266 | |
| 1267 | PyDoc_STRVAR(s_unpack_from__doc__, |
| 1268 | "unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\ |
| 1269 | \n\ |
| 1270 | Return tuple containing values unpacked according to this Struct's format.\n\ |
| 1271 | Unlike unpack, unpack_from can unpack values from any object supporting\n\ |
| 1272 | the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\ |
| 1273 | See struct.__doc__ for more on format strings."); |
| 1274 | |
| 1275 | static PyObject * |
| 1276 | s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
| 1277 | { |
| 1278 | static char *kwlist[] = {"buffer", "offset", 0}; |
| 1279 | #if (PY_VERSION_HEX < 0x02050000) |
| 1280 | static char *fmt = "z#|i:unpack_from"; |
| 1281 | #else |
| 1282 | static char *fmt = "z#|n:unpack_from"; |
| 1283 | #endif |
| 1284 | Py_ssize_t buffer_len = 0, offset = 0; |
| 1285 | char *buffer = NULL; |
| 1286 | PyStructObject *soself = (PyStructObject *)self; |
| 1287 | assert(PyStruct_Check(self)); |
| 1288 | assert(soself->s_codes != NULL); |
| 1289 | |
| 1290 | if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, |
| 1291 | &buffer, &buffer_len, &offset)) |
| 1292 | return NULL; |
| 1293 | |
| 1294 | if (buffer == NULL) { |
| 1295 | PyErr_Format(StructError, |
| 1296 | "unpack_from requires a buffer argument"); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1297 | return NULL; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1298 | } |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (offset < 0) |
| 1301 | offset += buffer_len; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1302 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1303 | if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
| 1304 | PyErr_Format(StructError, |
| 1305 | "unpack_from requires a buffer of at least %d bytes", |
| 1306 | soself->s_size); |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | return s_unpack_internal(soself, buffer + offset); |
| 1310 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1311 | |
| 1312 | PyDoc_STRVAR(s_pack__doc__, |
| 1313 | "pack(v1, v2, ...) -> string\n\ |
| 1314 | \n\ |
| 1315 | Return a string containing values v1, v2, ... packed according to this\n\ |
| 1316 | Struct's format. See struct.__doc__ for more on format strings."); |
| 1317 | |
| 1318 | static PyObject * |
| 1319 | s_pack(PyObject *self, PyObject *args) |
| 1320 | { |
| 1321 | PyStructObject *soself; |
| 1322 | PyObject *result; |
| 1323 | char *restart; |
| 1324 | formatcode *code; |
| 1325 | Py_ssize_t i; |
| 1326 | |
| 1327 | soself = (PyStructObject *)self; |
| 1328 | assert(PyStruct_Check(self)); |
| 1329 | assert(soself->s_codes != NULL); |
| 1330 | if (args == NULL || !PyTuple_Check(args) || |
| 1331 | PyTuple_GET_SIZE(args) != soself->s_len) |
| 1332 | { |
| 1333 | PyErr_Format(StructError, |
| 1334 | "pack requires exactly %d arguments", soself->s_len); |
| 1335 | return NULL; |
| 1336 | } |
| 1337 | |
| 1338 | result = PyString_FromStringAndSize((char *)NULL, soself->s_size); |
| 1339 | if (result == NULL) |
| 1340 | return NULL; |
| 1341 | |
| 1342 | restart = PyString_AS_STRING(result); |
| 1343 | memset(restart, '\0', soself->s_size); |
| 1344 | i = 0; |
| 1345 | for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
| 1346 | Py_ssize_t n; |
| 1347 | PyObject *v; |
| 1348 | const formatdef *e = code->fmtdef; |
| 1349 | char *res = restart + code->offset; |
| 1350 | if (e->format == 's') { |
| 1351 | v = PyTuple_GET_ITEM(args, i++); |
| 1352 | if (!PyString_Check(v)) { |
| 1353 | PyErr_SetString(StructError, |
| 1354 | "argument for 's' must be a string"); |
| 1355 | goto fail; |
| 1356 | } |
| 1357 | n = PyString_GET_SIZE(v); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1358 | if (n > code->size) |
| 1359 | n = code->size; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1360 | if (n > 0) |
| 1361 | memcpy(res, PyString_AS_STRING(v), n); |
| 1362 | } else if (e->format == 'p') { |
| 1363 | v = PyTuple_GET_ITEM(args, i++); |
| 1364 | if (!PyString_Check(v)) { |
| 1365 | PyErr_SetString(StructError, |
| 1366 | "argument for 'p' must be a string"); |
| 1367 | goto fail; |
| 1368 | } |
| 1369 | n = PyString_GET_SIZE(v); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1370 | if (n > (code->size - 1)) |
| 1371 | n = code->size - 1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1372 | if (n > 0) |
| 1373 | memcpy(res + 1, PyString_AS_STRING(v), n); |
| 1374 | if (n > 255) |
| 1375 | n = 255; |
| 1376 | *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); |
| 1377 | } else { |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1378 | v = PyTuple_GET_ITEM(args, i++); |
| 1379 | if (e->pack(res, v, e) < 0) |
| 1380 | goto fail; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | return result; |
| 1385 | |
| 1386 | fail: |
| 1387 | Py_DECREF(result); |
| 1388 | return NULL; |
| 1389 | |
| 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | /* List of functions */ |
| 1394 | |
| 1395 | static struct PyMethodDef s_methods[] = { |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1396 | {"pack", (PyCFunction)s_pack, METH_VARARGS, s_pack__doc__}, |
| 1397 | {"unpack", (PyCFunction)s_unpack, METH_O, s_unpack__doc__}, |
| 1398 | {"unpack_from", (PyCFunction)s_unpack_from, METH_KEYWORDS, s_unpack_from__doc__}, |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1399 | {NULL, NULL} /* sentinel */ |
| 1400 | }; |
| 1401 | |
| 1402 | PyDoc_STRVAR(s__doc__, "Compiled struct object"); |
| 1403 | |
| 1404 | #define OFF(x) offsetof(PyStructObject, x) |
| 1405 | |
| 1406 | static PyMemberDef s_memberlist[] = { |
| 1407 | {"format", T_OBJECT, OFF(s_format), RO, |
| 1408 | "struct format string"}, |
| 1409 | {"size", T_INT, OFF(s_size), RO, |
| 1410 | "struct size in bytes"}, |
| 1411 | {"_len", T_INT, OFF(s_len), RO, |
| 1412 | "number of items expected in tuple"}, |
| 1413 | {NULL} /* Sentinel */ |
| 1414 | }; |
| 1415 | |
| 1416 | |
| 1417 | static |
| 1418 | PyTypeObject PyStructType = { |
Bob Ippolito | 3fc2bb9 | 2006-05-25 19:03:19 +0000 | [diff] [blame^] | 1419 | PyObject_HEAD_INIT(NULL) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1420 | 0, |
| 1421 | "Struct", |
| 1422 | sizeof(PyStructObject), |
| 1423 | 0, |
| 1424 | (destructor)s_dealloc, /* tp_dealloc */ |
| 1425 | 0, /* tp_print */ |
| 1426 | 0, /* tp_getattr */ |
| 1427 | 0, /* tp_setattr */ |
| 1428 | 0, /* tp_compare */ |
| 1429 | 0, /* tp_repr */ |
| 1430 | 0, /* tp_as_number */ |
| 1431 | 0, /* tp_as_sequence */ |
| 1432 | 0, /* tp_as_mapping */ |
| 1433 | 0, /* tp_hash */ |
| 1434 | 0, /* tp_call */ |
| 1435 | 0, /* tp_str */ |
| 1436 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1437 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 1438 | 0, /* tp_as_buffer */ |
| 1439 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 1440 | s__doc__, /* tp_doc */ |
| 1441 | 0, /* tp_traverse */ |
| 1442 | 0, /* tp_clear */ |
| 1443 | 0, /* tp_richcompare */ |
| 1444 | offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */ |
| 1445 | 0, /* tp_iter */ |
| 1446 | 0, /* tp_iternext */ |
| 1447 | s_methods, /* tp_methods */ |
| 1448 | s_memberlist, /* tp_members */ |
| 1449 | 0, /* tp_getset */ |
| 1450 | 0, /* tp_base */ |
| 1451 | 0, /* tp_dict */ |
| 1452 | 0, /* tp_descr_get */ |
| 1453 | 0, /* tp_descr_set */ |
| 1454 | 0, /* tp_dictoffset */ |
| 1455 | s_init, /* tp_init */ |
| 1456 | PyType_GenericAlloc, /* tp_alloc */ |
| 1457 | s_new, /* tp_new */ |
| 1458 | PyObject_Del, /* tp_free */ |
| 1459 | }; |
| 1460 | |
| 1461 | /* Module initialization */ |
| 1462 | |
| 1463 | PyMODINIT_FUNC |
| 1464 | init_struct(void) |
| 1465 | { |
| 1466 | PyObject *m = Py_InitModule("_struct", NULL); |
| 1467 | if (m == NULL) |
| 1468 | return; |
| 1469 | |
Bob Ippolito | 3fc2bb9 | 2006-05-25 19:03:19 +0000 | [diff] [blame^] | 1470 | PyStructType.ob_type = &PyType_Type; |
| 1471 | if (PyType_Ready(&PyStructType) < 0) |
| 1472 | return; |
| 1473 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1474 | /* Add some symbolic constants to the module */ |
| 1475 | if (StructError == NULL) { |
| 1476 | StructError = PyErr_NewException("struct.error", NULL, NULL); |
| 1477 | if (StructError == NULL) |
| 1478 | return; |
| 1479 | } |
| 1480 | Py_INCREF(StructError); |
| 1481 | PyModule_AddObject(m, "error", StructError); |
| 1482 | Py_INCREF((PyObject*)&PyStructType); |
| 1483 | PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); |
| 1484 | } |