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