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