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