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