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