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