Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* struct module -- pack values into and (out of) strings */ |
| 33 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 34 | /* New version supporting byte order, alignment and size options, |
| 35 | character strings, and unsigned numbers */ |
| 36 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 37 | #include "Python.h" |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 39 | #include <limits.h> |
| 40 | |
| 41 | |
| 42 | /* Exception */ |
| 43 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 44 | static PyObject *StructError; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | /* Define various structs to figure out the alignments of types */ |
| 48 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 49 | #ifdef __MWERKS__ |
| 50 | /* |
| 51 | ** XXXX We have a problem here. There are no unique alignment rules |
| 52 | ** on the PowerPC mac. |
| 53 | */ |
| 54 | #ifdef __powerc |
| 55 | #pragma options align=mac68k |
| 56 | #endif |
| 57 | #endif /* __MWERKS__ */ |
| 58 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 59 | typedef struct { char c; short x; } s_short; |
| 60 | typedef struct { char c; int x; } s_int; |
| 61 | typedef struct { char c; long x; } s_long; |
| 62 | typedef struct { char c; float x; } s_float; |
| 63 | typedef struct { char c; double x; } s_double; |
| 64 | |
| 65 | #define SHORT_ALIGN (sizeof(s_short) - sizeof(short)) |
| 66 | #define INT_ALIGN (sizeof(s_int) - sizeof(int)) |
| 67 | #define LONG_ALIGN (sizeof(s_long) - sizeof(long)) |
| 68 | #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float)) |
| 69 | #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double)) |
| 70 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 71 | #ifdef __powerc |
| 72 | #pragma options align=reset |
| 73 | #endif |
| 74 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 76 | /* Global that will contain 2**<bits-per-long> */ |
| 77 | |
| 78 | static PyObject *offset = NULL; |
| 79 | |
| 80 | |
| 81 | /* Helper to create 2**<bits-per-long> */ |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 82 | /* XXX This assumes 2's complement arithmetic */ |
| 83 | |
| 84 | static PyObject * |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 85 | init_offset() |
| 86 | { |
| 87 | PyObject *result = NULL; |
| 88 | PyObject *one = PyLong_FromLong(1L); |
| 89 | PyObject *shiftcount = PyLong_FromLong(8 * sizeof(long)); |
| 90 | if (one == NULL || shiftcount == NULL) |
| 91 | goto finally; |
| 92 | result = PyNumber_Lshift(one, shiftcount); |
| 93 | finally: |
| 94 | Py_XDECREF(one); |
| 95 | Py_XDECREF(shiftcount); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /* Helper to add offset to a number */ |
| 101 | |
| 102 | static PyObject * |
| 103 | add_offset(v) |
| 104 | PyObject *v; |
| 105 | { |
| 106 | PyObject *result = NULL; |
| 107 | if (offset == NULL) { |
| 108 | if ((offset = init_offset()) == NULL) |
| 109 | goto finally; |
| 110 | } |
| 111 | result = PyNumber_Add(v, offset); |
| 112 | finally: |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /* Same, but subtracting */ |
| 118 | |
| 119 | static PyObject * |
| 120 | sub_offset(v) |
| 121 | PyObject *v; |
| 122 | { |
| 123 | PyObject *result = NULL; |
| 124 | if (offset == NULL) { |
| 125 | if ((offset = init_offset()) == NULL) |
| 126 | goto finally; |
| 127 | } |
| 128 | result = PyNumber_Subtract(v, offset); |
| 129 | finally: |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* Helper routine to turn a (signed) long into an unsigned long */ |
| 135 | |
| 136 | static PyObject * |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 137 | make_ulong(x) |
| 138 | long x; |
| 139 | { |
| 140 | PyObject *v = PyLong_FromLong(x); |
| 141 | if (x < 0 && v != NULL) { |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 142 | PyObject *w = add_offset(v); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 143 | Py_DECREF(v); |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 144 | return w; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | return v; |
| 148 | } |
| 149 | |
| 150 | /* Helper routine to get a Python integer and raise the appropriate error |
| 151 | if it isn't one */ |
| 152 | |
| 153 | static int |
| 154 | get_long(v, p) |
| 155 | PyObject *v; |
| 156 | long *p; |
| 157 | { |
| 158 | long x = PyInt_AsLong(v); |
| 159 | if (x == -1 && PyErr_Occurred()) { |
| 160 | if (PyErr_Occurred() == PyExc_TypeError) |
| 161 | PyErr_SetString(StructError, |
| 162 | "required argument is not an integer"); |
| 163 | return -1; |
| 164 | } |
| 165 | *p = x; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 170 | /* Same, but handling unsigned long */ |
| 171 | |
| 172 | static int |
| 173 | get_ulong(v, p) |
| 174 | PyObject *v; |
| 175 | unsigned long *p; |
| 176 | { |
| 177 | long x = PyInt_AsLong(v); |
| 178 | PyObject *exc; |
| 179 | if (x == -1 && (exc = PyErr_Occurred()) != NULL) { |
| 180 | if (exc == PyExc_OverflowError) { |
| 181 | /* Try again after subtracting offset */ |
| 182 | PyObject *w; |
| 183 | PyErr_Clear(); |
| 184 | if ((w = sub_offset(v)) == NULL) |
| 185 | return -1; |
| 186 | x = PyInt_AsLong(w); |
| 187 | Py_DECREF(w); |
| 188 | if (x != -1 || (exc = PyErr_Occurred()) == NULL) |
| 189 | goto okay; |
| 190 | } |
| 191 | if (exc == PyExc_TypeError) |
| 192 | PyErr_SetString(StructError, |
| 193 | "required argument is not an integer"); |
| 194 | else |
| 195 | return -1; |
| 196 | } |
| 197 | okay: |
| 198 | *p = x; |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 203 | /* The translation function for each format character is table driven */ |
| 204 | |
| 205 | typedef struct _formatdef { |
| 206 | char format; |
| 207 | int size; |
| 208 | int alignment; |
| 209 | PyObject* (*unpack) Py_PROTO((const char *, |
| 210 | const struct _formatdef *)); |
| 211 | int (*pack) Py_PROTO((char *, |
| 212 | PyObject *, |
| 213 | const struct _formatdef *)); |
| 214 | } formatdef; |
| 215 | |
| 216 | static PyObject * |
| 217 | nu_char(p, f) |
| 218 | const char *p; |
| 219 | const formatdef *f; |
| 220 | { |
| 221 | return PyString_FromStringAndSize(p, 1); |
| 222 | } |
| 223 | |
| 224 | static PyObject * |
| 225 | nu_byte(p, f) |
| 226 | const char *p; |
| 227 | const formatdef *f; |
| 228 | { |
| 229 | return PyInt_FromLong((long) *(signed char *)p); |
| 230 | } |
| 231 | |
| 232 | static PyObject * |
| 233 | nu_ubyte(p, f) |
| 234 | const char *p; |
| 235 | const formatdef *f; |
| 236 | { |
| 237 | return PyInt_FromLong((long) *(unsigned char *)p); |
| 238 | } |
| 239 | |
| 240 | static PyObject * |
| 241 | nu_short(p, f) |
| 242 | const char *p; |
| 243 | const formatdef *f; |
| 244 | { |
| 245 | return PyInt_FromLong((long) *(short *)p); |
| 246 | } |
| 247 | |
| 248 | static PyObject * |
| 249 | nu_ushort(p, f) |
| 250 | const char *p; |
| 251 | const formatdef *f; |
| 252 | { |
| 253 | return PyInt_FromLong((long) *(unsigned short *)p); |
| 254 | } |
| 255 | |
| 256 | static PyObject * |
| 257 | nu_int(p, f) |
| 258 | const char *p; |
| 259 | const formatdef *f; |
| 260 | { |
| 261 | return PyInt_FromLong((long) *(int *)p); |
| 262 | } |
| 263 | |
| 264 | static PyObject * |
| 265 | nu_uint(p, f) |
| 266 | const char *p; |
| 267 | const formatdef *f; |
| 268 | { |
| 269 | unsigned int x = *(unsigned int *)p; |
| 270 | #if INT_MAX == LONG_MAX |
| 271 | return make_ulong((long)x); |
| 272 | #else |
| 273 | return PyInt_FromLong((long)x); |
| 274 | #endif |
| 275 | } |
| 276 | |
| 277 | static PyObject * |
| 278 | nu_long(p, f) |
| 279 | const char *p; |
| 280 | const formatdef *f; |
| 281 | { |
| 282 | return PyInt_FromLong(*(long *)p); |
| 283 | } |
| 284 | |
| 285 | static PyObject * |
| 286 | nu_ulong(p, f) |
| 287 | const char *p; |
| 288 | const formatdef *f; |
| 289 | { |
| 290 | return make_ulong(*(long *)p); |
| 291 | } |
| 292 | |
| 293 | static PyObject * |
| 294 | nu_float(p, f) |
| 295 | const char *p; |
| 296 | const formatdef *f; |
| 297 | { |
| 298 | float x; |
| 299 | memcpy((char *)&x, p, sizeof(float)); |
| 300 | return PyFloat_FromDouble((double)x); |
| 301 | } |
| 302 | |
| 303 | static PyObject * |
| 304 | nu_double(p, f) |
| 305 | const char *p; |
| 306 | const formatdef *f; |
| 307 | { |
| 308 | double x; |
| 309 | memcpy((char *)&x, p, sizeof(double)); |
| 310 | return PyFloat_FromDouble(x); |
| 311 | } |
| 312 | |
| 313 | static int |
| 314 | np_byte(p, v, f) |
| 315 | char *p; |
| 316 | PyObject *v; |
| 317 | const formatdef *f; |
| 318 | { |
| 319 | long x; |
| 320 | if (get_long(v, &x) < 0) |
| 321 | return -1; |
| 322 | *p = x; |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int |
| 327 | np_char(p, v, f) |
| 328 | char *p; |
| 329 | PyObject *v; |
| 330 | const formatdef *f; |
| 331 | { |
| 332 | if (!PyString_Check(v) || PyString_Size(v) != 1) { |
| 333 | PyErr_SetString(StructError, |
| 334 | "char format require string of length 1"); |
| 335 | return -1; |
| 336 | } |
| 337 | *p = *PyString_AsString(v); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int |
| 342 | np_short(p, v, f) |
| 343 | char *p; |
| 344 | PyObject *v; |
| 345 | const formatdef *f; |
| 346 | { |
| 347 | long x; |
| 348 | if (get_long(v, &x) < 0) |
| 349 | return -1; |
| 350 | * (short *)p = x; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int |
| 355 | np_int(p, v, f) |
| 356 | char *p; |
| 357 | PyObject *v; |
| 358 | const formatdef *f; |
| 359 | { |
| 360 | long x; |
| 361 | if (get_long(v, &x) < 0) |
| 362 | return -1; |
| 363 | * (int *)p = x; |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 368 | np_uint(p, v, f) |
| 369 | char *p; |
| 370 | PyObject *v; |
| 371 | const formatdef *f; |
| 372 | { |
| 373 | unsigned long x; |
| 374 | if (get_ulong(v, &x) < 0) |
| 375 | return -1; |
| 376 | * (unsigned int *)p = x; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 381 | np_long(p, v, f) |
| 382 | char *p; |
| 383 | PyObject *v; |
| 384 | const formatdef *f; |
| 385 | { |
| 386 | long x; |
| 387 | if (get_long(v, &x) < 0) |
| 388 | return -1; |
| 389 | * (long *)p = x; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 394 | np_ulong(p, v, f) |
| 395 | char *p; |
| 396 | PyObject *v; |
| 397 | const formatdef *f; |
| 398 | { |
| 399 | unsigned long x; |
| 400 | if (get_ulong(v, &x) < 0) |
| 401 | return -1; |
| 402 | * (unsigned long *)p = x; |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 407 | np_float(p, v, f) |
| 408 | char *p; |
| 409 | PyObject *v; |
| 410 | const formatdef *f; |
| 411 | { |
| 412 | float x = (float)PyFloat_AsDouble(v); |
| 413 | if (x == -1 && PyErr_Occurred()) { |
| 414 | PyErr_SetString(StructError, |
| 415 | "required argument is not a float"); |
| 416 | return -1; |
| 417 | } |
| 418 | memcpy(p, (char *)&x, sizeof(float)); |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int |
| 423 | np_double(p, v, f) |
| 424 | char *p; |
| 425 | PyObject *v; |
| 426 | const formatdef *f; |
| 427 | { |
| 428 | double x = PyFloat_AsDouble(v); |
| 429 | if (x == -1 && PyErr_Occurred()) { |
| 430 | PyErr_SetString(StructError, |
| 431 | "required argument is not a float"); |
| 432 | return -1; |
| 433 | } |
| 434 | memcpy(p, (char *)&x, sizeof(double)); |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static formatdef native_table[] = { |
| 439 | {'x', sizeof(char), 0, NULL}, |
| 440 | {'b', sizeof(char), 0, nu_byte, np_byte}, |
| 441 | {'B', sizeof(char), 0, nu_ubyte, np_byte}, |
| 442 | {'c', sizeof(char), 0, nu_char, np_char}, |
| 443 | {'s', sizeof(char), 0, NULL}, |
| 444 | {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
| 445 | {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_short}, |
| 446 | {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 447 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 448 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 449 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 450 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 451 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
| 452 | {0} |
| 453 | }; |
| 454 | |
| 455 | static PyObject * |
| 456 | bu_int(p, f) |
| 457 | const char *p; |
| 458 | const formatdef *f; |
| 459 | { |
| 460 | long x = 0; |
| 461 | int i = f->size; |
| 462 | do { |
| 463 | x = (x<<8) | (*p++ & 0xFF); |
| 464 | } while (--i > 0); |
| 465 | i = 8*(sizeof(long) - f->size); |
| 466 | if (i) { |
| 467 | x <<= i; |
| 468 | x >>= i; |
| 469 | } |
| 470 | return PyInt_FromLong(x); |
| 471 | } |
| 472 | |
| 473 | static PyObject * |
| 474 | bu_uint(p, f) |
| 475 | const char *p; |
| 476 | const formatdef *f; |
| 477 | { |
| 478 | long x = 0; |
| 479 | int i = f->size; |
| 480 | do { |
| 481 | x = (x<<8) | (*p++ & 0xFF); |
| 482 | } while (--i > 0); |
| 483 | if (f->size == sizeof(long)) |
| 484 | return make_ulong(x); |
| 485 | else |
| 486 | return PyLong_FromLong(x); |
| 487 | } |
| 488 | |
| 489 | static int |
| 490 | bp_int(p, v, f) |
| 491 | char *p; |
| 492 | PyObject *v; |
| 493 | const formatdef *f; |
| 494 | { |
| 495 | long x; |
| 496 | int i; |
| 497 | if (get_long(v, &x) < 0) |
| 498 | return -1; |
| 499 | i = f->size; |
| 500 | do { |
| 501 | p[--i] = x; |
| 502 | x >>= 8; |
| 503 | } while (i > 0); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 507 | static int |
| 508 | bp_uint(p, v, f) |
| 509 | char *p; |
| 510 | PyObject *v; |
| 511 | const formatdef *f; |
| 512 | { |
| 513 | unsigned long x; |
| 514 | int i; |
| 515 | if (get_ulong(v, &x) < 0) |
| 516 | return -1; |
| 517 | i = f->size; |
| 518 | do { |
| 519 | p[--i] = x; |
| 520 | x >>= 8; |
| 521 | } while (i > 0); |
| 522 | return 0; |
| 523 | } |
| 524 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 525 | static formatdef bigendian_table[] = { |
| 526 | {'x', 1, 0, NULL}, |
| 527 | {'b', 1, 0, bu_int, bp_int}, |
| 528 | {'B', 1, 0, bu_uint, bp_int}, |
| 529 | {'c', 1, 0, nu_char, np_char}, |
| 530 | {'s', 1, 0, NULL}, |
| 531 | {'h', 2, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 532 | {'H', 2, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 533 | {'i', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 534 | {'I', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 535 | {'l', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 536 | {'L', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 537 | /* No float and double! */ |
| 538 | {0} |
| 539 | }; |
| 540 | |
| 541 | static PyObject * |
| 542 | lu_int(p, f) |
| 543 | const char *p; |
| 544 | const formatdef *f; |
| 545 | { |
| 546 | long x = 0; |
| 547 | int i = f->size; |
| 548 | do { |
| 549 | x = (x<<8) | (p[--i] & 0xFF); |
| 550 | } while (i > 0); |
| 551 | i = 8*(sizeof(long) - f->size); |
| 552 | if (i) { |
| 553 | x <<= i; |
| 554 | x >>= i; |
| 555 | } |
| 556 | return PyInt_FromLong(x); |
| 557 | } |
| 558 | |
| 559 | static PyObject * |
| 560 | lu_uint(p, f) |
| 561 | const char *p; |
| 562 | const formatdef *f; |
| 563 | { |
| 564 | long x = 0; |
| 565 | int i = f->size; |
| 566 | do { |
| 567 | x = (x<<8) | (p[--i] & 0xFF); |
| 568 | } while (i > 0); |
| 569 | if (f->size == sizeof(long)) |
| 570 | return make_ulong(x); |
| 571 | else |
| 572 | return PyLong_FromLong(x); |
| 573 | } |
| 574 | |
| 575 | static int |
| 576 | lp_int(p, v, f) |
| 577 | char *p; |
| 578 | PyObject *v; |
| 579 | const formatdef *f; |
| 580 | { |
| 581 | long x; |
| 582 | int i; |
| 583 | if (get_long(v, &x) < 0) |
| 584 | return -1; |
| 585 | i = f->size; |
| 586 | do { |
| 587 | *p++ = x; |
| 588 | x >>= 8; |
| 589 | } while (--i > 0); |
| 590 | return 0; |
| 591 | } |
| 592 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 593 | static int |
| 594 | lp_uint(p, v, f) |
| 595 | char *p; |
| 596 | PyObject *v; |
| 597 | const formatdef *f; |
| 598 | { |
| 599 | unsigned long x; |
| 600 | int i; |
| 601 | if (get_ulong(v, &x) < 0) |
| 602 | return -1; |
| 603 | i = f->size; |
| 604 | do { |
| 605 | *p++ = x; |
| 606 | x >>= 8; |
| 607 | } while (--i > 0); |
| 608 | return 0; |
| 609 | } |
| 610 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 611 | static formatdef lilendian_table[] = { |
| 612 | {'x', 1, 0, NULL}, |
| 613 | {'b', 1, 0, lu_int, lp_int}, |
| 614 | {'B', 1, 0, lu_uint, lp_int}, |
| 615 | {'c', 1, 0, nu_char, np_char}, |
| 616 | {'s', 1, 0, NULL}, |
| 617 | {'h', 2, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 618 | {'H', 2, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 619 | {'i', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 620 | {'I', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 621 | {'l', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame^] | 622 | {'L', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 623 | /* No float and double! */ |
| 624 | {0} |
| 625 | }; |
| 626 | |
| 627 | |
| 628 | static const formatdef * |
| 629 | whichtable(pfmt) |
| 630 | const char **pfmt; |
| 631 | { |
| 632 | const char *fmt = (*pfmt)++; /* May be backed out of later */ |
| 633 | switch (*fmt) { |
| 634 | case '<': |
| 635 | return lilendian_table; |
| 636 | case '>': |
| 637 | case '!': /* Network byte order is big-endian */ |
| 638 | return bigendian_table; |
| 639 | case '=': { /* Host byte order -- different from native in aligment! */ |
| 640 | int n = 1; |
| 641 | char *p = (char *) &n; |
| 642 | if (*p == 1) |
| 643 | return lilendian_table; |
| 644 | else |
| 645 | return bigendian_table; |
| 646 | } |
| 647 | default: |
| 648 | --*pfmt; /* Back out of pointer increment */ |
| 649 | /* Fall through */ |
| 650 | case '@': |
| 651 | return native_table; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /* Get the table entry for a format code */ |
| 657 | |
| 658 | static const formatdef * |
| 659 | getentry(c, f) |
| 660 | int c; |
| 661 | const formatdef *f; |
| 662 | { |
| 663 | for (; f->format != '\0'; f++) { |
| 664 | if (f->format == c) { |
| 665 | return f; |
| 666 | } |
| 667 | } |
| 668 | PyErr_SetString(StructError, "bad char in struct format"); |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 673 | /* Align a size according to a format code */ |
| 674 | |
| 675 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 676 | align(size, c, e) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 677 | int size; |
| 678 | int c; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 679 | const formatdef *e; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 680 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 681 | if (e->format == c) { |
| 682 | if (e->alignment) { |
| 683 | size = ((size + e->alignment - 1) |
| 684 | / e->alignment) |
| 685 | * e->alignment; |
| 686 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 687 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 688 | return size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
| 692 | /* calculate the size of a format string */ |
| 693 | |
| 694 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 695 | calcsize(fmt, f) |
| 696 | const char *fmt; |
| 697 | const formatdef *f; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 698 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 699 | const formatdef *e; |
| 700 | const char *s; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 701 | char c; |
| 702 | int size, num, itemsize, x; |
| 703 | |
| 704 | s = fmt; |
| 705 | size = 0; |
| 706 | while ((c = *s++) != '\0') { |
| 707 | if ('0' <= c && c <= '9') { |
| 708 | num = c - '0'; |
| 709 | while ('0' <= (c = *s++) && c <= '9') { |
| 710 | x = num*10 + (c - '0'); |
| 711 | if (x/10 != num) { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 712 | PyErr_SetString( |
| 713 | StructError, |
| 714 | "overflow in item count"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 715 | return -1; |
| 716 | } |
| 717 | num = x; |
| 718 | } |
| 719 | if (c == '\0') |
| 720 | break; |
| 721 | } |
| 722 | else |
| 723 | num = 1; |
| 724 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 725 | e = getentry(c, f); |
| 726 | if (e == NULL) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 727 | return -1; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 728 | itemsize = e->size; |
| 729 | size = align(size, c, e); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 730 | x = num * itemsize; |
| 731 | size += x; |
| 732 | if (x/itemsize != num || size < 0) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 733 | PyErr_SetString(StructError, |
| 734 | "total struct size too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 735 | return -1; |
| 736 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | return size; |
| 740 | } |
| 741 | |
| 742 | |
| 743 | /* pack(fmt, v1, v2, ...) --> string */ |
| 744 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 745 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 746 | struct_calcsize(self, args) |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 747 | PyObject *self; /* Not used */ |
| 748 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 749 | { |
| 750 | char *fmt; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 751 | const formatdef *f; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 752 | int size; |
| 753 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 754 | if (!PyArg_ParseTuple(args, "s", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 755 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 756 | f = whichtable(&fmt); |
| 757 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 758 | if (size < 0) |
| 759 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 760 | return PyInt_FromLong((long)size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | |
| 764 | /* pack(fmt, v1, v2, ...) --> string */ |
| 765 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 766 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 767 | struct_pack(self, args) |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 768 | PyObject *self; /* Not used */ |
| 769 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 770 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 771 | const formatdef *f, *e; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 772 | PyObject *format, *result, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 773 | char *fmt; |
| 774 | int size, num; |
| 775 | int i, n; |
| 776 | char *s, *res, *restart; |
| 777 | char c; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 778 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 779 | if (args == NULL || !PyTuple_Check(args) || |
| 780 | (n = PyTuple_Size(args)) < 1) |
| 781 | { |
| 782 | PyErr_BadArgument(); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 783 | return NULL; |
| 784 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 785 | format = PyTuple_GetItem(args, 0); |
| 786 | if (!PyArg_Parse(format, "s", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 787 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 788 | f = whichtable(&fmt); |
| 789 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 790 | if (size < 0) |
| 791 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 792 | result = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 793 | if (result == NULL) |
| 794 | return NULL; |
| 795 | |
| 796 | s = fmt; |
| 797 | i = 1; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 798 | res = restart = PyString_AsString(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 799 | |
| 800 | while ((c = *s++) != '\0') { |
| 801 | if ('0' <= c && c <= '9') { |
| 802 | num = c - '0'; |
| 803 | while ('0' <= (c = *s++) && c <= '9') |
| 804 | num = num*10 + (c - '0'); |
| 805 | if (c == '\0') |
| 806 | break; |
| 807 | } |
| 808 | else |
| 809 | num = 1; |
| 810 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 811 | e = getentry(c, f); |
| 812 | if (e == NULL) |
| 813 | goto fail; |
| 814 | res = restart + align((int)(res-restart), c, e); |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 815 | if (num == 0 && c != 's') |
| 816 | continue; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 817 | do { |
| 818 | if (c == 'x') { |
| 819 | /* doesn't consume arguments */ |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 820 | memset(res, '\0', num); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 821 | res += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 822 | break; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 823 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 824 | if (i >= n) { |
| 825 | PyErr_SetString(StructError, |
| 826 | "insufficient arguments to pack"); |
| 827 | goto fail; |
| 828 | } |
| 829 | v = PyTuple_GetItem(args, i++); |
| 830 | if (v == NULL) |
| 831 | goto fail; |
| 832 | if (c == 's') { |
| 833 | /* num is string size, not repeat count */ |
| 834 | int n; |
| 835 | if (!PyString_Check(v)) { |
| 836 | PyErr_SetString(StructError, |
| 837 | "argument for 's' must be a string"); |
| 838 | goto fail; |
| 839 | } |
| 840 | n = PyString_Size(v); |
| 841 | if (n > num) |
| 842 | n = num; |
| 843 | if (n > 0) |
| 844 | memcpy(res, PyString_AsString(v), n); |
| 845 | if (n < num) |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 846 | memset(res+n, '\0', num-n); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 847 | res += num; |
| 848 | break; |
| 849 | } |
| 850 | else { |
| 851 | if (e->pack(res, v, e) < 0) |
| 852 | goto fail; |
| 853 | res += e->size; |
| 854 | } |
| 855 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | if (i < n) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 859 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 860 | "too many arguments for pack format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 861 | goto fail; |
| 862 | } |
| 863 | |
| 864 | return result; |
| 865 | |
| 866 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 867 | Py_DECREF(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | |
| 872 | /* unpack(fmt, string) --> (v1, v2, ...) */ |
| 873 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 874 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 875 | struct_unpack(self, args) |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 876 | PyObject *self; /* Not used */ |
| 877 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 878 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 879 | const formatdef *f, *e; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 880 | char *str, *start, *fmt, *s; |
| 881 | char c; |
| 882 | int len, size, num, x; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 883 | PyObject *res, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 884 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 885 | if (!PyArg_ParseTuple(args, "ss#", &fmt, &start, &len)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 886 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 887 | f = whichtable(&fmt); |
| 888 | size = calcsize(fmt, f); |
| 889 | if (size < 0) |
| 890 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 891 | if (size != len) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 892 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 893 | "unpack str size does not match format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 894 | return NULL; |
| 895 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 896 | res = PyList_New(0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 897 | if (res == NULL) |
| 898 | return NULL; |
| 899 | str = start; |
| 900 | s = fmt; |
| 901 | while ((c = *s++) != '\0') { |
| 902 | if ('0' <= c && c <= '9') { |
| 903 | num = c - '0'; |
| 904 | while ('0' <= (c = *s++) && c <= '9') |
| 905 | num = num*10 + (c - '0'); |
| 906 | if (c == '\0') |
| 907 | break; |
| 908 | } |
| 909 | else |
| 910 | num = 1; |
| 911 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 912 | e = getentry(c, f); |
| 913 | if (e == NULL) |
| 914 | goto fail; |
| 915 | str = start + align((int)(str-start), c, e); |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 916 | if (num == 0 && c != 's') |
| 917 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 918 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 919 | do { |
| 920 | if (c == 'x') { |
| 921 | str += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 922 | break; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 923 | } |
| 924 | if (c == 's') { |
| 925 | /* num is string size, not repeat count */ |
| 926 | v = PyString_FromStringAndSize(str, num); |
| 927 | if (v == NULL) |
| 928 | goto fail; |
| 929 | str += num; |
| 930 | num = 0; |
| 931 | } |
| 932 | else { |
| 933 | v = e->unpack(str, e); |
| 934 | if (v == NULL) |
| 935 | goto fail; |
| 936 | str += e->size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 937 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 938 | if (v == NULL || PyList_Append(res, v) < 0) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 939 | goto fail; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 940 | Py_DECREF(v); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 941 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 944 | v = PyList_AsTuple(res); |
| 945 | Py_DECREF(res); |
| 946 | return v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 947 | |
| 948 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 949 | Py_DECREF(res); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 950 | return NULL; |
| 951 | } |
| 952 | |
Guido van Rossum | 90ddb7b | 1992-08-19 16:44:15 +0000 | [diff] [blame] | 953 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 954 | /* List of functions */ |
| 955 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 956 | static PyMethodDef struct_methods[] = { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 957 | {"calcsize", struct_calcsize, METH_VARARGS}, |
| 958 | {"pack", struct_pack, METH_VARARGS}, |
| 959 | {"unpack", struct_unpack, METH_VARARGS}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 960 | {NULL, NULL} /* sentinel */ |
| 961 | }; |
| 962 | |
| 963 | |
| 964 | /* Module initialization */ |
| 965 | |
| 966 | void |
| 967 | initstruct() |
| 968 | { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 969 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 970 | |
| 971 | /* Create the module and add the functions */ |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 972 | m = Py_InitModule("struct", struct_methods); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 973 | |
| 974 | /* Add some symbolic constants to the module */ |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 975 | d = PyModule_GetDict(m); |
| 976 | StructError = PyString_FromString("struct.error"); |
| 977 | PyDict_SetItemString(d, "error", StructError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 978 | |
| 979 | /* Check for errors */ |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 980 | if (PyErr_Occurred()) |
| 981 | Py_FatalError("can't initialize module struct"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 982 | } |