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