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